Merge from trunk.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static bool line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static bool display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static bool message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static bool message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_max_ascent, last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 bool help_echo_showing_p;
578
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
584
585 #define TEXT_PROP_DISTANCE_LIMIT 100
586
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
601
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
609
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
612
613 void
614 redisplay_other_windows (void)
615 {
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
618 }
619
620 void
621 wset_redisplay (struct window *w)
622 {
623 /* Beware: selected_window can be nil during early stages. */
624 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
625 redisplay_other_windows ();
626 w->redisplay = true;
627 }
628
629 void
630 fset_redisplay (struct frame *f)
631 {
632 redisplay_other_windows ();
633 f->redisplay = true;
634 }
635
636 void
637 bset_redisplay (struct buffer *b)
638 {
639 int count = buffer_window_count (b);
640 if (count > 0)
641 {
642 /* ... it's visible in other window than selected, */
643 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
644 redisplay_other_windows ();
645 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
646 so that if we later set windows_or_buffers_changed, this buffer will
647 not be omitted. */
648 b->text->redisplay = true;
649 }
650 }
651
652 void
653 bset_update_mode_line (struct buffer *b)
654 {
655 if (!update_mode_lines)
656 update_mode_lines = REDISPLAY_SOME;
657 b->text->redisplay = true;
658 }
659
660 #ifdef GLYPH_DEBUG
661
662 /* Non-zero means print traces of redisplay if compiled with
663 GLYPH_DEBUG defined. */
664
665 int trace_redisplay_p;
666
667 #endif /* GLYPH_DEBUG */
668
669 #ifdef DEBUG_TRACE_MOVE
670 /* Non-zero means trace with TRACE_MOVE to stderr. */
671 int trace_move;
672
673 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
674 #else
675 #define TRACE_MOVE(x) (void) 0
676 #endif
677
678 static Lisp_Object Qauto_hscroll_mode;
679
680 /* Buffer being redisplayed -- for redisplay_window_error. */
681
682 static struct buffer *displayed_buffer;
683
684 /* Value returned from text property handlers (see below). */
685
686 enum prop_handled
687 {
688 HANDLED_NORMALLY,
689 HANDLED_RECOMPUTE_PROPS,
690 HANDLED_OVERLAY_STRING_CONSUMED,
691 HANDLED_RETURN
692 };
693
694 /* A description of text properties that redisplay is interested
695 in. */
696
697 struct props
698 {
699 /* The name of the property. */
700 Lisp_Object *name;
701
702 /* A unique index for the property. */
703 enum prop_idx idx;
704
705 /* A handler function called to set up iterator IT from the property
706 at IT's current position. Value is used to steer handle_stop. */
707 enum prop_handled (*handler) (struct it *it);
708 };
709
710 static enum prop_handled handle_face_prop (struct it *);
711 static enum prop_handled handle_invisible_prop (struct it *);
712 static enum prop_handled handle_display_prop (struct it *);
713 static enum prop_handled handle_composition_prop (struct it *);
714 static enum prop_handled handle_overlay_change (struct it *);
715 static enum prop_handled handle_fontified_prop (struct it *);
716
717 /* Properties handled by iterators. */
718
719 static struct props it_props[] =
720 {
721 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
722 /* Handle `face' before `display' because some sub-properties of
723 `display' need to know the face. */
724 {&Qface, FACE_PROP_IDX, handle_face_prop},
725 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
726 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
727 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
728 {NULL, 0, NULL}
729 };
730
731 /* Value is the position described by X. If X is a marker, value is
732 the marker_position of X. Otherwise, value is X. */
733
734 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
735
736 /* Enumeration returned by some move_it_.* functions internally. */
737
738 enum move_it_result
739 {
740 /* Not used. Undefined value. */
741 MOVE_UNDEFINED,
742
743 /* Move ended at the requested buffer position or ZV. */
744 MOVE_POS_MATCH_OR_ZV,
745
746 /* Move ended at the requested X pixel position. */
747 MOVE_X_REACHED,
748
749 /* Move within a line ended at the end of a line that must be
750 continued. */
751 MOVE_LINE_CONTINUED,
752
753 /* Move within a line ended at the end of a line that would
754 be displayed truncated. */
755 MOVE_LINE_TRUNCATED,
756
757 /* Move within a line ended at a line end. */
758 MOVE_NEWLINE_OR_CR
759 };
760
761 /* This counter is used to clear the face cache every once in a while
762 in redisplay_internal. It is incremented for each redisplay.
763 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
764 cleared. */
765
766 #define CLEAR_FACE_CACHE_COUNT 500
767 static int clear_face_cache_count;
768
769 /* Similarly for the image cache. */
770
771 #ifdef HAVE_WINDOW_SYSTEM
772 #define CLEAR_IMAGE_CACHE_COUNT 101
773 static int clear_image_cache_count;
774
775 /* Null glyph slice */
776 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
777 #endif
778
779 /* True while redisplay_internal is in progress. */
780
781 bool redisplaying_p;
782
783 static Lisp_Object Qinhibit_free_realized_faces;
784 static Lisp_Object Qmode_line_default_help_echo;
785
786 /* If a string, XTread_socket generates an event to display that string.
787 (The display is done in read_char.) */
788
789 Lisp_Object help_echo_string;
790 Lisp_Object help_echo_window;
791 Lisp_Object help_echo_object;
792 ptrdiff_t help_echo_pos;
793
794 /* Temporary variable for XTread_socket. */
795
796 Lisp_Object previous_help_echo_string;
797
798 /* Platform-independent portion of hourglass implementation. */
799
800 #ifdef HAVE_WINDOW_SYSTEM
801
802 /* Non-zero means an hourglass cursor is currently shown. */
803 bool hourglass_shown_p;
804
805 /* If non-null, an asynchronous timer that, when it expires, displays
806 an hourglass cursor on all frames. */
807 struct atimer *hourglass_atimer;
808
809 #endif /* HAVE_WINDOW_SYSTEM */
810
811 /* Name of the face used to display glyphless characters. */
812 static Lisp_Object Qglyphless_char;
813
814 /* Symbol for the purpose of Vglyphless_char_display. */
815 static Lisp_Object Qglyphless_char_display;
816
817 /* Method symbols for Vglyphless_char_display. */
818 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
819
820 /* Default number of seconds to wait before displaying an hourglass
821 cursor. */
822 #define DEFAULT_HOURGLASS_DELAY 1
823
824 #ifdef HAVE_WINDOW_SYSTEM
825
826 /* Default pixel width of `thin-space' display method. */
827 #define THIN_SPACE_WIDTH 1
828
829 #endif /* HAVE_WINDOW_SYSTEM */
830
831 /* Function prototypes. */
832
833 static void setup_for_ellipsis (struct it *, int);
834 static void set_iterator_to_next (struct it *, int);
835 static void mark_window_display_accurate_1 (struct window *, int);
836 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
837 static int display_prop_string_p (Lisp_Object, Lisp_Object);
838 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
839 static int cursor_row_p (struct glyph_row *);
840 static int redisplay_mode_lines (Lisp_Object, bool);
841 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
842
843 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
844
845 static void handle_line_prefix (struct it *);
846
847 static void pint2str (char *, int, ptrdiff_t);
848 static void pint2hrstr (char *, int, ptrdiff_t);
849 static struct text_pos run_window_scroll_functions (Lisp_Object,
850 struct text_pos);
851 static int text_outside_line_unchanged_p (struct window *,
852 ptrdiff_t, ptrdiff_t);
853 static void store_mode_line_noprop_char (char);
854 static int store_mode_line_noprop (const char *, int, int);
855 static void handle_stop (struct it *);
856 static void handle_stop_backwards (struct it *, ptrdiff_t);
857 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
858 static void ensure_echo_area_buffers (void);
859 static void unwind_with_echo_area_buffer (Lisp_Object);
860 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
861 static int with_echo_area_buffer (struct window *, int,
862 int (*) (ptrdiff_t, Lisp_Object),
863 ptrdiff_t, Lisp_Object);
864 static void clear_garbaged_frames (void);
865 static int current_message_1 (ptrdiff_t, Lisp_Object);
866 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
867 static void set_message (Lisp_Object);
868 static int set_message_1 (ptrdiff_t, Lisp_Object);
869 static int display_echo_area (struct window *);
870 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
871 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
872 static void unwind_redisplay (void);
873 static int string_char_and_length (const unsigned char *, int *);
874 static struct text_pos display_prop_end (struct it *, Lisp_Object,
875 struct text_pos);
876 static int compute_window_start_on_continuation_line (struct window *);
877 static void insert_left_trunc_glyphs (struct it *);
878 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
879 Lisp_Object);
880 static void extend_face_to_end_of_line (struct it *);
881 static int append_space_for_newline (struct it *, int);
882 static int cursor_row_fully_visible_p (struct window *, int, int);
883 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
884 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
885 static int trailing_whitespace_p (ptrdiff_t);
886 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
887 static void push_it (struct it *, struct text_pos *);
888 static void iterate_out_of_display_property (struct it *);
889 static void pop_it (struct it *);
890 static void sync_frame_with_window_matrix_rows (struct window *);
891 static void redisplay_internal (void);
892 static int echo_area_display (int);
893 static void redisplay_windows (Lisp_Object);
894 static void redisplay_window (Lisp_Object, bool);
895 static Lisp_Object redisplay_window_error (Lisp_Object);
896 static Lisp_Object redisplay_window_0 (Lisp_Object);
897 static Lisp_Object redisplay_window_1 (Lisp_Object);
898 static int set_cursor_from_row (struct window *, struct glyph_row *,
899 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
900 int, int);
901 static int update_menu_bar (struct frame *, int, int);
902 static int try_window_reusing_current_matrix (struct window *);
903 static int try_window_id (struct window *);
904 static int display_line (struct it *);
905 static int display_mode_lines (struct window *);
906 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
907 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
908 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
909 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
910 static void display_menu_bar (struct window *);
911 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
912 ptrdiff_t *);
913 static int display_string (const char *, Lisp_Object, Lisp_Object,
914 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
915 static void compute_line_metrics (struct it *);
916 static void run_redisplay_end_trigger_hook (struct it *);
917 static int get_overlay_strings (struct it *, ptrdiff_t);
918 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
919 static void next_overlay_string (struct it *);
920 static void reseat (struct it *, struct text_pos, int);
921 static void reseat_1 (struct it *, struct text_pos, int);
922 static void back_to_previous_visible_line_start (struct it *);
923 static void reseat_at_next_visible_line_start (struct it *, int);
924 static int next_element_from_ellipsis (struct it *);
925 static int next_element_from_display_vector (struct it *);
926 static int next_element_from_string (struct it *);
927 static int next_element_from_c_string (struct it *);
928 static int next_element_from_buffer (struct it *);
929 static int next_element_from_composition (struct it *);
930 static int next_element_from_image (struct it *);
931 static int next_element_from_stretch (struct it *);
932 static void load_overlay_strings (struct it *, ptrdiff_t);
933 static int init_from_display_pos (struct it *, struct window *,
934 struct display_pos *);
935 static void reseat_to_string (struct it *, const char *,
936 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
937 static int get_next_display_element (struct it *);
938 static enum move_it_result
939 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
940 enum move_operation_enum);
941 static void get_visually_first_element (struct it *);
942 static void init_to_row_start (struct it *, struct window *,
943 struct glyph_row *);
944 static int init_to_row_end (struct it *, struct window *,
945 struct glyph_row *);
946 static void back_to_previous_line_start (struct it *);
947 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
948 static struct text_pos string_pos_nchars_ahead (struct text_pos,
949 Lisp_Object, ptrdiff_t);
950 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
951 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
952 static ptrdiff_t number_of_chars (const char *, bool);
953 static void compute_stop_pos (struct it *);
954 static void compute_string_pos (struct text_pos *, struct text_pos,
955 Lisp_Object);
956 static int face_before_or_after_it_pos (struct it *, int);
957 static ptrdiff_t next_overlay_change (ptrdiff_t);
958 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
959 Lisp_Object, struct text_pos *, ptrdiff_t, int);
960 static int handle_single_display_spec (struct it *, Lisp_Object,
961 Lisp_Object, Lisp_Object,
962 struct text_pos *, ptrdiff_t, int, int);
963 static int underlying_face_id (struct it *);
964 static int in_ellipses_for_invisible_text_p (struct display_pos *,
965 struct window *);
966
967 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
968 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
969
970 #ifdef HAVE_WINDOW_SYSTEM
971
972 static void x_consider_frame_title (Lisp_Object);
973 static void update_tool_bar (struct frame *, int);
974 static int redisplay_tool_bar (struct frame *);
975 static void x_draw_bottom_divider (struct window *w);
976 static void notice_overwritten_cursor (struct window *,
977 enum glyph_row_area,
978 int, int, int, int);
979 static void append_stretch_glyph (struct it *, Lisp_Object,
980 int, int, int);
981
982
983 #endif /* HAVE_WINDOW_SYSTEM */
984
985 static void produce_special_glyphs (struct it *, enum display_element_type);
986 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
987 static int coords_in_mouse_face_p (struct window *, int, int);
988
989
990 \f
991 /***********************************************************************
992 Window display dimensions
993 ***********************************************************************/
994
995 /* Return the bottom boundary y-position for text lines in window W.
996 This is the first y position at which a line cannot start.
997 It is relative to the top of the window.
998
999 This is the height of W minus the height of a mode line, if any. */
1000
1001 int
1002 window_text_bottom_y (struct window *w)
1003 {
1004 int height = WINDOW_PIXEL_HEIGHT (w);
1005
1006 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1007
1008 if (WINDOW_WANTS_MODELINE_P (w))
1009 height -= CURRENT_MODE_LINE_HEIGHT (w);
1010
1011 return height;
1012 }
1013
1014 /* Return the pixel width of display area AREA of window W.
1015 ANY_AREA means return the total width of W, not including
1016 fringes to the left and right of the window. */
1017
1018 int
1019 window_box_width (struct window *w, enum glyph_row_area area)
1020 {
1021 int pixels = w->pixel_width;
1022
1023 if (!w->pseudo_window_p)
1024 {
1025 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1026 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1027
1028 if (area == TEXT_AREA)
1029 pixels -= (WINDOW_MARGINS_WIDTH (w)
1030 + WINDOW_FRINGES_WIDTH (w));
1031 else if (area == LEFT_MARGIN_AREA)
1032 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1033 else if (area == RIGHT_MARGIN_AREA)
1034 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1035 }
1036
1037 return pixels;
1038 }
1039
1040
1041 /* Return the pixel height of the display area of window W, not
1042 including mode lines of W, if any. */
1043
1044 int
1045 window_box_height (struct window *w)
1046 {
1047 struct frame *f = XFRAME (w->frame);
1048 int height = WINDOW_PIXEL_HEIGHT (w);
1049
1050 eassert (height >= 0);
1051
1052 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1053
1054 /* Note: the code below that determines the mode-line/header-line
1055 height is essentially the same as that contained in the macro
1056 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1057 the appropriate glyph row has its `mode_line_p' flag set,
1058 and if it doesn't, uses estimate_mode_line_height instead. */
1059
1060 if (WINDOW_WANTS_MODELINE_P (w))
1061 {
1062 struct glyph_row *ml_row
1063 = (w->current_matrix && w->current_matrix->rows
1064 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1065 : 0);
1066 if (ml_row && ml_row->mode_line_p)
1067 height -= ml_row->height;
1068 else
1069 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1070 }
1071
1072 if (WINDOW_WANTS_HEADER_LINE_P (w))
1073 {
1074 struct glyph_row *hl_row
1075 = (w->current_matrix && w->current_matrix->rows
1076 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1077 : 0);
1078 if (hl_row && hl_row->mode_line_p)
1079 height -= hl_row->height;
1080 else
1081 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1082 }
1083
1084 /* With a very small font and a mode-line that's taller than
1085 default, we might end up with a negative height. */
1086 return max (0, height);
1087 }
1088
1089 /* Return the window-relative coordinate of the left edge of display
1090 area AREA of window W. ANY_AREA means return the left edge of the
1091 whole window, to the right of the left fringe of W. */
1092
1093 int
1094 window_box_left_offset (struct window *w, enum glyph_row_area area)
1095 {
1096 int x;
1097
1098 if (w->pseudo_window_p)
1099 return 0;
1100
1101 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1102
1103 if (area == TEXT_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA));
1106 else if (area == RIGHT_MARGIN_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA)
1109 + window_box_width (w, TEXT_AREA)
1110 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1111 ? 0
1112 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1113 else if (area == LEFT_MARGIN_AREA
1114 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1115 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1116
1117 return x;
1118 }
1119
1120
1121 /* Return the window-relative coordinate of the right edge of display
1122 area AREA of window W. ANY_AREA means return the right edge of the
1123 whole window, to the left of the right fringe of W. */
1124
1125 int
1126 window_box_right_offset (struct window *w, enum glyph_row_area area)
1127 {
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1129 }
1130
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. ANY_AREA means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1134
1135 int
1136 window_box_left (struct window *w, enum glyph_row_area area)
1137 {
1138 struct frame *f = XFRAME (w->frame);
1139 int x;
1140
1141 if (w->pseudo_window_p)
1142 return FRAME_INTERNAL_BORDER_WIDTH (f);
1143
1144 x = (WINDOW_LEFT_EDGE_X (w)
1145 + window_box_left_offset (w, area));
1146
1147 return x;
1148 }
1149
1150
1151 /* Return the frame-relative coordinate of the right edge of display
1152 area AREA of window W. ANY_AREA means return the right edge of the
1153 whole window, to the left of the right fringe of W. */
1154
1155 int
1156 window_box_right (struct window *w, enum glyph_row_area area)
1157 {
1158 return window_box_left (w, area) + window_box_width (w, area);
1159 }
1160
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines, in frame-relative coordinates. ANY_AREA means the
1163 whole window, not including the left and right fringes of
1164 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1165 coordinates of the upper-left corner of the box. Return in
1166 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1167
1168 void
1169 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1170 int *box_y, int *box_width, int *box_height)
1171 {
1172 if (box_width)
1173 *box_width = window_box_width (w, area);
1174 if (box_height)
1175 *box_height = window_box_height (w);
1176 if (box_x)
1177 *box_x = window_box_left (w, area);
1178 if (box_y)
1179 {
1180 *box_y = WINDOW_TOP_EDGE_Y (w);
1181 if (WINDOW_WANTS_HEADER_LINE_P (w))
1182 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1183 }
1184 }
1185
1186 #ifdef HAVE_WINDOW_SYSTEM
1187
1188 /* Get the bounding box of the display area AREA of window W, without
1189 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1190 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1191 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1192 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1193 box. */
1194
1195 static void
1196 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1197 int *bottom_right_x, int *bottom_right_y)
1198 {
1199 window_box (w, ANY_AREA, top_left_x, top_left_y,
1200 bottom_right_x, bottom_right_y);
1201 *bottom_right_x += *top_left_x;
1202 *bottom_right_y += *top_left_y;
1203 }
1204
1205 #endif /* HAVE_WINDOW_SYSTEM */
1206
1207 /***********************************************************************
1208 Utilities
1209 ***********************************************************************/
1210
1211 /* Return the bottom y-position of the line the iterator IT is in.
1212 This can modify IT's settings. */
1213
1214 int
1215 line_bottom_y (struct it *it)
1216 {
1217 int line_height = it->max_ascent + it->max_descent;
1218 int line_top_y = it->current_y;
1219
1220 if (line_height == 0)
1221 {
1222 if (last_height)
1223 line_height = last_height;
1224 else if (IT_CHARPOS (*it) < ZV)
1225 {
1226 move_it_by_lines (it, 1);
1227 line_height = (it->max_ascent || it->max_descent
1228 ? it->max_ascent + it->max_descent
1229 : last_height);
1230 }
1231 else
1232 {
1233 struct glyph_row *row = it->glyph_row;
1234
1235 /* Use the default character height. */
1236 it->glyph_row = NULL;
1237 it->what = IT_CHARACTER;
1238 it->c = ' ';
1239 it->len = 1;
1240 PRODUCE_GLYPHS (it);
1241 line_height = it->ascent + it->descent;
1242 it->glyph_row = row;
1243 }
1244 }
1245
1246 return line_top_y + line_height;
1247 }
1248
1249 DEFUN ("line-pixel-height", Fline_pixel_height,
1250 Sline_pixel_height, 0, 0, 0,
1251 doc: /* Return height in pixels of text line in the selected window.
1252
1253 Value is the height in pixels of the line at point. */)
1254 (void)
1255 {
1256 struct it it;
1257 struct text_pos pt;
1258 struct window *w = XWINDOW (selected_window);
1259
1260 SET_TEXT_POS (pt, PT, PT_BYTE);
1261 start_display (&it, w, pt);
1262 it.vpos = it.current_y = 0;
1263 last_height = 0;
1264 return make_number (line_bottom_y (&it));
1265 }
1266
1267 /* Return the default pixel height of text lines in window W. The
1268 value is the canonical height of the W frame's default font, plus
1269 any extra space required by the line-spacing variable or frame
1270 parameter.
1271
1272 Implementation note: this ignores any line-spacing text properties
1273 put on the newline characters. This is because those properties
1274 only affect the _screen_ line ending in the newline (i.e., in a
1275 continued line, only the last screen line will be affected), which
1276 means only a small number of lines in a buffer can ever use this
1277 feature. Since this function is used to compute the default pixel
1278 equivalent of text lines in a window, we can safely ignore those
1279 few lines. For the same reasons, we ignore the line-height
1280 properties. */
1281 int
1282 default_line_pixel_height (struct window *w)
1283 {
1284 struct frame *f = WINDOW_XFRAME (w);
1285 int height = FRAME_LINE_HEIGHT (f);
1286
1287 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1288 {
1289 struct buffer *b = XBUFFER (w->contents);
1290 Lisp_Object val = BVAR (b, extra_line_spacing);
1291
1292 if (NILP (val))
1293 val = BVAR (&buffer_defaults, extra_line_spacing);
1294 if (!NILP (val))
1295 {
1296 if (RANGED_INTEGERP (0, val, INT_MAX))
1297 height += XFASTINT (val);
1298 else if (FLOATP (val))
1299 {
1300 int addon = XFLOAT_DATA (val) * height + 0.5;
1301
1302 if (addon >= 0)
1303 height += addon;
1304 }
1305 }
1306 else
1307 height += f->extra_line_spacing;
1308 }
1309
1310 return height;
1311 }
1312
1313 /* Subroutine of pos_visible_p below. Extracts a display string, if
1314 any, from the display spec given as its argument. */
1315 static Lisp_Object
1316 string_from_display_spec (Lisp_Object spec)
1317 {
1318 if (CONSP (spec))
1319 {
1320 while (CONSP (spec))
1321 {
1322 if (STRINGP (XCAR (spec)))
1323 return XCAR (spec);
1324 spec = XCDR (spec);
1325 }
1326 }
1327 else if (VECTORP (spec))
1328 {
1329 ptrdiff_t i;
1330
1331 for (i = 0; i < ASIZE (spec); i++)
1332 {
1333 if (STRINGP (AREF (spec, i)))
1334 return AREF (spec, i);
1335 }
1336 return Qnil;
1337 }
1338
1339 return spec;
1340 }
1341
1342
1343 /* Limit insanely large values of W->hscroll on frame F to the largest
1344 value that will still prevent first_visible_x and last_visible_x of
1345 'struct it' from overflowing an int. */
1346 static int
1347 window_hscroll_limited (struct window *w, struct frame *f)
1348 {
1349 ptrdiff_t window_hscroll = w->hscroll;
1350 int window_text_width = window_box_width (w, TEXT_AREA);
1351 int colwidth = FRAME_COLUMN_WIDTH (f);
1352
1353 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1354 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1355
1356 return window_hscroll;
1357 }
1358
1359 /* Return 1 if position CHARPOS is visible in window W.
1360 CHARPOS < 0 means return info about WINDOW_END position.
1361 If visible, set *X and *Y to pixel coordinates of top left corner.
1362 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1363 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1364
1365 int
1366 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1367 int *rtop, int *rbot, int *rowh, int *vpos)
1368 {
1369 struct it it;
1370 void *itdata = bidi_shelve_cache ();
1371 struct text_pos top;
1372 int visible_p = 0;
1373 struct buffer *old_buffer = NULL;
1374
1375 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1376 return visible_p;
1377
1378 if (XBUFFER (w->contents) != current_buffer)
1379 {
1380 old_buffer = current_buffer;
1381 set_buffer_internal_1 (XBUFFER (w->contents));
1382 }
1383
1384 SET_TEXT_POS_FROM_MARKER (top, w->start);
1385 /* Scrolling a minibuffer window via scroll bar when the echo area
1386 shows long text sometimes resets the minibuffer contents behind
1387 our backs. */
1388 if (CHARPOS (top) > ZV)
1389 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1390
1391 /* Compute exact mode line heights. */
1392 if (WINDOW_WANTS_MODELINE_P (w))
1393 w->mode_line_height
1394 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1395 BVAR (current_buffer, mode_line_format));
1396
1397 if (WINDOW_WANTS_HEADER_LINE_P (w))
1398 w->header_line_height
1399 = display_mode_line (w, HEADER_LINE_FACE_ID,
1400 BVAR (current_buffer, header_line_format));
1401
1402 start_display (&it, w, top);
1403 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1404 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1405
1406 if (charpos >= 0
1407 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1408 && IT_CHARPOS (it) >= charpos)
1409 /* When scanning backwards under bidi iteration, move_it_to
1410 stops at or _before_ CHARPOS, because it stops at or to
1411 the _right_ of the character at CHARPOS. */
1412 || (it.bidi_p && it.bidi_it.scan_dir == -1
1413 && IT_CHARPOS (it) <= charpos)))
1414 {
1415 /* We have reached CHARPOS, or passed it. How the call to
1416 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1417 or covered by a display property, move_it_to stops at the end
1418 of the invisible text, to the right of CHARPOS. (ii) If
1419 CHARPOS is in a display vector, move_it_to stops on its last
1420 glyph. */
1421 int top_x = it.current_x;
1422 int top_y = it.current_y;
1423 /* Calling line_bottom_y may change it.method, it.position, etc. */
1424 enum it_method it_method = it.method;
1425 int bottom_y = (last_height = 0, line_bottom_y (&it));
1426 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1427
1428 if (top_y < window_top_y)
1429 visible_p = bottom_y > window_top_y;
1430 else if (top_y < it.last_visible_y)
1431 visible_p = true;
1432 if (bottom_y >= it.last_visible_y
1433 && it.bidi_p && it.bidi_it.scan_dir == -1
1434 && IT_CHARPOS (it) < charpos)
1435 {
1436 /* When the last line of the window is scanned backwards
1437 under bidi iteration, we could be duped into thinking
1438 that we have passed CHARPOS, when in fact move_it_to
1439 simply stopped short of CHARPOS because it reached
1440 last_visible_y. To see if that's what happened, we call
1441 move_it_to again with a slightly larger vertical limit,
1442 and see if it actually moved vertically; if it did, we
1443 didn't really reach CHARPOS, which is beyond window end. */
1444 struct it save_it = it;
1445 /* Why 10? because we don't know how many canonical lines
1446 will the height of the next line(s) be. So we guess. */
1447 int ten_more_lines = 10 * default_line_pixel_height (w);
1448
1449 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1450 MOVE_TO_POS | MOVE_TO_Y);
1451 if (it.current_y > top_y)
1452 visible_p = 0;
1453
1454 it = save_it;
1455 }
1456 if (visible_p)
1457 {
1458 if (it_method == GET_FROM_DISPLAY_VECTOR)
1459 {
1460 /* We stopped on the last glyph of a display vector.
1461 Try and recompute. Hack alert! */
1462 if (charpos < 2 || top.charpos >= charpos)
1463 top_x = it.glyph_row->x;
1464 else
1465 {
1466 struct it it2, it2_prev;
1467 /* The idea is to get to the previous buffer
1468 position, consume the character there, and use
1469 the pixel coordinates we get after that. But if
1470 the previous buffer position is also displayed
1471 from a display vector, we need to consume all of
1472 the glyphs from that display vector. */
1473 start_display (&it2, w, top);
1474 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1475 /* If we didn't get to CHARPOS - 1, there's some
1476 replacing display property at that position, and
1477 we stopped after it. That is exactly the place
1478 whose coordinates we want. */
1479 if (IT_CHARPOS (it2) != charpos - 1)
1480 it2_prev = it2;
1481 else
1482 {
1483 /* Iterate until we get out of the display
1484 vector that displays the character at
1485 CHARPOS - 1. */
1486 do {
1487 get_next_display_element (&it2);
1488 PRODUCE_GLYPHS (&it2);
1489 it2_prev = it2;
1490 set_iterator_to_next (&it2, 1);
1491 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1492 && IT_CHARPOS (it2) < charpos);
1493 }
1494 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1495 || it2_prev.current_x > it2_prev.last_visible_x)
1496 top_x = it.glyph_row->x;
1497 else
1498 {
1499 top_x = it2_prev.current_x;
1500 top_y = it2_prev.current_y;
1501 }
1502 }
1503 }
1504 else if (IT_CHARPOS (it) != charpos)
1505 {
1506 Lisp_Object cpos = make_number (charpos);
1507 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1508 Lisp_Object string = string_from_display_spec (spec);
1509 struct text_pos tpos;
1510 int replacing_spec_p;
1511 bool newline_in_string
1512 = (STRINGP (string)
1513 && memchr (SDATA (string), '\n', SBYTES (string)));
1514
1515 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1516 replacing_spec_p
1517 = (!NILP (spec)
1518 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1519 charpos, FRAME_WINDOW_P (it.f)));
1520 /* The tricky code below is needed because there's a
1521 discrepancy between move_it_to and how we set cursor
1522 when PT is at the beginning of a portion of text
1523 covered by a display property or an overlay with a
1524 display property, or the display line ends in a
1525 newline from a display string. move_it_to will stop
1526 _after_ such display strings, whereas
1527 set_cursor_from_row conspires with cursor_row_p to
1528 place the cursor on the first glyph produced from the
1529 display string. */
1530
1531 /* We have overshoot PT because it is covered by a
1532 display property that replaces the text it covers.
1533 If the string includes embedded newlines, we are also
1534 in the wrong display line. Backtrack to the correct
1535 line, where the display property begins. */
1536 if (replacing_spec_p)
1537 {
1538 Lisp_Object startpos, endpos;
1539 EMACS_INT start, end;
1540 struct it it3;
1541 int it3_moved;
1542
1543 /* Find the first and the last buffer positions
1544 covered by the display string. */
1545 endpos =
1546 Fnext_single_char_property_change (cpos, Qdisplay,
1547 Qnil, Qnil);
1548 startpos =
1549 Fprevious_single_char_property_change (endpos, Qdisplay,
1550 Qnil, Qnil);
1551 start = XFASTINT (startpos);
1552 end = XFASTINT (endpos);
1553 /* Move to the last buffer position before the
1554 display property. */
1555 start_display (&it3, w, top);
1556 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1557 /* Move forward one more line if the position before
1558 the display string is a newline or if it is the
1559 rightmost character on a line that is
1560 continued or word-wrapped. */
1561 if (it3.method == GET_FROM_BUFFER
1562 && (it3.c == '\n'
1563 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1564 move_it_by_lines (&it3, 1);
1565 else if (move_it_in_display_line_to (&it3, -1,
1566 it3.current_x
1567 + it3.pixel_width,
1568 MOVE_TO_X)
1569 == MOVE_LINE_CONTINUED)
1570 {
1571 move_it_by_lines (&it3, 1);
1572 /* When we are under word-wrap, the #$@%!
1573 move_it_by_lines moves 2 lines, so we need to
1574 fix that up. */
1575 if (it3.line_wrap == WORD_WRAP)
1576 move_it_by_lines (&it3, -1);
1577 }
1578
1579 /* Record the vertical coordinate of the display
1580 line where we wound up. */
1581 top_y = it3.current_y;
1582 if (it3.bidi_p)
1583 {
1584 /* When characters are reordered for display,
1585 the character displayed to the left of the
1586 display string could be _after_ the display
1587 property in the logical order. Use the
1588 smallest vertical position of these two. */
1589 start_display (&it3, w, top);
1590 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1591 if (it3.current_y < top_y)
1592 top_y = it3.current_y;
1593 }
1594 /* Move from the top of the window to the beginning
1595 of the display line where the display string
1596 begins. */
1597 start_display (&it3, w, top);
1598 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1599 /* If it3_moved stays zero after the 'while' loop
1600 below, that means we already were at a newline
1601 before the loop (e.g., the display string begins
1602 with a newline), so we don't need to (and cannot)
1603 inspect the glyphs of it3.glyph_row, because
1604 PRODUCE_GLYPHS will not produce anything for a
1605 newline, and thus it3.glyph_row stays at its
1606 stale content it got at top of the window. */
1607 it3_moved = 0;
1608 /* Finally, advance the iterator until we hit the
1609 first display element whose character position is
1610 CHARPOS, or until the first newline from the
1611 display string, which signals the end of the
1612 display line. */
1613 while (get_next_display_element (&it3))
1614 {
1615 PRODUCE_GLYPHS (&it3);
1616 if (IT_CHARPOS (it3) == charpos
1617 || ITERATOR_AT_END_OF_LINE_P (&it3))
1618 break;
1619 it3_moved = 1;
1620 set_iterator_to_next (&it3, 0);
1621 }
1622 top_x = it3.current_x - it3.pixel_width;
1623 /* Normally, we would exit the above loop because we
1624 found the display element whose character
1625 position is CHARPOS. For the contingency that we
1626 didn't, and stopped at the first newline from the
1627 display string, move back over the glyphs
1628 produced from the string, until we find the
1629 rightmost glyph not from the string. */
1630 if (it3_moved
1631 && newline_in_string
1632 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1633 {
1634 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1635 + it3.glyph_row->used[TEXT_AREA];
1636
1637 while (EQ ((g - 1)->object, string))
1638 {
1639 --g;
1640 top_x -= g->pixel_width;
1641 }
1642 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1643 + it3.glyph_row->used[TEXT_AREA]);
1644 }
1645 }
1646 }
1647
1648 *x = top_x;
1649 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1650 *rtop = max (0, window_top_y - top_y);
1651 *rbot = max (0, bottom_y - it.last_visible_y);
1652 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1653 - max (top_y, window_top_y)));
1654 *vpos = it.vpos;
1655 }
1656 }
1657 else
1658 {
1659 /* We were asked to provide info about WINDOW_END. */
1660 struct it it2;
1661 void *it2data = NULL;
1662
1663 SAVE_IT (it2, it, it2data);
1664 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1665 move_it_by_lines (&it, 1);
1666 if (charpos < IT_CHARPOS (it)
1667 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1668 {
1669 visible_p = true;
1670 RESTORE_IT (&it2, &it2, it2data);
1671 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1672 *x = it2.current_x;
1673 *y = it2.current_y + it2.max_ascent - it2.ascent;
1674 *rtop = max (0, -it2.current_y);
1675 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1676 - it.last_visible_y));
1677 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1678 it.last_visible_y)
1679 - max (it2.current_y,
1680 WINDOW_HEADER_LINE_HEIGHT (w))));
1681 *vpos = it2.vpos;
1682 }
1683 else
1684 bidi_unshelve_cache (it2data, 1);
1685 }
1686 bidi_unshelve_cache (itdata, 0);
1687
1688 if (old_buffer)
1689 set_buffer_internal_1 (old_buffer);
1690
1691 if (visible_p && w->hscroll > 0)
1692 *x -=
1693 window_hscroll_limited (w, WINDOW_XFRAME (w))
1694 * WINDOW_FRAME_COLUMN_WIDTH (w);
1695
1696 #if 0
1697 /* Debugging code. */
1698 if (visible_p)
1699 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1700 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1701 else
1702 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1703 #endif
1704
1705 return visible_p;
1706 }
1707
1708
1709 /* Return the next character from STR. Return in *LEN the length of
1710 the character. This is like STRING_CHAR_AND_LENGTH but never
1711 returns an invalid character. If we find one, we return a `?', but
1712 with the length of the invalid character. */
1713
1714 static int
1715 string_char_and_length (const unsigned char *str, int *len)
1716 {
1717 int c;
1718
1719 c = STRING_CHAR_AND_LENGTH (str, *len);
1720 if (!CHAR_VALID_P (c))
1721 /* We may not change the length here because other places in Emacs
1722 don't use this function, i.e. they silently accept invalid
1723 characters. */
1724 c = '?';
1725
1726 return c;
1727 }
1728
1729
1730
1731 /* Given a position POS containing a valid character and byte position
1732 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1733
1734 static struct text_pos
1735 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1736 {
1737 eassert (STRINGP (string) && nchars >= 0);
1738
1739 if (STRING_MULTIBYTE (string))
1740 {
1741 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1742 int len;
1743
1744 while (nchars--)
1745 {
1746 string_char_and_length (p, &len);
1747 p += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1750 }
1751 }
1752 else
1753 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1754
1755 return pos;
1756 }
1757
1758
1759 /* Value is the text position, i.e. character and byte position,
1760 for character position CHARPOS in STRING. */
1761
1762 static struct text_pos
1763 string_pos (ptrdiff_t charpos, Lisp_Object string)
1764 {
1765 struct text_pos pos;
1766 eassert (STRINGP (string));
1767 eassert (charpos >= 0);
1768 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1769 return pos;
1770 }
1771
1772
1773 /* Value is a text position, i.e. character and byte position, for
1774 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1775 means recognize multibyte characters. */
1776
1777 static struct text_pos
1778 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1779 {
1780 struct text_pos pos;
1781
1782 eassert (s != NULL);
1783 eassert (charpos >= 0);
1784
1785 if (multibyte_p)
1786 {
1787 int len;
1788
1789 SET_TEXT_POS (pos, 0, 0);
1790 while (charpos--)
1791 {
1792 string_char_and_length ((const unsigned char *) s, &len);
1793 s += len;
1794 CHARPOS (pos) += 1;
1795 BYTEPOS (pos) += len;
1796 }
1797 }
1798 else
1799 SET_TEXT_POS (pos, charpos, charpos);
1800
1801 return pos;
1802 }
1803
1804
1805 /* Value is the number of characters in C string S. MULTIBYTE_P
1806 non-zero means recognize multibyte characters. */
1807
1808 static ptrdiff_t
1809 number_of_chars (const char *s, bool multibyte_p)
1810 {
1811 ptrdiff_t nchars;
1812
1813 if (multibyte_p)
1814 {
1815 ptrdiff_t rest = strlen (s);
1816 int len;
1817 const unsigned char *p = (const unsigned char *) s;
1818
1819 for (nchars = 0; rest > 0; ++nchars)
1820 {
1821 string_char_and_length (p, &len);
1822 rest -= len, p += len;
1823 }
1824 }
1825 else
1826 nchars = strlen (s);
1827
1828 return nchars;
1829 }
1830
1831
1832 /* Compute byte position NEWPOS->bytepos corresponding to
1833 NEWPOS->charpos. POS is a known position in string STRING.
1834 NEWPOS->charpos must be >= POS.charpos. */
1835
1836 static void
1837 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1838 {
1839 eassert (STRINGP (string));
1840 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1841
1842 if (STRING_MULTIBYTE (string))
1843 *newpos = string_pos_nchars_ahead (pos, string,
1844 CHARPOS (*newpos) - CHARPOS (pos));
1845 else
1846 BYTEPOS (*newpos) = CHARPOS (*newpos);
1847 }
1848
1849 /* EXPORT:
1850 Return an estimation of the pixel height of mode or header lines on
1851 frame F. FACE_ID specifies what line's height to estimate. */
1852
1853 int
1854 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1855 {
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1858 {
1859 int height = FONT_HEIGHT (FRAME_FONT (f));
1860
1861 /* This function is called so early when Emacs starts that the face
1862 cache and mode line face are not yet initialized. */
1863 if (FRAME_FACE_CACHE (f))
1864 {
1865 struct face *face = FACE_FROM_ID (f, face_id);
1866 if (face)
1867 {
1868 if (face->font)
1869 height = FONT_HEIGHT (face->font);
1870 if (face->box_line_width > 0)
1871 height += 2 * face->box_line_width;
1872 }
1873 }
1874
1875 return height;
1876 }
1877 #endif
1878
1879 return 1;
1880 }
1881
1882 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1883 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1884 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1885 not force the value into range. */
1886
1887 void
1888 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1889 int *x, int *y, NativeRectangle *bounds, int noclip)
1890 {
1891
1892 #ifdef HAVE_WINDOW_SYSTEM
1893 if (FRAME_WINDOW_P (f))
1894 {
1895 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1896 even for negative values. */
1897 if (pix_x < 0)
1898 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1899 if (pix_y < 0)
1900 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1901
1902 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1903 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1904
1905 if (bounds)
1906 STORE_NATIVE_RECT (*bounds,
1907 FRAME_COL_TO_PIXEL_X (f, pix_x),
1908 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1909 FRAME_COLUMN_WIDTH (f) - 1,
1910 FRAME_LINE_HEIGHT (f) - 1);
1911
1912 /* PXW: Should we clip pixels before converting to columns/lines? */
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 if (s->row->mode_line_p)
2054 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2055 else
2056 r.width = WINDOW_PIXEL_WIDTH (s->w);
2057
2058 /* Unless displaying a mode or menu bar line, which are always
2059 fully visible, clip to the visible part of the row. */
2060 if (s->w->pseudo_window_p)
2061 r.height = s->row->visible_height;
2062 else
2063 r.height = s->height;
2064 }
2065 else
2066 {
2067 /* This is a text line that may be partially visible. */
2068 r.x = window_box_left (s->w, s->area);
2069 r.width = window_box_width (s->w, s->area);
2070 r.height = s->row->visible_height;
2071 }
2072
2073 if (s->clip_head)
2074 if (r.x < s->clip_head->x)
2075 {
2076 if (r.width >= s->clip_head->x - r.x)
2077 r.width -= s->clip_head->x - r.x;
2078 else
2079 r.width = 0;
2080 r.x = s->clip_head->x;
2081 }
2082 if (s->clip_tail)
2083 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2084 {
2085 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2086 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2087 else
2088 r.width = 0;
2089 }
2090
2091 /* If S draws overlapping rows, it's sufficient to use the top and
2092 bottom of the window for clipping because this glyph string
2093 intentionally draws over other lines. */
2094 if (s->for_overlaps)
2095 {
2096 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2097 r.height = window_text_bottom_y (s->w) - r.y;
2098
2099 /* Alas, the above simple strategy does not work for the
2100 environments with anti-aliased text: if the same text is
2101 drawn onto the same place multiple times, it gets thicker.
2102 If the overlap we are processing is for the erased cursor, we
2103 take the intersection with the rectangle of the cursor. */
2104 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2105 {
2106 XRectangle rc, r_save = r;
2107
2108 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2109 rc.y = s->w->phys_cursor.y;
2110 rc.width = s->w->phys_cursor_width;
2111 rc.height = s->w->phys_cursor_height;
2112
2113 x_intersect_rectangles (&r_save, &rc, &r);
2114 }
2115 }
2116 else
2117 {
2118 /* Don't use S->y for clipping because it doesn't take partially
2119 visible lines into account. For example, it can be negative for
2120 partially visible lines at the top of a window. */
2121 if (!s->row->full_width_p
2122 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2123 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2124 else
2125 r.y = max (0, s->row->y);
2126 }
2127
2128 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2129
2130 /* If drawing the cursor, don't let glyph draw outside its
2131 advertised boundaries. Cleartype does this under some circumstances. */
2132 if (s->hl == DRAW_CURSOR)
2133 {
2134 struct glyph *glyph = s->first_glyph;
2135 int height, max_y;
2136
2137 if (s->x > r.x)
2138 {
2139 r.width -= s->x - r.x;
2140 r.x = s->x;
2141 }
2142 r.width = min (r.width, glyph->pixel_width);
2143
2144 /* If r.y is below window bottom, ensure that we still see a cursor. */
2145 height = min (glyph->ascent + glyph->descent,
2146 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2147 max_y = window_text_bottom_y (s->w) - height;
2148 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2149 if (s->ybase - glyph->ascent > max_y)
2150 {
2151 r.y = max_y;
2152 r.height = height;
2153 }
2154 else
2155 {
2156 /* Don't draw cursor glyph taller than our actual glyph. */
2157 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2158 if (height < r.height)
2159 {
2160 max_y = r.y + r.height;
2161 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2162 r.height = min (max_y - r.y, height);
2163 }
2164 }
2165 }
2166
2167 if (s->row->clip)
2168 {
2169 XRectangle r_save = r;
2170
2171 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2172 r.width = 0;
2173 }
2174
2175 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2176 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2177 {
2178 #ifdef CONVERT_FROM_XRECT
2179 CONVERT_FROM_XRECT (r, *rects);
2180 #else
2181 *rects = r;
2182 #endif
2183 return 1;
2184 }
2185 else
2186 {
2187 /* If we are processing overlapping and allowed to return
2188 multiple clipping rectangles, we exclude the row of the glyph
2189 string from the clipping rectangle. This is to avoid drawing
2190 the same text on the environment with anti-aliasing. */
2191 #ifdef CONVERT_FROM_XRECT
2192 XRectangle rs[2];
2193 #else
2194 XRectangle *rs = rects;
2195 #endif
2196 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2197
2198 if (s->for_overlaps & OVERLAPS_PRED)
2199 {
2200 rs[i] = r;
2201 if (r.y + r.height > row_y)
2202 {
2203 if (r.y < row_y)
2204 rs[i].height = row_y - r.y;
2205 else
2206 rs[i].height = 0;
2207 }
2208 i++;
2209 }
2210 if (s->for_overlaps & OVERLAPS_SUCC)
2211 {
2212 rs[i] = r;
2213 if (r.y < row_y + s->row->visible_height)
2214 {
2215 if (r.y + r.height > row_y + s->row->visible_height)
2216 {
2217 rs[i].y = row_y + s->row->visible_height;
2218 rs[i].height = r.y + r.height - rs[i].y;
2219 }
2220 else
2221 rs[i].height = 0;
2222 }
2223 i++;
2224 }
2225
2226 n = i;
2227 #ifdef CONVERT_FROM_XRECT
2228 for (i = 0; i < n; i++)
2229 CONVERT_FROM_XRECT (rs[i], rects[i]);
2230 #endif
2231 return n;
2232 }
2233 }
2234
2235 /* EXPORT:
2236 Return in *NR the clipping rectangle for glyph string S. */
2237
2238 void
2239 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2240 {
2241 get_glyph_string_clip_rects (s, nr, 1);
2242 }
2243
2244
2245 /* EXPORT:
2246 Return the position and height of the phys cursor in window W.
2247 Set w->phys_cursor_width to width of phys cursor.
2248 */
2249
2250 void
2251 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2252 struct glyph *glyph, int *xp, int *yp, int *heightp)
2253 {
2254 struct frame *f = XFRAME (WINDOW_FRAME (w));
2255 int x, y, wd, h, h0, y0;
2256
2257 /* Compute the width of the rectangle to draw. If on a stretch
2258 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2259 rectangle as wide as the glyph, but use a canonical character
2260 width instead. */
2261 wd = glyph->pixel_width - 1;
2262 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2263 wd++; /* Why? */
2264 #endif
2265
2266 x = w->phys_cursor.x;
2267 if (x < 0)
2268 {
2269 wd += x;
2270 x = 0;
2271 }
2272
2273 if (glyph->type == STRETCH_GLYPH
2274 && !x_stretch_cursor_p)
2275 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2276 w->phys_cursor_width = wd;
2277
2278 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2279
2280 /* If y is below window bottom, ensure that we still see a cursor. */
2281 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2282
2283 h = max (h0, glyph->ascent + glyph->descent);
2284 h0 = min (h0, glyph->ascent + glyph->descent);
2285
2286 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2287 if (y < y0)
2288 {
2289 h = max (h - (y0 - y) + 1, h0);
2290 y = y0 - 1;
2291 }
2292 else
2293 {
2294 y0 = window_text_bottom_y (w) - h0;
2295 if (y > y0)
2296 {
2297 h += y - y0;
2298 y = y0;
2299 }
2300 }
2301
2302 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2303 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2304 *heightp = h;
2305 }
2306
2307 /*
2308 * Remember which glyph the mouse is over.
2309 */
2310
2311 void
2312 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2313 {
2314 Lisp_Object window;
2315 struct window *w;
2316 struct glyph_row *r, *gr, *end_row;
2317 enum window_part part;
2318 enum glyph_row_area area;
2319 int x, y, width, height;
2320
2321 /* Try to determine frame pixel position and size of the glyph under
2322 frame pixel coordinates X/Y on frame F. */
2323
2324 if (window_resize_pixelwise)
2325 {
2326 width = height = 1;
2327 goto virtual_glyph;
2328 }
2329 else if (!f->glyphs_initialized_p
2330 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2331 NILP (window)))
2332 {
2333 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2334 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2335 goto virtual_glyph;
2336 }
2337
2338 w = XWINDOW (window);
2339 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2340 height = WINDOW_FRAME_LINE_HEIGHT (w);
2341
2342 x = window_relative_x_coord (w, part, gx);
2343 y = gy - WINDOW_TOP_EDGE_Y (w);
2344
2345 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2346 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2347
2348 if (w->pseudo_window_p)
2349 {
2350 area = TEXT_AREA;
2351 part = ON_MODE_LINE; /* Don't adjust margin. */
2352 goto text_glyph;
2353 }
2354
2355 switch (part)
2356 {
2357 case ON_LEFT_MARGIN:
2358 area = LEFT_MARGIN_AREA;
2359 goto text_glyph;
2360
2361 case ON_RIGHT_MARGIN:
2362 area = RIGHT_MARGIN_AREA;
2363 goto text_glyph;
2364
2365 case ON_HEADER_LINE:
2366 case ON_MODE_LINE:
2367 gr = (part == ON_HEADER_LINE
2368 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2369 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2370 gy = gr->y;
2371 area = TEXT_AREA;
2372 goto text_glyph_row_found;
2373
2374 case ON_TEXT:
2375 area = TEXT_AREA;
2376
2377 text_glyph:
2378 gr = 0; gy = 0;
2379 for (; r <= end_row && r->enabled_p; ++r)
2380 if (r->y + r->height > y)
2381 {
2382 gr = r; gy = r->y;
2383 break;
2384 }
2385
2386 text_glyph_row_found:
2387 if (gr && gy <= y)
2388 {
2389 struct glyph *g = gr->glyphs[area];
2390 struct glyph *end = g + gr->used[area];
2391
2392 height = gr->height;
2393 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2394 if (gx + g->pixel_width > x)
2395 break;
2396
2397 if (g < end)
2398 {
2399 if (g->type == IMAGE_GLYPH)
2400 {
2401 /* Don't remember when mouse is over image, as
2402 image may have hot-spots. */
2403 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2404 return;
2405 }
2406 width = g->pixel_width;
2407 }
2408 else
2409 {
2410 /* Use nominal char spacing at end of line. */
2411 x -= gx;
2412 gx += (x / width) * width;
2413 }
2414
2415 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2416 gx += window_box_left_offset (w, area);
2417 }
2418 else
2419 {
2420 /* Use nominal line height at end of window. */
2421 gx = (x / width) * width;
2422 y -= gy;
2423 gy += (y / height) * height;
2424 }
2425 break;
2426
2427 case ON_LEFT_FRINGE:
2428 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2429 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2430 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2431 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2432 goto row_glyph;
2433
2434 case ON_RIGHT_FRINGE:
2435 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2436 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2437 : window_box_right_offset (w, TEXT_AREA));
2438 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2439 goto row_glyph;
2440
2441 case ON_SCROLL_BAR:
2442 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2443 ? 0
2444 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2445 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2446 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2447 : 0)));
2448 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2449
2450 row_glyph:
2451 gr = 0, gy = 0;
2452 for (; r <= end_row && r->enabled_p; ++r)
2453 if (r->y + r->height > y)
2454 {
2455 gr = r; gy = r->y;
2456 break;
2457 }
2458
2459 if (gr && gy <= y)
2460 height = gr->height;
2461 else
2462 {
2463 /* Use nominal line height at end of window. */
2464 y -= gy;
2465 gy += (y / height) * height;
2466 }
2467 break;
2468
2469 default:
2470 ;
2471 virtual_glyph:
2472 /* If there is no glyph under the mouse, then we divide the screen
2473 into a grid of the smallest glyph in the frame, and use that
2474 as our "glyph". */
2475
2476 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2477 round down even for negative values. */
2478 if (gx < 0)
2479 gx -= width - 1;
2480 if (gy < 0)
2481 gy -= height - 1;
2482
2483 gx = (gx / width) * width;
2484 gy = (gy / height) * height;
2485
2486 goto store_rect;
2487 }
2488
2489 gx += WINDOW_LEFT_EDGE_X (w);
2490 gy += WINDOW_TOP_EDGE_Y (w);
2491
2492 store_rect:
2493 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2494
2495 /* Visible feedback for debugging. */
2496 #if 0
2497 #if HAVE_X_WINDOWS
2498 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2499 f->output_data.x->normal_gc,
2500 gx, gy, width, height);
2501 #endif
2502 #endif
2503 }
2504
2505
2506 #endif /* HAVE_WINDOW_SYSTEM */
2507
2508 static void
2509 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2510 {
2511 eassert (w);
2512 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2513 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2514 w->window_end_vpos
2515 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2516 }
2517
2518 /***********************************************************************
2519 Lisp form evaluation
2520 ***********************************************************************/
2521
2522 /* Error handler for safe_eval and safe_call. */
2523
2524 static Lisp_Object
2525 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2526 {
2527 add_to_log ("Error during redisplay: %S signaled %S",
2528 Flist (nargs, args), arg);
2529 return Qnil;
2530 }
2531
2532 /* Call function FUNC with the rest of NARGS - 1 arguments
2533 following. Return the result, or nil if something went
2534 wrong. Prevent redisplay during the evaluation. */
2535
2536 Lisp_Object
2537 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2538 {
2539 Lisp_Object val;
2540
2541 if (inhibit_eval_during_redisplay)
2542 val = Qnil;
2543 else
2544 {
2545 va_list ap;
2546 ptrdiff_t i;
2547 ptrdiff_t count = SPECPDL_INDEX ();
2548 struct gcpro gcpro1;
2549 Lisp_Object *args = alloca (nargs * word_size);
2550
2551 args[0] = func;
2552 va_start (ap, func);
2553 for (i = 1; i < nargs; i++)
2554 args[i] = va_arg (ap, Lisp_Object);
2555 va_end (ap);
2556
2557 GCPRO1 (args[0]);
2558 gcpro1.nvars = nargs;
2559 specbind (Qinhibit_redisplay, Qt);
2560 /* Use Qt to ensure debugger does not run,
2561 so there is no possibility of wanting to redisplay. */
2562 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2563 safe_eval_handler);
2564 UNGCPRO;
2565 val = unbind_to (count, val);
2566 }
2567
2568 return val;
2569 }
2570
2571
2572 /* Call function FN with one argument ARG.
2573 Return the result, or nil if something went wrong. */
2574
2575 Lisp_Object
2576 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2577 {
2578 return safe_call (2, fn, arg);
2579 }
2580
2581 static Lisp_Object Qeval;
2582
2583 Lisp_Object
2584 safe_eval (Lisp_Object sexpr)
2585 {
2586 return safe_call1 (Qeval, sexpr);
2587 }
2588
2589 /* Call function FN with two arguments ARG1 and ARG2.
2590 Return the result, or nil if something went wrong. */
2591
2592 Lisp_Object
2593 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2594 {
2595 return safe_call (3, fn, arg1, arg2);
2596 }
2597
2598
2599 \f
2600 /***********************************************************************
2601 Debugging
2602 ***********************************************************************/
2603
2604 #if 0
2605
2606 /* Define CHECK_IT to perform sanity checks on iterators.
2607 This is for debugging. It is too slow to do unconditionally. */
2608
2609 static void
2610 check_it (struct it *it)
2611 {
2612 if (it->method == GET_FROM_STRING)
2613 {
2614 eassert (STRINGP (it->string));
2615 eassert (IT_STRING_CHARPOS (*it) >= 0);
2616 }
2617 else
2618 {
2619 eassert (IT_STRING_CHARPOS (*it) < 0);
2620 if (it->method == GET_FROM_BUFFER)
2621 {
2622 /* Check that character and byte positions agree. */
2623 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2624 }
2625 }
2626
2627 if (it->dpvec)
2628 eassert (it->current.dpvec_index >= 0);
2629 else
2630 eassert (it->current.dpvec_index < 0);
2631 }
2632
2633 #define CHECK_IT(IT) check_it ((IT))
2634
2635 #else /* not 0 */
2636
2637 #define CHECK_IT(IT) (void) 0
2638
2639 #endif /* not 0 */
2640
2641
2642 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2643
2644 /* Check that the window end of window W is what we expect it
2645 to be---the last row in the current matrix displaying text. */
2646
2647 static void
2648 check_window_end (struct window *w)
2649 {
2650 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2651 {
2652 struct glyph_row *row;
2653 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2654 !row->enabled_p
2655 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2656 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2657 }
2658 }
2659
2660 #define CHECK_WINDOW_END(W) check_window_end ((W))
2661
2662 #else
2663
2664 #define CHECK_WINDOW_END(W) (void) 0
2665
2666 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2667
2668 /***********************************************************************
2669 Iterator initialization
2670 ***********************************************************************/
2671
2672 /* Initialize IT for displaying current_buffer in window W, starting
2673 at character position CHARPOS. CHARPOS < 0 means that no buffer
2674 position is specified which is useful when the iterator is assigned
2675 a position later. BYTEPOS is the byte position corresponding to
2676 CHARPOS.
2677
2678 If ROW is not null, calls to produce_glyphs with IT as parameter
2679 will produce glyphs in that row.
2680
2681 BASE_FACE_ID is the id of a base face to use. It must be one of
2682 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2683 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2684 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2685
2686 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2687 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2688 will be initialized to use the corresponding mode line glyph row of
2689 the desired matrix of W. */
2690
2691 void
2692 init_iterator (struct it *it, struct window *w,
2693 ptrdiff_t charpos, ptrdiff_t bytepos,
2694 struct glyph_row *row, enum face_id base_face_id)
2695 {
2696 enum face_id remapped_base_face_id = base_face_id;
2697
2698 /* Some precondition checks. */
2699 eassert (w != NULL && it != NULL);
2700 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2701 && charpos <= ZV));
2702
2703 /* If face attributes have been changed since the last redisplay,
2704 free realized faces now because they depend on face definitions
2705 that might have changed. Don't free faces while there might be
2706 desired matrices pending which reference these faces. */
2707 if (face_change_count && !inhibit_free_realized_faces)
2708 {
2709 face_change_count = 0;
2710 free_all_realized_faces (Qnil);
2711 }
2712
2713 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2714 if (! NILP (Vface_remapping_alist))
2715 remapped_base_face_id
2716 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2717
2718 /* Use one of the mode line rows of W's desired matrix if
2719 appropriate. */
2720 if (row == NULL)
2721 {
2722 if (base_face_id == MODE_LINE_FACE_ID
2723 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2724 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2725 else if (base_face_id == HEADER_LINE_FACE_ID)
2726 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2727 }
2728
2729 /* Clear IT. */
2730 memset (it, 0, sizeof *it);
2731 it->current.overlay_string_index = -1;
2732 it->current.dpvec_index = -1;
2733 it->base_face_id = remapped_base_face_id;
2734 it->string = Qnil;
2735 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2736 it->paragraph_embedding = L2R;
2737 it->bidi_it.string.lstring = Qnil;
2738 it->bidi_it.string.s = NULL;
2739 it->bidi_it.string.bufpos = 0;
2740 it->bidi_it.w = w;
2741
2742 /* The window in which we iterate over current_buffer: */
2743 XSETWINDOW (it->window, w);
2744 it->w = w;
2745 it->f = XFRAME (w->frame);
2746
2747 it->cmp_it.id = -1;
2748
2749 /* Extra space between lines (on window systems only). */
2750 if (base_face_id == DEFAULT_FACE_ID
2751 && FRAME_WINDOW_P (it->f))
2752 {
2753 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2755 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2756 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2757 * FRAME_LINE_HEIGHT (it->f));
2758 else if (it->f->extra_line_spacing > 0)
2759 it->extra_line_spacing = it->f->extra_line_spacing;
2760 it->max_extra_line_spacing = 0;
2761 }
2762
2763 /* If realized faces have been removed, e.g. because of face
2764 attribute changes of named faces, recompute them. When running
2765 in batch mode, the face cache of the initial frame is null. If
2766 we happen to get called, make a dummy face cache. */
2767 if (FRAME_FACE_CACHE (it->f) == NULL)
2768 init_frame_faces (it->f);
2769 if (FRAME_FACE_CACHE (it->f)->used == 0)
2770 recompute_basic_faces (it->f);
2771
2772 /* Current value of the `slice', `space-width', and 'height' properties. */
2773 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2774 it->space_width = Qnil;
2775 it->font_height = Qnil;
2776 it->override_ascent = -1;
2777
2778 /* Are control characters displayed as `^C'? */
2779 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2780
2781 /* -1 means everything between a CR and the following line end
2782 is invisible. >0 means lines indented more than this value are
2783 invisible. */
2784 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2785 ? (clip_to_bounds
2786 (-1, XINT (BVAR (current_buffer, selective_display)),
2787 PTRDIFF_MAX))
2788 : (!NILP (BVAR (current_buffer, selective_display))
2789 ? -1 : 0));
2790 it->selective_display_ellipsis_p
2791 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2792
2793 /* Display table to use. */
2794 it->dp = window_display_table (w);
2795
2796 /* Are multibyte characters enabled in current_buffer? */
2797 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2798
2799 /* Get the position at which the redisplay_end_trigger hook should
2800 be run, if it is to be run at all. */
2801 if (MARKERP (w->redisplay_end_trigger)
2802 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2803 it->redisplay_end_trigger_charpos
2804 = marker_position (w->redisplay_end_trigger);
2805 else if (INTEGERP (w->redisplay_end_trigger))
2806 it->redisplay_end_trigger_charpos =
2807 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2808
2809 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2810
2811 /* Are lines in the display truncated? */
2812 if (base_face_id != DEFAULT_FACE_ID
2813 || it->w->hscroll
2814 || (! WINDOW_FULL_WIDTH_P (it->w)
2815 && ((!NILP (Vtruncate_partial_width_windows)
2816 && !INTEGERP (Vtruncate_partial_width_windows))
2817 || (INTEGERP (Vtruncate_partial_width_windows)
2818 /* PXW: Shall we do something about this? */
2819 && (WINDOW_TOTAL_COLS (it->w)
2820 < XINT (Vtruncate_partial_width_windows))))))
2821 it->line_wrap = TRUNCATE;
2822 else if (NILP (BVAR (current_buffer, truncate_lines)))
2823 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2824 ? WINDOW_WRAP : WORD_WRAP;
2825 else
2826 it->line_wrap = TRUNCATE;
2827
2828 /* Get dimensions of truncation and continuation glyphs. These are
2829 displayed as fringe bitmaps under X, but we need them for such
2830 frames when the fringes are turned off. But leave the dimensions
2831 zero for tooltip frames, as these glyphs look ugly there and also
2832 sabotage calculations of tooltip dimensions in x-show-tip. */
2833 #ifdef HAVE_WINDOW_SYSTEM
2834 if (!(FRAME_WINDOW_P (it->f)
2835 && FRAMEP (tip_frame)
2836 && it->f == XFRAME (tip_frame)))
2837 #endif
2838 {
2839 if (it->line_wrap == TRUNCATE)
2840 {
2841 /* We will need the truncation glyph. */
2842 eassert (it->glyph_row == NULL);
2843 produce_special_glyphs (it, IT_TRUNCATION);
2844 it->truncation_pixel_width = it->pixel_width;
2845 }
2846 else
2847 {
2848 /* We will need the continuation glyph. */
2849 eassert (it->glyph_row == NULL);
2850 produce_special_glyphs (it, IT_CONTINUATION);
2851 it->continuation_pixel_width = it->pixel_width;
2852 }
2853 }
2854
2855 /* Reset these values to zero because the produce_special_glyphs
2856 above has changed them. */
2857 it->pixel_width = it->ascent = it->descent = 0;
2858 it->phys_ascent = it->phys_descent = 0;
2859
2860 /* Set this after getting the dimensions of truncation and
2861 continuation glyphs, so that we don't produce glyphs when calling
2862 produce_special_glyphs, above. */
2863 it->glyph_row = row;
2864 it->area = TEXT_AREA;
2865
2866 /* Forget any previous info about this row being reversed. */
2867 if (it->glyph_row)
2868 it->glyph_row->reversed_p = 0;
2869
2870 /* Get the dimensions of the display area. The display area
2871 consists of the visible window area plus a horizontally scrolled
2872 part to the left of the window. All x-values are relative to the
2873 start of this total display area. */
2874 if (base_face_id != DEFAULT_FACE_ID)
2875 {
2876 /* Mode lines, menu bar in terminal frames. */
2877 it->first_visible_x = 0;
2878 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2879 }
2880 else
2881 {
2882 it->first_visible_x
2883 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2884 it->last_visible_x = (it->first_visible_x
2885 + window_box_width (w, TEXT_AREA));
2886
2887 /* If we truncate lines, leave room for the truncation glyph(s) at
2888 the right margin. Otherwise, leave room for the continuation
2889 glyph(s). Done only if the window has no fringes. Since we
2890 don't know at this point whether there will be any R2L lines in
2891 the window, we reserve space for truncation/continuation glyphs
2892 even if only one of the fringes is absent. */
2893 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2894 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2895 {
2896 if (it->line_wrap == TRUNCATE)
2897 it->last_visible_x -= it->truncation_pixel_width;
2898 else
2899 it->last_visible_x -= it->continuation_pixel_width;
2900 }
2901
2902 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2903 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2904 }
2905
2906 /* Leave room for a border glyph. */
2907 if (!FRAME_WINDOW_P (it->f)
2908 && !WINDOW_RIGHTMOST_P (it->w))
2909 it->last_visible_x -= 1;
2910
2911 it->last_visible_y = window_text_bottom_y (w);
2912
2913 /* For mode lines and alike, arrange for the first glyph having a
2914 left box line if the face specifies a box. */
2915 if (base_face_id != DEFAULT_FACE_ID)
2916 {
2917 struct face *face;
2918
2919 it->face_id = remapped_base_face_id;
2920
2921 /* If we have a boxed mode line, make the first character appear
2922 with a left box line. */
2923 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2924 if (face->box != FACE_NO_BOX)
2925 it->start_of_box_run_p = true;
2926 }
2927
2928 /* If a buffer position was specified, set the iterator there,
2929 getting overlays and face properties from that position. */
2930 if (charpos >= BUF_BEG (current_buffer))
2931 {
2932 it->end_charpos = ZV;
2933 eassert (charpos == BYTE_TO_CHAR (bytepos));
2934 IT_CHARPOS (*it) = charpos;
2935 IT_BYTEPOS (*it) = bytepos;
2936
2937 /* We will rely on `reseat' to set this up properly, via
2938 handle_face_prop. */
2939 it->face_id = it->base_face_id;
2940
2941 it->start = it->current;
2942 /* Do we need to reorder bidirectional text? Not if this is a
2943 unibyte buffer: by definition, none of the single-byte
2944 characters are strong R2L, so no reordering is needed. And
2945 bidi.c doesn't support unibyte buffers anyway. Also, don't
2946 reorder while we are loading loadup.el, since the tables of
2947 character properties needed for reordering are not yet
2948 available. */
2949 it->bidi_p =
2950 NILP (Vpurify_flag)
2951 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2952 && it->multibyte_p;
2953
2954 /* If we are to reorder bidirectional text, init the bidi
2955 iterator. */
2956 if (it->bidi_p)
2957 {
2958 /* Note the paragraph direction that this buffer wants to
2959 use. */
2960 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2961 Qleft_to_right))
2962 it->paragraph_embedding = L2R;
2963 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2964 Qright_to_left))
2965 it->paragraph_embedding = R2L;
2966 else
2967 it->paragraph_embedding = NEUTRAL_DIR;
2968 bidi_unshelve_cache (NULL, 0);
2969 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2970 &it->bidi_it);
2971 }
2972
2973 /* Compute faces etc. */
2974 reseat (it, it->current.pos, 1);
2975 }
2976
2977 CHECK_IT (it);
2978 }
2979
2980
2981 /* Initialize IT for the display of window W with window start POS. */
2982
2983 void
2984 start_display (struct it *it, struct window *w, struct text_pos pos)
2985 {
2986 struct glyph_row *row;
2987 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2988
2989 row = w->desired_matrix->rows + first_vpos;
2990 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2991 it->first_vpos = first_vpos;
2992
2993 /* Don't reseat to previous visible line start if current start
2994 position is in a string or image. */
2995 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2996 {
2997 int start_at_line_beg_p;
2998 int first_y = it->current_y;
2999
3000 /* If window start is not at a line start, skip forward to POS to
3001 get the correct continuation lines width. */
3002 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3003 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3004 if (!start_at_line_beg_p)
3005 {
3006 int new_x;
3007
3008 reseat_at_previous_visible_line_start (it);
3009 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3010
3011 new_x = it->current_x + it->pixel_width;
3012
3013 /* If lines are continued, this line may end in the middle
3014 of a multi-glyph character (e.g. a control character
3015 displayed as \003, or in the middle of an overlay
3016 string). In this case move_it_to above will not have
3017 taken us to the start of the continuation line but to the
3018 end of the continued line. */
3019 if (it->current_x > 0
3020 && it->line_wrap != TRUNCATE /* Lines are continued. */
3021 && (/* And glyph doesn't fit on the line. */
3022 new_x > it->last_visible_x
3023 /* Or it fits exactly and we're on a window
3024 system frame. */
3025 || (new_x == it->last_visible_x
3026 && FRAME_WINDOW_P (it->f)
3027 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3028 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3029 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3030 {
3031 if ((it->current.dpvec_index >= 0
3032 || it->current.overlay_string_index >= 0)
3033 /* If we are on a newline from a display vector or
3034 overlay string, then we are already at the end of
3035 a screen line; no need to go to the next line in
3036 that case, as this line is not really continued.
3037 (If we do go to the next line, C-e will not DTRT.) */
3038 && it->c != '\n')
3039 {
3040 set_iterator_to_next (it, 1);
3041 move_it_in_display_line_to (it, -1, -1, 0);
3042 }
3043
3044 it->continuation_lines_width += it->current_x;
3045 }
3046 /* If the character at POS is displayed via a display
3047 vector, move_it_to above stops at the final glyph of
3048 IT->dpvec. To make the caller redisplay that character
3049 again (a.k.a. start at POS), we need to reset the
3050 dpvec_index to the beginning of IT->dpvec. */
3051 else if (it->current.dpvec_index >= 0)
3052 it->current.dpvec_index = 0;
3053
3054 /* We're starting a new display line, not affected by the
3055 height of the continued line, so clear the appropriate
3056 fields in the iterator structure. */
3057 it->max_ascent = it->max_descent = 0;
3058 it->max_phys_ascent = it->max_phys_descent = 0;
3059
3060 it->current_y = first_y;
3061 it->vpos = 0;
3062 it->current_x = it->hpos = 0;
3063 }
3064 }
3065 }
3066
3067
3068 /* Return 1 if POS is a position in ellipses displayed for invisible
3069 text. W is the window we display, for text property lookup. */
3070
3071 static int
3072 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3073 {
3074 Lisp_Object prop, window;
3075 int ellipses_p = 0;
3076 ptrdiff_t charpos = CHARPOS (pos->pos);
3077
3078 /* If POS specifies a position in a display vector, this might
3079 be for an ellipsis displayed for invisible text. We won't
3080 get the iterator set up for delivering that ellipsis unless
3081 we make sure that it gets aware of the invisible text. */
3082 if (pos->dpvec_index >= 0
3083 && pos->overlay_string_index < 0
3084 && CHARPOS (pos->string_pos) < 0
3085 && charpos > BEGV
3086 && (XSETWINDOW (window, w),
3087 prop = Fget_char_property (make_number (charpos),
3088 Qinvisible, window),
3089 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3090 {
3091 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3092 window);
3093 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3094 }
3095
3096 return ellipses_p;
3097 }
3098
3099
3100 /* Initialize IT for stepping through current_buffer in window W,
3101 starting at position POS that includes overlay string and display
3102 vector/ control character translation position information. Value
3103 is zero if there are overlay strings with newlines at POS. */
3104
3105 static int
3106 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3107 {
3108 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3109 int i, overlay_strings_with_newlines = 0;
3110
3111 /* If POS specifies a position in a display vector, this might
3112 be for an ellipsis displayed for invisible text. We won't
3113 get the iterator set up for delivering that ellipsis unless
3114 we make sure that it gets aware of the invisible text. */
3115 if (in_ellipses_for_invisible_text_p (pos, w))
3116 {
3117 --charpos;
3118 bytepos = 0;
3119 }
3120
3121 /* Keep in mind: the call to reseat in init_iterator skips invisible
3122 text, so we might end up at a position different from POS. This
3123 is only a problem when POS is a row start after a newline and an
3124 overlay starts there with an after-string, and the overlay has an
3125 invisible property. Since we don't skip invisible text in
3126 display_line and elsewhere immediately after consuming the
3127 newline before the row start, such a POS will not be in a string,
3128 but the call to init_iterator below will move us to the
3129 after-string. */
3130 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3131
3132 /* This only scans the current chunk -- it should scan all chunks.
3133 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3134 to 16 in 22.1 to make this a lesser problem. */
3135 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3136 {
3137 const char *s = SSDATA (it->overlay_strings[i]);
3138 const char *e = s + SBYTES (it->overlay_strings[i]);
3139
3140 while (s < e && *s != '\n')
3141 ++s;
3142
3143 if (s < e)
3144 {
3145 overlay_strings_with_newlines = 1;
3146 break;
3147 }
3148 }
3149
3150 /* If position is within an overlay string, set up IT to the right
3151 overlay string. */
3152 if (pos->overlay_string_index >= 0)
3153 {
3154 int relative_index;
3155
3156 /* If the first overlay string happens to have a `display'
3157 property for an image, the iterator will be set up for that
3158 image, and we have to undo that setup first before we can
3159 correct the overlay string index. */
3160 if (it->method == GET_FROM_IMAGE)
3161 pop_it (it);
3162
3163 /* We already have the first chunk of overlay strings in
3164 IT->overlay_strings. Load more until the one for
3165 pos->overlay_string_index is in IT->overlay_strings. */
3166 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3167 {
3168 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3169 it->current.overlay_string_index = 0;
3170 while (n--)
3171 {
3172 load_overlay_strings (it, 0);
3173 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3174 }
3175 }
3176
3177 it->current.overlay_string_index = pos->overlay_string_index;
3178 relative_index = (it->current.overlay_string_index
3179 % OVERLAY_STRING_CHUNK_SIZE);
3180 it->string = it->overlay_strings[relative_index];
3181 eassert (STRINGP (it->string));
3182 it->current.string_pos = pos->string_pos;
3183 it->method = GET_FROM_STRING;
3184 it->end_charpos = SCHARS (it->string);
3185 /* Set up the bidi iterator for this overlay string. */
3186 if (it->bidi_p)
3187 {
3188 it->bidi_it.string.lstring = it->string;
3189 it->bidi_it.string.s = NULL;
3190 it->bidi_it.string.schars = SCHARS (it->string);
3191 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3192 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3193 it->bidi_it.string.unibyte = !it->multibyte_p;
3194 it->bidi_it.w = it->w;
3195 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3196 FRAME_WINDOW_P (it->f), &it->bidi_it);
3197
3198 /* Synchronize the state of the bidi iterator with
3199 pos->string_pos. For any string position other than
3200 zero, this will be done automagically when we resume
3201 iteration over the string and get_visually_first_element
3202 is called. But if string_pos is zero, and the string is
3203 to be reordered for display, we need to resync manually,
3204 since it could be that the iteration state recorded in
3205 pos ended at string_pos of 0 moving backwards in string. */
3206 if (CHARPOS (pos->string_pos) == 0)
3207 {
3208 get_visually_first_element (it);
3209 if (IT_STRING_CHARPOS (*it) != 0)
3210 do {
3211 /* Paranoia. */
3212 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3213 bidi_move_to_visually_next (&it->bidi_it);
3214 } while (it->bidi_it.charpos != 0);
3215 }
3216 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3217 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3218 }
3219 }
3220
3221 if (CHARPOS (pos->string_pos) >= 0)
3222 {
3223 /* Recorded position is not in an overlay string, but in another
3224 string. This can only be a string from a `display' property.
3225 IT should already be filled with that string. */
3226 it->current.string_pos = pos->string_pos;
3227 eassert (STRINGP (it->string));
3228 if (it->bidi_p)
3229 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3230 FRAME_WINDOW_P (it->f), &it->bidi_it);
3231 }
3232
3233 /* Restore position in display vector translations, control
3234 character translations or ellipses. */
3235 if (pos->dpvec_index >= 0)
3236 {
3237 if (it->dpvec == NULL)
3238 get_next_display_element (it);
3239 eassert (it->dpvec && it->current.dpvec_index == 0);
3240 it->current.dpvec_index = pos->dpvec_index;
3241 }
3242
3243 CHECK_IT (it);
3244 return !overlay_strings_with_newlines;
3245 }
3246
3247
3248 /* Initialize IT for stepping through current_buffer in window W
3249 starting at ROW->start. */
3250
3251 static void
3252 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3253 {
3254 init_from_display_pos (it, w, &row->start);
3255 it->start = row->start;
3256 it->continuation_lines_width = row->continuation_lines_width;
3257 CHECK_IT (it);
3258 }
3259
3260
3261 /* Initialize IT for stepping through current_buffer in window W
3262 starting in the line following ROW, i.e. starting at ROW->end.
3263 Value is zero if there are overlay strings with newlines at ROW's
3264 end position. */
3265
3266 static int
3267 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3268 {
3269 int success = 0;
3270
3271 if (init_from_display_pos (it, w, &row->end))
3272 {
3273 if (row->continued_p)
3274 it->continuation_lines_width
3275 = row->continuation_lines_width + row->pixel_width;
3276 CHECK_IT (it);
3277 success = 1;
3278 }
3279
3280 return success;
3281 }
3282
3283
3284
3285 \f
3286 /***********************************************************************
3287 Text properties
3288 ***********************************************************************/
3289
3290 /* Called when IT reaches IT->stop_charpos. Handle text property and
3291 overlay changes. Set IT->stop_charpos to the next position where
3292 to stop. */
3293
3294 static void
3295 handle_stop (struct it *it)
3296 {
3297 enum prop_handled handled;
3298 int handle_overlay_change_p;
3299 struct props *p;
3300
3301 it->dpvec = NULL;
3302 it->current.dpvec_index = -1;
3303 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3304 it->ignore_overlay_strings_at_pos_p = 0;
3305 it->ellipsis_p = 0;
3306
3307 /* Use face of preceding text for ellipsis (if invisible) */
3308 if (it->selective_display_ellipsis_p)
3309 it->saved_face_id = it->face_id;
3310
3311 do
3312 {
3313 handled = HANDLED_NORMALLY;
3314
3315 /* Call text property handlers. */
3316 for (p = it_props; p->handler; ++p)
3317 {
3318 handled = p->handler (it);
3319
3320 if (handled == HANDLED_RECOMPUTE_PROPS)
3321 break;
3322 else if (handled == HANDLED_RETURN)
3323 {
3324 /* We still want to show before and after strings from
3325 overlays even if the actual buffer text is replaced. */
3326 if (!handle_overlay_change_p
3327 || it->sp > 1
3328 /* Don't call get_overlay_strings_1 if we already
3329 have overlay strings loaded, because doing so
3330 will load them again and push the iterator state
3331 onto the stack one more time, which is not
3332 expected by the rest of the code that processes
3333 overlay strings. */
3334 || (it->current.overlay_string_index < 0
3335 ? !get_overlay_strings_1 (it, 0, 0)
3336 : 0))
3337 {
3338 if (it->ellipsis_p)
3339 setup_for_ellipsis (it, 0);
3340 /* When handling a display spec, we might load an
3341 empty string. In that case, discard it here. We
3342 used to discard it in handle_single_display_spec,
3343 but that causes get_overlay_strings_1, above, to
3344 ignore overlay strings that we must check. */
3345 if (STRINGP (it->string) && !SCHARS (it->string))
3346 pop_it (it);
3347 return;
3348 }
3349 else if (STRINGP (it->string) && !SCHARS (it->string))
3350 pop_it (it);
3351 else
3352 {
3353 it->ignore_overlay_strings_at_pos_p = true;
3354 it->string_from_display_prop_p = 0;
3355 it->from_disp_prop_p = 0;
3356 handle_overlay_change_p = 0;
3357 }
3358 handled = HANDLED_RECOMPUTE_PROPS;
3359 break;
3360 }
3361 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3362 handle_overlay_change_p = 0;
3363 }
3364
3365 if (handled != HANDLED_RECOMPUTE_PROPS)
3366 {
3367 /* Don't check for overlay strings below when set to deliver
3368 characters from a display vector. */
3369 if (it->method == GET_FROM_DISPLAY_VECTOR)
3370 handle_overlay_change_p = 0;
3371
3372 /* Handle overlay changes.
3373 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3374 if it finds overlays. */
3375 if (handle_overlay_change_p)
3376 handled = handle_overlay_change (it);
3377 }
3378
3379 if (it->ellipsis_p)
3380 {
3381 setup_for_ellipsis (it, 0);
3382 break;
3383 }
3384 }
3385 while (handled == HANDLED_RECOMPUTE_PROPS);
3386
3387 /* Determine where to stop next. */
3388 if (handled == HANDLED_NORMALLY)
3389 compute_stop_pos (it);
3390 }
3391
3392
3393 /* Compute IT->stop_charpos from text property and overlay change
3394 information for IT's current position. */
3395
3396 static void
3397 compute_stop_pos (struct it *it)
3398 {
3399 register INTERVAL iv, next_iv;
3400 Lisp_Object object, limit, position;
3401 ptrdiff_t charpos, bytepos;
3402
3403 if (STRINGP (it->string))
3404 {
3405 /* Strings are usually short, so don't limit the search for
3406 properties. */
3407 it->stop_charpos = it->end_charpos;
3408 object = it->string;
3409 limit = Qnil;
3410 charpos = IT_STRING_CHARPOS (*it);
3411 bytepos = IT_STRING_BYTEPOS (*it);
3412 }
3413 else
3414 {
3415 ptrdiff_t pos;
3416
3417 /* If end_charpos is out of range for some reason, such as a
3418 misbehaving display function, rationalize it (Bug#5984). */
3419 if (it->end_charpos > ZV)
3420 it->end_charpos = ZV;
3421 it->stop_charpos = it->end_charpos;
3422
3423 /* If next overlay change is in front of the current stop pos
3424 (which is IT->end_charpos), stop there. Note: value of
3425 next_overlay_change is point-max if no overlay change
3426 follows. */
3427 charpos = IT_CHARPOS (*it);
3428 bytepos = IT_BYTEPOS (*it);
3429 pos = next_overlay_change (charpos);
3430 if (pos < it->stop_charpos)
3431 it->stop_charpos = pos;
3432
3433 /* Set up variables for computing the stop position from text
3434 property changes. */
3435 XSETBUFFER (object, current_buffer);
3436 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3437 }
3438
3439 /* Get the interval containing IT's position. Value is a null
3440 interval if there isn't such an interval. */
3441 position = make_number (charpos);
3442 iv = validate_interval_range (object, &position, &position, 0);
3443 if (iv)
3444 {
3445 Lisp_Object values_here[LAST_PROP_IDX];
3446 struct props *p;
3447
3448 /* Get properties here. */
3449 for (p = it_props; p->handler; ++p)
3450 values_here[p->idx] = textget (iv->plist, *p->name);
3451
3452 /* Look for an interval following iv that has different
3453 properties. */
3454 for (next_iv = next_interval (iv);
3455 (next_iv
3456 && (NILP (limit)
3457 || XFASTINT (limit) > next_iv->position));
3458 next_iv = next_interval (next_iv))
3459 {
3460 for (p = it_props; p->handler; ++p)
3461 {
3462 Lisp_Object new_value;
3463
3464 new_value = textget (next_iv->plist, *p->name);
3465 if (!EQ (values_here[p->idx], new_value))
3466 break;
3467 }
3468
3469 if (p->handler)
3470 break;
3471 }
3472
3473 if (next_iv)
3474 {
3475 if (INTEGERP (limit)
3476 && next_iv->position >= XFASTINT (limit))
3477 /* No text property change up to limit. */
3478 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3479 else
3480 /* Text properties change in next_iv. */
3481 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3482 }
3483 }
3484
3485 if (it->cmp_it.id < 0)
3486 {
3487 ptrdiff_t stoppos = it->end_charpos;
3488
3489 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3490 stoppos = -1;
3491 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3492 stoppos, it->string);
3493 }
3494
3495 eassert (STRINGP (it->string)
3496 || (it->stop_charpos >= BEGV
3497 && it->stop_charpos >= IT_CHARPOS (*it)));
3498 }
3499
3500
3501 /* Return the position of the next overlay change after POS in
3502 current_buffer. Value is point-max if no overlay change
3503 follows. This is like `next-overlay-change' but doesn't use
3504 xmalloc. */
3505
3506 static ptrdiff_t
3507 next_overlay_change (ptrdiff_t pos)
3508 {
3509 ptrdiff_t i, noverlays;
3510 ptrdiff_t endpos;
3511 Lisp_Object *overlays;
3512
3513 /* Get all overlays at the given position. */
3514 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3515
3516 /* If any of these overlays ends before endpos,
3517 use its ending point instead. */
3518 for (i = 0; i < noverlays; ++i)
3519 {
3520 Lisp_Object oend;
3521 ptrdiff_t oendpos;
3522
3523 oend = OVERLAY_END (overlays[i]);
3524 oendpos = OVERLAY_POSITION (oend);
3525 endpos = min (endpos, oendpos);
3526 }
3527
3528 return endpos;
3529 }
3530
3531 /* How many characters forward to search for a display property or
3532 display string. Searching too far forward makes the bidi display
3533 sluggish, especially in small windows. */
3534 #define MAX_DISP_SCAN 250
3535
3536 /* Return the character position of a display string at or after
3537 position specified by POSITION. If no display string exists at or
3538 after POSITION, return ZV. A display string is either an overlay
3539 with `display' property whose value is a string, or a `display'
3540 text property whose value is a string. STRING is data about the
3541 string to iterate; if STRING->lstring is nil, we are iterating a
3542 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3543 on a GUI frame. DISP_PROP is set to zero if we searched
3544 MAX_DISP_SCAN characters forward without finding any display
3545 strings, non-zero otherwise. It is set to 2 if the display string
3546 uses any kind of `(space ...)' spec that will produce a stretch of
3547 white space in the text area. */
3548 ptrdiff_t
3549 compute_display_string_pos (struct text_pos *position,
3550 struct bidi_string_data *string,
3551 struct window *w,
3552 int frame_window_p, int *disp_prop)
3553 {
3554 /* OBJECT = nil means current buffer. */
3555 Lisp_Object object, object1;
3556 Lisp_Object pos, spec, limpos;
3557 int string_p = (string && (STRINGP (string->lstring) || string->s));
3558 ptrdiff_t eob = string_p ? string->schars : ZV;
3559 ptrdiff_t begb = string_p ? 0 : BEGV;
3560 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3561 ptrdiff_t lim =
3562 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3563 struct text_pos tpos;
3564 int rv = 0;
3565
3566 if (string && STRINGP (string->lstring))
3567 object1 = object = string->lstring;
3568 else if (w && !string_p)
3569 {
3570 XSETWINDOW (object, w);
3571 object1 = Qnil;
3572 }
3573 else
3574 object1 = object = Qnil;
3575
3576 *disp_prop = 1;
3577
3578 if (charpos >= eob
3579 /* We don't support display properties whose values are strings
3580 that have display string properties. */
3581 || string->from_disp_str
3582 /* C strings cannot have display properties. */
3583 || (string->s && !STRINGP (object)))
3584 {
3585 *disp_prop = 0;
3586 return eob;
3587 }
3588
3589 /* If the character at CHARPOS is where the display string begins,
3590 return CHARPOS. */
3591 pos = make_number (charpos);
3592 if (STRINGP (object))
3593 bufpos = string->bufpos;
3594 else
3595 bufpos = charpos;
3596 tpos = *position;
3597 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3598 && (charpos <= begb
3599 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3600 object),
3601 spec))
3602 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3603 frame_window_p)))
3604 {
3605 if (rv == 2)
3606 *disp_prop = 2;
3607 return charpos;
3608 }
3609
3610 /* Look forward for the first character with a `display' property
3611 that will replace the underlying text when displayed. */
3612 limpos = make_number (lim);
3613 do {
3614 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3615 CHARPOS (tpos) = XFASTINT (pos);
3616 if (CHARPOS (tpos) >= lim)
3617 {
3618 *disp_prop = 0;
3619 break;
3620 }
3621 if (STRINGP (object))
3622 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3623 else
3624 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3625 spec = Fget_char_property (pos, Qdisplay, object);
3626 if (!STRINGP (object))
3627 bufpos = CHARPOS (tpos);
3628 } while (NILP (spec)
3629 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3630 bufpos, frame_window_p)));
3631 if (rv == 2)
3632 *disp_prop = 2;
3633
3634 return CHARPOS (tpos);
3635 }
3636
3637 /* Return the character position of the end of the display string that
3638 started at CHARPOS. If there's no display string at CHARPOS,
3639 return -1. A display string is either an overlay with `display'
3640 property whose value is a string or a `display' text property whose
3641 value is a string. */
3642 ptrdiff_t
3643 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3644 {
3645 /* OBJECT = nil means current buffer. */
3646 Lisp_Object object =
3647 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3648 Lisp_Object pos = make_number (charpos);
3649 ptrdiff_t eob =
3650 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3651
3652 if (charpos >= eob || (string->s && !STRINGP (object)))
3653 return eob;
3654
3655 /* It could happen that the display property or overlay was removed
3656 since we found it in compute_display_string_pos above. One way
3657 this can happen is if JIT font-lock was called (through
3658 handle_fontified_prop), and jit-lock-functions remove text
3659 properties or overlays from the portion of buffer that includes
3660 CHARPOS. Muse mode is known to do that, for example. In this
3661 case, we return -1 to the caller, to signal that no display
3662 string is actually present at CHARPOS. See bidi_fetch_char for
3663 how this is handled.
3664
3665 An alternative would be to never look for display properties past
3666 it->stop_charpos. But neither compute_display_string_pos nor
3667 bidi_fetch_char that calls it know or care where the next
3668 stop_charpos is. */
3669 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3670 return -1;
3671
3672 /* Look forward for the first character where the `display' property
3673 changes. */
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3675
3676 return XFASTINT (pos);
3677 }
3678
3679
3680 \f
3681 /***********************************************************************
3682 Fontification
3683 ***********************************************************************/
3684
3685 /* Handle changes in the `fontified' property of the current buffer by
3686 calling hook functions from Qfontification_functions to fontify
3687 regions of text. */
3688
3689 static enum prop_handled
3690 handle_fontified_prop (struct it *it)
3691 {
3692 Lisp_Object prop, pos;
3693 enum prop_handled handled = HANDLED_NORMALLY;
3694
3695 if (!NILP (Vmemory_full))
3696 return handled;
3697
3698 /* Get the value of the `fontified' property at IT's current buffer
3699 position. (The `fontified' property doesn't have a special
3700 meaning in strings.) If the value is nil, call functions from
3701 Qfontification_functions. */
3702 if (!STRINGP (it->string)
3703 && it->s == NULL
3704 && !NILP (Vfontification_functions)
3705 && !NILP (Vrun_hooks)
3706 && (pos = make_number (IT_CHARPOS (*it)),
3707 prop = Fget_char_property (pos, Qfontified, Qnil),
3708 /* Ignore the special cased nil value always present at EOB since
3709 no amount of fontifying will be able to change it. */
3710 NILP (prop) && IT_CHARPOS (*it) < Z))
3711 {
3712 ptrdiff_t count = SPECPDL_INDEX ();
3713 Lisp_Object val;
3714 struct buffer *obuf = current_buffer;
3715 ptrdiff_t begv = BEGV, zv = ZV;
3716 bool old_clip_changed = current_buffer->clip_changed;
3717
3718 val = Vfontification_functions;
3719 specbind (Qfontification_functions, Qnil);
3720
3721 eassert (it->end_charpos == ZV);
3722
3723 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3724 safe_call1 (val, pos);
3725 else
3726 {
3727 Lisp_Object fns, fn;
3728 struct gcpro gcpro1, gcpro2;
3729
3730 fns = Qnil;
3731 GCPRO2 (val, fns);
3732
3733 for (; CONSP (val); val = XCDR (val))
3734 {
3735 fn = XCAR (val);
3736
3737 if (EQ (fn, Qt))
3738 {
3739 /* A value of t indicates this hook has a local
3740 binding; it means to run the global binding too.
3741 In a global value, t should not occur. If it
3742 does, we must ignore it to avoid an endless
3743 loop. */
3744 for (fns = Fdefault_value (Qfontification_functions);
3745 CONSP (fns);
3746 fns = XCDR (fns))
3747 {
3748 fn = XCAR (fns);
3749 if (!EQ (fn, Qt))
3750 safe_call1 (fn, pos);
3751 }
3752 }
3753 else
3754 safe_call1 (fn, pos);
3755 }
3756
3757 UNGCPRO;
3758 }
3759
3760 unbind_to (count, Qnil);
3761
3762 /* Fontification functions routinely call `save-restriction'.
3763 Normally, this tags clip_changed, which can confuse redisplay
3764 (see discussion in Bug#6671). Since we don't perform any
3765 special handling of fontification changes in the case where
3766 `save-restriction' isn't called, there's no point doing so in
3767 this case either. So, if the buffer's restrictions are
3768 actually left unchanged, reset clip_changed. */
3769 if (obuf == current_buffer)
3770 {
3771 if (begv == BEGV && zv == ZV)
3772 current_buffer->clip_changed = old_clip_changed;
3773 }
3774 /* There isn't much we can reasonably do to protect against
3775 misbehaving fontification, but here's a fig leaf. */
3776 else if (BUFFER_LIVE_P (obuf))
3777 set_buffer_internal_1 (obuf);
3778
3779 /* The fontification code may have added/removed text.
3780 It could do even a lot worse, but let's at least protect against
3781 the most obvious case where only the text past `pos' gets changed',
3782 as is/was done in grep.el where some escapes sequences are turned
3783 into face properties (bug#7876). */
3784 it->end_charpos = ZV;
3785
3786 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3787 something. This avoids an endless loop if they failed to
3788 fontify the text for which reason ever. */
3789 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3790 handled = HANDLED_RECOMPUTE_PROPS;
3791 }
3792
3793 return handled;
3794 }
3795
3796
3797 \f
3798 /***********************************************************************
3799 Faces
3800 ***********************************************************************/
3801
3802 /* Set up iterator IT from face properties at its current position.
3803 Called from handle_stop. */
3804
3805 static enum prop_handled
3806 handle_face_prop (struct it *it)
3807 {
3808 int new_face_id;
3809 ptrdiff_t next_stop;
3810
3811 if (!STRINGP (it->string))
3812 {
3813 new_face_id
3814 = face_at_buffer_position (it->w,
3815 IT_CHARPOS (*it),
3816 &next_stop,
3817 (IT_CHARPOS (*it)
3818 + TEXT_PROP_DISTANCE_LIMIT),
3819 0, it->base_face_id);
3820
3821 /* Is this a start of a run of characters with box face?
3822 Caveat: this can be called for a freshly initialized
3823 iterator; face_id is -1 in this case. We know that the new
3824 face will not change until limit, i.e. if the new face has a
3825 box, all characters up to limit will have one. But, as
3826 usual, we don't know whether limit is really the end. */
3827 if (new_face_id != it->face_id)
3828 {
3829 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3830 /* If it->face_id is -1, old_face below will be NULL, see
3831 the definition of FACE_FROM_ID. This will happen if this
3832 is the initial call that gets the face. */
3833 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3834
3835 /* If the value of face_id of the iterator is -1, we have to
3836 look in front of IT's position and see whether there is a
3837 face there that's different from new_face_id. */
3838 if (!old_face && IT_CHARPOS (*it) > BEG)
3839 {
3840 int prev_face_id = face_before_it_pos (it);
3841
3842 old_face = FACE_FROM_ID (it->f, prev_face_id);
3843 }
3844
3845 /* If the new face has a box, but the old face does not,
3846 this is the start of a run of characters with box face,
3847 i.e. this character has a shadow on the left side. */
3848 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3849 && (old_face == NULL || !old_face->box));
3850 it->face_box_p = new_face->box != FACE_NO_BOX;
3851 }
3852 }
3853 else
3854 {
3855 int base_face_id;
3856 ptrdiff_t bufpos;
3857 int i;
3858 Lisp_Object from_overlay
3859 = (it->current.overlay_string_index >= 0
3860 ? it->string_overlays[it->current.overlay_string_index
3861 % OVERLAY_STRING_CHUNK_SIZE]
3862 : Qnil);
3863
3864 /* See if we got to this string directly or indirectly from
3865 an overlay property. That includes the before-string or
3866 after-string of an overlay, strings in display properties
3867 provided by an overlay, their text properties, etc.
3868
3869 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3870 if (! NILP (from_overlay))
3871 for (i = it->sp - 1; i >= 0; i--)
3872 {
3873 if (it->stack[i].current.overlay_string_index >= 0)
3874 from_overlay
3875 = it->string_overlays[it->stack[i].current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE];
3877 else if (! NILP (it->stack[i].from_overlay))
3878 from_overlay = it->stack[i].from_overlay;
3879
3880 if (!NILP (from_overlay))
3881 break;
3882 }
3883
3884 if (! NILP (from_overlay))
3885 {
3886 bufpos = IT_CHARPOS (*it);
3887 /* For a string from an overlay, the base face depends
3888 only on text properties and ignores overlays. */
3889 base_face_id
3890 = face_for_overlay_string (it->w,
3891 IT_CHARPOS (*it),
3892 &next_stop,
3893 (IT_CHARPOS (*it)
3894 + TEXT_PROP_DISTANCE_LIMIT),
3895 0,
3896 from_overlay);
3897 }
3898 else
3899 {
3900 bufpos = 0;
3901
3902 /* For strings from a `display' property, use the face at
3903 IT's current buffer position as the base face to merge
3904 with, so that overlay strings appear in the same face as
3905 surrounding text, unless they specify their own faces.
3906 For strings from wrap-prefix and line-prefix properties,
3907 use the default face, possibly remapped via
3908 Vface_remapping_alist. */
3909 base_face_id = it->string_from_prefix_prop_p
3910 ? (!NILP (Vface_remapping_alist)
3911 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3912 : DEFAULT_FACE_ID)
3913 : underlying_face_id (it);
3914 }
3915
3916 new_face_id = face_at_string_position (it->w,
3917 it->string,
3918 IT_STRING_CHARPOS (*it),
3919 bufpos,
3920 &next_stop,
3921 base_face_id, 0);
3922
3923 /* Is this a start of a run of characters with box? Caveat:
3924 this can be called for a freshly allocated iterator; face_id
3925 is -1 is this case. We know that the new face will not
3926 change until the next check pos, i.e. if the new face has a
3927 box, all characters up to that position will have a
3928 box. But, as usual, we don't know whether that position
3929 is really the end. */
3930 if (new_face_id != it->face_id)
3931 {
3932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3933 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3934
3935 /* If new face has a box but old face hasn't, this is the
3936 start of a run of characters with box, i.e. it has a
3937 shadow on the left side. */
3938 it->start_of_box_run_p
3939 = new_face->box && (old_face == NULL || !old_face->box);
3940 it->face_box_p = new_face->box != FACE_NO_BOX;
3941 }
3942 }
3943
3944 it->face_id = new_face_id;
3945 return HANDLED_NORMALLY;
3946 }
3947
3948
3949 /* Return the ID of the face ``underlying'' IT's current position,
3950 which is in a string. If the iterator is associated with a
3951 buffer, return the face at IT's current buffer position.
3952 Otherwise, use the iterator's base_face_id. */
3953
3954 static int
3955 underlying_face_id (struct it *it)
3956 {
3957 int face_id = it->base_face_id, i;
3958
3959 eassert (STRINGP (it->string));
3960
3961 for (i = it->sp - 1; i >= 0; --i)
3962 if (NILP (it->stack[i].string))
3963 face_id = it->stack[i].face_id;
3964
3965 return face_id;
3966 }
3967
3968
3969 /* Compute the face one character before or after the current position
3970 of IT, in the visual order. BEFORE_P non-zero means get the face
3971 in front (to the left in L2R paragraphs, to the right in R2L
3972 paragraphs) of IT's screen position. Value is the ID of the face. */
3973
3974 static int
3975 face_before_or_after_it_pos (struct it *it, int before_p)
3976 {
3977 int face_id, limit;
3978 ptrdiff_t next_check_charpos;
3979 struct it it_copy;
3980 void *it_copy_data = NULL;
3981
3982 eassert (it->s == NULL);
3983
3984 if (STRINGP (it->string))
3985 {
3986 ptrdiff_t bufpos, charpos;
3987 int base_face_id;
3988
3989 /* No face change past the end of the string (for the case
3990 we are padding with spaces). No face change before the
3991 string start. */
3992 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3993 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3994 return it->face_id;
3995
3996 if (!it->bidi_p)
3997 {
3998 /* Set charpos to the position before or after IT's current
3999 position, in the logical order, which in the non-bidi
4000 case is the same as the visual order. */
4001 if (before_p)
4002 charpos = IT_STRING_CHARPOS (*it) - 1;
4003 else if (it->what == IT_COMPOSITION)
4004 /* For composition, we must check the character after the
4005 composition. */
4006 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4007 else
4008 charpos = IT_STRING_CHARPOS (*it) + 1;
4009 }
4010 else
4011 {
4012 if (before_p)
4013 {
4014 /* With bidi iteration, the character before the current
4015 in the visual order cannot be found by simple
4016 iteration, because "reverse" reordering is not
4017 supported. Instead, we need to use the move_it_*
4018 family of functions. */
4019 /* Ignore face changes before the first visible
4020 character on this display line. */
4021 if (it->current_x <= it->first_visible_x)
4022 return it->face_id;
4023 SAVE_IT (it_copy, *it, it_copy_data);
4024 /* Implementation note: Since move_it_in_display_line
4025 works in the iterator geometry, and thinks the first
4026 character is always the leftmost, even in R2L lines,
4027 we don't need to distinguish between the R2L and L2R
4028 cases here. */
4029 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4030 it_copy.current_x - 1, MOVE_TO_X);
4031 charpos = IT_STRING_CHARPOS (it_copy);
4032 RESTORE_IT (it, it, it_copy_data);
4033 }
4034 else
4035 {
4036 /* Set charpos to the string position of the character
4037 that comes after IT's current position in the visual
4038 order. */
4039 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4040
4041 it_copy = *it;
4042 while (n--)
4043 bidi_move_to_visually_next (&it_copy.bidi_it);
4044
4045 charpos = it_copy.bidi_it.charpos;
4046 }
4047 }
4048 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4049
4050 if (it->current.overlay_string_index >= 0)
4051 bufpos = IT_CHARPOS (*it);
4052 else
4053 bufpos = 0;
4054
4055 base_face_id = underlying_face_id (it);
4056
4057 /* Get the face for ASCII, or unibyte. */
4058 face_id = face_at_string_position (it->w,
4059 it->string,
4060 charpos,
4061 bufpos,
4062 &next_check_charpos,
4063 base_face_id, 0);
4064
4065 /* Correct the face for charsets different from ASCII. Do it
4066 for the multibyte case only. The face returned above is
4067 suitable for unibyte text if IT->string is unibyte. */
4068 if (STRING_MULTIBYTE (it->string))
4069 {
4070 struct text_pos pos1 = string_pos (charpos, it->string);
4071 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4072 int c, len;
4073 struct face *face = FACE_FROM_ID (it->f, face_id);
4074
4075 c = string_char_and_length (p, &len);
4076 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4077 }
4078 }
4079 else
4080 {
4081 struct text_pos pos;
4082
4083 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4084 || (IT_CHARPOS (*it) <= BEGV && before_p))
4085 return it->face_id;
4086
4087 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4088 pos = it->current.pos;
4089
4090 if (!it->bidi_p)
4091 {
4092 if (before_p)
4093 DEC_TEXT_POS (pos, it->multibyte_p);
4094 else
4095 {
4096 if (it->what == IT_COMPOSITION)
4097 {
4098 /* For composition, we must check the position after
4099 the composition. */
4100 pos.charpos += it->cmp_it.nchars;
4101 pos.bytepos += it->len;
4102 }
4103 else
4104 INC_TEXT_POS (pos, it->multibyte_p);
4105 }
4106 }
4107 else
4108 {
4109 if (before_p)
4110 {
4111 /* With bidi iteration, the character before the current
4112 in the visual order cannot be found by simple
4113 iteration, because "reverse" reordering is not
4114 supported. Instead, we need to use the move_it_*
4115 family of functions. */
4116 /* Ignore face changes before the first visible
4117 character on this display line. */
4118 if (it->current_x <= it->first_visible_x)
4119 return it->face_id;
4120 SAVE_IT (it_copy, *it, it_copy_data);
4121 /* Implementation note: Since move_it_in_display_line
4122 works in the iterator geometry, and thinks the first
4123 character is always the leftmost, even in R2L lines,
4124 we don't need to distinguish between the R2L and L2R
4125 cases here. */
4126 move_it_in_display_line (&it_copy, ZV,
4127 it_copy.current_x - 1, MOVE_TO_X);
4128 pos = it_copy.current.pos;
4129 RESTORE_IT (it, it, it_copy_data);
4130 }
4131 else
4132 {
4133 /* Set charpos to the buffer position of the character
4134 that comes after IT's current position in the visual
4135 order. */
4136 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4137
4138 it_copy = *it;
4139 while (n--)
4140 bidi_move_to_visually_next (&it_copy.bidi_it);
4141
4142 SET_TEXT_POS (pos,
4143 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4144 }
4145 }
4146 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4147
4148 /* Determine face for CHARSET_ASCII, or unibyte. */
4149 face_id = face_at_buffer_position (it->w,
4150 CHARPOS (pos),
4151 &next_check_charpos,
4152 limit, 0, -1);
4153
4154 /* Correct the face for charsets different from ASCII. Do it
4155 for the multibyte case only. The face returned above is
4156 suitable for unibyte text if current_buffer is unibyte. */
4157 if (it->multibyte_p)
4158 {
4159 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4160 struct face *face = FACE_FROM_ID (it->f, face_id);
4161 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4162 }
4163 }
4164
4165 return face_id;
4166 }
4167
4168
4169 \f
4170 /***********************************************************************
4171 Invisible text
4172 ***********************************************************************/
4173
4174 /* Set up iterator IT from invisible properties at its current
4175 position. Called from handle_stop. */
4176
4177 static enum prop_handled
4178 handle_invisible_prop (struct it *it)
4179 {
4180 enum prop_handled handled = HANDLED_NORMALLY;
4181 int invis_p;
4182 Lisp_Object prop;
4183
4184 if (STRINGP (it->string))
4185 {
4186 Lisp_Object end_charpos, limit, charpos;
4187
4188 /* Get the value of the invisible text property at the
4189 current position. Value will be nil if there is no such
4190 property. */
4191 charpos = make_number (IT_STRING_CHARPOS (*it));
4192 prop = Fget_text_property (charpos, Qinvisible, it->string);
4193 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4194
4195 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4196 {
4197 /* Record whether we have to display an ellipsis for the
4198 invisible text. */
4199 int display_ellipsis_p = (invis_p == 2);
4200 ptrdiff_t len, endpos;
4201
4202 handled = HANDLED_RECOMPUTE_PROPS;
4203
4204 /* Get the position at which the next visible text can be
4205 found in IT->string, if any. */
4206 endpos = len = SCHARS (it->string);
4207 XSETINT (limit, len);
4208 do
4209 {
4210 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4211 it->string, limit);
4212 if (INTEGERP (end_charpos))
4213 {
4214 endpos = XFASTINT (end_charpos);
4215 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4216 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4217 if (invis_p == 2)
4218 display_ellipsis_p = true;
4219 }
4220 }
4221 while (invis_p && endpos < len);
4222
4223 if (display_ellipsis_p)
4224 it->ellipsis_p = true;
4225
4226 if (endpos < len)
4227 {
4228 /* Text at END_CHARPOS is visible. Move IT there. */
4229 struct text_pos old;
4230 ptrdiff_t oldpos;
4231
4232 old = it->current.string_pos;
4233 oldpos = CHARPOS (old);
4234 if (it->bidi_p)
4235 {
4236 if (it->bidi_it.first_elt
4237 && it->bidi_it.charpos < SCHARS (it->string))
4238 bidi_paragraph_init (it->paragraph_embedding,
4239 &it->bidi_it, 1);
4240 /* Bidi-iterate out of the invisible text. */
4241 do
4242 {
4243 bidi_move_to_visually_next (&it->bidi_it);
4244 }
4245 while (oldpos <= it->bidi_it.charpos
4246 && it->bidi_it.charpos < endpos);
4247
4248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4249 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4250 if (IT_CHARPOS (*it) >= endpos)
4251 it->prev_stop = endpos;
4252 }
4253 else
4254 {
4255 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4256 compute_string_pos (&it->current.string_pos, old, it->string);
4257 }
4258 }
4259 else
4260 {
4261 /* The rest of the string is invisible. If this is an
4262 overlay string, proceed with the next overlay string
4263 or whatever comes and return a character from there. */
4264 if (it->current.overlay_string_index >= 0
4265 && !display_ellipsis_p)
4266 {
4267 next_overlay_string (it);
4268 /* Don't check for overlay strings when we just
4269 finished processing them. */
4270 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4271 }
4272 else
4273 {
4274 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4275 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4276 }
4277 }
4278 }
4279 }
4280 else
4281 {
4282 ptrdiff_t newpos, next_stop, start_charpos, tem;
4283 Lisp_Object pos, overlay;
4284
4285 /* First of all, is there invisible text at this position? */
4286 tem = start_charpos = IT_CHARPOS (*it);
4287 pos = make_number (tem);
4288 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4289 &overlay);
4290 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4291
4292 /* If we are on invisible text, skip over it. */
4293 if (invis_p && start_charpos < it->end_charpos)
4294 {
4295 /* Record whether we have to display an ellipsis for the
4296 invisible text. */
4297 int display_ellipsis_p = invis_p == 2;
4298
4299 handled = HANDLED_RECOMPUTE_PROPS;
4300
4301 /* Loop skipping over invisible text. The loop is left at
4302 ZV or with IT on the first char being visible again. */
4303 do
4304 {
4305 /* Try to skip some invisible text. Return value is the
4306 position reached which can be equal to where we start
4307 if there is nothing invisible there. This skips both
4308 over invisible text properties and overlays with
4309 invisible property. */
4310 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4311
4312 /* If we skipped nothing at all we weren't at invisible
4313 text in the first place. If everything to the end of
4314 the buffer was skipped, end the loop. */
4315 if (newpos == tem || newpos >= ZV)
4316 invis_p = 0;
4317 else
4318 {
4319 /* We skipped some characters but not necessarily
4320 all there are. Check if we ended up on visible
4321 text. Fget_char_property returns the property of
4322 the char before the given position, i.e. if we
4323 get invis_p = 0, this means that the char at
4324 newpos is visible. */
4325 pos = make_number (newpos);
4326 prop = Fget_char_property (pos, Qinvisible, it->window);
4327 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4328 }
4329
4330 /* If we ended up on invisible text, proceed to
4331 skip starting with next_stop. */
4332 if (invis_p)
4333 tem = next_stop;
4334
4335 /* If there are adjacent invisible texts, don't lose the
4336 second one's ellipsis. */
4337 if (invis_p == 2)
4338 display_ellipsis_p = true;
4339 }
4340 while (invis_p);
4341
4342 /* The position newpos is now either ZV or on visible text. */
4343 if (it->bidi_p)
4344 {
4345 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4346 int on_newline
4347 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4348 int after_newline
4349 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4350
4351 /* If the invisible text ends on a newline or on a
4352 character after a newline, we can avoid the costly,
4353 character by character, bidi iteration to NEWPOS, and
4354 instead simply reseat the iterator there. That's
4355 because all bidi reordering information is tossed at
4356 the newline. This is a big win for modes that hide
4357 complete lines, like Outline, Org, etc. */
4358 if (on_newline || after_newline)
4359 {
4360 struct text_pos tpos;
4361 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4362
4363 SET_TEXT_POS (tpos, newpos, bpos);
4364 reseat_1 (it, tpos, 0);
4365 /* If we reseat on a newline/ZV, we need to prep the
4366 bidi iterator for advancing to the next character
4367 after the newline/EOB, keeping the current paragraph
4368 direction (so that PRODUCE_GLYPHS does TRT wrt
4369 prepending/appending glyphs to a glyph row). */
4370 if (on_newline)
4371 {
4372 it->bidi_it.first_elt = 0;
4373 it->bidi_it.paragraph_dir = pdir;
4374 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4375 it->bidi_it.nchars = 1;
4376 it->bidi_it.ch_len = 1;
4377 }
4378 }
4379 else /* Must use the slow method. */
4380 {
4381 /* With bidi iteration, the region of invisible text
4382 could start and/or end in the middle of a
4383 non-base embedding level. Therefore, we need to
4384 skip invisible text using the bidi iterator,
4385 starting at IT's current position, until we find
4386 ourselves outside of the invisible text.
4387 Skipping invisible text _after_ bidi iteration
4388 avoids affecting the visual order of the
4389 displayed text when invisible properties are
4390 added or removed. */
4391 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4392 {
4393 /* If we were `reseat'ed to a new paragraph,
4394 determine the paragraph base direction. We
4395 need to do it now because
4396 next_element_from_buffer may not have a
4397 chance to do it, if we are going to skip any
4398 text at the beginning, which resets the
4399 FIRST_ELT flag. */
4400 bidi_paragraph_init (it->paragraph_embedding,
4401 &it->bidi_it, 1);
4402 }
4403 do
4404 {
4405 bidi_move_to_visually_next (&it->bidi_it);
4406 }
4407 while (it->stop_charpos <= it->bidi_it.charpos
4408 && it->bidi_it.charpos < newpos);
4409 IT_CHARPOS (*it) = it->bidi_it.charpos;
4410 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4411 /* If we overstepped NEWPOS, record its position in
4412 the iterator, so that we skip invisible text if
4413 later the bidi iteration lands us in the
4414 invisible region again. */
4415 if (IT_CHARPOS (*it) >= newpos)
4416 it->prev_stop = newpos;
4417 }
4418 }
4419 else
4420 {
4421 IT_CHARPOS (*it) = newpos;
4422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4423 }
4424
4425 /* If there are before-strings at the start of invisible
4426 text, and the text is invisible because of a text
4427 property, arrange to show before-strings because 20.x did
4428 it that way. (If the text is invisible because of an
4429 overlay property instead of a text property, this is
4430 already handled in the overlay code.) */
4431 if (NILP (overlay)
4432 && get_overlay_strings (it, it->stop_charpos))
4433 {
4434 handled = HANDLED_RECOMPUTE_PROPS;
4435 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4436 }
4437 else if (display_ellipsis_p)
4438 {
4439 /* Make sure that the glyphs of the ellipsis will get
4440 correct `charpos' values. If we would not update
4441 it->position here, the glyphs would belong to the
4442 last visible character _before_ the invisible
4443 text, which confuses `set_cursor_from_row'.
4444
4445 We use the last invisible position instead of the
4446 first because this way the cursor is always drawn on
4447 the first "." of the ellipsis, whenever PT is inside
4448 the invisible text. Otherwise the cursor would be
4449 placed _after_ the ellipsis when the point is after the
4450 first invisible character. */
4451 if (!STRINGP (it->object))
4452 {
4453 it->position.charpos = newpos - 1;
4454 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4455 }
4456 it->ellipsis_p = true;
4457 /* Let the ellipsis display before
4458 considering any properties of the following char.
4459 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4460 handled = HANDLED_RETURN;
4461 }
4462 }
4463 }
4464
4465 return handled;
4466 }
4467
4468
4469 /* Make iterator IT return `...' next.
4470 Replaces LEN characters from buffer. */
4471
4472 static void
4473 setup_for_ellipsis (struct it *it, int len)
4474 {
4475 /* Use the display table definition for `...'. Invalid glyphs
4476 will be handled by the method returning elements from dpvec. */
4477 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4478 {
4479 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4480 it->dpvec = v->contents;
4481 it->dpend = v->contents + v->header.size;
4482 }
4483 else
4484 {
4485 /* Default `...'. */
4486 it->dpvec = default_invis_vector;
4487 it->dpend = default_invis_vector + 3;
4488 }
4489
4490 it->dpvec_char_len = len;
4491 it->current.dpvec_index = 0;
4492 it->dpvec_face_id = -1;
4493
4494 /* Remember the current face id in case glyphs specify faces.
4495 IT's face is restored in set_iterator_to_next.
4496 saved_face_id was set to preceding char's face in handle_stop. */
4497 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4498 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4499
4500 it->method = GET_FROM_DISPLAY_VECTOR;
4501 it->ellipsis_p = true;
4502 }
4503
4504
4505 \f
4506 /***********************************************************************
4507 'display' property
4508 ***********************************************************************/
4509
4510 /* Set up iterator IT from `display' property at its current position.
4511 Called from handle_stop.
4512 We return HANDLED_RETURN if some part of the display property
4513 overrides the display of the buffer text itself.
4514 Otherwise we return HANDLED_NORMALLY. */
4515
4516 static enum prop_handled
4517 handle_display_prop (struct it *it)
4518 {
4519 Lisp_Object propval, object, overlay;
4520 struct text_pos *position;
4521 ptrdiff_t bufpos;
4522 /* Nonzero if some property replaces the display of the text itself. */
4523 int display_replaced_p = 0;
4524
4525 if (STRINGP (it->string))
4526 {
4527 object = it->string;
4528 position = &it->current.string_pos;
4529 bufpos = CHARPOS (it->current.pos);
4530 }
4531 else
4532 {
4533 XSETWINDOW (object, it->w);
4534 position = &it->current.pos;
4535 bufpos = CHARPOS (*position);
4536 }
4537
4538 /* Reset those iterator values set from display property values. */
4539 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4540 it->space_width = Qnil;
4541 it->font_height = Qnil;
4542 it->voffset = 0;
4543
4544 /* We don't support recursive `display' properties, i.e. string
4545 values that have a string `display' property, that have a string
4546 `display' property etc. */
4547 if (!it->string_from_display_prop_p)
4548 it->area = TEXT_AREA;
4549
4550 propval = get_char_property_and_overlay (make_number (position->charpos),
4551 Qdisplay, object, &overlay);
4552 if (NILP (propval))
4553 return HANDLED_NORMALLY;
4554 /* Now OVERLAY is the overlay that gave us this property, or nil
4555 if it was a text property. */
4556
4557 if (!STRINGP (it->string))
4558 object = it->w->contents;
4559
4560 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4561 position, bufpos,
4562 FRAME_WINDOW_P (it->f));
4563
4564 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4565 }
4566
4567 /* Subroutine of handle_display_prop. Returns non-zero if the display
4568 specification in SPEC is a replacing specification, i.e. it would
4569 replace the text covered by `display' property with something else,
4570 such as an image or a display string. If SPEC includes any kind or
4571 `(space ...) specification, the value is 2; this is used by
4572 compute_display_string_pos, which see.
4573
4574 See handle_single_display_spec for documentation of arguments.
4575 frame_window_p is non-zero if the window being redisplayed is on a
4576 GUI frame; this argument is used only if IT is NULL, see below.
4577
4578 IT can be NULL, if this is called by the bidi reordering code
4579 through compute_display_string_pos, which see. In that case, this
4580 function only examines SPEC, but does not otherwise "handle" it, in
4581 the sense that it doesn't set up members of IT from the display
4582 spec. */
4583 static int
4584 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4585 Lisp_Object overlay, struct text_pos *position,
4586 ptrdiff_t bufpos, int frame_window_p)
4587 {
4588 int replacing_p = 0;
4589 int rv;
4590
4591 if (CONSP (spec)
4592 /* Simple specifications. */
4593 && !EQ (XCAR (spec), Qimage)
4594 && !EQ (XCAR (spec), Qspace)
4595 && !EQ (XCAR (spec), Qwhen)
4596 && !EQ (XCAR (spec), Qslice)
4597 && !EQ (XCAR (spec), Qspace_width)
4598 && !EQ (XCAR (spec), Qheight)
4599 && !EQ (XCAR (spec), Qraise)
4600 /* Marginal area specifications. */
4601 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4602 && !EQ (XCAR (spec), Qleft_fringe)
4603 && !EQ (XCAR (spec), Qright_fringe)
4604 && !NILP (XCAR (spec)))
4605 {
4606 for (; CONSP (spec); spec = XCDR (spec))
4607 {
4608 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4609 overlay, position, bufpos,
4610 replacing_p, frame_window_p)))
4611 {
4612 replacing_p = rv;
4613 /* If some text in a string is replaced, `position' no
4614 longer points to the position of `object'. */
4615 if (!it || STRINGP (object))
4616 break;
4617 }
4618 }
4619 }
4620 else if (VECTORP (spec))
4621 {
4622 ptrdiff_t i;
4623 for (i = 0; i < ASIZE (spec); ++i)
4624 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4625 overlay, position, bufpos,
4626 replacing_p, frame_window_p)))
4627 {
4628 replacing_p = rv;
4629 /* If some text in a string is replaced, `position' no
4630 longer points to the position of `object'. */
4631 if (!it || STRINGP (object))
4632 break;
4633 }
4634 }
4635 else
4636 {
4637 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4638 position, bufpos, 0,
4639 frame_window_p)))
4640 replacing_p = rv;
4641 }
4642
4643 return replacing_p;
4644 }
4645
4646 /* Value is the position of the end of the `display' property starting
4647 at START_POS in OBJECT. */
4648
4649 static struct text_pos
4650 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4651 {
4652 Lisp_Object end;
4653 struct text_pos end_pos;
4654
4655 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4656 Qdisplay, object, Qnil);
4657 CHARPOS (end_pos) = XFASTINT (end);
4658 if (STRINGP (object))
4659 compute_string_pos (&end_pos, start_pos, it->string);
4660 else
4661 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4662
4663 return end_pos;
4664 }
4665
4666
4667 /* Set up IT from a single `display' property specification SPEC. OBJECT
4668 is the object in which the `display' property was found. *POSITION
4669 is the position in OBJECT at which the `display' property was found.
4670 BUFPOS is the buffer position of OBJECT (different from POSITION if
4671 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4672 previously saw a display specification which already replaced text
4673 display with something else, for example an image; we ignore such
4674 properties after the first one has been processed.
4675
4676 OVERLAY is the overlay this `display' property came from,
4677 or nil if it was a text property.
4678
4679 If SPEC is a `space' or `image' specification, and in some other
4680 cases too, set *POSITION to the position where the `display'
4681 property ends.
4682
4683 If IT is NULL, only examine the property specification in SPEC, but
4684 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4685 is intended to be displayed in a window on a GUI frame.
4686
4687 Value is non-zero if something was found which replaces the display
4688 of buffer or string text. */
4689
4690 static int
4691 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4692 Lisp_Object overlay, struct text_pos *position,
4693 ptrdiff_t bufpos, int display_replaced_p,
4694 int frame_window_p)
4695 {
4696 Lisp_Object form;
4697 Lisp_Object location, value;
4698 struct text_pos start_pos = *position;
4699 int valid_p;
4700
4701 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4702 If the result is non-nil, use VALUE instead of SPEC. */
4703 form = Qt;
4704 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4705 {
4706 spec = XCDR (spec);
4707 if (!CONSP (spec))
4708 return 0;
4709 form = XCAR (spec);
4710 spec = XCDR (spec);
4711 }
4712
4713 if (!NILP (form) && !EQ (form, Qt))
4714 {
4715 ptrdiff_t count = SPECPDL_INDEX ();
4716 struct gcpro gcpro1;
4717
4718 /* Bind `object' to the object having the `display' property, a
4719 buffer or string. Bind `position' to the position in the
4720 object where the property was found, and `buffer-position'
4721 to the current position in the buffer. */
4722
4723 if (NILP (object))
4724 XSETBUFFER (object, current_buffer);
4725 specbind (Qobject, object);
4726 specbind (Qposition, make_number (CHARPOS (*position)));
4727 specbind (Qbuffer_position, make_number (bufpos));
4728 GCPRO1 (form);
4729 form = safe_eval (form);
4730 UNGCPRO;
4731 unbind_to (count, Qnil);
4732 }
4733
4734 if (NILP (form))
4735 return 0;
4736
4737 /* Handle `(height HEIGHT)' specifications. */
4738 if (CONSP (spec)
4739 && EQ (XCAR (spec), Qheight)
4740 && CONSP (XCDR (spec)))
4741 {
4742 if (it)
4743 {
4744 if (!FRAME_WINDOW_P (it->f))
4745 return 0;
4746
4747 it->font_height = XCAR (XCDR (spec));
4748 if (!NILP (it->font_height))
4749 {
4750 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4751 int new_height = -1;
4752
4753 if (CONSP (it->font_height)
4754 && (EQ (XCAR (it->font_height), Qplus)
4755 || EQ (XCAR (it->font_height), Qminus))
4756 && CONSP (XCDR (it->font_height))
4757 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4758 {
4759 /* `(+ N)' or `(- N)' where N is an integer. */
4760 int steps = XINT (XCAR (XCDR (it->font_height)));
4761 if (EQ (XCAR (it->font_height), Qplus))
4762 steps = - steps;
4763 it->face_id = smaller_face (it->f, it->face_id, steps);
4764 }
4765 else if (FUNCTIONP (it->font_height))
4766 {
4767 /* Call function with current height as argument.
4768 Value is the new height. */
4769 Lisp_Object height;
4770 height = safe_call1 (it->font_height,
4771 face->lface[LFACE_HEIGHT_INDEX]);
4772 if (NUMBERP (height))
4773 new_height = XFLOATINT (height);
4774 }
4775 else if (NUMBERP (it->font_height))
4776 {
4777 /* Value is a multiple of the canonical char height. */
4778 struct face *f;
4779
4780 f = FACE_FROM_ID (it->f,
4781 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4782 new_height = (XFLOATINT (it->font_height)
4783 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4784 }
4785 else
4786 {
4787 /* Evaluate IT->font_height with `height' bound to the
4788 current specified height to get the new height. */
4789 ptrdiff_t count = SPECPDL_INDEX ();
4790
4791 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4792 value = safe_eval (it->font_height);
4793 unbind_to (count, Qnil);
4794
4795 if (NUMBERP (value))
4796 new_height = XFLOATINT (value);
4797 }
4798
4799 if (new_height > 0)
4800 it->face_id = face_with_height (it->f, it->face_id, new_height);
4801 }
4802 }
4803
4804 return 0;
4805 }
4806
4807 /* Handle `(space-width WIDTH)'. */
4808 if (CONSP (spec)
4809 && EQ (XCAR (spec), Qspace_width)
4810 && CONSP (XCDR (spec)))
4811 {
4812 if (it)
4813 {
4814 if (!FRAME_WINDOW_P (it->f))
4815 return 0;
4816
4817 value = XCAR (XCDR (spec));
4818 if (NUMBERP (value) && XFLOATINT (value) > 0)
4819 it->space_width = value;
4820 }
4821
4822 return 0;
4823 }
4824
4825 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4826 if (CONSP (spec)
4827 && EQ (XCAR (spec), Qslice))
4828 {
4829 Lisp_Object tem;
4830
4831 if (it)
4832 {
4833 if (!FRAME_WINDOW_P (it->f))
4834 return 0;
4835
4836 if (tem = XCDR (spec), CONSP (tem))
4837 {
4838 it->slice.x = XCAR (tem);
4839 if (tem = XCDR (tem), CONSP (tem))
4840 {
4841 it->slice.y = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 {
4844 it->slice.width = XCAR (tem);
4845 if (tem = XCDR (tem), CONSP (tem))
4846 it->slice.height = XCAR (tem);
4847 }
4848 }
4849 }
4850 }
4851
4852 return 0;
4853 }
4854
4855 /* Handle `(raise FACTOR)'. */
4856 if (CONSP (spec)
4857 && EQ (XCAR (spec), Qraise)
4858 && CONSP (XCDR (spec)))
4859 {
4860 if (it)
4861 {
4862 if (!FRAME_WINDOW_P (it->f))
4863 return 0;
4864
4865 #ifdef HAVE_WINDOW_SYSTEM
4866 value = XCAR (XCDR (spec));
4867 if (NUMBERP (value))
4868 {
4869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4870 it->voffset = - (XFLOATINT (value)
4871 * (FONT_HEIGHT (face->font)));
4872 }
4873 #endif /* HAVE_WINDOW_SYSTEM */
4874 }
4875
4876 return 0;
4877 }
4878
4879 /* Don't handle the other kinds of display specifications
4880 inside a string that we got from a `display' property. */
4881 if (it && it->string_from_display_prop_p)
4882 return 0;
4883
4884 /* Characters having this form of property are not displayed, so
4885 we have to find the end of the property. */
4886 if (it)
4887 {
4888 start_pos = *position;
4889 *position = display_prop_end (it, object, start_pos);
4890 }
4891 value = Qnil;
4892
4893 /* Stop the scan at that end position--we assume that all
4894 text properties change there. */
4895 if (it)
4896 it->stop_charpos = position->charpos;
4897
4898 /* Handle `(left-fringe BITMAP [FACE])'
4899 and `(right-fringe BITMAP [FACE])'. */
4900 if (CONSP (spec)
4901 && (EQ (XCAR (spec), Qleft_fringe)
4902 || EQ (XCAR (spec), Qright_fringe))
4903 && CONSP (XCDR (spec)))
4904 {
4905 int fringe_bitmap;
4906
4907 if (it)
4908 {
4909 if (!FRAME_WINDOW_P (it->f))
4910 /* If we return here, POSITION has been advanced
4911 across the text with this property. */
4912 {
4913 /* Synchronize the bidi iterator with POSITION. This is
4914 needed because we are not going to push the iterator
4915 on behalf of this display property, so there will be
4916 no pop_it call to do this synchronization for us. */
4917 if (it->bidi_p)
4918 {
4919 it->position = *position;
4920 iterate_out_of_display_property (it);
4921 *position = it->position;
4922 }
4923 return 1;
4924 }
4925 }
4926 else if (!frame_window_p)
4927 return 1;
4928
4929 #ifdef HAVE_WINDOW_SYSTEM
4930 value = XCAR (XCDR (spec));
4931 if (!SYMBOLP (value)
4932 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4933 /* If we return here, POSITION has been advanced
4934 across the text with this property. */
4935 {
4936 if (it && it->bidi_p)
4937 {
4938 it->position = *position;
4939 iterate_out_of_display_property (it);
4940 *position = it->position;
4941 }
4942 return 1;
4943 }
4944
4945 if (it)
4946 {
4947 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4948
4949 if (CONSP (XCDR (XCDR (spec))))
4950 {
4951 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4952 int face_id2 = lookup_derived_face (it->f, face_name,
4953 FRINGE_FACE_ID, 0);
4954 if (face_id2 >= 0)
4955 face_id = face_id2;
4956 }
4957
4958 /* Save current settings of IT so that we can restore them
4959 when we are finished with the glyph property value. */
4960 push_it (it, position);
4961
4962 it->area = TEXT_AREA;
4963 it->what = IT_IMAGE;
4964 it->image_id = -1; /* no image */
4965 it->position = start_pos;
4966 it->object = NILP (object) ? it->w->contents : object;
4967 it->method = GET_FROM_IMAGE;
4968 it->from_overlay = Qnil;
4969 it->face_id = face_id;
4970 it->from_disp_prop_p = true;
4971
4972 /* Say that we haven't consumed the characters with
4973 `display' property yet. The call to pop_it in
4974 set_iterator_to_next will clean this up. */
4975 *position = start_pos;
4976
4977 if (EQ (XCAR (spec), Qleft_fringe))
4978 {
4979 it->left_user_fringe_bitmap = fringe_bitmap;
4980 it->left_user_fringe_face_id = face_id;
4981 }
4982 else
4983 {
4984 it->right_user_fringe_bitmap = fringe_bitmap;
4985 it->right_user_fringe_face_id = face_id;
4986 }
4987 }
4988 #endif /* HAVE_WINDOW_SYSTEM */
4989 return 1;
4990 }
4991
4992 /* Prepare to handle `((margin left-margin) ...)',
4993 `((margin right-margin) ...)' and `((margin nil) ...)'
4994 prefixes for display specifications. */
4995 location = Qunbound;
4996 if (CONSP (spec) && CONSP (XCAR (spec)))
4997 {
4998 Lisp_Object tem;
4999
5000 value = XCDR (spec);
5001 if (CONSP (value))
5002 value = XCAR (value);
5003
5004 tem = XCAR (spec);
5005 if (EQ (XCAR (tem), Qmargin)
5006 && (tem = XCDR (tem),
5007 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5008 (NILP (tem)
5009 || EQ (tem, Qleft_margin)
5010 || EQ (tem, Qright_margin))))
5011 location = tem;
5012 }
5013
5014 if (EQ (location, Qunbound))
5015 {
5016 location = Qnil;
5017 value = spec;
5018 }
5019
5020 /* After this point, VALUE is the property after any
5021 margin prefix has been stripped. It must be a string,
5022 an image specification, or `(space ...)'.
5023
5024 LOCATION specifies where to display: `left-margin',
5025 `right-margin' or nil. */
5026
5027 valid_p = (STRINGP (value)
5028 #ifdef HAVE_WINDOW_SYSTEM
5029 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5030 && valid_image_p (value))
5031 #endif /* not HAVE_WINDOW_SYSTEM */
5032 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5033
5034 if (valid_p && !display_replaced_p)
5035 {
5036 int retval = 1;
5037
5038 if (!it)
5039 {
5040 /* Callers need to know whether the display spec is any kind
5041 of `(space ...)' spec that is about to affect text-area
5042 display. */
5043 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5044 retval = 2;
5045 return retval;
5046 }
5047
5048 /* Save current settings of IT so that we can restore them
5049 when we are finished with the glyph property value. */
5050 push_it (it, position);
5051 it->from_overlay = overlay;
5052 it->from_disp_prop_p = true;
5053
5054 if (NILP (location))
5055 it->area = TEXT_AREA;
5056 else if (EQ (location, Qleft_margin))
5057 it->area = LEFT_MARGIN_AREA;
5058 else
5059 it->area = RIGHT_MARGIN_AREA;
5060
5061 if (STRINGP (value))
5062 {
5063 it->string = value;
5064 it->multibyte_p = STRING_MULTIBYTE (it->string);
5065 it->current.overlay_string_index = -1;
5066 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5067 it->end_charpos = it->string_nchars = SCHARS (it->string);
5068 it->method = GET_FROM_STRING;
5069 it->stop_charpos = 0;
5070 it->prev_stop = 0;
5071 it->base_level_stop = 0;
5072 it->string_from_display_prop_p = true;
5073 /* Say that we haven't consumed the characters with
5074 `display' property yet. The call to pop_it in
5075 set_iterator_to_next will clean this up. */
5076 if (BUFFERP (object))
5077 *position = start_pos;
5078
5079 /* Force paragraph direction to be that of the parent
5080 object. If the parent object's paragraph direction is
5081 not yet determined, default to L2R. */
5082 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5083 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5084 else
5085 it->paragraph_embedding = L2R;
5086
5087 /* Set up the bidi iterator for this display string. */
5088 if (it->bidi_p)
5089 {
5090 it->bidi_it.string.lstring = it->string;
5091 it->bidi_it.string.s = NULL;
5092 it->bidi_it.string.schars = it->end_charpos;
5093 it->bidi_it.string.bufpos = bufpos;
5094 it->bidi_it.string.from_disp_str = 1;
5095 it->bidi_it.string.unibyte = !it->multibyte_p;
5096 it->bidi_it.w = it->w;
5097 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5098 }
5099 }
5100 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5101 {
5102 it->method = GET_FROM_STRETCH;
5103 it->object = value;
5104 *position = it->position = start_pos;
5105 retval = 1 + (it->area == TEXT_AREA);
5106 }
5107 #ifdef HAVE_WINDOW_SYSTEM
5108 else
5109 {
5110 it->what = IT_IMAGE;
5111 it->image_id = lookup_image (it->f, value);
5112 it->position = start_pos;
5113 it->object = NILP (object) ? it->w->contents : object;
5114 it->method = GET_FROM_IMAGE;
5115
5116 /* Say that we haven't consumed the characters with
5117 `display' property yet. The call to pop_it in
5118 set_iterator_to_next will clean this up. */
5119 *position = start_pos;
5120 }
5121 #endif /* HAVE_WINDOW_SYSTEM */
5122
5123 return retval;
5124 }
5125
5126 /* Invalid property or property not supported. Restore
5127 POSITION to what it was before. */
5128 *position = start_pos;
5129 return 0;
5130 }
5131
5132 /* Check if PROP is a display property value whose text should be
5133 treated as intangible. OVERLAY is the overlay from which PROP
5134 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5135 specify the buffer position covered by PROP. */
5136
5137 int
5138 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5139 ptrdiff_t charpos, ptrdiff_t bytepos)
5140 {
5141 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5142 struct text_pos position;
5143
5144 SET_TEXT_POS (position, charpos, bytepos);
5145 return handle_display_spec (NULL, prop, Qnil, overlay,
5146 &position, charpos, frame_window_p);
5147 }
5148
5149
5150 /* Return 1 if PROP is a display sub-property value containing STRING.
5151
5152 Implementation note: this and the following function are really
5153 special cases of handle_display_spec and
5154 handle_single_display_spec, and should ideally use the same code.
5155 Until they do, these two pairs must be consistent and must be
5156 modified in sync. */
5157
5158 static int
5159 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5160 {
5161 if (EQ (string, prop))
5162 return 1;
5163
5164 /* Skip over `when FORM'. */
5165 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5166 {
5167 prop = XCDR (prop);
5168 if (!CONSP (prop))
5169 return 0;
5170 /* Actually, the condition following `when' should be eval'ed,
5171 like handle_single_display_spec does, and we should return
5172 zero if it evaluates to nil. However, this function is
5173 called only when the buffer was already displayed and some
5174 glyph in the glyph matrix was found to come from a display
5175 string. Therefore, the condition was already evaluated, and
5176 the result was non-nil, otherwise the display string wouldn't
5177 have been displayed and we would have never been called for
5178 this property. Thus, we can skip the evaluation and assume
5179 its result is non-nil. */
5180 prop = XCDR (prop);
5181 }
5182
5183 if (CONSP (prop))
5184 /* Skip over `margin LOCATION'. */
5185 if (EQ (XCAR (prop), Qmargin))
5186 {
5187 prop = XCDR (prop);
5188 if (!CONSP (prop))
5189 return 0;
5190
5191 prop = XCDR (prop);
5192 if (!CONSP (prop))
5193 return 0;
5194 }
5195
5196 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5197 }
5198
5199
5200 /* Return 1 if STRING appears in the `display' property PROP. */
5201
5202 static int
5203 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5204 {
5205 if (CONSP (prop)
5206 && !EQ (XCAR (prop), Qwhen)
5207 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5208 {
5209 /* A list of sub-properties. */
5210 while (CONSP (prop))
5211 {
5212 if (single_display_spec_string_p (XCAR (prop), string))
5213 return 1;
5214 prop = XCDR (prop);
5215 }
5216 }
5217 else if (VECTORP (prop))
5218 {
5219 /* A vector of sub-properties. */
5220 ptrdiff_t i;
5221 for (i = 0; i < ASIZE (prop); ++i)
5222 if (single_display_spec_string_p (AREF (prop, i), string))
5223 return 1;
5224 }
5225 else
5226 return single_display_spec_string_p (prop, string);
5227
5228 return 0;
5229 }
5230
5231 /* Look for STRING in overlays and text properties in the current
5232 buffer, between character positions FROM and TO (excluding TO).
5233 BACK_P non-zero means look back (in this case, TO is supposed to be
5234 less than FROM).
5235 Value is the first character position where STRING was found, or
5236 zero if it wasn't found before hitting TO.
5237
5238 This function may only use code that doesn't eval because it is
5239 called asynchronously from note_mouse_highlight. */
5240
5241 static ptrdiff_t
5242 string_buffer_position_lim (Lisp_Object string,
5243 ptrdiff_t from, ptrdiff_t to, int back_p)
5244 {
5245 Lisp_Object limit, prop, pos;
5246 int found = 0;
5247
5248 pos = make_number (max (from, BEGV));
5249
5250 if (!back_p) /* looking forward */
5251 {
5252 limit = make_number (min (to, ZV));
5253 while (!found && !EQ (pos, limit))
5254 {
5255 prop = Fget_char_property (pos, Qdisplay, Qnil);
5256 if (!NILP (prop) && display_prop_string_p (prop, string))
5257 found = 1;
5258 else
5259 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5260 limit);
5261 }
5262 }
5263 else /* looking back */
5264 {
5265 limit = make_number (max (to, BEGV));
5266 while (!found && !EQ (pos, limit))
5267 {
5268 prop = Fget_char_property (pos, Qdisplay, Qnil);
5269 if (!NILP (prop) && display_prop_string_p (prop, string))
5270 found = 1;
5271 else
5272 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5273 limit);
5274 }
5275 }
5276
5277 return found ? XINT (pos) : 0;
5278 }
5279
5280 /* Determine which buffer position in current buffer STRING comes from.
5281 AROUND_CHARPOS is an approximate position where it could come from.
5282 Value is the buffer position or 0 if it couldn't be determined.
5283
5284 This function is necessary because we don't record buffer positions
5285 in glyphs generated from strings (to keep struct glyph small).
5286 This function may only use code that doesn't eval because it is
5287 called asynchronously from note_mouse_highlight. */
5288
5289 static ptrdiff_t
5290 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5291 {
5292 const int MAX_DISTANCE = 1000;
5293 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5294 around_charpos + MAX_DISTANCE,
5295 0);
5296
5297 if (!found)
5298 found = string_buffer_position_lim (string, around_charpos,
5299 around_charpos - MAX_DISTANCE, 1);
5300 return found;
5301 }
5302
5303
5304 \f
5305 /***********************************************************************
5306 `composition' property
5307 ***********************************************************************/
5308
5309 /* Set up iterator IT from `composition' property at its current
5310 position. Called from handle_stop. */
5311
5312 static enum prop_handled
5313 handle_composition_prop (struct it *it)
5314 {
5315 Lisp_Object prop, string;
5316 ptrdiff_t pos, pos_byte, start, end;
5317
5318 if (STRINGP (it->string))
5319 {
5320 unsigned char *s;
5321
5322 pos = IT_STRING_CHARPOS (*it);
5323 pos_byte = IT_STRING_BYTEPOS (*it);
5324 string = it->string;
5325 s = SDATA (string) + pos_byte;
5326 it->c = STRING_CHAR (s);
5327 }
5328 else
5329 {
5330 pos = IT_CHARPOS (*it);
5331 pos_byte = IT_BYTEPOS (*it);
5332 string = Qnil;
5333 it->c = FETCH_CHAR (pos_byte);
5334 }
5335
5336 /* If there's a valid composition and point is not inside of the
5337 composition (in the case that the composition is from the current
5338 buffer), draw a glyph composed from the composition components. */
5339 if (find_composition (pos, -1, &start, &end, &prop, string)
5340 && composition_valid_p (start, end, prop)
5341 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5342 {
5343 if (start < pos)
5344 /* As we can't handle this situation (perhaps font-lock added
5345 a new composition), we just return here hoping that next
5346 redisplay will detect this composition much earlier. */
5347 return HANDLED_NORMALLY;
5348 if (start != pos)
5349 {
5350 if (STRINGP (it->string))
5351 pos_byte = string_char_to_byte (it->string, start);
5352 else
5353 pos_byte = CHAR_TO_BYTE (start);
5354 }
5355 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5356 prop, string);
5357
5358 if (it->cmp_it.id >= 0)
5359 {
5360 it->cmp_it.ch = -1;
5361 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5362 it->cmp_it.nglyphs = -1;
5363 }
5364 }
5365
5366 return HANDLED_NORMALLY;
5367 }
5368
5369
5370 \f
5371 /***********************************************************************
5372 Overlay strings
5373 ***********************************************************************/
5374
5375 /* The following structure is used to record overlay strings for
5376 later sorting in load_overlay_strings. */
5377
5378 struct overlay_entry
5379 {
5380 Lisp_Object overlay;
5381 Lisp_Object string;
5382 EMACS_INT priority;
5383 int after_string_p;
5384 };
5385
5386
5387 /* Set up iterator IT from overlay strings at its current position.
5388 Called from handle_stop. */
5389
5390 static enum prop_handled
5391 handle_overlay_change (struct it *it)
5392 {
5393 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5394 return HANDLED_RECOMPUTE_PROPS;
5395 else
5396 return HANDLED_NORMALLY;
5397 }
5398
5399
5400 /* Set up the next overlay string for delivery by IT, if there is an
5401 overlay string to deliver. Called by set_iterator_to_next when the
5402 end of the current overlay string is reached. If there are more
5403 overlay strings to display, IT->string and
5404 IT->current.overlay_string_index are set appropriately here.
5405 Otherwise IT->string is set to nil. */
5406
5407 static void
5408 next_overlay_string (struct it *it)
5409 {
5410 ++it->current.overlay_string_index;
5411 if (it->current.overlay_string_index == it->n_overlay_strings)
5412 {
5413 /* No more overlay strings. Restore IT's settings to what
5414 they were before overlay strings were processed, and
5415 continue to deliver from current_buffer. */
5416
5417 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5418 pop_it (it);
5419 eassert (it->sp > 0
5420 || (NILP (it->string)
5421 && it->method == GET_FROM_BUFFER
5422 && it->stop_charpos >= BEGV
5423 && it->stop_charpos <= it->end_charpos));
5424 it->current.overlay_string_index = -1;
5425 it->n_overlay_strings = 0;
5426 it->overlay_strings_charpos = -1;
5427 /* If there's an empty display string on the stack, pop the
5428 stack, to resync the bidi iterator with IT's position. Such
5429 empty strings are pushed onto the stack in
5430 get_overlay_strings_1. */
5431 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5432 pop_it (it);
5433
5434 /* If we're at the end of the buffer, record that we have
5435 processed the overlay strings there already, so that
5436 next_element_from_buffer doesn't try it again. */
5437 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5438 it->overlay_strings_at_end_processed_p = true;
5439 }
5440 else
5441 {
5442 /* There are more overlay strings to process. If
5443 IT->current.overlay_string_index has advanced to a position
5444 where we must load IT->overlay_strings with more strings, do
5445 it. We must load at the IT->overlay_strings_charpos where
5446 IT->n_overlay_strings was originally computed; when invisible
5447 text is present, this might not be IT_CHARPOS (Bug#7016). */
5448 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5449
5450 if (it->current.overlay_string_index && i == 0)
5451 load_overlay_strings (it, it->overlay_strings_charpos);
5452
5453 /* Initialize IT to deliver display elements from the overlay
5454 string. */
5455 it->string = it->overlay_strings[i];
5456 it->multibyte_p = STRING_MULTIBYTE (it->string);
5457 SET_TEXT_POS (it->current.string_pos, 0, 0);
5458 it->method = GET_FROM_STRING;
5459 it->stop_charpos = 0;
5460 it->end_charpos = SCHARS (it->string);
5461 if (it->cmp_it.stop_pos >= 0)
5462 it->cmp_it.stop_pos = 0;
5463 it->prev_stop = 0;
5464 it->base_level_stop = 0;
5465
5466 /* Set up the bidi iterator for this overlay string. */
5467 if (it->bidi_p)
5468 {
5469 it->bidi_it.string.lstring = it->string;
5470 it->bidi_it.string.s = NULL;
5471 it->bidi_it.string.schars = SCHARS (it->string);
5472 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5473 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5474 it->bidi_it.string.unibyte = !it->multibyte_p;
5475 it->bidi_it.w = it->w;
5476 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5477 }
5478 }
5479
5480 CHECK_IT (it);
5481 }
5482
5483
5484 /* Compare two overlay_entry structures E1 and E2. Used as a
5485 comparison function for qsort in load_overlay_strings. Overlay
5486 strings for the same position are sorted so that
5487
5488 1. All after-strings come in front of before-strings, except
5489 when they come from the same overlay.
5490
5491 2. Within after-strings, strings are sorted so that overlay strings
5492 from overlays with higher priorities come first.
5493
5494 2. Within before-strings, strings are sorted so that overlay
5495 strings from overlays with higher priorities come last.
5496
5497 Value is analogous to strcmp. */
5498
5499
5500 static int
5501 compare_overlay_entries (const void *e1, const void *e2)
5502 {
5503 struct overlay_entry const *entry1 = e1;
5504 struct overlay_entry const *entry2 = e2;
5505 int result;
5506
5507 if (entry1->after_string_p != entry2->after_string_p)
5508 {
5509 /* Let after-strings appear in front of before-strings if
5510 they come from different overlays. */
5511 if (EQ (entry1->overlay, entry2->overlay))
5512 result = entry1->after_string_p ? 1 : -1;
5513 else
5514 result = entry1->after_string_p ? -1 : 1;
5515 }
5516 else if (entry1->priority != entry2->priority)
5517 {
5518 if (entry1->after_string_p)
5519 /* After-strings sorted in order of decreasing priority. */
5520 result = entry2->priority < entry1->priority ? -1 : 1;
5521 else
5522 /* Before-strings sorted in order of increasing priority. */
5523 result = entry1->priority < entry2->priority ? -1 : 1;
5524 }
5525 else
5526 result = 0;
5527
5528 return result;
5529 }
5530
5531
5532 /* Load the vector IT->overlay_strings with overlay strings from IT's
5533 current buffer position, or from CHARPOS if that is > 0. Set
5534 IT->n_overlays to the total number of overlay strings found.
5535
5536 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5537 a time. On entry into load_overlay_strings,
5538 IT->current.overlay_string_index gives the number of overlay
5539 strings that have already been loaded by previous calls to this
5540 function.
5541
5542 IT->add_overlay_start contains an additional overlay start
5543 position to consider for taking overlay strings from, if non-zero.
5544 This position comes into play when the overlay has an `invisible'
5545 property, and both before and after-strings. When we've skipped to
5546 the end of the overlay, because of its `invisible' property, we
5547 nevertheless want its before-string to appear.
5548 IT->add_overlay_start will contain the overlay start position
5549 in this case.
5550
5551 Overlay strings are sorted so that after-string strings come in
5552 front of before-string strings. Within before and after-strings,
5553 strings are sorted by overlay priority. See also function
5554 compare_overlay_entries. */
5555
5556 static void
5557 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5558 {
5559 Lisp_Object overlay, window, str, invisible;
5560 struct Lisp_Overlay *ov;
5561 ptrdiff_t start, end;
5562 ptrdiff_t size = 20;
5563 ptrdiff_t n = 0, i, j;
5564 int invis_p;
5565 struct overlay_entry *entries = alloca (size * sizeof *entries);
5566 USE_SAFE_ALLOCA;
5567
5568 if (charpos <= 0)
5569 charpos = IT_CHARPOS (*it);
5570
5571 /* Append the overlay string STRING of overlay OVERLAY to vector
5572 `entries' which has size `size' and currently contains `n'
5573 elements. AFTER_P non-zero means STRING is an after-string of
5574 OVERLAY. */
5575 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5576 do \
5577 { \
5578 Lisp_Object priority; \
5579 \
5580 if (n == size) \
5581 { \
5582 struct overlay_entry *old = entries; \
5583 SAFE_NALLOCA (entries, 2, size); \
5584 memcpy (entries, old, size * sizeof *entries); \
5585 size *= 2; \
5586 } \
5587 \
5588 entries[n].string = (STRING); \
5589 entries[n].overlay = (OVERLAY); \
5590 priority = Foverlay_get ((OVERLAY), Qpriority); \
5591 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5592 entries[n].after_string_p = (AFTER_P); \
5593 ++n; \
5594 } \
5595 while (0)
5596
5597 /* Process overlay before the overlay center. */
5598 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5599 {
5600 XSETMISC (overlay, ov);
5601 eassert (OVERLAYP (overlay));
5602 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5603 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5604
5605 if (end < charpos)
5606 break;
5607
5608 /* Skip this overlay if it doesn't start or end at IT's current
5609 position. */
5610 if (end != charpos && start != charpos)
5611 continue;
5612
5613 /* Skip this overlay if it doesn't apply to IT->w. */
5614 window = Foverlay_get (overlay, Qwindow);
5615 if (WINDOWP (window) && XWINDOW (window) != it->w)
5616 continue;
5617
5618 /* If the text ``under'' the overlay is invisible, both before-
5619 and after-strings from this overlay are visible; start and
5620 end position are indistinguishable. */
5621 invisible = Foverlay_get (overlay, Qinvisible);
5622 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5623
5624 /* If overlay has a non-empty before-string, record it. */
5625 if ((start == charpos || (end == charpos && invis_p))
5626 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5627 && SCHARS (str))
5628 RECORD_OVERLAY_STRING (overlay, str, 0);
5629
5630 /* If overlay has a non-empty after-string, record it. */
5631 if ((end == charpos || (start == charpos && invis_p))
5632 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5633 && SCHARS (str))
5634 RECORD_OVERLAY_STRING (overlay, str, 1);
5635 }
5636
5637 /* Process overlays after the overlay center. */
5638 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5639 {
5640 XSETMISC (overlay, ov);
5641 eassert (OVERLAYP (overlay));
5642 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5643 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5644
5645 if (start > charpos)
5646 break;
5647
5648 /* Skip this overlay if it doesn't start or end at IT's current
5649 position. */
5650 if (end != charpos && start != charpos)
5651 continue;
5652
5653 /* Skip this overlay if it doesn't apply to IT->w. */
5654 window = Foverlay_get (overlay, Qwindow);
5655 if (WINDOWP (window) && XWINDOW (window) != it->w)
5656 continue;
5657
5658 /* If the text ``under'' the overlay is invisible, it has a zero
5659 dimension, and both before- and after-strings apply. */
5660 invisible = Foverlay_get (overlay, Qinvisible);
5661 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5662
5663 /* If overlay has a non-empty before-string, record it. */
5664 if ((start == charpos || (end == charpos && invis_p))
5665 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5666 && SCHARS (str))
5667 RECORD_OVERLAY_STRING (overlay, str, 0);
5668
5669 /* If overlay has a non-empty after-string, record it. */
5670 if ((end == charpos || (start == charpos && invis_p))
5671 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5672 && SCHARS (str))
5673 RECORD_OVERLAY_STRING (overlay, str, 1);
5674 }
5675
5676 #undef RECORD_OVERLAY_STRING
5677
5678 /* Sort entries. */
5679 if (n > 1)
5680 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5681
5682 /* Record number of overlay strings, and where we computed it. */
5683 it->n_overlay_strings = n;
5684 it->overlay_strings_charpos = charpos;
5685
5686 /* IT->current.overlay_string_index is the number of overlay strings
5687 that have already been consumed by IT. Copy some of the
5688 remaining overlay strings to IT->overlay_strings. */
5689 i = 0;
5690 j = it->current.overlay_string_index;
5691 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5692 {
5693 it->overlay_strings[i] = entries[j].string;
5694 it->string_overlays[i++] = entries[j++].overlay;
5695 }
5696
5697 CHECK_IT (it);
5698 SAFE_FREE ();
5699 }
5700
5701
5702 /* Get the first chunk of overlay strings at IT's current buffer
5703 position, or at CHARPOS if that is > 0. Value is non-zero if at
5704 least one overlay string was found. */
5705
5706 static int
5707 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5708 {
5709 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5710 process. This fills IT->overlay_strings with strings, and sets
5711 IT->n_overlay_strings to the total number of strings to process.
5712 IT->pos.overlay_string_index has to be set temporarily to zero
5713 because load_overlay_strings needs this; it must be set to -1
5714 when no overlay strings are found because a zero value would
5715 indicate a position in the first overlay string. */
5716 it->current.overlay_string_index = 0;
5717 load_overlay_strings (it, charpos);
5718
5719 /* If we found overlay strings, set up IT to deliver display
5720 elements from the first one. Otherwise set up IT to deliver
5721 from current_buffer. */
5722 if (it->n_overlay_strings)
5723 {
5724 /* Make sure we know settings in current_buffer, so that we can
5725 restore meaningful values when we're done with the overlay
5726 strings. */
5727 if (compute_stop_p)
5728 compute_stop_pos (it);
5729 eassert (it->face_id >= 0);
5730
5731 /* Save IT's settings. They are restored after all overlay
5732 strings have been processed. */
5733 eassert (!compute_stop_p || it->sp == 0);
5734
5735 /* When called from handle_stop, there might be an empty display
5736 string loaded. In that case, don't bother saving it. But
5737 don't use this optimization with the bidi iterator, since we
5738 need the corresponding pop_it call to resync the bidi
5739 iterator's position with IT's position, after we are done
5740 with the overlay strings. (The corresponding call to pop_it
5741 in case of an empty display string is in
5742 next_overlay_string.) */
5743 if (!(!it->bidi_p
5744 && STRINGP (it->string) && !SCHARS (it->string)))
5745 push_it (it, NULL);
5746
5747 /* Set up IT to deliver display elements from the first overlay
5748 string. */
5749 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5750 it->string = it->overlay_strings[0];
5751 it->from_overlay = Qnil;
5752 it->stop_charpos = 0;
5753 eassert (STRINGP (it->string));
5754 it->end_charpos = SCHARS (it->string);
5755 it->prev_stop = 0;
5756 it->base_level_stop = 0;
5757 it->multibyte_p = STRING_MULTIBYTE (it->string);
5758 it->method = GET_FROM_STRING;
5759 it->from_disp_prop_p = 0;
5760
5761 /* Force paragraph direction to be that of the parent
5762 buffer. */
5763 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5764 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5765 else
5766 it->paragraph_embedding = L2R;
5767
5768 /* Set up the bidi iterator for this overlay string. */
5769 if (it->bidi_p)
5770 {
5771 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5772
5773 it->bidi_it.string.lstring = it->string;
5774 it->bidi_it.string.s = NULL;
5775 it->bidi_it.string.schars = SCHARS (it->string);
5776 it->bidi_it.string.bufpos = pos;
5777 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5778 it->bidi_it.string.unibyte = !it->multibyte_p;
5779 it->bidi_it.w = it->w;
5780 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5781 }
5782 return 1;
5783 }
5784
5785 it->current.overlay_string_index = -1;
5786 return 0;
5787 }
5788
5789 static int
5790 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5791 {
5792 it->string = Qnil;
5793 it->method = GET_FROM_BUFFER;
5794
5795 (void) get_overlay_strings_1 (it, charpos, 1);
5796
5797 CHECK_IT (it);
5798
5799 /* Value is non-zero if we found at least one overlay string. */
5800 return STRINGP (it->string);
5801 }
5802
5803
5804 \f
5805 /***********************************************************************
5806 Saving and restoring state
5807 ***********************************************************************/
5808
5809 /* Save current settings of IT on IT->stack. Called, for example,
5810 before setting up IT for an overlay string, to be able to restore
5811 IT's settings to what they were after the overlay string has been
5812 processed. If POSITION is non-NULL, it is the position to save on
5813 the stack instead of IT->position. */
5814
5815 static void
5816 push_it (struct it *it, struct text_pos *position)
5817 {
5818 struct iterator_stack_entry *p;
5819
5820 eassert (it->sp < IT_STACK_SIZE);
5821 p = it->stack + it->sp;
5822
5823 p->stop_charpos = it->stop_charpos;
5824 p->prev_stop = it->prev_stop;
5825 p->base_level_stop = it->base_level_stop;
5826 p->cmp_it = it->cmp_it;
5827 eassert (it->face_id >= 0);
5828 p->face_id = it->face_id;
5829 p->string = it->string;
5830 p->method = it->method;
5831 p->from_overlay = it->from_overlay;
5832 switch (p->method)
5833 {
5834 case GET_FROM_IMAGE:
5835 p->u.image.object = it->object;
5836 p->u.image.image_id = it->image_id;
5837 p->u.image.slice = it->slice;
5838 break;
5839 case GET_FROM_STRETCH:
5840 p->u.stretch.object = it->object;
5841 break;
5842 }
5843 p->position = position ? *position : it->position;
5844 p->current = it->current;
5845 p->end_charpos = it->end_charpos;
5846 p->string_nchars = it->string_nchars;
5847 p->area = it->area;
5848 p->multibyte_p = it->multibyte_p;
5849 p->avoid_cursor_p = it->avoid_cursor_p;
5850 p->space_width = it->space_width;
5851 p->font_height = it->font_height;
5852 p->voffset = it->voffset;
5853 p->string_from_display_prop_p = it->string_from_display_prop_p;
5854 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5855 p->display_ellipsis_p = 0;
5856 p->line_wrap = it->line_wrap;
5857 p->bidi_p = it->bidi_p;
5858 p->paragraph_embedding = it->paragraph_embedding;
5859 p->from_disp_prop_p = it->from_disp_prop_p;
5860 ++it->sp;
5861
5862 /* Save the state of the bidi iterator as well. */
5863 if (it->bidi_p)
5864 bidi_push_it (&it->bidi_it);
5865 }
5866
5867 static void
5868 iterate_out_of_display_property (struct it *it)
5869 {
5870 int buffer_p = !STRINGP (it->string);
5871 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5872 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5873
5874 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5875
5876 /* Maybe initialize paragraph direction. If we are at the beginning
5877 of a new paragraph, next_element_from_buffer may not have a
5878 chance to do that. */
5879 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5880 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5881 /* prev_stop can be zero, so check against BEGV as well. */
5882 while (it->bidi_it.charpos >= bob
5883 && it->prev_stop <= it->bidi_it.charpos
5884 && it->bidi_it.charpos < CHARPOS (it->position)
5885 && it->bidi_it.charpos < eob)
5886 bidi_move_to_visually_next (&it->bidi_it);
5887 /* Record the stop_pos we just crossed, for when we cross it
5888 back, maybe. */
5889 if (it->bidi_it.charpos > CHARPOS (it->position))
5890 it->prev_stop = CHARPOS (it->position);
5891 /* If we ended up not where pop_it put us, resync IT's
5892 positional members with the bidi iterator. */
5893 if (it->bidi_it.charpos != CHARPOS (it->position))
5894 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5895 if (buffer_p)
5896 it->current.pos = it->position;
5897 else
5898 it->current.string_pos = it->position;
5899 }
5900
5901 /* Restore IT's settings from IT->stack. Called, for example, when no
5902 more overlay strings must be processed, and we return to delivering
5903 display elements from a buffer, or when the end of a string from a
5904 `display' property is reached and we return to delivering display
5905 elements from an overlay string, or from a buffer. */
5906
5907 static void
5908 pop_it (struct it *it)
5909 {
5910 struct iterator_stack_entry *p;
5911 int from_display_prop = it->from_disp_prop_p;
5912
5913 eassert (it->sp > 0);
5914 --it->sp;
5915 p = it->stack + it->sp;
5916 it->stop_charpos = p->stop_charpos;
5917 it->prev_stop = p->prev_stop;
5918 it->base_level_stop = p->base_level_stop;
5919 it->cmp_it = p->cmp_it;
5920 it->face_id = p->face_id;
5921 it->current = p->current;
5922 it->position = p->position;
5923 it->string = p->string;
5924 it->from_overlay = p->from_overlay;
5925 if (NILP (it->string))
5926 SET_TEXT_POS (it->current.string_pos, -1, -1);
5927 it->method = p->method;
5928 switch (it->method)
5929 {
5930 case GET_FROM_IMAGE:
5931 it->image_id = p->u.image.image_id;
5932 it->object = p->u.image.object;
5933 it->slice = p->u.image.slice;
5934 break;
5935 case GET_FROM_STRETCH:
5936 it->object = p->u.stretch.object;
5937 break;
5938 case GET_FROM_BUFFER:
5939 it->object = it->w->contents;
5940 break;
5941 case GET_FROM_STRING:
5942 it->object = it->string;
5943 break;
5944 case GET_FROM_DISPLAY_VECTOR:
5945 if (it->s)
5946 it->method = GET_FROM_C_STRING;
5947 else if (STRINGP (it->string))
5948 it->method = GET_FROM_STRING;
5949 else
5950 {
5951 it->method = GET_FROM_BUFFER;
5952 it->object = it->w->contents;
5953 }
5954 }
5955 it->end_charpos = p->end_charpos;
5956 it->string_nchars = p->string_nchars;
5957 it->area = p->area;
5958 it->multibyte_p = p->multibyte_p;
5959 it->avoid_cursor_p = p->avoid_cursor_p;
5960 it->space_width = p->space_width;
5961 it->font_height = p->font_height;
5962 it->voffset = p->voffset;
5963 it->string_from_display_prop_p = p->string_from_display_prop_p;
5964 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5965 it->line_wrap = p->line_wrap;
5966 it->bidi_p = p->bidi_p;
5967 it->paragraph_embedding = p->paragraph_embedding;
5968 it->from_disp_prop_p = p->from_disp_prop_p;
5969 if (it->bidi_p)
5970 {
5971 bidi_pop_it (&it->bidi_it);
5972 /* Bidi-iterate until we get out of the portion of text, if any,
5973 covered by a `display' text property or by an overlay with
5974 `display' property. (We cannot just jump there, because the
5975 internal coherency of the bidi iterator state can not be
5976 preserved across such jumps.) We also must determine the
5977 paragraph base direction if the overlay we just processed is
5978 at the beginning of a new paragraph. */
5979 if (from_display_prop
5980 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5981 iterate_out_of_display_property (it);
5982
5983 eassert ((BUFFERP (it->object)
5984 && IT_CHARPOS (*it) == it->bidi_it.charpos
5985 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5986 || (STRINGP (it->object)
5987 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5988 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5989 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5990 }
5991 }
5992
5993
5994 \f
5995 /***********************************************************************
5996 Moving over lines
5997 ***********************************************************************/
5998
5999 /* Set IT's current position to the previous line start. */
6000
6001 static void
6002 back_to_previous_line_start (struct it *it)
6003 {
6004 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6005
6006 DEC_BOTH (cp, bp);
6007 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6008 }
6009
6010
6011 /* Move IT to the next line start.
6012
6013 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6014 we skipped over part of the text (as opposed to moving the iterator
6015 continuously over the text). Otherwise, don't change the value
6016 of *SKIPPED_P.
6017
6018 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6019 iterator on the newline, if it was found.
6020
6021 Newlines may come from buffer text, overlay strings, or strings
6022 displayed via the `display' property. That's the reason we can't
6023 simply use find_newline_no_quit.
6024
6025 Note that this function may not skip over invisible text that is so
6026 because of text properties and immediately follows a newline. If
6027 it would, function reseat_at_next_visible_line_start, when called
6028 from set_iterator_to_next, would effectively make invisible
6029 characters following a newline part of the wrong glyph row, which
6030 leads to wrong cursor motion. */
6031
6032 static int
6033 forward_to_next_line_start (struct it *it, int *skipped_p,
6034 struct bidi_it *bidi_it_prev)
6035 {
6036 ptrdiff_t old_selective;
6037 int newline_found_p, n;
6038 const int MAX_NEWLINE_DISTANCE = 500;
6039
6040 /* If already on a newline, just consume it to avoid unintended
6041 skipping over invisible text below. */
6042 if (it->what == IT_CHARACTER
6043 && it->c == '\n'
6044 && CHARPOS (it->position) == IT_CHARPOS (*it))
6045 {
6046 if (it->bidi_p && bidi_it_prev)
6047 *bidi_it_prev = it->bidi_it;
6048 set_iterator_to_next (it, 0);
6049 it->c = 0;
6050 return 1;
6051 }
6052
6053 /* Don't handle selective display in the following. It's (a)
6054 unnecessary because it's done by the caller, and (b) leads to an
6055 infinite recursion because next_element_from_ellipsis indirectly
6056 calls this function. */
6057 old_selective = it->selective;
6058 it->selective = 0;
6059
6060 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6061 from buffer text. */
6062 for (n = newline_found_p = 0;
6063 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6064 n += STRINGP (it->string) ? 0 : 1)
6065 {
6066 if (!get_next_display_element (it))
6067 return 0;
6068 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6069 if (newline_found_p && it->bidi_p && bidi_it_prev)
6070 *bidi_it_prev = it->bidi_it;
6071 set_iterator_to_next (it, 0);
6072 }
6073
6074 /* If we didn't find a newline near enough, see if we can use a
6075 short-cut. */
6076 if (!newline_found_p)
6077 {
6078 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6079 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6080 1, &bytepos);
6081 Lisp_Object pos;
6082
6083 eassert (!STRINGP (it->string));
6084
6085 /* If there isn't any `display' property in sight, and no
6086 overlays, we can just use the position of the newline in
6087 buffer text. */
6088 if (it->stop_charpos >= limit
6089 || ((pos = Fnext_single_property_change (make_number (start),
6090 Qdisplay, Qnil,
6091 make_number (limit)),
6092 NILP (pos))
6093 && next_overlay_change (start) == ZV))
6094 {
6095 if (!it->bidi_p)
6096 {
6097 IT_CHARPOS (*it) = limit;
6098 IT_BYTEPOS (*it) = bytepos;
6099 }
6100 else
6101 {
6102 struct bidi_it bprev;
6103
6104 /* Help bidi.c avoid expensive searches for display
6105 properties and overlays, by telling it that there are
6106 none up to `limit'. */
6107 if (it->bidi_it.disp_pos < limit)
6108 {
6109 it->bidi_it.disp_pos = limit;
6110 it->bidi_it.disp_prop = 0;
6111 }
6112 do {
6113 bprev = it->bidi_it;
6114 bidi_move_to_visually_next (&it->bidi_it);
6115 } while (it->bidi_it.charpos != limit);
6116 IT_CHARPOS (*it) = limit;
6117 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6118 if (bidi_it_prev)
6119 *bidi_it_prev = bprev;
6120 }
6121 *skipped_p = newline_found_p = true;
6122 }
6123 else
6124 {
6125 while (get_next_display_element (it)
6126 && !newline_found_p)
6127 {
6128 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6129 if (newline_found_p && it->bidi_p && bidi_it_prev)
6130 *bidi_it_prev = it->bidi_it;
6131 set_iterator_to_next (it, 0);
6132 }
6133 }
6134 }
6135
6136 it->selective = old_selective;
6137 return newline_found_p;
6138 }
6139
6140
6141 /* Set IT's current position to the previous visible line start. Skip
6142 invisible text that is so either due to text properties or due to
6143 selective display. Caution: this does not change IT->current_x and
6144 IT->hpos. */
6145
6146 static void
6147 back_to_previous_visible_line_start (struct it *it)
6148 {
6149 while (IT_CHARPOS (*it) > BEGV)
6150 {
6151 back_to_previous_line_start (it);
6152
6153 if (IT_CHARPOS (*it) <= BEGV)
6154 break;
6155
6156 /* If selective > 0, then lines indented more than its value are
6157 invisible. */
6158 if (it->selective > 0
6159 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6160 it->selective))
6161 continue;
6162
6163 /* Check the newline before point for invisibility. */
6164 {
6165 Lisp_Object prop;
6166 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6167 Qinvisible, it->window);
6168 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6169 continue;
6170 }
6171
6172 if (IT_CHARPOS (*it) <= BEGV)
6173 break;
6174
6175 {
6176 struct it it2;
6177 void *it2data = NULL;
6178 ptrdiff_t pos;
6179 ptrdiff_t beg, end;
6180 Lisp_Object val, overlay;
6181
6182 SAVE_IT (it2, *it, it2data);
6183
6184 /* If newline is part of a composition, continue from start of composition */
6185 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6186 && beg < IT_CHARPOS (*it))
6187 goto replaced;
6188
6189 /* If newline is replaced by a display property, find start of overlay
6190 or interval and continue search from that point. */
6191 pos = --IT_CHARPOS (it2);
6192 --IT_BYTEPOS (it2);
6193 it2.sp = 0;
6194 bidi_unshelve_cache (NULL, 0);
6195 it2.string_from_display_prop_p = 0;
6196 it2.from_disp_prop_p = 0;
6197 if (handle_display_prop (&it2) == HANDLED_RETURN
6198 && !NILP (val = get_char_property_and_overlay
6199 (make_number (pos), Qdisplay, Qnil, &overlay))
6200 && (OVERLAYP (overlay)
6201 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6202 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6203 {
6204 RESTORE_IT (it, it, it2data);
6205 goto replaced;
6206 }
6207
6208 /* Newline is not replaced by anything -- so we are done. */
6209 RESTORE_IT (it, it, it2data);
6210 break;
6211
6212 replaced:
6213 if (beg < BEGV)
6214 beg = BEGV;
6215 IT_CHARPOS (*it) = beg;
6216 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6217 }
6218 }
6219
6220 it->continuation_lines_width = 0;
6221
6222 eassert (IT_CHARPOS (*it) >= BEGV);
6223 eassert (IT_CHARPOS (*it) == BEGV
6224 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6225 CHECK_IT (it);
6226 }
6227
6228
6229 /* Reseat iterator IT at the previous visible line start. Skip
6230 invisible text that is so either due to text properties or due to
6231 selective display. At the end, update IT's overlay information,
6232 face information etc. */
6233
6234 void
6235 reseat_at_previous_visible_line_start (struct it *it)
6236 {
6237 back_to_previous_visible_line_start (it);
6238 reseat (it, it->current.pos, 1);
6239 CHECK_IT (it);
6240 }
6241
6242
6243 /* Reseat iterator IT on the next visible line start in the current
6244 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6245 preceding the line start. Skip over invisible text that is so
6246 because of selective display. Compute faces, overlays etc at the
6247 new position. Note that this function does not skip over text that
6248 is invisible because of text properties. */
6249
6250 static void
6251 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6252 {
6253 int newline_found_p, skipped_p = 0;
6254 struct bidi_it bidi_it_prev;
6255
6256 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6257
6258 /* Skip over lines that are invisible because they are indented
6259 more than the value of IT->selective. */
6260 if (it->selective > 0)
6261 while (IT_CHARPOS (*it) < ZV
6262 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6263 it->selective))
6264 {
6265 eassert (IT_BYTEPOS (*it) == BEGV
6266 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6267 newline_found_p =
6268 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6269 }
6270
6271 /* Position on the newline if that's what's requested. */
6272 if (on_newline_p && newline_found_p)
6273 {
6274 if (STRINGP (it->string))
6275 {
6276 if (IT_STRING_CHARPOS (*it) > 0)
6277 {
6278 if (!it->bidi_p)
6279 {
6280 --IT_STRING_CHARPOS (*it);
6281 --IT_STRING_BYTEPOS (*it);
6282 }
6283 else
6284 {
6285 /* We need to restore the bidi iterator to the state
6286 it had on the newline, and resync the IT's
6287 position with that. */
6288 it->bidi_it = bidi_it_prev;
6289 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6290 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6291 }
6292 }
6293 }
6294 else if (IT_CHARPOS (*it) > BEGV)
6295 {
6296 if (!it->bidi_p)
6297 {
6298 --IT_CHARPOS (*it);
6299 --IT_BYTEPOS (*it);
6300 }
6301 else
6302 {
6303 /* We need to restore the bidi iterator to the state it
6304 had on the newline and resync IT with that. */
6305 it->bidi_it = bidi_it_prev;
6306 IT_CHARPOS (*it) = it->bidi_it.charpos;
6307 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6308 }
6309 reseat (it, it->current.pos, 0);
6310 }
6311 }
6312 else if (skipped_p)
6313 reseat (it, it->current.pos, 0);
6314
6315 CHECK_IT (it);
6316 }
6317
6318
6319 \f
6320 /***********************************************************************
6321 Changing an iterator's position
6322 ***********************************************************************/
6323
6324 /* Change IT's current position to POS in current_buffer. If FORCE_P
6325 is non-zero, always check for text properties at the new position.
6326 Otherwise, text properties are only looked up if POS >=
6327 IT->check_charpos of a property. */
6328
6329 static void
6330 reseat (struct it *it, struct text_pos pos, int force_p)
6331 {
6332 ptrdiff_t original_pos = IT_CHARPOS (*it);
6333
6334 reseat_1 (it, pos, 0);
6335
6336 /* Determine where to check text properties. Avoid doing it
6337 where possible because text property lookup is very expensive. */
6338 if (force_p
6339 || CHARPOS (pos) > it->stop_charpos
6340 || CHARPOS (pos) < original_pos)
6341 {
6342 if (it->bidi_p)
6343 {
6344 /* For bidi iteration, we need to prime prev_stop and
6345 base_level_stop with our best estimations. */
6346 /* Implementation note: Of course, POS is not necessarily a
6347 stop position, so assigning prev_pos to it is a lie; we
6348 should have called compute_stop_backwards. However, if
6349 the current buffer does not include any R2L characters,
6350 that call would be a waste of cycles, because the
6351 iterator will never move back, and thus never cross this
6352 "fake" stop position. So we delay that backward search
6353 until the time we really need it, in next_element_from_buffer. */
6354 if (CHARPOS (pos) != it->prev_stop)
6355 it->prev_stop = CHARPOS (pos);
6356 if (CHARPOS (pos) < it->base_level_stop)
6357 it->base_level_stop = 0; /* meaning it's unknown */
6358 handle_stop (it);
6359 }
6360 else
6361 {
6362 handle_stop (it);
6363 it->prev_stop = it->base_level_stop = 0;
6364 }
6365
6366 }
6367
6368 CHECK_IT (it);
6369 }
6370
6371
6372 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6373 IT->stop_pos to POS, also. */
6374
6375 static void
6376 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6377 {
6378 /* Don't call this function when scanning a C string. */
6379 eassert (it->s == NULL);
6380
6381 /* POS must be a reasonable value. */
6382 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6383
6384 it->current.pos = it->position = pos;
6385 it->end_charpos = ZV;
6386 it->dpvec = NULL;
6387 it->current.dpvec_index = -1;
6388 it->current.overlay_string_index = -1;
6389 IT_STRING_CHARPOS (*it) = -1;
6390 IT_STRING_BYTEPOS (*it) = -1;
6391 it->string = Qnil;
6392 it->method = GET_FROM_BUFFER;
6393 it->object = it->w->contents;
6394 it->area = TEXT_AREA;
6395 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6396 it->sp = 0;
6397 it->string_from_display_prop_p = 0;
6398 it->string_from_prefix_prop_p = 0;
6399
6400 it->from_disp_prop_p = 0;
6401 it->face_before_selective_p = 0;
6402 if (it->bidi_p)
6403 {
6404 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6405 &it->bidi_it);
6406 bidi_unshelve_cache (NULL, 0);
6407 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6408 it->bidi_it.string.s = NULL;
6409 it->bidi_it.string.lstring = Qnil;
6410 it->bidi_it.string.bufpos = 0;
6411 it->bidi_it.string.unibyte = 0;
6412 it->bidi_it.w = it->w;
6413 }
6414
6415 if (set_stop_p)
6416 {
6417 it->stop_charpos = CHARPOS (pos);
6418 it->base_level_stop = CHARPOS (pos);
6419 }
6420 /* This make the information stored in it->cmp_it invalidate. */
6421 it->cmp_it.id = -1;
6422 }
6423
6424
6425 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6426 If S is non-null, it is a C string to iterate over. Otherwise,
6427 STRING gives a Lisp string to iterate over.
6428
6429 If PRECISION > 0, don't return more then PRECISION number of
6430 characters from the string.
6431
6432 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6433 characters have been returned. FIELD_WIDTH < 0 means an infinite
6434 field width.
6435
6436 MULTIBYTE = 0 means disable processing of multibyte characters,
6437 MULTIBYTE > 0 means enable it,
6438 MULTIBYTE < 0 means use IT->multibyte_p.
6439
6440 IT must be initialized via a prior call to init_iterator before
6441 calling this function. */
6442
6443 static void
6444 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6445 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6446 int multibyte)
6447 {
6448 /* No text property checks performed by default, but see below. */
6449 it->stop_charpos = -1;
6450
6451 /* Set iterator position and end position. */
6452 memset (&it->current, 0, sizeof it->current);
6453 it->current.overlay_string_index = -1;
6454 it->current.dpvec_index = -1;
6455 eassert (charpos >= 0);
6456
6457 /* If STRING is specified, use its multibyteness, otherwise use the
6458 setting of MULTIBYTE, if specified. */
6459 if (multibyte >= 0)
6460 it->multibyte_p = multibyte > 0;
6461
6462 /* Bidirectional reordering of strings is controlled by the default
6463 value of bidi-display-reordering. Don't try to reorder while
6464 loading loadup.el, as the necessary character property tables are
6465 not yet available. */
6466 it->bidi_p =
6467 NILP (Vpurify_flag)
6468 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6469
6470 if (s == NULL)
6471 {
6472 eassert (STRINGP (string));
6473 it->string = string;
6474 it->s = NULL;
6475 it->end_charpos = it->string_nchars = SCHARS (string);
6476 it->method = GET_FROM_STRING;
6477 it->current.string_pos = string_pos (charpos, string);
6478
6479 if (it->bidi_p)
6480 {
6481 it->bidi_it.string.lstring = string;
6482 it->bidi_it.string.s = NULL;
6483 it->bidi_it.string.schars = it->end_charpos;
6484 it->bidi_it.string.bufpos = 0;
6485 it->bidi_it.string.from_disp_str = 0;
6486 it->bidi_it.string.unibyte = !it->multibyte_p;
6487 it->bidi_it.w = it->w;
6488 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6489 FRAME_WINDOW_P (it->f), &it->bidi_it);
6490 }
6491 }
6492 else
6493 {
6494 it->s = (const unsigned char *) s;
6495 it->string = Qnil;
6496
6497 /* Note that we use IT->current.pos, not it->current.string_pos,
6498 for displaying C strings. */
6499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6500 if (it->multibyte_p)
6501 {
6502 it->current.pos = c_string_pos (charpos, s, 1);
6503 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6504 }
6505 else
6506 {
6507 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6508 it->end_charpos = it->string_nchars = strlen (s);
6509 }
6510
6511 if (it->bidi_p)
6512 {
6513 it->bidi_it.string.lstring = Qnil;
6514 it->bidi_it.string.s = (const unsigned char *) s;
6515 it->bidi_it.string.schars = it->end_charpos;
6516 it->bidi_it.string.bufpos = 0;
6517 it->bidi_it.string.from_disp_str = 0;
6518 it->bidi_it.string.unibyte = !it->multibyte_p;
6519 it->bidi_it.w = it->w;
6520 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6521 &it->bidi_it);
6522 }
6523 it->method = GET_FROM_C_STRING;
6524 }
6525
6526 /* PRECISION > 0 means don't return more than PRECISION characters
6527 from the string. */
6528 if (precision > 0 && it->end_charpos - charpos > precision)
6529 {
6530 it->end_charpos = it->string_nchars = charpos + precision;
6531 if (it->bidi_p)
6532 it->bidi_it.string.schars = it->end_charpos;
6533 }
6534
6535 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6536 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6537 FIELD_WIDTH < 0 means infinite field width. This is useful for
6538 padding with `-' at the end of a mode line. */
6539 if (field_width < 0)
6540 field_width = INFINITY;
6541 /* Implementation note: We deliberately don't enlarge
6542 it->bidi_it.string.schars here to fit it->end_charpos, because
6543 the bidi iterator cannot produce characters out of thin air. */
6544 if (field_width > it->end_charpos - charpos)
6545 it->end_charpos = charpos + field_width;
6546
6547 /* Use the standard display table for displaying strings. */
6548 if (DISP_TABLE_P (Vstandard_display_table))
6549 it->dp = XCHAR_TABLE (Vstandard_display_table);
6550
6551 it->stop_charpos = charpos;
6552 it->prev_stop = charpos;
6553 it->base_level_stop = 0;
6554 if (it->bidi_p)
6555 {
6556 it->bidi_it.first_elt = 1;
6557 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6558 it->bidi_it.disp_pos = -1;
6559 }
6560 if (s == NULL && it->multibyte_p)
6561 {
6562 ptrdiff_t endpos = SCHARS (it->string);
6563 if (endpos > it->end_charpos)
6564 endpos = it->end_charpos;
6565 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6566 it->string);
6567 }
6568 CHECK_IT (it);
6569 }
6570
6571
6572 \f
6573 /***********************************************************************
6574 Iteration
6575 ***********************************************************************/
6576
6577 /* Map enum it_method value to corresponding next_element_from_* function. */
6578
6579 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6580 {
6581 next_element_from_buffer,
6582 next_element_from_display_vector,
6583 next_element_from_string,
6584 next_element_from_c_string,
6585 next_element_from_image,
6586 next_element_from_stretch
6587 };
6588
6589 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6590
6591
6592 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6593 (possibly with the following characters). */
6594
6595 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6596 ((IT)->cmp_it.id >= 0 \
6597 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6598 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6599 END_CHARPOS, (IT)->w, \
6600 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6601 (IT)->string)))
6602
6603
6604 /* Lookup the char-table Vglyphless_char_display for character C (-1
6605 if we want information for no-font case), and return the display
6606 method symbol. By side-effect, update it->what and
6607 it->glyphless_method. This function is called from
6608 get_next_display_element for each character element, and from
6609 x_produce_glyphs when no suitable font was found. */
6610
6611 Lisp_Object
6612 lookup_glyphless_char_display (int c, struct it *it)
6613 {
6614 Lisp_Object glyphless_method = Qnil;
6615
6616 if (CHAR_TABLE_P (Vglyphless_char_display)
6617 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6618 {
6619 if (c >= 0)
6620 {
6621 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6622 if (CONSP (glyphless_method))
6623 glyphless_method = FRAME_WINDOW_P (it->f)
6624 ? XCAR (glyphless_method)
6625 : XCDR (glyphless_method);
6626 }
6627 else
6628 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6629 }
6630
6631 retry:
6632 if (NILP (glyphless_method))
6633 {
6634 if (c >= 0)
6635 /* The default is to display the character by a proper font. */
6636 return Qnil;
6637 /* The default for the no-font case is to display an empty box. */
6638 glyphless_method = Qempty_box;
6639 }
6640 if (EQ (glyphless_method, Qzero_width))
6641 {
6642 if (c >= 0)
6643 return glyphless_method;
6644 /* This method can't be used for the no-font case. */
6645 glyphless_method = Qempty_box;
6646 }
6647 if (EQ (glyphless_method, Qthin_space))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6649 else if (EQ (glyphless_method, Qempty_box))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6651 else if (EQ (glyphless_method, Qhex_code))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6653 else if (STRINGP (glyphless_method))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6655 else
6656 {
6657 /* Invalid value. We use the default method. */
6658 glyphless_method = Qnil;
6659 goto retry;
6660 }
6661 it->what = IT_GLYPHLESS;
6662 return glyphless_method;
6663 }
6664
6665 /* Merge escape glyph face and cache the result. */
6666
6667 static struct frame *last_escape_glyph_frame = NULL;
6668 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6669 static int last_escape_glyph_merged_face_id = 0;
6670
6671 static int
6672 merge_escape_glyph_face (struct it *it)
6673 {
6674 int face_id;
6675
6676 if (it->f == last_escape_glyph_frame
6677 && it->face_id == last_escape_glyph_face_id)
6678 face_id = last_escape_glyph_merged_face_id;
6679 else
6680 {
6681 /* Merge the `escape-glyph' face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6683 last_escape_glyph_frame = it->f;
6684 last_escape_glyph_face_id = it->face_id;
6685 last_escape_glyph_merged_face_id = face_id;
6686 }
6687 return face_id;
6688 }
6689
6690 /* Likewise for glyphless glyph face. */
6691
6692 static struct frame *last_glyphless_glyph_frame = NULL;
6693 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 static int last_glyphless_glyph_merged_face_id = 0;
6695
6696 int
6697 merge_glyphless_glyph_face (struct it *it)
6698 {
6699 int face_id;
6700
6701 if (it->f == last_glyphless_glyph_frame
6702 && it->face_id == last_glyphless_glyph_face_id)
6703 face_id = last_glyphless_glyph_merged_face_id;
6704 else
6705 {
6706 /* Merge the `glyphless-char' face into the current face. */
6707 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6708 last_glyphless_glyph_frame = it->f;
6709 last_glyphless_glyph_face_id = it->face_id;
6710 last_glyphless_glyph_merged_face_id = face_id;
6711 }
6712 return face_id;
6713 }
6714
6715 /* Load IT's display element fields with information about the next
6716 display element from the current position of IT. Value is zero if
6717 end of buffer (or C string) is reached. */
6718
6719 static int
6720 get_next_display_element (struct it *it)
6721 {
6722 /* Non-zero means that we found a display element. Zero means that
6723 we hit the end of what we iterate over. Performance note: the
6724 function pointer `method' used here turns out to be faster than
6725 using a sequence of if-statements. */
6726 int success_p;
6727
6728 get_next:
6729 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6730
6731 if (it->what == IT_CHARACTER)
6732 {
6733 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6734 and only if (a) the resolved directionality of that character
6735 is R..." */
6736 /* FIXME: Do we need an exception for characters from display
6737 tables? */
6738 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6739 it->c = bidi_mirror_char (it->c);
6740 /* Map via display table or translate control characters.
6741 IT->c, IT->len etc. have been set to the next character by
6742 the function call above. If we have a display table, and it
6743 contains an entry for IT->c, translate it. Don't do this if
6744 IT->c itself comes from a display table, otherwise we could
6745 end up in an infinite recursion. (An alternative could be to
6746 count the recursion depth of this function and signal an
6747 error when a certain maximum depth is reached.) Is it worth
6748 it? */
6749 if (success_p && it->dpvec == NULL)
6750 {
6751 Lisp_Object dv;
6752 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6753 int nonascii_space_p = 0;
6754 int nonascii_hyphen_p = 0;
6755 int c = it->c; /* This is the character to display. */
6756
6757 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6758 {
6759 eassert (SINGLE_BYTE_CHAR_P (c));
6760 if (unibyte_display_via_language_environment)
6761 {
6762 c = DECODE_CHAR (unibyte, c);
6763 if (c < 0)
6764 c = BYTE8_TO_CHAR (it->c);
6765 }
6766 else
6767 c = BYTE8_TO_CHAR (it->c);
6768 }
6769
6770 if (it->dp
6771 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6772 VECTORP (dv)))
6773 {
6774 struct Lisp_Vector *v = XVECTOR (dv);
6775
6776 /* Return the first character from the display table
6777 entry, if not empty. If empty, don't display the
6778 current character. */
6779 if (v->header.size)
6780 {
6781 it->dpvec_char_len = it->len;
6782 it->dpvec = v->contents;
6783 it->dpend = v->contents + v->header.size;
6784 it->current.dpvec_index = 0;
6785 it->dpvec_face_id = -1;
6786 it->saved_face_id = it->face_id;
6787 it->method = GET_FROM_DISPLAY_VECTOR;
6788 it->ellipsis_p = 0;
6789 }
6790 else
6791 {
6792 set_iterator_to_next (it, 0);
6793 }
6794 goto get_next;
6795 }
6796
6797 if (! NILP (lookup_glyphless_char_display (c, it)))
6798 {
6799 if (it->what == IT_GLYPHLESS)
6800 goto done;
6801 /* Don't display this character. */
6802 set_iterator_to_next (it, 0);
6803 goto get_next;
6804 }
6805
6806 /* If `nobreak-char-display' is non-nil, we display
6807 non-ASCII spaces and hyphens specially. */
6808 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6809 {
6810 if (c == 0xA0)
6811 nonascii_space_p = true;
6812 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6813 nonascii_hyphen_p = true;
6814 }
6815
6816 /* Translate control characters into `\003' or `^C' form.
6817 Control characters coming from a display table entry are
6818 currently not translated because we use IT->dpvec to hold
6819 the translation. This could easily be changed but I
6820 don't believe that it is worth doing.
6821
6822 The characters handled by `nobreak-char-display' must be
6823 translated too.
6824
6825 Non-printable characters and raw-byte characters are also
6826 translated to octal form. */
6827 if (((c < ' ' || c == 127) /* ASCII control chars. */
6828 ? (it->area != TEXT_AREA
6829 /* In mode line, treat \n, \t like other crl chars. */
6830 || (c != '\t'
6831 && it->glyph_row
6832 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6833 || (c != '\n' && c != '\t'))
6834 : (nonascii_space_p
6835 || nonascii_hyphen_p
6836 || CHAR_BYTE8_P (c)
6837 || ! CHAR_PRINTABLE_P (c))))
6838 {
6839 /* C is a control character, non-ASCII space/hyphen,
6840 raw-byte, or a non-printable character which must be
6841 displayed either as '\003' or as `^C' where the '\\'
6842 and '^' can be defined in the display table. Fill
6843 IT->ctl_chars with glyphs for what we have to
6844 display. Then, set IT->dpvec to these glyphs. */
6845 Lisp_Object gc;
6846 int ctl_len;
6847 int face_id;
6848 int lface_id = 0;
6849 int escape_glyph;
6850
6851 /* Handle control characters with ^. */
6852
6853 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6854 {
6855 int g;
6856
6857 g = '^'; /* default glyph for Control */
6858 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6859 if (it->dp
6860 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6861 {
6862 g = GLYPH_CODE_CHAR (gc);
6863 lface_id = GLYPH_CODE_FACE (gc);
6864 }
6865
6866 face_id = (lface_id
6867 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6868 : merge_escape_glyph_face (it));
6869
6870 XSETINT (it->ctl_chars[0], g);
6871 XSETINT (it->ctl_chars[1], c ^ 0100);
6872 ctl_len = 2;
6873 goto display_control;
6874 }
6875
6876 /* Handle non-ascii space in the mode where it only gets
6877 highlighting. */
6878
6879 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6880 {
6881 /* Merge `nobreak-space' into the current face. */
6882 face_id = merge_faces (it->f, Qnobreak_space, 0,
6883 it->face_id);
6884 XSETINT (it->ctl_chars[0], ' ');
6885 ctl_len = 1;
6886 goto display_control;
6887 }
6888
6889 /* Handle sequences that start with the "escape glyph". */
6890
6891 /* the default escape glyph is \. */
6892 escape_glyph = '\\';
6893
6894 if (it->dp
6895 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6896 {
6897 escape_glyph = GLYPH_CODE_CHAR (gc);
6898 lface_id = GLYPH_CODE_FACE (gc);
6899 }
6900
6901 face_id = (lface_id
6902 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6903 : merge_escape_glyph_face (it));
6904
6905 /* Draw non-ASCII hyphen with just highlighting: */
6906
6907 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6908 {
6909 XSETINT (it->ctl_chars[0], '-');
6910 ctl_len = 1;
6911 goto display_control;
6912 }
6913
6914 /* Draw non-ASCII space/hyphen with escape glyph: */
6915
6916 if (nonascii_space_p || nonascii_hyphen_p)
6917 {
6918 XSETINT (it->ctl_chars[0], escape_glyph);
6919 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6920 ctl_len = 2;
6921 goto display_control;
6922 }
6923
6924 {
6925 char str[10];
6926 int len, i;
6927
6928 if (CHAR_BYTE8_P (c))
6929 /* Display \200 instead of \17777600. */
6930 c = CHAR_TO_BYTE8 (c);
6931 len = sprintf (str, "%03o", c);
6932
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 for (i = 0; i < len; i++)
6935 XSETINT (it->ctl_chars[i + 1], str[i]);
6936 ctl_len = len + 1;
6937 }
6938
6939 display_control:
6940 /* Set up IT->dpvec and return first character from it. */
6941 it->dpvec_char_len = it->len;
6942 it->dpvec = it->ctl_chars;
6943 it->dpend = it->dpvec + ctl_len;
6944 it->current.dpvec_index = 0;
6945 it->dpvec_face_id = face_id;
6946 it->saved_face_id = it->face_id;
6947 it->method = GET_FROM_DISPLAY_VECTOR;
6948 it->ellipsis_p = 0;
6949 goto get_next;
6950 }
6951 it->char_to_display = c;
6952 }
6953 else if (success_p)
6954 {
6955 it->char_to_display = it->c;
6956 }
6957 }
6958
6959 #ifdef HAVE_WINDOW_SYSTEM
6960 /* Adjust face id for a multibyte character. There are no multibyte
6961 character in unibyte text. */
6962 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6963 && it->multibyte_p
6964 && success_p
6965 && FRAME_WINDOW_P (it->f))
6966 {
6967 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6968
6969 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6970 {
6971 /* Automatic composition with glyph-string. */
6972 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6973
6974 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6975 }
6976 else
6977 {
6978 ptrdiff_t pos = (it->s ? -1
6979 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6980 : IT_CHARPOS (*it));
6981 int c;
6982
6983 if (it->what == IT_CHARACTER)
6984 c = it->char_to_display;
6985 else
6986 {
6987 struct composition *cmp = composition_table[it->cmp_it.id];
6988 int i;
6989
6990 c = ' ';
6991 for (i = 0; i < cmp->glyph_len; i++)
6992 /* TAB in a composition means display glyphs with
6993 padding space on the left or right. */
6994 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6995 break;
6996 }
6997 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6998 }
6999 }
7000 #endif /* HAVE_WINDOW_SYSTEM */
7001
7002 done:
7003 /* Is this character the last one of a run of characters with
7004 box? If yes, set IT->end_of_box_run_p to 1. */
7005 if (it->face_box_p
7006 && it->s == NULL)
7007 {
7008 if (it->method == GET_FROM_STRING && it->sp)
7009 {
7010 int face_id = underlying_face_id (it);
7011 struct face *face = FACE_FROM_ID (it->f, face_id);
7012
7013 if (face)
7014 {
7015 if (face->box == FACE_NO_BOX)
7016 {
7017 /* If the box comes from face properties in a
7018 display string, check faces in that string. */
7019 int string_face_id = face_after_it_pos (it);
7020 it->end_of_box_run_p
7021 = (FACE_FROM_ID (it->f, string_face_id)->box
7022 == FACE_NO_BOX);
7023 }
7024 /* Otherwise, the box comes from the underlying face.
7025 If this is the last string character displayed, check
7026 the next buffer location. */
7027 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7028 && (it->current.overlay_string_index
7029 == it->n_overlay_strings - 1))
7030 {
7031 ptrdiff_t ignore;
7032 int next_face_id;
7033 struct text_pos pos = it->current.pos;
7034 INC_TEXT_POS (pos, it->multibyte_p);
7035
7036 next_face_id = face_at_buffer_position
7037 (it->w, CHARPOS (pos), &ignore,
7038 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7039 -1);
7040 it->end_of_box_run_p
7041 = (FACE_FROM_ID (it->f, next_face_id)->box
7042 == FACE_NO_BOX);
7043 }
7044 }
7045 }
7046 /* next_element_from_display_vector sets this flag according to
7047 faces of the display vector glyphs, see there. */
7048 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7049 {
7050 int face_id = face_after_it_pos (it);
7051 it->end_of_box_run_p
7052 = (face_id != it->face_id
7053 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7054 }
7055 }
7056 /* If we reached the end of the object we've been iterating (e.g., a
7057 display string or an overlay string), and there's something on
7058 IT->stack, proceed with what's on the stack. It doesn't make
7059 sense to return zero if there's unprocessed stuff on the stack,
7060 because otherwise that stuff will never be displayed. */
7061 if (!success_p && it->sp > 0)
7062 {
7063 set_iterator_to_next (it, 0);
7064 success_p = get_next_display_element (it);
7065 }
7066
7067 /* Value is 0 if end of buffer or string reached. */
7068 return success_p;
7069 }
7070
7071
7072 /* Move IT to the next display element.
7073
7074 RESEAT_P non-zero means if called on a newline in buffer text,
7075 skip to the next visible line start.
7076
7077 Functions get_next_display_element and set_iterator_to_next are
7078 separate because I find this arrangement easier to handle than a
7079 get_next_display_element function that also increments IT's
7080 position. The way it is we can first look at an iterator's current
7081 display element, decide whether it fits on a line, and if it does,
7082 increment the iterator position. The other way around we probably
7083 would either need a flag indicating whether the iterator has to be
7084 incremented the next time, or we would have to implement a
7085 decrement position function which would not be easy to write. */
7086
7087 void
7088 set_iterator_to_next (struct it *it, int reseat_p)
7089 {
7090 /* Reset flags indicating start and end of a sequence of characters
7091 with box. Reset them at the start of this function because
7092 moving the iterator to a new position might set them. */
7093 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7094
7095 switch (it->method)
7096 {
7097 case GET_FROM_BUFFER:
7098 /* The current display element of IT is a character from
7099 current_buffer. Advance in the buffer, and maybe skip over
7100 invisible lines that are so because of selective display. */
7101 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7102 reseat_at_next_visible_line_start (it, 0);
7103 else if (it->cmp_it.id >= 0)
7104 {
7105 /* We are currently getting glyphs from a composition. */
7106 int i;
7107
7108 if (! it->bidi_p)
7109 {
7110 IT_CHARPOS (*it) += it->cmp_it.nchars;
7111 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 {
7114 it->cmp_it.from = it->cmp_it.to;
7115 }
7116 else
7117 {
7118 it->cmp_it.id = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it),
7121 it->end_charpos, Qnil);
7122 }
7123 }
7124 else if (! it->cmp_it.reversed_p)
7125 {
7126 /* Composition created while scanning forward. */
7127 /* Update IT's char/byte positions to point to the first
7128 character of the next grapheme cluster, or to the
7129 character visually after the current composition. */
7130 for (i = 0; i < it->cmp_it.nchars; i++)
7131 bidi_move_to_visually_next (&it->bidi_it);
7132 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7133 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134
7135 if (it->cmp_it.to < it->cmp_it.nglyphs)
7136 {
7137 /* Proceed to the next grapheme cluster. */
7138 it->cmp_it.from = it->cmp_it.to;
7139 }
7140 else
7141 {
7142 /* No more grapheme clusters in this composition.
7143 Find the next stop position. */
7144 ptrdiff_t stop = it->end_charpos;
7145 if (it->bidi_it.scan_dir < 0)
7146 /* Now we are scanning backward and don't know
7147 where to stop. */
7148 stop = -1;
7149 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7150 IT_BYTEPOS (*it), stop, Qnil);
7151 }
7152 }
7153 else
7154 {
7155 /* Composition created while scanning backward. */
7156 /* Update IT's char/byte positions to point to the last
7157 character of the previous grapheme cluster, or the
7158 character visually after the current composition. */
7159 for (i = 0; i < it->cmp_it.nchars; i++)
7160 bidi_move_to_visually_next (&it->bidi_it);
7161 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7162 IT_CHARPOS (*it) = it->bidi_it.charpos;
7163 if (it->cmp_it.from > 0)
7164 {
7165 /* Proceed to the previous grapheme cluster. */
7166 it->cmp_it.to = it->cmp_it.from;
7167 }
7168 else
7169 {
7170 /* No more grapheme clusters in this composition.
7171 Find the next stop position. */
7172 ptrdiff_t stop = it->end_charpos;
7173 if (it->bidi_it.scan_dir < 0)
7174 /* Now we are scanning backward and don't know
7175 where to stop. */
7176 stop = -1;
7177 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7178 IT_BYTEPOS (*it), stop, Qnil);
7179 }
7180 }
7181 }
7182 else
7183 {
7184 eassert (it->len != 0);
7185
7186 if (!it->bidi_p)
7187 {
7188 IT_BYTEPOS (*it) += it->len;
7189 IT_CHARPOS (*it) += 1;
7190 }
7191 else
7192 {
7193 int prev_scan_dir = it->bidi_it.scan_dir;
7194 /* If this is a new paragraph, determine its base
7195 direction (a.k.a. its base embedding level). */
7196 if (it->bidi_it.new_paragraph)
7197 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7198 bidi_move_to_visually_next (&it->bidi_it);
7199 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7200 IT_CHARPOS (*it) = it->bidi_it.charpos;
7201 if (prev_scan_dir != it->bidi_it.scan_dir)
7202 {
7203 /* As the scan direction was changed, we must
7204 re-compute the stop position for composition. */
7205 ptrdiff_t stop = it->end_charpos;
7206 if (it->bidi_it.scan_dir < 0)
7207 stop = -1;
7208 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7209 IT_BYTEPOS (*it), stop, Qnil);
7210 }
7211 }
7212 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7213 }
7214 break;
7215
7216 case GET_FROM_C_STRING:
7217 /* Current display element of IT is from a C string. */
7218 if (!it->bidi_p
7219 /* If the string position is beyond string's end, it means
7220 next_element_from_c_string is padding the string with
7221 blanks, in which case we bypass the bidi iterator,
7222 because it cannot deal with such virtual characters. */
7223 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7224 {
7225 IT_BYTEPOS (*it) += it->len;
7226 IT_CHARPOS (*it) += 1;
7227 }
7228 else
7229 {
7230 bidi_move_to_visually_next (&it->bidi_it);
7231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7232 IT_CHARPOS (*it) = it->bidi_it.charpos;
7233 }
7234 break;
7235
7236 case GET_FROM_DISPLAY_VECTOR:
7237 /* Current display element of IT is from a display table entry.
7238 Advance in the display table definition. Reset it to null if
7239 end reached, and continue with characters from buffers/
7240 strings. */
7241 ++it->current.dpvec_index;
7242
7243 /* Restore face of the iterator to what they were before the
7244 display vector entry (these entries may contain faces). */
7245 it->face_id = it->saved_face_id;
7246
7247 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7248 {
7249 int recheck_faces = it->ellipsis_p;
7250
7251 if (it->s)
7252 it->method = GET_FROM_C_STRING;
7253 else if (STRINGP (it->string))
7254 it->method = GET_FROM_STRING;
7255 else
7256 {
7257 it->method = GET_FROM_BUFFER;
7258 it->object = it->w->contents;
7259 }
7260
7261 it->dpvec = NULL;
7262 it->current.dpvec_index = -1;
7263
7264 /* Skip over characters which were displayed via IT->dpvec. */
7265 if (it->dpvec_char_len < 0)
7266 reseat_at_next_visible_line_start (it, 1);
7267 else if (it->dpvec_char_len > 0)
7268 {
7269 if (it->method == GET_FROM_STRING
7270 && it->current.overlay_string_index >= 0
7271 && it->n_overlay_strings > 0)
7272 it->ignore_overlay_strings_at_pos_p = true;
7273 it->len = it->dpvec_char_len;
7274 set_iterator_to_next (it, reseat_p);
7275 }
7276
7277 /* Maybe recheck faces after display vector. */
7278 if (recheck_faces)
7279 it->stop_charpos = IT_CHARPOS (*it);
7280 }
7281 break;
7282
7283 case GET_FROM_STRING:
7284 /* Current display element is a character from a Lisp string. */
7285 eassert (it->s == NULL && STRINGP (it->string));
7286 /* Don't advance past string end. These conditions are true
7287 when set_iterator_to_next is called at the end of
7288 get_next_display_element, in which case the Lisp string is
7289 already exhausted, and all we want is pop the iterator
7290 stack. */
7291 if (it->current.overlay_string_index >= 0)
7292 {
7293 /* This is an overlay string, so there's no padding with
7294 spaces, and the number of characters in the string is
7295 where the string ends. */
7296 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7297 goto consider_string_end;
7298 }
7299 else
7300 {
7301 /* Not an overlay string. There could be padding, so test
7302 against it->end_charpos. */
7303 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7304 goto consider_string_end;
7305 }
7306 if (it->cmp_it.id >= 0)
7307 {
7308 int i;
7309
7310 if (! it->bidi_p)
7311 {
7312 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7313 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7314 if (it->cmp_it.to < it->cmp_it.nglyphs)
7315 it->cmp_it.from = it->cmp_it.to;
7316 else
7317 {
7318 it->cmp_it.id = -1;
7319 composition_compute_stop_pos (&it->cmp_it,
7320 IT_STRING_CHARPOS (*it),
7321 IT_STRING_BYTEPOS (*it),
7322 it->end_charpos, it->string);
7323 }
7324 }
7325 else if (! it->cmp_it.reversed_p)
7326 {
7327 for (i = 0; i < it->cmp_it.nchars; i++)
7328 bidi_move_to_visually_next (&it->bidi_it);
7329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7330 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331
7332 if (it->cmp_it.to < it->cmp_it.nglyphs)
7333 it->cmp_it.from = it->cmp_it.to;
7334 else
7335 {
7336 ptrdiff_t stop = it->end_charpos;
7337 if (it->bidi_it.scan_dir < 0)
7338 stop = -1;
7339 composition_compute_stop_pos (&it->cmp_it,
7340 IT_STRING_CHARPOS (*it),
7341 IT_STRING_BYTEPOS (*it), stop,
7342 it->string);
7343 }
7344 }
7345 else
7346 {
7347 for (i = 0; i < it->cmp_it.nchars; i++)
7348 bidi_move_to_visually_next (&it->bidi_it);
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7350 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7351 if (it->cmp_it.from > 0)
7352 it->cmp_it.to = it->cmp_it.from;
7353 else
7354 {
7355 ptrdiff_t stop = it->end_charpos;
7356 if (it->bidi_it.scan_dir < 0)
7357 stop = -1;
7358 composition_compute_stop_pos (&it->cmp_it,
7359 IT_STRING_CHARPOS (*it),
7360 IT_STRING_BYTEPOS (*it), stop,
7361 it->string);
7362 }
7363 }
7364 }
7365 else
7366 {
7367 if (!it->bidi_p
7368 /* If the string position is beyond string's end, it
7369 means next_element_from_string is padding the string
7370 with blanks, in which case we bypass the bidi
7371 iterator, because it cannot deal with such virtual
7372 characters. */
7373 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7374 {
7375 IT_STRING_BYTEPOS (*it) += it->len;
7376 IT_STRING_CHARPOS (*it) += 1;
7377 }
7378 else
7379 {
7380 int prev_scan_dir = it->bidi_it.scan_dir;
7381
7382 bidi_move_to_visually_next (&it->bidi_it);
7383 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7384 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7385 if (prev_scan_dir != it->bidi_it.scan_dir)
7386 {
7387 ptrdiff_t stop = it->end_charpos;
7388
7389 if (it->bidi_it.scan_dir < 0)
7390 stop = -1;
7391 composition_compute_stop_pos (&it->cmp_it,
7392 IT_STRING_CHARPOS (*it),
7393 IT_STRING_BYTEPOS (*it), stop,
7394 it->string);
7395 }
7396 }
7397 }
7398
7399 consider_string_end:
7400
7401 if (it->current.overlay_string_index >= 0)
7402 {
7403 /* IT->string is an overlay string. Advance to the
7404 next, if there is one. */
7405 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 {
7407 it->ellipsis_p = 0;
7408 next_overlay_string (it);
7409 if (it->ellipsis_p)
7410 setup_for_ellipsis (it, 0);
7411 }
7412 }
7413 else
7414 {
7415 /* IT->string is not an overlay string. If we reached
7416 its end, and there is something on IT->stack, proceed
7417 with what is on the stack. This can be either another
7418 string, this time an overlay string, or a buffer. */
7419 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7420 && it->sp > 0)
7421 {
7422 pop_it (it);
7423 if (it->method == GET_FROM_STRING)
7424 goto consider_string_end;
7425 }
7426 }
7427 break;
7428
7429 case GET_FROM_IMAGE:
7430 case GET_FROM_STRETCH:
7431 /* The position etc with which we have to proceed are on
7432 the stack. The position may be at the end of a string,
7433 if the `display' property takes up the whole string. */
7434 eassert (it->sp > 0);
7435 pop_it (it);
7436 if (it->method == GET_FROM_STRING)
7437 goto consider_string_end;
7438 break;
7439
7440 default:
7441 /* There are no other methods defined, so this should be a bug. */
7442 emacs_abort ();
7443 }
7444
7445 eassert (it->method != GET_FROM_STRING
7446 || (STRINGP (it->string)
7447 && IT_STRING_CHARPOS (*it) >= 0));
7448 }
7449
7450 /* Load IT's display element fields with information about the next
7451 display element which comes from a display table entry or from the
7452 result of translating a control character to one of the forms `^C'
7453 or `\003'.
7454
7455 IT->dpvec holds the glyphs to return as characters.
7456 IT->saved_face_id holds the face id before the display vector--it
7457 is restored into IT->face_id in set_iterator_to_next. */
7458
7459 static int
7460 next_element_from_display_vector (struct it *it)
7461 {
7462 Lisp_Object gc;
7463 int prev_face_id = it->face_id;
7464 int next_face_id;
7465
7466 /* Precondition. */
7467 eassert (it->dpvec && it->current.dpvec_index >= 0);
7468
7469 it->face_id = it->saved_face_id;
7470
7471 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7472 That seemed totally bogus - so I changed it... */
7473 gc = it->dpvec[it->current.dpvec_index];
7474
7475 if (GLYPH_CODE_P (gc))
7476 {
7477 struct face *this_face, *prev_face, *next_face;
7478
7479 it->c = GLYPH_CODE_CHAR (gc);
7480 it->len = CHAR_BYTES (it->c);
7481
7482 /* The entry may contain a face id to use. Such a face id is
7483 the id of a Lisp face, not a realized face. A face id of
7484 zero means no face is specified. */
7485 if (it->dpvec_face_id >= 0)
7486 it->face_id = it->dpvec_face_id;
7487 else
7488 {
7489 int lface_id = GLYPH_CODE_FACE (gc);
7490 if (lface_id > 0)
7491 it->face_id = merge_faces (it->f, Qt, lface_id,
7492 it->saved_face_id);
7493 }
7494
7495 /* Glyphs in the display vector could have the box face, so we
7496 need to set the related flags in the iterator, as
7497 appropriate. */
7498 this_face = FACE_FROM_ID (it->f, it->face_id);
7499 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7500
7501 /* Is this character the first character of a box-face run? */
7502 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7503 && (!prev_face
7504 || prev_face->box == FACE_NO_BOX));
7505
7506 /* For the last character of the box-face run, we need to look
7507 either at the next glyph from the display vector, or at the
7508 face we saw before the display vector. */
7509 next_face_id = it->saved_face_id;
7510 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7511 {
7512 if (it->dpvec_face_id >= 0)
7513 next_face_id = it->dpvec_face_id;
7514 else
7515 {
7516 int lface_id =
7517 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7518
7519 if (lface_id > 0)
7520 next_face_id = merge_faces (it->f, Qt, lface_id,
7521 it->saved_face_id);
7522 }
7523 }
7524 next_face = FACE_FROM_ID (it->f, next_face_id);
7525 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7526 && (!next_face
7527 || next_face->box == FACE_NO_BOX));
7528 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7529 }
7530 else
7531 /* Display table entry is invalid. Return a space. */
7532 it->c = ' ', it->len = 1;
7533
7534 /* Don't change position and object of the iterator here. They are
7535 still the values of the character that had this display table
7536 entry or was translated, and that's what we want. */
7537 it->what = IT_CHARACTER;
7538 return 1;
7539 }
7540
7541 /* Get the first element of string/buffer in the visual order, after
7542 being reseated to a new position in a string or a buffer. */
7543 static void
7544 get_visually_first_element (struct it *it)
7545 {
7546 int string_p = STRINGP (it->string) || it->s;
7547 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7548 ptrdiff_t bob = (string_p ? 0 : BEGV);
7549
7550 if (STRINGP (it->string))
7551 {
7552 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7553 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7554 }
7555 else
7556 {
7557 it->bidi_it.charpos = IT_CHARPOS (*it);
7558 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7559 }
7560
7561 if (it->bidi_it.charpos == eob)
7562 {
7563 /* Nothing to do, but reset the FIRST_ELT flag, like
7564 bidi_paragraph_init does, because we are not going to
7565 call it. */
7566 it->bidi_it.first_elt = 0;
7567 }
7568 else if (it->bidi_it.charpos == bob
7569 || (!string_p
7570 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7571 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7572 {
7573 /* If we are at the beginning of a line/string, we can produce
7574 the next element right away. */
7575 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7576 bidi_move_to_visually_next (&it->bidi_it);
7577 }
7578 else
7579 {
7580 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7581
7582 /* We need to prime the bidi iterator starting at the line's or
7583 string's beginning, before we will be able to produce the
7584 next element. */
7585 if (string_p)
7586 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7587 else
7588 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7589 IT_BYTEPOS (*it), -1,
7590 &it->bidi_it.bytepos);
7591 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7592 do
7593 {
7594 /* Now return to buffer/string position where we were asked
7595 to get the next display element, and produce that. */
7596 bidi_move_to_visually_next (&it->bidi_it);
7597 }
7598 while (it->bidi_it.bytepos != orig_bytepos
7599 && it->bidi_it.charpos < eob);
7600 }
7601
7602 /* Adjust IT's position information to where we ended up. */
7603 if (STRINGP (it->string))
7604 {
7605 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7607 }
7608 else
7609 {
7610 IT_CHARPOS (*it) = it->bidi_it.charpos;
7611 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7612 }
7613
7614 if (STRINGP (it->string) || !it->s)
7615 {
7616 ptrdiff_t stop, charpos, bytepos;
7617
7618 if (STRINGP (it->string))
7619 {
7620 eassert (!it->s);
7621 stop = SCHARS (it->string);
7622 if (stop > it->end_charpos)
7623 stop = it->end_charpos;
7624 charpos = IT_STRING_CHARPOS (*it);
7625 bytepos = IT_STRING_BYTEPOS (*it);
7626 }
7627 else
7628 {
7629 stop = it->end_charpos;
7630 charpos = IT_CHARPOS (*it);
7631 bytepos = IT_BYTEPOS (*it);
7632 }
7633 if (it->bidi_it.scan_dir < 0)
7634 stop = -1;
7635 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7636 it->string);
7637 }
7638 }
7639
7640 /* Load IT with the next display element from Lisp string IT->string.
7641 IT->current.string_pos is the current position within the string.
7642 If IT->current.overlay_string_index >= 0, the Lisp string is an
7643 overlay string. */
7644
7645 static int
7646 next_element_from_string (struct it *it)
7647 {
7648 struct text_pos position;
7649
7650 eassert (STRINGP (it->string));
7651 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7652 eassert (IT_STRING_CHARPOS (*it) >= 0);
7653 position = it->current.string_pos;
7654
7655 /* With bidi reordering, the character to display might not be the
7656 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7657 that we were reseat()ed to a new string, whose paragraph
7658 direction is not known. */
7659 if (it->bidi_p && it->bidi_it.first_elt)
7660 {
7661 get_visually_first_element (it);
7662 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7663 }
7664
7665 /* Time to check for invisible text? */
7666 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7667 {
7668 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7669 {
7670 if (!(!it->bidi_p
7671 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7672 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7673 {
7674 /* With bidi non-linear iteration, we could find
7675 ourselves far beyond the last computed stop_charpos,
7676 with several other stop positions in between that we
7677 missed. Scan them all now, in buffer's logical
7678 order, until we find and handle the last stop_charpos
7679 that precedes our current position. */
7680 handle_stop_backwards (it, it->stop_charpos);
7681 return GET_NEXT_DISPLAY_ELEMENT (it);
7682 }
7683 else
7684 {
7685 if (it->bidi_p)
7686 {
7687 /* Take note of the stop position we just moved
7688 across, for when we will move back across it. */
7689 it->prev_stop = it->stop_charpos;
7690 /* If we are at base paragraph embedding level, take
7691 note of the last stop position seen at this
7692 level. */
7693 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7694 it->base_level_stop = it->stop_charpos;
7695 }
7696 handle_stop (it);
7697
7698 /* Since a handler may have changed IT->method, we must
7699 recurse here. */
7700 return GET_NEXT_DISPLAY_ELEMENT (it);
7701 }
7702 }
7703 else if (it->bidi_p
7704 /* If we are before prev_stop, we may have overstepped
7705 on our way backwards a stop_pos, and if so, we need
7706 to handle that stop_pos. */
7707 && IT_STRING_CHARPOS (*it) < it->prev_stop
7708 /* We can sometimes back up for reasons that have nothing
7709 to do with bidi reordering. E.g., compositions. The
7710 code below is only needed when we are above the base
7711 embedding level, so test for that explicitly. */
7712 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7713 {
7714 /* If we lost track of base_level_stop, we have no better
7715 place for handle_stop_backwards to start from than string
7716 beginning. This happens, e.g., when we were reseated to
7717 the previous screenful of text by vertical-motion. */
7718 if (it->base_level_stop <= 0
7719 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7720 it->base_level_stop = 0;
7721 handle_stop_backwards (it, it->base_level_stop);
7722 return GET_NEXT_DISPLAY_ELEMENT (it);
7723 }
7724 }
7725
7726 if (it->current.overlay_string_index >= 0)
7727 {
7728 /* Get the next character from an overlay string. In overlay
7729 strings, there is no field width or padding with spaces to
7730 do. */
7731 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7732 {
7733 it->what = IT_EOB;
7734 return 0;
7735 }
7736 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7737 IT_STRING_BYTEPOS (*it),
7738 it->bidi_it.scan_dir < 0
7739 ? -1
7740 : SCHARS (it->string))
7741 && next_element_from_composition (it))
7742 {
7743 return 1;
7744 }
7745 else if (STRING_MULTIBYTE (it->string))
7746 {
7747 const unsigned char *s = (SDATA (it->string)
7748 + IT_STRING_BYTEPOS (*it));
7749 it->c = string_char_and_length (s, &it->len);
7750 }
7751 else
7752 {
7753 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7754 it->len = 1;
7755 }
7756 }
7757 else
7758 {
7759 /* Get the next character from a Lisp string that is not an
7760 overlay string. Such strings come from the mode line, for
7761 example. We may have to pad with spaces, or truncate the
7762 string. See also next_element_from_c_string. */
7763 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7764 {
7765 it->what = IT_EOB;
7766 return 0;
7767 }
7768 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7769 {
7770 /* Pad with spaces. */
7771 it->c = ' ', it->len = 1;
7772 CHARPOS (position) = BYTEPOS (position) = -1;
7773 }
7774 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7775 IT_STRING_BYTEPOS (*it),
7776 it->bidi_it.scan_dir < 0
7777 ? -1
7778 : it->string_nchars)
7779 && next_element_from_composition (it))
7780 {
7781 return 1;
7782 }
7783 else if (STRING_MULTIBYTE (it->string))
7784 {
7785 const unsigned char *s = (SDATA (it->string)
7786 + IT_STRING_BYTEPOS (*it));
7787 it->c = string_char_and_length (s, &it->len);
7788 }
7789 else
7790 {
7791 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7792 it->len = 1;
7793 }
7794 }
7795
7796 /* Record what we have and where it came from. */
7797 it->what = IT_CHARACTER;
7798 it->object = it->string;
7799 it->position = position;
7800 return 1;
7801 }
7802
7803
7804 /* Load IT with next display element from C string IT->s.
7805 IT->string_nchars is the maximum number of characters to return
7806 from the string. IT->end_charpos may be greater than
7807 IT->string_nchars when this function is called, in which case we
7808 may have to return padding spaces. Value is zero if end of string
7809 reached, including padding spaces. */
7810
7811 static int
7812 next_element_from_c_string (struct it *it)
7813 {
7814 bool success_p = true;
7815
7816 eassert (it->s);
7817 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7818 it->what = IT_CHARACTER;
7819 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7820 it->object = Qnil;
7821
7822 /* With bidi reordering, the character to display might not be the
7823 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7824 we were reseated to a new string, whose paragraph direction is
7825 not known. */
7826 if (it->bidi_p && it->bidi_it.first_elt)
7827 get_visually_first_element (it);
7828
7829 /* IT's position can be greater than IT->string_nchars in case a
7830 field width or precision has been specified when the iterator was
7831 initialized. */
7832 if (IT_CHARPOS (*it) >= it->end_charpos)
7833 {
7834 /* End of the game. */
7835 it->what = IT_EOB;
7836 success_p = 0;
7837 }
7838 else if (IT_CHARPOS (*it) >= it->string_nchars)
7839 {
7840 /* Pad with spaces. */
7841 it->c = ' ', it->len = 1;
7842 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7843 }
7844 else if (it->multibyte_p)
7845 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7846 else
7847 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7848
7849 return success_p;
7850 }
7851
7852
7853 /* Set up IT to return characters from an ellipsis, if appropriate.
7854 The definition of the ellipsis glyphs may come from a display table
7855 entry. This function fills IT with the first glyph from the
7856 ellipsis if an ellipsis is to be displayed. */
7857
7858 static int
7859 next_element_from_ellipsis (struct it *it)
7860 {
7861 if (it->selective_display_ellipsis_p)
7862 setup_for_ellipsis (it, it->len);
7863 else
7864 {
7865 /* The face at the current position may be different from the
7866 face we find after the invisible text. Remember what it
7867 was in IT->saved_face_id, and signal that it's there by
7868 setting face_before_selective_p. */
7869 it->saved_face_id = it->face_id;
7870 it->method = GET_FROM_BUFFER;
7871 it->object = it->w->contents;
7872 reseat_at_next_visible_line_start (it, 1);
7873 it->face_before_selective_p = true;
7874 }
7875
7876 return GET_NEXT_DISPLAY_ELEMENT (it);
7877 }
7878
7879
7880 /* Deliver an image display element. The iterator IT is already
7881 filled with image information (done in handle_display_prop). Value
7882 is always 1. */
7883
7884
7885 static int
7886 next_element_from_image (struct it *it)
7887 {
7888 it->what = IT_IMAGE;
7889 it->ignore_overlay_strings_at_pos_p = 0;
7890 return 1;
7891 }
7892
7893
7894 /* Fill iterator IT with next display element from a stretch glyph
7895 property. IT->object is the value of the text property. Value is
7896 always 1. */
7897
7898 static int
7899 next_element_from_stretch (struct it *it)
7900 {
7901 it->what = IT_STRETCH;
7902 return 1;
7903 }
7904
7905 /* Scan backwards from IT's current position until we find a stop
7906 position, or until BEGV. This is called when we find ourself
7907 before both the last known prev_stop and base_level_stop while
7908 reordering bidirectional text. */
7909
7910 static void
7911 compute_stop_pos_backwards (struct it *it)
7912 {
7913 const int SCAN_BACK_LIMIT = 1000;
7914 struct text_pos pos;
7915 struct display_pos save_current = it->current;
7916 struct text_pos save_position = it->position;
7917 ptrdiff_t charpos = IT_CHARPOS (*it);
7918 ptrdiff_t where_we_are = charpos;
7919 ptrdiff_t save_stop_pos = it->stop_charpos;
7920 ptrdiff_t save_end_pos = it->end_charpos;
7921
7922 eassert (NILP (it->string) && !it->s);
7923 eassert (it->bidi_p);
7924 it->bidi_p = 0;
7925 do
7926 {
7927 it->end_charpos = min (charpos + 1, ZV);
7928 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7929 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7930 reseat_1 (it, pos, 0);
7931 compute_stop_pos (it);
7932 /* We must advance forward, right? */
7933 if (it->stop_charpos <= charpos)
7934 emacs_abort ();
7935 }
7936 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7937
7938 if (it->stop_charpos <= where_we_are)
7939 it->prev_stop = it->stop_charpos;
7940 else
7941 it->prev_stop = BEGV;
7942 it->bidi_p = true;
7943 it->current = save_current;
7944 it->position = save_position;
7945 it->stop_charpos = save_stop_pos;
7946 it->end_charpos = save_end_pos;
7947 }
7948
7949 /* Scan forward from CHARPOS in the current buffer/string, until we
7950 find a stop position > current IT's position. Then handle the stop
7951 position before that. This is called when we bump into a stop
7952 position while reordering bidirectional text. CHARPOS should be
7953 the last previously processed stop_pos (or BEGV/0, if none were
7954 processed yet) whose position is less that IT's current
7955 position. */
7956
7957 static void
7958 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7959 {
7960 int bufp = !STRINGP (it->string);
7961 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7962 struct display_pos save_current = it->current;
7963 struct text_pos save_position = it->position;
7964 struct text_pos pos1;
7965 ptrdiff_t next_stop;
7966
7967 /* Scan in strict logical order. */
7968 eassert (it->bidi_p);
7969 it->bidi_p = 0;
7970 do
7971 {
7972 it->prev_stop = charpos;
7973 if (bufp)
7974 {
7975 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7976 reseat_1 (it, pos1, 0);
7977 }
7978 else
7979 it->current.string_pos = string_pos (charpos, it->string);
7980 compute_stop_pos (it);
7981 /* We must advance forward, right? */
7982 if (it->stop_charpos <= it->prev_stop)
7983 emacs_abort ();
7984 charpos = it->stop_charpos;
7985 }
7986 while (charpos <= where_we_are);
7987
7988 it->bidi_p = true;
7989 it->current = save_current;
7990 it->position = save_position;
7991 next_stop = it->stop_charpos;
7992 it->stop_charpos = it->prev_stop;
7993 handle_stop (it);
7994 it->stop_charpos = next_stop;
7995 }
7996
7997 /* Load IT with the next display element from current_buffer. Value
7998 is zero if end of buffer reached. IT->stop_charpos is the next
7999 position at which to stop and check for text properties or buffer
8000 end. */
8001
8002 static int
8003 next_element_from_buffer (struct it *it)
8004 {
8005 bool success_p = true;
8006
8007 eassert (IT_CHARPOS (*it) >= BEGV);
8008 eassert (NILP (it->string) && !it->s);
8009 eassert (!it->bidi_p
8010 || (EQ (it->bidi_it.string.lstring, Qnil)
8011 && it->bidi_it.string.s == NULL));
8012
8013 /* With bidi reordering, the character to display might not be the
8014 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8015 we were reseat()ed to a new buffer position, which is potentially
8016 a different paragraph. */
8017 if (it->bidi_p && it->bidi_it.first_elt)
8018 {
8019 get_visually_first_element (it);
8020 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8021 }
8022
8023 if (IT_CHARPOS (*it) >= it->stop_charpos)
8024 {
8025 if (IT_CHARPOS (*it) >= it->end_charpos)
8026 {
8027 int overlay_strings_follow_p;
8028
8029 /* End of the game, except when overlay strings follow that
8030 haven't been returned yet. */
8031 if (it->overlay_strings_at_end_processed_p)
8032 overlay_strings_follow_p = 0;
8033 else
8034 {
8035 it->overlay_strings_at_end_processed_p = true;
8036 overlay_strings_follow_p = get_overlay_strings (it, 0);
8037 }
8038
8039 if (overlay_strings_follow_p)
8040 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8041 else
8042 {
8043 it->what = IT_EOB;
8044 it->position = it->current.pos;
8045 success_p = 0;
8046 }
8047 }
8048 else if (!(!it->bidi_p
8049 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8050 || IT_CHARPOS (*it) == it->stop_charpos))
8051 {
8052 /* With bidi non-linear iteration, we could find ourselves
8053 far beyond the last computed stop_charpos, with several
8054 other stop positions in between that we missed. Scan
8055 them all now, in buffer's logical order, until we find
8056 and handle the last stop_charpos that precedes our
8057 current position. */
8058 handle_stop_backwards (it, it->stop_charpos);
8059 return GET_NEXT_DISPLAY_ELEMENT (it);
8060 }
8061 else
8062 {
8063 if (it->bidi_p)
8064 {
8065 /* Take note of the stop position we just moved across,
8066 for when we will move back across it. */
8067 it->prev_stop = it->stop_charpos;
8068 /* If we are at base paragraph embedding level, take
8069 note of the last stop position seen at this
8070 level. */
8071 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8072 it->base_level_stop = it->stop_charpos;
8073 }
8074 handle_stop (it);
8075 return GET_NEXT_DISPLAY_ELEMENT (it);
8076 }
8077 }
8078 else if (it->bidi_p
8079 /* If we are before prev_stop, we may have overstepped on
8080 our way backwards a stop_pos, and if so, we need to
8081 handle that stop_pos. */
8082 && IT_CHARPOS (*it) < it->prev_stop
8083 /* We can sometimes back up for reasons that have nothing
8084 to do with bidi reordering. E.g., compositions. The
8085 code below is only needed when we are above the base
8086 embedding level, so test for that explicitly. */
8087 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8088 {
8089 if (it->base_level_stop <= 0
8090 || IT_CHARPOS (*it) < it->base_level_stop)
8091 {
8092 /* If we lost track of base_level_stop, we need to find
8093 prev_stop by looking backwards. This happens, e.g., when
8094 we were reseated to the previous screenful of text by
8095 vertical-motion. */
8096 it->base_level_stop = BEGV;
8097 compute_stop_pos_backwards (it);
8098 handle_stop_backwards (it, it->prev_stop);
8099 }
8100 else
8101 handle_stop_backwards (it, it->base_level_stop);
8102 return GET_NEXT_DISPLAY_ELEMENT (it);
8103 }
8104 else
8105 {
8106 /* No face changes, overlays etc. in sight, so just return a
8107 character from current_buffer. */
8108 unsigned char *p;
8109 ptrdiff_t stop;
8110
8111 /* Maybe run the redisplay end trigger hook. Performance note:
8112 This doesn't seem to cost measurable time. */
8113 if (it->redisplay_end_trigger_charpos
8114 && it->glyph_row
8115 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8116 run_redisplay_end_trigger_hook (it);
8117
8118 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8119 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8120 stop)
8121 && next_element_from_composition (it))
8122 {
8123 return 1;
8124 }
8125
8126 /* Get the next character, maybe multibyte. */
8127 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8128 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8129 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8130 else
8131 it->c = *p, it->len = 1;
8132
8133 /* Record what we have and where it came from. */
8134 it->what = IT_CHARACTER;
8135 it->object = it->w->contents;
8136 it->position = it->current.pos;
8137
8138 /* Normally we return the character found above, except when we
8139 really want to return an ellipsis for selective display. */
8140 if (it->selective)
8141 {
8142 if (it->c == '\n')
8143 {
8144 /* A value of selective > 0 means hide lines indented more
8145 than that number of columns. */
8146 if (it->selective > 0
8147 && IT_CHARPOS (*it) + 1 < ZV
8148 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8149 IT_BYTEPOS (*it) + 1,
8150 it->selective))
8151 {
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8154 }
8155 }
8156 else if (it->c == '\r' && it->selective == -1)
8157 {
8158 /* A value of selective == -1 means that everything from the
8159 CR to the end of the line is invisible, with maybe an
8160 ellipsis displayed for it. */
8161 success_p = next_element_from_ellipsis (it);
8162 it->dpvec_char_len = -1;
8163 }
8164 }
8165 }
8166
8167 /* Value is zero if end of buffer reached. */
8168 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8169 return success_p;
8170 }
8171
8172
8173 /* Run the redisplay end trigger hook for IT. */
8174
8175 static void
8176 run_redisplay_end_trigger_hook (struct it *it)
8177 {
8178 Lisp_Object args[3];
8179
8180 /* IT->glyph_row should be non-null, i.e. we should be actually
8181 displaying something, or otherwise we should not run the hook. */
8182 eassert (it->glyph_row);
8183
8184 /* Set up hook arguments. */
8185 args[0] = Qredisplay_end_trigger_functions;
8186 args[1] = it->window;
8187 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8188 it->redisplay_end_trigger_charpos = 0;
8189
8190 /* Since we are *trying* to run these functions, don't try to run
8191 them again, even if they get an error. */
8192 wset_redisplay_end_trigger (it->w, Qnil);
8193 Frun_hook_with_args (3, args);
8194
8195 /* Notice if it changed the face of the character we are on. */
8196 handle_face_prop (it);
8197 }
8198
8199
8200 /* Deliver a composition display element. Unlike the other
8201 next_element_from_XXX, this function is not registered in the array
8202 get_next_element[]. It is called from next_element_from_buffer and
8203 next_element_from_string when necessary. */
8204
8205 static int
8206 next_element_from_composition (struct it *it)
8207 {
8208 it->what = IT_COMPOSITION;
8209 it->len = it->cmp_it.nbytes;
8210 if (STRINGP (it->string))
8211 {
8212 if (it->c < 0)
8213 {
8214 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8215 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8216 return 0;
8217 }
8218 it->position = it->current.string_pos;
8219 it->object = it->string;
8220 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8221 IT_STRING_BYTEPOS (*it), it->string);
8222 }
8223 else
8224 {
8225 if (it->c < 0)
8226 {
8227 IT_CHARPOS (*it) += it->cmp_it.nchars;
8228 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8229 if (it->bidi_p)
8230 {
8231 if (it->bidi_it.new_paragraph)
8232 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8233 /* Resync the bidi iterator with IT's new position.
8234 FIXME: this doesn't support bidirectional text. */
8235 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8236 bidi_move_to_visually_next (&it->bidi_it);
8237 }
8238 return 0;
8239 }
8240 it->position = it->current.pos;
8241 it->object = it->w->contents;
8242 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8243 IT_BYTEPOS (*it), Qnil);
8244 }
8245 return 1;
8246 }
8247
8248
8249 \f
8250 /***********************************************************************
8251 Moving an iterator without producing glyphs
8252 ***********************************************************************/
8253
8254 /* Check if iterator is at a position corresponding to a valid buffer
8255 position after some move_it_ call. */
8256
8257 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8258 ((it)->method == GET_FROM_STRING \
8259 ? IT_STRING_CHARPOS (*it) == 0 \
8260 : 1)
8261
8262
8263 /* Move iterator IT to a specified buffer or X position within one
8264 line on the display without producing glyphs.
8265
8266 OP should be a bit mask including some or all of these bits:
8267 MOVE_TO_X: Stop upon reaching x-position TO_X.
8268 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8269 Regardless of OP's value, stop upon reaching the end of the display line.
8270
8271 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8272 This means, in particular, that TO_X includes window's horizontal
8273 scroll amount.
8274
8275 The return value has several possible values that
8276 say what condition caused the scan to stop:
8277
8278 MOVE_POS_MATCH_OR_ZV
8279 - when TO_POS or ZV was reached.
8280
8281 MOVE_X_REACHED
8282 -when TO_X was reached before TO_POS or ZV were reached.
8283
8284 MOVE_LINE_CONTINUED
8285 - when we reached the end of the display area and the line must
8286 be continued.
8287
8288 MOVE_LINE_TRUNCATED
8289 - when we reached the end of the display area and the line is
8290 truncated.
8291
8292 MOVE_NEWLINE_OR_CR
8293 - when we stopped at a line end, i.e. a newline or a CR and selective
8294 display is on. */
8295
8296 static enum move_it_result
8297 move_it_in_display_line_to (struct it *it,
8298 ptrdiff_t to_charpos, int to_x,
8299 enum move_operation_enum op)
8300 {
8301 enum move_it_result result = MOVE_UNDEFINED;
8302 struct glyph_row *saved_glyph_row;
8303 struct it wrap_it, atpos_it, atx_it, ppos_it;
8304 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8305 void *ppos_data = NULL;
8306 int may_wrap = 0;
8307 enum it_method prev_method = it->method;
8308 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8309 int saw_smaller_pos = prev_pos < to_charpos;
8310
8311 /* Don't produce glyphs in produce_glyphs. */
8312 saved_glyph_row = it->glyph_row;
8313 it->glyph_row = NULL;
8314
8315 /* Use wrap_it to save a copy of IT wherever a word wrap could
8316 occur. Use atpos_it to save a copy of IT at the desired buffer
8317 position, if found, so that we can scan ahead and check if the
8318 word later overshoots the window edge. Use atx_it similarly, for
8319 pixel positions. */
8320 wrap_it.sp = -1;
8321 atpos_it.sp = -1;
8322 atx_it.sp = -1;
8323
8324 /* Use ppos_it under bidi reordering to save a copy of IT for the
8325 position > CHARPOS that is the closest to CHARPOS. We restore
8326 that position in IT when we have scanned the entire display line
8327 without finding a match for CHARPOS and all the character
8328 positions are greater than CHARPOS. */
8329 if (it->bidi_p)
8330 {
8331 SAVE_IT (ppos_it, *it, ppos_data);
8332 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8333 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8334 SAVE_IT (ppos_it, *it, ppos_data);
8335 }
8336
8337 #define BUFFER_POS_REACHED_P() \
8338 ((op & MOVE_TO_POS) != 0 \
8339 && BUFFERP (it->object) \
8340 && (IT_CHARPOS (*it) == to_charpos \
8341 || ((!it->bidi_p \
8342 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8343 && IT_CHARPOS (*it) > to_charpos) \
8344 || (it->what == IT_COMPOSITION \
8345 && ((IT_CHARPOS (*it) > to_charpos \
8346 && to_charpos >= it->cmp_it.charpos) \
8347 || (IT_CHARPOS (*it) < to_charpos \
8348 && to_charpos <= it->cmp_it.charpos)))) \
8349 && (it->method == GET_FROM_BUFFER \
8350 || (it->method == GET_FROM_DISPLAY_VECTOR \
8351 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8352
8353 /* If there's a line-/wrap-prefix, handle it. */
8354 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8355 && it->current_y < it->last_visible_y)
8356 handle_line_prefix (it);
8357
8358 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8359 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8360
8361 while (1)
8362 {
8363 int x, i, ascent = 0, descent = 0;
8364
8365 /* Utility macro to reset an iterator with x, ascent, and descent. */
8366 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8367 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8368 (IT)->max_descent = descent)
8369
8370 /* Stop if we move beyond TO_CHARPOS (after an image or a
8371 display string or stretch glyph). */
8372 if ((op & MOVE_TO_POS) != 0
8373 && BUFFERP (it->object)
8374 && it->method == GET_FROM_BUFFER
8375 && (((!it->bidi_p
8376 /* When the iterator is at base embedding level, we
8377 are guaranteed that characters are delivered for
8378 display in strictly increasing order of their
8379 buffer positions. */
8380 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8381 && IT_CHARPOS (*it) > to_charpos)
8382 || (it->bidi_p
8383 && (prev_method == GET_FROM_IMAGE
8384 || prev_method == GET_FROM_STRETCH
8385 || prev_method == GET_FROM_STRING)
8386 /* Passed TO_CHARPOS from left to right. */
8387 && ((prev_pos < to_charpos
8388 && IT_CHARPOS (*it) > to_charpos)
8389 /* Passed TO_CHARPOS from right to left. */
8390 || (prev_pos > to_charpos
8391 && IT_CHARPOS (*it) < to_charpos)))))
8392 {
8393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8394 {
8395 result = MOVE_POS_MATCH_OR_ZV;
8396 break;
8397 }
8398 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8399 /* If wrap_it is valid, the current position might be in a
8400 word that is wrapped. So, save the iterator in
8401 atpos_it and continue to see if wrapping happens. */
8402 SAVE_IT (atpos_it, *it, atpos_data);
8403 }
8404
8405 /* Stop when ZV reached.
8406 We used to stop here when TO_CHARPOS reached as well, but that is
8407 too soon if this glyph does not fit on this line. So we handle it
8408 explicitly below. */
8409 if (!get_next_display_element (it))
8410 {
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 break;
8413 }
8414
8415 if (it->line_wrap == TRUNCATE)
8416 {
8417 if (BUFFER_POS_REACHED_P ())
8418 {
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8421 }
8422 }
8423 else
8424 {
8425 if (it->line_wrap == WORD_WRAP)
8426 {
8427 if (IT_DISPLAYING_WHITESPACE (it))
8428 may_wrap = 1;
8429 else if (may_wrap)
8430 {
8431 /* We have reached a glyph that follows one or more
8432 whitespace characters. If the position is
8433 already found, we are done. */
8434 if (atpos_it.sp >= 0)
8435 {
8436 RESTORE_IT (it, &atpos_it, atpos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8438 goto done;
8439 }
8440 if (atx_it.sp >= 0)
8441 {
8442 RESTORE_IT (it, &atx_it, atx_data);
8443 result = MOVE_X_REACHED;
8444 goto done;
8445 }
8446 /* Otherwise, we can wrap here. */
8447 SAVE_IT (wrap_it, *it, wrap_data);
8448 may_wrap = 0;
8449 }
8450 }
8451 }
8452
8453 /* Remember the line height for the current line, in case
8454 the next element doesn't fit on the line. */
8455 ascent = it->max_ascent;
8456 descent = it->max_descent;
8457
8458 /* The call to produce_glyphs will get the metrics of the
8459 display element IT is loaded with. Record the x-position
8460 before this display element, in case it doesn't fit on the
8461 line. */
8462 x = it->current_x;
8463
8464 PRODUCE_GLYPHS (it);
8465
8466 if (it->area != TEXT_AREA)
8467 {
8468 prev_method = it->method;
8469 if (it->method == GET_FROM_BUFFER)
8470 prev_pos = IT_CHARPOS (*it);
8471 set_iterator_to_next (it, 1);
8472 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8473 SET_TEXT_POS (this_line_min_pos,
8474 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8475 if (it->bidi_p
8476 && (op & MOVE_TO_POS)
8477 && IT_CHARPOS (*it) > to_charpos
8478 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8479 SAVE_IT (ppos_it, *it, ppos_data);
8480 continue;
8481 }
8482
8483 /* The number of glyphs we get back in IT->nglyphs will normally
8484 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8485 character on a terminal frame, or (iii) a line end. For the
8486 second case, IT->nglyphs - 1 padding glyphs will be present.
8487 (On X frames, there is only one glyph produced for a
8488 composite character.)
8489
8490 The behavior implemented below means, for continuation lines,
8491 that as many spaces of a TAB as fit on the current line are
8492 displayed there. For terminal frames, as many glyphs of a
8493 multi-glyph character are displayed in the current line, too.
8494 This is what the old redisplay code did, and we keep it that
8495 way. Under X, the whole shape of a complex character must
8496 fit on the line or it will be completely displayed in the
8497 next line.
8498
8499 Note that both for tabs and padding glyphs, all glyphs have
8500 the same width. */
8501 if (it->nglyphs)
8502 {
8503 /* More than one glyph or glyph doesn't fit on line. All
8504 glyphs have the same width. */
8505 int single_glyph_width = it->pixel_width / it->nglyphs;
8506 int new_x;
8507 int x_before_this_char = x;
8508 int hpos_before_this_char = it->hpos;
8509
8510 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8511 {
8512 new_x = x + single_glyph_width;
8513
8514 /* We want to leave anything reaching TO_X to the caller. */
8515 if ((op & MOVE_TO_X) && new_x > to_x)
8516 {
8517 if (BUFFER_POS_REACHED_P ())
8518 {
8519 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8520 goto buffer_pos_reached;
8521 if (atpos_it.sp < 0)
8522 {
8523 SAVE_IT (atpos_it, *it, atpos_data);
8524 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8525 }
8526 }
8527 else
8528 {
8529 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8530 {
8531 it->current_x = x;
8532 result = MOVE_X_REACHED;
8533 break;
8534 }
8535 if (atx_it.sp < 0)
8536 {
8537 SAVE_IT (atx_it, *it, atx_data);
8538 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8539 }
8540 }
8541 }
8542
8543 if (/* Lines are continued. */
8544 it->line_wrap != TRUNCATE
8545 && (/* And glyph doesn't fit on the line. */
8546 new_x > it->last_visible_x
8547 /* Or it fits exactly and we're on a window
8548 system frame. */
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)
8551 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8552 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8553 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8554 {
8555 if (/* IT->hpos == 0 means the very first glyph
8556 doesn't fit on the line, e.g. a wide image. */
8557 it->hpos == 0
8558 || (new_x == it->last_visible_x
8559 && FRAME_WINDOW_P (it->f)))
8560 {
8561 ++it->hpos;
8562 it->current_x = new_x;
8563
8564 /* The character's last glyph just barely fits
8565 in this row. */
8566 if (i == it->nglyphs - 1)
8567 {
8568 /* If this is the destination position,
8569 return a position *before* it in this row,
8570 now that we know it fits in this row. */
8571 if (BUFFER_POS_REACHED_P ())
8572 {
8573 if (it->line_wrap != WORD_WRAP
8574 || wrap_it.sp < 0)
8575 {
8576 it->hpos = hpos_before_this_char;
8577 it->current_x = x_before_this_char;
8578 result = MOVE_POS_MATCH_OR_ZV;
8579 break;
8580 }
8581 if (it->line_wrap == WORD_WRAP
8582 && atpos_it.sp < 0)
8583 {
8584 SAVE_IT (atpos_it, *it, atpos_data);
8585 atpos_it.current_x = x_before_this_char;
8586 atpos_it.hpos = hpos_before_this_char;
8587 }
8588 }
8589
8590 prev_method = it->method;
8591 if (it->method == GET_FROM_BUFFER)
8592 prev_pos = IT_CHARPOS (*it);
8593 set_iterator_to_next (it, 1);
8594 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8595 SET_TEXT_POS (this_line_min_pos,
8596 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8597 /* On graphical terminals, newlines may
8598 "overflow" into the fringe if
8599 overflow-newline-into-fringe is non-nil.
8600 On text terminals, and on graphical
8601 terminals with no right margin, newlines
8602 may overflow into the last glyph on the
8603 display line.*/
8604 if (!FRAME_WINDOW_P (it->f)
8605 || ((it->bidi_p
8606 && it->bidi_it.paragraph_dir == R2L)
8607 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8608 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8609 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8610 {
8611 if (!get_next_display_element (it))
8612 {
8613 result = MOVE_POS_MATCH_OR_ZV;
8614 break;
8615 }
8616 if (BUFFER_POS_REACHED_P ())
8617 {
8618 if (ITERATOR_AT_END_OF_LINE_P (it))
8619 result = MOVE_POS_MATCH_OR_ZV;
8620 else
8621 result = MOVE_LINE_CONTINUED;
8622 break;
8623 }
8624 if (ITERATOR_AT_END_OF_LINE_P (it)
8625 && (it->line_wrap != WORD_WRAP
8626 || wrap_it.sp < 0))
8627 {
8628 result = MOVE_NEWLINE_OR_CR;
8629 break;
8630 }
8631 }
8632 }
8633 }
8634 else
8635 IT_RESET_X_ASCENT_DESCENT (it);
8636
8637 if (wrap_it.sp >= 0)
8638 {
8639 RESTORE_IT (it, &wrap_it, wrap_data);
8640 atpos_it.sp = -1;
8641 atx_it.sp = -1;
8642 }
8643
8644 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8645 IT_CHARPOS (*it)));
8646 result = MOVE_LINE_CONTINUED;
8647 break;
8648 }
8649
8650 if (BUFFER_POS_REACHED_P ())
8651 {
8652 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8653 goto buffer_pos_reached;
8654 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8655 {
8656 SAVE_IT (atpos_it, *it, atpos_data);
8657 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8658 }
8659 }
8660
8661 if (new_x > it->first_visible_x)
8662 {
8663 /* Glyph is visible. Increment number of glyphs that
8664 would be displayed. */
8665 ++it->hpos;
8666 }
8667 }
8668
8669 if (result != MOVE_UNDEFINED)
8670 break;
8671 }
8672 else if (BUFFER_POS_REACHED_P ())
8673 {
8674 buffer_pos_reached:
8675 IT_RESET_X_ASCENT_DESCENT (it);
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8678 }
8679 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8680 {
8681 /* Stop when TO_X specified and reached. This check is
8682 necessary here because of lines consisting of a line end,
8683 only. The line end will not produce any glyphs and we
8684 would never get MOVE_X_REACHED. */
8685 eassert (it->nglyphs == 0);
8686 result = MOVE_X_REACHED;
8687 break;
8688 }
8689
8690 /* Is this a line end? If yes, we're done. */
8691 if (ITERATOR_AT_END_OF_LINE_P (it))
8692 {
8693 /* If we are past TO_CHARPOS, but never saw any character
8694 positions smaller than TO_CHARPOS, return
8695 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8696 did. */
8697 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8698 {
8699 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8700 {
8701 if (IT_CHARPOS (ppos_it) < ZV)
8702 {
8703 RESTORE_IT (it, &ppos_it, ppos_data);
8704 result = MOVE_POS_MATCH_OR_ZV;
8705 }
8706 else
8707 goto buffer_pos_reached;
8708 }
8709 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8710 && IT_CHARPOS (*it) > to_charpos)
8711 goto buffer_pos_reached;
8712 else
8713 result = MOVE_NEWLINE_OR_CR;
8714 }
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8717 break;
8718 }
8719
8720 prev_method = it->method;
8721 if (it->method == GET_FROM_BUFFER)
8722 prev_pos = IT_CHARPOS (*it);
8723 /* The current display element has been consumed. Advance
8724 to the next. */
8725 set_iterator_to_next (it, 1);
8726 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8727 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8728 if (IT_CHARPOS (*it) < to_charpos)
8729 saw_smaller_pos = 1;
8730 if (it->bidi_p
8731 && (op & MOVE_TO_POS)
8732 && IT_CHARPOS (*it) >= to_charpos
8733 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8734 SAVE_IT (ppos_it, *it, ppos_data);
8735
8736 /* Stop if lines are truncated and IT's current x-position is
8737 past the right edge of the window now. */
8738 if (it->line_wrap == TRUNCATE
8739 && it->current_x >= it->last_visible_x)
8740 {
8741 if (!FRAME_WINDOW_P (it->f)
8742 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8743 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8744 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8745 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8746 {
8747 int at_eob_p = 0;
8748
8749 if ((at_eob_p = !get_next_display_element (it))
8750 || BUFFER_POS_REACHED_P ()
8751 /* If we are past TO_CHARPOS, but never saw any
8752 character positions smaller than TO_CHARPOS,
8753 return MOVE_POS_MATCH_OR_ZV, like the
8754 unidirectional display did. */
8755 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8756 && !saw_smaller_pos
8757 && IT_CHARPOS (*it) > to_charpos))
8758 {
8759 if (it->bidi_p
8760 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8761 RESTORE_IT (it, &ppos_it, ppos_data);
8762 result = MOVE_POS_MATCH_OR_ZV;
8763 break;
8764 }
8765 if (ITERATOR_AT_END_OF_LINE_P (it))
8766 {
8767 result = MOVE_NEWLINE_OR_CR;
8768 break;
8769 }
8770 }
8771 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8772 && !saw_smaller_pos
8773 && IT_CHARPOS (*it) > to_charpos)
8774 {
8775 if (IT_CHARPOS (ppos_it) < ZV)
8776 RESTORE_IT (it, &ppos_it, ppos_data);
8777 result = MOVE_POS_MATCH_OR_ZV;
8778 break;
8779 }
8780 result = MOVE_LINE_TRUNCATED;
8781 break;
8782 }
8783 #undef IT_RESET_X_ASCENT_DESCENT
8784 }
8785
8786 #undef BUFFER_POS_REACHED_P
8787
8788 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8789 restore the saved iterator. */
8790 if (atpos_it.sp >= 0)
8791 RESTORE_IT (it, &atpos_it, atpos_data);
8792 else if (atx_it.sp >= 0)
8793 RESTORE_IT (it, &atx_it, atx_data);
8794
8795 done:
8796
8797 if (atpos_data)
8798 bidi_unshelve_cache (atpos_data, 1);
8799 if (atx_data)
8800 bidi_unshelve_cache (atx_data, 1);
8801 if (wrap_data)
8802 bidi_unshelve_cache (wrap_data, 1);
8803 if (ppos_data)
8804 bidi_unshelve_cache (ppos_data, 1);
8805
8806 /* Restore the iterator settings altered at the beginning of this
8807 function. */
8808 it->glyph_row = saved_glyph_row;
8809 return result;
8810 }
8811
8812 /* For external use. */
8813 void
8814 move_it_in_display_line (struct it *it,
8815 ptrdiff_t to_charpos, int to_x,
8816 enum move_operation_enum op)
8817 {
8818 if (it->line_wrap == WORD_WRAP
8819 && (op & MOVE_TO_X))
8820 {
8821 struct it save_it;
8822 void *save_data = NULL;
8823 int skip;
8824
8825 SAVE_IT (save_it, *it, save_data);
8826 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8827 /* When word-wrap is on, TO_X may lie past the end
8828 of a wrapped line. Then it->current is the
8829 character on the next line, so backtrack to the
8830 space before the wrap point. */
8831 if (skip == MOVE_LINE_CONTINUED)
8832 {
8833 int prev_x = max (it->current_x - 1, 0);
8834 RESTORE_IT (it, &save_it, save_data);
8835 move_it_in_display_line_to
8836 (it, -1, prev_x, MOVE_TO_X);
8837 }
8838 else
8839 bidi_unshelve_cache (save_data, 1);
8840 }
8841 else
8842 move_it_in_display_line_to (it, to_charpos, to_x, op);
8843 }
8844
8845
8846 /* Move IT forward until it satisfies one or more of the criteria in
8847 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8848
8849 OP is a bit-mask that specifies where to stop, and in particular,
8850 which of those four position arguments makes a difference. See the
8851 description of enum move_operation_enum.
8852
8853 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8854 screen line, this function will set IT to the next position that is
8855 displayed to the right of TO_CHARPOS on the screen.
8856
8857 Return the maximum pixel length of any line scanned but never more
8858 than it.last_visible_x. */
8859
8860 int
8861 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8862 {
8863 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8864 int line_height, line_start_x = 0, reached = 0;
8865 int max_current_x = 0;
8866 void *backup_data = NULL;
8867
8868 for (;;)
8869 {
8870 if (op & MOVE_TO_VPOS)
8871 {
8872 /* If no TO_CHARPOS and no TO_X specified, stop at the
8873 start of the line TO_VPOS. */
8874 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8875 {
8876 if (it->vpos == to_vpos)
8877 {
8878 reached = 1;
8879 break;
8880 }
8881 else
8882 skip = move_it_in_display_line_to (it, -1, -1, 0);
8883 }
8884 else
8885 {
8886 /* TO_VPOS >= 0 means stop at TO_X in the line at
8887 TO_VPOS, or at TO_POS, whichever comes first. */
8888 if (it->vpos == to_vpos)
8889 {
8890 reached = 2;
8891 break;
8892 }
8893
8894 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8895
8896 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8897 {
8898 reached = 3;
8899 break;
8900 }
8901 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8902 {
8903 /* We have reached TO_X but not in the line we want. */
8904 skip = move_it_in_display_line_to (it, to_charpos,
8905 -1, MOVE_TO_POS);
8906 if (skip == MOVE_POS_MATCH_OR_ZV)
8907 {
8908 reached = 4;
8909 break;
8910 }
8911 }
8912 }
8913 }
8914 else if (op & MOVE_TO_Y)
8915 {
8916 struct it it_backup;
8917
8918 if (it->line_wrap == WORD_WRAP)
8919 SAVE_IT (it_backup, *it, backup_data);
8920
8921 /* TO_Y specified means stop at TO_X in the line containing
8922 TO_Y---or at TO_CHARPOS if this is reached first. The
8923 problem is that we can't really tell whether the line
8924 contains TO_Y before we have completely scanned it, and
8925 this may skip past TO_X. What we do is to first scan to
8926 TO_X.
8927
8928 If TO_X is not specified, use a TO_X of zero. The reason
8929 is to make the outcome of this function more predictable.
8930 If we didn't use TO_X == 0, we would stop at the end of
8931 the line which is probably not what a caller would expect
8932 to happen. */
8933 skip = move_it_in_display_line_to
8934 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8935 (MOVE_TO_X | (op & MOVE_TO_POS)));
8936
8937 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8938 if (skip == MOVE_POS_MATCH_OR_ZV)
8939 reached = 5;
8940 else if (skip == MOVE_X_REACHED)
8941 {
8942 /* If TO_X was reached, we want to know whether TO_Y is
8943 in the line. We know this is the case if the already
8944 scanned glyphs make the line tall enough. Otherwise,
8945 we must check by scanning the rest of the line. */
8946 line_height = it->max_ascent + it->max_descent;
8947 if (to_y >= it->current_y
8948 && to_y < it->current_y + line_height)
8949 {
8950 reached = 6;
8951 break;
8952 }
8953 SAVE_IT (it_backup, *it, backup_data);
8954 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8955 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8956 op & MOVE_TO_POS);
8957 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8958 line_height = it->max_ascent + it->max_descent;
8959 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8960
8961 if (to_y >= it->current_y
8962 && to_y < it->current_y + line_height)
8963 {
8964 /* If TO_Y is in this line and TO_X was reached
8965 above, we scanned too far. We have to restore
8966 IT's settings to the ones before skipping. But
8967 keep the more accurate values of max_ascent and
8968 max_descent we've found while skipping the rest
8969 of the line, for the sake of callers, such as
8970 pos_visible_p, that need to know the line
8971 height. */
8972 int max_ascent = it->max_ascent;
8973 int max_descent = it->max_descent;
8974
8975 RESTORE_IT (it, &it_backup, backup_data);
8976 it->max_ascent = max_ascent;
8977 it->max_descent = max_descent;
8978 reached = 6;
8979 }
8980 else
8981 {
8982 skip = skip2;
8983 if (skip == MOVE_POS_MATCH_OR_ZV)
8984 reached = 7;
8985 }
8986 }
8987 else
8988 {
8989 /* Check whether TO_Y is in this line. */
8990 line_height = it->max_ascent + it->max_descent;
8991 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8992
8993 if (to_y >= it->current_y
8994 && to_y < it->current_y + line_height)
8995 {
8996 if (to_y > it->current_y)
8997 max_current_x = max (it->current_x, max_current_x);
8998
8999 /* When word-wrap is on, TO_X may lie past the end
9000 of a wrapped line. Then it->current is the
9001 character on the next line, so backtrack to the
9002 space before the wrap point. */
9003 if (skip == MOVE_LINE_CONTINUED
9004 && it->line_wrap == WORD_WRAP)
9005 {
9006 int prev_x = max (it->current_x - 1, 0);
9007 RESTORE_IT (it, &it_backup, backup_data);
9008 skip = move_it_in_display_line_to
9009 (it, -1, prev_x, MOVE_TO_X);
9010 }
9011
9012 reached = 6;
9013 }
9014 }
9015
9016 if (reached)
9017 {
9018 max_current_x = max (it->current_x, max_current_x);
9019 break;
9020 }
9021 }
9022 else if (BUFFERP (it->object)
9023 && (it->method == GET_FROM_BUFFER
9024 || it->method == GET_FROM_STRETCH)
9025 && IT_CHARPOS (*it) >= to_charpos
9026 /* Under bidi iteration, a call to set_iterator_to_next
9027 can scan far beyond to_charpos if the initial
9028 portion of the next line needs to be reordered. In
9029 that case, give move_it_in_display_line_to another
9030 chance below. */
9031 && !(it->bidi_p
9032 && it->bidi_it.scan_dir == -1))
9033 skip = MOVE_POS_MATCH_OR_ZV;
9034 else
9035 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9036
9037 switch (skip)
9038 {
9039 case MOVE_POS_MATCH_OR_ZV:
9040 max_current_x = max (it->current_x, max_current_x);
9041 reached = 8;
9042 goto out;
9043
9044 case MOVE_NEWLINE_OR_CR:
9045 max_current_x = max (it->current_x, max_current_x);
9046 set_iterator_to_next (it, 1);
9047 it->continuation_lines_width = 0;
9048 break;
9049
9050 case MOVE_LINE_TRUNCATED:
9051 max_current_x = it->last_visible_x;
9052 it->continuation_lines_width = 0;
9053 reseat_at_next_visible_line_start (it, 0);
9054 if ((op & MOVE_TO_POS) != 0
9055 && IT_CHARPOS (*it) > to_charpos)
9056 {
9057 reached = 9;
9058 goto out;
9059 }
9060 break;
9061
9062 case MOVE_LINE_CONTINUED:
9063 max_current_x = it->last_visible_x;
9064 /* For continued lines ending in a tab, some of the glyphs
9065 associated with the tab are displayed on the current
9066 line. Since it->current_x does not include these glyphs,
9067 we use it->last_visible_x instead. */
9068 if (it->c == '\t')
9069 {
9070 it->continuation_lines_width += it->last_visible_x;
9071 /* When moving by vpos, ensure that the iterator really
9072 advances to the next line (bug#847, bug#969). Fixme:
9073 do we need to do this in other circumstances? */
9074 if (it->current_x != it->last_visible_x
9075 && (op & MOVE_TO_VPOS)
9076 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9077 {
9078 line_start_x = it->current_x + it->pixel_width
9079 - it->last_visible_x;
9080 set_iterator_to_next (it, 0);
9081 }
9082 }
9083 else
9084 it->continuation_lines_width += it->current_x;
9085 break;
9086
9087 default:
9088 emacs_abort ();
9089 }
9090
9091 /* Reset/increment for the next run. */
9092 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9093 it->current_x = line_start_x;
9094 line_start_x = 0;
9095 it->hpos = 0;
9096 it->current_y += it->max_ascent + it->max_descent;
9097 ++it->vpos;
9098 last_height = it->max_ascent + it->max_descent;
9099 last_max_ascent = it->max_ascent;
9100 it->max_ascent = it->max_descent = 0;
9101 }
9102
9103 out:
9104
9105 /* On text terminals, we may stop at the end of a line in the middle
9106 of a multi-character glyph. If the glyph itself is continued,
9107 i.e. it is actually displayed on the next line, don't treat this
9108 stopping point as valid; move to the next line instead (unless
9109 that brings us offscreen). */
9110 if (!FRAME_WINDOW_P (it->f)
9111 && op & MOVE_TO_POS
9112 && IT_CHARPOS (*it) == to_charpos
9113 && it->what == IT_CHARACTER
9114 && it->nglyphs > 1
9115 && it->line_wrap == WINDOW_WRAP
9116 && it->current_x == it->last_visible_x - 1
9117 && it->c != '\n'
9118 && it->c != '\t'
9119 && it->vpos < it->w->window_end_vpos)
9120 {
9121 it->continuation_lines_width += it->current_x;
9122 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9123 it->current_y += it->max_ascent + it->max_descent;
9124 ++it->vpos;
9125 last_height = it->max_ascent + it->max_descent;
9126 last_max_ascent = it->max_ascent;
9127 }
9128
9129 if (backup_data)
9130 bidi_unshelve_cache (backup_data, 1);
9131
9132 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9133
9134 return max_current_x;
9135 }
9136
9137
9138 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9139
9140 If DY > 0, move IT backward at least that many pixels. DY = 0
9141 means move IT backward to the preceding line start or BEGV. This
9142 function may move over more than DY pixels if IT->current_y - DY
9143 ends up in the middle of a line; in this case IT->current_y will be
9144 set to the top of the line moved to. */
9145
9146 void
9147 move_it_vertically_backward (struct it *it, int dy)
9148 {
9149 int nlines, h;
9150 struct it it2, it3;
9151 void *it2data = NULL, *it3data = NULL;
9152 ptrdiff_t start_pos;
9153 int nchars_per_row
9154 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9155 ptrdiff_t pos_limit;
9156
9157 move_further_back:
9158 eassert (dy >= 0);
9159
9160 start_pos = IT_CHARPOS (*it);
9161
9162 /* Estimate how many newlines we must move back. */
9163 nlines = max (1, dy / default_line_pixel_height (it->w));
9164 if (it->line_wrap == TRUNCATE)
9165 pos_limit = BEGV;
9166 else
9167 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9168
9169 /* Set the iterator's position that many lines back. But don't go
9170 back more than NLINES full screen lines -- this wins a day with
9171 buffers which have very long lines. */
9172 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9173 back_to_previous_visible_line_start (it);
9174
9175 /* Reseat the iterator here. When moving backward, we don't want
9176 reseat to skip forward over invisible text, set up the iterator
9177 to deliver from overlay strings at the new position etc. So,
9178 use reseat_1 here. */
9179 reseat_1 (it, it->current.pos, 1);
9180
9181 /* We are now surely at a line start. */
9182 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9183 reordering is in effect. */
9184 it->continuation_lines_width = 0;
9185
9186 /* Move forward and see what y-distance we moved. First move to the
9187 start of the next line so that we get its height. We need this
9188 height to be able to tell whether we reached the specified
9189 y-distance. */
9190 SAVE_IT (it2, *it, it2data);
9191 it2.max_ascent = it2.max_descent = 0;
9192 do
9193 {
9194 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9195 MOVE_TO_POS | MOVE_TO_VPOS);
9196 }
9197 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9198 /* If we are in a display string which starts at START_POS,
9199 and that display string includes a newline, and we are
9200 right after that newline (i.e. at the beginning of a
9201 display line), exit the loop, because otherwise we will
9202 infloop, since move_it_to will see that it is already at
9203 START_POS and will not move. */
9204 || (it2.method == GET_FROM_STRING
9205 && IT_CHARPOS (it2) == start_pos
9206 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9207 eassert (IT_CHARPOS (*it) >= BEGV);
9208 SAVE_IT (it3, it2, it3data);
9209
9210 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9211 eassert (IT_CHARPOS (*it) >= BEGV);
9212 /* H is the actual vertical distance from the position in *IT
9213 and the starting position. */
9214 h = it2.current_y - it->current_y;
9215 /* NLINES is the distance in number of lines. */
9216 nlines = it2.vpos - it->vpos;
9217
9218 /* Correct IT's y and vpos position
9219 so that they are relative to the starting point. */
9220 it->vpos -= nlines;
9221 it->current_y -= h;
9222
9223 if (dy == 0)
9224 {
9225 /* DY == 0 means move to the start of the screen line. The
9226 value of nlines is > 0 if continuation lines were involved,
9227 or if the original IT position was at start of a line. */
9228 RESTORE_IT (it, it, it2data);
9229 if (nlines > 0)
9230 move_it_by_lines (it, nlines);
9231 /* The above code moves us to some position NLINES down,
9232 usually to its first glyph (leftmost in an L2R line), but
9233 that's not necessarily the start of the line, under bidi
9234 reordering. We want to get to the character position
9235 that is immediately after the newline of the previous
9236 line. */
9237 if (it->bidi_p
9238 && !it->continuation_lines_width
9239 && !STRINGP (it->string)
9240 && IT_CHARPOS (*it) > BEGV
9241 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9242 {
9243 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9244
9245 DEC_BOTH (cp, bp);
9246 cp = find_newline_no_quit (cp, bp, -1, NULL);
9247 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9248 }
9249 bidi_unshelve_cache (it3data, 1);
9250 }
9251 else
9252 {
9253 /* The y-position we try to reach, relative to *IT.
9254 Note that H has been subtracted in front of the if-statement. */
9255 int target_y = it->current_y + h - dy;
9256 int y0 = it3.current_y;
9257 int y1;
9258 int line_height;
9259
9260 RESTORE_IT (&it3, &it3, it3data);
9261 y1 = line_bottom_y (&it3);
9262 line_height = y1 - y0;
9263 RESTORE_IT (it, it, it2data);
9264 /* If we did not reach target_y, try to move further backward if
9265 we can. If we moved too far backward, try to move forward. */
9266 if (target_y < it->current_y
9267 /* This is heuristic. In a window that's 3 lines high, with
9268 a line height of 13 pixels each, recentering with point
9269 on the bottom line will try to move -39/2 = 19 pixels
9270 backward. Try to avoid moving into the first line. */
9271 && (it->current_y - target_y
9272 > min (window_box_height (it->w), line_height * 2 / 3))
9273 && IT_CHARPOS (*it) > BEGV)
9274 {
9275 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9276 target_y - it->current_y));
9277 dy = it->current_y - target_y;
9278 goto move_further_back;
9279 }
9280 else if (target_y >= it->current_y + line_height
9281 && IT_CHARPOS (*it) < ZV)
9282 {
9283 /* Should move forward by at least one line, maybe more.
9284
9285 Note: Calling move_it_by_lines can be expensive on
9286 terminal frames, where compute_motion is used (via
9287 vmotion) to do the job, when there are very long lines
9288 and truncate-lines is nil. That's the reason for
9289 treating terminal frames specially here. */
9290
9291 if (!FRAME_WINDOW_P (it->f))
9292 move_it_vertically (it, target_y - (it->current_y + line_height));
9293 else
9294 {
9295 do
9296 {
9297 move_it_by_lines (it, 1);
9298 }
9299 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9300 }
9301 }
9302 }
9303 }
9304
9305
9306 /* Move IT by a specified amount of pixel lines DY. DY negative means
9307 move backwards. DY = 0 means move to start of screen line. At the
9308 end, IT will be on the start of a screen line. */
9309
9310 void
9311 move_it_vertically (struct it *it, int dy)
9312 {
9313 if (dy <= 0)
9314 move_it_vertically_backward (it, -dy);
9315 else
9316 {
9317 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9318 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9319 MOVE_TO_POS | MOVE_TO_Y);
9320 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9321
9322 /* If buffer ends in ZV without a newline, move to the start of
9323 the line to satisfy the post-condition. */
9324 if (IT_CHARPOS (*it) == ZV
9325 && ZV > BEGV
9326 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9327 move_it_by_lines (it, 0);
9328 }
9329 }
9330
9331
9332 /* Move iterator IT past the end of the text line it is in. */
9333
9334 void
9335 move_it_past_eol (struct it *it)
9336 {
9337 enum move_it_result rc;
9338
9339 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9340 if (rc == MOVE_NEWLINE_OR_CR)
9341 set_iterator_to_next (it, 0);
9342 }
9343
9344
9345 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9346 negative means move up. DVPOS == 0 means move to the start of the
9347 screen line.
9348
9349 Optimization idea: If we would know that IT->f doesn't use
9350 a face with proportional font, we could be faster for
9351 truncate-lines nil. */
9352
9353 void
9354 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9355 {
9356
9357 /* The commented-out optimization uses vmotion on terminals. This
9358 gives bad results, because elements like it->what, on which
9359 callers such as pos_visible_p rely, aren't updated. */
9360 /* struct position pos;
9361 if (!FRAME_WINDOW_P (it->f))
9362 {
9363 struct text_pos textpos;
9364
9365 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9366 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9367 reseat (it, textpos, 1);
9368 it->vpos += pos.vpos;
9369 it->current_y += pos.vpos;
9370 }
9371 else */
9372
9373 if (dvpos == 0)
9374 {
9375 /* DVPOS == 0 means move to the start of the screen line. */
9376 move_it_vertically_backward (it, 0);
9377 /* Let next call to line_bottom_y calculate real line height. */
9378 last_height = 0;
9379 }
9380 else if (dvpos > 0)
9381 {
9382 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9383 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9384 {
9385 /* Only move to the next buffer position if we ended up in a
9386 string from display property, not in an overlay string
9387 (before-string or after-string). That is because the
9388 latter don't conceal the underlying buffer position, so
9389 we can ask to move the iterator to the exact position we
9390 are interested in. Note that, even if we are already at
9391 IT_CHARPOS (*it), the call below is not a no-op, as it
9392 will detect that we are at the end of the string, pop the
9393 iterator, and compute it->current_x and it->hpos
9394 correctly. */
9395 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9396 -1, -1, -1, MOVE_TO_POS);
9397 }
9398 }
9399 else
9400 {
9401 struct it it2;
9402 void *it2data = NULL;
9403 ptrdiff_t start_charpos, i;
9404 int nchars_per_row
9405 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9406 ptrdiff_t pos_limit;
9407
9408 /* Start at the beginning of the screen line containing IT's
9409 position. This may actually move vertically backwards,
9410 in case of overlays, so adjust dvpos accordingly. */
9411 dvpos += it->vpos;
9412 move_it_vertically_backward (it, 0);
9413 dvpos -= it->vpos;
9414
9415 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9416 screen lines, and reseat the iterator there. */
9417 start_charpos = IT_CHARPOS (*it);
9418 if (it->line_wrap == TRUNCATE)
9419 pos_limit = BEGV;
9420 else
9421 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9422 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9423 back_to_previous_visible_line_start (it);
9424 reseat (it, it->current.pos, 1);
9425
9426 /* Move further back if we end up in a string or an image. */
9427 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9428 {
9429 /* First try to move to start of display line. */
9430 dvpos += it->vpos;
9431 move_it_vertically_backward (it, 0);
9432 dvpos -= it->vpos;
9433 if (IT_POS_VALID_AFTER_MOVE_P (it))
9434 break;
9435 /* If start of line is still in string or image,
9436 move further back. */
9437 back_to_previous_visible_line_start (it);
9438 reseat (it, it->current.pos, 1);
9439 dvpos--;
9440 }
9441
9442 it->current_x = it->hpos = 0;
9443
9444 /* Above call may have moved too far if continuation lines
9445 are involved. Scan forward and see if it did. */
9446 SAVE_IT (it2, *it, it2data);
9447 it2.vpos = it2.current_y = 0;
9448 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9449 it->vpos -= it2.vpos;
9450 it->current_y -= it2.current_y;
9451 it->current_x = it->hpos = 0;
9452
9453 /* If we moved too far back, move IT some lines forward. */
9454 if (it2.vpos > -dvpos)
9455 {
9456 int delta = it2.vpos + dvpos;
9457
9458 RESTORE_IT (&it2, &it2, it2data);
9459 SAVE_IT (it2, *it, it2data);
9460 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9461 /* Move back again if we got too far ahead. */
9462 if (IT_CHARPOS (*it) >= start_charpos)
9463 RESTORE_IT (it, &it2, it2data);
9464 else
9465 bidi_unshelve_cache (it2data, 1);
9466 }
9467 else
9468 RESTORE_IT (it, it, it2data);
9469 }
9470 }
9471
9472 /* Return 1 if IT points into the middle of a display vector. */
9473
9474 int
9475 in_display_vector_p (struct it *it)
9476 {
9477 return (it->method == GET_FROM_DISPLAY_VECTOR
9478 && it->current.dpvec_index > 0
9479 && it->dpvec + it->current.dpvec_index != it->dpend);
9480 }
9481
9482 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9483 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9484 WINDOW must be a live window and defaults to the selected one. The
9485 return value is a cons of the maximum pixel-width of any text line and
9486 the maximum pixel-height of all text lines.
9487
9488 The optional argument FROM, if non-nil, specifies the first text
9489 position and defaults to the minimum accessible position of the buffer.
9490 If FROM is t, use the minimum accessible position that is not a newline
9491 character. TO, if non-nil, specifies the last text position and
9492 defaults to the maximum accessible position of the buffer. If TO is t,
9493 use the maximum accessible position that is not a newline character.
9494
9495 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9496 width that can be returned. X_LIMIT nil or omitted, means to use the
9497 pixel-width of WINDOW's body; use this if you do not intend to change
9498 the width of WINDOW. Use the maximum width WINDOW may assume if you
9499 intend to change WINDOW's width.
9500
9501 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9502 height that can be returned. Text lines whose y-coordinate is beyond
9503 Y_LIMIT are ignored. Since calculating the text height of a large
9504 buffer can take some time, it makes sense to specify this argument if
9505 the size of the buffer is unknown.
9506
9507 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9508 include the height of the mode- or header-line of WINDOW in the return
9509 value. If it is either the symbol `mode-line' or `header-line', include
9510 only the height of that line, if present, in the return value. If t,
9511 include the height of any of these lines in the return value. */)
9512 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9513 Lisp_Object mode_and_header_line)
9514 {
9515 struct window *w = decode_live_window (window);
9516 Lisp_Object buf;
9517 struct buffer *b;
9518 struct it it;
9519 struct buffer *old_buffer = NULL;
9520 ptrdiff_t start, end, pos;
9521 struct text_pos startp;
9522 void *itdata = NULL;
9523 int c, max_y = -1, x = 0, y = 0;
9524
9525 buf = w->contents;
9526 CHECK_BUFFER (buf);
9527 b = XBUFFER (buf);
9528
9529 if (b != current_buffer)
9530 {
9531 old_buffer = current_buffer;
9532 set_buffer_internal (b);
9533 }
9534
9535 if (NILP (from))
9536 start = BEGV;
9537 else if (EQ (from, Qt))
9538 {
9539 start = pos = BEGV;
9540 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9541 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9542 start = pos;
9543 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9544 start = pos;
9545 }
9546 else
9547 {
9548 CHECK_NUMBER_COERCE_MARKER (from);
9549 start = min (max (XINT (from), BEGV), ZV);
9550 }
9551
9552 if (NILP (to))
9553 end = ZV;
9554 else if (EQ (to, Qt))
9555 {
9556 end = pos = ZV;
9557 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9558 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9559 end = pos;
9560 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9561 end = pos;
9562 }
9563 else
9564 {
9565 CHECK_NUMBER_COERCE_MARKER (to);
9566 end = max (start, min (XINT (to), ZV));
9567 }
9568
9569 if (!NILP (y_limit))
9570 {
9571 CHECK_NUMBER (y_limit);
9572 max_y = min (XINT (y_limit), INT_MAX);
9573 }
9574
9575 itdata = bidi_shelve_cache ();
9576 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9577 start_display (&it, w, startp);
9578
9579 /** move_it_vertically_backward (&it, 0); **/
9580 if (NILP (x_limit))
9581 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9582 else
9583 {
9584 CHECK_NUMBER (x_limit);
9585 it.last_visible_x = min (XINT (x_limit), INFINITY);
9586 /* Actually, we never want move_it_to stop at to_x. But to make
9587 sure that move_it_in_display_line_to always moves far enough,
9588 we set it to INT_MAX and specify MOVE_TO_X. */
9589 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9591 }
9592
9593 if (start == end)
9594 y = it.current_y;
9595 else
9596 {
9597 /* Count last line. */
9598 last_height = 0;
9599 y = line_bottom_y (&it); /* - y; */
9600 }
9601
9602 if (!EQ (mode_and_header_line, Qheader_line)
9603 && !EQ (mode_and_header_line, Qt))
9604 /* Do not count the header-line which was counted automatically by
9605 start_display. */
9606 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9607
9608 if (EQ (mode_and_header_line, Qmode_line)
9609 || EQ (mode_and_header_line, Qt))
9610 /* Do count the mode-line which is not included automatically by
9611 start_display. */
9612 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9613
9614 bidi_unshelve_cache (itdata, 0);
9615
9616 if (old_buffer)
9617 set_buffer_internal (old_buffer);
9618
9619 return Fcons (make_number (x), make_number (y));
9620 }
9621 \f
9622 /***********************************************************************
9623 Messages
9624 ***********************************************************************/
9625
9626
9627 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9628 to *Messages*. */
9629
9630 void
9631 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9632 {
9633 Lisp_Object args[3];
9634 Lisp_Object msg, fmt;
9635 char *buffer;
9636 ptrdiff_t len;
9637 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9638 USE_SAFE_ALLOCA;
9639
9640 fmt = msg = Qnil;
9641 GCPRO4 (fmt, msg, arg1, arg2);
9642
9643 args[0] = fmt = build_string (format);
9644 args[1] = arg1;
9645 args[2] = arg2;
9646 msg = Fformat (3, args);
9647
9648 len = SBYTES (msg) + 1;
9649 buffer = SAFE_ALLOCA (len);
9650 memcpy (buffer, SDATA (msg), len);
9651
9652 message_dolog (buffer, len - 1, 1, 0);
9653 SAFE_FREE ();
9654
9655 UNGCPRO;
9656 }
9657
9658
9659 /* Output a newline in the *Messages* buffer if "needs" one. */
9660
9661 void
9662 message_log_maybe_newline (void)
9663 {
9664 if (message_log_need_newline)
9665 message_dolog ("", 0, 1, 0);
9666 }
9667
9668
9669 /* Add a string M of length NBYTES to the message log, optionally
9670 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9671 true, means interpret the contents of M as multibyte. This
9672 function calls low-level routines in order to bypass text property
9673 hooks, etc. which might not be safe to run.
9674
9675 This may GC (insert may run before/after change hooks),
9676 so the buffer M must NOT point to a Lisp string. */
9677
9678 void
9679 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9680 {
9681 const unsigned char *msg = (const unsigned char *) m;
9682
9683 if (!NILP (Vmemory_full))
9684 return;
9685
9686 if (!NILP (Vmessage_log_max))
9687 {
9688 struct buffer *oldbuf;
9689 Lisp_Object oldpoint, oldbegv, oldzv;
9690 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9691 ptrdiff_t point_at_end = 0;
9692 ptrdiff_t zv_at_end = 0;
9693 Lisp_Object old_deactivate_mark;
9694 struct gcpro gcpro1;
9695
9696 old_deactivate_mark = Vdeactivate_mark;
9697 oldbuf = current_buffer;
9698
9699 /* Ensure the Messages buffer exists, and switch to it.
9700 If we created it, set the major-mode. */
9701 {
9702 int newbuffer = 0;
9703 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9704
9705 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9706
9707 if (newbuffer
9708 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9709 call0 (intern ("messages-buffer-mode"));
9710 }
9711
9712 bset_undo_list (current_buffer, Qt);
9713 bset_cache_long_scans (current_buffer, Qnil);
9714
9715 oldpoint = message_dolog_marker1;
9716 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9717 oldbegv = message_dolog_marker2;
9718 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9719 oldzv = message_dolog_marker3;
9720 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9721 GCPRO1 (old_deactivate_mark);
9722
9723 if (PT == Z)
9724 point_at_end = 1;
9725 if (ZV == Z)
9726 zv_at_end = 1;
9727
9728 BEGV = BEG;
9729 BEGV_BYTE = BEG_BYTE;
9730 ZV = Z;
9731 ZV_BYTE = Z_BYTE;
9732 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9733
9734 /* Insert the string--maybe converting multibyte to single byte
9735 or vice versa, so that all the text fits the buffer. */
9736 if (multibyte
9737 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9738 {
9739 ptrdiff_t i;
9740 int c, char_bytes;
9741 char work[1];
9742
9743 /* Convert a multibyte string to single-byte
9744 for the *Message* buffer. */
9745 for (i = 0; i < nbytes; i += char_bytes)
9746 {
9747 c = string_char_and_length (msg + i, &char_bytes);
9748 work[0] = (ASCII_CHAR_P (c)
9749 ? c
9750 : multibyte_char_to_unibyte (c));
9751 insert_1_both (work, 1, 1, 1, 0, 0);
9752 }
9753 }
9754 else if (! multibyte
9755 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9756 {
9757 ptrdiff_t i;
9758 int c, char_bytes;
9759 unsigned char str[MAX_MULTIBYTE_LENGTH];
9760 /* Convert a single-byte string to multibyte
9761 for the *Message* buffer. */
9762 for (i = 0; i < nbytes; i++)
9763 {
9764 c = msg[i];
9765 MAKE_CHAR_MULTIBYTE (c);
9766 char_bytes = CHAR_STRING (c, str);
9767 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9768 }
9769 }
9770 else if (nbytes)
9771 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9772
9773 if (nlflag)
9774 {
9775 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9776 printmax_t dups;
9777
9778 insert_1_both ("\n", 1, 1, 1, 0, 0);
9779
9780 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9781 this_bol = PT;
9782 this_bol_byte = PT_BYTE;
9783
9784 /* See if this line duplicates the previous one.
9785 If so, combine duplicates. */
9786 if (this_bol > BEG)
9787 {
9788 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9789 prev_bol = PT;
9790 prev_bol_byte = PT_BYTE;
9791
9792 dups = message_log_check_duplicate (prev_bol_byte,
9793 this_bol_byte);
9794 if (dups)
9795 {
9796 del_range_both (prev_bol, prev_bol_byte,
9797 this_bol, this_bol_byte, 0);
9798 if (dups > 1)
9799 {
9800 char dupstr[sizeof " [ times]"
9801 + INT_STRLEN_BOUND (printmax_t)];
9802
9803 /* If you change this format, don't forget to also
9804 change message_log_check_duplicate. */
9805 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9806 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9807 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9808 }
9809 }
9810 }
9811
9812 /* If we have more than the desired maximum number of lines
9813 in the *Messages* buffer now, delete the oldest ones.
9814 This is safe because we don't have undo in this buffer. */
9815
9816 if (NATNUMP (Vmessage_log_max))
9817 {
9818 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9819 -XFASTINT (Vmessage_log_max) - 1, 0);
9820 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9821 }
9822 }
9823 BEGV = marker_position (oldbegv);
9824 BEGV_BYTE = marker_byte_position (oldbegv);
9825
9826 if (zv_at_end)
9827 {
9828 ZV = Z;
9829 ZV_BYTE = Z_BYTE;
9830 }
9831 else
9832 {
9833 ZV = marker_position (oldzv);
9834 ZV_BYTE = marker_byte_position (oldzv);
9835 }
9836
9837 if (point_at_end)
9838 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9839 else
9840 /* We can't do Fgoto_char (oldpoint) because it will run some
9841 Lisp code. */
9842 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9843 marker_byte_position (oldpoint));
9844
9845 UNGCPRO;
9846 unchain_marker (XMARKER (oldpoint));
9847 unchain_marker (XMARKER (oldbegv));
9848 unchain_marker (XMARKER (oldzv));
9849
9850 /* We called insert_1_both above with its 5th argument (PREPARE)
9851 zero, which prevents insert_1_both from calling
9852 prepare_to_modify_buffer, which in turns prevents us from
9853 incrementing windows_or_buffers_changed even if *Messages* is
9854 shown in some window. So we must manually set
9855 windows_or_buffers_changed here to make up for that. */
9856 windows_or_buffers_changed = old_windows_or_buffers_changed;
9857 bset_redisplay (current_buffer);
9858
9859 set_buffer_internal (oldbuf);
9860
9861 message_log_need_newline = !nlflag;
9862 Vdeactivate_mark = old_deactivate_mark;
9863 }
9864 }
9865
9866
9867 /* We are at the end of the buffer after just having inserted a newline.
9868 (Note: We depend on the fact we won't be crossing the gap.)
9869 Check to see if the most recent message looks a lot like the previous one.
9870 Return 0 if different, 1 if the new one should just replace it, or a
9871 value N > 1 if we should also append " [N times]". */
9872
9873 static intmax_t
9874 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9875 {
9876 ptrdiff_t i;
9877 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9878 int seen_dots = 0;
9879 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9880 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9881
9882 for (i = 0; i < len; i++)
9883 {
9884 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9885 seen_dots = 1;
9886 if (p1[i] != p2[i])
9887 return seen_dots;
9888 }
9889 p1 += len;
9890 if (*p1 == '\n')
9891 return 2;
9892 if (*p1++ == ' ' && *p1++ == '[')
9893 {
9894 char *pend;
9895 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9896 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9897 return n + 1;
9898 }
9899 return 0;
9900 }
9901 \f
9902
9903 /* Display an echo area message M with a specified length of NBYTES
9904 bytes. The string may include null characters. If M is not a
9905 string, clear out any existing message, and let the mini-buffer
9906 text show through.
9907
9908 This function cancels echoing. */
9909
9910 void
9911 message3 (Lisp_Object m)
9912 {
9913 struct gcpro gcpro1;
9914
9915 GCPRO1 (m);
9916 clear_message (true, true);
9917 cancel_echoing ();
9918
9919 /* First flush out any partial line written with print. */
9920 message_log_maybe_newline ();
9921 if (STRINGP (m))
9922 {
9923 ptrdiff_t nbytes = SBYTES (m);
9924 bool multibyte = STRING_MULTIBYTE (m);
9925 USE_SAFE_ALLOCA;
9926 char *buffer = SAFE_ALLOCA (nbytes);
9927 memcpy (buffer, SDATA (m), nbytes);
9928 message_dolog (buffer, nbytes, 1, multibyte);
9929 SAFE_FREE ();
9930 }
9931 message3_nolog (m);
9932
9933 UNGCPRO;
9934 }
9935
9936
9937 /* The non-logging version of message3.
9938 This does not cancel echoing, because it is used for echoing.
9939 Perhaps we need to make a separate function for echoing
9940 and make this cancel echoing. */
9941
9942 void
9943 message3_nolog (Lisp_Object m)
9944 {
9945 struct frame *sf = SELECTED_FRAME ();
9946
9947 if (FRAME_INITIAL_P (sf))
9948 {
9949 if (noninteractive_need_newline)
9950 putc ('\n', stderr);
9951 noninteractive_need_newline = 0;
9952 if (STRINGP (m))
9953 {
9954 Lisp_Object s = ENCODE_SYSTEM (m);
9955
9956 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9957 }
9958 if (cursor_in_echo_area == 0)
9959 fprintf (stderr, "\n");
9960 fflush (stderr);
9961 }
9962 /* Error messages get reported properly by cmd_error, so this must be just an
9963 informative message; if the frame hasn't really been initialized yet, just
9964 toss it. */
9965 else if (INTERACTIVE && sf->glyphs_initialized_p)
9966 {
9967 /* Get the frame containing the mini-buffer
9968 that the selected frame is using. */
9969 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9970 Lisp_Object frame = XWINDOW (mini_window)->frame;
9971 struct frame *f = XFRAME (frame);
9972
9973 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9974 Fmake_frame_visible (frame);
9975
9976 if (STRINGP (m) && SCHARS (m) > 0)
9977 {
9978 set_message (m);
9979 if (minibuffer_auto_raise)
9980 Fraise_frame (frame);
9981 /* Assume we are not echoing.
9982 (If we are, echo_now will override this.) */
9983 echo_message_buffer = Qnil;
9984 }
9985 else
9986 clear_message (true, true);
9987
9988 do_pending_window_change (0);
9989 echo_area_display (1);
9990 do_pending_window_change (0);
9991 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9992 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9993 }
9994 }
9995
9996
9997 /* Display a null-terminated echo area message M. If M is 0, clear
9998 out any existing message, and let the mini-buffer text show through.
9999
10000 The buffer M must continue to exist until after the echo area gets
10001 cleared or some other message gets displayed there. Do not pass
10002 text that is stored in a Lisp string. Do not pass text in a buffer
10003 that was alloca'd. */
10004
10005 void
10006 message1 (const char *m)
10007 {
10008 message3 (m ? build_unibyte_string (m) : Qnil);
10009 }
10010
10011
10012 /* The non-logging counterpart of message1. */
10013
10014 void
10015 message1_nolog (const char *m)
10016 {
10017 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10018 }
10019
10020 /* Display a message M which contains a single %s
10021 which gets replaced with STRING. */
10022
10023 void
10024 message_with_string (const char *m, Lisp_Object string, int log)
10025 {
10026 CHECK_STRING (string);
10027
10028 if (noninteractive)
10029 {
10030 if (m)
10031 {
10032 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10033 String whose data pointer might be passed to us in M. So
10034 we use a local copy. */
10035 char *fmt = xstrdup (m);
10036
10037 if (noninteractive_need_newline)
10038 putc ('\n', stderr);
10039 noninteractive_need_newline = 0;
10040 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10041 if (!cursor_in_echo_area)
10042 fprintf (stderr, "\n");
10043 fflush (stderr);
10044 xfree (fmt);
10045 }
10046 }
10047 else if (INTERACTIVE)
10048 {
10049 /* The frame whose minibuffer we're going to display the message on.
10050 It may be larger than the selected frame, so we need
10051 to use its buffer, not the selected frame's buffer. */
10052 Lisp_Object mini_window;
10053 struct frame *f, *sf = SELECTED_FRAME ();
10054
10055 /* Get the frame containing the minibuffer
10056 that the selected frame is using. */
10057 mini_window = FRAME_MINIBUF_WINDOW (sf);
10058 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10059
10060 /* Error messages get reported properly by cmd_error, so this must be
10061 just an informative message; if the frame hasn't really been
10062 initialized yet, just toss it. */
10063 if (f->glyphs_initialized_p)
10064 {
10065 Lisp_Object args[2], msg;
10066 struct gcpro gcpro1, gcpro2;
10067
10068 args[0] = build_string (m);
10069 args[1] = msg = string;
10070 GCPRO2 (args[0], msg);
10071 gcpro1.nvars = 2;
10072
10073 msg = Fformat (2, args);
10074
10075 if (log)
10076 message3 (msg);
10077 else
10078 message3_nolog (msg);
10079
10080 UNGCPRO;
10081
10082 /* Print should start at the beginning of the message
10083 buffer next time. */
10084 message_buf_print = 0;
10085 }
10086 }
10087 }
10088
10089
10090 /* Dump an informative message to the minibuf. If M is 0, clear out
10091 any existing message, and let the mini-buffer text show through. */
10092
10093 static void
10094 vmessage (const char *m, va_list ap)
10095 {
10096 if (noninteractive)
10097 {
10098 if (m)
10099 {
10100 if (noninteractive_need_newline)
10101 putc ('\n', stderr);
10102 noninteractive_need_newline = 0;
10103 vfprintf (stderr, m, ap);
10104 if (cursor_in_echo_area == 0)
10105 fprintf (stderr, "\n");
10106 fflush (stderr);
10107 }
10108 }
10109 else if (INTERACTIVE)
10110 {
10111 /* The frame whose mini-buffer we're going to display the message
10112 on. It may be larger than the selected frame, so we need to
10113 use its buffer, not the selected frame's buffer. */
10114 Lisp_Object mini_window;
10115 struct frame *f, *sf = SELECTED_FRAME ();
10116
10117 /* Get the frame containing the mini-buffer
10118 that the selected frame is using. */
10119 mini_window = FRAME_MINIBUF_WINDOW (sf);
10120 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10121
10122 /* Error messages get reported properly by cmd_error, so this must be
10123 just an informative message; if the frame hasn't really been
10124 initialized yet, just toss it. */
10125 if (f->glyphs_initialized_p)
10126 {
10127 if (m)
10128 {
10129 ptrdiff_t len;
10130 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10131 char *message_buf = alloca (maxsize + 1);
10132
10133 len = doprnt (message_buf, maxsize, m, 0, ap);
10134
10135 message3 (make_string (message_buf, len));
10136 }
10137 else
10138 message1 (0);
10139
10140 /* Print should start at the beginning of the message
10141 buffer next time. */
10142 message_buf_print = 0;
10143 }
10144 }
10145 }
10146
10147 void
10148 message (const char *m, ...)
10149 {
10150 va_list ap;
10151 va_start (ap, m);
10152 vmessage (m, ap);
10153 va_end (ap);
10154 }
10155
10156
10157 #if 0
10158 /* The non-logging version of message. */
10159
10160 void
10161 message_nolog (const char *m, ...)
10162 {
10163 Lisp_Object old_log_max;
10164 va_list ap;
10165 va_start (ap, m);
10166 old_log_max = Vmessage_log_max;
10167 Vmessage_log_max = Qnil;
10168 vmessage (m, ap);
10169 Vmessage_log_max = old_log_max;
10170 va_end (ap);
10171 }
10172 #endif
10173
10174
10175 /* Display the current message in the current mini-buffer. This is
10176 only called from error handlers in process.c, and is not time
10177 critical. */
10178
10179 void
10180 update_echo_area (void)
10181 {
10182 if (!NILP (echo_area_buffer[0]))
10183 {
10184 Lisp_Object string;
10185 string = Fcurrent_message ();
10186 message3 (string);
10187 }
10188 }
10189
10190
10191 /* Make sure echo area buffers in `echo_buffers' are live.
10192 If they aren't, make new ones. */
10193
10194 static void
10195 ensure_echo_area_buffers (void)
10196 {
10197 int i;
10198
10199 for (i = 0; i < 2; ++i)
10200 if (!BUFFERP (echo_buffer[i])
10201 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10202 {
10203 char name[30];
10204 Lisp_Object old_buffer;
10205 int j;
10206
10207 old_buffer = echo_buffer[i];
10208 echo_buffer[i] = Fget_buffer_create
10209 (make_formatted_string (name, " *Echo Area %d*", i));
10210 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10211 /* to force word wrap in echo area -
10212 it was decided to postpone this*/
10213 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10214
10215 for (j = 0; j < 2; ++j)
10216 if (EQ (old_buffer, echo_area_buffer[j]))
10217 echo_area_buffer[j] = echo_buffer[i];
10218 }
10219 }
10220
10221
10222 /* Call FN with args A1..A2 with either the current or last displayed
10223 echo_area_buffer as current buffer.
10224
10225 WHICH zero means use the current message buffer
10226 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10227 from echo_buffer[] and clear it.
10228
10229 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10230 suitable buffer from echo_buffer[] and clear it.
10231
10232 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10233 that the current message becomes the last displayed one, make
10234 choose a suitable buffer for echo_area_buffer[0], and clear it.
10235
10236 Value is what FN returns. */
10237
10238 static int
10239 with_echo_area_buffer (struct window *w, int which,
10240 int (*fn) (ptrdiff_t, Lisp_Object),
10241 ptrdiff_t a1, Lisp_Object a2)
10242 {
10243 Lisp_Object buffer;
10244 int this_one, the_other, clear_buffer_p, rc;
10245 ptrdiff_t count = SPECPDL_INDEX ();
10246
10247 /* If buffers aren't live, make new ones. */
10248 ensure_echo_area_buffers ();
10249
10250 clear_buffer_p = 0;
10251
10252 if (which == 0)
10253 this_one = 0, the_other = 1;
10254 else if (which > 0)
10255 this_one = 1, the_other = 0;
10256 else
10257 {
10258 this_one = 0, the_other = 1;
10259 clear_buffer_p = true;
10260
10261 /* We need a fresh one in case the current echo buffer equals
10262 the one containing the last displayed echo area message. */
10263 if (!NILP (echo_area_buffer[this_one])
10264 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10265 echo_area_buffer[this_one] = Qnil;
10266 }
10267
10268 /* Choose a suitable buffer from echo_buffer[] is we don't
10269 have one. */
10270 if (NILP (echo_area_buffer[this_one]))
10271 {
10272 echo_area_buffer[this_one]
10273 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10274 ? echo_buffer[the_other]
10275 : echo_buffer[this_one]);
10276 clear_buffer_p = true;
10277 }
10278
10279 buffer = echo_area_buffer[this_one];
10280
10281 /* Don't get confused by reusing the buffer used for echoing
10282 for a different purpose. */
10283 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10284 cancel_echoing ();
10285
10286 record_unwind_protect (unwind_with_echo_area_buffer,
10287 with_echo_area_buffer_unwind_data (w));
10288
10289 /* Make the echo area buffer current. Note that for display
10290 purposes, it is not necessary that the displayed window's buffer
10291 == current_buffer, except for text property lookup. So, let's
10292 only set that buffer temporarily here without doing a full
10293 Fset_window_buffer. We must also change w->pointm, though,
10294 because otherwise an assertions in unshow_buffer fails, and Emacs
10295 aborts. */
10296 set_buffer_internal_1 (XBUFFER (buffer));
10297 if (w)
10298 {
10299 wset_buffer (w, buffer);
10300 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10301 }
10302
10303 bset_undo_list (current_buffer, Qt);
10304 bset_read_only (current_buffer, Qnil);
10305 specbind (Qinhibit_read_only, Qt);
10306 specbind (Qinhibit_modification_hooks, Qt);
10307
10308 if (clear_buffer_p && Z > BEG)
10309 del_range (BEG, Z);
10310
10311 eassert (BEGV >= BEG);
10312 eassert (ZV <= Z && ZV >= BEGV);
10313
10314 rc = fn (a1, a2);
10315
10316 eassert (BEGV >= BEG);
10317 eassert (ZV <= Z && ZV >= BEGV);
10318
10319 unbind_to (count, Qnil);
10320 return rc;
10321 }
10322
10323
10324 /* Save state that should be preserved around the call to the function
10325 FN called in with_echo_area_buffer. */
10326
10327 static Lisp_Object
10328 with_echo_area_buffer_unwind_data (struct window *w)
10329 {
10330 int i = 0;
10331 Lisp_Object vector, tmp;
10332
10333 /* Reduce consing by keeping one vector in
10334 Vwith_echo_area_save_vector. */
10335 vector = Vwith_echo_area_save_vector;
10336 Vwith_echo_area_save_vector = Qnil;
10337
10338 if (NILP (vector))
10339 vector = Fmake_vector (make_number (9), Qnil);
10340
10341 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10342 ASET (vector, i, Vdeactivate_mark); ++i;
10343 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10344
10345 if (w)
10346 {
10347 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10348 ASET (vector, i, w->contents); ++i;
10349 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10351 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10352 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10353 }
10354 else
10355 {
10356 int end = i + 6;
10357 for (; i < end; ++i)
10358 ASET (vector, i, Qnil);
10359 }
10360
10361 eassert (i == ASIZE (vector));
10362 return vector;
10363 }
10364
10365
10366 /* Restore global state from VECTOR which was created by
10367 with_echo_area_buffer_unwind_data. */
10368
10369 static void
10370 unwind_with_echo_area_buffer (Lisp_Object vector)
10371 {
10372 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10373 Vdeactivate_mark = AREF (vector, 1);
10374 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10375
10376 if (WINDOWP (AREF (vector, 3)))
10377 {
10378 struct window *w;
10379 Lisp_Object buffer;
10380
10381 w = XWINDOW (AREF (vector, 3));
10382 buffer = AREF (vector, 4);
10383
10384 wset_buffer (w, buffer);
10385 set_marker_both (w->pointm, buffer,
10386 XFASTINT (AREF (vector, 5)),
10387 XFASTINT (AREF (vector, 6)));
10388 set_marker_both (w->start, buffer,
10389 XFASTINT (AREF (vector, 7)),
10390 XFASTINT (AREF (vector, 8)));
10391 }
10392
10393 Vwith_echo_area_save_vector = vector;
10394 }
10395
10396
10397 /* Set up the echo area for use by print functions. MULTIBYTE_P
10398 non-zero means we will print multibyte. */
10399
10400 void
10401 setup_echo_area_for_printing (int multibyte_p)
10402 {
10403 /* If we can't find an echo area any more, exit. */
10404 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10405 Fkill_emacs (Qnil);
10406
10407 ensure_echo_area_buffers ();
10408
10409 if (!message_buf_print)
10410 {
10411 /* A message has been output since the last time we printed.
10412 Choose a fresh echo area buffer. */
10413 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10414 echo_area_buffer[0] = echo_buffer[1];
10415 else
10416 echo_area_buffer[0] = echo_buffer[0];
10417
10418 /* Switch to that buffer and clear it. */
10419 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10420 bset_truncate_lines (current_buffer, Qnil);
10421
10422 if (Z > BEG)
10423 {
10424 ptrdiff_t count = SPECPDL_INDEX ();
10425 specbind (Qinhibit_read_only, Qt);
10426 /* Note that undo recording is always disabled. */
10427 del_range (BEG, Z);
10428 unbind_to (count, Qnil);
10429 }
10430 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10431
10432 /* Set up the buffer for the multibyteness we need. */
10433 if (multibyte_p
10434 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10435 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10436
10437 /* Raise the frame containing the echo area. */
10438 if (minibuffer_auto_raise)
10439 {
10440 struct frame *sf = SELECTED_FRAME ();
10441 Lisp_Object mini_window;
10442 mini_window = FRAME_MINIBUF_WINDOW (sf);
10443 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10444 }
10445
10446 message_log_maybe_newline ();
10447 message_buf_print = 1;
10448 }
10449 else
10450 {
10451 if (NILP (echo_area_buffer[0]))
10452 {
10453 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10454 echo_area_buffer[0] = echo_buffer[1];
10455 else
10456 echo_area_buffer[0] = echo_buffer[0];
10457 }
10458
10459 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10460 {
10461 /* Someone switched buffers between print requests. */
10462 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10463 bset_truncate_lines (current_buffer, Qnil);
10464 }
10465 }
10466 }
10467
10468
10469 /* Display an echo area message in window W. Value is non-zero if W's
10470 height is changed. If display_last_displayed_message_p is
10471 non-zero, display the message that was last displayed, otherwise
10472 display the current message. */
10473
10474 static int
10475 display_echo_area (struct window *w)
10476 {
10477 int i, no_message_p, window_height_changed_p;
10478
10479 /* Temporarily disable garbage collections while displaying the echo
10480 area. This is done because a GC can print a message itself.
10481 That message would modify the echo area buffer's contents while a
10482 redisplay of the buffer is going on, and seriously confuse
10483 redisplay. */
10484 ptrdiff_t count = inhibit_garbage_collection ();
10485
10486 /* If there is no message, we must call display_echo_area_1
10487 nevertheless because it resizes the window. But we will have to
10488 reset the echo_area_buffer in question to nil at the end because
10489 with_echo_area_buffer will sets it to an empty buffer. */
10490 i = display_last_displayed_message_p ? 1 : 0;
10491 no_message_p = NILP (echo_area_buffer[i]);
10492
10493 window_height_changed_p
10494 = with_echo_area_buffer (w, display_last_displayed_message_p,
10495 display_echo_area_1,
10496 (intptr_t) w, Qnil);
10497
10498 if (no_message_p)
10499 echo_area_buffer[i] = Qnil;
10500
10501 unbind_to (count, Qnil);
10502 return window_height_changed_p;
10503 }
10504
10505
10506 /* Helper for display_echo_area. Display the current buffer which
10507 contains the current echo area message in window W, a mini-window,
10508 a pointer to which is passed in A1. A2..A4 are currently not used.
10509 Change the height of W so that all of the message is displayed.
10510 Value is non-zero if height of W was changed. */
10511
10512 static int
10513 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10514 {
10515 intptr_t i1 = a1;
10516 struct window *w = (struct window *) i1;
10517 Lisp_Object window;
10518 struct text_pos start;
10519 int window_height_changed_p = 0;
10520
10521 /* Do this before displaying, so that we have a large enough glyph
10522 matrix for the display. If we can't get enough space for the
10523 whole text, display the last N lines. That works by setting w->start. */
10524 window_height_changed_p = resize_mini_window (w, 0);
10525
10526 /* Use the starting position chosen by resize_mini_window. */
10527 SET_TEXT_POS_FROM_MARKER (start, w->start);
10528
10529 /* Display. */
10530 clear_glyph_matrix (w->desired_matrix);
10531 XSETWINDOW (window, w);
10532 try_window (window, start, 0);
10533
10534 return window_height_changed_p;
10535 }
10536
10537
10538 /* Resize the echo area window to exactly the size needed for the
10539 currently displayed message, if there is one. If a mini-buffer
10540 is active, don't shrink it. */
10541
10542 void
10543 resize_echo_area_exactly (void)
10544 {
10545 if (BUFFERP (echo_area_buffer[0])
10546 && WINDOWP (echo_area_window))
10547 {
10548 struct window *w = XWINDOW (echo_area_window);
10549 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10550 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10551 (intptr_t) w, resize_exactly);
10552 if (resized_p)
10553 {
10554 windows_or_buffers_changed = 42;
10555 update_mode_lines = 30;
10556 redisplay_internal ();
10557 }
10558 }
10559 }
10560
10561
10562 /* Callback function for with_echo_area_buffer, when used from
10563 resize_echo_area_exactly. A1 contains a pointer to the window to
10564 resize, EXACTLY non-nil means resize the mini-window exactly to the
10565 size of the text displayed. A3 and A4 are not used. Value is what
10566 resize_mini_window returns. */
10567
10568 static int
10569 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10570 {
10571 intptr_t i1 = a1;
10572 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10573 }
10574
10575
10576 /* Resize mini-window W to fit the size of its contents. EXACT_P
10577 means size the window exactly to the size needed. Otherwise, it's
10578 only enlarged until W's buffer is empty.
10579
10580 Set W->start to the right place to begin display. If the whole
10581 contents fit, start at the beginning. Otherwise, start so as
10582 to make the end of the contents appear. This is particularly
10583 important for y-or-n-p, but seems desirable generally.
10584
10585 Value is non-zero if the window height has been changed. */
10586
10587 int
10588 resize_mini_window (struct window *w, int exact_p)
10589 {
10590 struct frame *f = XFRAME (w->frame);
10591 int window_height_changed_p = 0;
10592
10593 eassert (MINI_WINDOW_P (w));
10594
10595 /* By default, start display at the beginning. */
10596 set_marker_both (w->start, w->contents,
10597 BUF_BEGV (XBUFFER (w->contents)),
10598 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10599
10600 /* Don't resize windows while redisplaying a window; it would
10601 confuse redisplay functions when the size of the window they are
10602 displaying changes from under them. Such a resizing can happen,
10603 for instance, when which-func prints a long message while
10604 we are running fontification-functions. We're running these
10605 functions with safe_call which binds inhibit-redisplay to t. */
10606 if (!NILP (Vinhibit_redisplay))
10607 return 0;
10608
10609 /* Nil means don't try to resize. */
10610 if (NILP (Vresize_mini_windows)
10611 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10612 return 0;
10613
10614 if (!FRAME_MINIBUF_ONLY_P (f))
10615 {
10616 struct it it;
10617 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10618 + WINDOW_PIXEL_HEIGHT (w));
10619 int unit = FRAME_LINE_HEIGHT (f);
10620 int height, max_height;
10621 struct text_pos start;
10622 struct buffer *old_current_buffer = NULL;
10623
10624 if (current_buffer != XBUFFER (w->contents))
10625 {
10626 old_current_buffer = current_buffer;
10627 set_buffer_internal (XBUFFER (w->contents));
10628 }
10629
10630 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10631
10632 /* Compute the max. number of lines specified by the user. */
10633 if (FLOATP (Vmax_mini_window_height))
10634 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10635 else if (INTEGERP (Vmax_mini_window_height))
10636 max_height = XINT (Vmax_mini_window_height) * unit;
10637 else
10638 max_height = total_height / 4;
10639
10640 /* Correct that max. height if it's bogus. */
10641 max_height = clip_to_bounds (unit, max_height, total_height);
10642
10643 /* Find out the height of the text in the window. */
10644 if (it.line_wrap == TRUNCATE)
10645 height = unit;
10646 else
10647 {
10648 last_height = 0;
10649 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10650 if (it.max_ascent == 0 && it.max_descent == 0)
10651 height = it.current_y + last_height;
10652 else
10653 height = it.current_y + it.max_ascent + it.max_descent;
10654 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10655 }
10656
10657 /* Compute a suitable window start. */
10658 if (height > max_height)
10659 {
10660 height = max_height;
10661 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10662 move_it_vertically_backward (&it, height);
10663 start = it.current.pos;
10664 }
10665 else
10666 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10667 SET_MARKER_FROM_TEXT_POS (w->start, start);
10668
10669 if (EQ (Vresize_mini_windows, Qgrow_only))
10670 {
10671 /* Let it grow only, until we display an empty message, in which
10672 case the window shrinks again. */
10673 if (height > WINDOW_PIXEL_HEIGHT (w))
10674 {
10675 int old_height = WINDOW_PIXEL_HEIGHT (w);
10676
10677 FRAME_WINDOWS_FROZEN (f) = 1;
10678 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10679 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10680 }
10681 else if (height < WINDOW_PIXEL_HEIGHT (w)
10682 && (exact_p || BEGV == ZV))
10683 {
10684 int old_height = WINDOW_PIXEL_HEIGHT (w);
10685
10686 FRAME_WINDOWS_FROZEN (f) = 0;
10687 shrink_mini_window (w, 1);
10688 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10689 }
10690 }
10691 else
10692 {
10693 /* Always resize to exact size needed. */
10694 if (height > WINDOW_PIXEL_HEIGHT (w))
10695 {
10696 int old_height = WINDOW_PIXEL_HEIGHT (w);
10697
10698 FRAME_WINDOWS_FROZEN (f) = 1;
10699 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10700 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10701 }
10702 else if (height < WINDOW_PIXEL_HEIGHT (w))
10703 {
10704 int old_height = WINDOW_PIXEL_HEIGHT (w);
10705
10706 FRAME_WINDOWS_FROZEN (f) = 0;
10707 shrink_mini_window (w, 1);
10708
10709 if (height)
10710 {
10711 FRAME_WINDOWS_FROZEN (f) = 1;
10712 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10713 }
10714
10715 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10716 }
10717 }
10718
10719 if (old_current_buffer)
10720 set_buffer_internal (old_current_buffer);
10721 }
10722
10723 return window_height_changed_p;
10724 }
10725
10726
10727 /* Value is the current message, a string, or nil if there is no
10728 current message. */
10729
10730 Lisp_Object
10731 current_message (void)
10732 {
10733 Lisp_Object msg;
10734
10735 if (!BUFFERP (echo_area_buffer[0]))
10736 msg = Qnil;
10737 else
10738 {
10739 with_echo_area_buffer (0, 0, current_message_1,
10740 (intptr_t) &msg, Qnil);
10741 if (NILP (msg))
10742 echo_area_buffer[0] = Qnil;
10743 }
10744
10745 return msg;
10746 }
10747
10748
10749 static int
10750 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10751 {
10752 intptr_t i1 = a1;
10753 Lisp_Object *msg = (Lisp_Object *) i1;
10754
10755 if (Z > BEG)
10756 *msg = make_buffer_string (BEG, Z, 1);
10757 else
10758 *msg = Qnil;
10759 return 0;
10760 }
10761
10762
10763 /* Push the current message on Vmessage_stack for later restoration
10764 by restore_message. Value is non-zero if the current message isn't
10765 empty. This is a relatively infrequent operation, so it's not
10766 worth optimizing. */
10767
10768 bool
10769 push_message (void)
10770 {
10771 Lisp_Object msg = current_message ();
10772 Vmessage_stack = Fcons (msg, Vmessage_stack);
10773 return STRINGP (msg);
10774 }
10775
10776
10777 /* Restore message display from the top of Vmessage_stack. */
10778
10779 void
10780 restore_message (void)
10781 {
10782 eassert (CONSP (Vmessage_stack));
10783 message3_nolog (XCAR (Vmessage_stack));
10784 }
10785
10786
10787 /* Handler for unwind-protect calling pop_message. */
10788
10789 void
10790 pop_message_unwind (void)
10791 {
10792 /* Pop the top-most entry off Vmessage_stack. */
10793 eassert (CONSP (Vmessage_stack));
10794 Vmessage_stack = XCDR (Vmessage_stack);
10795 }
10796
10797
10798 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10799 exits. If the stack is not empty, we have a missing pop_message
10800 somewhere. */
10801
10802 void
10803 check_message_stack (void)
10804 {
10805 if (!NILP (Vmessage_stack))
10806 emacs_abort ();
10807 }
10808
10809
10810 /* Truncate to NCHARS what will be displayed in the echo area the next
10811 time we display it---but don't redisplay it now. */
10812
10813 void
10814 truncate_echo_area (ptrdiff_t nchars)
10815 {
10816 if (nchars == 0)
10817 echo_area_buffer[0] = Qnil;
10818 else if (!noninteractive
10819 && INTERACTIVE
10820 && !NILP (echo_area_buffer[0]))
10821 {
10822 struct frame *sf = SELECTED_FRAME ();
10823 /* Error messages get reported properly by cmd_error, so this must be
10824 just an informative message; if the frame hasn't really been
10825 initialized yet, just toss it. */
10826 if (sf->glyphs_initialized_p)
10827 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10828 }
10829 }
10830
10831
10832 /* Helper function for truncate_echo_area. Truncate the current
10833 message to at most NCHARS characters. */
10834
10835 static int
10836 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10837 {
10838 if (BEG + nchars < Z)
10839 del_range (BEG + nchars, Z);
10840 if (Z == BEG)
10841 echo_area_buffer[0] = Qnil;
10842 return 0;
10843 }
10844
10845 /* Set the current message to STRING. */
10846
10847 static void
10848 set_message (Lisp_Object string)
10849 {
10850 eassert (STRINGP (string));
10851
10852 message_enable_multibyte = STRING_MULTIBYTE (string);
10853
10854 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10855 message_buf_print = 0;
10856 help_echo_showing_p = 0;
10857
10858 if (STRINGP (Vdebug_on_message)
10859 && STRINGP (string)
10860 && fast_string_match (Vdebug_on_message, string) >= 0)
10861 call_debugger (list2 (Qerror, string));
10862 }
10863
10864
10865 /* Helper function for set_message. First argument is ignored and second
10866 argument has the same meaning as for set_message.
10867 This function is called with the echo area buffer being current. */
10868
10869 static int
10870 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10871 {
10872 eassert (STRINGP (string));
10873
10874 /* Change multibyteness of the echo buffer appropriately. */
10875 if (message_enable_multibyte
10876 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10877 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10878
10879 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10880 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10881 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10882
10883 /* Insert new message at BEG. */
10884 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10885
10886 /* This function takes care of single/multibyte conversion.
10887 We just have to ensure that the echo area buffer has the right
10888 setting of enable_multibyte_characters. */
10889 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10890
10891 return 0;
10892 }
10893
10894
10895 /* Clear messages. CURRENT_P non-zero means clear the current
10896 message. LAST_DISPLAYED_P non-zero means clear the message
10897 last displayed. */
10898
10899 void
10900 clear_message (bool current_p, bool last_displayed_p)
10901 {
10902 if (current_p)
10903 {
10904 echo_area_buffer[0] = Qnil;
10905 message_cleared_p = true;
10906 }
10907
10908 if (last_displayed_p)
10909 echo_area_buffer[1] = Qnil;
10910
10911 message_buf_print = 0;
10912 }
10913
10914 /* Clear garbaged frames.
10915
10916 This function is used where the old redisplay called
10917 redraw_garbaged_frames which in turn called redraw_frame which in
10918 turn called clear_frame. The call to clear_frame was a source of
10919 flickering. I believe a clear_frame is not necessary. It should
10920 suffice in the new redisplay to invalidate all current matrices,
10921 and ensure a complete redisplay of all windows. */
10922
10923 static void
10924 clear_garbaged_frames (void)
10925 {
10926 if (frame_garbaged)
10927 {
10928 Lisp_Object tail, frame;
10929
10930 FOR_EACH_FRAME (tail, frame)
10931 {
10932 struct frame *f = XFRAME (frame);
10933
10934 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10935 {
10936 if (f->resized_p)
10937 redraw_frame (f);
10938 else
10939 clear_current_matrices (f);
10940 fset_redisplay (f);
10941 f->garbaged = false;
10942 f->resized_p = false;
10943 }
10944 }
10945
10946 frame_garbaged = false;
10947 }
10948 }
10949
10950
10951 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10952 is non-zero update selected_frame. Value is non-zero if the
10953 mini-windows height has been changed. */
10954
10955 static int
10956 echo_area_display (int update_frame_p)
10957 {
10958 Lisp_Object mini_window;
10959 struct window *w;
10960 struct frame *f;
10961 int window_height_changed_p = 0;
10962 struct frame *sf = SELECTED_FRAME ();
10963
10964 mini_window = FRAME_MINIBUF_WINDOW (sf);
10965 w = XWINDOW (mini_window);
10966 f = XFRAME (WINDOW_FRAME (w));
10967
10968 /* Don't display if frame is invisible or not yet initialized. */
10969 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10970 return 0;
10971
10972 #ifdef HAVE_WINDOW_SYSTEM
10973 /* When Emacs starts, selected_frame may be the initial terminal
10974 frame. If we let this through, a message would be displayed on
10975 the terminal. */
10976 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10977 return 0;
10978 #endif /* HAVE_WINDOW_SYSTEM */
10979
10980 /* Redraw garbaged frames. */
10981 clear_garbaged_frames ();
10982
10983 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10984 {
10985 echo_area_window = mini_window;
10986 window_height_changed_p = display_echo_area (w);
10987 w->must_be_updated_p = true;
10988
10989 /* Update the display, unless called from redisplay_internal.
10990 Also don't update the screen during redisplay itself. The
10991 update will happen at the end of redisplay, and an update
10992 here could cause confusion. */
10993 if (update_frame_p && !redisplaying_p)
10994 {
10995 int n = 0;
10996
10997 /* If the display update has been interrupted by pending
10998 input, update mode lines in the frame. Due to the
10999 pending input, it might have been that redisplay hasn't
11000 been called, so that mode lines above the echo area are
11001 garbaged. This looks odd, so we prevent it here. */
11002 if (!display_completed)
11003 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11004
11005 if (window_height_changed_p
11006 /* Don't do this if Emacs is shutting down. Redisplay
11007 needs to run hooks. */
11008 && !NILP (Vrun_hooks))
11009 {
11010 /* Must update other windows. Likewise as in other
11011 cases, don't let this update be interrupted by
11012 pending input. */
11013 ptrdiff_t count = SPECPDL_INDEX ();
11014 specbind (Qredisplay_dont_pause, Qt);
11015 windows_or_buffers_changed = 44;
11016 redisplay_internal ();
11017 unbind_to (count, Qnil);
11018 }
11019 else if (FRAME_WINDOW_P (f) && n == 0)
11020 {
11021 /* Window configuration is the same as before.
11022 Can do with a display update of the echo area,
11023 unless we displayed some mode lines. */
11024 update_single_window (w, 1);
11025 flush_frame (f);
11026 }
11027 else
11028 update_frame (f, 1, 1);
11029
11030 /* If cursor is in the echo area, make sure that the next
11031 redisplay displays the minibuffer, so that the cursor will
11032 be replaced with what the minibuffer wants. */
11033 if (cursor_in_echo_area)
11034 wset_redisplay (XWINDOW (mini_window));
11035 }
11036 }
11037 else if (!EQ (mini_window, selected_window))
11038 wset_redisplay (XWINDOW (mini_window));
11039
11040 /* Last displayed message is now the current message. */
11041 echo_area_buffer[1] = echo_area_buffer[0];
11042 /* Inform read_char that we're not echoing. */
11043 echo_message_buffer = Qnil;
11044
11045 /* Prevent redisplay optimization in redisplay_internal by resetting
11046 this_line_start_pos. This is done because the mini-buffer now
11047 displays the message instead of its buffer text. */
11048 if (EQ (mini_window, selected_window))
11049 CHARPOS (this_line_start_pos) = 0;
11050
11051 return window_height_changed_p;
11052 }
11053
11054 /* Nonzero if W's buffer was changed but not saved. */
11055
11056 static int
11057 window_buffer_changed (struct window *w)
11058 {
11059 struct buffer *b = XBUFFER (w->contents);
11060
11061 eassert (BUFFER_LIVE_P (b));
11062
11063 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11064 }
11065
11066 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11067
11068 static int
11069 mode_line_update_needed (struct window *w)
11070 {
11071 return (w->column_number_displayed != -1
11072 && !(PT == w->last_point && !window_outdated (w))
11073 && (w->column_number_displayed != current_column ()));
11074 }
11075
11076 /* Nonzero if window start of W is frozen and may not be changed during
11077 redisplay. */
11078
11079 static bool
11080 window_frozen_p (struct window *w)
11081 {
11082 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11083 {
11084 Lisp_Object window;
11085
11086 XSETWINDOW (window, w);
11087 if (MINI_WINDOW_P (w))
11088 return 0;
11089 else if (EQ (window, selected_window))
11090 return 0;
11091 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11092 && EQ (window, Vminibuf_scroll_window))
11093 /* This special window can't be frozen too. */
11094 return 0;
11095 else
11096 return 1;
11097 }
11098 return 0;
11099 }
11100
11101 /***********************************************************************
11102 Mode Lines and Frame Titles
11103 ***********************************************************************/
11104
11105 /* A buffer for constructing non-propertized mode-line strings and
11106 frame titles in it; allocated from the heap in init_xdisp and
11107 resized as needed in store_mode_line_noprop_char. */
11108
11109 static char *mode_line_noprop_buf;
11110
11111 /* The buffer's end, and a current output position in it. */
11112
11113 static char *mode_line_noprop_buf_end;
11114 static char *mode_line_noprop_ptr;
11115
11116 #define MODE_LINE_NOPROP_LEN(start) \
11117 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11118
11119 static enum {
11120 MODE_LINE_DISPLAY = 0,
11121 MODE_LINE_TITLE,
11122 MODE_LINE_NOPROP,
11123 MODE_LINE_STRING
11124 } mode_line_target;
11125
11126 /* Alist that caches the results of :propertize.
11127 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11128 static Lisp_Object mode_line_proptrans_alist;
11129
11130 /* List of strings making up the mode-line. */
11131 static Lisp_Object mode_line_string_list;
11132
11133 /* Base face property when building propertized mode line string. */
11134 static Lisp_Object mode_line_string_face;
11135 static Lisp_Object mode_line_string_face_prop;
11136
11137
11138 /* Unwind data for mode line strings */
11139
11140 static Lisp_Object Vmode_line_unwind_vector;
11141
11142 static Lisp_Object
11143 format_mode_line_unwind_data (struct frame *target_frame,
11144 struct buffer *obuf,
11145 Lisp_Object owin,
11146 int save_proptrans)
11147 {
11148 Lisp_Object vector, tmp;
11149
11150 /* Reduce consing by keeping one vector in
11151 Vwith_echo_area_save_vector. */
11152 vector = Vmode_line_unwind_vector;
11153 Vmode_line_unwind_vector = Qnil;
11154
11155 if (NILP (vector))
11156 vector = Fmake_vector (make_number (10), Qnil);
11157
11158 ASET (vector, 0, make_number (mode_line_target));
11159 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11160 ASET (vector, 2, mode_line_string_list);
11161 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11162 ASET (vector, 4, mode_line_string_face);
11163 ASET (vector, 5, mode_line_string_face_prop);
11164
11165 if (obuf)
11166 XSETBUFFER (tmp, obuf);
11167 else
11168 tmp = Qnil;
11169 ASET (vector, 6, tmp);
11170 ASET (vector, 7, owin);
11171 if (target_frame)
11172 {
11173 /* Similarly to `with-selected-window', if the operation selects
11174 a window on another frame, we must restore that frame's
11175 selected window, and (for a tty) the top-frame. */
11176 ASET (vector, 8, target_frame->selected_window);
11177 if (FRAME_TERMCAP_P (target_frame))
11178 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11179 }
11180
11181 return vector;
11182 }
11183
11184 static void
11185 unwind_format_mode_line (Lisp_Object vector)
11186 {
11187 Lisp_Object old_window = AREF (vector, 7);
11188 Lisp_Object target_frame_window = AREF (vector, 8);
11189 Lisp_Object old_top_frame = AREF (vector, 9);
11190
11191 mode_line_target = XINT (AREF (vector, 0));
11192 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11193 mode_line_string_list = AREF (vector, 2);
11194 if (! EQ (AREF (vector, 3), Qt))
11195 mode_line_proptrans_alist = AREF (vector, 3);
11196 mode_line_string_face = AREF (vector, 4);
11197 mode_line_string_face_prop = AREF (vector, 5);
11198
11199 /* Select window before buffer, since it may change the buffer. */
11200 if (!NILP (old_window))
11201 {
11202 /* If the operation that we are unwinding had selected a window
11203 on a different frame, reset its frame-selected-window. For a
11204 text terminal, reset its top-frame if necessary. */
11205 if (!NILP (target_frame_window))
11206 {
11207 Lisp_Object frame
11208 = WINDOW_FRAME (XWINDOW (target_frame_window));
11209
11210 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11211 Fselect_window (target_frame_window, Qt);
11212
11213 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11214 Fselect_frame (old_top_frame, Qt);
11215 }
11216
11217 Fselect_window (old_window, Qt);
11218 }
11219
11220 if (!NILP (AREF (vector, 6)))
11221 {
11222 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11223 ASET (vector, 6, Qnil);
11224 }
11225
11226 Vmode_line_unwind_vector = vector;
11227 }
11228
11229
11230 /* Store a single character C for the frame title in mode_line_noprop_buf.
11231 Re-allocate mode_line_noprop_buf if necessary. */
11232
11233 static void
11234 store_mode_line_noprop_char (char c)
11235 {
11236 /* If output position has reached the end of the allocated buffer,
11237 increase the buffer's size. */
11238 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11239 {
11240 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11241 ptrdiff_t size = len;
11242 mode_line_noprop_buf =
11243 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11244 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11245 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11246 }
11247
11248 *mode_line_noprop_ptr++ = c;
11249 }
11250
11251
11252 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11253 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11254 characters that yield more columns than PRECISION; PRECISION <= 0
11255 means copy the whole string. Pad with spaces until FIELD_WIDTH
11256 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11257 pad. Called from display_mode_element when it is used to build a
11258 frame title. */
11259
11260 static int
11261 store_mode_line_noprop (const char *string, int field_width, int precision)
11262 {
11263 const unsigned char *str = (const unsigned char *) string;
11264 int n = 0;
11265 ptrdiff_t dummy, nbytes;
11266
11267 /* Copy at most PRECISION chars from STR. */
11268 nbytes = strlen (string);
11269 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11270 while (nbytes--)
11271 store_mode_line_noprop_char (*str++);
11272
11273 /* Fill up with spaces until FIELD_WIDTH reached. */
11274 while (field_width > 0
11275 && n < field_width)
11276 {
11277 store_mode_line_noprop_char (' ');
11278 ++n;
11279 }
11280
11281 return n;
11282 }
11283
11284 /***********************************************************************
11285 Frame Titles
11286 ***********************************************************************/
11287
11288 #ifdef HAVE_WINDOW_SYSTEM
11289
11290 /* Set the title of FRAME, if it has changed. The title format is
11291 Vicon_title_format if FRAME is iconified, otherwise it is
11292 frame_title_format. */
11293
11294 static void
11295 x_consider_frame_title (Lisp_Object frame)
11296 {
11297 struct frame *f = XFRAME (frame);
11298
11299 if (FRAME_WINDOW_P (f)
11300 || FRAME_MINIBUF_ONLY_P (f)
11301 || f->explicit_name)
11302 {
11303 /* Do we have more than one visible frame on this X display? */
11304 Lisp_Object tail, other_frame, fmt;
11305 ptrdiff_t title_start;
11306 char *title;
11307 ptrdiff_t len;
11308 struct it it;
11309 ptrdiff_t count = SPECPDL_INDEX ();
11310
11311 FOR_EACH_FRAME (tail, other_frame)
11312 {
11313 struct frame *tf = XFRAME (other_frame);
11314
11315 if (tf != f
11316 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11317 && !FRAME_MINIBUF_ONLY_P (tf)
11318 && !EQ (other_frame, tip_frame)
11319 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11320 break;
11321 }
11322
11323 /* Set global variable indicating that multiple frames exist. */
11324 multiple_frames = CONSP (tail);
11325
11326 /* Switch to the buffer of selected window of the frame. Set up
11327 mode_line_target so that display_mode_element will output into
11328 mode_line_noprop_buf; then display the title. */
11329 record_unwind_protect (unwind_format_mode_line,
11330 format_mode_line_unwind_data
11331 (f, current_buffer, selected_window, 0));
11332
11333 Fselect_window (f->selected_window, Qt);
11334 set_buffer_internal_1
11335 (XBUFFER (XWINDOW (f->selected_window)->contents));
11336 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11337
11338 mode_line_target = MODE_LINE_TITLE;
11339 title_start = MODE_LINE_NOPROP_LEN (0);
11340 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11341 NULL, DEFAULT_FACE_ID);
11342 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11343 len = MODE_LINE_NOPROP_LEN (title_start);
11344 title = mode_line_noprop_buf + title_start;
11345 unbind_to (count, Qnil);
11346
11347 /* Set the title only if it's changed. This avoids consing in
11348 the common case where it hasn't. (If it turns out that we've
11349 already wasted too much time by walking through the list with
11350 display_mode_element, then we might need to optimize at a
11351 higher level than this.) */
11352 if (! STRINGP (f->name)
11353 || SBYTES (f->name) != len
11354 || memcmp (title, SDATA (f->name), len) != 0)
11355 x_implicitly_set_name (f, make_string (title, len), Qnil);
11356 }
11357 }
11358
11359 #endif /* not HAVE_WINDOW_SYSTEM */
11360
11361 \f
11362 /***********************************************************************
11363 Menu Bars
11364 ***********************************************************************/
11365
11366 /* Non-zero if we will not redisplay all visible windows. */
11367 #define REDISPLAY_SOME_P() \
11368 ((windows_or_buffers_changed == 0 \
11369 || windows_or_buffers_changed == REDISPLAY_SOME) \
11370 && (update_mode_lines == 0 \
11371 || update_mode_lines == REDISPLAY_SOME))
11372
11373 /* Prepare for redisplay by updating menu-bar item lists when
11374 appropriate. This can call eval. */
11375
11376 static void
11377 prepare_menu_bars (void)
11378 {
11379 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11380 bool some_windows = REDISPLAY_SOME_P ();
11381 struct gcpro gcpro1, gcpro2;
11382 Lisp_Object tooltip_frame;
11383
11384 #ifdef HAVE_WINDOW_SYSTEM
11385 tooltip_frame = tip_frame;
11386 #else
11387 tooltip_frame = Qnil;
11388 #endif
11389
11390 if (FUNCTIONP (Vpre_redisplay_function))
11391 {
11392 Lisp_Object windows = all_windows ? Qt : Qnil;
11393 if (all_windows && some_windows)
11394 {
11395 Lisp_Object ws = window_list ();
11396 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11397 {
11398 Lisp_Object this = XCAR (ws);
11399 struct window *w = XWINDOW (this);
11400 if (w->redisplay
11401 || XFRAME (w->frame)->redisplay
11402 || XBUFFER (w->contents)->text->redisplay)
11403 {
11404 windows = Fcons (this, windows);
11405 }
11406 }
11407 }
11408 safe_call1 (Vpre_redisplay_function, windows);
11409 }
11410
11411 /* Update all frame titles based on their buffer names, etc. We do
11412 this before the menu bars so that the buffer-menu will show the
11413 up-to-date frame titles. */
11414 #ifdef HAVE_WINDOW_SYSTEM
11415 if (all_windows)
11416 {
11417 Lisp_Object tail, frame;
11418
11419 FOR_EACH_FRAME (tail, frame)
11420 {
11421 struct frame *f = XFRAME (frame);
11422 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11423 if (some_windows
11424 && !f->redisplay
11425 && !w->redisplay
11426 && !XBUFFER (w->contents)->text->redisplay)
11427 continue;
11428
11429 if (!EQ (frame, tooltip_frame)
11430 && (FRAME_ICONIFIED_P (f)
11431 || FRAME_VISIBLE_P (f) == 1
11432 /* Exclude TTY frames that are obscured because they
11433 are not the top frame on their console. This is
11434 because x_consider_frame_title actually switches
11435 to the frame, which for TTY frames means it is
11436 marked as garbaged, and will be completely
11437 redrawn on the next redisplay cycle. This causes
11438 TTY frames to be completely redrawn, when there
11439 are more than one of them, even though nothing
11440 should be changed on display. */
11441 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11442 x_consider_frame_title (frame);
11443 }
11444 }
11445 #endif /* HAVE_WINDOW_SYSTEM */
11446
11447 /* Update the menu bar item lists, if appropriate. This has to be
11448 done before any actual redisplay or generation of display lines. */
11449
11450 if (all_windows)
11451 {
11452 Lisp_Object tail, frame;
11453 ptrdiff_t count = SPECPDL_INDEX ();
11454 /* 1 means that update_menu_bar has run its hooks
11455 so any further calls to update_menu_bar shouldn't do so again. */
11456 int menu_bar_hooks_run = 0;
11457
11458 record_unwind_save_match_data ();
11459
11460 FOR_EACH_FRAME (tail, frame)
11461 {
11462 struct frame *f = XFRAME (frame);
11463 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11464
11465 /* Ignore tooltip frame. */
11466 if (EQ (frame, tooltip_frame))
11467 continue;
11468
11469 if (some_windows
11470 && !f->redisplay
11471 && !w->redisplay
11472 && !XBUFFER (w->contents)->text->redisplay)
11473 continue;
11474
11475 /* If a window on this frame changed size, report that to
11476 the user and clear the size-change flag. */
11477 if (FRAME_WINDOW_SIZES_CHANGED (f))
11478 {
11479 Lisp_Object functions;
11480
11481 /* Clear flag first in case we get an error below. */
11482 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11483 functions = Vwindow_size_change_functions;
11484 GCPRO2 (tail, functions);
11485
11486 while (CONSP (functions))
11487 {
11488 if (!EQ (XCAR (functions), Qt))
11489 call1 (XCAR (functions), frame);
11490 functions = XCDR (functions);
11491 }
11492 UNGCPRO;
11493 }
11494
11495 GCPRO1 (tail);
11496 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11497 #ifdef HAVE_WINDOW_SYSTEM
11498 update_tool_bar (f, 0);
11499 #endif
11500 #ifdef HAVE_NS
11501 if (windows_or_buffers_changed
11502 && FRAME_NS_P (f))
11503 ns_set_doc_edited
11504 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11505 #endif
11506 UNGCPRO;
11507 }
11508
11509 unbind_to (count, Qnil);
11510 }
11511 else
11512 {
11513 struct frame *sf = SELECTED_FRAME ();
11514 update_menu_bar (sf, 1, 0);
11515 #ifdef HAVE_WINDOW_SYSTEM
11516 update_tool_bar (sf, 1);
11517 #endif
11518 }
11519 }
11520
11521
11522 /* Update the menu bar item list for frame F. This has to be done
11523 before we start to fill in any display lines, because it can call
11524 eval.
11525
11526 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11527
11528 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11529 already ran the menu bar hooks for this redisplay, so there
11530 is no need to run them again. The return value is the
11531 updated value of this flag, to pass to the next call. */
11532
11533 static int
11534 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11535 {
11536 Lisp_Object window;
11537 register struct window *w;
11538
11539 /* If called recursively during a menu update, do nothing. This can
11540 happen when, for instance, an activate-menubar-hook causes a
11541 redisplay. */
11542 if (inhibit_menubar_update)
11543 return hooks_run;
11544
11545 window = FRAME_SELECTED_WINDOW (f);
11546 w = XWINDOW (window);
11547
11548 if (FRAME_WINDOW_P (f)
11549 ?
11550 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11551 || defined (HAVE_NS) || defined (USE_GTK)
11552 FRAME_EXTERNAL_MENU_BAR (f)
11553 #else
11554 FRAME_MENU_BAR_LINES (f) > 0
11555 #endif
11556 : FRAME_MENU_BAR_LINES (f) > 0)
11557 {
11558 /* If the user has switched buffers or windows, we need to
11559 recompute to reflect the new bindings. But we'll
11560 recompute when update_mode_lines is set too; that means
11561 that people can use force-mode-line-update to request
11562 that the menu bar be recomputed. The adverse effect on
11563 the rest of the redisplay algorithm is about the same as
11564 windows_or_buffers_changed anyway. */
11565 if (windows_or_buffers_changed
11566 /* This used to test w->update_mode_line, but we believe
11567 there is no need to recompute the menu in that case. */
11568 || update_mode_lines
11569 || window_buffer_changed (w))
11570 {
11571 struct buffer *prev = current_buffer;
11572 ptrdiff_t count = SPECPDL_INDEX ();
11573
11574 specbind (Qinhibit_menubar_update, Qt);
11575
11576 set_buffer_internal_1 (XBUFFER (w->contents));
11577 if (save_match_data)
11578 record_unwind_save_match_data ();
11579 if (NILP (Voverriding_local_map_menu_flag))
11580 {
11581 specbind (Qoverriding_terminal_local_map, Qnil);
11582 specbind (Qoverriding_local_map, Qnil);
11583 }
11584
11585 if (!hooks_run)
11586 {
11587 /* Run the Lucid hook. */
11588 safe_run_hooks (Qactivate_menubar_hook);
11589
11590 /* If it has changed current-menubar from previous value,
11591 really recompute the menu-bar from the value. */
11592 if (! NILP (Vlucid_menu_bar_dirty_flag))
11593 call0 (Qrecompute_lucid_menubar);
11594
11595 safe_run_hooks (Qmenu_bar_update_hook);
11596
11597 hooks_run = 1;
11598 }
11599
11600 XSETFRAME (Vmenu_updating_frame, f);
11601 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11602
11603 /* Redisplay the menu bar in case we changed it. */
11604 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11605 || defined (HAVE_NS) || defined (USE_GTK)
11606 if (FRAME_WINDOW_P (f))
11607 {
11608 #if defined (HAVE_NS)
11609 /* All frames on Mac OS share the same menubar. So only
11610 the selected frame should be allowed to set it. */
11611 if (f == SELECTED_FRAME ())
11612 #endif
11613 set_frame_menubar (f, 0, 0);
11614 }
11615 else
11616 /* On a terminal screen, the menu bar is an ordinary screen
11617 line, and this makes it get updated. */
11618 w->update_mode_line = 1;
11619 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11620 /* In the non-toolkit version, the menu bar is an ordinary screen
11621 line, and this makes it get updated. */
11622 w->update_mode_line = 1;
11623 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11624
11625 unbind_to (count, Qnil);
11626 set_buffer_internal_1 (prev);
11627 }
11628 }
11629
11630 return hooks_run;
11631 }
11632
11633 /***********************************************************************
11634 Tool-bars
11635 ***********************************************************************/
11636
11637 #ifdef HAVE_WINDOW_SYSTEM
11638
11639 /* Tool-bar item index of the item on which a mouse button was pressed
11640 or -1. */
11641
11642 int last_tool_bar_item;
11643
11644 /* Select `frame' temporarily without running all the code in
11645 do_switch_frame.
11646 FIXME: Maybe do_switch_frame should be trimmed down similarly
11647 when `norecord' is set. */
11648 static void
11649 fast_set_selected_frame (Lisp_Object frame)
11650 {
11651 if (!EQ (selected_frame, frame))
11652 {
11653 selected_frame = frame;
11654 selected_window = XFRAME (frame)->selected_window;
11655 }
11656 }
11657
11658 /* Update the tool-bar item list for frame F. This has to be done
11659 before we start to fill in any display lines. Called from
11660 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11661 and restore it here. */
11662
11663 static void
11664 update_tool_bar (struct frame *f, int save_match_data)
11665 {
11666 #if defined (USE_GTK) || defined (HAVE_NS)
11667 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11668 #else
11669 int do_update = (WINDOWP (f->tool_bar_window)
11670 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11671 #endif
11672
11673 if (do_update)
11674 {
11675 Lisp_Object window;
11676 struct window *w;
11677
11678 window = FRAME_SELECTED_WINDOW (f);
11679 w = XWINDOW (window);
11680
11681 /* If the user has switched buffers or windows, we need to
11682 recompute to reflect the new bindings. But we'll
11683 recompute when update_mode_lines is set too; that means
11684 that people can use force-mode-line-update to request
11685 that the menu bar be recomputed. The adverse effect on
11686 the rest of the redisplay algorithm is about the same as
11687 windows_or_buffers_changed anyway. */
11688 if (windows_or_buffers_changed
11689 || w->update_mode_line
11690 || update_mode_lines
11691 || window_buffer_changed (w))
11692 {
11693 struct buffer *prev = current_buffer;
11694 ptrdiff_t count = SPECPDL_INDEX ();
11695 Lisp_Object frame, new_tool_bar;
11696 int new_n_tool_bar;
11697 struct gcpro gcpro1;
11698
11699 /* Set current_buffer to the buffer of the selected
11700 window of the frame, so that we get the right local
11701 keymaps. */
11702 set_buffer_internal_1 (XBUFFER (w->contents));
11703
11704 /* Save match data, if we must. */
11705 if (save_match_data)
11706 record_unwind_save_match_data ();
11707
11708 /* Make sure that we don't accidentally use bogus keymaps. */
11709 if (NILP (Voverriding_local_map_menu_flag))
11710 {
11711 specbind (Qoverriding_terminal_local_map, Qnil);
11712 specbind (Qoverriding_local_map, Qnil);
11713 }
11714
11715 GCPRO1 (new_tool_bar);
11716
11717 /* We must temporarily set the selected frame to this frame
11718 before calling tool_bar_items, because the calculation of
11719 the tool-bar keymap uses the selected frame (see
11720 `tool-bar-make-keymap' in tool-bar.el). */
11721 eassert (EQ (selected_window,
11722 /* Since we only explicitly preserve selected_frame,
11723 check that selected_window would be redundant. */
11724 XFRAME (selected_frame)->selected_window));
11725 record_unwind_protect (fast_set_selected_frame, selected_frame);
11726 XSETFRAME (frame, f);
11727 fast_set_selected_frame (frame);
11728
11729 /* Build desired tool-bar items from keymaps. */
11730 new_tool_bar
11731 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11732 &new_n_tool_bar);
11733
11734 /* Redisplay the tool-bar if we changed it. */
11735 if (new_n_tool_bar != f->n_tool_bar_items
11736 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11737 {
11738 /* Redisplay that happens asynchronously due to an expose event
11739 may access f->tool_bar_items. Make sure we update both
11740 variables within BLOCK_INPUT so no such event interrupts. */
11741 block_input ();
11742 fset_tool_bar_items (f, new_tool_bar);
11743 f->n_tool_bar_items = new_n_tool_bar;
11744 w->update_mode_line = 1;
11745 unblock_input ();
11746 }
11747
11748 UNGCPRO;
11749
11750 unbind_to (count, Qnil);
11751 set_buffer_internal_1 (prev);
11752 }
11753 }
11754 }
11755
11756 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11757
11758 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11759 F's desired tool-bar contents. F->tool_bar_items must have
11760 been set up previously by calling prepare_menu_bars. */
11761
11762 static void
11763 build_desired_tool_bar_string (struct frame *f)
11764 {
11765 int i, size, size_needed;
11766 struct gcpro gcpro1, gcpro2, gcpro3;
11767 Lisp_Object image, plist, props;
11768
11769 image = plist = props = Qnil;
11770 GCPRO3 (image, plist, props);
11771
11772 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11773 Otherwise, make a new string. */
11774
11775 /* The size of the string we might be able to reuse. */
11776 size = (STRINGP (f->desired_tool_bar_string)
11777 ? SCHARS (f->desired_tool_bar_string)
11778 : 0);
11779
11780 /* We need one space in the string for each image. */
11781 size_needed = f->n_tool_bar_items;
11782
11783 /* Reuse f->desired_tool_bar_string, if possible. */
11784 if (size < size_needed || NILP (f->desired_tool_bar_string))
11785 fset_desired_tool_bar_string
11786 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11787 else
11788 {
11789 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11790 Fremove_text_properties (make_number (0), make_number (size),
11791 props, f->desired_tool_bar_string);
11792 }
11793
11794 /* Put a `display' property on the string for the images to display,
11795 put a `menu_item' property on tool-bar items with a value that
11796 is the index of the item in F's tool-bar item vector. */
11797 for (i = 0; i < f->n_tool_bar_items; ++i)
11798 {
11799 #define PROP(IDX) \
11800 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11801
11802 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11803 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11804 int hmargin, vmargin, relief, idx, end;
11805
11806 /* If image is a vector, choose the image according to the
11807 button state. */
11808 image = PROP (TOOL_BAR_ITEM_IMAGES);
11809 if (VECTORP (image))
11810 {
11811 if (enabled_p)
11812 idx = (selected_p
11813 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11814 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11815 else
11816 idx = (selected_p
11817 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11818 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11819
11820 eassert (ASIZE (image) >= idx);
11821 image = AREF (image, idx);
11822 }
11823 else
11824 idx = -1;
11825
11826 /* Ignore invalid image specifications. */
11827 if (!valid_image_p (image))
11828 continue;
11829
11830 /* Display the tool-bar button pressed, or depressed. */
11831 plist = Fcopy_sequence (XCDR (image));
11832
11833 /* Compute margin and relief to draw. */
11834 relief = (tool_bar_button_relief >= 0
11835 ? tool_bar_button_relief
11836 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11837 hmargin = vmargin = relief;
11838
11839 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11840 INT_MAX - max (hmargin, vmargin)))
11841 {
11842 hmargin += XFASTINT (Vtool_bar_button_margin);
11843 vmargin += XFASTINT (Vtool_bar_button_margin);
11844 }
11845 else if (CONSP (Vtool_bar_button_margin))
11846 {
11847 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11848 INT_MAX - hmargin))
11849 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11850
11851 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11852 INT_MAX - vmargin))
11853 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11854 }
11855
11856 if (auto_raise_tool_bar_buttons_p)
11857 {
11858 /* Add a `:relief' property to the image spec if the item is
11859 selected. */
11860 if (selected_p)
11861 {
11862 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11863 hmargin -= relief;
11864 vmargin -= relief;
11865 }
11866 }
11867 else
11868 {
11869 /* If image is selected, display it pressed, i.e. with a
11870 negative relief. If it's not selected, display it with a
11871 raised relief. */
11872 plist = Fplist_put (plist, QCrelief,
11873 (selected_p
11874 ? make_number (-relief)
11875 : make_number (relief)));
11876 hmargin -= relief;
11877 vmargin -= relief;
11878 }
11879
11880 /* Put a margin around the image. */
11881 if (hmargin || vmargin)
11882 {
11883 if (hmargin == vmargin)
11884 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11885 else
11886 plist = Fplist_put (plist, QCmargin,
11887 Fcons (make_number (hmargin),
11888 make_number (vmargin)));
11889 }
11890
11891 /* If button is not enabled, and we don't have special images
11892 for the disabled state, make the image appear disabled by
11893 applying an appropriate algorithm to it. */
11894 if (!enabled_p && idx < 0)
11895 plist = Fplist_put (plist, QCconversion, Qdisabled);
11896
11897 /* Put a `display' text property on the string for the image to
11898 display. Put a `menu-item' property on the string that gives
11899 the start of this item's properties in the tool-bar items
11900 vector. */
11901 image = Fcons (Qimage, plist);
11902 props = list4 (Qdisplay, image,
11903 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11904
11905 /* Let the last image hide all remaining spaces in the tool bar
11906 string. The string can be longer than needed when we reuse a
11907 previous string. */
11908 if (i + 1 == f->n_tool_bar_items)
11909 end = SCHARS (f->desired_tool_bar_string);
11910 else
11911 end = i + 1;
11912 Fadd_text_properties (make_number (i), make_number (end),
11913 props, f->desired_tool_bar_string);
11914 #undef PROP
11915 }
11916
11917 UNGCPRO;
11918 }
11919
11920
11921 /* Display one line of the tool-bar of frame IT->f.
11922
11923 HEIGHT specifies the desired height of the tool-bar line.
11924 If the actual height of the glyph row is less than HEIGHT, the
11925 row's height is increased to HEIGHT, and the icons are centered
11926 vertically in the new height.
11927
11928 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11929 count a final empty row in case the tool-bar width exactly matches
11930 the window width.
11931 */
11932
11933 static void
11934 display_tool_bar_line (struct it *it, int height)
11935 {
11936 struct glyph_row *row = it->glyph_row;
11937 int max_x = it->last_visible_x;
11938 struct glyph *last;
11939
11940 prepare_desired_row (row);
11941 row->y = it->current_y;
11942
11943 /* Note that this isn't made use of if the face hasn't a box,
11944 so there's no need to check the face here. */
11945 it->start_of_box_run_p = 1;
11946
11947 while (it->current_x < max_x)
11948 {
11949 int x, n_glyphs_before, i, nglyphs;
11950 struct it it_before;
11951
11952 /* Get the next display element. */
11953 if (!get_next_display_element (it))
11954 {
11955 /* Don't count empty row if we are counting needed tool-bar lines. */
11956 if (height < 0 && !it->hpos)
11957 return;
11958 break;
11959 }
11960
11961 /* Produce glyphs. */
11962 n_glyphs_before = row->used[TEXT_AREA];
11963 it_before = *it;
11964
11965 PRODUCE_GLYPHS (it);
11966
11967 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11968 i = 0;
11969 x = it_before.current_x;
11970 while (i < nglyphs)
11971 {
11972 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11973
11974 if (x + glyph->pixel_width > max_x)
11975 {
11976 /* Glyph doesn't fit on line. Backtrack. */
11977 row->used[TEXT_AREA] = n_glyphs_before;
11978 *it = it_before;
11979 /* If this is the only glyph on this line, it will never fit on the
11980 tool-bar, so skip it. But ensure there is at least one glyph,
11981 so we don't accidentally disable the tool-bar. */
11982 if (n_glyphs_before == 0
11983 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11984 break;
11985 goto out;
11986 }
11987
11988 ++it->hpos;
11989 x += glyph->pixel_width;
11990 ++i;
11991 }
11992
11993 /* Stop at line end. */
11994 if (ITERATOR_AT_END_OF_LINE_P (it))
11995 break;
11996
11997 set_iterator_to_next (it, 1);
11998 }
11999
12000 out:;
12001
12002 row->displays_text_p = row->used[TEXT_AREA] != 0;
12003
12004 /* Use default face for the border below the tool bar.
12005
12006 FIXME: When auto-resize-tool-bars is grow-only, there is
12007 no additional border below the possibly empty tool-bar lines.
12008 So to make the extra empty lines look "normal", we have to
12009 use the tool-bar face for the border too. */
12010 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12011 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12012 it->face_id = DEFAULT_FACE_ID;
12013
12014 extend_face_to_end_of_line (it);
12015 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12016 last->right_box_line_p = 1;
12017 if (last == row->glyphs[TEXT_AREA])
12018 last->left_box_line_p = 1;
12019
12020 /* Make line the desired height and center it vertically. */
12021 if ((height -= it->max_ascent + it->max_descent) > 0)
12022 {
12023 /* Don't add more than one line height. */
12024 height %= FRAME_LINE_HEIGHT (it->f);
12025 it->max_ascent += height / 2;
12026 it->max_descent += (height + 1) / 2;
12027 }
12028
12029 compute_line_metrics (it);
12030
12031 /* If line is empty, make it occupy the rest of the tool-bar. */
12032 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12033 {
12034 row->height = row->phys_height = it->last_visible_y - row->y;
12035 row->visible_height = row->height;
12036 row->ascent = row->phys_ascent = 0;
12037 row->extra_line_spacing = 0;
12038 }
12039
12040 row->full_width_p = 1;
12041 row->continued_p = 0;
12042 row->truncated_on_left_p = 0;
12043 row->truncated_on_right_p = 0;
12044
12045 it->current_x = it->hpos = 0;
12046 it->current_y += row->height;
12047 ++it->vpos;
12048 ++it->glyph_row;
12049 }
12050
12051
12052 /* Max tool-bar height. Basically, this is what makes all other windows
12053 disappear when the frame gets too small. Rethink this! */
12054
12055 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12056 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12057
12058 /* Value is the number of screen lines needed to make all tool-bar
12059 items of frame F visible. The number of actual rows needed is
12060 returned in *N_ROWS if non-NULL. */
12061
12062 static int
12063 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12064 {
12065 struct window *w = XWINDOW (f->tool_bar_window);
12066 struct it it;
12067 /* tool_bar_height is called from redisplay_tool_bar after building
12068 the desired matrix, so use (unused) mode-line row as temporary row to
12069 avoid destroying the first tool-bar row. */
12070 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12071
12072 /* Initialize an iterator for iteration over
12073 F->desired_tool_bar_string in the tool-bar window of frame F. */
12074 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12075 it.first_visible_x = 0;
12076 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
12077 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12078 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12079 it.paragraph_embedding = L2R;
12080
12081 while (!ITERATOR_AT_END_P (&it))
12082 {
12083 clear_glyph_row (temp_row);
12084 it.glyph_row = temp_row;
12085 display_tool_bar_line (&it, -1);
12086 }
12087 clear_glyph_row (temp_row);
12088
12089 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12090 if (n_rows)
12091 *n_rows = it.vpos > 0 ? it.vpos : -1;
12092
12093 if (pixelwise)
12094 return it.current_y;
12095 else
12096 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12097 }
12098
12099 #endif /* !USE_GTK && !HAVE_NS */
12100
12101 #if defined USE_GTK || defined HAVE_NS
12102 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12103 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12104 #endif
12105
12106 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12107 0, 2, 0,
12108 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12109 If FRAME is nil or omitted, use the selected frame. Optional argument
12110 PIXELWISE non-nil means return the height of the tool bar inpixels. */)
12111 (Lisp_Object frame, Lisp_Object pixelwise)
12112 {
12113 int height = 0;
12114
12115 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12116 struct frame *f = decode_any_frame (frame);
12117
12118 if (WINDOWP (f->tool_bar_window)
12119 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12120 {
12121 update_tool_bar (f, 1);
12122 if (f->n_tool_bar_items)
12123 {
12124 build_desired_tool_bar_string (f);
12125 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12126 }
12127 }
12128 #endif
12129
12130 return make_number (height);
12131 }
12132
12133
12134 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12135 height should be changed. */
12136
12137 static int
12138 redisplay_tool_bar (struct frame *f)
12139 {
12140 #if defined (USE_GTK) || defined (HAVE_NS)
12141
12142 if (FRAME_EXTERNAL_TOOL_BAR (f))
12143 update_frame_tool_bar (f);
12144 return 0;
12145
12146 #else /* !USE_GTK && !HAVE_NS */
12147
12148 struct window *w;
12149 struct it it;
12150 struct glyph_row *row;
12151
12152 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12153 do anything. This means you must start with tool-bar-lines
12154 non-zero to get the auto-sizing effect. Or in other words, you
12155 can turn off tool-bars by specifying tool-bar-lines zero. */
12156 if (!WINDOWP (f->tool_bar_window)
12157 || (w = XWINDOW (f->tool_bar_window),
12158 WINDOW_PIXEL_HEIGHT (w) == 0))
12159 return 0;
12160
12161 /* Set up an iterator for the tool-bar window. */
12162 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12163 it.first_visible_x = 0;
12164 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12165 row = it.glyph_row;
12166
12167 /* Build a string that represents the contents of the tool-bar. */
12168 build_desired_tool_bar_string (f);
12169 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12170 /* FIXME: This should be controlled by a user option. But it
12171 doesn't make sense to have an R2L tool bar if the menu bar cannot
12172 be drawn also R2L, and making the menu bar R2L is tricky due
12173 toolkit-specific code that implements it. If an R2L tool bar is
12174 ever supported, display_tool_bar_line should also be augmented to
12175 call unproduce_glyphs like display_line and display_string
12176 do. */
12177 it.paragraph_embedding = L2R;
12178
12179 if (f->n_tool_bar_rows == 0)
12180 {
12181 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12182
12183 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12184 {
12185 Lisp_Object frame;
12186 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12187 / FRAME_LINE_HEIGHT (f));
12188
12189 XSETFRAME (frame, f);
12190 Fmodify_frame_parameters (frame,
12191 list1 (Fcons (Qtool_bar_lines,
12192 make_number (new_lines))));
12193 /* Always do that now. */
12194 clear_glyph_matrix (w->desired_matrix);
12195 f->fonts_changed = 1;
12196 return 1;
12197 }
12198 }
12199
12200 /* Display as many lines as needed to display all tool-bar items. */
12201
12202 if (f->n_tool_bar_rows > 0)
12203 {
12204 int border, rows, height, extra;
12205
12206 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12207 border = XINT (Vtool_bar_border);
12208 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12209 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12210 else if (EQ (Vtool_bar_border, Qborder_width))
12211 border = f->border_width;
12212 else
12213 border = 0;
12214 if (border < 0)
12215 border = 0;
12216
12217 rows = f->n_tool_bar_rows;
12218 height = max (1, (it.last_visible_y - border) / rows);
12219 extra = it.last_visible_y - border - height * rows;
12220
12221 while (it.current_y < it.last_visible_y)
12222 {
12223 int h = 0;
12224 if (extra > 0 && rows-- > 0)
12225 {
12226 h = (extra + rows - 1) / rows;
12227 extra -= h;
12228 }
12229 display_tool_bar_line (&it, height + h);
12230 }
12231 }
12232 else
12233 {
12234 while (it.current_y < it.last_visible_y)
12235 display_tool_bar_line (&it, 0);
12236 }
12237
12238 /* It doesn't make much sense to try scrolling in the tool-bar
12239 window, so don't do it. */
12240 w->desired_matrix->no_scrolling_p = 1;
12241 w->must_be_updated_p = 1;
12242
12243 if (!NILP (Vauto_resize_tool_bars))
12244 {
12245 /* Do we really allow the toolbar to occupy the whole frame? */
12246 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12247 int change_height_p = 0;
12248
12249 /* If we couldn't display everything, change the tool-bar's
12250 height if there is room for more. */
12251 if (IT_STRING_CHARPOS (it) < it.end_charpos
12252 && it.current_y < max_tool_bar_height)
12253 change_height_p = 1;
12254
12255 row = it.glyph_row - 1;
12256
12257 /* If there are blank lines at the end, except for a partially
12258 visible blank line at the end that is smaller than
12259 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12260 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12261 && row->height >= FRAME_LINE_HEIGHT (f))
12262 change_height_p = 1;
12263
12264 /* If row displays tool-bar items, but is partially visible,
12265 change the tool-bar's height. */
12266 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12267 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12268 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12269 change_height_p = 1;
12270
12271 /* Resize windows as needed by changing the `tool-bar-lines'
12272 frame parameter. */
12273 if (change_height_p)
12274 {
12275 Lisp_Object frame;
12276 int nrows;
12277 int new_height = tool_bar_height (f, &nrows, 1);
12278
12279 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12280 && !f->minimize_tool_bar_window_p)
12281 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12282 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12283 f->minimize_tool_bar_window_p = 0;
12284
12285 if (change_height_p)
12286 {
12287 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12288 / FRAME_LINE_HEIGHT (f));
12289
12290 XSETFRAME (frame, f);
12291 Fmodify_frame_parameters (frame,
12292 list1 (Fcons (Qtool_bar_lines,
12293 make_number (new_lines))));
12294 /* Always do that now. */
12295 clear_glyph_matrix (w->desired_matrix);
12296 f->n_tool_bar_rows = nrows;
12297 f->fonts_changed = 1;
12298 return 1;
12299 }
12300 }
12301 }
12302
12303 f->minimize_tool_bar_window_p = 0;
12304 return 0;
12305
12306 #endif /* USE_GTK || HAVE_NS */
12307 }
12308
12309 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12310
12311 /* Get information about the tool-bar item which is displayed in GLYPH
12312 on frame F. Return in *PROP_IDX the index where tool-bar item
12313 properties start in F->tool_bar_items. Value is zero if
12314 GLYPH doesn't display a tool-bar item. */
12315
12316 static int
12317 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12318 {
12319 Lisp_Object prop;
12320 int success_p;
12321 int charpos;
12322
12323 /* This function can be called asynchronously, which means we must
12324 exclude any possibility that Fget_text_property signals an
12325 error. */
12326 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12327 charpos = max (0, charpos);
12328
12329 /* Get the text property `menu-item' at pos. The value of that
12330 property is the start index of this item's properties in
12331 F->tool_bar_items. */
12332 prop = Fget_text_property (make_number (charpos),
12333 Qmenu_item, f->current_tool_bar_string);
12334 if (INTEGERP (prop))
12335 {
12336 *prop_idx = XINT (prop);
12337 success_p = 1;
12338 }
12339 else
12340 success_p = 0;
12341
12342 return success_p;
12343 }
12344
12345 \f
12346 /* Get information about the tool-bar item at position X/Y on frame F.
12347 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12348 the current matrix of the tool-bar window of F, or NULL if not
12349 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12350 item in F->tool_bar_items. Value is
12351
12352 -1 if X/Y is not on a tool-bar item
12353 0 if X/Y is on the same item that was highlighted before.
12354 1 otherwise. */
12355
12356 static int
12357 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12358 int *hpos, int *vpos, int *prop_idx)
12359 {
12360 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12361 struct window *w = XWINDOW (f->tool_bar_window);
12362 int area;
12363
12364 /* Find the glyph under X/Y. */
12365 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12366 if (*glyph == NULL)
12367 return -1;
12368
12369 /* Get the start of this tool-bar item's properties in
12370 f->tool_bar_items. */
12371 if (!tool_bar_item_info (f, *glyph, prop_idx))
12372 return -1;
12373
12374 /* Is mouse on the highlighted item? */
12375 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12376 && *vpos >= hlinfo->mouse_face_beg_row
12377 && *vpos <= hlinfo->mouse_face_end_row
12378 && (*vpos > hlinfo->mouse_face_beg_row
12379 || *hpos >= hlinfo->mouse_face_beg_col)
12380 && (*vpos < hlinfo->mouse_face_end_row
12381 || *hpos < hlinfo->mouse_face_end_col
12382 || hlinfo->mouse_face_past_end))
12383 return 0;
12384
12385 return 1;
12386 }
12387
12388
12389 /* EXPORT:
12390 Handle mouse button event on the tool-bar of frame F, at
12391 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12392 0 for button release. MODIFIERS is event modifiers for button
12393 release. */
12394
12395 void
12396 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12397 int modifiers)
12398 {
12399 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12400 struct window *w = XWINDOW (f->tool_bar_window);
12401 int hpos, vpos, prop_idx;
12402 struct glyph *glyph;
12403 Lisp_Object enabled_p;
12404 int ts;
12405
12406 /* If not on the highlighted tool-bar item, and mouse-highlight is
12407 non-nil, return. This is so we generate the tool-bar button
12408 click only when the mouse button is released on the same item as
12409 where it was pressed. However, when mouse-highlight is disabled,
12410 generate the click when the button is released regardless of the
12411 highlight, since tool-bar items are not highlighted in that
12412 case. */
12413 frame_to_window_pixel_xy (w, &x, &y);
12414 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12415 if (ts == -1
12416 || (ts != 0 && !NILP (Vmouse_highlight)))
12417 return;
12418
12419 /* When mouse-highlight is off, generate the click for the item
12420 where the button was pressed, disregarding where it was
12421 released. */
12422 if (NILP (Vmouse_highlight) && !down_p)
12423 prop_idx = last_tool_bar_item;
12424
12425 /* If item is disabled, do nothing. */
12426 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12427 if (NILP (enabled_p))
12428 return;
12429
12430 if (down_p)
12431 {
12432 /* Show item in pressed state. */
12433 if (!NILP (Vmouse_highlight))
12434 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12435 last_tool_bar_item = prop_idx;
12436 }
12437 else
12438 {
12439 Lisp_Object key, frame;
12440 struct input_event event;
12441 EVENT_INIT (event);
12442
12443 /* Show item in released state. */
12444 if (!NILP (Vmouse_highlight))
12445 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12446
12447 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12448
12449 XSETFRAME (frame, f);
12450 event.kind = TOOL_BAR_EVENT;
12451 event.frame_or_window = frame;
12452 event.arg = frame;
12453 kbd_buffer_store_event (&event);
12454
12455 event.kind = TOOL_BAR_EVENT;
12456 event.frame_or_window = frame;
12457 event.arg = key;
12458 event.modifiers = modifiers;
12459 kbd_buffer_store_event (&event);
12460 last_tool_bar_item = -1;
12461 }
12462 }
12463
12464
12465 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12466 tool-bar window-relative coordinates X/Y. Called from
12467 note_mouse_highlight. */
12468
12469 static void
12470 note_tool_bar_highlight (struct frame *f, int x, int y)
12471 {
12472 Lisp_Object window = f->tool_bar_window;
12473 struct window *w = XWINDOW (window);
12474 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12475 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12476 int hpos, vpos;
12477 struct glyph *glyph;
12478 struct glyph_row *row;
12479 int i;
12480 Lisp_Object enabled_p;
12481 int prop_idx;
12482 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12483 int mouse_down_p, rc;
12484
12485 /* Function note_mouse_highlight is called with negative X/Y
12486 values when mouse moves outside of the frame. */
12487 if (x <= 0 || y <= 0)
12488 {
12489 clear_mouse_face (hlinfo);
12490 return;
12491 }
12492
12493 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12494 if (rc < 0)
12495 {
12496 /* Not on tool-bar item. */
12497 clear_mouse_face (hlinfo);
12498 return;
12499 }
12500 else if (rc == 0)
12501 /* On same tool-bar item as before. */
12502 goto set_help_echo;
12503
12504 clear_mouse_face (hlinfo);
12505
12506 /* Mouse is down, but on different tool-bar item? */
12507 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12508 && f == dpyinfo->last_mouse_frame);
12509
12510 if (mouse_down_p
12511 && last_tool_bar_item != prop_idx)
12512 return;
12513
12514 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12515
12516 /* If tool-bar item is not enabled, don't highlight it. */
12517 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12518 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12519 {
12520 /* Compute the x-position of the glyph. In front and past the
12521 image is a space. We include this in the highlighted area. */
12522 row = MATRIX_ROW (w->current_matrix, vpos);
12523 for (i = x = 0; i < hpos; ++i)
12524 x += row->glyphs[TEXT_AREA][i].pixel_width;
12525
12526 /* Record this as the current active region. */
12527 hlinfo->mouse_face_beg_col = hpos;
12528 hlinfo->mouse_face_beg_row = vpos;
12529 hlinfo->mouse_face_beg_x = x;
12530 hlinfo->mouse_face_past_end = 0;
12531
12532 hlinfo->mouse_face_end_col = hpos + 1;
12533 hlinfo->mouse_face_end_row = vpos;
12534 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12535 hlinfo->mouse_face_window = window;
12536 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12537
12538 /* Display it as active. */
12539 show_mouse_face (hlinfo, draw);
12540 }
12541
12542 set_help_echo:
12543
12544 /* Set help_echo_string to a help string to display for this tool-bar item.
12545 XTread_socket does the rest. */
12546 help_echo_object = help_echo_window = Qnil;
12547 help_echo_pos = -1;
12548 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12549 if (NILP (help_echo_string))
12550 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12551 }
12552
12553 #endif /* !USE_GTK && !HAVE_NS */
12554
12555 #endif /* HAVE_WINDOW_SYSTEM */
12556
12557
12558 \f
12559 /************************************************************************
12560 Horizontal scrolling
12561 ************************************************************************/
12562
12563 static int hscroll_window_tree (Lisp_Object);
12564 static int hscroll_windows (Lisp_Object);
12565
12566 /* For all leaf windows in the window tree rooted at WINDOW, set their
12567 hscroll value so that PT is (i) visible in the window, and (ii) so
12568 that it is not within a certain margin at the window's left and
12569 right border. Value is non-zero if any window's hscroll has been
12570 changed. */
12571
12572 static int
12573 hscroll_window_tree (Lisp_Object window)
12574 {
12575 int hscrolled_p = 0;
12576 int hscroll_relative_p = FLOATP (Vhscroll_step);
12577 int hscroll_step_abs = 0;
12578 double hscroll_step_rel = 0;
12579
12580 if (hscroll_relative_p)
12581 {
12582 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12583 if (hscroll_step_rel < 0)
12584 {
12585 hscroll_relative_p = 0;
12586 hscroll_step_abs = 0;
12587 }
12588 }
12589 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12590 {
12591 hscroll_step_abs = XINT (Vhscroll_step);
12592 if (hscroll_step_abs < 0)
12593 hscroll_step_abs = 0;
12594 }
12595 else
12596 hscroll_step_abs = 0;
12597
12598 while (WINDOWP (window))
12599 {
12600 struct window *w = XWINDOW (window);
12601
12602 if (WINDOWP (w->contents))
12603 hscrolled_p |= hscroll_window_tree (w->contents);
12604 else if (w->cursor.vpos >= 0)
12605 {
12606 int h_margin;
12607 int text_area_width;
12608 struct glyph_row *current_cursor_row
12609 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12610 struct glyph_row *desired_cursor_row
12611 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12612 struct glyph_row *cursor_row
12613 = (desired_cursor_row->enabled_p
12614 ? desired_cursor_row
12615 : current_cursor_row);
12616 int row_r2l_p = cursor_row->reversed_p;
12617
12618 text_area_width = window_box_width (w, TEXT_AREA);
12619
12620 /* Scroll when cursor is inside this scroll margin. */
12621 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12622
12623 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12624 /* For left-to-right rows, hscroll when cursor is either
12625 (i) inside the right hscroll margin, or (ii) if it is
12626 inside the left margin and the window is already
12627 hscrolled. */
12628 && ((!row_r2l_p
12629 && ((w->hscroll
12630 && w->cursor.x <= h_margin)
12631 || (cursor_row->enabled_p
12632 && cursor_row->truncated_on_right_p
12633 && (w->cursor.x >= text_area_width - h_margin))))
12634 /* For right-to-left rows, the logic is similar,
12635 except that rules for scrolling to left and right
12636 are reversed. E.g., if cursor.x <= h_margin, we
12637 need to hscroll "to the right" unconditionally,
12638 and that will scroll the screen to the left so as
12639 to reveal the next portion of the row. */
12640 || (row_r2l_p
12641 && ((cursor_row->enabled_p
12642 /* FIXME: It is confusing to set the
12643 truncated_on_right_p flag when R2L rows
12644 are actually truncated on the left. */
12645 && cursor_row->truncated_on_right_p
12646 && w->cursor.x <= h_margin)
12647 || (w->hscroll
12648 && (w->cursor.x >= text_area_width - h_margin))))))
12649 {
12650 struct it it;
12651 ptrdiff_t hscroll;
12652 struct buffer *saved_current_buffer;
12653 ptrdiff_t pt;
12654 int wanted_x;
12655
12656 /* Find point in a display of infinite width. */
12657 saved_current_buffer = current_buffer;
12658 current_buffer = XBUFFER (w->contents);
12659
12660 if (w == XWINDOW (selected_window))
12661 pt = PT;
12662 else
12663 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12664
12665 /* Move iterator to pt starting at cursor_row->start in
12666 a line with infinite width. */
12667 init_to_row_start (&it, w, cursor_row);
12668 it.last_visible_x = INFINITY;
12669 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12670 current_buffer = saved_current_buffer;
12671
12672 /* Position cursor in window. */
12673 if (!hscroll_relative_p && hscroll_step_abs == 0)
12674 hscroll = max (0, (it.current_x
12675 - (ITERATOR_AT_END_OF_LINE_P (&it)
12676 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12677 : (text_area_width / 2))))
12678 / FRAME_COLUMN_WIDTH (it.f);
12679 else if ((!row_r2l_p
12680 && w->cursor.x >= text_area_width - h_margin)
12681 || (row_r2l_p && w->cursor.x <= h_margin))
12682 {
12683 if (hscroll_relative_p)
12684 wanted_x = text_area_width * (1 - hscroll_step_rel)
12685 - h_margin;
12686 else
12687 wanted_x = text_area_width
12688 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12689 - h_margin;
12690 hscroll
12691 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12692 }
12693 else
12694 {
12695 if (hscroll_relative_p)
12696 wanted_x = text_area_width * hscroll_step_rel
12697 + h_margin;
12698 else
12699 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12700 + h_margin;
12701 hscroll
12702 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12703 }
12704 hscroll = max (hscroll, w->min_hscroll);
12705
12706 /* Don't prevent redisplay optimizations if hscroll
12707 hasn't changed, as it will unnecessarily slow down
12708 redisplay. */
12709 if (w->hscroll != hscroll)
12710 {
12711 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12712 w->hscroll = hscroll;
12713 hscrolled_p = 1;
12714 }
12715 }
12716 }
12717
12718 window = w->next;
12719 }
12720
12721 /* Value is non-zero if hscroll of any leaf window has been changed. */
12722 return hscrolled_p;
12723 }
12724
12725
12726 /* Set hscroll so that cursor is visible and not inside horizontal
12727 scroll margins for all windows in the tree rooted at WINDOW. See
12728 also hscroll_window_tree above. Value is non-zero if any window's
12729 hscroll has been changed. If it has, desired matrices on the frame
12730 of WINDOW are cleared. */
12731
12732 static int
12733 hscroll_windows (Lisp_Object window)
12734 {
12735 int hscrolled_p = hscroll_window_tree (window);
12736 if (hscrolled_p)
12737 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12738 return hscrolled_p;
12739 }
12740
12741
12742 \f
12743 /************************************************************************
12744 Redisplay
12745 ************************************************************************/
12746
12747 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12748 to a non-zero value. This is sometimes handy to have in a debugger
12749 session. */
12750
12751 #ifdef GLYPH_DEBUG
12752
12753 /* First and last unchanged row for try_window_id. */
12754
12755 static int debug_first_unchanged_at_end_vpos;
12756 static int debug_last_unchanged_at_beg_vpos;
12757
12758 /* Delta vpos and y. */
12759
12760 static int debug_dvpos, debug_dy;
12761
12762 /* Delta in characters and bytes for try_window_id. */
12763
12764 static ptrdiff_t debug_delta, debug_delta_bytes;
12765
12766 /* Values of window_end_pos and window_end_vpos at the end of
12767 try_window_id. */
12768
12769 static ptrdiff_t debug_end_vpos;
12770
12771 /* Append a string to W->desired_matrix->method. FMT is a printf
12772 format string. If trace_redisplay_p is non-zero also printf the
12773 resulting string to stderr. */
12774
12775 static void debug_method_add (struct window *, char const *, ...)
12776 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12777
12778 static void
12779 debug_method_add (struct window *w, char const *fmt, ...)
12780 {
12781 void *ptr = w;
12782 char *method = w->desired_matrix->method;
12783 int len = strlen (method);
12784 int size = sizeof w->desired_matrix->method;
12785 int remaining = size - len - 1;
12786 va_list ap;
12787
12788 if (len && remaining)
12789 {
12790 method[len] = '|';
12791 --remaining, ++len;
12792 }
12793
12794 va_start (ap, fmt);
12795 vsnprintf (method + len, remaining + 1, fmt, ap);
12796 va_end (ap);
12797
12798 if (trace_redisplay_p)
12799 fprintf (stderr, "%p (%s): %s\n",
12800 ptr,
12801 ((BUFFERP (w->contents)
12802 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12803 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12804 : "no buffer"),
12805 method + len);
12806 }
12807
12808 #endif /* GLYPH_DEBUG */
12809
12810
12811 /* Value is non-zero if all changes in window W, which displays
12812 current_buffer, are in the text between START and END. START is a
12813 buffer position, END is given as a distance from Z. Used in
12814 redisplay_internal for display optimization. */
12815
12816 static int
12817 text_outside_line_unchanged_p (struct window *w,
12818 ptrdiff_t start, ptrdiff_t end)
12819 {
12820 int unchanged_p = 1;
12821
12822 /* If text or overlays have changed, see where. */
12823 if (window_outdated (w))
12824 {
12825 /* Gap in the line? */
12826 if (GPT < start || Z - GPT < end)
12827 unchanged_p = 0;
12828
12829 /* Changes start in front of the line, or end after it? */
12830 if (unchanged_p
12831 && (BEG_UNCHANGED < start - 1
12832 || END_UNCHANGED < end))
12833 unchanged_p = 0;
12834
12835 /* If selective display, can't optimize if changes start at the
12836 beginning of the line. */
12837 if (unchanged_p
12838 && INTEGERP (BVAR (current_buffer, selective_display))
12839 && XINT (BVAR (current_buffer, selective_display)) > 0
12840 && (BEG_UNCHANGED < start || GPT <= start))
12841 unchanged_p = 0;
12842
12843 /* If there are overlays at the start or end of the line, these
12844 may have overlay strings with newlines in them. A change at
12845 START, for instance, may actually concern the display of such
12846 overlay strings as well, and they are displayed on different
12847 lines. So, quickly rule out this case. (For the future, it
12848 might be desirable to implement something more telling than
12849 just BEG/END_UNCHANGED.) */
12850 if (unchanged_p)
12851 {
12852 if (BEG + BEG_UNCHANGED == start
12853 && overlay_touches_p (start))
12854 unchanged_p = 0;
12855 if (END_UNCHANGED == end
12856 && overlay_touches_p (Z - end))
12857 unchanged_p = 0;
12858 }
12859
12860 /* Under bidi reordering, adding or deleting a character in the
12861 beginning of a paragraph, before the first strong directional
12862 character, can change the base direction of the paragraph (unless
12863 the buffer specifies a fixed paragraph direction), which will
12864 require to redisplay the whole paragraph. It might be worthwhile
12865 to find the paragraph limits and widen the range of redisplayed
12866 lines to that, but for now just give up this optimization. */
12867 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12868 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12869 unchanged_p = 0;
12870 }
12871
12872 return unchanged_p;
12873 }
12874
12875
12876 /* Do a frame update, taking possible shortcuts into account. This is
12877 the main external entry point for redisplay.
12878
12879 If the last redisplay displayed an echo area message and that message
12880 is no longer requested, we clear the echo area or bring back the
12881 mini-buffer if that is in use. */
12882
12883 void
12884 redisplay (void)
12885 {
12886 redisplay_internal ();
12887 }
12888
12889
12890 static Lisp_Object
12891 overlay_arrow_string_or_property (Lisp_Object var)
12892 {
12893 Lisp_Object val;
12894
12895 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12896 return val;
12897
12898 return Voverlay_arrow_string;
12899 }
12900
12901 /* Return 1 if there are any overlay-arrows in current_buffer. */
12902 static int
12903 overlay_arrow_in_current_buffer_p (void)
12904 {
12905 Lisp_Object vlist;
12906
12907 for (vlist = Voverlay_arrow_variable_list;
12908 CONSP (vlist);
12909 vlist = XCDR (vlist))
12910 {
12911 Lisp_Object var = XCAR (vlist);
12912 Lisp_Object val;
12913
12914 if (!SYMBOLP (var))
12915 continue;
12916 val = find_symbol_value (var);
12917 if (MARKERP (val)
12918 && current_buffer == XMARKER (val)->buffer)
12919 return 1;
12920 }
12921 return 0;
12922 }
12923
12924
12925 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12926 has changed. */
12927
12928 static int
12929 overlay_arrows_changed_p (void)
12930 {
12931 Lisp_Object vlist;
12932
12933 for (vlist = Voverlay_arrow_variable_list;
12934 CONSP (vlist);
12935 vlist = XCDR (vlist))
12936 {
12937 Lisp_Object var = XCAR (vlist);
12938 Lisp_Object val, pstr;
12939
12940 if (!SYMBOLP (var))
12941 continue;
12942 val = find_symbol_value (var);
12943 if (!MARKERP (val))
12944 continue;
12945 if (! EQ (COERCE_MARKER (val),
12946 Fget (var, Qlast_arrow_position))
12947 || ! (pstr = overlay_arrow_string_or_property (var),
12948 EQ (pstr, Fget (var, Qlast_arrow_string))))
12949 return 1;
12950 }
12951 return 0;
12952 }
12953
12954 /* Mark overlay arrows to be updated on next redisplay. */
12955
12956 static void
12957 update_overlay_arrows (int up_to_date)
12958 {
12959 Lisp_Object vlist;
12960
12961 for (vlist = Voverlay_arrow_variable_list;
12962 CONSP (vlist);
12963 vlist = XCDR (vlist))
12964 {
12965 Lisp_Object var = XCAR (vlist);
12966
12967 if (!SYMBOLP (var))
12968 continue;
12969
12970 if (up_to_date > 0)
12971 {
12972 Lisp_Object val = find_symbol_value (var);
12973 Fput (var, Qlast_arrow_position,
12974 COERCE_MARKER (val));
12975 Fput (var, Qlast_arrow_string,
12976 overlay_arrow_string_or_property (var));
12977 }
12978 else if (up_to_date < 0
12979 || !NILP (Fget (var, Qlast_arrow_position)))
12980 {
12981 Fput (var, Qlast_arrow_position, Qt);
12982 Fput (var, Qlast_arrow_string, Qt);
12983 }
12984 }
12985 }
12986
12987
12988 /* Return overlay arrow string to display at row.
12989 Return integer (bitmap number) for arrow bitmap in left fringe.
12990 Return nil if no overlay arrow. */
12991
12992 static Lisp_Object
12993 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12994 {
12995 Lisp_Object vlist;
12996
12997 for (vlist = Voverlay_arrow_variable_list;
12998 CONSP (vlist);
12999 vlist = XCDR (vlist))
13000 {
13001 Lisp_Object var = XCAR (vlist);
13002 Lisp_Object val;
13003
13004 if (!SYMBOLP (var))
13005 continue;
13006
13007 val = find_symbol_value (var);
13008
13009 if (MARKERP (val)
13010 && current_buffer == XMARKER (val)->buffer
13011 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13012 {
13013 if (FRAME_WINDOW_P (it->f)
13014 /* FIXME: if ROW->reversed_p is set, this should test
13015 the right fringe, not the left one. */
13016 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13017 {
13018 #ifdef HAVE_WINDOW_SYSTEM
13019 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13020 {
13021 int fringe_bitmap;
13022 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13023 return make_number (fringe_bitmap);
13024 }
13025 #endif
13026 return make_number (-1); /* Use default arrow bitmap. */
13027 }
13028 return overlay_arrow_string_or_property (var);
13029 }
13030 }
13031
13032 return Qnil;
13033 }
13034
13035 /* Return 1 if point moved out of or into a composition. Otherwise
13036 return 0. PREV_BUF and PREV_PT are the last point buffer and
13037 position. BUF and PT are the current point buffer and position. */
13038
13039 static int
13040 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13041 struct buffer *buf, ptrdiff_t pt)
13042 {
13043 ptrdiff_t start, end;
13044 Lisp_Object prop;
13045 Lisp_Object buffer;
13046
13047 XSETBUFFER (buffer, buf);
13048 /* Check a composition at the last point if point moved within the
13049 same buffer. */
13050 if (prev_buf == buf)
13051 {
13052 if (prev_pt == pt)
13053 /* Point didn't move. */
13054 return 0;
13055
13056 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13057 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13058 && composition_valid_p (start, end, prop)
13059 && start < prev_pt && end > prev_pt)
13060 /* The last point was within the composition. Return 1 iff
13061 point moved out of the composition. */
13062 return (pt <= start || pt >= end);
13063 }
13064
13065 /* Check a composition at the current point. */
13066 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13067 && find_composition (pt, -1, &start, &end, &prop, buffer)
13068 && composition_valid_p (start, end, prop)
13069 && start < pt && end > pt);
13070 }
13071
13072 /* Reconsider the clip changes of buffer which is displayed in W. */
13073
13074 static void
13075 reconsider_clip_changes (struct window *w)
13076 {
13077 struct buffer *b = XBUFFER (w->contents);
13078
13079 if (b->clip_changed
13080 && w->window_end_valid
13081 && w->current_matrix->buffer == b
13082 && w->current_matrix->zv == BUF_ZV (b)
13083 && w->current_matrix->begv == BUF_BEGV (b))
13084 b->clip_changed = 0;
13085
13086 /* If display wasn't paused, and W is not a tool bar window, see if
13087 point has been moved into or out of a composition. In that case,
13088 we set b->clip_changed to 1 to force updating the screen. If
13089 b->clip_changed has already been set to 1, we can skip this
13090 check. */
13091 if (!b->clip_changed && w->window_end_valid)
13092 {
13093 ptrdiff_t pt = (w == XWINDOW (selected_window)
13094 ? PT : marker_position (w->pointm));
13095
13096 if ((w->current_matrix->buffer != b || pt != w->last_point)
13097 && check_point_in_composition (w->current_matrix->buffer,
13098 w->last_point, b, pt))
13099 b->clip_changed = 1;
13100 }
13101 }
13102
13103 static void
13104 propagate_buffer_redisplay (void)
13105 { /* Resetting b->text->redisplay is problematic!
13106 We can't just reset it in the case that some window that displays
13107 it has not been redisplayed; and such a window can stay
13108 unredisplayed for a long time if it's currently invisible.
13109 But we do want to reset it at the end of redisplay otherwise
13110 its displayed windows will keep being redisplayed over and over
13111 again.
13112 So we copy all b->text->redisplay flags up to their windows here,
13113 such that mark_window_display_accurate can safely reset
13114 b->text->redisplay. */
13115 Lisp_Object ws = window_list ();
13116 for (; CONSP (ws); ws = XCDR (ws))
13117 {
13118 struct window *thisw = XWINDOW (XCAR (ws));
13119 struct buffer *thisb = XBUFFER (thisw->contents);
13120 if (thisb->text->redisplay)
13121 thisw->redisplay = true;
13122 }
13123 }
13124
13125 #define STOP_POLLING \
13126 do { if (! polling_stopped_here) stop_polling (); \
13127 polling_stopped_here = 1; } while (0)
13128
13129 #define RESUME_POLLING \
13130 do { if (polling_stopped_here) start_polling (); \
13131 polling_stopped_here = 0; } while (0)
13132
13133
13134 /* Perhaps in the future avoid recentering windows if it
13135 is not necessary; currently that causes some problems. */
13136
13137 static void
13138 redisplay_internal (void)
13139 {
13140 struct window *w = XWINDOW (selected_window);
13141 struct window *sw;
13142 struct frame *fr;
13143 int pending;
13144 bool must_finish = 0, match_p;
13145 struct text_pos tlbufpos, tlendpos;
13146 int number_of_visible_frames;
13147 ptrdiff_t count;
13148 struct frame *sf;
13149 int polling_stopped_here = 0;
13150 Lisp_Object tail, frame;
13151
13152 /* True means redisplay has to consider all windows on all
13153 frames. False, only selected_window is considered. */
13154 bool consider_all_windows_p;
13155
13156 /* True means redisplay has to redisplay the miniwindow. */
13157 bool update_miniwindow_p = false;
13158
13159 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13160
13161 /* No redisplay if running in batch mode or frame is not yet fully
13162 initialized, or redisplay is explicitly turned off by setting
13163 Vinhibit_redisplay. */
13164 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13165 || !NILP (Vinhibit_redisplay))
13166 return;
13167
13168 /* Don't examine these until after testing Vinhibit_redisplay.
13169 When Emacs is shutting down, perhaps because its connection to
13170 X has dropped, we should not look at them at all. */
13171 fr = XFRAME (w->frame);
13172 sf = SELECTED_FRAME ();
13173
13174 if (!fr->glyphs_initialized_p)
13175 return;
13176
13177 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13178 if (popup_activated ())
13179 return;
13180 #endif
13181
13182 /* I don't think this happens but let's be paranoid. */
13183 if (redisplaying_p)
13184 return;
13185
13186 /* Record a function that clears redisplaying_p
13187 when we leave this function. */
13188 count = SPECPDL_INDEX ();
13189 record_unwind_protect_void (unwind_redisplay);
13190 redisplaying_p = 1;
13191 specbind (Qinhibit_free_realized_faces, Qnil);
13192
13193 /* Record this function, so it appears on the profiler's backtraces. */
13194 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13195
13196 FOR_EACH_FRAME (tail, frame)
13197 XFRAME (frame)->already_hscrolled_p = 0;
13198
13199 retry:
13200 /* Remember the currently selected window. */
13201 sw = w;
13202
13203 pending = 0;
13204 last_escape_glyph_frame = NULL;
13205 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13206 last_glyphless_glyph_frame = NULL;
13207 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13208
13209 /* If face_change_count is non-zero, init_iterator will free all
13210 realized faces, which includes the faces referenced from current
13211 matrices. So, we can't reuse current matrices in this case. */
13212 if (face_change_count)
13213 windows_or_buffers_changed = 47;
13214
13215 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13216 && FRAME_TTY (sf)->previous_frame != sf)
13217 {
13218 /* Since frames on a single ASCII terminal share the same
13219 display area, displaying a different frame means redisplay
13220 the whole thing. */
13221 SET_FRAME_GARBAGED (sf);
13222 #ifndef DOS_NT
13223 set_tty_color_mode (FRAME_TTY (sf), sf);
13224 #endif
13225 FRAME_TTY (sf)->previous_frame = sf;
13226 }
13227
13228 /* Set the visible flags for all frames. Do this before checking for
13229 resized or garbaged frames; they want to know if their frames are
13230 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13231 number_of_visible_frames = 0;
13232
13233 FOR_EACH_FRAME (tail, frame)
13234 {
13235 struct frame *f = XFRAME (frame);
13236
13237 if (FRAME_VISIBLE_P (f))
13238 {
13239 ++number_of_visible_frames;
13240 /* Adjust matrices for visible frames only. */
13241 if (f->fonts_changed)
13242 {
13243 adjust_frame_glyphs (f);
13244 f->fonts_changed = 0;
13245 }
13246 /* If cursor type has been changed on the frame
13247 other than selected, consider all frames. */
13248 if (f != sf && f->cursor_type_changed)
13249 update_mode_lines = 31;
13250 }
13251 clear_desired_matrices (f);
13252 }
13253
13254 /* Notice any pending interrupt request to change frame size. */
13255 do_pending_window_change (1);
13256
13257 /* do_pending_window_change could change the selected_window due to
13258 frame resizing which makes the selected window too small. */
13259 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13260 sw = w;
13261
13262 /* Clear frames marked as garbaged. */
13263 clear_garbaged_frames ();
13264
13265 /* Build menubar and tool-bar items. */
13266 if (NILP (Vmemory_full))
13267 prepare_menu_bars ();
13268
13269 reconsider_clip_changes (w);
13270
13271 /* In most cases selected window displays current buffer. */
13272 match_p = XBUFFER (w->contents) == current_buffer;
13273 if (match_p)
13274 {
13275 /* Detect case that we need to write or remove a star in the mode line. */
13276 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13277 w->update_mode_line = 1;
13278
13279 if (mode_line_update_needed (w))
13280 w->update_mode_line = 1;
13281 }
13282
13283 /* Normally the message* functions will have already displayed and
13284 updated the echo area, but the frame may have been trashed, or
13285 the update may have been preempted, so display the echo area
13286 again here. Checking message_cleared_p captures the case that
13287 the echo area should be cleared. */
13288 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13289 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13290 || (message_cleared_p
13291 && minibuf_level == 0
13292 /* If the mini-window is currently selected, this means the
13293 echo-area doesn't show through. */
13294 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13295 {
13296 int window_height_changed_p = echo_area_display (0);
13297
13298 if (message_cleared_p)
13299 update_miniwindow_p = true;
13300
13301 must_finish = 1;
13302
13303 /* If we don't display the current message, don't clear the
13304 message_cleared_p flag, because, if we did, we wouldn't clear
13305 the echo area in the next redisplay which doesn't preserve
13306 the echo area. */
13307 if (!display_last_displayed_message_p)
13308 message_cleared_p = 0;
13309
13310 if (window_height_changed_p)
13311 {
13312 windows_or_buffers_changed = 50;
13313
13314 /* If window configuration was changed, frames may have been
13315 marked garbaged. Clear them or we will experience
13316 surprises wrt scrolling. */
13317 clear_garbaged_frames ();
13318 }
13319 }
13320 else if (EQ (selected_window, minibuf_window)
13321 && (current_buffer->clip_changed || window_outdated (w))
13322 && resize_mini_window (w, 0))
13323 {
13324 /* Resized active mini-window to fit the size of what it is
13325 showing if its contents might have changed. */
13326 must_finish = 1;
13327
13328 /* If window configuration was changed, frames may have been
13329 marked garbaged. Clear them or we will experience
13330 surprises wrt scrolling. */
13331 clear_garbaged_frames ();
13332 }
13333
13334 if (windows_or_buffers_changed && !update_mode_lines)
13335 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13336 only the windows's contents needs to be refreshed, or whether the
13337 mode-lines also need a refresh. */
13338 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13339 ? REDISPLAY_SOME : 32);
13340
13341 /* If specs for an arrow have changed, do thorough redisplay
13342 to ensure we remove any arrow that should no longer exist. */
13343 if (overlay_arrows_changed_p ())
13344 /* Apparently, this is the only case where we update other windows,
13345 without updating other mode-lines. */
13346 windows_or_buffers_changed = 49;
13347
13348 consider_all_windows_p = (update_mode_lines
13349 || windows_or_buffers_changed);
13350
13351 #define AINC(a,i) \
13352 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13353 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13354
13355 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13356 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13357
13358 /* Optimize the case that only the line containing the cursor in the
13359 selected window has changed. Variables starting with this_ are
13360 set in display_line and record information about the line
13361 containing the cursor. */
13362 tlbufpos = this_line_start_pos;
13363 tlendpos = this_line_end_pos;
13364 if (!consider_all_windows_p
13365 && CHARPOS (tlbufpos) > 0
13366 && !w->update_mode_line
13367 && !current_buffer->clip_changed
13368 && !current_buffer->prevent_redisplay_optimizations_p
13369 && FRAME_VISIBLE_P (XFRAME (w->frame))
13370 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13371 && !XFRAME (w->frame)->cursor_type_changed
13372 /* Make sure recorded data applies to current buffer, etc. */
13373 && this_line_buffer == current_buffer
13374 && match_p
13375 && !w->force_start
13376 && !w->optional_new_start
13377 /* Point must be on the line that we have info recorded about. */
13378 && PT >= CHARPOS (tlbufpos)
13379 && PT <= Z - CHARPOS (tlendpos)
13380 /* All text outside that line, including its final newline,
13381 must be unchanged. */
13382 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13383 CHARPOS (tlendpos)))
13384 {
13385 if (CHARPOS (tlbufpos) > BEGV
13386 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13387 && (CHARPOS (tlbufpos) == ZV
13388 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13389 /* Former continuation line has disappeared by becoming empty. */
13390 goto cancel;
13391 else if (window_outdated (w) || MINI_WINDOW_P (w))
13392 {
13393 /* We have to handle the case of continuation around a
13394 wide-column character (see the comment in indent.c around
13395 line 1340).
13396
13397 For instance, in the following case:
13398
13399 -------- Insert --------
13400 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13401 J_I_ ==> J_I_ `^^' are cursors.
13402 ^^ ^^
13403 -------- --------
13404
13405 As we have to redraw the line above, we cannot use this
13406 optimization. */
13407
13408 struct it it;
13409 int line_height_before = this_line_pixel_height;
13410
13411 /* Note that start_display will handle the case that the
13412 line starting at tlbufpos is a continuation line. */
13413 start_display (&it, w, tlbufpos);
13414
13415 /* Implementation note: It this still necessary? */
13416 if (it.current_x != this_line_start_x)
13417 goto cancel;
13418
13419 TRACE ((stderr, "trying display optimization 1\n"));
13420 w->cursor.vpos = -1;
13421 overlay_arrow_seen = 0;
13422 it.vpos = this_line_vpos;
13423 it.current_y = this_line_y;
13424 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13425 display_line (&it);
13426
13427 /* If line contains point, is not continued,
13428 and ends at same distance from eob as before, we win. */
13429 if (w->cursor.vpos >= 0
13430 /* Line is not continued, otherwise this_line_start_pos
13431 would have been set to 0 in display_line. */
13432 && CHARPOS (this_line_start_pos)
13433 /* Line ends as before. */
13434 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13435 /* Line has same height as before. Otherwise other lines
13436 would have to be shifted up or down. */
13437 && this_line_pixel_height == line_height_before)
13438 {
13439 /* If this is not the window's last line, we must adjust
13440 the charstarts of the lines below. */
13441 if (it.current_y < it.last_visible_y)
13442 {
13443 struct glyph_row *row
13444 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13445 ptrdiff_t delta, delta_bytes;
13446
13447 /* We used to distinguish between two cases here,
13448 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13449 when the line ends in a newline or the end of the
13450 buffer's accessible portion. But both cases did
13451 the same, so they were collapsed. */
13452 delta = (Z
13453 - CHARPOS (tlendpos)
13454 - MATRIX_ROW_START_CHARPOS (row));
13455 delta_bytes = (Z_BYTE
13456 - BYTEPOS (tlendpos)
13457 - MATRIX_ROW_START_BYTEPOS (row));
13458
13459 increment_matrix_positions (w->current_matrix,
13460 this_line_vpos + 1,
13461 w->current_matrix->nrows,
13462 delta, delta_bytes);
13463 }
13464
13465 /* If this row displays text now but previously didn't,
13466 or vice versa, w->window_end_vpos may have to be
13467 adjusted. */
13468 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13469 {
13470 if (w->window_end_vpos < this_line_vpos)
13471 w->window_end_vpos = this_line_vpos;
13472 }
13473 else if (w->window_end_vpos == this_line_vpos
13474 && this_line_vpos > 0)
13475 w->window_end_vpos = this_line_vpos - 1;
13476 w->window_end_valid = 0;
13477
13478 /* Update hint: No need to try to scroll in update_window. */
13479 w->desired_matrix->no_scrolling_p = 1;
13480
13481 #ifdef GLYPH_DEBUG
13482 *w->desired_matrix->method = 0;
13483 debug_method_add (w, "optimization 1");
13484 #endif
13485 #ifdef HAVE_WINDOW_SYSTEM
13486 update_window_fringes (w, 0);
13487 #endif
13488 goto update;
13489 }
13490 else
13491 goto cancel;
13492 }
13493 else if (/* Cursor position hasn't changed. */
13494 PT == w->last_point
13495 /* Make sure the cursor was last displayed
13496 in this window. Otherwise we have to reposition it. */
13497
13498 /* PXW: Must be converted to pixels, probably. */
13499 && 0 <= w->cursor.vpos
13500 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13501 {
13502 if (!must_finish)
13503 {
13504 do_pending_window_change (1);
13505 /* If selected_window changed, redisplay again. */
13506 if (WINDOWP (selected_window)
13507 && (w = XWINDOW (selected_window)) != sw)
13508 goto retry;
13509
13510 /* We used to always goto end_of_redisplay here, but this
13511 isn't enough if we have a blinking cursor. */
13512 if (w->cursor_off_p == w->last_cursor_off_p)
13513 goto end_of_redisplay;
13514 }
13515 goto update;
13516 }
13517 /* If highlighting the region, or if the cursor is in the echo area,
13518 then we can't just move the cursor. */
13519 else if (NILP (Vshow_trailing_whitespace)
13520 && !cursor_in_echo_area)
13521 {
13522 struct it it;
13523 struct glyph_row *row;
13524
13525 /* Skip from tlbufpos to PT and see where it is. Note that
13526 PT may be in invisible text. If so, we will end at the
13527 next visible position. */
13528 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13529 NULL, DEFAULT_FACE_ID);
13530 it.current_x = this_line_start_x;
13531 it.current_y = this_line_y;
13532 it.vpos = this_line_vpos;
13533
13534 /* The call to move_it_to stops in front of PT, but
13535 moves over before-strings. */
13536 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13537
13538 if (it.vpos == this_line_vpos
13539 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13540 row->enabled_p))
13541 {
13542 eassert (this_line_vpos == it.vpos);
13543 eassert (this_line_y == it.current_y);
13544 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13545 #ifdef GLYPH_DEBUG
13546 *w->desired_matrix->method = 0;
13547 debug_method_add (w, "optimization 3");
13548 #endif
13549 goto update;
13550 }
13551 else
13552 goto cancel;
13553 }
13554
13555 cancel:
13556 /* Text changed drastically or point moved off of line. */
13557 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13558 }
13559
13560 CHARPOS (this_line_start_pos) = 0;
13561 ++clear_face_cache_count;
13562 #ifdef HAVE_WINDOW_SYSTEM
13563 ++clear_image_cache_count;
13564 #endif
13565
13566 /* Build desired matrices, and update the display. If
13567 consider_all_windows_p is non-zero, do it for all windows on all
13568 frames. Otherwise do it for selected_window, only. */
13569
13570 if (consider_all_windows_p)
13571 {
13572 FOR_EACH_FRAME (tail, frame)
13573 XFRAME (frame)->updated_p = 0;
13574
13575 propagate_buffer_redisplay ();
13576
13577 FOR_EACH_FRAME (tail, frame)
13578 {
13579 struct frame *f = XFRAME (frame);
13580
13581 /* We don't have to do anything for unselected terminal
13582 frames. */
13583 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13584 && !EQ (FRAME_TTY (f)->top_frame, frame))
13585 continue;
13586
13587 retry_frame:
13588
13589 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13590 {
13591 bool gcscrollbars
13592 /* Only GC scrollbars when we redisplay the whole frame. */
13593 = f->redisplay || !REDISPLAY_SOME_P ();
13594 /* Mark all the scroll bars to be removed; we'll redeem
13595 the ones we want when we redisplay their windows. */
13596 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13597 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13598
13599 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13600 redisplay_windows (FRAME_ROOT_WINDOW (f));
13601 /* Remember that the invisible frames need to be redisplayed next
13602 time they're visible. */
13603 else if (!REDISPLAY_SOME_P ())
13604 f->redisplay = true;
13605
13606 /* The X error handler may have deleted that frame. */
13607 if (!FRAME_LIVE_P (f))
13608 continue;
13609
13610 /* Any scroll bars which redisplay_windows should have
13611 nuked should now go away. */
13612 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13613 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13614
13615 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13616 {
13617 /* If fonts changed on visible frame, display again. */
13618 if (f->fonts_changed)
13619 {
13620 adjust_frame_glyphs (f);
13621 f->fonts_changed = 0;
13622 goto retry_frame;
13623 }
13624
13625 /* See if we have to hscroll. */
13626 if (!f->already_hscrolled_p)
13627 {
13628 f->already_hscrolled_p = 1;
13629 if (hscroll_windows (f->root_window))
13630 goto retry_frame;
13631 }
13632
13633 /* Prevent various kinds of signals during display
13634 update. stdio is not robust about handling
13635 signals, which can cause an apparent I/O error. */
13636 if (interrupt_input)
13637 unrequest_sigio ();
13638 STOP_POLLING;
13639
13640 pending |= update_frame (f, 0, 0);
13641 f->cursor_type_changed = 0;
13642 f->updated_p = 1;
13643 }
13644 }
13645 }
13646
13647 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13648
13649 if (!pending)
13650 {
13651 /* Do the mark_window_display_accurate after all windows have
13652 been redisplayed because this call resets flags in buffers
13653 which are needed for proper redisplay. */
13654 FOR_EACH_FRAME (tail, frame)
13655 {
13656 struct frame *f = XFRAME (frame);
13657 if (f->updated_p)
13658 {
13659 f->redisplay = false;
13660 mark_window_display_accurate (f->root_window, 1);
13661 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13662 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13663 }
13664 }
13665 }
13666 }
13667 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13668 {
13669 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13670 struct frame *mini_frame;
13671
13672 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13673 /* Use list_of_error, not Qerror, so that
13674 we catch only errors and don't run the debugger. */
13675 internal_condition_case_1 (redisplay_window_1, selected_window,
13676 list_of_error,
13677 redisplay_window_error);
13678 if (update_miniwindow_p)
13679 internal_condition_case_1 (redisplay_window_1, mini_window,
13680 list_of_error,
13681 redisplay_window_error);
13682
13683 /* Compare desired and current matrices, perform output. */
13684
13685 update:
13686 /* If fonts changed, display again. */
13687 if (sf->fonts_changed)
13688 goto retry;
13689
13690 /* Prevent various kinds of signals during display update.
13691 stdio is not robust about handling signals,
13692 which can cause an apparent I/O error. */
13693 if (interrupt_input)
13694 unrequest_sigio ();
13695 STOP_POLLING;
13696
13697 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13698 {
13699 if (hscroll_windows (selected_window))
13700 goto retry;
13701
13702 XWINDOW (selected_window)->must_be_updated_p = true;
13703 pending = update_frame (sf, 0, 0);
13704 sf->cursor_type_changed = 0;
13705 }
13706
13707 /* We may have called echo_area_display at the top of this
13708 function. If the echo area is on another frame, that may
13709 have put text on a frame other than the selected one, so the
13710 above call to update_frame would not have caught it. Catch
13711 it here. */
13712 mini_window = FRAME_MINIBUF_WINDOW (sf);
13713 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13714
13715 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13716 {
13717 XWINDOW (mini_window)->must_be_updated_p = true;
13718 pending |= update_frame (mini_frame, 0, 0);
13719 mini_frame->cursor_type_changed = 0;
13720 if (!pending && hscroll_windows (mini_window))
13721 goto retry;
13722 }
13723 }
13724
13725 /* If display was paused because of pending input, make sure we do a
13726 thorough update the next time. */
13727 if (pending)
13728 {
13729 /* Prevent the optimization at the beginning of
13730 redisplay_internal that tries a single-line update of the
13731 line containing the cursor in the selected window. */
13732 CHARPOS (this_line_start_pos) = 0;
13733
13734 /* Let the overlay arrow be updated the next time. */
13735 update_overlay_arrows (0);
13736
13737 /* If we pause after scrolling, some rows in the current
13738 matrices of some windows are not valid. */
13739 if (!WINDOW_FULL_WIDTH_P (w)
13740 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13741 update_mode_lines = 36;
13742 }
13743 else
13744 {
13745 if (!consider_all_windows_p)
13746 {
13747 /* This has already been done above if
13748 consider_all_windows_p is set. */
13749 if (XBUFFER (w->contents)->text->redisplay
13750 && buffer_window_count (XBUFFER (w->contents)) > 1)
13751 /* This can happen if b->text->redisplay was set during
13752 jit-lock. */
13753 propagate_buffer_redisplay ();
13754 mark_window_display_accurate_1 (w, 1);
13755
13756 /* Say overlay arrows are up to date. */
13757 update_overlay_arrows (1);
13758
13759 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13760 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13761 }
13762
13763 update_mode_lines = 0;
13764 windows_or_buffers_changed = 0;
13765 }
13766
13767 /* Start SIGIO interrupts coming again. Having them off during the
13768 code above makes it less likely one will discard output, but not
13769 impossible, since there might be stuff in the system buffer here.
13770 But it is much hairier to try to do anything about that. */
13771 if (interrupt_input)
13772 request_sigio ();
13773 RESUME_POLLING;
13774
13775 /* If a frame has become visible which was not before, redisplay
13776 again, so that we display it. Expose events for such a frame
13777 (which it gets when becoming visible) don't call the parts of
13778 redisplay constructing glyphs, so simply exposing a frame won't
13779 display anything in this case. So, we have to display these
13780 frames here explicitly. */
13781 if (!pending)
13782 {
13783 int new_count = 0;
13784
13785 FOR_EACH_FRAME (tail, frame)
13786 {
13787 if (XFRAME (frame)->visible)
13788 new_count++;
13789 }
13790
13791 if (new_count != number_of_visible_frames)
13792 windows_or_buffers_changed = 52;
13793 }
13794
13795 /* Change frame size now if a change is pending. */
13796 do_pending_window_change (1);
13797
13798 /* If we just did a pending size change, or have additional
13799 visible frames, or selected_window changed, redisplay again. */
13800 if ((windows_or_buffers_changed && !pending)
13801 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13802 goto retry;
13803
13804 /* Clear the face and image caches.
13805
13806 We used to do this only if consider_all_windows_p. But the cache
13807 needs to be cleared if a timer creates images in the current
13808 buffer (e.g. the test case in Bug#6230). */
13809
13810 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13811 {
13812 clear_face_cache (0);
13813 clear_face_cache_count = 0;
13814 }
13815
13816 #ifdef HAVE_WINDOW_SYSTEM
13817 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13818 {
13819 clear_image_caches (Qnil);
13820 clear_image_cache_count = 0;
13821 }
13822 #endif /* HAVE_WINDOW_SYSTEM */
13823
13824 end_of_redisplay:
13825 if (interrupt_input && interrupts_deferred)
13826 request_sigio ();
13827
13828 unbind_to (count, Qnil);
13829 RESUME_POLLING;
13830 }
13831
13832
13833 /* Redisplay, but leave alone any recent echo area message unless
13834 another message has been requested in its place.
13835
13836 This is useful in situations where you need to redisplay but no
13837 user action has occurred, making it inappropriate for the message
13838 area to be cleared. See tracking_off and
13839 wait_reading_process_output for examples of these situations.
13840
13841 FROM_WHERE is an integer saying from where this function was
13842 called. This is useful for debugging. */
13843
13844 void
13845 redisplay_preserve_echo_area (int from_where)
13846 {
13847 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13848
13849 if (!NILP (echo_area_buffer[1]))
13850 {
13851 /* We have a previously displayed message, but no current
13852 message. Redisplay the previous message. */
13853 display_last_displayed_message_p = 1;
13854 redisplay_internal ();
13855 display_last_displayed_message_p = 0;
13856 }
13857 else
13858 redisplay_internal ();
13859
13860 flush_frame (SELECTED_FRAME ());
13861 }
13862
13863
13864 /* Function registered with record_unwind_protect in redisplay_internal. */
13865
13866 static void
13867 unwind_redisplay (void)
13868 {
13869 redisplaying_p = 0;
13870 }
13871
13872
13873 /* Mark the display of leaf window W as accurate or inaccurate.
13874 If ACCURATE_P is non-zero mark display of W as accurate. If
13875 ACCURATE_P is zero, arrange for W to be redisplayed the next
13876 time redisplay_internal is called. */
13877
13878 static void
13879 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13880 {
13881 struct buffer *b = XBUFFER (w->contents);
13882
13883 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13884 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13885 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13886
13887 if (accurate_p)
13888 {
13889 b->clip_changed = false;
13890 b->prevent_redisplay_optimizations_p = false;
13891 eassert (buffer_window_count (b) > 0);
13892 /* Resetting b->text->redisplay is problematic!
13893 In order to make it safer to do it here, redisplay_internal must
13894 have copied all b->text->redisplay to their respective windows. */
13895 b->text->redisplay = false;
13896
13897 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13898 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13899 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13900 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13901
13902 w->current_matrix->buffer = b;
13903 w->current_matrix->begv = BUF_BEGV (b);
13904 w->current_matrix->zv = BUF_ZV (b);
13905
13906 w->last_cursor_vpos = w->cursor.vpos;
13907 w->last_cursor_off_p = w->cursor_off_p;
13908
13909 if (w == XWINDOW (selected_window))
13910 w->last_point = BUF_PT (b);
13911 else
13912 w->last_point = marker_position (w->pointm);
13913
13914 w->window_end_valid = true;
13915 w->update_mode_line = false;
13916 }
13917
13918 w->redisplay = !accurate_p;
13919 }
13920
13921
13922 /* Mark the display of windows in the window tree rooted at WINDOW as
13923 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13924 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13925 be redisplayed the next time redisplay_internal is called. */
13926
13927 void
13928 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13929 {
13930 struct window *w;
13931
13932 for (; !NILP (window); window = w->next)
13933 {
13934 w = XWINDOW (window);
13935 if (WINDOWP (w->contents))
13936 mark_window_display_accurate (w->contents, accurate_p);
13937 else
13938 mark_window_display_accurate_1 (w, accurate_p);
13939 }
13940
13941 if (accurate_p)
13942 update_overlay_arrows (1);
13943 else
13944 /* Force a thorough redisplay the next time by setting
13945 last_arrow_position and last_arrow_string to t, which is
13946 unequal to any useful value of Voverlay_arrow_... */
13947 update_overlay_arrows (-1);
13948 }
13949
13950
13951 /* Return value in display table DP (Lisp_Char_Table *) for character
13952 C. Since a display table doesn't have any parent, we don't have to
13953 follow parent. Do not call this function directly but use the
13954 macro DISP_CHAR_VECTOR. */
13955
13956 Lisp_Object
13957 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13958 {
13959 Lisp_Object val;
13960
13961 if (ASCII_CHAR_P (c))
13962 {
13963 val = dp->ascii;
13964 if (SUB_CHAR_TABLE_P (val))
13965 val = XSUB_CHAR_TABLE (val)->contents[c];
13966 }
13967 else
13968 {
13969 Lisp_Object table;
13970
13971 XSETCHAR_TABLE (table, dp);
13972 val = char_table_ref (table, c);
13973 }
13974 if (NILP (val))
13975 val = dp->defalt;
13976 return val;
13977 }
13978
13979
13980 \f
13981 /***********************************************************************
13982 Window Redisplay
13983 ***********************************************************************/
13984
13985 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13986
13987 static void
13988 redisplay_windows (Lisp_Object window)
13989 {
13990 while (!NILP (window))
13991 {
13992 struct window *w = XWINDOW (window);
13993
13994 if (WINDOWP (w->contents))
13995 redisplay_windows (w->contents);
13996 else if (BUFFERP (w->contents))
13997 {
13998 displayed_buffer = XBUFFER (w->contents);
13999 /* Use list_of_error, not Qerror, so that
14000 we catch only errors and don't run the debugger. */
14001 internal_condition_case_1 (redisplay_window_0, window,
14002 list_of_error,
14003 redisplay_window_error);
14004 }
14005
14006 window = w->next;
14007 }
14008 }
14009
14010 static Lisp_Object
14011 redisplay_window_error (Lisp_Object ignore)
14012 {
14013 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14014 return Qnil;
14015 }
14016
14017 static Lisp_Object
14018 redisplay_window_0 (Lisp_Object window)
14019 {
14020 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14021 redisplay_window (window, false);
14022 return Qnil;
14023 }
14024
14025 static Lisp_Object
14026 redisplay_window_1 (Lisp_Object window)
14027 {
14028 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14029 redisplay_window (window, true);
14030 return Qnil;
14031 }
14032 \f
14033
14034 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14035 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14036 which positions recorded in ROW differ from current buffer
14037 positions.
14038
14039 Return 0 if cursor is not on this row, 1 otherwise. */
14040
14041 static int
14042 set_cursor_from_row (struct window *w, struct glyph_row *row,
14043 struct glyph_matrix *matrix,
14044 ptrdiff_t delta, ptrdiff_t delta_bytes,
14045 int dy, int dvpos)
14046 {
14047 struct glyph *glyph = row->glyphs[TEXT_AREA];
14048 struct glyph *end = glyph + row->used[TEXT_AREA];
14049 struct glyph *cursor = NULL;
14050 /* The last known character position in row. */
14051 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14052 int x = row->x;
14053 ptrdiff_t pt_old = PT - delta;
14054 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14055 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14056 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14057 /* A glyph beyond the edge of TEXT_AREA which we should never
14058 touch. */
14059 struct glyph *glyphs_end = end;
14060 /* Non-zero means we've found a match for cursor position, but that
14061 glyph has the avoid_cursor_p flag set. */
14062 int match_with_avoid_cursor = 0;
14063 /* Non-zero means we've seen at least one glyph that came from a
14064 display string. */
14065 int string_seen = 0;
14066 /* Largest and smallest buffer positions seen so far during scan of
14067 glyph row. */
14068 ptrdiff_t bpos_max = pos_before;
14069 ptrdiff_t bpos_min = pos_after;
14070 /* Last buffer position covered by an overlay string with an integer
14071 `cursor' property. */
14072 ptrdiff_t bpos_covered = 0;
14073 /* Non-zero means the display string on which to display the cursor
14074 comes from a text property, not from an overlay. */
14075 int string_from_text_prop = 0;
14076
14077 /* Don't even try doing anything if called for a mode-line or
14078 header-line row, since the rest of the code isn't prepared to
14079 deal with such calamities. */
14080 eassert (!row->mode_line_p);
14081 if (row->mode_line_p)
14082 return 0;
14083
14084 /* Skip over glyphs not having an object at the start and the end of
14085 the row. These are special glyphs like truncation marks on
14086 terminal frames. */
14087 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14088 {
14089 if (!row->reversed_p)
14090 {
14091 while (glyph < end
14092 && INTEGERP (glyph->object)
14093 && glyph->charpos < 0)
14094 {
14095 x += glyph->pixel_width;
14096 ++glyph;
14097 }
14098 while (end > glyph
14099 && INTEGERP ((end - 1)->object)
14100 /* CHARPOS is zero for blanks and stretch glyphs
14101 inserted by extend_face_to_end_of_line. */
14102 && (end - 1)->charpos <= 0)
14103 --end;
14104 glyph_before = glyph - 1;
14105 glyph_after = end;
14106 }
14107 else
14108 {
14109 struct glyph *g;
14110
14111 /* If the glyph row is reversed, we need to process it from back
14112 to front, so swap the edge pointers. */
14113 glyphs_end = end = glyph - 1;
14114 glyph += row->used[TEXT_AREA] - 1;
14115
14116 while (glyph > end + 1
14117 && INTEGERP (glyph->object)
14118 && glyph->charpos < 0)
14119 {
14120 --glyph;
14121 x -= glyph->pixel_width;
14122 }
14123 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14124 --glyph;
14125 /* By default, in reversed rows we put the cursor on the
14126 rightmost (first in the reading order) glyph. */
14127 for (g = end + 1; g < glyph; g++)
14128 x += g->pixel_width;
14129 while (end < glyph
14130 && INTEGERP ((end + 1)->object)
14131 && (end + 1)->charpos <= 0)
14132 ++end;
14133 glyph_before = glyph + 1;
14134 glyph_after = end;
14135 }
14136 }
14137 else if (row->reversed_p)
14138 {
14139 /* In R2L rows that don't display text, put the cursor on the
14140 rightmost glyph. Case in point: an empty last line that is
14141 part of an R2L paragraph. */
14142 cursor = end - 1;
14143 /* Avoid placing the cursor on the last glyph of the row, where
14144 on terminal frames we hold the vertical border between
14145 adjacent windows. */
14146 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14147 && !WINDOW_RIGHTMOST_P (w)
14148 && cursor == row->glyphs[LAST_AREA] - 1)
14149 cursor--;
14150 x = -1; /* will be computed below, at label compute_x */
14151 }
14152
14153 /* Step 1: Try to find the glyph whose character position
14154 corresponds to point. If that's not possible, find 2 glyphs
14155 whose character positions are the closest to point, one before
14156 point, the other after it. */
14157 if (!row->reversed_p)
14158 while (/* not marched to end of glyph row */
14159 glyph < end
14160 /* glyph was not inserted by redisplay for internal purposes */
14161 && !INTEGERP (glyph->object))
14162 {
14163 if (BUFFERP (glyph->object))
14164 {
14165 ptrdiff_t dpos = glyph->charpos - pt_old;
14166
14167 if (glyph->charpos > bpos_max)
14168 bpos_max = glyph->charpos;
14169 if (glyph->charpos < bpos_min)
14170 bpos_min = glyph->charpos;
14171 if (!glyph->avoid_cursor_p)
14172 {
14173 /* If we hit point, we've found the glyph on which to
14174 display the cursor. */
14175 if (dpos == 0)
14176 {
14177 match_with_avoid_cursor = 0;
14178 break;
14179 }
14180 /* See if we've found a better approximation to
14181 POS_BEFORE or to POS_AFTER. */
14182 if (0 > dpos && dpos > pos_before - pt_old)
14183 {
14184 pos_before = glyph->charpos;
14185 glyph_before = glyph;
14186 }
14187 else if (0 < dpos && dpos < pos_after - pt_old)
14188 {
14189 pos_after = glyph->charpos;
14190 glyph_after = glyph;
14191 }
14192 }
14193 else if (dpos == 0)
14194 match_with_avoid_cursor = 1;
14195 }
14196 else if (STRINGP (glyph->object))
14197 {
14198 Lisp_Object chprop;
14199 ptrdiff_t glyph_pos = glyph->charpos;
14200
14201 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14202 glyph->object);
14203 if (!NILP (chprop))
14204 {
14205 /* If the string came from a `display' text property,
14206 look up the buffer position of that property and
14207 use that position to update bpos_max, as if we
14208 actually saw such a position in one of the row's
14209 glyphs. This helps with supporting integer values
14210 of `cursor' property on the display string in
14211 situations where most or all of the row's buffer
14212 text is completely covered by display properties,
14213 so that no glyph with valid buffer positions is
14214 ever seen in the row. */
14215 ptrdiff_t prop_pos =
14216 string_buffer_position_lim (glyph->object, pos_before,
14217 pos_after, 0);
14218
14219 if (prop_pos >= pos_before)
14220 bpos_max = prop_pos - 1;
14221 }
14222 if (INTEGERP (chprop))
14223 {
14224 bpos_covered = bpos_max + XINT (chprop);
14225 /* If the `cursor' property covers buffer positions up
14226 to and including point, we should display cursor on
14227 this glyph. Note that, if a `cursor' property on one
14228 of the string's characters has an integer value, we
14229 will break out of the loop below _before_ we get to
14230 the position match above. IOW, integer values of
14231 the `cursor' property override the "exact match for
14232 point" strategy of positioning the cursor. */
14233 /* Implementation note: bpos_max == pt_old when, e.g.,
14234 we are in an empty line, where bpos_max is set to
14235 MATRIX_ROW_START_CHARPOS, see above. */
14236 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14237 {
14238 cursor = glyph;
14239 break;
14240 }
14241 }
14242
14243 string_seen = 1;
14244 }
14245 x += glyph->pixel_width;
14246 ++glyph;
14247 }
14248 else if (glyph > end) /* row is reversed */
14249 while (!INTEGERP (glyph->object))
14250 {
14251 if (BUFFERP (glyph->object))
14252 {
14253 ptrdiff_t dpos = glyph->charpos - pt_old;
14254
14255 if (glyph->charpos > bpos_max)
14256 bpos_max = glyph->charpos;
14257 if (glyph->charpos < bpos_min)
14258 bpos_min = glyph->charpos;
14259 if (!glyph->avoid_cursor_p)
14260 {
14261 if (dpos == 0)
14262 {
14263 match_with_avoid_cursor = 0;
14264 break;
14265 }
14266 if (0 > dpos && dpos > pos_before - pt_old)
14267 {
14268 pos_before = glyph->charpos;
14269 glyph_before = glyph;
14270 }
14271 else if (0 < dpos && dpos < pos_after - pt_old)
14272 {
14273 pos_after = glyph->charpos;
14274 glyph_after = glyph;
14275 }
14276 }
14277 else if (dpos == 0)
14278 match_with_avoid_cursor = 1;
14279 }
14280 else if (STRINGP (glyph->object))
14281 {
14282 Lisp_Object chprop;
14283 ptrdiff_t glyph_pos = glyph->charpos;
14284
14285 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14286 glyph->object);
14287 if (!NILP (chprop))
14288 {
14289 ptrdiff_t prop_pos =
14290 string_buffer_position_lim (glyph->object, pos_before,
14291 pos_after, 0);
14292
14293 if (prop_pos >= pos_before)
14294 bpos_max = prop_pos - 1;
14295 }
14296 if (INTEGERP (chprop))
14297 {
14298 bpos_covered = bpos_max + XINT (chprop);
14299 /* If the `cursor' property covers buffer positions up
14300 to and including point, we should display cursor on
14301 this glyph. */
14302 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14303 {
14304 cursor = glyph;
14305 break;
14306 }
14307 }
14308 string_seen = 1;
14309 }
14310 --glyph;
14311 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14312 {
14313 x--; /* can't use any pixel_width */
14314 break;
14315 }
14316 x -= glyph->pixel_width;
14317 }
14318
14319 /* Step 2: If we didn't find an exact match for point, we need to
14320 look for a proper place to put the cursor among glyphs between
14321 GLYPH_BEFORE and GLYPH_AFTER. */
14322 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14323 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14324 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14325 {
14326 /* An empty line has a single glyph whose OBJECT is zero and
14327 whose CHARPOS is the position of a newline on that line.
14328 Note that on a TTY, there are more glyphs after that, which
14329 were produced by extend_face_to_end_of_line, but their
14330 CHARPOS is zero or negative. */
14331 int empty_line_p =
14332 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14333 && INTEGERP (glyph->object) && glyph->charpos > 0
14334 /* On a TTY, continued and truncated rows also have a glyph at
14335 their end whose OBJECT is zero and whose CHARPOS is
14336 positive (the continuation and truncation glyphs), but such
14337 rows are obviously not "empty". */
14338 && !(row->continued_p || row->truncated_on_right_p);
14339
14340 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14341 {
14342 ptrdiff_t ellipsis_pos;
14343
14344 /* Scan back over the ellipsis glyphs. */
14345 if (!row->reversed_p)
14346 {
14347 ellipsis_pos = (glyph - 1)->charpos;
14348 while (glyph > row->glyphs[TEXT_AREA]
14349 && (glyph - 1)->charpos == ellipsis_pos)
14350 glyph--, x -= glyph->pixel_width;
14351 /* That loop always goes one position too far, including
14352 the glyph before the ellipsis. So scan forward over
14353 that one. */
14354 x += glyph->pixel_width;
14355 glyph++;
14356 }
14357 else /* row is reversed */
14358 {
14359 ellipsis_pos = (glyph + 1)->charpos;
14360 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14361 && (glyph + 1)->charpos == ellipsis_pos)
14362 glyph++, x += glyph->pixel_width;
14363 x -= glyph->pixel_width;
14364 glyph--;
14365 }
14366 }
14367 else if (match_with_avoid_cursor)
14368 {
14369 cursor = glyph_after;
14370 x = -1;
14371 }
14372 else if (string_seen)
14373 {
14374 int incr = row->reversed_p ? -1 : +1;
14375
14376 /* Need to find the glyph that came out of a string which is
14377 present at point. That glyph is somewhere between
14378 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14379 positioned between POS_BEFORE and POS_AFTER in the
14380 buffer. */
14381 struct glyph *start, *stop;
14382 ptrdiff_t pos = pos_before;
14383
14384 x = -1;
14385
14386 /* If the row ends in a newline from a display string,
14387 reordering could have moved the glyphs belonging to the
14388 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14389 in this case we extend the search to the last glyph in
14390 the row that was not inserted by redisplay. */
14391 if (row->ends_in_newline_from_string_p)
14392 {
14393 glyph_after = end;
14394 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14395 }
14396
14397 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14398 correspond to POS_BEFORE and POS_AFTER, respectively. We
14399 need START and STOP in the order that corresponds to the
14400 row's direction as given by its reversed_p flag. If the
14401 directionality of characters between POS_BEFORE and
14402 POS_AFTER is the opposite of the row's base direction,
14403 these characters will have been reordered for display,
14404 and we need to reverse START and STOP. */
14405 if (!row->reversed_p)
14406 {
14407 start = min (glyph_before, glyph_after);
14408 stop = max (glyph_before, glyph_after);
14409 }
14410 else
14411 {
14412 start = max (glyph_before, glyph_after);
14413 stop = min (glyph_before, glyph_after);
14414 }
14415 for (glyph = start + incr;
14416 row->reversed_p ? glyph > stop : glyph < stop; )
14417 {
14418
14419 /* Any glyphs that come from the buffer are here because
14420 of bidi reordering. Skip them, and only pay
14421 attention to glyphs that came from some string. */
14422 if (STRINGP (glyph->object))
14423 {
14424 Lisp_Object str;
14425 ptrdiff_t tem;
14426 /* If the display property covers the newline, we
14427 need to search for it one position farther. */
14428 ptrdiff_t lim = pos_after
14429 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14430
14431 string_from_text_prop = 0;
14432 str = glyph->object;
14433 tem = string_buffer_position_lim (str, pos, lim, 0);
14434 if (tem == 0 /* from overlay */
14435 || pos <= tem)
14436 {
14437 /* If the string from which this glyph came is
14438 found in the buffer at point, or at position
14439 that is closer to point than pos_after, then
14440 we've found the glyph we've been looking for.
14441 If it comes from an overlay (tem == 0), and
14442 it has the `cursor' property on one of its
14443 glyphs, record that glyph as a candidate for
14444 displaying the cursor. (As in the
14445 unidirectional version, we will display the
14446 cursor on the last candidate we find.) */
14447 if (tem == 0
14448 || tem == pt_old
14449 || (tem - pt_old > 0 && tem < pos_after))
14450 {
14451 /* The glyphs from this string could have
14452 been reordered. Find the one with the
14453 smallest string position. Or there could
14454 be a character in the string with the
14455 `cursor' property, which means display
14456 cursor on that character's glyph. */
14457 ptrdiff_t strpos = glyph->charpos;
14458
14459 if (tem)
14460 {
14461 cursor = glyph;
14462 string_from_text_prop = 1;
14463 }
14464 for ( ;
14465 (row->reversed_p ? glyph > stop : glyph < stop)
14466 && EQ (glyph->object, str);
14467 glyph += incr)
14468 {
14469 Lisp_Object cprop;
14470 ptrdiff_t gpos = glyph->charpos;
14471
14472 cprop = Fget_char_property (make_number (gpos),
14473 Qcursor,
14474 glyph->object);
14475 if (!NILP (cprop))
14476 {
14477 cursor = glyph;
14478 break;
14479 }
14480 if (tem && glyph->charpos < strpos)
14481 {
14482 strpos = glyph->charpos;
14483 cursor = glyph;
14484 }
14485 }
14486
14487 if (tem == pt_old
14488 || (tem - pt_old > 0 && tem < pos_after))
14489 goto compute_x;
14490 }
14491 if (tem)
14492 pos = tem + 1; /* don't find previous instances */
14493 }
14494 /* This string is not what we want; skip all of the
14495 glyphs that came from it. */
14496 while ((row->reversed_p ? glyph > stop : glyph < stop)
14497 && EQ (glyph->object, str))
14498 glyph += incr;
14499 }
14500 else
14501 glyph += incr;
14502 }
14503
14504 /* If we reached the end of the line, and END was from a string,
14505 the cursor is not on this line. */
14506 if (cursor == NULL
14507 && (row->reversed_p ? glyph <= end : glyph >= end)
14508 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14509 && STRINGP (end->object)
14510 && row->continued_p)
14511 return 0;
14512 }
14513 /* A truncated row may not include PT among its character positions.
14514 Setting the cursor inside the scroll margin will trigger
14515 recalculation of hscroll in hscroll_window_tree. But if a
14516 display string covers point, defer to the string-handling
14517 code below to figure this out. */
14518 else if (row->truncated_on_left_p && pt_old < bpos_min)
14519 {
14520 cursor = glyph_before;
14521 x = -1;
14522 }
14523 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14524 /* Zero-width characters produce no glyphs. */
14525 || (!empty_line_p
14526 && (row->reversed_p
14527 ? glyph_after > glyphs_end
14528 : glyph_after < glyphs_end)))
14529 {
14530 cursor = glyph_after;
14531 x = -1;
14532 }
14533 }
14534
14535 compute_x:
14536 if (cursor != NULL)
14537 glyph = cursor;
14538 else if (glyph == glyphs_end
14539 && pos_before == pos_after
14540 && STRINGP ((row->reversed_p
14541 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14542 : row->glyphs[TEXT_AREA])->object))
14543 {
14544 /* If all the glyphs of this row came from strings, put the
14545 cursor on the first glyph of the row. This avoids having the
14546 cursor outside of the text area in this very rare and hard
14547 use case. */
14548 glyph =
14549 row->reversed_p
14550 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14551 : row->glyphs[TEXT_AREA];
14552 }
14553 if (x < 0)
14554 {
14555 struct glyph *g;
14556
14557 /* Need to compute x that corresponds to GLYPH. */
14558 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14559 {
14560 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14561 emacs_abort ();
14562 x += g->pixel_width;
14563 }
14564 }
14565
14566 /* ROW could be part of a continued line, which, under bidi
14567 reordering, might have other rows whose start and end charpos
14568 occlude point. Only set w->cursor if we found a better
14569 approximation to the cursor position than we have from previously
14570 examined candidate rows belonging to the same continued line. */
14571 if (/* We already have a candidate row. */
14572 w->cursor.vpos >= 0
14573 /* That candidate is not the row we are processing. */
14574 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14575 /* Make sure cursor.vpos specifies a row whose start and end
14576 charpos occlude point, and it is valid candidate for being a
14577 cursor-row. This is because some callers of this function
14578 leave cursor.vpos at the row where the cursor was displayed
14579 during the last redisplay cycle. */
14580 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14581 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14582 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14583 {
14584 struct glyph *g1
14585 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14586
14587 /* Don't consider glyphs that are outside TEXT_AREA. */
14588 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14589 return 0;
14590 /* Keep the candidate whose buffer position is the closest to
14591 point or has the `cursor' property. */
14592 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14593 w->cursor.hpos >= 0
14594 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14595 && ((BUFFERP (g1->object)
14596 && (g1->charpos == pt_old /* An exact match always wins. */
14597 || (BUFFERP (glyph->object)
14598 && eabs (g1->charpos - pt_old)
14599 < eabs (glyph->charpos - pt_old))))
14600 /* Previous candidate is a glyph from a string that has
14601 a non-nil `cursor' property. */
14602 || (STRINGP (g1->object)
14603 && (!NILP (Fget_char_property (make_number (g1->charpos),
14604 Qcursor, g1->object))
14605 /* Previous candidate is from the same display
14606 string as this one, and the display string
14607 came from a text property. */
14608 || (EQ (g1->object, glyph->object)
14609 && string_from_text_prop)
14610 /* this candidate is from newline and its
14611 position is not an exact match */
14612 || (INTEGERP (glyph->object)
14613 && glyph->charpos != pt_old)))))
14614 return 0;
14615 /* If this candidate gives an exact match, use that. */
14616 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14617 /* If this candidate is a glyph created for the
14618 terminating newline of a line, and point is on that
14619 newline, it wins because it's an exact match. */
14620 || (!row->continued_p
14621 && INTEGERP (glyph->object)
14622 && glyph->charpos == 0
14623 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14624 /* Otherwise, keep the candidate that comes from a row
14625 spanning less buffer positions. This may win when one or
14626 both candidate positions are on glyphs that came from
14627 display strings, for which we cannot compare buffer
14628 positions. */
14629 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14630 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14631 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14632 return 0;
14633 }
14634 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14635 w->cursor.x = x;
14636 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14637 w->cursor.y = row->y + dy;
14638
14639 if (w == XWINDOW (selected_window))
14640 {
14641 if (!row->continued_p
14642 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14643 && row->x == 0)
14644 {
14645 this_line_buffer = XBUFFER (w->contents);
14646
14647 CHARPOS (this_line_start_pos)
14648 = MATRIX_ROW_START_CHARPOS (row) + delta;
14649 BYTEPOS (this_line_start_pos)
14650 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14651
14652 CHARPOS (this_line_end_pos)
14653 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14654 BYTEPOS (this_line_end_pos)
14655 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14656
14657 this_line_y = w->cursor.y;
14658 this_line_pixel_height = row->height;
14659 this_line_vpos = w->cursor.vpos;
14660 this_line_start_x = row->x;
14661 }
14662 else
14663 CHARPOS (this_line_start_pos) = 0;
14664 }
14665
14666 return 1;
14667 }
14668
14669
14670 /* Run window scroll functions, if any, for WINDOW with new window
14671 start STARTP. Sets the window start of WINDOW to that position.
14672
14673 We assume that the window's buffer is really current. */
14674
14675 static struct text_pos
14676 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14677 {
14678 struct window *w = XWINDOW (window);
14679 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14680
14681 eassert (current_buffer == XBUFFER (w->contents));
14682
14683 if (!NILP (Vwindow_scroll_functions))
14684 {
14685 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14686 make_number (CHARPOS (startp)));
14687 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14688 /* In case the hook functions switch buffers. */
14689 set_buffer_internal (XBUFFER (w->contents));
14690 }
14691
14692 return startp;
14693 }
14694
14695
14696 /* Make sure the line containing the cursor is fully visible.
14697 A value of 1 means there is nothing to be done.
14698 (Either the line is fully visible, or it cannot be made so,
14699 or we cannot tell.)
14700
14701 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14702 is higher than window.
14703
14704 A value of 0 means the caller should do scrolling
14705 as if point had gone off the screen. */
14706
14707 static int
14708 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14709 {
14710 struct glyph_matrix *matrix;
14711 struct glyph_row *row;
14712 int window_height;
14713
14714 if (!make_cursor_line_fully_visible_p)
14715 return 1;
14716
14717 /* It's not always possible to find the cursor, e.g, when a window
14718 is full of overlay strings. Don't do anything in that case. */
14719 if (w->cursor.vpos < 0)
14720 return 1;
14721
14722 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14723 row = MATRIX_ROW (matrix, w->cursor.vpos);
14724
14725 /* If the cursor row is not partially visible, there's nothing to do. */
14726 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14727 return 1;
14728
14729 /* If the row the cursor is in is taller than the window's height,
14730 it's not clear what to do, so do nothing. */
14731 window_height = window_box_height (w);
14732 if (row->height >= window_height)
14733 {
14734 if (!force_p || MINI_WINDOW_P (w)
14735 || w->vscroll || w->cursor.vpos == 0)
14736 return 1;
14737 }
14738 return 0;
14739 }
14740
14741
14742 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14743 non-zero means only WINDOW is redisplayed in redisplay_internal.
14744 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14745 in redisplay_window to bring a partially visible line into view in
14746 the case that only the cursor has moved.
14747
14748 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14749 last screen line's vertical height extends past the end of the screen.
14750
14751 Value is
14752
14753 1 if scrolling succeeded
14754
14755 0 if scrolling didn't find point.
14756
14757 -1 if new fonts have been loaded so that we must interrupt
14758 redisplay, adjust glyph matrices, and try again. */
14759
14760 enum
14761 {
14762 SCROLLING_SUCCESS,
14763 SCROLLING_FAILED,
14764 SCROLLING_NEED_LARGER_MATRICES
14765 };
14766
14767 /* If scroll-conservatively is more than this, never recenter.
14768
14769 If you change this, don't forget to update the doc string of
14770 `scroll-conservatively' and the Emacs manual. */
14771 #define SCROLL_LIMIT 100
14772
14773 static int
14774 try_scrolling (Lisp_Object window, int just_this_one_p,
14775 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14776 int temp_scroll_step, int last_line_misfit)
14777 {
14778 struct window *w = XWINDOW (window);
14779 struct frame *f = XFRAME (w->frame);
14780 struct text_pos pos, startp;
14781 struct it it;
14782 int this_scroll_margin, scroll_max, rc, height;
14783 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14784 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14785 Lisp_Object aggressive;
14786 /* We will never try scrolling more than this number of lines. */
14787 int scroll_limit = SCROLL_LIMIT;
14788 int frame_line_height = default_line_pixel_height (w);
14789 int window_total_lines
14790 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14791
14792 #ifdef GLYPH_DEBUG
14793 debug_method_add (w, "try_scrolling");
14794 #endif
14795
14796 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14797
14798 /* Compute scroll margin height in pixels. We scroll when point is
14799 within this distance from the top or bottom of the window. */
14800 if (scroll_margin > 0)
14801 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14802 * frame_line_height;
14803 else
14804 this_scroll_margin = 0;
14805
14806 /* Force arg_scroll_conservatively to have a reasonable value, to
14807 avoid scrolling too far away with slow move_it_* functions. Note
14808 that the user can supply scroll-conservatively equal to
14809 `most-positive-fixnum', which can be larger than INT_MAX. */
14810 if (arg_scroll_conservatively > scroll_limit)
14811 {
14812 arg_scroll_conservatively = scroll_limit + 1;
14813 scroll_max = scroll_limit * frame_line_height;
14814 }
14815 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14816 /* Compute how much we should try to scroll maximally to bring
14817 point into view. */
14818 scroll_max = (max (scroll_step,
14819 max (arg_scroll_conservatively, temp_scroll_step))
14820 * frame_line_height);
14821 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14822 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14823 /* We're trying to scroll because of aggressive scrolling but no
14824 scroll_step is set. Choose an arbitrary one. */
14825 scroll_max = 10 * frame_line_height;
14826 else
14827 scroll_max = 0;
14828
14829 too_near_end:
14830
14831 /* Decide whether to scroll down. */
14832 if (PT > CHARPOS (startp))
14833 {
14834 int scroll_margin_y;
14835
14836 /* Compute the pixel ypos of the scroll margin, then move IT to
14837 either that ypos or PT, whichever comes first. */
14838 start_display (&it, w, startp);
14839 scroll_margin_y = it.last_visible_y - this_scroll_margin
14840 - frame_line_height * extra_scroll_margin_lines;
14841 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14842 (MOVE_TO_POS | MOVE_TO_Y));
14843
14844 if (PT > CHARPOS (it.current.pos))
14845 {
14846 int y0 = line_bottom_y (&it);
14847 /* Compute how many pixels below window bottom to stop searching
14848 for PT. This avoids costly search for PT that is far away if
14849 the user limited scrolling by a small number of lines, but
14850 always finds PT if scroll_conservatively is set to a large
14851 number, such as most-positive-fixnum. */
14852 int slack = max (scroll_max, 10 * frame_line_height);
14853 int y_to_move = it.last_visible_y + slack;
14854
14855 /* Compute the distance from the scroll margin to PT or to
14856 the scroll limit, whichever comes first. This should
14857 include the height of the cursor line, to make that line
14858 fully visible. */
14859 move_it_to (&it, PT, -1, y_to_move,
14860 -1, MOVE_TO_POS | MOVE_TO_Y);
14861 dy = line_bottom_y (&it) - y0;
14862
14863 if (dy > scroll_max)
14864 return SCROLLING_FAILED;
14865
14866 if (dy > 0)
14867 scroll_down_p = 1;
14868 }
14869 }
14870
14871 if (scroll_down_p)
14872 {
14873 /* Point is in or below the bottom scroll margin, so move the
14874 window start down. If scrolling conservatively, move it just
14875 enough down to make point visible. If scroll_step is set,
14876 move it down by scroll_step. */
14877 if (arg_scroll_conservatively)
14878 amount_to_scroll
14879 = min (max (dy, frame_line_height),
14880 frame_line_height * arg_scroll_conservatively);
14881 else if (scroll_step || temp_scroll_step)
14882 amount_to_scroll = scroll_max;
14883 else
14884 {
14885 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14886 height = WINDOW_BOX_TEXT_HEIGHT (w);
14887 if (NUMBERP (aggressive))
14888 {
14889 double float_amount = XFLOATINT (aggressive) * height;
14890 int aggressive_scroll = float_amount;
14891 if (aggressive_scroll == 0 && float_amount > 0)
14892 aggressive_scroll = 1;
14893 /* Don't let point enter the scroll margin near top of
14894 the window. This could happen if the value of
14895 scroll_up_aggressively is too large and there are
14896 non-zero margins, because scroll_up_aggressively
14897 means put point that fraction of window height
14898 _from_the_bottom_margin_. */
14899 if (aggressive_scroll + 2*this_scroll_margin > height)
14900 aggressive_scroll = height - 2*this_scroll_margin;
14901 amount_to_scroll = dy + aggressive_scroll;
14902 }
14903 }
14904
14905 if (amount_to_scroll <= 0)
14906 return SCROLLING_FAILED;
14907
14908 start_display (&it, w, startp);
14909 if (arg_scroll_conservatively <= scroll_limit)
14910 move_it_vertically (&it, amount_to_scroll);
14911 else
14912 {
14913 /* Extra precision for users who set scroll-conservatively
14914 to a large number: make sure the amount we scroll
14915 the window start is never less than amount_to_scroll,
14916 which was computed as distance from window bottom to
14917 point. This matters when lines at window top and lines
14918 below window bottom have different height. */
14919 struct it it1;
14920 void *it1data = NULL;
14921 /* We use a temporary it1 because line_bottom_y can modify
14922 its argument, if it moves one line down; see there. */
14923 int start_y;
14924
14925 SAVE_IT (it1, it, it1data);
14926 start_y = line_bottom_y (&it1);
14927 do {
14928 RESTORE_IT (&it, &it, it1data);
14929 move_it_by_lines (&it, 1);
14930 SAVE_IT (it1, it, it1data);
14931 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14932 }
14933
14934 /* If STARTP is unchanged, move it down another screen line. */
14935 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14936 move_it_by_lines (&it, 1);
14937 startp = it.current.pos;
14938 }
14939 else
14940 {
14941 struct text_pos scroll_margin_pos = startp;
14942 int y_offset = 0;
14943
14944 /* See if point is inside the scroll margin at the top of the
14945 window. */
14946 if (this_scroll_margin)
14947 {
14948 int y_start;
14949
14950 start_display (&it, w, startp);
14951 y_start = it.current_y;
14952 move_it_vertically (&it, this_scroll_margin);
14953 scroll_margin_pos = it.current.pos;
14954 /* If we didn't move enough before hitting ZV, request
14955 additional amount of scroll, to move point out of the
14956 scroll margin. */
14957 if (IT_CHARPOS (it) == ZV
14958 && it.current_y - y_start < this_scroll_margin)
14959 y_offset = this_scroll_margin - (it.current_y - y_start);
14960 }
14961
14962 if (PT < CHARPOS (scroll_margin_pos))
14963 {
14964 /* Point is in the scroll margin at the top of the window or
14965 above what is displayed in the window. */
14966 int y0, y_to_move;
14967
14968 /* Compute the vertical distance from PT to the scroll
14969 margin position. Move as far as scroll_max allows, or
14970 one screenful, or 10 screen lines, whichever is largest.
14971 Give up if distance is greater than scroll_max or if we
14972 didn't reach the scroll margin position. */
14973 SET_TEXT_POS (pos, PT, PT_BYTE);
14974 start_display (&it, w, pos);
14975 y0 = it.current_y;
14976 y_to_move = max (it.last_visible_y,
14977 max (scroll_max, 10 * frame_line_height));
14978 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14979 y_to_move, -1,
14980 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14981 dy = it.current_y - y0;
14982 if (dy > scroll_max
14983 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14984 return SCROLLING_FAILED;
14985
14986 /* Additional scroll for when ZV was too close to point. */
14987 dy += y_offset;
14988
14989 /* Compute new window start. */
14990 start_display (&it, w, startp);
14991
14992 if (arg_scroll_conservatively)
14993 amount_to_scroll = max (dy, frame_line_height *
14994 max (scroll_step, temp_scroll_step));
14995 else if (scroll_step || temp_scroll_step)
14996 amount_to_scroll = scroll_max;
14997 else
14998 {
14999 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15000 height = WINDOW_BOX_TEXT_HEIGHT (w);
15001 if (NUMBERP (aggressive))
15002 {
15003 double float_amount = XFLOATINT (aggressive) * height;
15004 int aggressive_scroll = float_amount;
15005 if (aggressive_scroll == 0 && float_amount > 0)
15006 aggressive_scroll = 1;
15007 /* Don't let point enter the scroll margin near
15008 bottom of the window, if the value of
15009 scroll_down_aggressively happens to be too
15010 large. */
15011 if (aggressive_scroll + 2*this_scroll_margin > height)
15012 aggressive_scroll = height - 2*this_scroll_margin;
15013 amount_to_scroll = dy + aggressive_scroll;
15014 }
15015 }
15016
15017 if (amount_to_scroll <= 0)
15018 return SCROLLING_FAILED;
15019
15020 move_it_vertically_backward (&it, amount_to_scroll);
15021 startp = it.current.pos;
15022 }
15023 }
15024
15025 /* Run window scroll functions. */
15026 startp = run_window_scroll_functions (window, startp);
15027
15028 /* Display the window. Give up if new fonts are loaded, or if point
15029 doesn't appear. */
15030 if (!try_window (window, startp, 0))
15031 rc = SCROLLING_NEED_LARGER_MATRICES;
15032 else if (w->cursor.vpos < 0)
15033 {
15034 clear_glyph_matrix (w->desired_matrix);
15035 rc = SCROLLING_FAILED;
15036 }
15037 else
15038 {
15039 /* Maybe forget recorded base line for line number display. */
15040 if (!just_this_one_p
15041 || current_buffer->clip_changed
15042 || BEG_UNCHANGED < CHARPOS (startp))
15043 w->base_line_number = 0;
15044
15045 /* If cursor ends up on a partially visible line,
15046 treat that as being off the bottom of the screen. */
15047 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15048 /* It's possible that the cursor is on the first line of the
15049 buffer, which is partially obscured due to a vscroll
15050 (Bug#7537). In that case, avoid looping forever. */
15051 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15052 {
15053 clear_glyph_matrix (w->desired_matrix);
15054 ++extra_scroll_margin_lines;
15055 goto too_near_end;
15056 }
15057 rc = SCROLLING_SUCCESS;
15058 }
15059
15060 return rc;
15061 }
15062
15063
15064 /* Compute a suitable window start for window W if display of W starts
15065 on a continuation line. Value is non-zero if a new window start
15066 was computed.
15067
15068 The new window start will be computed, based on W's width, starting
15069 from the start of the continued line. It is the start of the
15070 screen line with the minimum distance from the old start W->start. */
15071
15072 static int
15073 compute_window_start_on_continuation_line (struct window *w)
15074 {
15075 struct text_pos pos, start_pos;
15076 int window_start_changed_p = 0;
15077
15078 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15079
15080 /* If window start is on a continuation line... Window start may be
15081 < BEGV in case there's invisible text at the start of the
15082 buffer (M-x rmail, for example). */
15083 if (CHARPOS (start_pos) > BEGV
15084 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15085 {
15086 struct it it;
15087 struct glyph_row *row;
15088
15089 /* Handle the case that the window start is out of range. */
15090 if (CHARPOS (start_pos) < BEGV)
15091 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15092 else if (CHARPOS (start_pos) > ZV)
15093 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15094
15095 /* Find the start of the continued line. This should be fast
15096 because find_newline is fast (newline cache). */
15097 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15098 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15099 row, DEFAULT_FACE_ID);
15100 reseat_at_previous_visible_line_start (&it);
15101
15102 /* If the line start is "too far" away from the window start,
15103 say it takes too much time to compute a new window start. */
15104 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15105 /* PXW: Do we need upper bounds here? */
15106 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15107 {
15108 int min_distance, distance;
15109
15110 /* Move forward by display lines to find the new window
15111 start. If window width was enlarged, the new start can
15112 be expected to be > the old start. If window width was
15113 decreased, the new window start will be < the old start.
15114 So, we're looking for the display line start with the
15115 minimum distance from the old window start. */
15116 pos = it.current.pos;
15117 min_distance = INFINITY;
15118 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15119 distance < min_distance)
15120 {
15121 min_distance = distance;
15122 pos = it.current.pos;
15123 if (it.line_wrap == WORD_WRAP)
15124 {
15125 /* Under WORD_WRAP, move_it_by_lines is likely to
15126 overshoot and stop not at the first, but the
15127 second character from the left margin. So in
15128 that case, we need a more tight control on the X
15129 coordinate of the iterator than move_it_by_lines
15130 promises in its contract. The method is to first
15131 go to the last (rightmost) visible character of a
15132 line, then move to the leftmost character on the
15133 next line in a separate call. */
15134 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15135 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15136 move_it_to (&it, ZV, 0,
15137 it.current_y + it.max_ascent + it.max_descent, -1,
15138 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15139 }
15140 else
15141 move_it_by_lines (&it, 1);
15142 }
15143
15144 /* Set the window start there. */
15145 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15146 window_start_changed_p = 1;
15147 }
15148 }
15149
15150 return window_start_changed_p;
15151 }
15152
15153
15154 /* Try cursor movement in case text has not changed in window WINDOW,
15155 with window start STARTP. Value is
15156
15157 CURSOR_MOVEMENT_SUCCESS if successful
15158
15159 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15160
15161 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15162 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15163 we want to scroll as if scroll-step were set to 1. See the code.
15164
15165 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15166 which case we have to abort this redisplay, and adjust matrices
15167 first. */
15168
15169 enum
15170 {
15171 CURSOR_MOVEMENT_SUCCESS,
15172 CURSOR_MOVEMENT_CANNOT_BE_USED,
15173 CURSOR_MOVEMENT_MUST_SCROLL,
15174 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15175 };
15176
15177 static int
15178 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15179 {
15180 struct window *w = XWINDOW (window);
15181 struct frame *f = XFRAME (w->frame);
15182 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15183
15184 #ifdef GLYPH_DEBUG
15185 if (inhibit_try_cursor_movement)
15186 return rc;
15187 #endif
15188
15189 /* Previously, there was a check for Lisp integer in the
15190 if-statement below. Now, this field is converted to
15191 ptrdiff_t, thus zero means invalid position in a buffer. */
15192 eassert (w->last_point > 0);
15193 /* Likewise there was a check whether window_end_vpos is nil or larger
15194 than the window. Now window_end_vpos is int and so never nil, but
15195 let's leave eassert to check whether it fits in the window. */
15196 eassert (w->window_end_vpos < w->current_matrix->nrows);
15197
15198 /* Handle case where text has not changed, only point, and it has
15199 not moved off the frame. */
15200 if (/* Point may be in this window. */
15201 PT >= CHARPOS (startp)
15202 /* Selective display hasn't changed. */
15203 && !current_buffer->clip_changed
15204 /* Function force-mode-line-update is used to force a thorough
15205 redisplay. It sets either windows_or_buffers_changed or
15206 update_mode_lines. So don't take a shortcut here for these
15207 cases. */
15208 && !update_mode_lines
15209 && !windows_or_buffers_changed
15210 && !f->cursor_type_changed
15211 && NILP (Vshow_trailing_whitespace)
15212 /* This code is not used for mini-buffer for the sake of the case
15213 of redisplaying to replace an echo area message; since in
15214 that case the mini-buffer contents per se are usually
15215 unchanged. This code is of no real use in the mini-buffer
15216 since the handling of this_line_start_pos, etc., in redisplay
15217 handles the same cases. */
15218 && !EQ (window, minibuf_window)
15219 && (FRAME_WINDOW_P (f)
15220 || !overlay_arrow_in_current_buffer_p ()))
15221 {
15222 int this_scroll_margin, top_scroll_margin;
15223 struct glyph_row *row = NULL;
15224 int frame_line_height = default_line_pixel_height (w);
15225 int window_total_lines
15226 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15227
15228 #ifdef GLYPH_DEBUG
15229 debug_method_add (w, "cursor movement");
15230 #endif
15231
15232 /* Scroll if point within this distance from the top or bottom
15233 of the window. This is a pixel value. */
15234 if (scroll_margin > 0)
15235 {
15236 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15237 this_scroll_margin *= frame_line_height;
15238 }
15239 else
15240 this_scroll_margin = 0;
15241
15242 top_scroll_margin = this_scroll_margin;
15243 if (WINDOW_WANTS_HEADER_LINE_P (w))
15244 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15245
15246 /* Start with the row the cursor was displayed during the last
15247 not paused redisplay. Give up if that row is not valid. */
15248 if (w->last_cursor_vpos < 0
15249 || w->last_cursor_vpos >= w->current_matrix->nrows)
15250 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15251 else
15252 {
15253 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15254 if (row->mode_line_p)
15255 ++row;
15256 if (!row->enabled_p)
15257 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15258 }
15259
15260 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15261 {
15262 int scroll_p = 0, must_scroll = 0;
15263 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15264
15265 if (PT > w->last_point)
15266 {
15267 /* Point has moved forward. */
15268 while (MATRIX_ROW_END_CHARPOS (row) < PT
15269 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15270 {
15271 eassert (row->enabled_p);
15272 ++row;
15273 }
15274
15275 /* If the end position of a row equals the start
15276 position of the next row, and PT is at that position,
15277 we would rather display cursor in the next line. */
15278 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15279 && MATRIX_ROW_END_CHARPOS (row) == PT
15280 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15281 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15282 && !cursor_row_p (row))
15283 ++row;
15284
15285 /* If within the scroll margin, scroll. Note that
15286 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15287 the next line would be drawn, and that
15288 this_scroll_margin can be zero. */
15289 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15290 || PT > MATRIX_ROW_END_CHARPOS (row)
15291 /* Line is completely visible last line in window
15292 and PT is to be set in the next line. */
15293 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15294 && PT == MATRIX_ROW_END_CHARPOS (row)
15295 && !row->ends_at_zv_p
15296 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15297 scroll_p = 1;
15298 }
15299 else if (PT < w->last_point)
15300 {
15301 /* Cursor has to be moved backward. Note that PT >=
15302 CHARPOS (startp) because of the outer if-statement. */
15303 while (!row->mode_line_p
15304 && (MATRIX_ROW_START_CHARPOS (row) > PT
15305 || (MATRIX_ROW_START_CHARPOS (row) == PT
15306 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15307 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15308 row > w->current_matrix->rows
15309 && (row-1)->ends_in_newline_from_string_p))))
15310 && (row->y > top_scroll_margin
15311 || CHARPOS (startp) == BEGV))
15312 {
15313 eassert (row->enabled_p);
15314 --row;
15315 }
15316
15317 /* Consider the following case: Window starts at BEGV,
15318 there is invisible, intangible text at BEGV, so that
15319 display starts at some point START > BEGV. It can
15320 happen that we are called with PT somewhere between
15321 BEGV and START. Try to handle that case. */
15322 if (row < w->current_matrix->rows
15323 || row->mode_line_p)
15324 {
15325 row = w->current_matrix->rows;
15326 if (row->mode_line_p)
15327 ++row;
15328 }
15329
15330 /* Due to newlines in overlay strings, we may have to
15331 skip forward over overlay strings. */
15332 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15333 && MATRIX_ROW_END_CHARPOS (row) == PT
15334 && !cursor_row_p (row))
15335 ++row;
15336
15337 /* If within the scroll margin, scroll. */
15338 if (row->y < top_scroll_margin
15339 && CHARPOS (startp) != BEGV)
15340 scroll_p = 1;
15341 }
15342 else
15343 {
15344 /* Cursor did not move. So don't scroll even if cursor line
15345 is partially visible, as it was so before. */
15346 rc = CURSOR_MOVEMENT_SUCCESS;
15347 }
15348
15349 if (PT < MATRIX_ROW_START_CHARPOS (row)
15350 || PT > MATRIX_ROW_END_CHARPOS (row))
15351 {
15352 /* if PT is not in the glyph row, give up. */
15353 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15354 must_scroll = 1;
15355 }
15356 else if (rc != CURSOR_MOVEMENT_SUCCESS
15357 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15358 {
15359 struct glyph_row *row1;
15360
15361 /* If rows are bidi-reordered and point moved, back up
15362 until we find a row that does not belong to a
15363 continuation line. This is because we must consider
15364 all rows of a continued line as candidates for the
15365 new cursor positioning, since row start and end
15366 positions change non-linearly with vertical position
15367 in such rows. */
15368 /* FIXME: Revisit this when glyph ``spilling'' in
15369 continuation lines' rows is implemented for
15370 bidi-reordered rows. */
15371 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15372 MATRIX_ROW_CONTINUATION_LINE_P (row);
15373 --row)
15374 {
15375 /* If we hit the beginning of the displayed portion
15376 without finding the first row of a continued
15377 line, give up. */
15378 if (row <= row1)
15379 {
15380 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15381 break;
15382 }
15383 eassert (row->enabled_p);
15384 }
15385 }
15386 if (must_scroll)
15387 ;
15388 else if (rc != CURSOR_MOVEMENT_SUCCESS
15389 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15390 /* Make sure this isn't a header line by any chance, since
15391 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15392 && !row->mode_line_p
15393 && make_cursor_line_fully_visible_p)
15394 {
15395 if (PT == MATRIX_ROW_END_CHARPOS (row)
15396 && !row->ends_at_zv_p
15397 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15398 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15399 else if (row->height > window_box_height (w))
15400 {
15401 /* If we end up in a partially visible line, let's
15402 make it fully visible, except when it's taller
15403 than the window, in which case we can't do much
15404 about it. */
15405 *scroll_step = 1;
15406 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15407 }
15408 else
15409 {
15410 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15411 if (!cursor_row_fully_visible_p (w, 0, 1))
15412 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15413 else
15414 rc = CURSOR_MOVEMENT_SUCCESS;
15415 }
15416 }
15417 else if (scroll_p)
15418 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15419 else if (rc != CURSOR_MOVEMENT_SUCCESS
15420 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15421 {
15422 /* With bidi-reordered rows, there could be more than
15423 one candidate row whose start and end positions
15424 occlude point. We need to let set_cursor_from_row
15425 find the best candidate. */
15426 /* FIXME: Revisit this when glyph ``spilling'' in
15427 continuation lines' rows is implemented for
15428 bidi-reordered rows. */
15429 int rv = 0;
15430
15431 do
15432 {
15433 int at_zv_p = 0, exact_match_p = 0;
15434
15435 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15436 && PT <= MATRIX_ROW_END_CHARPOS (row)
15437 && cursor_row_p (row))
15438 rv |= set_cursor_from_row (w, row, w->current_matrix,
15439 0, 0, 0, 0);
15440 /* As soon as we've found the exact match for point,
15441 or the first suitable row whose ends_at_zv_p flag
15442 is set, we are done. */
15443 at_zv_p =
15444 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15445 if (rv && !at_zv_p
15446 && w->cursor.hpos >= 0
15447 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15448 w->cursor.vpos))
15449 {
15450 struct glyph_row *candidate =
15451 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15452 struct glyph *g =
15453 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15454 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15455
15456 exact_match_p =
15457 (BUFFERP (g->object) && g->charpos == PT)
15458 || (INTEGERP (g->object)
15459 && (g->charpos == PT
15460 || (g->charpos == 0 && endpos - 1 == PT)));
15461 }
15462 if (rv && (at_zv_p || exact_match_p))
15463 {
15464 rc = CURSOR_MOVEMENT_SUCCESS;
15465 break;
15466 }
15467 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15468 break;
15469 ++row;
15470 }
15471 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15472 || row->continued_p)
15473 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15474 || (MATRIX_ROW_START_CHARPOS (row) == PT
15475 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15476 /* If we didn't find any candidate rows, or exited the
15477 loop before all the candidates were examined, signal
15478 to the caller that this method failed. */
15479 if (rc != CURSOR_MOVEMENT_SUCCESS
15480 && !(rv
15481 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15482 && !row->continued_p))
15483 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15484 else if (rv)
15485 rc = CURSOR_MOVEMENT_SUCCESS;
15486 }
15487 else
15488 {
15489 do
15490 {
15491 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15492 {
15493 rc = CURSOR_MOVEMENT_SUCCESS;
15494 break;
15495 }
15496 ++row;
15497 }
15498 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15499 && MATRIX_ROW_START_CHARPOS (row) == PT
15500 && cursor_row_p (row));
15501 }
15502 }
15503 }
15504
15505 return rc;
15506 }
15507
15508 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15509 static
15510 #endif
15511 void
15512 set_vertical_scroll_bar (struct window *w)
15513 {
15514 ptrdiff_t start, end, whole;
15515
15516 /* Calculate the start and end positions for the current window.
15517 At some point, it would be nice to choose between scrollbars
15518 which reflect the whole buffer size, with special markers
15519 indicating narrowing, and scrollbars which reflect only the
15520 visible region.
15521
15522 Note that mini-buffers sometimes aren't displaying any text. */
15523 if (!MINI_WINDOW_P (w)
15524 || (w == XWINDOW (minibuf_window)
15525 && NILP (echo_area_buffer[0])))
15526 {
15527 struct buffer *buf = XBUFFER (w->contents);
15528 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15529 start = marker_position (w->start) - BUF_BEGV (buf);
15530 /* I don't think this is guaranteed to be right. For the
15531 moment, we'll pretend it is. */
15532 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15533
15534 if (end < start)
15535 end = start;
15536 if (whole < (end - start))
15537 whole = end - start;
15538 }
15539 else
15540 start = end = whole = 0;
15541
15542 /* Indicate what this scroll bar ought to be displaying now. */
15543 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15544 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15545 (w, end - start, whole, start);
15546 }
15547
15548
15549 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15550 selected_window is redisplayed.
15551
15552 We can return without actually redisplaying the window if fonts has been
15553 changed on window's frame. In that case, redisplay_internal will retry. */
15554
15555 static void
15556 redisplay_window (Lisp_Object window, bool just_this_one_p)
15557 {
15558 struct window *w = XWINDOW (window);
15559 struct frame *f = XFRAME (w->frame);
15560 struct buffer *buffer = XBUFFER (w->contents);
15561 struct buffer *old = current_buffer;
15562 struct text_pos lpoint, opoint, startp;
15563 int update_mode_line;
15564 int tem;
15565 struct it it;
15566 /* Record it now because it's overwritten. */
15567 bool current_matrix_up_to_date_p = false;
15568 bool used_current_matrix_p = false;
15569 /* This is less strict than current_matrix_up_to_date_p.
15570 It indicates that the buffer contents and narrowing are unchanged. */
15571 bool buffer_unchanged_p = false;
15572 int temp_scroll_step = 0;
15573 ptrdiff_t count = SPECPDL_INDEX ();
15574 int rc;
15575 int centering_position = -1;
15576 int last_line_misfit = 0;
15577 ptrdiff_t beg_unchanged, end_unchanged;
15578 int frame_line_height;
15579
15580 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15581 opoint = lpoint;
15582
15583 #ifdef GLYPH_DEBUG
15584 *w->desired_matrix->method = 0;
15585 #endif
15586
15587 if (!just_this_one_p
15588 && REDISPLAY_SOME_P ()
15589 && !w->redisplay
15590 && !f->redisplay
15591 && !buffer->text->redisplay)
15592 return;
15593
15594 /* Make sure that both W's markers are valid. */
15595 eassert (XMARKER (w->start)->buffer == buffer);
15596 eassert (XMARKER (w->pointm)->buffer == buffer);
15597
15598 restart:
15599 reconsider_clip_changes (w);
15600 frame_line_height = default_line_pixel_height (w);
15601
15602 /* Has the mode line to be updated? */
15603 update_mode_line = (w->update_mode_line
15604 || update_mode_lines
15605 || buffer->clip_changed
15606 || buffer->prevent_redisplay_optimizations_p);
15607
15608 if (!just_this_one_p)
15609 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15610 cleverly elsewhere. */
15611 w->must_be_updated_p = true;
15612
15613 if (MINI_WINDOW_P (w))
15614 {
15615 if (w == XWINDOW (echo_area_window)
15616 && !NILP (echo_area_buffer[0]))
15617 {
15618 if (update_mode_line)
15619 /* We may have to update a tty frame's menu bar or a
15620 tool-bar. Example `M-x C-h C-h C-g'. */
15621 goto finish_menu_bars;
15622 else
15623 /* We've already displayed the echo area glyphs in this window. */
15624 goto finish_scroll_bars;
15625 }
15626 else if ((w != XWINDOW (minibuf_window)
15627 || minibuf_level == 0)
15628 /* When buffer is nonempty, redisplay window normally. */
15629 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15630 /* Quail displays non-mini buffers in minibuffer window.
15631 In that case, redisplay the window normally. */
15632 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15633 {
15634 /* W is a mini-buffer window, but it's not active, so clear
15635 it. */
15636 int yb = window_text_bottom_y (w);
15637 struct glyph_row *row;
15638 int y;
15639
15640 for (y = 0, row = w->desired_matrix->rows;
15641 y < yb;
15642 y += row->height, ++row)
15643 blank_row (w, row, y);
15644 goto finish_scroll_bars;
15645 }
15646
15647 clear_glyph_matrix (w->desired_matrix);
15648 }
15649
15650 /* Otherwise set up data on this window; select its buffer and point
15651 value. */
15652 /* Really select the buffer, for the sake of buffer-local
15653 variables. */
15654 set_buffer_internal_1 (XBUFFER (w->contents));
15655
15656 current_matrix_up_to_date_p
15657 = (w->window_end_valid
15658 && !current_buffer->clip_changed
15659 && !current_buffer->prevent_redisplay_optimizations_p
15660 && !window_outdated (w));
15661
15662 /* Run the window-bottom-change-functions
15663 if it is possible that the text on the screen has changed
15664 (either due to modification of the text, or any other reason). */
15665 if (!current_matrix_up_to_date_p
15666 && !NILP (Vwindow_text_change_functions))
15667 {
15668 safe_run_hooks (Qwindow_text_change_functions);
15669 goto restart;
15670 }
15671
15672 beg_unchanged = BEG_UNCHANGED;
15673 end_unchanged = END_UNCHANGED;
15674
15675 SET_TEXT_POS (opoint, PT, PT_BYTE);
15676
15677 specbind (Qinhibit_point_motion_hooks, Qt);
15678
15679 buffer_unchanged_p
15680 = (w->window_end_valid
15681 && !current_buffer->clip_changed
15682 && !window_outdated (w));
15683
15684 /* When windows_or_buffers_changed is non-zero, we can't rely
15685 on the window end being valid, so set it to zero there. */
15686 if (windows_or_buffers_changed)
15687 {
15688 /* If window starts on a continuation line, maybe adjust the
15689 window start in case the window's width changed. */
15690 if (XMARKER (w->start)->buffer == current_buffer)
15691 compute_window_start_on_continuation_line (w);
15692
15693 w->window_end_valid = false;
15694 /* If so, we also can't rely on current matrix
15695 and should not fool try_cursor_movement below. */
15696 current_matrix_up_to_date_p = false;
15697 }
15698
15699 /* Some sanity checks. */
15700 CHECK_WINDOW_END (w);
15701 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15702 emacs_abort ();
15703 if (BYTEPOS (opoint) < CHARPOS (opoint))
15704 emacs_abort ();
15705
15706 if (mode_line_update_needed (w))
15707 update_mode_line = 1;
15708
15709 /* Point refers normally to the selected window. For any other
15710 window, set up appropriate value. */
15711 if (!EQ (window, selected_window))
15712 {
15713 ptrdiff_t new_pt = marker_position (w->pointm);
15714 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15715 if (new_pt < BEGV)
15716 {
15717 new_pt = BEGV;
15718 new_pt_byte = BEGV_BYTE;
15719 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15720 }
15721 else if (new_pt > (ZV - 1))
15722 {
15723 new_pt = ZV;
15724 new_pt_byte = ZV_BYTE;
15725 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15726 }
15727
15728 /* We don't use SET_PT so that the point-motion hooks don't run. */
15729 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15730 }
15731
15732 /* If any of the character widths specified in the display table
15733 have changed, invalidate the width run cache. It's true that
15734 this may be a bit late to catch such changes, but the rest of
15735 redisplay goes (non-fatally) haywire when the display table is
15736 changed, so why should we worry about doing any better? */
15737 if (current_buffer->width_run_cache)
15738 {
15739 struct Lisp_Char_Table *disptab = buffer_display_table ();
15740
15741 if (! disptab_matches_widthtab
15742 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15743 {
15744 invalidate_region_cache (current_buffer,
15745 current_buffer->width_run_cache,
15746 BEG, Z);
15747 recompute_width_table (current_buffer, disptab);
15748 }
15749 }
15750
15751 /* If window-start is screwed up, choose a new one. */
15752 if (XMARKER (w->start)->buffer != current_buffer)
15753 goto recenter;
15754
15755 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15756
15757 /* If someone specified a new starting point but did not insist,
15758 check whether it can be used. */
15759 if (w->optional_new_start
15760 && CHARPOS (startp) >= BEGV
15761 && CHARPOS (startp) <= ZV)
15762 {
15763 w->optional_new_start = 0;
15764 start_display (&it, w, startp);
15765 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15766 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15767 if (IT_CHARPOS (it) == PT)
15768 w->force_start = 1;
15769 /* IT may overshoot PT if text at PT is invisible. */
15770 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15771 w->force_start = 1;
15772 }
15773
15774 force_start:
15775
15776 /* Handle case where place to start displaying has been specified,
15777 unless the specified location is outside the accessible range. */
15778 if (w->force_start || window_frozen_p (w))
15779 {
15780 /* We set this later on if we have to adjust point. */
15781 int new_vpos = -1;
15782
15783 w->force_start = 0;
15784 w->vscroll = 0;
15785 w->window_end_valid = 0;
15786
15787 /* Forget any recorded base line for line number display. */
15788 if (!buffer_unchanged_p)
15789 w->base_line_number = 0;
15790
15791 /* Redisplay the mode line. Select the buffer properly for that.
15792 Also, run the hook window-scroll-functions
15793 because we have scrolled. */
15794 /* Note, we do this after clearing force_start because
15795 if there's an error, it is better to forget about force_start
15796 than to get into an infinite loop calling the hook functions
15797 and having them get more errors. */
15798 if (!update_mode_line
15799 || ! NILP (Vwindow_scroll_functions))
15800 {
15801 update_mode_line = 1;
15802 w->update_mode_line = 1;
15803 startp = run_window_scroll_functions (window, startp);
15804 }
15805
15806 if (CHARPOS (startp) < BEGV)
15807 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15808 else if (CHARPOS (startp) > ZV)
15809 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15810
15811 /* Redisplay, then check if cursor has been set during the
15812 redisplay. Give up if new fonts were loaded. */
15813 /* We used to issue a CHECK_MARGINS argument to try_window here,
15814 but this causes scrolling to fail when point begins inside
15815 the scroll margin (bug#148) -- cyd */
15816 if (!try_window (window, startp, 0))
15817 {
15818 w->force_start = 1;
15819 clear_glyph_matrix (w->desired_matrix);
15820 goto need_larger_matrices;
15821 }
15822
15823 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15824 {
15825 /* If point does not appear, try to move point so it does
15826 appear. The desired matrix has been built above, so we
15827 can use it here. */
15828 new_vpos = window_box_height (w) / 2;
15829 }
15830
15831 if (!cursor_row_fully_visible_p (w, 0, 0))
15832 {
15833 /* Point does appear, but on a line partly visible at end of window.
15834 Move it back to a fully-visible line. */
15835 new_vpos = window_box_height (w);
15836 }
15837 else if (w->cursor.vpos >= 0)
15838 {
15839 /* Some people insist on not letting point enter the scroll
15840 margin, even though this part handles windows that didn't
15841 scroll at all. */
15842 int window_total_lines
15843 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15844 int margin = min (scroll_margin, window_total_lines / 4);
15845 int pixel_margin = margin * frame_line_height;
15846 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15847
15848 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15849 below, which finds the row to move point to, advances by
15850 the Y coordinate of the _next_ row, see the definition of
15851 MATRIX_ROW_BOTTOM_Y. */
15852 if (w->cursor.vpos < margin + header_line)
15853 {
15854 w->cursor.vpos = -1;
15855 clear_glyph_matrix (w->desired_matrix);
15856 goto try_to_scroll;
15857 }
15858 else
15859 {
15860 int window_height = window_box_height (w);
15861
15862 if (header_line)
15863 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15864 if (w->cursor.y >= window_height - pixel_margin)
15865 {
15866 w->cursor.vpos = -1;
15867 clear_glyph_matrix (w->desired_matrix);
15868 goto try_to_scroll;
15869 }
15870 }
15871 }
15872
15873 /* If we need to move point for either of the above reasons,
15874 now actually do it. */
15875 if (new_vpos >= 0)
15876 {
15877 struct glyph_row *row;
15878
15879 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15880 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15881 ++row;
15882
15883 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15884 MATRIX_ROW_START_BYTEPOS (row));
15885
15886 if (w != XWINDOW (selected_window))
15887 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15888 else if (current_buffer == old)
15889 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15890
15891 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15892
15893 /* If we are highlighting the region, then we just changed
15894 the region, so redisplay to show it. */
15895 /* FIXME: We need to (re)run pre-redisplay-function! */
15896 /* if (markpos_of_region () >= 0)
15897 {
15898 clear_glyph_matrix (w->desired_matrix);
15899 if (!try_window (window, startp, 0))
15900 goto need_larger_matrices;
15901 }
15902 */
15903 }
15904
15905 #ifdef GLYPH_DEBUG
15906 debug_method_add (w, "forced window start");
15907 #endif
15908 goto done;
15909 }
15910
15911 /* Handle case where text has not changed, only point, and it has
15912 not moved off the frame, and we are not retrying after hscroll.
15913 (current_matrix_up_to_date_p is nonzero when retrying.) */
15914 if (current_matrix_up_to_date_p
15915 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15916 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15917 {
15918 switch (rc)
15919 {
15920 case CURSOR_MOVEMENT_SUCCESS:
15921 used_current_matrix_p = 1;
15922 goto done;
15923
15924 case CURSOR_MOVEMENT_MUST_SCROLL:
15925 goto try_to_scroll;
15926
15927 default:
15928 emacs_abort ();
15929 }
15930 }
15931 /* If current starting point was originally the beginning of a line
15932 but no longer is, find a new starting point. */
15933 else if (w->start_at_line_beg
15934 && !(CHARPOS (startp) <= BEGV
15935 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15936 {
15937 #ifdef GLYPH_DEBUG
15938 debug_method_add (w, "recenter 1");
15939 #endif
15940 goto recenter;
15941 }
15942
15943 /* Try scrolling with try_window_id. Value is > 0 if update has
15944 been done, it is -1 if we know that the same window start will
15945 not work. It is 0 if unsuccessful for some other reason. */
15946 else if ((tem = try_window_id (w)) != 0)
15947 {
15948 #ifdef GLYPH_DEBUG
15949 debug_method_add (w, "try_window_id %d", tem);
15950 #endif
15951
15952 if (f->fonts_changed)
15953 goto need_larger_matrices;
15954 if (tem > 0)
15955 goto done;
15956
15957 /* Otherwise try_window_id has returned -1 which means that we
15958 don't want the alternative below this comment to execute. */
15959 }
15960 else if (CHARPOS (startp) >= BEGV
15961 && CHARPOS (startp) <= ZV
15962 && PT >= CHARPOS (startp)
15963 && (CHARPOS (startp) < ZV
15964 /* Avoid starting at end of buffer. */
15965 || CHARPOS (startp) == BEGV
15966 || !window_outdated (w)))
15967 {
15968 int d1, d2, d3, d4, d5, d6;
15969
15970 /* If first window line is a continuation line, and window start
15971 is inside the modified region, but the first change is before
15972 current window start, we must select a new window start.
15973
15974 However, if this is the result of a down-mouse event (e.g. by
15975 extending the mouse-drag-overlay), we don't want to select a
15976 new window start, since that would change the position under
15977 the mouse, resulting in an unwanted mouse-movement rather
15978 than a simple mouse-click. */
15979 if (!w->start_at_line_beg
15980 && NILP (do_mouse_tracking)
15981 && CHARPOS (startp) > BEGV
15982 && CHARPOS (startp) > BEG + beg_unchanged
15983 && CHARPOS (startp) <= Z - end_unchanged
15984 /* Even if w->start_at_line_beg is nil, a new window may
15985 start at a line_beg, since that's how set_buffer_window
15986 sets it. So, we need to check the return value of
15987 compute_window_start_on_continuation_line. (See also
15988 bug#197). */
15989 && XMARKER (w->start)->buffer == current_buffer
15990 && compute_window_start_on_continuation_line (w)
15991 /* It doesn't make sense to force the window start like we
15992 do at label force_start if it is already known that point
15993 will not be visible in the resulting window, because
15994 doing so will move point from its correct position
15995 instead of scrolling the window to bring point into view.
15996 See bug#9324. */
15997 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15998 {
15999 w->force_start = 1;
16000 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16001 goto force_start;
16002 }
16003
16004 #ifdef GLYPH_DEBUG
16005 debug_method_add (w, "same window start");
16006 #endif
16007
16008 /* Try to redisplay starting at same place as before.
16009 If point has not moved off frame, accept the results. */
16010 if (!current_matrix_up_to_date_p
16011 /* Don't use try_window_reusing_current_matrix in this case
16012 because a window scroll function can have changed the
16013 buffer. */
16014 || !NILP (Vwindow_scroll_functions)
16015 || MINI_WINDOW_P (w)
16016 || !(used_current_matrix_p
16017 = try_window_reusing_current_matrix (w)))
16018 {
16019 IF_DEBUG (debug_method_add (w, "1"));
16020 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16021 /* -1 means we need to scroll.
16022 0 means we need new matrices, but fonts_changed
16023 is set in that case, so we will detect it below. */
16024 goto try_to_scroll;
16025 }
16026
16027 if (f->fonts_changed)
16028 goto need_larger_matrices;
16029
16030 if (w->cursor.vpos >= 0)
16031 {
16032 if (!just_this_one_p
16033 || current_buffer->clip_changed
16034 || BEG_UNCHANGED < CHARPOS (startp))
16035 /* Forget any recorded base line for line number display. */
16036 w->base_line_number = 0;
16037
16038 if (!cursor_row_fully_visible_p (w, 1, 0))
16039 {
16040 clear_glyph_matrix (w->desired_matrix);
16041 last_line_misfit = 1;
16042 }
16043 /* Drop through and scroll. */
16044 else
16045 goto done;
16046 }
16047 else
16048 clear_glyph_matrix (w->desired_matrix);
16049 }
16050
16051 try_to_scroll:
16052
16053 /* Redisplay the mode line. Select the buffer properly for that. */
16054 if (!update_mode_line)
16055 {
16056 update_mode_line = 1;
16057 w->update_mode_line = 1;
16058 }
16059
16060 /* Try to scroll by specified few lines. */
16061 if ((scroll_conservatively
16062 || emacs_scroll_step
16063 || temp_scroll_step
16064 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16065 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16066 && CHARPOS (startp) >= BEGV
16067 && CHARPOS (startp) <= ZV)
16068 {
16069 /* The function returns -1 if new fonts were loaded, 1 if
16070 successful, 0 if not successful. */
16071 int ss = try_scrolling (window, just_this_one_p,
16072 scroll_conservatively,
16073 emacs_scroll_step,
16074 temp_scroll_step, last_line_misfit);
16075 switch (ss)
16076 {
16077 case SCROLLING_SUCCESS:
16078 goto done;
16079
16080 case SCROLLING_NEED_LARGER_MATRICES:
16081 goto need_larger_matrices;
16082
16083 case SCROLLING_FAILED:
16084 break;
16085
16086 default:
16087 emacs_abort ();
16088 }
16089 }
16090
16091 /* Finally, just choose a place to start which positions point
16092 according to user preferences. */
16093
16094 recenter:
16095
16096 #ifdef GLYPH_DEBUG
16097 debug_method_add (w, "recenter");
16098 #endif
16099
16100 /* Forget any previously recorded base line for line number display. */
16101 if (!buffer_unchanged_p)
16102 w->base_line_number = 0;
16103
16104 /* Determine the window start relative to point. */
16105 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16106 it.current_y = it.last_visible_y;
16107 if (centering_position < 0)
16108 {
16109 int window_total_lines
16110 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16111 int margin =
16112 scroll_margin > 0
16113 ? min (scroll_margin, window_total_lines / 4)
16114 : 0;
16115 ptrdiff_t margin_pos = CHARPOS (startp);
16116 Lisp_Object aggressive;
16117 int scrolling_up;
16118
16119 /* If there is a scroll margin at the top of the window, find
16120 its character position. */
16121 if (margin
16122 /* Cannot call start_display if startp is not in the
16123 accessible region of the buffer. This can happen when we
16124 have just switched to a different buffer and/or changed
16125 its restriction. In that case, startp is initialized to
16126 the character position 1 (BEGV) because we did not yet
16127 have chance to display the buffer even once. */
16128 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16129 {
16130 struct it it1;
16131 void *it1data = NULL;
16132
16133 SAVE_IT (it1, it, it1data);
16134 start_display (&it1, w, startp);
16135 move_it_vertically (&it1, margin * frame_line_height);
16136 margin_pos = IT_CHARPOS (it1);
16137 RESTORE_IT (&it, &it, it1data);
16138 }
16139 scrolling_up = PT > margin_pos;
16140 aggressive =
16141 scrolling_up
16142 ? BVAR (current_buffer, scroll_up_aggressively)
16143 : BVAR (current_buffer, scroll_down_aggressively);
16144
16145 if (!MINI_WINDOW_P (w)
16146 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16147 {
16148 int pt_offset = 0;
16149
16150 /* Setting scroll-conservatively overrides
16151 scroll-*-aggressively. */
16152 if (!scroll_conservatively && NUMBERP (aggressive))
16153 {
16154 double float_amount = XFLOATINT (aggressive);
16155
16156 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16157 if (pt_offset == 0 && float_amount > 0)
16158 pt_offset = 1;
16159 if (pt_offset && margin > 0)
16160 margin -= 1;
16161 }
16162 /* Compute how much to move the window start backward from
16163 point so that point will be displayed where the user
16164 wants it. */
16165 if (scrolling_up)
16166 {
16167 centering_position = it.last_visible_y;
16168 if (pt_offset)
16169 centering_position -= pt_offset;
16170 centering_position -=
16171 frame_line_height * (1 + margin + (last_line_misfit != 0))
16172 + WINDOW_HEADER_LINE_HEIGHT (w);
16173 /* Don't let point enter the scroll margin near top of
16174 the window. */
16175 if (centering_position < margin * frame_line_height)
16176 centering_position = margin * frame_line_height;
16177 }
16178 else
16179 centering_position = margin * frame_line_height + pt_offset;
16180 }
16181 else
16182 /* Set the window start half the height of the window backward
16183 from point. */
16184 centering_position = window_box_height (w) / 2;
16185 }
16186 move_it_vertically_backward (&it, centering_position);
16187
16188 eassert (IT_CHARPOS (it) >= BEGV);
16189
16190 /* The function move_it_vertically_backward may move over more
16191 than the specified y-distance. If it->w is small, e.g. a
16192 mini-buffer window, we may end up in front of the window's
16193 display area. Start displaying at the start of the line
16194 containing PT in this case. */
16195 if (it.current_y <= 0)
16196 {
16197 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16198 move_it_vertically_backward (&it, 0);
16199 it.current_y = 0;
16200 }
16201
16202 it.current_x = it.hpos = 0;
16203
16204 /* Set the window start position here explicitly, to avoid an
16205 infinite loop in case the functions in window-scroll-functions
16206 get errors. */
16207 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16208
16209 /* Run scroll hooks. */
16210 startp = run_window_scroll_functions (window, it.current.pos);
16211
16212 /* Redisplay the window. */
16213 if (!current_matrix_up_to_date_p
16214 || windows_or_buffers_changed
16215 || f->cursor_type_changed
16216 /* Don't use try_window_reusing_current_matrix in this case
16217 because it can have changed the buffer. */
16218 || !NILP (Vwindow_scroll_functions)
16219 || !just_this_one_p
16220 || MINI_WINDOW_P (w)
16221 || !(used_current_matrix_p
16222 = try_window_reusing_current_matrix (w)))
16223 try_window (window, startp, 0);
16224
16225 /* If new fonts have been loaded (due to fontsets), give up. We
16226 have to start a new redisplay since we need to re-adjust glyph
16227 matrices. */
16228 if (f->fonts_changed)
16229 goto need_larger_matrices;
16230
16231 /* If cursor did not appear assume that the middle of the window is
16232 in the first line of the window. Do it again with the next line.
16233 (Imagine a window of height 100, displaying two lines of height
16234 60. Moving back 50 from it->last_visible_y will end in the first
16235 line.) */
16236 if (w->cursor.vpos < 0)
16237 {
16238 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16239 {
16240 clear_glyph_matrix (w->desired_matrix);
16241 move_it_by_lines (&it, 1);
16242 try_window (window, it.current.pos, 0);
16243 }
16244 else if (PT < IT_CHARPOS (it))
16245 {
16246 clear_glyph_matrix (w->desired_matrix);
16247 move_it_by_lines (&it, -1);
16248 try_window (window, it.current.pos, 0);
16249 }
16250 else
16251 {
16252 /* Not much we can do about it. */
16253 }
16254 }
16255
16256 /* Consider the following case: Window starts at BEGV, there is
16257 invisible, intangible text at BEGV, so that display starts at
16258 some point START > BEGV. It can happen that we are called with
16259 PT somewhere between BEGV and START. Try to handle that case. */
16260 if (w->cursor.vpos < 0)
16261 {
16262 struct glyph_row *row = w->current_matrix->rows;
16263 if (row->mode_line_p)
16264 ++row;
16265 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16266 }
16267
16268 if (!cursor_row_fully_visible_p (w, 0, 0))
16269 {
16270 /* If vscroll is enabled, disable it and try again. */
16271 if (w->vscroll)
16272 {
16273 w->vscroll = 0;
16274 clear_glyph_matrix (w->desired_matrix);
16275 goto recenter;
16276 }
16277
16278 /* Users who set scroll-conservatively to a large number want
16279 point just above/below the scroll margin. If we ended up
16280 with point's row partially visible, move the window start to
16281 make that row fully visible and out of the margin. */
16282 if (scroll_conservatively > SCROLL_LIMIT)
16283 {
16284 int window_total_lines
16285 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16286 int margin =
16287 scroll_margin > 0
16288 ? min (scroll_margin, window_total_lines / 4)
16289 : 0;
16290 int move_down = w->cursor.vpos >= window_total_lines / 2;
16291
16292 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16293 clear_glyph_matrix (w->desired_matrix);
16294 if (1 == try_window (window, it.current.pos,
16295 TRY_WINDOW_CHECK_MARGINS))
16296 goto done;
16297 }
16298
16299 /* If centering point failed to make the whole line visible,
16300 put point at the top instead. That has to make the whole line
16301 visible, if it can be done. */
16302 if (centering_position == 0)
16303 goto done;
16304
16305 clear_glyph_matrix (w->desired_matrix);
16306 centering_position = 0;
16307 goto recenter;
16308 }
16309
16310 done:
16311
16312 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16313 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16314 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16315
16316 /* Display the mode line, if we must. */
16317 if ((update_mode_line
16318 /* If window not full width, must redo its mode line
16319 if (a) the window to its side is being redone and
16320 (b) we do a frame-based redisplay. This is a consequence
16321 of how inverted lines are drawn in frame-based redisplay. */
16322 || (!just_this_one_p
16323 && !FRAME_WINDOW_P (f)
16324 && !WINDOW_FULL_WIDTH_P (w))
16325 /* Line number to display. */
16326 || w->base_line_pos > 0
16327 /* Column number is displayed and different from the one displayed. */
16328 || (w->column_number_displayed != -1
16329 && (w->column_number_displayed != current_column ())))
16330 /* This means that the window has a mode line. */
16331 && (WINDOW_WANTS_MODELINE_P (w)
16332 || WINDOW_WANTS_HEADER_LINE_P (w)))
16333 {
16334
16335 display_mode_lines (w);
16336
16337 /* If mode line height has changed, arrange for a thorough
16338 immediate redisplay using the correct mode line height. */
16339 if (WINDOW_WANTS_MODELINE_P (w)
16340 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16341 {
16342 f->fonts_changed = 1;
16343 w->mode_line_height = -1;
16344 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16345 = DESIRED_MODE_LINE_HEIGHT (w);
16346 }
16347
16348 /* If header line height has changed, arrange for a thorough
16349 immediate redisplay using the correct header line height. */
16350 if (WINDOW_WANTS_HEADER_LINE_P (w)
16351 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16352 {
16353 f->fonts_changed = 1;
16354 w->header_line_height = -1;
16355 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16356 = DESIRED_HEADER_LINE_HEIGHT (w);
16357 }
16358
16359 if (f->fonts_changed)
16360 goto need_larger_matrices;
16361 }
16362
16363 if (!line_number_displayed && w->base_line_pos != -1)
16364 {
16365 w->base_line_pos = 0;
16366 w->base_line_number = 0;
16367 }
16368
16369 finish_menu_bars:
16370
16371 /* When we reach a frame's selected window, redo the frame's menu bar. */
16372 if (update_mode_line
16373 && EQ (FRAME_SELECTED_WINDOW (f), window))
16374 {
16375 int redisplay_menu_p = 0;
16376
16377 if (FRAME_WINDOW_P (f))
16378 {
16379 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16380 || defined (HAVE_NS) || defined (USE_GTK)
16381 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16382 #else
16383 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16384 #endif
16385 }
16386 else
16387 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16388
16389 if (redisplay_menu_p)
16390 display_menu_bar (w);
16391
16392 #ifdef HAVE_WINDOW_SYSTEM
16393 if (FRAME_WINDOW_P (f))
16394 {
16395 #if defined (USE_GTK) || defined (HAVE_NS)
16396 if (FRAME_EXTERNAL_TOOL_BAR (f))
16397 redisplay_tool_bar (f);
16398 #else
16399 if (WINDOWP (f->tool_bar_window)
16400 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16401 || !NILP (Vauto_resize_tool_bars))
16402 && redisplay_tool_bar (f))
16403 ignore_mouse_drag_p = 1;
16404 #endif
16405 }
16406 #endif
16407 }
16408
16409 #ifdef HAVE_WINDOW_SYSTEM
16410 if (FRAME_WINDOW_P (f)
16411 && update_window_fringes (w, (just_this_one_p
16412 || (!used_current_matrix_p && !overlay_arrow_seen)
16413 || w->pseudo_window_p)))
16414 {
16415 update_begin (f);
16416 block_input ();
16417 if (draw_window_fringes (w, 1))
16418 {
16419 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16420 x_draw_right_divider (w);
16421 else
16422 x_draw_vertical_border (w);
16423 }
16424 unblock_input ();
16425 update_end (f);
16426 }
16427
16428 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16429 x_draw_bottom_divider (w);
16430 #endif /* HAVE_WINDOW_SYSTEM */
16431
16432 /* We go to this label, with fonts_changed set, if it is
16433 necessary to try again using larger glyph matrices.
16434 We have to redeem the scroll bar even in this case,
16435 because the loop in redisplay_internal expects that. */
16436 need_larger_matrices:
16437 ;
16438 finish_scroll_bars:
16439
16440 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16441 {
16442 /* Set the thumb's position and size. */
16443 set_vertical_scroll_bar (w);
16444
16445 /* Note that we actually used the scroll bar attached to this
16446 window, so it shouldn't be deleted at the end of redisplay. */
16447 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16448 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16449 }
16450
16451 /* Restore current_buffer and value of point in it. The window
16452 update may have changed the buffer, so first make sure `opoint'
16453 is still valid (Bug#6177). */
16454 if (CHARPOS (opoint) < BEGV)
16455 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16456 else if (CHARPOS (opoint) > ZV)
16457 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16458 else
16459 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16460
16461 set_buffer_internal_1 (old);
16462 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16463 shorter. This can be caused by log truncation in *Messages*. */
16464 if (CHARPOS (lpoint) <= ZV)
16465 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16466
16467 unbind_to (count, Qnil);
16468 }
16469
16470
16471 /* Build the complete desired matrix of WINDOW with a window start
16472 buffer position POS.
16473
16474 Value is 1 if successful. It is zero if fonts were loaded during
16475 redisplay which makes re-adjusting glyph matrices necessary, and -1
16476 if point would appear in the scroll margins.
16477 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16478 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16479 set in FLAGS.) */
16480
16481 int
16482 try_window (Lisp_Object window, struct text_pos pos, int flags)
16483 {
16484 struct window *w = XWINDOW (window);
16485 struct it it;
16486 struct glyph_row *last_text_row = NULL;
16487 struct frame *f = XFRAME (w->frame);
16488 int frame_line_height = default_line_pixel_height (w);
16489
16490 /* Make POS the new window start. */
16491 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16492
16493 /* Mark cursor position as unknown. No overlay arrow seen. */
16494 w->cursor.vpos = -1;
16495 overlay_arrow_seen = 0;
16496
16497 /* Initialize iterator and info to start at POS. */
16498 start_display (&it, w, pos);
16499
16500 /* Display all lines of W. */
16501 while (it.current_y < it.last_visible_y)
16502 {
16503 if (display_line (&it))
16504 last_text_row = it.glyph_row - 1;
16505 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16506 return 0;
16507 }
16508
16509 /* Don't let the cursor end in the scroll margins. */
16510 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16511 && !MINI_WINDOW_P (w))
16512 {
16513 int this_scroll_margin;
16514 int window_total_lines
16515 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16516
16517 if (scroll_margin > 0)
16518 {
16519 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16520 this_scroll_margin *= frame_line_height;
16521 }
16522 else
16523 this_scroll_margin = 0;
16524
16525 if ((w->cursor.y >= 0 /* not vscrolled */
16526 && w->cursor.y < this_scroll_margin
16527 && CHARPOS (pos) > BEGV
16528 && IT_CHARPOS (it) < ZV)
16529 /* rms: considering make_cursor_line_fully_visible_p here
16530 seems to give wrong results. We don't want to recenter
16531 when the last line is partly visible, we want to allow
16532 that case to be handled in the usual way. */
16533 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16534 {
16535 w->cursor.vpos = -1;
16536 clear_glyph_matrix (w->desired_matrix);
16537 return -1;
16538 }
16539 }
16540
16541 /* If bottom moved off end of frame, change mode line percentage. */
16542 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16543 w->update_mode_line = 1;
16544
16545 /* Set window_end_pos to the offset of the last character displayed
16546 on the window from the end of current_buffer. Set
16547 window_end_vpos to its row number. */
16548 if (last_text_row)
16549 {
16550 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16551 adjust_window_ends (w, last_text_row, 0);
16552 eassert
16553 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16554 w->window_end_vpos)));
16555 }
16556 else
16557 {
16558 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16559 w->window_end_pos = Z - ZV;
16560 w->window_end_vpos = 0;
16561 }
16562
16563 /* But that is not valid info until redisplay finishes. */
16564 w->window_end_valid = 0;
16565 return 1;
16566 }
16567
16568
16569 \f
16570 /************************************************************************
16571 Window redisplay reusing current matrix when buffer has not changed
16572 ************************************************************************/
16573
16574 /* Try redisplay of window W showing an unchanged buffer with a
16575 different window start than the last time it was displayed by
16576 reusing its current matrix. Value is non-zero if successful.
16577 W->start is the new window start. */
16578
16579 static int
16580 try_window_reusing_current_matrix (struct window *w)
16581 {
16582 struct frame *f = XFRAME (w->frame);
16583 struct glyph_row *bottom_row;
16584 struct it it;
16585 struct run run;
16586 struct text_pos start, new_start;
16587 int nrows_scrolled, i;
16588 struct glyph_row *last_text_row;
16589 struct glyph_row *last_reused_text_row;
16590 struct glyph_row *start_row;
16591 int start_vpos, min_y, max_y;
16592
16593 #ifdef GLYPH_DEBUG
16594 if (inhibit_try_window_reusing)
16595 return 0;
16596 #endif
16597
16598 if (/* This function doesn't handle terminal frames. */
16599 !FRAME_WINDOW_P (f)
16600 /* Don't try to reuse the display if windows have been split
16601 or such. */
16602 || windows_or_buffers_changed
16603 || f->cursor_type_changed)
16604 return 0;
16605
16606 /* Can't do this if showing trailing whitespace. */
16607 if (!NILP (Vshow_trailing_whitespace))
16608 return 0;
16609
16610 /* If top-line visibility has changed, give up. */
16611 if (WINDOW_WANTS_HEADER_LINE_P (w)
16612 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16613 return 0;
16614
16615 /* Give up if old or new display is scrolled vertically. We could
16616 make this function handle this, but right now it doesn't. */
16617 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16618 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16619 return 0;
16620
16621 /* The variable new_start now holds the new window start. The old
16622 start `start' can be determined from the current matrix. */
16623 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16624 start = start_row->minpos;
16625 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16626
16627 /* Clear the desired matrix for the display below. */
16628 clear_glyph_matrix (w->desired_matrix);
16629
16630 if (CHARPOS (new_start) <= CHARPOS (start))
16631 {
16632 /* Don't use this method if the display starts with an ellipsis
16633 displayed for invisible text. It's not easy to handle that case
16634 below, and it's certainly not worth the effort since this is
16635 not a frequent case. */
16636 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16637 return 0;
16638
16639 IF_DEBUG (debug_method_add (w, "twu1"));
16640
16641 /* Display up to a row that can be reused. The variable
16642 last_text_row is set to the last row displayed that displays
16643 text. Note that it.vpos == 0 if or if not there is a
16644 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16645 start_display (&it, w, new_start);
16646 w->cursor.vpos = -1;
16647 last_text_row = last_reused_text_row = NULL;
16648
16649 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16650 {
16651 /* If we have reached into the characters in the START row,
16652 that means the line boundaries have changed. So we
16653 can't start copying with the row START. Maybe it will
16654 work to start copying with the following row. */
16655 while (IT_CHARPOS (it) > CHARPOS (start))
16656 {
16657 /* Advance to the next row as the "start". */
16658 start_row++;
16659 start = start_row->minpos;
16660 /* If there are no more rows to try, or just one, give up. */
16661 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16662 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16663 || CHARPOS (start) == ZV)
16664 {
16665 clear_glyph_matrix (w->desired_matrix);
16666 return 0;
16667 }
16668
16669 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16670 }
16671 /* If we have reached alignment, we can copy the rest of the
16672 rows. */
16673 if (IT_CHARPOS (it) == CHARPOS (start)
16674 /* Don't accept "alignment" inside a display vector,
16675 since start_row could have started in the middle of
16676 that same display vector (thus their character
16677 positions match), and we have no way of telling if
16678 that is the case. */
16679 && it.current.dpvec_index < 0)
16680 break;
16681
16682 if (display_line (&it))
16683 last_text_row = it.glyph_row - 1;
16684
16685 }
16686
16687 /* A value of current_y < last_visible_y means that we stopped
16688 at the previous window start, which in turn means that we
16689 have at least one reusable row. */
16690 if (it.current_y < it.last_visible_y)
16691 {
16692 struct glyph_row *row;
16693
16694 /* IT.vpos always starts from 0; it counts text lines. */
16695 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16696
16697 /* Find PT if not already found in the lines displayed. */
16698 if (w->cursor.vpos < 0)
16699 {
16700 int dy = it.current_y - start_row->y;
16701
16702 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16703 row = row_containing_pos (w, PT, row, NULL, dy);
16704 if (row)
16705 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16706 dy, nrows_scrolled);
16707 else
16708 {
16709 clear_glyph_matrix (w->desired_matrix);
16710 return 0;
16711 }
16712 }
16713
16714 /* Scroll the display. Do it before the current matrix is
16715 changed. The problem here is that update has not yet
16716 run, i.e. part of the current matrix is not up to date.
16717 scroll_run_hook will clear the cursor, and use the
16718 current matrix to get the height of the row the cursor is
16719 in. */
16720 run.current_y = start_row->y;
16721 run.desired_y = it.current_y;
16722 run.height = it.last_visible_y - it.current_y;
16723
16724 if (run.height > 0 && run.current_y != run.desired_y)
16725 {
16726 update_begin (f);
16727 FRAME_RIF (f)->update_window_begin_hook (w);
16728 FRAME_RIF (f)->clear_window_mouse_face (w);
16729 FRAME_RIF (f)->scroll_run_hook (w, &run);
16730 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16731 update_end (f);
16732 }
16733
16734 /* Shift current matrix down by nrows_scrolled lines. */
16735 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16736 rotate_matrix (w->current_matrix,
16737 start_vpos,
16738 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16739 nrows_scrolled);
16740
16741 /* Disable lines that must be updated. */
16742 for (i = 0; i < nrows_scrolled; ++i)
16743 (start_row + i)->enabled_p = 0;
16744
16745 /* Re-compute Y positions. */
16746 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16747 max_y = it.last_visible_y;
16748 for (row = start_row + nrows_scrolled;
16749 row < bottom_row;
16750 ++row)
16751 {
16752 row->y = it.current_y;
16753 row->visible_height = row->height;
16754
16755 if (row->y < min_y)
16756 row->visible_height -= min_y - row->y;
16757 if (row->y + row->height > max_y)
16758 row->visible_height -= row->y + row->height - max_y;
16759 if (row->fringe_bitmap_periodic_p)
16760 row->redraw_fringe_bitmaps_p = 1;
16761
16762 it.current_y += row->height;
16763
16764 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16765 last_reused_text_row = row;
16766 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16767 break;
16768 }
16769
16770 /* Disable lines in the current matrix which are now
16771 below the window. */
16772 for (++row; row < bottom_row; ++row)
16773 row->enabled_p = row->mode_line_p = 0;
16774 }
16775
16776 /* Update window_end_pos etc.; last_reused_text_row is the last
16777 reused row from the current matrix containing text, if any.
16778 The value of last_text_row is the last displayed line
16779 containing text. */
16780 if (last_reused_text_row)
16781 adjust_window_ends (w, last_reused_text_row, 1);
16782 else if (last_text_row)
16783 adjust_window_ends (w, last_text_row, 0);
16784 else
16785 {
16786 /* This window must be completely empty. */
16787 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16788 w->window_end_pos = Z - ZV;
16789 w->window_end_vpos = 0;
16790 }
16791 w->window_end_valid = 0;
16792
16793 /* Update hint: don't try scrolling again in update_window. */
16794 w->desired_matrix->no_scrolling_p = 1;
16795
16796 #ifdef GLYPH_DEBUG
16797 debug_method_add (w, "try_window_reusing_current_matrix 1");
16798 #endif
16799 return 1;
16800 }
16801 else if (CHARPOS (new_start) > CHARPOS (start))
16802 {
16803 struct glyph_row *pt_row, *row;
16804 struct glyph_row *first_reusable_row;
16805 struct glyph_row *first_row_to_display;
16806 int dy;
16807 int yb = window_text_bottom_y (w);
16808
16809 /* Find the row starting at new_start, if there is one. Don't
16810 reuse a partially visible line at the end. */
16811 first_reusable_row = start_row;
16812 while (first_reusable_row->enabled_p
16813 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16814 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16815 < CHARPOS (new_start)))
16816 ++first_reusable_row;
16817
16818 /* Give up if there is no row to reuse. */
16819 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16820 || !first_reusable_row->enabled_p
16821 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16822 != CHARPOS (new_start)))
16823 return 0;
16824
16825 /* We can reuse fully visible rows beginning with
16826 first_reusable_row to the end of the window. Set
16827 first_row_to_display to the first row that cannot be reused.
16828 Set pt_row to the row containing point, if there is any. */
16829 pt_row = NULL;
16830 for (first_row_to_display = first_reusable_row;
16831 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16832 ++first_row_to_display)
16833 {
16834 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16835 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16836 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16837 && first_row_to_display->ends_at_zv_p
16838 && pt_row == NULL)))
16839 pt_row = first_row_to_display;
16840 }
16841
16842 /* Start displaying at the start of first_row_to_display. */
16843 eassert (first_row_to_display->y < yb);
16844 init_to_row_start (&it, w, first_row_to_display);
16845
16846 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16847 - start_vpos);
16848 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16849 - nrows_scrolled);
16850 it.current_y = (first_row_to_display->y - first_reusable_row->y
16851 + WINDOW_HEADER_LINE_HEIGHT (w));
16852
16853 /* Display lines beginning with first_row_to_display in the
16854 desired matrix. Set last_text_row to the last row displayed
16855 that displays text. */
16856 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16857 if (pt_row == NULL)
16858 w->cursor.vpos = -1;
16859 last_text_row = NULL;
16860 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16861 if (display_line (&it))
16862 last_text_row = it.glyph_row - 1;
16863
16864 /* If point is in a reused row, adjust y and vpos of the cursor
16865 position. */
16866 if (pt_row)
16867 {
16868 w->cursor.vpos -= nrows_scrolled;
16869 w->cursor.y -= first_reusable_row->y - start_row->y;
16870 }
16871
16872 /* Give up if point isn't in a row displayed or reused. (This
16873 also handles the case where w->cursor.vpos < nrows_scrolled
16874 after the calls to display_line, which can happen with scroll
16875 margins. See bug#1295.) */
16876 if (w->cursor.vpos < 0)
16877 {
16878 clear_glyph_matrix (w->desired_matrix);
16879 return 0;
16880 }
16881
16882 /* Scroll the display. */
16883 run.current_y = first_reusable_row->y;
16884 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16885 run.height = it.last_visible_y - run.current_y;
16886 dy = run.current_y - run.desired_y;
16887
16888 if (run.height)
16889 {
16890 update_begin (f);
16891 FRAME_RIF (f)->update_window_begin_hook (w);
16892 FRAME_RIF (f)->clear_window_mouse_face (w);
16893 FRAME_RIF (f)->scroll_run_hook (w, &run);
16894 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16895 update_end (f);
16896 }
16897
16898 /* Adjust Y positions of reused rows. */
16899 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16900 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16901 max_y = it.last_visible_y;
16902 for (row = first_reusable_row; row < first_row_to_display; ++row)
16903 {
16904 row->y -= dy;
16905 row->visible_height = row->height;
16906 if (row->y < min_y)
16907 row->visible_height -= min_y - row->y;
16908 if (row->y + row->height > max_y)
16909 row->visible_height -= row->y + row->height - max_y;
16910 if (row->fringe_bitmap_periodic_p)
16911 row->redraw_fringe_bitmaps_p = 1;
16912 }
16913
16914 /* Scroll the current matrix. */
16915 eassert (nrows_scrolled > 0);
16916 rotate_matrix (w->current_matrix,
16917 start_vpos,
16918 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16919 -nrows_scrolled);
16920
16921 /* Disable rows not reused. */
16922 for (row -= nrows_scrolled; row < bottom_row; ++row)
16923 row->enabled_p = 0;
16924
16925 /* Point may have moved to a different line, so we cannot assume that
16926 the previous cursor position is valid; locate the correct row. */
16927 if (pt_row)
16928 {
16929 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16930 row < bottom_row
16931 && PT >= MATRIX_ROW_END_CHARPOS (row)
16932 && !row->ends_at_zv_p;
16933 row++)
16934 {
16935 w->cursor.vpos++;
16936 w->cursor.y = row->y;
16937 }
16938 if (row < bottom_row)
16939 {
16940 /* Can't simply scan the row for point with
16941 bidi-reordered glyph rows. Let set_cursor_from_row
16942 figure out where to put the cursor, and if it fails,
16943 give up. */
16944 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16945 {
16946 if (!set_cursor_from_row (w, row, w->current_matrix,
16947 0, 0, 0, 0))
16948 {
16949 clear_glyph_matrix (w->desired_matrix);
16950 return 0;
16951 }
16952 }
16953 else
16954 {
16955 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16956 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16957
16958 for (; glyph < end
16959 && (!BUFFERP (glyph->object)
16960 || glyph->charpos < PT);
16961 glyph++)
16962 {
16963 w->cursor.hpos++;
16964 w->cursor.x += glyph->pixel_width;
16965 }
16966 }
16967 }
16968 }
16969
16970 /* Adjust window end. A null value of last_text_row means that
16971 the window end is in reused rows which in turn means that
16972 only its vpos can have changed. */
16973 if (last_text_row)
16974 adjust_window_ends (w, last_text_row, 0);
16975 else
16976 w->window_end_vpos -= nrows_scrolled;
16977
16978 w->window_end_valid = 0;
16979 w->desired_matrix->no_scrolling_p = 1;
16980
16981 #ifdef GLYPH_DEBUG
16982 debug_method_add (w, "try_window_reusing_current_matrix 2");
16983 #endif
16984 return 1;
16985 }
16986
16987 return 0;
16988 }
16989
16990
16991 \f
16992 /************************************************************************
16993 Window redisplay reusing current matrix when buffer has changed
16994 ************************************************************************/
16995
16996 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16997 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16998 ptrdiff_t *, ptrdiff_t *);
16999 static struct glyph_row *
17000 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17001 struct glyph_row *);
17002
17003
17004 /* Return the last row in MATRIX displaying text. If row START is
17005 non-null, start searching with that row. IT gives the dimensions
17006 of the display. Value is null if matrix is empty; otherwise it is
17007 a pointer to the row found. */
17008
17009 static struct glyph_row *
17010 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17011 struct glyph_row *start)
17012 {
17013 struct glyph_row *row, *row_found;
17014
17015 /* Set row_found to the last row in IT->w's current matrix
17016 displaying text. The loop looks funny but think of partially
17017 visible lines. */
17018 row_found = NULL;
17019 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17020 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17021 {
17022 eassert (row->enabled_p);
17023 row_found = row;
17024 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17025 break;
17026 ++row;
17027 }
17028
17029 return row_found;
17030 }
17031
17032
17033 /* Return the last row in the current matrix of W that is not affected
17034 by changes at the start of current_buffer that occurred since W's
17035 current matrix was built. Value is null if no such row exists.
17036
17037 BEG_UNCHANGED us the number of characters unchanged at the start of
17038 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17039 first changed character in current_buffer. Characters at positions <
17040 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17041 when the current matrix was built. */
17042
17043 static struct glyph_row *
17044 find_last_unchanged_at_beg_row (struct window *w)
17045 {
17046 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17047 struct glyph_row *row;
17048 struct glyph_row *row_found = NULL;
17049 int yb = window_text_bottom_y (w);
17050
17051 /* Find the last row displaying unchanged text. */
17052 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17053 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17054 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17055 ++row)
17056 {
17057 if (/* If row ends before first_changed_pos, it is unchanged,
17058 except in some case. */
17059 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17060 /* When row ends in ZV and we write at ZV it is not
17061 unchanged. */
17062 && !row->ends_at_zv_p
17063 /* When first_changed_pos is the end of a continued line,
17064 row is not unchanged because it may be no longer
17065 continued. */
17066 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17067 && (row->continued_p
17068 || row->exact_window_width_line_p))
17069 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17070 needs to be recomputed, so don't consider this row as
17071 unchanged. This happens when the last line was
17072 bidi-reordered and was killed immediately before this
17073 redisplay cycle. In that case, ROW->end stores the
17074 buffer position of the first visual-order character of
17075 the killed text, which is now beyond ZV. */
17076 && CHARPOS (row->end.pos) <= ZV)
17077 row_found = row;
17078
17079 /* Stop if last visible row. */
17080 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17081 break;
17082 }
17083
17084 return row_found;
17085 }
17086
17087
17088 /* Find the first glyph row in the current matrix of W that is not
17089 affected by changes at the end of current_buffer since the
17090 time W's current matrix was built.
17091
17092 Return in *DELTA the number of chars by which buffer positions in
17093 unchanged text at the end of current_buffer must be adjusted.
17094
17095 Return in *DELTA_BYTES the corresponding number of bytes.
17096
17097 Value is null if no such row exists, i.e. all rows are affected by
17098 changes. */
17099
17100 static struct glyph_row *
17101 find_first_unchanged_at_end_row (struct window *w,
17102 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17103 {
17104 struct glyph_row *row;
17105 struct glyph_row *row_found = NULL;
17106
17107 *delta = *delta_bytes = 0;
17108
17109 /* Display must not have been paused, otherwise the current matrix
17110 is not up to date. */
17111 eassert (w->window_end_valid);
17112
17113 /* A value of window_end_pos >= END_UNCHANGED means that the window
17114 end is in the range of changed text. If so, there is no
17115 unchanged row at the end of W's current matrix. */
17116 if (w->window_end_pos >= END_UNCHANGED)
17117 return NULL;
17118
17119 /* Set row to the last row in W's current matrix displaying text. */
17120 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17121
17122 /* If matrix is entirely empty, no unchanged row exists. */
17123 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17124 {
17125 /* The value of row is the last glyph row in the matrix having a
17126 meaningful buffer position in it. The end position of row
17127 corresponds to window_end_pos. This allows us to translate
17128 buffer positions in the current matrix to current buffer
17129 positions for characters not in changed text. */
17130 ptrdiff_t Z_old =
17131 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17132 ptrdiff_t Z_BYTE_old =
17133 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17134 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17135 struct glyph_row *first_text_row
17136 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17137
17138 *delta = Z - Z_old;
17139 *delta_bytes = Z_BYTE - Z_BYTE_old;
17140
17141 /* Set last_unchanged_pos to the buffer position of the last
17142 character in the buffer that has not been changed. Z is the
17143 index + 1 of the last character in current_buffer, i.e. by
17144 subtracting END_UNCHANGED we get the index of the last
17145 unchanged character, and we have to add BEG to get its buffer
17146 position. */
17147 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17148 last_unchanged_pos_old = last_unchanged_pos - *delta;
17149
17150 /* Search backward from ROW for a row displaying a line that
17151 starts at a minimum position >= last_unchanged_pos_old. */
17152 for (; row > first_text_row; --row)
17153 {
17154 /* This used to abort, but it can happen.
17155 It is ok to just stop the search instead here. KFS. */
17156 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17157 break;
17158
17159 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17160 row_found = row;
17161 }
17162 }
17163
17164 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17165
17166 return row_found;
17167 }
17168
17169
17170 /* Make sure that glyph rows in the current matrix of window W
17171 reference the same glyph memory as corresponding rows in the
17172 frame's frame matrix. This function is called after scrolling W's
17173 current matrix on a terminal frame in try_window_id and
17174 try_window_reusing_current_matrix. */
17175
17176 static void
17177 sync_frame_with_window_matrix_rows (struct window *w)
17178 {
17179 struct frame *f = XFRAME (w->frame);
17180 struct glyph_row *window_row, *window_row_end, *frame_row;
17181
17182 /* Preconditions: W must be a leaf window and full-width. Its frame
17183 must have a frame matrix. */
17184 eassert (BUFFERP (w->contents));
17185 eassert (WINDOW_FULL_WIDTH_P (w));
17186 eassert (!FRAME_WINDOW_P (f));
17187
17188 /* If W is a full-width window, glyph pointers in W's current matrix
17189 have, by definition, to be the same as glyph pointers in the
17190 corresponding frame matrix. Note that frame matrices have no
17191 marginal areas (see build_frame_matrix). */
17192 window_row = w->current_matrix->rows;
17193 window_row_end = window_row + w->current_matrix->nrows;
17194 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17195 while (window_row < window_row_end)
17196 {
17197 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17198 struct glyph *end = window_row->glyphs[LAST_AREA];
17199
17200 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17201 frame_row->glyphs[TEXT_AREA] = start;
17202 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17203 frame_row->glyphs[LAST_AREA] = end;
17204
17205 /* Disable frame rows whose corresponding window rows have
17206 been disabled in try_window_id. */
17207 if (!window_row->enabled_p)
17208 frame_row->enabled_p = 0;
17209
17210 ++window_row, ++frame_row;
17211 }
17212 }
17213
17214
17215 /* Find the glyph row in window W containing CHARPOS. Consider all
17216 rows between START and END (not inclusive). END null means search
17217 all rows to the end of the display area of W. Value is the row
17218 containing CHARPOS or null. */
17219
17220 struct glyph_row *
17221 row_containing_pos (struct window *w, ptrdiff_t charpos,
17222 struct glyph_row *start, struct glyph_row *end, int dy)
17223 {
17224 struct glyph_row *row = start;
17225 struct glyph_row *best_row = NULL;
17226 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17227 int last_y;
17228
17229 /* If we happen to start on a header-line, skip that. */
17230 if (row->mode_line_p)
17231 ++row;
17232
17233 if ((end && row >= end) || !row->enabled_p)
17234 return NULL;
17235
17236 last_y = window_text_bottom_y (w) - dy;
17237
17238 while (1)
17239 {
17240 /* Give up if we have gone too far. */
17241 if (end && row >= end)
17242 return NULL;
17243 /* This formerly returned if they were equal.
17244 I think that both quantities are of a "last plus one" type;
17245 if so, when they are equal, the row is within the screen. -- rms. */
17246 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17247 return NULL;
17248
17249 /* If it is in this row, return this row. */
17250 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17251 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17252 /* The end position of a row equals the start
17253 position of the next row. If CHARPOS is there, we
17254 would rather consider it displayed in the next
17255 line, except when this line ends in ZV. */
17256 && !row_for_charpos_p (row, charpos)))
17257 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17258 {
17259 struct glyph *g;
17260
17261 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17262 || (!best_row && !row->continued_p))
17263 return row;
17264 /* In bidi-reordered rows, there could be several rows whose
17265 edges surround CHARPOS, all of these rows belonging to
17266 the same continued line. We need to find the row which
17267 fits CHARPOS the best. */
17268 for (g = row->glyphs[TEXT_AREA];
17269 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17270 g++)
17271 {
17272 if (!STRINGP (g->object))
17273 {
17274 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17275 {
17276 mindif = eabs (g->charpos - charpos);
17277 best_row = row;
17278 /* Exact match always wins. */
17279 if (mindif == 0)
17280 return best_row;
17281 }
17282 }
17283 }
17284 }
17285 else if (best_row && !row->continued_p)
17286 return best_row;
17287 ++row;
17288 }
17289 }
17290
17291
17292 /* Try to redisplay window W by reusing its existing display. W's
17293 current matrix must be up to date when this function is called,
17294 i.e. window_end_valid must be nonzero.
17295
17296 Value is
17297
17298 1 if display has been updated
17299 0 if otherwise unsuccessful
17300 -1 if redisplay with same window start is known not to succeed
17301
17302 The following steps are performed:
17303
17304 1. Find the last row in the current matrix of W that is not
17305 affected by changes at the start of current_buffer. If no such row
17306 is found, give up.
17307
17308 2. Find the first row in W's current matrix that is not affected by
17309 changes at the end of current_buffer. Maybe there is no such row.
17310
17311 3. Display lines beginning with the row + 1 found in step 1 to the
17312 row found in step 2 or, if step 2 didn't find a row, to the end of
17313 the window.
17314
17315 4. If cursor is not known to appear on the window, give up.
17316
17317 5. If display stopped at the row found in step 2, scroll the
17318 display and current matrix as needed.
17319
17320 6. Maybe display some lines at the end of W, if we must. This can
17321 happen under various circumstances, like a partially visible line
17322 becoming fully visible, or because newly displayed lines are displayed
17323 in smaller font sizes.
17324
17325 7. Update W's window end information. */
17326
17327 static int
17328 try_window_id (struct window *w)
17329 {
17330 struct frame *f = XFRAME (w->frame);
17331 struct glyph_matrix *current_matrix = w->current_matrix;
17332 struct glyph_matrix *desired_matrix = w->desired_matrix;
17333 struct glyph_row *last_unchanged_at_beg_row;
17334 struct glyph_row *first_unchanged_at_end_row;
17335 struct glyph_row *row;
17336 struct glyph_row *bottom_row;
17337 int bottom_vpos;
17338 struct it it;
17339 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17340 int dvpos, dy;
17341 struct text_pos start_pos;
17342 struct run run;
17343 int first_unchanged_at_end_vpos = 0;
17344 struct glyph_row *last_text_row, *last_text_row_at_end;
17345 struct text_pos start;
17346 ptrdiff_t first_changed_charpos, last_changed_charpos;
17347
17348 #ifdef GLYPH_DEBUG
17349 if (inhibit_try_window_id)
17350 return 0;
17351 #endif
17352
17353 /* This is handy for debugging. */
17354 #if 0
17355 #define GIVE_UP(X) \
17356 do { \
17357 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17358 return 0; \
17359 } while (0)
17360 #else
17361 #define GIVE_UP(X) return 0
17362 #endif
17363
17364 SET_TEXT_POS_FROM_MARKER (start, w->start);
17365
17366 /* Don't use this for mini-windows because these can show
17367 messages and mini-buffers, and we don't handle that here. */
17368 if (MINI_WINDOW_P (w))
17369 GIVE_UP (1);
17370
17371 /* This flag is used to prevent redisplay optimizations. */
17372 if (windows_or_buffers_changed || f->cursor_type_changed)
17373 GIVE_UP (2);
17374
17375 /* Verify that narrowing has not changed.
17376 Also verify that we were not told to prevent redisplay optimizations.
17377 It would be nice to further
17378 reduce the number of cases where this prevents try_window_id. */
17379 if (current_buffer->clip_changed
17380 || current_buffer->prevent_redisplay_optimizations_p)
17381 GIVE_UP (3);
17382
17383 /* Window must either use window-based redisplay or be full width. */
17384 if (!FRAME_WINDOW_P (f)
17385 && (!FRAME_LINE_INS_DEL_OK (f)
17386 || !WINDOW_FULL_WIDTH_P (w)))
17387 GIVE_UP (4);
17388
17389 /* Give up if point is known NOT to appear in W. */
17390 if (PT < CHARPOS (start))
17391 GIVE_UP (5);
17392
17393 /* Another way to prevent redisplay optimizations. */
17394 if (w->last_modified == 0)
17395 GIVE_UP (6);
17396
17397 /* Verify that window is not hscrolled. */
17398 if (w->hscroll != 0)
17399 GIVE_UP (7);
17400
17401 /* Verify that display wasn't paused. */
17402 if (!w->window_end_valid)
17403 GIVE_UP (8);
17404
17405 /* Likewise if highlighting trailing whitespace. */
17406 if (!NILP (Vshow_trailing_whitespace))
17407 GIVE_UP (11);
17408
17409 /* Can't use this if overlay arrow position and/or string have
17410 changed. */
17411 if (overlay_arrows_changed_p ())
17412 GIVE_UP (12);
17413
17414 /* When word-wrap is on, adding a space to the first word of a
17415 wrapped line can change the wrap position, altering the line
17416 above it. It might be worthwhile to handle this more
17417 intelligently, but for now just redisplay from scratch. */
17418 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17419 GIVE_UP (21);
17420
17421 /* Under bidi reordering, adding or deleting a character in the
17422 beginning of a paragraph, before the first strong directional
17423 character, can change the base direction of the paragraph (unless
17424 the buffer specifies a fixed paragraph direction), which will
17425 require to redisplay the whole paragraph. It might be worthwhile
17426 to find the paragraph limits and widen the range of redisplayed
17427 lines to that, but for now just give up this optimization and
17428 redisplay from scratch. */
17429 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17430 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17431 GIVE_UP (22);
17432
17433 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17434 only if buffer has really changed. The reason is that the gap is
17435 initially at Z for freshly visited files. The code below would
17436 set end_unchanged to 0 in that case. */
17437 if (MODIFF > SAVE_MODIFF
17438 /* This seems to happen sometimes after saving a buffer. */
17439 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17440 {
17441 if (GPT - BEG < BEG_UNCHANGED)
17442 BEG_UNCHANGED = GPT - BEG;
17443 if (Z - GPT < END_UNCHANGED)
17444 END_UNCHANGED = Z - GPT;
17445 }
17446
17447 /* The position of the first and last character that has been changed. */
17448 first_changed_charpos = BEG + BEG_UNCHANGED;
17449 last_changed_charpos = Z - END_UNCHANGED;
17450
17451 /* If window starts after a line end, and the last change is in
17452 front of that newline, then changes don't affect the display.
17453 This case happens with stealth-fontification. Note that although
17454 the display is unchanged, glyph positions in the matrix have to
17455 be adjusted, of course. */
17456 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17457 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17458 && ((last_changed_charpos < CHARPOS (start)
17459 && CHARPOS (start) == BEGV)
17460 || (last_changed_charpos < CHARPOS (start) - 1
17461 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17462 {
17463 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17464 struct glyph_row *r0;
17465
17466 /* Compute how many chars/bytes have been added to or removed
17467 from the buffer. */
17468 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17469 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17470 Z_delta = Z - Z_old;
17471 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17472
17473 /* Give up if PT is not in the window. Note that it already has
17474 been checked at the start of try_window_id that PT is not in
17475 front of the window start. */
17476 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17477 GIVE_UP (13);
17478
17479 /* If window start is unchanged, we can reuse the whole matrix
17480 as is, after adjusting glyph positions. No need to compute
17481 the window end again, since its offset from Z hasn't changed. */
17482 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17483 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17484 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17485 /* PT must not be in a partially visible line. */
17486 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17487 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17488 {
17489 /* Adjust positions in the glyph matrix. */
17490 if (Z_delta || Z_delta_bytes)
17491 {
17492 struct glyph_row *r1
17493 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17494 increment_matrix_positions (w->current_matrix,
17495 MATRIX_ROW_VPOS (r0, current_matrix),
17496 MATRIX_ROW_VPOS (r1, current_matrix),
17497 Z_delta, Z_delta_bytes);
17498 }
17499
17500 /* Set the cursor. */
17501 row = row_containing_pos (w, PT, r0, NULL, 0);
17502 if (row)
17503 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17504 return 1;
17505 }
17506 }
17507
17508 /* Handle the case that changes are all below what is displayed in
17509 the window, and that PT is in the window. This shortcut cannot
17510 be taken if ZV is visible in the window, and text has been added
17511 there that is visible in the window. */
17512 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17513 /* ZV is not visible in the window, or there are no
17514 changes at ZV, actually. */
17515 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17516 || first_changed_charpos == last_changed_charpos))
17517 {
17518 struct glyph_row *r0;
17519
17520 /* Give up if PT is not in the window. Note that it already has
17521 been checked at the start of try_window_id that PT is not in
17522 front of the window start. */
17523 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17524 GIVE_UP (14);
17525
17526 /* If window start is unchanged, we can reuse the whole matrix
17527 as is, without changing glyph positions since no text has
17528 been added/removed in front of the window end. */
17529 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17530 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17531 /* PT must not be in a partially visible line. */
17532 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17533 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17534 {
17535 /* We have to compute the window end anew since text
17536 could have been added/removed after it. */
17537 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17538 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17539
17540 /* Set the cursor. */
17541 row = row_containing_pos (w, PT, r0, NULL, 0);
17542 if (row)
17543 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17544 return 2;
17545 }
17546 }
17547
17548 /* Give up if window start is in the changed area.
17549
17550 The condition used to read
17551
17552 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17553
17554 but why that was tested escapes me at the moment. */
17555 if (CHARPOS (start) >= first_changed_charpos
17556 && CHARPOS (start) <= last_changed_charpos)
17557 GIVE_UP (15);
17558
17559 /* Check that window start agrees with the start of the first glyph
17560 row in its current matrix. Check this after we know the window
17561 start is not in changed text, otherwise positions would not be
17562 comparable. */
17563 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17564 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17565 GIVE_UP (16);
17566
17567 /* Give up if the window ends in strings. Overlay strings
17568 at the end are difficult to handle, so don't try. */
17569 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17570 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17571 GIVE_UP (20);
17572
17573 /* Compute the position at which we have to start displaying new
17574 lines. Some of the lines at the top of the window might be
17575 reusable because they are not displaying changed text. Find the
17576 last row in W's current matrix not affected by changes at the
17577 start of current_buffer. Value is null if changes start in the
17578 first line of window. */
17579 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17580 if (last_unchanged_at_beg_row)
17581 {
17582 /* Avoid starting to display in the middle of a character, a TAB
17583 for instance. This is easier than to set up the iterator
17584 exactly, and it's not a frequent case, so the additional
17585 effort wouldn't really pay off. */
17586 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17587 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17588 && last_unchanged_at_beg_row > w->current_matrix->rows)
17589 --last_unchanged_at_beg_row;
17590
17591 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17592 GIVE_UP (17);
17593
17594 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17595 GIVE_UP (18);
17596 start_pos = it.current.pos;
17597
17598 /* Start displaying new lines in the desired matrix at the same
17599 vpos we would use in the current matrix, i.e. below
17600 last_unchanged_at_beg_row. */
17601 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17602 current_matrix);
17603 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17604 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17605
17606 eassert (it.hpos == 0 && it.current_x == 0);
17607 }
17608 else
17609 {
17610 /* There are no reusable lines at the start of the window.
17611 Start displaying in the first text line. */
17612 start_display (&it, w, start);
17613 it.vpos = it.first_vpos;
17614 start_pos = it.current.pos;
17615 }
17616
17617 /* Find the first row that is not affected by changes at the end of
17618 the buffer. Value will be null if there is no unchanged row, in
17619 which case we must redisplay to the end of the window. delta
17620 will be set to the value by which buffer positions beginning with
17621 first_unchanged_at_end_row have to be adjusted due to text
17622 changes. */
17623 first_unchanged_at_end_row
17624 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17625 IF_DEBUG (debug_delta = delta);
17626 IF_DEBUG (debug_delta_bytes = delta_bytes);
17627
17628 /* Set stop_pos to the buffer position up to which we will have to
17629 display new lines. If first_unchanged_at_end_row != NULL, this
17630 is the buffer position of the start of the line displayed in that
17631 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17632 that we don't stop at a buffer position. */
17633 stop_pos = 0;
17634 if (first_unchanged_at_end_row)
17635 {
17636 eassert (last_unchanged_at_beg_row == NULL
17637 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17638
17639 /* If this is a continuation line, move forward to the next one
17640 that isn't. Changes in lines above affect this line.
17641 Caution: this may move first_unchanged_at_end_row to a row
17642 not displaying text. */
17643 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17644 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17645 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17646 < it.last_visible_y))
17647 ++first_unchanged_at_end_row;
17648
17649 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17650 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17651 >= it.last_visible_y))
17652 first_unchanged_at_end_row = NULL;
17653 else
17654 {
17655 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17656 + delta);
17657 first_unchanged_at_end_vpos
17658 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17659 eassert (stop_pos >= Z - END_UNCHANGED);
17660 }
17661 }
17662 else if (last_unchanged_at_beg_row == NULL)
17663 GIVE_UP (19);
17664
17665
17666 #ifdef GLYPH_DEBUG
17667
17668 /* Either there is no unchanged row at the end, or the one we have
17669 now displays text. This is a necessary condition for the window
17670 end pos calculation at the end of this function. */
17671 eassert (first_unchanged_at_end_row == NULL
17672 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17673
17674 debug_last_unchanged_at_beg_vpos
17675 = (last_unchanged_at_beg_row
17676 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17677 : -1);
17678 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17679
17680 #endif /* GLYPH_DEBUG */
17681
17682
17683 /* Display new lines. Set last_text_row to the last new line
17684 displayed which has text on it, i.e. might end up as being the
17685 line where the window_end_vpos is. */
17686 w->cursor.vpos = -1;
17687 last_text_row = NULL;
17688 overlay_arrow_seen = 0;
17689 while (it.current_y < it.last_visible_y
17690 && !f->fonts_changed
17691 && (first_unchanged_at_end_row == NULL
17692 || IT_CHARPOS (it) < stop_pos))
17693 {
17694 if (display_line (&it))
17695 last_text_row = it.glyph_row - 1;
17696 }
17697
17698 if (f->fonts_changed)
17699 return -1;
17700
17701
17702 /* Compute differences in buffer positions, y-positions etc. for
17703 lines reused at the bottom of the window. Compute what we can
17704 scroll. */
17705 if (first_unchanged_at_end_row
17706 /* No lines reused because we displayed everything up to the
17707 bottom of the window. */
17708 && it.current_y < it.last_visible_y)
17709 {
17710 dvpos = (it.vpos
17711 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17712 current_matrix));
17713 dy = it.current_y - first_unchanged_at_end_row->y;
17714 run.current_y = first_unchanged_at_end_row->y;
17715 run.desired_y = run.current_y + dy;
17716 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17717 }
17718 else
17719 {
17720 delta = delta_bytes = dvpos = dy
17721 = run.current_y = run.desired_y = run.height = 0;
17722 first_unchanged_at_end_row = NULL;
17723 }
17724 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17725
17726
17727 /* Find the cursor if not already found. We have to decide whether
17728 PT will appear on this window (it sometimes doesn't, but this is
17729 not a very frequent case.) This decision has to be made before
17730 the current matrix is altered. A value of cursor.vpos < 0 means
17731 that PT is either in one of the lines beginning at
17732 first_unchanged_at_end_row or below the window. Don't care for
17733 lines that might be displayed later at the window end; as
17734 mentioned, this is not a frequent case. */
17735 if (w->cursor.vpos < 0)
17736 {
17737 /* Cursor in unchanged rows at the top? */
17738 if (PT < CHARPOS (start_pos)
17739 && last_unchanged_at_beg_row)
17740 {
17741 row = row_containing_pos (w, PT,
17742 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17743 last_unchanged_at_beg_row + 1, 0);
17744 if (row)
17745 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17746 }
17747
17748 /* Start from first_unchanged_at_end_row looking for PT. */
17749 else if (first_unchanged_at_end_row)
17750 {
17751 row = row_containing_pos (w, PT - delta,
17752 first_unchanged_at_end_row, NULL, 0);
17753 if (row)
17754 set_cursor_from_row (w, row, w->current_matrix, delta,
17755 delta_bytes, dy, dvpos);
17756 }
17757
17758 /* Give up if cursor was not found. */
17759 if (w->cursor.vpos < 0)
17760 {
17761 clear_glyph_matrix (w->desired_matrix);
17762 return -1;
17763 }
17764 }
17765
17766 /* Don't let the cursor end in the scroll margins. */
17767 {
17768 int this_scroll_margin, cursor_height;
17769 int frame_line_height = default_line_pixel_height (w);
17770 int window_total_lines
17771 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17772
17773 this_scroll_margin =
17774 max (0, min (scroll_margin, window_total_lines / 4));
17775 this_scroll_margin *= frame_line_height;
17776 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17777
17778 if ((w->cursor.y < this_scroll_margin
17779 && CHARPOS (start) > BEGV)
17780 /* Old redisplay didn't take scroll margin into account at the bottom,
17781 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17782 || (w->cursor.y + (make_cursor_line_fully_visible_p
17783 ? cursor_height + this_scroll_margin
17784 : 1)) > it.last_visible_y)
17785 {
17786 w->cursor.vpos = -1;
17787 clear_glyph_matrix (w->desired_matrix);
17788 return -1;
17789 }
17790 }
17791
17792 /* Scroll the display. Do it before changing the current matrix so
17793 that xterm.c doesn't get confused about where the cursor glyph is
17794 found. */
17795 if (dy && run.height)
17796 {
17797 update_begin (f);
17798
17799 if (FRAME_WINDOW_P (f))
17800 {
17801 FRAME_RIF (f)->update_window_begin_hook (w);
17802 FRAME_RIF (f)->clear_window_mouse_face (w);
17803 FRAME_RIF (f)->scroll_run_hook (w, &run);
17804 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17805 }
17806 else
17807 {
17808 /* Terminal frame. In this case, dvpos gives the number of
17809 lines to scroll by; dvpos < 0 means scroll up. */
17810 int from_vpos
17811 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17812 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17813 int end = (WINDOW_TOP_EDGE_LINE (w)
17814 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17815 + window_internal_height (w));
17816
17817 #if defined (HAVE_GPM) || defined (MSDOS)
17818 x_clear_window_mouse_face (w);
17819 #endif
17820 /* Perform the operation on the screen. */
17821 if (dvpos > 0)
17822 {
17823 /* Scroll last_unchanged_at_beg_row to the end of the
17824 window down dvpos lines. */
17825 set_terminal_window (f, end);
17826
17827 /* On dumb terminals delete dvpos lines at the end
17828 before inserting dvpos empty lines. */
17829 if (!FRAME_SCROLL_REGION_OK (f))
17830 ins_del_lines (f, end - dvpos, -dvpos);
17831
17832 /* Insert dvpos empty lines in front of
17833 last_unchanged_at_beg_row. */
17834 ins_del_lines (f, from, dvpos);
17835 }
17836 else if (dvpos < 0)
17837 {
17838 /* Scroll up last_unchanged_at_beg_vpos to the end of
17839 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17840 set_terminal_window (f, end);
17841
17842 /* Delete dvpos lines in front of
17843 last_unchanged_at_beg_vpos. ins_del_lines will set
17844 the cursor to the given vpos and emit |dvpos| delete
17845 line sequences. */
17846 ins_del_lines (f, from + dvpos, dvpos);
17847
17848 /* On a dumb terminal insert dvpos empty lines at the
17849 end. */
17850 if (!FRAME_SCROLL_REGION_OK (f))
17851 ins_del_lines (f, end + dvpos, -dvpos);
17852 }
17853
17854 set_terminal_window (f, 0);
17855 }
17856
17857 update_end (f);
17858 }
17859
17860 /* Shift reused rows of the current matrix to the right position.
17861 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17862 text. */
17863 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17864 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17865 if (dvpos < 0)
17866 {
17867 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17868 bottom_vpos, dvpos);
17869 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17870 bottom_vpos);
17871 }
17872 else if (dvpos > 0)
17873 {
17874 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17875 bottom_vpos, dvpos);
17876 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17877 first_unchanged_at_end_vpos + dvpos);
17878 }
17879
17880 /* For frame-based redisplay, make sure that current frame and window
17881 matrix are in sync with respect to glyph memory. */
17882 if (!FRAME_WINDOW_P (f))
17883 sync_frame_with_window_matrix_rows (w);
17884
17885 /* Adjust buffer positions in reused rows. */
17886 if (delta || delta_bytes)
17887 increment_matrix_positions (current_matrix,
17888 first_unchanged_at_end_vpos + dvpos,
17889 bottom_vpos, delta, delta_bytes);
17890
17891 /* Adjust Y positions. */
17892 if (dy)
17893 shift_glyph_matrix (w, current_matrix,
17894 first_unchanged_at_end_vpos + dvpos,
17895 bottom_vpos, dy);
17896
17897 if (first_unchanged_at_end_row)
17898 {
17899 first_unchanged_at_end_row += dvpos;
17900 if (first_unchanged_at_end_row->y >= it.last_visible_y
17901 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17902 first_unchanged_at_end_row = NULL;
17903 }
17904
17905 /* If scrolling up, there may be some lines to display at the end of
17906 the window. */
17907 last_text_row_at_end = NULL;
17908 if (dy < 0)
17909 {
17910 /* Scrolling up can leave for example a partially visible line
17911 at the end of the window to be redisplayed. */
17912 /* Set last_row to the glyph row in the current matrix where the
17913 window end line is found. It has been moved up or down in
17914 the matrix by dvpos. */
17915 int last_vpos = w->window_end_vpos + dvpos;
17916 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17917
17918 /* If last_row is the window end line, it should display text. */
17919 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17920
17921 /* If window end line was partially visible before, begin
17922 displaying at that line. Otherwise begin displaying with the
17923 line following it. */
17924 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17925 {
17926 init_to_row_start (&it, w, last_row);
17927 it.vpos = last_vpos;
17928 it.current_y = last_row->y;
17929 }
17930 else
17931 {
17932 init_to_row_end (&it, w, last_row);
17933 it.vpos = 1 + last_vpos;
17934 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17935 ++last_row;
17936 }
17937
17938 /* We may start in a continuation line. If so, we have to
17939 get the right continuation_lines_width and current_x. */
17940 it.continuation_lines_width = last_row->continuation_lines_width;
17941 it.hpos = it.current_x = 0;
17942
17943 /* Display the rest of the lines at the window end. */
17944 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17945 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17946 {
17947 /* Is it always sure that the display agrees with lines in
17948 the current matrix? I don't think so, so we mark rows
17949 displayed invalid in the current matrix by setting their
17950 enabled_p flag to zero. */
17951 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17952 if (display_line (&it))
17953 last_text_row_at_end = it.glyph_row - 1;
17954 }
17955 }
17956
17957 /* Update window_end_pos and window_end_vpos. */
17958 if (first_unchanged_at_end_row && !last_text_row_at_end)
17959 {
17960 /* Window end line if one of the preserved rows from the current
17961 matrix. Set row to the last row displaying text in current
17962 matrix starting at first_unchanged_at_end_row, after
17963 scrolling. */
17964 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17965 row = find_last_row_displaying_text (w->current_matrix, &it,
17966 first_unchanged_at_end_row);
17967 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17968 adjust_window_ends (w, row, 1);
17969 eassert (w->window_end_bytepos >= 0);
17970 IF_DEBUG (debug_method_add (w, "A"));
17971 }
17972 else if (last_text_row_at_end)
17973 {
17974 adjust_window_ends (w, last_text_row_at_end, 0);
17975 eassert (w->window_end_bytepos >= 0);
17976 IF_DEBUG (debug_method_add (w, "B"));
17977 }
17978 else if (last_text_row)
17979 {
17980 /* We have displayed either to the end of the window or at the
17981 end of the window, i.e. the last row with text is to be found
17982 in the desired matrix. */
17983 adjust_window_ends (w, last_text_row, 0);
17984 eassert (w->window_end_bytepos >= 0);
17985 }
17986 else if (first_unchanged_at_end_row == NULL
17987 && last_text_row == NULL
17988 && last_text_row_at_end == NULL)
17989 {
17990 /* Displayed to end of window, but no line containing text was
17991 displayed. Lines were deleted at the end of the window. */
17992 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17993 int vpos = w->window_end_vpos;
17994 struct glyph_row *current_row = current_matrix->rows + vpos;
17995 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17996
17997 for (row = NULL;
17998 row == NULL && vpos >= first_vpos;
17999 --vpos, --current_row, --desired_row)
18000 {
18001 if (desired_row->enabled_p)
18002 {
18003 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18004 row = desired_row;
18005 }
18006 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18007 row = current_row;
18008 }
18009
18010 eassert (row != NULL);
18011 w->window_end_vpos = vpos + 1;
18012 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18013 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18014 eassert (w->window_end_bytepos >= 0);
18015 IF_DEBUG (debug_method_add (w, "C"));
18016 }
18017 else
18018 emacs_abort ();
18019
18020 IF_DEBUG (debug_end_pos = w->window_end_pos;
18021 debug_end_vpos = w->window_end_vpos);
18022
18023 /* Record that display has not been completed. */
18024 w->window_end_valid = 0;
18025 w->desired_matrix->no_scrolling_p = 1;
18026 return 3;
18027
18028 #undef GIVE_UP
18029 }
18030
18031
18032 \f
18033 /***********************************************************************
18034 More debugging support
18035 ***********************************************************************/
18036
18037 #ifdef GLYPH_DEBUG
18038
18039 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18040 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18041 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18042
18043
18044 /* Dump the contents of glyph matrix MATRIX on stderr.
18045
18046 GLYPHS 0 means don't show glyph contents.
18047 GLYPHS 1 means show glyphs in short form
18048 GLYPHS > 1 means show glyphs in long form. */
18049
18050 void
18051 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18052 {
18053 int i;
18054 for (i = 0; i < matrix->nrows; ++i)
18055 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18056 }
18057
18058
18059 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18060 the glyph row and area where the glyph comes from. */
18061
18062 void
18063 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18064 {
18065 if (glyph->type == CHAR_GLYPH
18066 || glyph->type == GLYPHLESS_GLYPH)
18067 {
18068 fprintf (stderr,
18069 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18070 glyph - row->glyphs[TEXT_AREA],
18071 (glyph->type == CHAR_GLYPH
18072 ? 'C'
18073 : 'G'),
18074 glyph->charpos,
18075 (BUFFERP (glyph->object)
18076 ? 'B'
18077 : (STRINGP (glyph->object)
18078 ? 'S'
18079 : (INTEGERP (glyph->object)
18080 ? '0'
18081 : '-'))),
18082 glyph->pixel_width,
18083 glyph->u.ch,
18084 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18085 ? glyph->u.ch
18086 : '.'),
18087 glyph->face_id,
18088 glyph->left_box_line_p,
18089 glyph->right_box_line_p);
18090 }
18091 else if (glyph->type == STRETCH_GLYPH)
18092 {
18093 fprintf (stderr,
18094 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18095 glyph - row->glyphs[TEXT_AREA],
18096 'S',
18097 glyph->charpos,
18098 (BUFFERP (glyph->object)
18099 ? 'B'
18100 : (STRINGP (glyph->object)
18101 ? 'S'
18102 : (INTEGERP (glyph->object)
18103 ? '0'
18104 : '-'))),
18105 glyph->pixel_width,
18106 0,
18107 ' ',
18108 glyph->face_id,
18109 glyph->left_box_line_p,
18110 glyph->right_box_line_p);
18111 }
18112 else if (glyph->type == IMAGE_GLYPH)
18113 {
18114 fprintf (stderr,
18115 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18116 glyph - row->glyphs[TEXT_AREA],
18117 'I',
18118 glyph->charpos,
18119 (BUFFERP (glyph->object)
18120 ? 'B'
18121 : (STRINGP (glyph->object)
18122 ? 'S'
18123 : (INTEGERP (glyph->object)
18124 ? '0'
18125 : '-'))),
18126 glyph->pixel_width,
18127 glyph->u.img_id,
18128 '.',
18129 glyph->face_id,
18130 glyph->left_box_line_p,
18131 glyph->right_box_line_p);
18132 }
18133 else if (glyph->type == COMPOSITE_GLYPH)
18134 {
18135 fprintf (stderr,
18136 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18137 glyph - row->glyphs[TEXT_AREA],
18138 '+',
18139 glyph->charpos,
18140 (BUFFERP (glyph->object)
18141 ? 'B'
18142 : (STRINGP (glyph->object)
18143 ? 'S'
18144 : (INTEGERP (glyph->object)
18145 ? '0'
18146 : '-'))),
18147 glyph->pixel_width,
18148 glyph->u.cmp.id);
18149 if (glyph->u.cmp.automatic)
18150 fprintf (stderr,
18151 "[%d-%d]",
18152 glyph->slice.cmp.from, glyph->slice.cmp.to);
18153 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18154 glyph->face_id,
18155 glyph->left_box_line_p,
18156 glyph->right_box_line_p);
18157 }
18158 }
18159
18160
18161 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18162 GLYPHS 0 means don't show glyph contents.
18163 GLYPHS 1 means show glyphs in short form
18164 GLYPHS > 1 means show glyphs in long form. */
18165
18166 void
18167 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18168 {
18169 if (glyphs != 1)
18170 {
18171 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18172 fprintf (stderr, "==============================================================================\n");
18173
18174 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18175 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18176 vpos,
18177 MATRIX_ROW_START_CHARPOS (row),
18178 MATRIX_ROW_END_CHARPOS (row),
18179 row->used[TEXT_AREA],
18180 row->contains_overlapping_glyphs_p,
18181 row->enabled_p,
18182 row->truncated_on_left_p,
18183 row->truncated_on_right_p,
18184 row->continued_p,
18185 MATRIX_ROW_CONTINUATION_LINE_P (row),
18186 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18187 row->ends_at_zv_p,
18188 row->fill_line_p,
18189 row->ends_in_middle_of_char_p,
18190 row->starts_in_middle_of_char_p,
18191 row->mouse_face_p,
18192 row->x,
18193 row->y,
18194 row->pixel_width,
18195 row->height,
18196 row->visible_height,
18197 row->ascent,
18198 row->phys_ascent);
18199 /* The next 3 lines should align to "Start" in the header. */
18200 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18201 row->end.overlay_string_index,
18202 row->continuation_lines_width);
18203 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18204 CHARPOS (row->start.string_pos),
18205 CHARPOS (row->end.string_pos));
18206 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18207 row->end.dpvec_index);
18208 }
18209
18210 if (glyphs > 1)
18211 {
18212 int area;
18213
18214 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18215 {
18216 struct glyph *glyph = row->glyphs[area];
18217 struct glyph *glyph_end = glyph + row->used[area];
18218
18219 /* Glyph for a line end in text. */
18220 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18221 ++glyph_end;
18222
18223 if (glyph < glyph_end)
18224 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18225
18226 for (; glyph < glyph_end; ++glyph)
18227 dump_glyph (row, glyph, area);
18228 }
18229 }
18230 else if (glyphs == 1)
18231 {
18232 int area;
18233
18234 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18235 {
18236 char *s = alloca (row->used[area] + 4);
18237 int i;
18238
18239 for (i = 0; i < row->used[area]; ++i)
18240 {
18241 struct glyph *glyph = row->glyphs[area] + i;
18242 if (i == row->used[area] - 1
18243 && area == TEXT_AREA
18244 && INTEGERP (glyph->object)
18245 && glyph->type == CHAR_GLYPH
18246 && glyph->u.ch == ' ')
18247 {
18248 strcpy (&s[i], "[\\n]");
18249 i += 4;
18250 }
18251 else if (glyph->type == CHAR_GLYPH
18252 && glyph->u.ch < 0x80
18253 && glyph->u.ch >= ' ')
18254 s[i] = glyph->u.ch;
18255 else
18256 s[i] = '.';
18257 }
18258
18259 s[i] = '\0';
18260 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18261 }
18262 }
18263 }
18264
18265
18266 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18267 Sdump_glyph_matrix, 0, 1, "p",
18268 doc: /* Dump the current matrix of the selected window to stderr.
18269 Shows contents of glyph row structures. With non-nil
18270 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18271 glyphs in short form, otherwise show glyphs in long form. */)
18272 (Lisp_Object glyphs)
18273 {
18274 struct window *w = XWINDOW (selected_window);
18275 struct buffer *buffer = XBUFFER (w->contents);
18276
18277 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18278 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18279 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18280 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18281 fprintf (stderr, "=============================================\n");
18282 dump_glyph_matrix (w->current_matrix,
18283 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18284 return Qnil;
18285 }
18286
18287
18288 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18289 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18290 (void)
18291 {
18292 struct frame *f = XFRAME (selected_frame);
18293 dump_glyph_matrix (f->current_matrix, 1);
18294 return Qnil;
18295 }
18296
18297
18298 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18299 doc: /* Dump glyph row ROW to stderr.
18300 GLYPH 0 means don't dump glyphs.
18301 GLYPH 1 means dump glyphs in short form.
18302 GLYPH > 1 or omitted means dump glyphs in long form. */)
18303 (Lisp_Object row, Lisp_Object glyphs)
18304 {
18305 struct glyph_matrix *matrix;
18306 EMACS_INT vpos;
18307
18308 CHECK_NUMBER (row);
18309 matrix = XWINDOW (selected_window)->current_matrix;
18310 vpos = XINT (row);
18311 if (vpos >= 0 && vpos < matrix->nrows)
18312 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18313 vpos,
18314 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18315 return Qnil;
18316 }
18317
18318
18319 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18320 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18321 GLYPH 0 means don't dump glyphs.
18322 GLYPH 1 means dump glyphs in short form.
18323 GLYPH > 1 or omitted means dump glyphs in long form.
18324
18325 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18326 do nothing. */)
18327 (Lisp_Object row, Lisp_Object glyphs)
18328 {
18329 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18330 struct frame *sf = SELECTED_FRAME ();
18331 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18332 EMACS_INT vpos;
18333
18334 CHECK_NUMBER (row);
18335 vpos = XINT (row);
18336 if (vpos >= 0 && vpos < m->nrows)
18337 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18338 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18339 #endif
18340 return Qnil;
18341 }
18342
18343
18344 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18345 doc: /* Toggle tracing of redisplay.
18346 With ARG, turn tracing on if and only if ARG is positive. */)
18347 (Lisp_Object arg)
18348 {
18349 if (NILP (arg))
18350 trace_redisplay_p = !trace_redisplay_p;
18351 else
18352 {
18353 arg = Fprefix_numeric_value (arg);
18354 trace_redisplay_p = XINT (arg) > 0;
18355 }
18356
18357 return Qnil;
18358 }
18359
18360
18361 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18362 doc: /* Like `format', but print result to stderr.
18363 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18364 (ptrdiff_t nargs, Lisp_Object *args)
18365 {
18366 Lisp_Object s = Fformat (nargs, args);
18367 fprintf (stderr, "%s", SDATA (s));
18368 return Qnil;
18369 }
18370
18371 #endif /* GLYPH_DEBUG */
18372
18373
18374 \f
18375 /***********************************************************************
18376 Building Desired Matrix Rows
18377 ***********************************************************************/
18378
18379 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18380 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18381
18382 static struct glyph_row *
18383 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18384 {
18385 struct frame *f = XFRAME (WINDOW_FRAME (w));
18386 struct buffer *buffer = XBUFFER (w->contents);
18387 struct buffer *old = current_buffer;
18388 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18389 int arrow_len = SCHARS (overlay_arrow_string);
18390 const unsigned char *arrow_end = arrow_string + arrow_len;
18391 const unsigned char *p;
18392 struct it it;
18393 bool multibyte_p;
18394 int n_glyphs_before;
18395
18396 set_buffer_temp (buffer);
18397 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18398 it.glyph_row->used[TEXT_AREA] = 0;
18399 SET_TEXT_POS (it.position, 0, 0);
18400
18401 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18402 p = arrow_string;
18403 while (p < arrow_end)
18404 {
18405 Lisp_Object face, ilisp;
18406
18407 /* Get the next character. */
18408 if (multibyte_p)
18409 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18410 else
18411 {
18412 it.c = it.char_to_display = *p, it.len = 1;
18413 if (! ASCII_CHAR_P (it.c))
18414 it.char_to_display = BYTE8_TO_CHAR (it.c);
18415 }
18416 p += it.len;
18417
18418 /* Get its face. */
18419 ilisp = make_number (p - arrow_string);
18420 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18421 it.face_id = compute_char_face (f, it.char_to_display, face);
18422
18423 /* Compute its width, get its glyphs. */
18424 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18425 SET_TEXT_POS (it.position, -1, -1);
18426 PRODUCE_GLYPHS (&it);
18427
18428 /* If this character doesn't fit any more in the line, we have
18429 to remove some glyphs. */
18430 if (it.current_x > it.last_visible_x)
18431 {
18432 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18433 break;
18434 }
18435 }
18436
18437 set_buffer_temp (old);
18438 return it.glyph_row;
18439 }
18440
18441
18442 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18443 glyphs to insert is determined by produce_special_glyphs. */
18444
18445 static void
18446 insert_left_trunc_glyphs (struct it *it)
18447 {
18448 struct it truncate_it;
18449 struct glyph *from, *end, *to, *toend;
18450
18451 eassert (!FRAME_WINDOW_P (it->f)
18452 || (!it->glyph_row->reversed_p
18453 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18454 || (it->glyph_row->reversed_p
18455 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18456
18457 /* Get the truncation glyphs. */
18458 truncate_it = *it;
18459 truncate_it.current_x = 0;
18460 truncate_it.face_id = DEFAULT_FACE_ID;
18461 truncate_it.glyph_row = &scratch_glyph_row;
18462 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18463 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18464 truncate_it.object = make_number (0);
18465 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18466
18467 /* Overwrite glyphs from IT with truncation glyphs. */
18468 if (!it->glyph_row->reversed_p)
18469 {
18470 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18471
18472 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18473 end = from + tused;
18474 to = it->glyph_row->glyphs[TEXT_AREA];
18475 toend = to + it->glyph_row->used[TEXT_AREA];
18476 if (FRAME_WINDOW_P (it->f))
18477 {
18478 /* On GUI frames, when variable-size fonts are displayed,
18479 the truncation glyphs may need more pixels than the row's
18480 glyphs they overwrite. We overwrite more glyphs to free
18481 enough screen real estate, and enlarge the stretch glyph
18482 on the right (see display_line), if there is one, to
18483 preserve the screen position of the truncation glyphs on
18484 the right. */
18485 int w = 0;
18486 struct glyph *g = to;
18487 short used;
18488
18489 /* The first glyph could be partially visible, in which case
18490 it->glyph_row->x will be negative. But we want the left
18491 truncation glyphs to be aligned at the left margin of the
18492 window, so we override the x coordinate at which the row
18493 will begin. */
18494 it->glyph_row->x = 0;
18495 while (g < toend && w < it->truncation_pixel_width)
18496 {
18497 w += g->pixel_width;
18498 ++g;
18499 }
18500 if (g - to - tused > 0)
18501 {
18502 memmove (to + tused, g, (toend - g) * sizeof(*g));
18503 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18504 }
18505 used = it->glyph_row->used[TEXT_AREA];
18506 if (it->glyph_row->truncated_on_right_p
18507 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18508 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18509 == STRETCH_GLYPH)
18510 {
18511 int extra = w - it->truncation_pixel_width;
18512
18513 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18514 }
18515 }
18516
18517 while (from < end)
18518 *to++ = *from++;
18519
18520 /* There may be padding glyphs left over. Overwrite them too. */
18521 if (!FRAME_WINDOW_P (it->f))
18522 {
18523 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18524 {
18525 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18526 while (from < end)
18527 *to++ = *from++;
18528 }
18529 }
18530
18531 if (to > toend)
18532 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18533 }
18534 else
18535 {
18536 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18537
18538 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18539 that back to front. */
18540 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18541 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18542 toend = it->glyph_row->glyphs[TEXT_AREA];
18543 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18544 if (FRAME_WINDOW_P (it->f))
18545 {
18546 int w = 0;
18547 struct glyph *g = to;
18548
18549 while (g >= toend && w < it->truncation_pixel_width)
18550 {
18551 w += g->pixel_width;
18552 --g;
18553 }
18554 if (to - g - tused > 0)
18555 to = g + tused;
18556 if (it->glyph_row->truncated_on_right_p
18557 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18558 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18559 {
18560 int extra = w - it->truncation_pixel_width;
18561
18562 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18563 }
18564 }
18565
18566 while (from >= end && to >= toend)
18567 *to-- = *from--;
18568 if (!FRAME_WINDOW_P (it->f))
18569 {
18570 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18571 {
18572 from =
18573 truncate_it.glyph_row->glyphs[TEXT_AREA]
18574 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18575 while (from >= end && to >= toend)
18576 *to-- = *from--;
18577 }
18578 }
18579 if (from >= end)
18580 {
18581 /* Need to free some room before prepending additional
18582 glyphs. */
18583 int move_by = from - end + 1;
18584 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18585 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18586
18587 for ( ; g >= g0; g--)
18588 g[move_by] = *g;
18589 while (from >= end)
18590 *to-- = *from--;
18591 it->glyph_row->used[TEXT_AREA] += move_by;
18592 }
18593 }
18594 }
18595
18596 /* Compute the hash code for ROW. */
18597 unsigned
18598 row_hash (struct glyph_row *row)
18599 {
18600 int area, k;
18601 unsigned hashval = 0;
18602
18603 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18604 for (k = 0; k < row->used[area]; ++k)
18605 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18606 + row->glyphs[area][k].u.val
18607 + row->glyphs[area][k].face_id
18608 + row->glyphs[area][k].padding_p
18609 + (row->glyphs[area][k].type << 2));
18610
18611 return hashval;
18612 }
18613
18614 /* Compute the pixel height and width of IT->glyph_row.
18615
18616 Most of the time, ascent and height of a display line will be equal
18617 to the max_ascent and max_height values of the display iterator
18618 structure. This is not the case if
18619
18620 1. We hit ZV without displaying anything. In this case, max_ascent
18621 and max_height will be zero.
18622
18623 2. We have some glyphs that don't contribute to the line height.
18624 (The glyph row flag contributes_to_line_height_p is for future
18625 pixmap extensions).
18626
18627 The first case is easily covered by using default values because in
18628 these cases, the line height does not really matter, except that it
18629 must not be zero. */
18630
18631 static void
18632 compute_line_metrics (struct it *it)
18633 {
18634 struct glyph_row *row = it->glyph_row;
18635
18636 if (FRAME_WINDOW_P (it->f))
18637 {
18638 int i, min_y, max_y;
18639
18640 /* The line may consist of one space only, that was added to
18641 place the cursor on it. If so, the row's height hasn't been
18642 computed yet. */
18643 if (row->height == 0)
18644 {
18645 if (it->max_ascent + it->max_descent == 0)
18646 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18647 row->ascent = it->max_ascent;
18648 row->height = it->max_ascent + it->max_descent;
18649 row->phys_ascent = it->max_phys_ascent;
18650 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18651 row->extra_line_spacing = it->max_extra_line_spacing;
18652 }
18653
18654 /* Compute the width of this line. */
18655 row->pixel_width = row->x;
18656 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18657 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18658
18659 eassert (row->pixel_width >= 0);
18660 eassert (row->ascent >= 0 && row->height > 0);
18661
18662 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18663 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18664
18665 /* If first line's physical ascent is larger than its logical
18666 ascent, use the physical ascent, and make the row taller.
18667 This makes accented characters fully visible. */
18668 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18669 && row->phys_ascent > row->ascent)
18670 {
18671 row->height += row->phys_ascent - row->ascent;
18672 row->ascent = row->phys_ascent;
18673 }
18674
18675 /* Compute how much of the line is visible. */
18676 row->visible_height = row->height;
18677
18678 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18679 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18680
18681 if (row->y < min_y)
18682 row->visible_height -= min_y - row->y;
18683 if (row->y + row->height > max_y)
18684 row->visible_height -= row->y + row->height - max_y;
18685 }
18686 else
18687 {
18688 row->pixel_width = row->used[TEXT_AREA];
18689 if (row->continued_p)
18690 row->pixel_width -= it->continuation_pixel_width;
18691 else if (row->truncated_on_right_p)
18692 row->pixel_width -= it->truncation_pixel_width;
18693 row->ascent = row->phys_ascent = 0;
18694 row->height = row->phys_height = row->visible_height = 1;
18695 row->extra_line_spacing = 0;
18696 }
18697
18698 /* Compute a hash code for this row. */
18699 row->hash = row_hash (row);
18700
18701 it->max_ascent = it->max_descent = 0;
18702 it->max_phys_ascent = it->max_phys_descent = 0;
18703 }
18704
18705
18706 /* Append one space to the glyph row of iterator IT if doing a
18707 window-based redisplay. The space has the same face as
18708 IT->face_id. Value is non-zero if a space was added.
18709
18710 This function is called to make sure that there is always one glyph
18711 at the end of a glyph row that the cursor can be set on under
18712 window-systems. (If there weren't such a glyph we would not know
18713 how wide and tall a box cursor should be displayed).
18714
18715 At the same time this space let's a nicely handle clearing to the
18716 end of the line if the row ends in italic text. */
18717
18718 static int
18719 append_space_for_newline (struct it *it, int default_face_p)
18720 {
18721 if (FRAME_WINDOW_P (it->f))
18722 {
18723 int n = it->glyph_row->used[TEXT_AREA];
18724
18725 if (it->glyph_row->glyphs[TEXT_AREA] + n
18726 < it->glyph_row->glyphs[1 + TEXT_AREA])
18727 {
18728 /* Save some values that must not be changed.
18729 Must save IT->c and IT->len because otherwise
18730 ITERATOR_AT_END_P wouldn't work anymore after
18731 append_space_for_newline has been called. */
18732 enum display_element_type saved_what = it->what;
18733 int saved_c = it->c, saved_len = it->len;
18734 int saved_char_to_display = it->char_to_display;
18735 int saved_x = it->current_x;
18736 int saved_face_id = it->face_id;
18737 int saved_box_end = it->end_of_box_run_p;
18738 struct text_pos saved_pos;
18739 Lisp_Object saved_object;
18740 struct face *face;
18741
18742 saved_object = it->object;
18743 saved_pos = it->position;
18744
18745 it->what = IT_CHARACTER;
18746 memset (&it->position, 0, sizeof it->position);
18747 it->object = make_number (0);
18748 it->c = it->char_to_display = ' ';
18749 it->len = 1;
18750
18751 /* If the default face was remapped, be sure to use the
18752 remapped face for the appended newline. */
18753 if (default_face_p)
18754 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18755 else if (it->face_before_selective_p)
18756 it->face_id = it->saved_face_id;
18757 face = FACE_FROM_ID (it->f, it->face_id);
18758 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18759 /* In R2L rows, we will prepend a stretch glyph that will
18760 have the end_of_box_run_p flag set for it, so there's no
18761 need for the appended newline glyph to have that flag
18762 set. */
18763 if (it->glyph_row->reversed_p
18764 /* But if the appended newline glyph goes all the way to
18765 the end of the row, there will be no stretch glyph,
18766 so leave the box flag set. */
18767 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18768 it->end_of_box_run_p = 0;
18769
18770 PRODUCE_GLYPHS (it);
18771
18772 it->override_ascent = -1;
18773 it->constrain_row_ascent_descent_p = 0;
18774 it->current_x = saved_x;
18775 it->object = saved_object;
18776 it->position = saved_pos;
18777 it->what = saved_what;
18778 it->face_id = saved_face_id;
18779 it->len = saved_len;
18780 it->c = saved_c;
18781 it->char_to_display = saved_char_to_display;
18782 it->end_of_box_run_p = saved_box_end;
18783 return 1;
18784 }
18785 }
18786
18787 return 0;
18788 }
18789
18790
18791 /* Extend the face of the last glyph in the text area of IT->glyph_row
18792 to the end of the display line. Called from display_line. If the
18793 glyph row is empty, add a space glyph to it so that we know the
18794 face to draw. Set the glyph row flag fill_line_p. If the glyph
18795 row is R2L, prepend a stretch glyph to cover the empty space to the
18796 left of the leftmost glyph. */
18797
18798 static void
18799 extend_face_to_end_of_line (struct it *it)
18800 {
18801 struct face *face, *default_face;
18802 struct frame *f = it->f;
18803
18804 /* If line is already filled, do nothing. Non window-system frames
18805 get a grace of one more ``pixel'' because their characters are
18806 1-``pixel'' wide, so they hit the equality too early. This grace
18807 is needed only for R2L rows that are not continued, to produce
18808 one extra blank where we could display the cursor. */
18809 if (it->current_x >= it->last_visible_x
18810 + (!FRAME_WINDOW_P (f)
18811 && it->glyph_row->reversed_p
18812 && !it->glyph_row->continued_p))
18813 return;
18814
18815 /* The default face, possibly remapped. */
18816 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18817
18818 /* Face extension extends the background and box of IT->face_id
18819 to the end of the line. If the background equals the background
18820 of the frame, we don't have to do anything. */
18821 if (it->face_before_selective_p)
18822 face = FACE_FROM_ID (f, it->saved_face_id);
18823 else
18824 face = FACE_FROM_ID (f, it->face_id);
18825
18826 if (FRAME_WINDOW_P (f)
18827 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18828 && face->box == FACE_NO_BOX
18829 && face->background == FRAME_BACKGROUND_PIXEL (f)
18830 #ifdef HAVE_WINDOW_SYSTEM
18831 && !face->stipple
18832 #endif
18833 && !it->glyph_row->reversed_p)
18834 return;
18835
18836 /* Set the glyph row flag indicating that the face of the last glyph
18837 in the text area has to be drawn to the end of the text area. */
18838 it->glyph_row->fill_line_p = 1;
18839
18840 /* If current character of IT is not ASCII, make sure we have the
18841 ASCII face. This will be automatically undone the next time
18842 get_next_display_element returns a multibyte character. Note
18843 that the character will always be single byte in unibyte
18844 text. */
18845 if (!ASCII_CHAR_P (it->c))
18846 {
18847 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18848 }
18849
18850 if (FRAME_WINDOW_P (f))
18851 {
18852 /* If the row is empty, add a space with the current face of IT,
18853 so that we know which face to draw. */
18854 if (it->glyph_row->used[TEXT_AREA] == 0)
18855 {
18856 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18857 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18858 it->glyph_row->used[TEXT_AREA] = 1;
18859 }
18860 #ifdef HAVE_WINDOW_SYSTEM
18861 if (it->glyph_row->reversed_p)
18862 {
18863 /* Prepend a stretch glyph to the row, such that the
18864 rightmost glyph will be drawn flushed all the way to the
18865 right margin of the window. The stretch glyph that will
18866 occupy the empty space, if any, to the left of the
18867 glyphs. */
18868 struct font *font = face->font ? face->font : FRAME_FONT (f);
18869 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18870 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18871 struct glyph *g;
18872 int row_width, stretch_ascent, stretch_width;
18873 struct text_pos saved_pos;
18874 int saved_face_id, saved_avoid_cursor, saved_box_start;
18875
18876 for (row_width = 0, g = row_start; g < row_end; g++)
18877 row_width += g->pixel_width;
18878 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18879 if (stretch_width > 0)
18880 {
18881 stretch_ascent =
18882 (((it->ascent + it->descent)
18883 * FONT_BASE (font)) / FONT_HEIGHT (font));
18884 saved_pos = it->position;
18885 memset (&it->position, 0, sizeof it->position);
18886 saved_avoid_cursor = it->avoid_cursor_p;
18887 it->avoid_cursor_p = 1;
18888 saved_face_id = it->face_id;
18889 saved_box_start = it->start_of_box_run_p;
18890 /* The last row's stretch glyph should get the default
18891 face, to avoid painting the rest of the window with
18892 the region face, if the region ends at ZV. */
18893 if (it->glyph_row->ends_at_zv_p)
18894 it->face_id = default_face->id;
18895 else
18896 it->face_id = face->id;
18897 it->start_of_box_run_p = 0;
18898 append_stretch_glyph (it, make_number (0), stretch_width,
18899 it->ascent + it->descent, stretch_ascent);
18900 it->position = saved_pos;
18901 it->avoid_cursor_p = saved_avoid_cursor;
18902 it->face_id = saved_face_id;
18903 it->start_of_box_run_p = saved_box_start;
18904 }
18905 }
18906 #endif /* HAVE_WINDOW_SYSTEM */
18907 }
18908 else
18909 {
18910 /* Save some values that must not be changed. */
18911 int saved_x = it->current_x;
18912 struct text_pos saved_pos;
18913 Lisp_Object saved_object;
18914 enum display_element_type saved_what = it->what;
18915 int saved_face_id = it->face_id;
18916
18917 saved_object = it->object;
18918 saved_pos = it->position;
18919
18920 it->what = IT_CHARACTER;
18921 memset (&it->position, 0, sizeof it->position);
18922 it->object = make_number (0);
18923 it->c = it->char_to_display = ' ';
18924 it->len = 1;
18925 /* The last row's blank glyphs should get the default face, to
18926 avoid painting the rest of the window with the region face,
18927 if the region ends at ZV. */
18928 if (it->glyph_row->ends_at_zv_p)
18929 it->face_id = default_face->id;
18930 else
18931 it->face_id = face->id;
18932
18933 PRODUCE_GLYPHS (it);
18934
18935 while (it->current_x <= it->last_visible_x)
18936 PRODUCE_GLYPHS (it);
18937
18938 /* Don't count these blanks really. It would let us insert a left
18939 truncation glyph below and make us set the cursor on them, maybe. */
18940 it->current_x = saved_x;
18941 it->object = saved_object;
18942 it->position = saved_pos;
18943 it->what = saved_what;
18944 it->face_id = saved_face_id;
18945 }
18946 }
18947
18948
18949 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18950 trailing whitespace. */
18951
18952 static int
18953 trailing_whitespace_p (ptrdiff_t charpos)
18954 {
18955 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18956 int c = 0;
18957
18958 while (bytepos < ZV_BYTE
18959 && (c = FETCH_CHAR (bytepos),
18960 c == ' ' || c == '\t'))
18961 ++bytepos;
18962
18963 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18964 {
18965 if (bytepos != PT_BYTE)
18966 return 1;
18967 }
18968 return 0;
18969 }
18970
18971
18972 /* Highlight trailing whitespace, if any, in ROW. */
18973
18974 static void
18975 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18976 {
18977 int used = row->used[TEXT_AREA];
18978
18979 if (used)
18980 {
18981 struct glyph *start = row->glyphs[TEXT_AREA];
18982 struct glyph *glyph = start + used - 1;
18983
18984 if (row->reversed_p)
18985 {
18986 /* Right-to-left rows need to be processed in the opposite
18987 direction, so swap the edge pointers. */
18988 glyph = start;
18989 start = row->glyphs[TEXT_AREA] + used - 1;
18990 }
18991
18992 /* Skip over glyphs inserted to display the cursor at the
18993 end of a line, for extending the face of the last glyph
18994 to the end of the line on terminals, and for truncation
18995 and continuation glyphs. */
18996 if (!row->reversed_p)
18997 {
18998 while (glyph >= start
18999 && glyph->type == CHAR_GLYPH
19000 && INTEGERP (glyph->object))
19001 --glyph;
19002 }
19003 else
19004 {
19005 while (glyph <= start
19006 && glyph->type == CHAR_GLYPH
19007 && INTEGERP (glyph->object))
19008 ++glyph;
19009 }
19010
19011 /* If last glyph is a space or stretch, and it's trailing
19012 whitespace, set the face of all trailing whitespace glyphs in
19013 IT->glyph_row to `trailing-whitespace'. */
19014 if ((row->reversed_p ? glyph <= start : glyph >= start)
19015 && BUFFERP (glyph->object)
19016 && (glyph->type == STRETCH_GLYPH
19017 || (glyph->type == CHAR_GLYPH
19018 && glyph->u.ch == ' '))
19019 && trailing_whitespace_p (glyph->charpos))
19020 {
19021 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19022 if (face_id < 0)
19023 return;
19024
19025 if (!row->reversed_p)
19026 {
19027 while (glyph >= start
19028 && BUFFERP (glyph->object)
19029 && (glyph->type == STRETCH_GLYPH
19030 || (glyph->type == CHAR_GLYPH
19031 && glyph->u.ch == ' ')))
19032 (glyph--)->face_id = face_id;
19033 }
19034 else
19035 {
19036 while (glyph <= start
19037 && BUFFERP (glyph->object)
19038 && (glyph->type == STRETCH_GLYPH
19039 || (glyph->type == CHAR_GLYPH
19040 && glyph->u.ch == ' ')))
19041 (glyph++)->face_id = face_id;
19042 }
19043 }
19044 }
19045 }
19046
19047
19048 /* Value is non-zero if glyph row ROW should be
19049 considered to hold the buffer position CHARPOS. */
19050
19051 static int
19052 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19053 {
19054 int result = 1;
19055
19056 if (charpos == CHARPOS (row->end.pos)
19057 || charpos == MATRIX_ROW_END_CHARPOS (row))
19058 {
19059 /* Suppose the row ends on a string.
19060 Unless the row is continued, that means it ends on a newline
19061 in the string. If it's anything other than a display string
19062 (e.g., a before-string from an overlay), we don't want the
19063 cursor there. (This heuristic seems to give the optimal
19064 behavior for the various types of multi-line strings.)
19065 One exception: if the string has `cursor' property on one of
19066 its characters, we _do_ want the cursor there. */
19067 if (CHARPOS (row->end.string_pos) >= 0)
19068 {
19069 if (row->continued_p)
19070 result = 1;
19071 else
19072 {
19073 /* Check for `display' property. */
19074 struct glyph *beg = row->glyphs[TEXT_AREA];
19075 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19076 struct glyph *glyph;
19077
19078 result = 0;
19079 for (glyph = end; glyph >= beg; --glyph)
19080 if (STRINGP (glyph->object))
19081 {
19082 Lisp_Object prop
19083 = Fget_char_property (make_number (charpos),
19084 Qdisplay, Qnil);
19085 result =
19086 (!NILP (prop)
19087 && display_prop_string_p (prop, glyph->object));
19088 /* If there's a `cursor' property on one of the
19089 string's characters, this row is a cursor row,
19090 even though this is not a display string. */
19091 if (!result)
19092 {
19093 Lisp_Object s = glyph->object;
19094
19095 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19096 {
19097 ptrdiff_t gpos = glyph->charpos;
19098
19099 if (!NILP (Fget_char_property (make_number (gpos),
19100 Qcursor, s)))
19101 {
19102 result = 1;
19103 break;
19104 }
19105 }
19106 }
19107 break;
19108 }
19109 }
19110 }
19111 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19112 {
19113 /* If the row ends in middle of a real character,
19114 and the line is continued, we want the cursor here.
19115 That's because CHARPOS (ROW->end.pos) would equal
19116 PT if PT is before the character. */
19117 if (!row->ends_in_ellipsis_p)
19118 result = row->continued_p;
19119 else
19120 /* If the row ends in an ellipsis, then
19121 CHARPOS (ROW->end.pos) will equal point after the
19122 invisible text. We want that position to be displayed
19123 after the ellipsis. */
19124 result = 0;
19125 }
19126 /* If the row ends at ZV, display the cursor at the end of that
19127 row instead of at the start of the row below. */
19128 else if (row->ends_at_zv_p)
19129 result = 1;
19130 else
19131 result = 0;
19132 }
19133
19134 return result;
19135 }
19136
19137 /* Value is non-zero if glyph row ROW should be
19138 used to hold the cursor. */
19139
19140 static int
19141 cursor_row_p (struct glyph_row *row)
19142 {
19143 return row_for_charpos_p (row, PT);
19144 }
19145
19146 \f
19147
19148 /* Push the property PROP so that it will be rendered at the current
19149 position in IT. Return 1 if PROP was successfully pushed, 0
19150 otherwise. Called from handle_line_prefix to handle the
19151 `line-prefix' and `wrap-prefix' properties. */
19152
19153 static int
19154 push_prefix_prop (struct it *it, Lisp_Object prop)
19155 {
19156 struct text_pos pos =
19157 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19158
19159 eassert (it->method == GET_FROM_BUFFER
19160 || it->method == GET_FROM_DISPLAY_VECTOR
19161 || it->method == GET_FROM_STRING);
19162
19163 /* We need to save the current buffer/string position, so it will be
19164 restored by pop_it, because iterate_out_of_display_property
19165 depends on that being set correctly, but some situations leave
19166 it->position not yet set when this function is called. */
19167 push_it (it, &pos);
19168
19169 if (STRINGP (prop))
19170 {
19171 if (SCHARS (prop) == 0)
19172 {
19173 pop_it (it);
19174 return 0;
19175 }
19176
19177 it->string = prop;
19178 it->string_from_prefix_prop_p = 1;
19179 it->multibyte_p = STRING_MULTIBYTE (it->string);
19180 it->current.overlay_string_index = -1;
19181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19182 it->end_charpos = it->string_nchars = SCHARS (it->string);
19183 it->method = GET_FROM_STRING;
19184 it->stop_charpos = 0;
19185 it->prev_stop = 0;
19186 it->base_level_stop = 0;
19187
19188 /* Force paragraph direction to be that of the parent
19189 buffer/string. */
19190 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19191 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19192 else
19193 it->paragraph_embedding = L2R;
19194
19195 /* Set up the bidi iterator for this display string. */
19196 if (it->bidi_p)
19197 {
19198 it->bidi_it.string.lstring = it->string;
19199 it->bidi_it.string.s = NULL;
19200 it->bidi_it.string.schars = it->end_charpos;
19201 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19202 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19203 it->bidi_it.string.unibyte = !it->multibyte_p;
19204 it->bidi_it.w = it->w;
19205 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19206 }
19207 }
19208 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19209 {
19210 it->method = GET_FROM_STRETCH;
19211 it->object = prop;
19212 }
19213 #ifdef HAVE_WINDOW_SYSTEM
19214 else if (IMAGEP (prop))
19215 {
19216 it->what = IT_IMAGE;
19217 it->image_id = lookup_image (it->f, prop);
19218 it->method = GET_FROM_IMAGE;
19219 }
19220 #endif /* HAVE_WINDOW_SYSTEM */
19221 else
19222 {
19223 pop_it (it); /* bogus display property, give up */
19224 return 0;
19225 }
19226
19227 return 1;
19228 }
19229
19230 /* Return the character-property PROP at the current position in IT. */
19231
19232 static Lisp_Object
19233 get_it_property (struct it *it, Lisp_Object prop)
19234 {
19235 Lisp_Object position, object = it->object;
19236
19237 if (STRINGP (object))
19238 position = make_number (IT_STRING_CHARPOS (*it));
19239 else if (BUFFERP (object))
19240 {
19241 position = make_number (IT_CHARPOS (*it));
19242 object = it->window;
19243 }
19244 else
19245 return Qnil;
19246
19247 return Fget_char_property (position, prop, object);
19248 }
19249
19250 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19251
19252 static void
19253 handle_line_prefix (struct it *it)
19254 {
19255 Lisp_Object prefix;
19256
19257 if (it->continuation_lines_width > 0)
19258 {
19259 prefix = get_it_property (it, Qwrap_prefix);
19260 if (NILP (prefix))
19261 prefix = Vwrap_prefix;
19262 }
19263 else
19264 {
19265 prefix = get_it_property (it, Qline_prefix);
19266 if (NILP (prefix))
19267 prefix = Vline_prefix;
19268 }
19269 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19270 {
19271 /* If the prefix is wider than the window, and we try to wrap
19272 it, it would acquire its own wrap prefix, and so on till the
19273 iterator stack overflows. So, don't wrap the prefix. */
19274 it->line_wrap = TRUNCATE;
19275 it->avoid_cursor_p = 1;
19276 }
19277 }
19278
19279 \f
19280
19281 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19282 only for R2L lines from display_line and display_string, when they
19283 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19284 the line/string needs to be continued on the next glyph row. */
19285 static void
19286 unproduce_glyphs (struct it *it, int n)
19287 {
19288 struct glyph *glyph, *end;
19289
19290 eassert (it->glyph_row);
19291 eassert (it->glyph_row->reversed_p);
19292 eassert (it->area == TEXT_AREA);
19293 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19294
19295 if (n > it->glyph_row->used[TEXT_AREA])
19296 n = it->glyph_row->used[TEXT_AREA];
19297 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19298 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19299 for ( ; glyph < end; glyph++)
19300 glyph[-n] = *glyph;
19301 }
19302
19303 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19304 and ROW->maxpos. */
19305 static void
19306 find_row_edges (struct it *it, struct glyph_row *row,
19307 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19308 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19309 {
19310 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19311 lines' rows is implemented for bidi-reordered rows. */
19312
19313 /* ROW->minpos is the value of min_pos, the minimal buffer position
19314 we have in ROW, or ROW->start.pos if that is smaller. */
19315 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19316 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19317 else
19318 /* We didn't find buffer positions smaller than ROW->start, or
19319 didn't find _any_ valid buffer positions in any of the glyphs,
19320 so we must trust the iterator's computed positions. */
19321 row->minpos = row->start.pos;
19322 if (max_pos <= 0)
19323 {
19324 max_pos = CHARPOS (it->current.pos);
19325 max_bpos = BYTEPOS (it->current.pos);
19326 }
19327
19328 /* Here are the various use-cases for ending the row, and the
19329 corresponding values for ROW->maxpos:
19330
19331 Line ends in a newline from buffer eol_pos + 1
19332 Line is continued from buffer max_pos + 1
19333 Line is truncated on right it->current.pos
19334 Line ends in a newline from string max_pos + 1(*)
19335 (*) + 1 only when line ends in a forward scan
19336 Line is continued from string max_pos
19337 Line is continued from display vector max_pos
19338 Line is entirely from a string min_pos == max_pos
19339 Line is entirely from a display vector min_pos == max_pos
19340 Line that ends at ZV ZV
19341
19342 If you discover other use-cases, please add them here as
19343 appropriate. */
19344 if (row->ends_at_zv_p)
19345 row->maxpos = it->current.pos;
19346 else if (row->used[TEXT_AREA])
19347 {
19348 int seen_this_string = 0;
19349 struct glyph_row *r1 = row - 1;
19350
19351 /* Did we see the same display string on the previous row? */
19352 if (STRINGP (it->object)
19353 /* this is not the first row */
19354 && row > it->w->desired_matrix->rows
19355 /* previous row is not the header line */
19356 && !r1->mode_line_p
19357 /* previous row also ends in a newline from a string */
19358 && r1->ends_in_newline_from_string_p)
19359 {
19360 struct glyph *start, *end;
19361
19362 /* Search for the last glyph of the previous row that came
19363 from buffer or string. Depending on whether the row is
19364 L2R or R2L, we need to process it front to back or the
19365 other way round. */
19366 if (!r1->reversed_p)
19367 {
19368 start = r1->glyphs[TEXT_AREA];
19369 end = start + r1->used[TEXT_AREA];
19370 /* Glyphs inserted by redisplay have an integer (zero)
19371 as their object. */
19372 while (end > start
19373 && INTEGERP ((end - 1)->object)
19374 && (end - 1)->charpos <= 0)
19375 --end;
19376 if (end > start)
19377 {
19378 if (EQ ((end - 1)->object, it->object))
19379 seen_this_string = 1;
19380 }
19381 else
19382 /* If all the glyphs of the previous row were inserted
19383 by redisplay, it means the previous row was
19384 produced from a single newline, which is only
19385 possible if that newline came from the same string
19386 as the one which produced this ROW. */
19387 seen_this_string = 1;
19388 }
19389 else
19390 {
19391 end = r1->glyphs[TEXT_AREA] - 1;
19392 start = end + r1->used[TEXT_AREA];
19393 while (end < start
19394 && INTEGERP ((end + 1)->object)
19395 && (end + 1)->charpos <= 0)
19396 ++end;
19397 if (end < start)
19398 {
19399 if (EQ ((end + 1)->object, it->object))
19400 seen_this_string = 1;
19401 }
19402 else
19403 seen_this_string = 1;
19404 }
19405 }
19406 /* Take note of each display string that covers a newline only
19407 once, the first time we see it. This is for when a display
19408 string includes more than one newline in it. */
19409 if (row->ends_in_newline_from_string_p && !seen_this_string)
19410 {
19411 /* If we were scanning the buffer forward when we displayed
19412 the string, we want to account for at least one buffer
19413 position that belongs to this row (position covered by
19414 the display string), so that cursor positioning will
19415 consider this row as a candidate when point is at the end
19416 of the visual line represented by this row. This is not
19417 required when scanning back, because max_pos will already
19418 have a much larger value. */
19419 if (CHARPOS (row->end.pos) > max_pos)
19420 INC_BOTH (max_pos, max_bpos);
19421 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19422 }
19423 else if (CHARPOS (it->eol_pos) > 0)
19424 SET_TEXT_POS (row->maxpos,
19425 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19426 else if (row->continued_p)
19427 {
19428 /* If max_pos is different from IT's current position, it
19429 means IT->method does not belong to the display element
19430 at max_pos. However, it also means that the display
19431 element at max_pos was displayed in its entirety on this
19432 line, which is equivalent to saying that the next line
19433 starts at the next buffer position. */
19434 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19435 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19436 else
19437 {
19438 INC_BOTH (max_pos, max_bpos);
19439 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19440 }
19441 }
19442 else if (row->truncated_on_right_p)
19443 /* display_line already called reseat_at_next_visible_line_start,
19444 which puts the iterator at the beginning of the next line, in
19445 the logical order. */
19446 row->maxpos = it->current.pos;
19447 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19448 /* A line that is entirely from a string/image/stretch... */
19449 row->maxpos = row->minpos;
19450 else
19451 emacs_abort ();
19452 }
19453 else
19454 row->maxpos = it->current.pos;
19455 }
19456
19457 /* Construct the glyph row IT->glyph_row in the desired matrix of
19458 IT->w from text at the current position of IT. See dispextern.h
19459 for an overview of struct it. Value is non-zero if
19460 IT->glyph_row displays text, as opposed to a line displaying ZV
19461 only. */
19462
19463 static int
19464 display_line (struct it *it)
19465 {
19466 struct glyph_row *row = it->glyph_row;
19467 Lisp_Object overlay_arrow_string;
19468 struct it wrap_it;
19469 void *wrap_data = NULL;
19470 int may_wrap = 0, wrap_x IF_LINT (= 0);
19471 int wrap_row_used = -1;
19472 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19473 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19474 int wrap_row_extra_line_spacing IF_LINT (= 0);
19475 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19476 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19477 int cvpos;
19478 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19479 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19480
19481 /* We always start displaying at hpos zero even if hscrolled. */
19482 eassert (it->hpos == 0 && it->current_x == 0);
19483
19484 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19485 >= it->w->desired_matrix->nrows)
19486 {
19487 it->w->nrows_scale_factor++;
19488 it->f->fonts_changed = 1;
19489 return 0;
19490 }
19491
19492 /* Clear the result glyph row and enable it. */
19493 prepare_desired_row (row);
19494
19495 row->y = it->current_y;
19496 row->start = it->start;
19497 row->continuation_lines_width = it->continuation_lines_width;
19498 row->displays_text_p = 1;
19499 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19500 it->starts_in_middle_of_char_p = 0;
19501
19502 /* Arrange the overlays nicely for our purposes. Usually, we call
19503 display_line on only one line at a time, in which case this
19504 can't really hurt too much, or we call it on lines which appear
19505 one after another in the buffer, in which case all calls to
19506 recenter_overlay_lists but the first will be pretty cheap. */
19507 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19508
19509 /* Move over display elements that are not visible because we are
19510 hscrolled. This may stop at an x-position < IT->first_visible_x
19511 if the first glyph is partially visible or if we hit a line end. */
19512 if (it->current_x < it->first_visible_x)
19513 {
19514 enum move_it_result move_result;
19515
19516 this_line_min_pos = row->start.pos;
19517 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19518 MOVE_TO_POS | MOVE_TO_X);
19519 /* If we are under a large hscroll, move_it_in_display_line_to
19520 could hit the end of the line without reaching
19521 it->first_visible_x. Pretend that we did reach it. This is
19522 especially important on a TTY, where we will call
19523 extend_face_to_end_of_line, which needs to know how many
19524 blank glyphs to produce. */
19525 if (it->current_x < it->first_visible_x
19526 && (move_result == MOVE_NEWLINE_OR_CR
19527 || move_result == MOVE_POS_MATCH_OR_ZV))
19528 it->current_x = it->first_visible_x;
19529
19530 /* Record the smallest positions seen while we moved over
19531 display elements that are not visible. This is needed by
19532 redisplay_internal for optimizing the case where the cursor
19533 stays inside the same line. The rest of this function only
19534 considers positions that are actually displayed, so
19535 RECORD_MAX_MIN_POS will not otherwise record positions that
19536 are hscrolled to the left of the left edge of the window. */
19537 min_pos = CHARPOS (this_line_min_pos);
19538 min_bpos = BYTEPOS (this_line_min_pos);
19539 }
19540 else
19541 {
19542 /* We only do this when not calling `move_it_in_display_line_to'
19543 above, because move_it_in_display_line_to calls
19544 handle_line_prefix itself. */
19545 handle_line_prefix (it);
19546 }
19547
19548 /* Get the initial row height. This is either the height of the
19549 text hscrolled, if there is any, or zero. */
19550 row->ascent = it->max_ascent;
19551 row->height = it->max_ascent + it->max_descent;
19552 row->phys_ascent = it->max_phys_ascent;
19553 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19554 row->extra_line_spacing = it->max_extra_line_spacing;
19555
19556 /* Utility macro to record max and min buffer positions seen until now. */
19557 #define RECORD_MAX_MIN_POS(IT) \
19558 do \
19559 { \
19560 int composition_p = !STRINGP ((IT)->string) \
19561 && ((IT)->what == IT_COMPOSITION); \
19562 ptrdiff_t current_pos = \
19563 composition_p ? (IT)->cmp_it.charpos \
19564 : IT_CHARPOS (*(IT)); \
19565 ptrdiff_t current_bpos = \
19566 composition_p ? CHAR_TO_BYTE (current_pos) \
19567 : IT_BYTEPOS (*(IT)); \
19568 if (current_pos < min_pos) \
19569 { \
19570 min_pos = current_pos; \
19571 min_bpos = current_bpos; \
19572 } \
19573 if (IT_CHARPOS (*it) > max_pos) \
19574 { \
19575 max_pos = IT_CHARPOS (*it); \
19576 max_bpos = IT_BYTEPOS (*it); \
19577 } \
19578 } \
19579 while (0)
19580
19581 /* Loop generating characters. The loop is left with IT on the next
19582 character to display. */
19583 while (1)
19584 {
19585 int n_glyphs_before, hpos_before, x_before;
19586 int x, nglyphs;
19587 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19588
19589 /* Retrieve the next thing to display. Value is zero if end of
19590 buffer reached. */
19591 if (!get_next_display_element (it))
19592 {
19593 /* Maybe add a space at the end of this line that is used to
19594 display the cursor there under X. Set the charpos of the
19595 first glyph of blank lines not corresponding to any text
19596 to -1. */
19597 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19598 row->exact_window_width_line_p = 1;
19599 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19600 || row->used[TEXT_AREA] == 0)
19601 {
19602 row->glyphs[TEXT_AREA]->charpos = -1;
19603 row->displays_text_p = 0;
19604
19605 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19606 && (!MINI_WINDOW_P (it->w)
19607 || (minibuf_level && EQ (it->window, minibuf_window))))
19608 row->indicate_empty_line_p = 1;
19609 }
19610
19611 it->continuation_lines_width = 0;
19612 row->ends_at_zv_p = 1;
19613 /* A row that displays right-to-left text must always have
19614 its last face extended all the way to the end of line,
19615 even if this row ends in ZV, because we still write to
19616 the screen left to right. We also need to extend the
19617 last face if the default face is remapped to some
19618 different face, otherwise the functions that clear
19619 portions of the screen will clear with the default face's
19620 background color. */
19621 if (row->reversed_p
19622 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19623 extend_face_to_end_of_line (it);
19624 break;
19625 }
19626
19627 /* Now, get the metrics of what we want to display. This also
19628 generates glyphs in `row' (which is IT->glyph_row). */
19629 n_glyphs_before = row->used[TEXT_AREA];
19630 x = it->current_x;
19631
19632 /* Remember the line height so far in case the next element doesn't
19633 fit on the line. */
19634 if (it->line_wrap != TRUNCATE)
19635 {
19636 ascent = it->max_ascent;
19637 descent = it->max_descent;
19638 phys_ascent = it->max_phys_ascent;
19639 phys_descent = it->max_phys_descent;
19640
19641 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19642 {
19643 if (IT_DISPLAYING_WHITESPACE (it))
19644 may_wrap = 1;
19645 else if (may_wrap)
19646 {
19647 SAVE_IT (wrap_it, *it, wrap_data);
19648 wrap_x = x;
19649 wrap_row_used = row->used[TEXT_AREA];
19650 wrap_row_ascent = row->ascent;
19651 wrap_row_height = row->height;
19652 wrap_row_phys_ascent = row->phys_ascent;
19653 wrap_row_phys_height = row->phys_height;
19654 wrap_row_extra_line_spacing = row->extra_line_spacing;
19655 wrap_row_min_pos = min_pos;
19656 wrap_row_min_bpos = min_bpos;
19657 wrap_row_max_pos = max_pos;
19658 wrap_row_max_bpos = max_bpos;
19659 may_wrap = 0;
19660 }
19661 }
19662 }
19663
19664 PRODUCE_GLYPHS (it);
19665
19666 /* If this display element was in marginal areas, continue with
19667 the next one. */
19668 if (it->area != TEXT_AREA)
19669 {
19670 row->ascent = max (row->ascent, it->max_ascent);
19671 row->height = max (row->height, it->max_ascent + it->max_descent);
19672 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19673 row->phys_height = max (row->phys_height,
19674 it->max_phys_ascent + it->max_phys_descent);
19675 row->extra_line_spacing = max (row->extra_line_spacing,
19676 it->max_extra_line_spacing);
19677 set_iterator_to_next (it, 1);
19678 continue;
19679 }
19680
19681 /* Does the display element fit on the line? If we truncate
19682 lines, we should draw past the right edge of the window. If
19683 we don't truncate, we want to stop so that we can display the
19684 continuation glyph before the right margin. If lines are
19685 continued, there are two possible strategies for characters
19686 resulting in more than 1 glyph (e.g. tabs): Display as many
19687 glyphs as possible in this line and leave the rest for the
19688 continuation line, or display the whole element in the next
19689 line. Original redisplay did the former, so we do it also. */
19690 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19691 hpos_before = it->hpos;
19692 x_before = x;
19693
19694 if (/* Not a newline. */
19695 nglyphs > 0
19696 /* Glyphs produced fit entirely in the line. */
19697 && it->current_x < it->last_visible_x)
19698 {
19699 it->hpos += nglyphs;
19700 row->ascent = max (row->ascent, it->max_ascent);
19701 row->height = max (row->height, it->max_ascent + it->max_descent);
19702 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19703 row->phys_height = max (row->phys_height,
19704 it->max_phys_ascent + it->max_phys_descent);
19705 row->extra_line_spacing = max (row->extra_line_spacing,
19706 it->max_extra_line_spacing);
19707 if (it->current_x - it->pixel_width < it->first_visible_x)
19708 row->x = x - it->first_visible_x;
19709 /* Record the maximum and minimum buffer positions seen so
19710 far in glyphs that will be displayed by this row. */
19711 if (it->bidi_p)
19712 RECORD_MAX_MIN_POS (it);
19713 }
19714 else
19715 {
19716 int i, new_x;
19717 struct glyph *glyph;
19718
19719 for (i = 0; i < nglyphs; ++i, x = new_x)
19720 {
19721 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19722 new_x = x + glyph->pixel_width;
19723
19724 if (/* Lines are continued. */
19725 it->line_wrap != TRUNCATE
19726 && (/* Glyph doesn't fit on the line. */
19727 new_x > it->last_visible_x
19728 /* Or it fits exactly on a window system frame. */
19729 || (new_x == it->last_visible_x
19730 && FRAME_WINDOW_P (it->f)
19731 && (row->reversed_p
19732 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19733 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19734 {
19735 /* End of a continued line. */
19736
19737 if (it->hpos == 0
19738 || (new_x == it->last_visible_x
19739 && FRAME_WINDOW_P (it->f)
19740 && (row->reversed_p
19741 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19742 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19743 {
19744 /* Current glyph is the only one on the line or
19745 fits exactly on the line. We must continue
19746 the line because we can't draw the cursor
19747 after the glyph. */
19748 row->continued_p = 1;
19749 it->current_x = new_x;
19750 it->continuation_lines_width += new_x;
19751 ++it->hpos;
19752 if (i == nglyphs - 1)
19753 {
19754 /* If line-wrap is on, check if a previous
19755 wrap point was found. */
19756 if (wrap_row_used > 0
19757 /* Even if there is a previous wrap
19758 point, continue the line here as
19759 usual, if (i) the previous character
19760 was a space or tab AND (ii) the
19761 current character is not. */
19762 && (!may_wrap
19763 || IT_DISPLAYING_WHITESPACE (it)))
19764 goto back_to_wrap;
19765
19766 /* Record the maximum and minimum buffer
19767 positions seen so far in glyphs that will be
19768 displayed by this row. */
19769 if (it->bidi_p)
19770 RECORD_MAX_MIN_POS (it);
19771 set_iterator_to_next (it, 1);
19772 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19773 {
19774 if (!get_next_display_element (it))
19775 {
19776 row->exact_window_width_line_p = 1;
19777 it->continuation_lines_width = 0;
19778 row->continued_p = 0;
19779 row->ends_at_zv_p = 1;
19780 }
19781 else if (ITERATOR_AT_END_OF_LINE_P (it))
19782 {
19783 row->continued_p = 0;
19784 row->exact_window_width_line_p = 1;
19785 }
19786 }
19787 }
19788 else if (it->bidi_p)
19789 RECORD_MAX_MIN_POS (it);
19790 }
19791 else if (CHAR_GLYPH_PADDING_P (*glyph)
19792 && !FRAME_WINDOW_P (it->f))
19793 {
19794 /* A padding glyph that doesn't fit on this line.
19795 This means the whole character doesn't fit
19796 on the line. */
19797 if (row->reversed_p)
19798 unproduce_glyphs (it, row->used[TEXT_AREA]
19799 - n_glyphs_before);
19800 row->used[TEXT_AREA] = n_glyphs_before;
19801
19802 /* Fill the rest of the row with continuation
19803 glyphs like in 20.x. */
19804 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19805 < row->glyphs[1 + TEXT_AREA])
19806 produce_special_glyphs (it, IT_CONTINUATION);
19807
19808 row->continued_p = 1;
19809 it->current_x = x_before;
19810 it->continuation_lines_width += x_before;
19811
19812 /* Restore the height to what it was before the
19813 element not fitting on the line. */
19814 it->max_ascent = ascent;
19815 it->max_descent = descent;
19816 it->max_phys_ascent = phys_ascent;
19817 it->max_phys_descent = phys_descent;
19818 }
19819 else if (wrap_row_used > 0)
19820 {
19821 back_to_wrap:
19822 if (row->reversed_p)
19823 unproduce_glyphs (it,
19824 row->used[TEXT_AREA] - wrap_row_used);
19825 RESTORE_IT (it, &wrap_it, wrap_data);
19826 it->continuation_lines_width += wrap_x;
19827 row->used[TEXT_AREA] = wrap_row_used;
19828 row->ascent = wrap_row_ascent;
19829 row->height = wrap_row_height;
19830 row->phys_ascent = wrap_row_phys_ascent;
19831 row->phys_height = wrap_row_phys_height;
19832 row->extra_line_spacing = wrap_row_extra_line_spacing;
19833 min_pos = wrap_row_min_pos;
19834 min_bpos = wrap_row_min_bpos;
19835 max_pos = wrap_row_max_pos;
19836 max_bpos = wrap_row_max_bpos;
19837 row->continued_p = 1;
19838 row->ends_at_zv_p = 0;
19839 row->exact_window_width_line_p = 0;
19840 it->continuation_lines_width += x;
19841
19842 /* Make sure that a non-default face is extended
19843 up to the right margin of the window. */
19844 extend_face_to_end_of_line (it);
19845 }
19846 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19847 {
19848 /* A TAB that extends past the right edge of the
19849 window. This produces a single glyph on
19850 window system frames. We leave the glyph in
19851 this row and let it fill the row, but don't
19852 consume the TAB. */
19853 if ((row->reversed_p
19854 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19855 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19856 produce_special_glyphs (it, IT_CONTINUATION);
19857 it->continuation_lines_width += it->last_visible_x;
19858 row->ends_in_middle_of_char_p = 1;
19859 row->continued_p = 1;
19860 glyph->pixel_width = it->last_visible_x - x;
19861 it->starts_in_middle_of_char_p = 1;
19862 }
19863 else
19864 {
19865 /* Something other than a TAB that draws past
19866 the right edge of the window. Restore
19867 positions to values before the element. */
19868 if (row->reversed_p)
19869 unproduce_glyphs (it, row->used[TEXT_AREA]
19870 - (n_glyphs_before + i));
19871 row->used[TEXT_AREA] = n_glyphs_before + i;
19872
19873 /* Display continuation glyphs. */
19874 it->current_x = x_before;
19875 it->continuation_lines_width += x;
19876 if (!FRAME_WINDOW_P (it->f)
19877 || (row->reversed_p
19878 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19879 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19880 produce_special_glyphs (it, IT_CONTINUATION);
19881 row->continued_p = 1;
19882
19883 extend_face_to_end_of_line (it);
19884
19885 if (nglyphs > 1 && i > 0)
19886 {
19887 row->ends_in_middle_of_char_p = 1;
19888 it->starts_in_middle_of_char_p = 1;
19889 }
19890
19891 /* Restore the height to what it was before the
19892 element not fitting on the line. */
19893 it->max_ascent = ascent;
19894 it->max_descent = descent;
19895 it->max_phys_ascent = phys_ascent;
19896 it->max_phys_descent = phys_descent;
19897 }
19898
19899 break;
19900 }
19901 else if (new_x > it->first_visible_x)
19902 {
19903 /* Increment number of glyphs actually displayed. */
19904 ++it->hpos;
19905
19906 /* Record the maximum and minimum buffer positions
19907 seen so far in glyphs that will be displayed by
19908 this row. */
19909 if (it->bidi_p)
19910 RECORD_MAX_MIN_POS (it);
19911
19912 if (x < it->first_visible_x)
19913 /* Glyph is partially visible, i.e. row starts at
19914 negative X position. */
19915 row->x = x - it->first_visible_x;
19916 }
19917 else
19918 {
19919 /* Glyph is completely off the left margin of the
19920 window. This should not happen because of the
19921 move_it_in_display_line at the start of this
19922 function, unless the text display area of the
19923 window is empty. */
19924 eassert (it->first_visible_x <= it->last_visible_x);
19925 }
19926 }
19927 /* Even if this display element produced no glyphs at all,
19928 we want to record its position. */
19929 if (it->bidi_p && nglyphs == 0)
19930 RECORD_MAX_MIN_POS (it);
19931
19932 row->ascent = max (row->ascent, it->max_ascent);
19933 row->height = max (row->height, it->max_ascent + it->max_descent);
19934 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19935 row->phys_height = max (row->phys_height,
19936 it->max_phys_ascent + it->max_phys_descent);
19937 row->extra_line_spacing = max (row->extra_line_spacing,
19938 it->max_extra_line_spacing);
19939
19940 /* End of this display line if row is continued. */
19941 if (row->continued_p || row->ends_at_zv_p)
19942 break;
19943 }
19944
19945 at_end_of_line:
19946 /* Is this a line end? If yes, we're also done, after making
19947 sure that a non-default face is extended up to the right
19948 margin of the window. */
19949 if (ITERATOR_AT_END_OF_LINE_P (it))
19950 {
19951 int used_before = row->used[TEXT_AREA];
19952
19953 row->ends_in_newline_from_string_p = STRINGP (it->object);
19954
19955 /* Add a space at the end of the line that is used to
19956 display the cursor there. */
19957 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19958 append_space_for_newline (it, 0);
19959
19960 /* Extend the face to the end of the line. */
19961 extend_face_to_end_of_line (it);
19962
19963 /* Make sure we have the position. */
19964 if (used_before == 0)
19965 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19966
19967 /* Record the position of the newline, for use in
19968 find_row_edges. */
19969 it->eol_pos = it->current.pos;
19970
19971 /* Consume the line end. This skips over invisible lines. */
19972 set_iterator_to_next (it, 1);
19973 it->continuation_lines_width = 0;
19974 break;
19975 }
19976
19977 /* Proceed with next display element. Note that this skips
19978 over lines invisible because of selective display. */
19979 set_iterator_to_next (it, 1);
19980
19981 /* If we truncate lines, we are done when the last displayed
19982 glyphs reach past the right margin of the window. */
19983 if (it->line_wrap == TRUNCATE
19984 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19985 ? (it->current_x >= it->last_visible_x)
19986 : (it->current_x > it->last_visible_x)))
19987 {
19988 /* Maybe add truncation glyphs. */
19989 if (!FRAME_WINDOW_P (it->f)
19990 || (row->reversed_p
19991 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19992 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19993 {
19994 int i, n;
19995
19996 if (!row->reversed_p)
19997 {
19998 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19999 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20000 break;
20001 }
20002 else
20003 {
20004 for (i = 0; i < row->used[TEXT_AREA]; i++)
20005 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20006 break;
20007 /* Remove any padding glyphs at the front of ROW, to
20008 make room for the truncation glyphs we will be
20009 adding below. The loop below always inserts at
20010 least one truncation glyph, so also remove the
20011 last glyph added to ROW. */
20012 unproduce_glyphs (it, i + 1);
20013 /* Adjust i for the loop below. */
20014 i = row->used[TEXT_AREA] - (i + 1);
20015 }
20016
20017 it->current_x = x_before;
20018 if (!FRAME_WINDOW_P (it->f))
20019 {
20020 for (n = row->used[TEXT_AREA]; i < n; ++i)
20021 {
20022 row->used[TEXT_AREA] = i;
20023 produce_special_glyphs (it, IT_TRUNCATION);
20024 }
20025 }
20026 else
20027 {
20028 row->used[TEXT_AREA] = i;
20029 produce_special_glyphs (it, IT_TRUNCATION);
20030 }
20031 }
20032 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20033 {
20034 /* Don't truncate if we can overflow newline into fringe. */
20035 if (!get_next_display_element (it))
20036 {
20037 it->continuation_lines_width = 0;
20038 row->ends_at_zv_p = 1;
20039 row->exact_window_width_line_p = 1;
20040 break;
20041 }
20042 if (ITERATOR_AT_END_OF_LINE_P (it))
20043 {
20044 row->exact_window_width_line_p = 1;
20045 goto at_end_of_line;
20046 }
20047 it->current_x = x_before;
20048 }
20049
20050 row->truncated_on_right_p = 1;
20051 it->continuation_lines_width = 0;
20052 reseat_at_next_visible_line_start (it, 0);
20053 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20054 it->hpos = hpos_before;
20055 break;
20056 }
20057 }
20058
20059 if (wrap_data)
20060 bidi_unshelve_cache (wrap_data, 1);
20061
20062 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20063 at the left window margin. */
20064 if (it->first_visible_x
20065 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20066 {
20067 if (!FRAME_WINDOW_P (it->f)
20068 || (row->reversed_p
20069 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20070 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20071 insert_left_trunc_glyphs (it);
20072 row->truncated_on_left_p = 1;
20073 }
20074
20075 /* Remember the position at which this line ends.
20076
20077 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20078 cannot be before the call to find_row_edges below, since that is
20079 where these positions are determined. */
20080 row->end = it->current;
20081 if (!it->bidi_p)
20082 {
20083 row->minpos = row->start.pos;
20084 row->maxpos = row->end.pos;
20085 }
20086 else
20087 {
20088 /* ROW->minpos and ROW->maxpos must be the smallest and
20089 `1 + the largest' buffer positions in ROW. But if ROW was
20090 bidi-reordered, these two positions can be anywhere in the
20091 row, so we must determine them now. */
20092 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20093 }
20094
20095 /* If the start of this line is the overlay arrow-position, then
20096 mark this glyph row as the one containing the overlay arrow.
20097 This is clearly a mess with variable size fonts. It would be
20098 better to let it be displayed like cursors under X. */
20099 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20100 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20101 !NILP (overlay_arrow_string)))
20102 {
20103 /* Overlay arrow in window redisplay is a fringe bitmap. */
20104 if (STRINGP (overlay_arrow_string))
20105 {
20106 struct glyph_row *arrow_row
20107 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20108 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20109 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20110 struct glyph *p = row->glyphs[TEXT_AREA];
20111 struct glyph *p2, *end;
20112
20113 /* Copy the arrow glyphs. */
20114 while (glyph < arrow_end)
20115 *p++ = *glyph++;
20116
20117 /* Throw away padding glyphs. */
20118 p2 = p;
20119 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20120 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20121 ++p2;
20122 if (p2 > p)
20123 {
20124 while (p2 < end)
20125 *p++ = *p2++;
20126 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20127 }
20128 }
20129 else
20130 {
20131 eassert (INTEGERP (overlay_arrow_string));
20132 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20133 }
20134 overlay_arrow_seen = 1;
20135 }
20136
20137 /* Highlight trailing whitespace. */
20138 if (!NILP (Vshow_trailing_whitespace))
20139 highlight_trailing_whitespace (it->f, it->glyph_row);
20140
20141 /* Compute pixel dimensions of this line. */
20142 compute_line_metrics (it);
20143
20144 /* Implementation note: No changes in the glyphs of ROW or in their
20145 faces can be done past this point, because compute_line_metrics
20146 computes ROW's hash value and stores it within the glyph_row
20147 structure. */
20148
20149 /* Record whether this row ends inside an ellipsis. */
20150 row->ends_in_ellipsis_p
20151 = (it->method == GET_FROM_DISPLAY_VECTOR
20152 && it->ellipsis_p);
20153
20154 /* Save fringe bitmaps in this row. */
20155 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20156 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20157 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20158 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20159
20160 it->left_user_fringe_bitmap = 0;
20161 it->left_user_fringe_face_id = 0;
20162 it->right_user_fringe_bitmap = 0;
20163 it->right_user_fringe_face_id = 0;
20164
20165 /* Maybe set the cursor. */
20166 cvpos = it->w->cursor.vpos;
20167 if ((cvpos < 0
20168 /* In bidi-reordered rows, keep checking for proper cursor
20169 position even if one has been found already, because buffer
20170 positions in such rows change non-linearly with ROW->VPOS,
20171 when a line is continued. One exception: when we are at ZV,
20172 display cursor on the first suitable glyph row, since all
20173 the empty rows after that also have their position set to ZV. */
20174 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20175 lines' rows is implemented for bidi-reordered rows. */
20176 || (it->bidi_p
20177 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20178 && PT >= MATRIX_ROW_START_CHARPOS (row)
20179 && PT <= MATRIX_ROW_END_CHARPOS (row)
20180 && cursor_row_p (row))
20181 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20182
20183 /* Prepare for the next line. This line starts horizontally at (X
20184 HPOS) = (0 0). Vertical positions are incremented. As a
20185 convenience for the caller, IT->glyph_row is set to the next
20186 row to be used. */
20187 it->current_x = it->hpos = 0;
20188 it->current_y += row->height;
20189 SET_TEXT_POS (it->eol_pos, 0, 0);
20190 ++it->vpos;
20191 ++it->glyph_row;
20192 /* The next row should by default use the same value of the
20193 reversed_p flag as this one. set_iterator_to_next decides when
20194 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20195 the flag accordingly. */
20196 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20197 it->glyph_row->reversed_p = row->reversed_p;
20198 it->start = row->end;
20199 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20200
20201 #undef RECORD_MAX_MIN_POS
20202 }
20203
20204 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20205 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20206 doc: /* Return paragraph direction at point in BUFFER.
20207 Value is either `left-to-right' or `right-to-left'.
20208 If BUFFER is omitted or nil, it defaults to the current buffer.
20209
20210 Paragraph direction determines how the text in the paragraph is displayed.
20211 In left-to-right paragraphs, text begins at the left margin of the window
20212 and the reading direction is generally left to right. In right-to-left
20213 paragraphs, text begins at the right margin and is read from right to left.
20214
20215 See also `bidi-paragraph-direction'. */)
20216 (Lisp_Object buffer)
20217 {
20218 struct buffer *buf = current_buffer;
20219 struct buffer *old = buf;
20220
20221 if (! NILP (buffer))
20222 {
20223 CHECK_BUFFER (buffer);
20224 buf = XBUFFER (buffer);
20225 }
20226
20227 if (NILP (BVAR (buf, bidi_display_reordering))
20228 || NILP (BVAR (buf, enable_multibyte_characters))
20229 /* When we are loading loadup.el, the character property tables
20230 needed for bidi iteration are not yet available. */
20231 || !NILP (Vpurify_flag))
20232 return Qleft_to_right;
20233 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20234 return BVAR (buf, bidi_paragraph_direction);
20235 else
20236 {
20237 /* Determine the direction from buffer text. We could try to
20238 use current_matrix if it is up to date, but this seems fast
20239 enough as it is. */
20240 struct bidi_it itb;
20241 ptrdiff_t pos = BUF_PT (buf);
20242 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20243 int c;
20244 void *itb_data = bidi_shelve_cache ();
20245
20246 set_buffer_temp (buf);
20247 /* bidi_paragraph_init finds the base direction of the paragraph
20248 by searching forward from paragraph start. We need the base
20249 direction of the current or _previous_ paragraph, so we need
20250 to make sure we are within that paragraph. To that end, find
20251 the previous non-empty line. */
20252 if (pos >= ZV && pos > BEGV)
20253 DEC_BOTH (pos, bytepos);
20254 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20255 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20256 {
20257 while ((c = FETCH_BYTE (bytepos)) == '\n'
20258 || c == ' ' || c == '\t' || c == '\f')
20259 {
20260 if (bytepos <= BEGV_BYTE)
20261 break;
20262 bytepos--;
20263 pos--;
20264 }
20265 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20266 bytepos--;
20267 }
20268 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20269 itb.paragraph_dir = NEUTRAL_DIR;
20270 itb.string.s = NULL;
20271 itb.string.lstring = Qnil;
20272 itb.string.bufpos = 0;
20273 itb.string.unibyte = 0;
20274 /* We have no window to use here for ignoring window-specific
20275 overlays. Using NULL for window pointer will cause
20276 compute_display_string_pos to use the current buffer. */
20277 itb.w = NULL;
20278 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20279 bidi_unshelve_cache (itb_data, 0);
20280 set_buffer_temp (old);
20281 switch (itb.paragraph_dir)
20282 {
20283 case L2R:
20284 return Qleft_to_right;
20285 break;
20286 case R2L:
20287 return Qright_to_left;
20288 break;
20289 default:
20290 emacs_abort ();
20291 }
20292 }
20293 }
20294
20295 DEFUN ("move-point-visually", Fmove_point_visually,
20296 Smove_point_visually, 1, 1, 0,
20297 doc: /* Move point in the visual order in the specified DIRECTION.
20298 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20299 left.
20300
20301 Value is the new character position of point. */)
20302 (Lisp_Object direction)
20303 {
20304 struct window *w = XWINDOW (selected_window);
20305 struct buffer *b = XBUFFER (w->contents);
20306 struct glyph_row *row;
20307 int dir;
20308 Lisp_Object paragraph_dir;
20309
20310 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20311 (!(ROW)->continued_p \
20312 && INTEGERP ((GLYPH)->object) \
20313 && (GLYPH)->type == CHAR_GLYPH \
20314 && (GLYPH)->u.ch == ' ' \
20315 && (GLYPH)->charpos >= 0 \
20316 && !(GLYPH)->avoid_cursor_p)
20317
20318 CHECK_NUMBER (direction);
20319 dir = XINT (direction);
20320 if (dir > 0)
20321 dir = 1;
20322 else
20323 dir = -1;
20324
20325 /* If current matrix is up-to-date, we can use the information
20326 recorded in the glyphs, at least as long as the goal is on the
20327 screen. */
20328 if (w->window_end_valid
20329 && !windows_or_buffers_changed
20330 && b
20331 && !b->clip_changed
20332 && !b->prevent_redisplay_optimizations_p
20333 && !window_outdated (w)
20334 && w->cursor.vpos >= 0
20335 && w->cursor.vpos < w->current_matrix->nrows
20336 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20337 {
20338 struct glyph *g = row->glyphs[TEXT_AREA];
20339 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20340 struct glyph *gpt = g + w->cursor.hpos;
20341
20342 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20343 {
20344 if (BUFFERP (g->object) && g->charpos != PT)
20345 {
20346 SET_PT (g->charpos);
20347 w->cursor.vpos = -1;
20348 return make_number (PT);
20349 }
20350 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20351 {
20352 ptrdiff_t new_pos;
20353
20354 if (BUFFERP (gpt->object))
20355 {
20356 new_pos = PT;
20357 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20358 new_pos += (row->reversed_p ? -dir : dir);
20359 else
20360 new_pos -= (row->reversed_p ? -dir : dir);;
20361 }
20362 else if (BUFFERP (g->object))
20363 new_pos = g->charpos;
20364 else
20365 break;
20366 SET_PT (new_pos);
20367 w->cursor.vpos = -1;
20368 return make_number (PT);
20369 }
20370 else if (ROW_GLYPH_NEWLINE_P (row, g))
20371 {
20372 /* Glyphs inserted at the end of a non-empty line for
20373 positioning the cursor have zero charpos, so we must
20374 deduce the value of point by other means. */
20375 if (g->charpos > 0)
20376 SET_PT (g->charpos);
20377 else if (row->ends_at_zv_p && PT != ZV)
20378 SET_PT (ZV);
20379 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20380 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20381 else
20382 break;
20383 w->cursor.vpos = -1;
20384 return make_number (PT);
20385 }
20386 }
20387 if (g == e || INTEGERP (g->object))
20388 {
20389 if (row->truncated_on_left_p || row->truncated_on_right_p)
20390 goto simulate_display;
20391 if (!row->reversed_p)
20392 row += dir;
20393 else
20394 row -= dir;
20395 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20396 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20397 goto simulate_display;
20398
20399 if (dir > 0)
20400 {
20401 if (row->reversed_p && !row->continued_p)
20402 {
20403 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20404 w->cursor.vpos = -1;
20405 return make_number (PT);
20406 }
20407 g = row->glyphs[TEXT_AREA];
20408 e = g + row->used[TEXT_AREA];
20409 for ( ; g < e; g++)
20410 {
20411 if (BUFFERP (g->object)
20412 /* Empty lines have only one glyph, which stands
20413 for the newline, and whose charpos is the
20414 buffer position of the newline. */
20415 || ROW_GLYPH_NEWLINE_P (row, g)
20416 /* When the buffer ends in a newline, the line at
20417 EOB also has one glyph, but its charpos is -1. */
20418 || (row->ends_at_zv_p
20419 && !row->reversed_p
20420 && INTEGERP (g->object)
20421 && g->type == CHAR_GLYPH
20422 && g->u.ch == ' '))
20423 {
20424 if (g->charpos > 0)
20425 SET_PT (g->charpos);
20426 else if (!row->reversed_p
20427 && row->ends_at_zv_p
20428 && PT != ZV)
20429 SET_PT (ZV);
20430 else
20431 continue;
20432 w->cursor.vpos = -1;
20433 return make_number (PT);
20434 }
20435 }
20436 }
20437 else
20438 {
20439 if (!row->reversed_p && !row->continued_p)
20440 {
20441 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20442 w->cursor.vpos = -1;
20443 return make_number (PT);
20444 }
20445 e = row->glyphs[TEXT_AREA];
20446 g = e + row->used[TEXT_AREA] - 1;
20447 for ( ; g >= e; g--)
20448 {
20449 if (BUFFERP (g->object)
20450 || (ROW_GLYPH_NEWLINE_P (row, g)
20451 && g->charpos > 0)
20452 /* Empty R2L lines on GUI frames have the buffer
20453 position of the newline stored in the stretch
20454 glyph. */
20455 || g->type == STRETCH_GLYPH
20456 || (row->ends_at_zv_p
20457 && row->reversed_p
20458 && INTEGERP (g->object)
20459 && g->type == CHAR_GLYPH
20460 && g->u.ch == ' '))
20461 {
20462 if (g->charpos > 0)
20463 SET_PT (g->charpos);
20464 else if (row->reversed_p
20465 && row->ends_at_zv_p
20466 && PT != ZV)
20467 SET_PT (ZV);
20468 else
20469 continue;
20470 w->cursor.vpos = -1;
20471 return make_number (PT);
20472 }
20473 }
20474 }
20475 }
20476 }
20477
20478 simulate_display:
20479
20480 /* If we wind up here, we failed to move by using the glyphs, so we
20481 need to simulate display instead. */
20482
20483 if (b)
20484 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20485 else
20486 paragraph_dir = Qleft_to_right;
20487 if (EQ (paragraph_dir, Qright_to_left))
20488 dir = -dir;
20489 if (PT <= BEGV && dir < 0)
20490 xsignal0 (Qbeginning_of_buffer);
20491 else if (PT >= ZV && dir > 0)
20492 xsignal0 (Qend_of_buffer);
20493 else
20494 {
20495 struct text_pos pt;
20496 struct it it;
20497 int pt_x, target_x, pixel_width, pt_vpos;
20498 bool at_eol_p;
20499 bool overshoot_expected = false;
20500 bool target_is_eol_p = false;
20501
20502 /* Setup the arena. */
20503 SET_TEXT_POS (pt, PT, PT_BYTE);
20504 start_display (&it, w, pt);
20505
20506 if (it.cmp_it.id < 0
20507 && it.method == GET_FROM_STRING
20508 && it.area == TEXT_AREA
20509 && it.string_from_display_prop_p
20510 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20511 overshoot_expected = true;
20512
20513 /* Find the X coordinate of point. We start from the beginning
20514 of this or previous line to make sure we are before point in
20515 the logical order (since the move_it_* functions can only
20516 move forward). */
20517 reseat_at_previous_visible_line_start (&it);
20518 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20519 if (IT_CHARPOS (it) != PT)
20520 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20521 -1, -1, -1, MOVE_TO_POS);
20522 pt_x = it.current_x;
20523 pt_vpos = it.vpos;
20524 if (dir > 0 || overshoot_expected)
20525 {
20526 struct glyph_row *row = it.glyph_row;
20527
20528 /* When point is at beginning of line, we don't have
20529 information about the glyph there loaded into struct
20530 it. Calling get_next_display_element fixes that. */
20531 if (pt_x == 0)
20532 get_next_display_element (&it);
20533 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20534 it.glyph_row = NULL;
20535 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20536 it.glyph_row = row;
20537 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20538 it, lest it will become out of sync with it's buffer
20539 position. */
20540 it.current_x = pt_x;
20541 }
20542 else
20543 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20544 pixel_width = it.pixel_width;
20545 if (overshoot_expected && at_eol_p)
20546 pixel_width = 0;
20547 else if (pixel_width <= 0)
20548 pixel_width = 1;
20549
20550 /* If there's a display string at point, we are actually at the
20551 glyph to the left of point, so we need to correct the X
20552 coordinate. */
20553 if (overshoot_expected)
20554 pt_x += pixel_width;
20555
20556 /* Compute target X coordinate, either to the left or to the
20557 right of point. On TTY frames, all characters have the same
20558 pixel width of 1, so we can use that. On GUI frames we don't
20559 have an easy way of getting at the pixel width of the
20560 character to the left of point, so we use a different method
20561 of getting to that place. */
20562 if (dir > 0)
20563 target_x = pt_x + pixel_width;
20564 else
20565 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20566
20567 /* Target X coordinate could be one line above or below the line
20568 of point, in which case we need to adjust the target X
20569 coordinate. Also, if moving to the left, we need to begin at
20570 the left edge of the point's screen line. */
20571 if (dir < 0)
20572 {
20573 if (pt_x > 0)
20574 {
20575 start_display (&it, w, pt);
20576 reseat_at_previous_visible_line_start (&it);
20577 it.current_x = it.current_y = it.hpos = 0;
20578 if (pt_vpos != 0)
20579 move_it_by_lines (&it, pt_vpos);
20580 }
20581 else
20582 {
20583 move_it_by_lines (&it, -1);
20584 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20585 target_is_eol_p = true;
20586 }
20587 }
20588 else
20589 {
20590 if (at_eol_p
20591 || (target_x >= it.last_visible_x
20592 && it.line_wrap != TRUNCATE))
20593 {
20594 if (pt_x > 0)
20595 move_it_by_lines (&it, 0);
20596 move_it_by_lines (&it, 1);
20597 target_x = 0;
20598 }
20599 }
20600
20601 /* Move to the target X coordinate. */
20602 #ifdef HAVE_WINDOW_SYSTEM
20603 /* On GUI frames, as we don't know the X coordinate of the
20604 character to the left of point, moving point to the left
20605 requires walking, one grapheme cluster at a time, until we
20606 find ourself at a place immediately to the left of the
20607 character at point. */
20608 if (FRAME_WINDOW_P (it.f) && dir < 0)
20609 {
20610 struct text_pos new_pos = it.current.pos;
20611 enum move_it_result rc = MOVE_X_REACHED;
20612
20613 while (it.current_x + it.pixel_width <= target_x
20614 && rc == MOVE_X_REACHED)
20615 {
20616 int new_x = it.current_x + it.pixel_width;
20617
20618 new_pos = it.current.pos;
20619 if (new_x == it.current_x)
20620 new_x++;
20621 rc = move_it_in_display_line_to (&it, ZV, new_x,
20622 MOVE_TO_POS | MOVE_TO_X);
20623 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20624 break;
20625 }
20626 /* If we ended up on a composed character inside
20627 bidi-reordered text (e.g., Hebrew text with diacritics),
20628 the iterator gives us the buffer position of the last (in
20629 logical order) character of the composed grapheme cluster,
20630 which is not what we want. So we cheat: we compute the
20631 character position of the character that follows (in the
20632 logical order) the one where the above loop stopped. That
20633 character will appear on display to the left of point. */
20634 if (it.bidi_p
20635 && it.bidi_it.scan_dir == -1
20636 && new_pos.charpos - IT_CHARPOS (it) > 1)
20637 {
20638 new_pos.charpos = IT_CHARPOS (it) + 1;
20639 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20640 }
20641 it.current.pos = new_pos;
20642 }
20643 else
20644 #endif
20645 if (it.current_x != target_x)
20646 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20647
20648 /* When lines are truncated, the above loop will stop at the
20649 window edge. But we want to get to the end of line, even if
20650 it is beyond the window edge; automatic hscroll will then
20651 scroll the window to show point as appropriate. */
20652 if (target_is_eol_p && it.line_wrap == TRUNCATE
20653 && get_next_display_element (&it))
20654 {
20655 struct text_pos new_pos = it.current.pos;
20656
20657 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20658 {
20659 set_iterator_to_next (&it, 0);
20660 if (it.method == GET_FROM_BUFFER)
20661 new_pos = it.current.pos;
20662 if (!get_next_display_element (&it))
20663 break;
20664 }
20665
20666 it.current.pos = new_pos;
20667 }
20668
20669 /* If we ended up in a display string that covers point, move to
20670 buffer position to the right in the visual order. */
20671 if (dir > 0)
20672 {
20673 while (IT_CHARPOS (it) == PT)
20674 {
20675 set_iterator_to_next (&it, 0);
20676 if (!get_next_display_element (&it))
20677 break;
20678 }
20679 }
20680
20681 /* Move point to that position. */
20682 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20683 }
20684
20685 return make_number (PT);
20686
20687 #undef ROW_GLYPH_NEWLINE_P
20688 }
20689
20690 \f
20691 /***********************************************************************
20692 Menu Bar
20693 ***********************************************************************/
20694
20695 /* Redisplay the menu bar in the frame for window W.
20696
20697 The menu bar of X frames that don't have X toolkit support is
20698 displayed in a special window W->frame->menu_bar_window.
20699
20700 The menu bar of terminal frames is treated specially as far as
20701 glyph matrices are concerned. Menu bar lines are not part of
20702 windows, so the update is done directly on the frame matrix rows
20703 for the menu bar. */
20704
20705 static void
20706 display_menu_bar (struct window *w)
20707 {
20708 struct frame *f = XFRAME (WINDOW_FRAME (w));
20709 struct it it;
20710 Lisp_Object items;
20711 int i;
20712
20713 /* Don't do all this for graphical frames. */
20714 #ifdef HAVE_NTGUI
20715 if (FRAME_W32_P (f))
20716 return;
20717 #endif
20718 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20719 if (FRAME_X_P (f))
20720 return;
20721 #endif
20722
20723 #ifdef HAVE_NS
20724 if (FRAME_NS_P (f))
20725 return;
20726 #endif /* HAVE_NS */
20727
20728 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20729 eassert (!FRAME_WINDOW_P (f));
20730 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20731 it.first_visible_x = 0;
20732 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20733 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20734 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20735 if (FRAME_WINDOW_P (f))
20736 {
20737 /* Menu bar lines are displayed in the desired matrix of the
20738 dummy window menu_bar_window. */
20739 struct window *menu_w;
20740 menu_w = XWINDOW (f->menu_bar_window);
20741 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20742 MENU_FACE_ID);
20743 it.first_visible_x = 0;
20744 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20745 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20746 }
20747 else
20748 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20749 {
20750 /* This is a TTY frame, i.e. character hpos/vpos are used as
20751 pixel x/y. */
20752 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20753 MENU_FACE_ID);
20754 it.first_visible_x = 0;
20755 it.last_visible_x = FRAME_COLS (f);
20756 }
20757
20758 /* FIXME: This should be controlled by a user option. See the
20759 comments in redisplay_tool_bar and display_mode_line about
20760 this. */
20761 it.paragraph_embedding = L2R;
20762
20763 /* Clear all rows of the menu bar. */
20764 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20765 {
20766 struct glyph_row *row = it.glyph_row + i;
20767 clear_glyph_row (row);
20768 row->enabled_p = 1;
20769 row->full_width_p = 1;
20770 }
20771
20772 /* Display all items of the menu bar. */
20773 items = FRAME_MENU_BAR_ITEMS (it.f);
20774 for (i = 0; i < ASIZE (items); i += 4)
20775 {
20776 Lisp_Object string;
20777
20778 /* Stop at nil string. */
20779 string = AREF (items, i + 1);
20780 if (NILP (string))
20781 break;
20782
20783 /* Remember where item was displayed. */
20784 ASET (items, i + 3, make_number (it.hpos));
20785
20786 /* Display the item, pad with one space. */
20787 if (it.current_x < it.last_visible_x)
20788 display_string (NULL, string, Qnil, 0, 0, &it,
20789 SCHARS (string) + 1, 0, 0, -1);
20790 }
20791
20792 /* Fill out the line with spaces. */
20793 if (it.current_x < it.last_visible_x)
20794 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20795
20796 /* Compute the total height of the lines. */
20797 compute_line_metrics (&it);
20798 }
20799
20800 /* Deep copy of a glyph row, including the glyphs. */
20801 static void
20802 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20803 {
20804 struct glyph *pointers[1 + LAST_AREA];
20805 int to_used = to->used[TEXT_AREA];
20806
20807 /* Save glyph pointers of TO. */
20808 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20809
20810 /* Do a structure assignment. */
20811 *to = *from;
20812
20813 /* Restore original glyph pointers of TO. */
20814 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20815
20816 /* Copy the glyphs. */
20817 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20818 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20819
20820 /* If we filled only part of the TO row, fill the rest with
20821 space_glyph (which will display as empty space). */
20822 if (to_used > from->used[TEXT_AREA])
20823 fill_up_frame_row_with_spaces (to, to_used);
20824 }
20825
20826 /* Display one menu item on a TTY, by overwriting the glyphs in the
20827 frame F's desired glyph matrix with glyphs produced from the menu
20828 item text. Called from term.c to display TTY drop-down menus one
20829 item at a time.
20830
20831 ITEM_TEXT is the menu item text as a C string.
20832
20833 FACE_ID is the face ID to be used for this menu item. FACE_ID
20834 could specify one of 3 faces: a face for an enabled item, a face
20835 for a disabled item, or a face for a selected item.
20836
20837 X and Y are coordinates of the first glyph in the frame's desired
20838 matrix to be overwritten by the menu item. Since this is a TTY, Y
20839 is the zero-based number of the glyph row and X is the zero-based
20840 glyph number in the row, starting from left, where to start
20841 displaying the item.
20842
20843 SUBMENU non-zero means this menu item drops down a submenu, which
20844 should be indicated by displaying a proper visual cue after the
20845 item text. */
20846
20847 void
20848 display_tty_menu_item (const char *item_text, int width, int face_id,
20849 int x, int y, int submenu)
20850 {
20851 struct it it;
20852 struct frame *f = SELECTED_FRAME ();
20853 struct window *w = XWINDOW (f->selected_window);
20854 int saved_used, saved_truncated, saved_width, saved_reversed;
20855 struct glyph_row *row;
20856 size_t item_len = strlen (item_text);
20857
20858 eassert (FRAME_TERMCAP_P (f));
20859
20860 /* Don't write beyond the matrix's last row. This can happen for
20861 TTY screens that are not high enough to show the entire menu.
20862 (This is actually a bit of defensive programming, as
20863 tty_menu_display already limits the number of menu items to one
20864 less than the number of screen lines.) */
20865 if (y >= f->desired_matrix->nrows)
20866 return;
20867
20868 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20869 it.first_visible_x = 0;
20870 it.last_visible_x = FRAME_COLS (f) - 1;
20871 row = it.glyph_row;
20872 /* Start with the row contents from the current matrix. */
20873 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20874 saved_width = row->full_width_p;
20875 row->full_width_p = 1;
20876 saved_reversed = row->reversed_p;
20877 row->reversed_p = 0;
20878 row->enabled_p = 1;
20879
20880 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20881 desired face. */
20882 eassert (x < f->desired_matrix->matrix_w);
20883 it.current_x = it.hpos = x;
20884 it.current_y = it.vpos = y;
20885 saved_used = row->used[TEXT_AREA];
20886 saved_truncated = row->truncated_on_right_p;
20887 row->used[TEXT_AREA] = x;
20888 it.face_id = face_id;
20889 it.line_wrap = TRUNCATE;
20890
20891 /* FIXME: This should be controlled by a user option. See the
20892 comments in redisplay_tool_bar and display_mode_line about this.
20893 Also, if paragraph_embedding could ever be R2L, changes will be
20894 needed to avoid shifting to the right the row characters in
20895 term.c:append_glyph. */
20896 it.paragraph_embedding = L2R;
20897
20898 /* Pad with a space on the left. */
20899 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20900 width--;
20901 /* Display the menu item, pad with spaces to WIDTH. */
20902 if (submenu)
20903 {
20904 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20905 item_len, 0, FRAME_COLS (f) - 1, -1);
20906 width -= item_len;
20907 /* Indicate with " >" that there's a submenu. */
20908 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20909 FRAME_COLS (f) - 1, -1);
20910 }
20911 else
20912 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20913 width, 0, FRAME_COLS (f) - 1, -1);
20914
20915 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20916 row->truncated_on_right_p = saved_truncated;
20917 row->hash = row_hash (row);
20918 row->full_width_p = saved_width;
20919 row->reversed_p = saved_reversed;
20920 }
20921 \f
20922 /***********************************************************************
20923 Mode Line
20924 ***********************************************************************/
20925
20926 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20927 FORCE is non-zero, redisplay mode lines unconditionally.
20928 Otherwise, redisplay only mode lines that are garbaged. Value is
20929 the number of windows whose mode lines were redisplayed. */
20930
20931 static int
20932 redisplay_mode_lines (Lisp_Object window, bool force)
20933 {
20934 int nwindows = 0;
20935
20936 while (!NILP (window))
20937 {
20938 struct window *w = XWINDOW (window);
20939
20940 if (WINDOWP (w->contents))
20941 nwindows += redisplay_mode_lines (w->contents, force);
20942 else if (force
20943 || FRAME_GARBAGED_P (XFRAME (w->frame))
20944 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20945 {
20946 struct text_pos lpoint;
20947 struct buffer *old = current_buffer;
20948
20949 /* Set the window's buffer for the mode line display. */
20950 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20951 set_buffer_internal_1 (XBUFFER (w->contents));
20952
20953 /* Point refers normally to the selected window. For any
20954 other window, set up appropriate value. */
20955 if (!EQ (window, selected_window))
20956 {
20957 struct text_pos pt;
20958
20959 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20960 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20961 }
20962
20963 /* Display mode lines. */
20964 clear_glyph_matrix (w->desired_matrix);
20965 if (display_mode_lines (w))
20966 ++nwindows;
20967
20968 /* Restore old settings. */
20969 set_buffer_internal_1 (old);
20970 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20971 }
20972
20973 window = w->next;
20974 }
20975
20976 return nwindows;
20977 }
20978
20979
20980 /* Display the mode and/or header line of window W. Value is the
20981 sum number of mode lines and header lines displayed. */
20982
20983 static int
20984 display_mode_lines (struct window *w)
20985 {
20986 Lisp_Object old_selected_window = selected_window;
20987 Lisp_Object old_selected_frame = selected_frame;
20988 Lisp_Object new_frame = w->frame;
20989 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20990 int n = 0;
20991
20992 selected_frame = new_frame;
20993 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20994 or window's point, then we'd need select_window_1 here as well. */
20995 XSETWINDOW (selected_window, w);
20996 XFRAME (new_frame)->selected_window = selected_window;
20997
20998 /* These will be set while the mode line specs are processed. */
20999 line_number_displayed = 0;
21000 w->column_number_displayed = -1;
21001
21002 if (WINDOW_WANTS_MODELINE_P (w))
21003 {
21004 struct window *sel_w = XWINDOW (old_selected_window);
21005
21006 /* Select mode line face based on the real selected window. */
21007 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21008 BVAR (current_buffer, mode_line_format));
21009 ++n;
21010 }
21011
21012 if (WINDOW_WANTS_HEADER_LINE_P (w))
21013 {
21014 display_mode_line (w, HEADER_LINE_FACE_ID,
21015 BVAR (current_buffer, header_line_format));
21016 ++n;
21017 }
21018
21019 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21020 selected_frame = old_selected_frame;
21021 selected_window = old_selected_window;
21022 if (n > 0)
21023 w->must_be_updated_p = true;
21024 return n;
21025 }
21026
21027
21028 /* Display mode or header line of window W. FACE_ID specifies which
21029 line to display; it is either MODE_LINE_FACE_ID or
21030 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21031 display. Value is the pixel height of the mode/header line
21032 displayed. */
21033
21034 static int
21035 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21036 {
21037 struct it it;
21038 struct face *face;
21039 ptrdiff_t count = SPECPDL_INDEX ();
21040
21041 init_iterator (&it, w, -1, -1, NULL, face_id);
21042 /* Don't extend on a previously drawn mode-line.
21043 This may happen if called from pos_visible_p. */
21044 it.glyph_row->enabled_p = 0;
21045 prepare_desired_row (it.glyph_row);
21046
21047 it.glyph_row->mode_line_p = 1;
21048
21049 /* FIXME: This should be controlled by a user option. But
21050 supporting such an option is not trivial, since the mode line is
21051 made up of many separate strings. */
21052 it.paragraph_embedding = L2R;
21053
21054 record_unwind_protect (unwind_format_mode_line,
21055 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21056
21057 mode_line_target = MODE_LINE_DISPLAY;
21058
21059 /* Temporarily make frame's keyboard the current kboard so that
21060 kboard-local variables in the mode_line_format will get the right
21061 values. */
21062 push_kboard (FRAME_KBOARD (it.f));
21063 record_unwind_save_match_data ();
21064 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21065 pop_kboard ();
21066
21067 unbind_to (count, Qnil);
21068
21069 /* Fill up with spaces. */
21070 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21071
21072 compute_line_metrics (&it);
21073 it.glyph_row->full_width_p = 1;
21074 it.glyph_row->continued_p = 0;
21075 it.glyph_row->truncated_on_left_p = 0;
21076 it.glyph_row->truncated_on_right_p = 0;
21077
21078 /* Make a 3D mode-line have a shadow at its right end. */
21079 face = FACE_FROM_ID (it.f, face_id);
21080 extend_face_to_end_of_line (&it);
21081 if (face->box != FACE_NO_BOX)
21082 {
21083 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21084 + it.glyph_row->used[TEXT_AREA] - 1);
21085 last->right_box_line_p = 1;
21086 }
21087
21088 return it.glyph_row->height;
21089 }
21090
21091 /* Move element ELT in LIST to the front of LIST.
21092 Return the updated list. */
21093
21094 static Lisp_Object
21095 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21096 {
21097 register Lisp_Object tail, prev;
21098 register Lisp_Object tem;
21099
21100 tail = list;
21101 prev = Qnil;
21102 while (CONSP (tail))
21103 {
21104 tem = XCAR (tail);
21105
21106 if (EQ (elt, tem))
21107 {
21108 /* Splice out the link TAIL. */
21109 if (NILP (prev))
21110 list = XCDR (tail);
21111 else
21112 Fsetcdr (prev, XCDR (tail));
21113
21114 /* Now make it the first. */
21115 Fsetcdr (tail, list);
21116 return tail;
21117 }
21118 else
21119 prev = tail;
21120 tail = XCDR (tail);
21121 QUIT;
21122 }
21123
21124 /* Not found--return unchanged LIST. */
21125 return list;
21126 }
21127
21128 /* Contribute ELT to the mode line for window IT->w. How it
21129 translates into text depends on its data type.
21130
21131 IT describes the display environment in which we display, as usual.
21132
21133 DEPTH is the depth in recursion. It is used to prevent
21134 infinite recursion here.
21135
21136 FIELD_WIDTH is the number of characters the display of ELT should
21137 occupy in the mode line, and PRECISION is the maximum number of
21138 characters to display from ELT's representation. See
21139 display_string for details.
21140
21141 Returns the hpos of the end of the text generated by ELT.
21142
21143 PROPS is a property list to add to any string we encounter.
21144
21145 If RISKY is nonzero, remove (disregard) any properties in any string
21146 we encounter, and ignore :eval and :propertize.
21147
21148 The global variable `mode_line_target' determines whether the
21149 output is passed to `store_mode_line_noprop',
21150 `store_mode_line_string', or `display_string'. */
21151
21152 static int
21153 display_mode_element (struct it *it, int depth, int field_width, int precision,
21154 Lisp_Object elt, Lisp_Object props, int risky)
21155 {
21156 int n = 0, field, prec;
21157 int literal = 0;
21158
21159 tail_recurse:
21160 if (depth > 100)
21161 elt = build_string ("*too-deep*");
21162
21163 depth++;
21164
21165 switch (XTYPE (elt))
21166 {
21167 case Lisp_String:
21168 {
21169 /* A string: output it and check for %-constructs within it. */
21170 unsigned char c;
21171 ptrdiff_t offset = 0;
21172
21173 if (SCHARS (elt) > 0
21174 && (!NILP (props) || risky))
21175 {
21176 Lisp_Object oprops, aelt;
21177 oprops = Ftext_properties_at (make_number (0), elt);
21178
21179 /* If the starting string's properties are not what
21180 we want, translate the string. Also, if the string
21181 is risky, do that anyway. */
21182
21183 if (NILP (Fequal (props, oprops)) || risky)
21184 {
21185 /* If the starting string has properties,
21186 merge the specified ones onto the existing ones. */
21187 if (! NILP (oprops) && !risky)
21188 {
21189 Lisp_Object tem;
21190
21191 oprops = Fcopy_sequence (oprops);
21192 tem = props;
21193 while (CONSP (tem))
21194 {
21195 oprops = Fplist_put (oprops, XCAR (tem),
21196 XCAR (XCDR (tem)));
21197 tem = XCDR (XCDR (tem));
21198 }
21199 props = oprops;
21200 }
21201
21202 aelt = Fassoc (elt, mode_line_proptrans_alist);
21203 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21204 {
21205 /* AELT is what we want. Move it to the front
21206 without consing. */
21207 elt = XCAR (aelt);
21208 mode_line_proptrans_alist
21209 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21210 }
21211 else
21212 {
21213 Lisp_Object tem;
21214
21215 /* If AELT has the wrong props, it is useless.
21216 so get rid of it. */
21217 if (! NILP (aelt))
21218 mode_line_proptrans_alist
21219 = Fdelq (aelt, mode_line_proptrans_alist);
21220
21221 elt = Fcopy_sequence (elt);
21222 Fset_text_properties (make_number (0), Flength (elt),
21223 props, elt);
21224 /* Add this item to mode_line_proptrans_alist. */
21225 mode_line_proptrans_alist
21226 = Fcons (Fcons (elt, props),
21227 mode_line_proptrans_alist);
21228 /* Truncate mode_line_proptrans_alist
21229 to at most 50 elements. */
21230 tem = Fnthcdr (make_number (50),
21231 mode_line_proptrans_alist);
21232 if (! NILP (tem))
21233 XSETCDR (tem, Qnil);
21234 }
21235 }
21236 }
21237
21238 offset = 0;
21239
21240 if (literal)
21241 {
21242 prec = precision - n;
21243 switch (mode_line_target)
21244 {
21245 case MODE_LINE_NOPROP:
21246 case MODE_LINE_TITLE:
21247 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21248 break;
21249 case MODE_LINE_STRING:
21250 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21251 break;
21252 case MODE_LINE_DISPLAY:
21253 n += display_string (NULL, elt, Qnil, 0, 0, it,
21254 0, prec, 0, STRING_MULTIBYTE (elt));
21255 break;
21256 }
21257
21258 break;
21259 }
21260
21261 /* Handle the non-literal case. */
21262
21263 while ((precision <= 0 || n < precision)
21264 && SREF (elt, offset) != 0
21265 && (mode_line_target != MODE_LINE_DISPLAY
21266 || it->current_x < it->last_visible_x))
21267 {
21268 ptrdiff_t last_offset = offset;
21269
21270 /* Advance to end of string or next format specifier. */
21271 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21272 ;
21273
21274 if (offset - 1 != last_offset)
21275 {
21276 ptrdiff_t nchars, nbytes;
21277
21278 /* Output to end of string or up to '%'. Field width
21279 is length of string. Don't output more than
21280 PRECISION allows us. */
21281 offset--;
21282
21283 prec = c_string_width (SDATA (elt) + last_offset,
21284 offset - last_offset, precision - n,
21285 &nchars, &nbytes);
21286
21287 switch (mode_line_target)
21288 {
21289 case MODE_LINE_NOPROP:
21290 case MODE_LINE_TITLE:
21291 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21292 break;
21293 case MODE_LINE_STRING:
21294 {
21295 ptrdiff_t bytepos = last_offset;
21296 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21297 ptrdiff_t endpos = (precision <= 0
21298 ? string_byte_to_char (elt, offset)
21299 : charpos + nchars);
21300
21301 n += store_mode_line_string (NULL,
21302 Fsubstring (elt, make_number (charpos),
21303 make_number (endpos)),
21304 0, 0, 0, Qnil);
21305 }
21306 break;
21307 case MODE_LINE_DISPLAY:
21308 {
21309 ptrdiff_t bytepos = last_offset;
21310 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21311
21312 if (precision <= 0)
21313 nchars = string_byte_to_char (elt, offset) - charpos;
21314 n += display_string (NULL, elt, Qnil, 0, charpos,
21315 it, 0, nchars, 0,
21316 STRING_MULTIBYTE (elt));
21317 }
21318 break;
21319 }
21320 }
21321 else /* c == '%' */
21322 {
21323 ptrdiff_t percent_position = offset;
21324
21325 /* Get the specified minimum width. Zero means
21326 don't pad. */
21327 field = 0;
21328 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21329 field = field * 10 + c - '0';
21330
21331 /* Don't pad beyond the total padding allowed. */
21332 if (field_width - n > 0 && field > field_width - n)
21333 field = field_width - n;
21334
21335 /* Note that either PRECISION <= 0 or N < PRECISION. */
21336 prec = precision - n;
21337
21338 if (c == 'M')
21339 n += display_mode_element (it, depth, field, prec,
21340 Vglobal_mode_string, props,
21341 risky);
21342 else if (c != 0)
21343 {
21344 bool multibyte;
21345 ptrdiff_t bytepos, charpos;
21346 const char *spec;
21347 Lisp_Object string;
21348
21349 bytepos = percent_position;
21350 charpos = (STRING_MULTIBYTE (elt)
21351 ? string_byte_to_char (elt, bytepos)
21352 : bytepos);
21353 spec = decode_mode_spec (it->w, c, field, &string);
21354 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21355
21356 switch (mode_line_target)
21357 {
21358 case MODE_LINE_NOPROP:
21359 case MODE_LINE_TITLE:
21360 n += store_mode_line_noprop (spec, field, prec);
21361 break;
21362 case MODE_LINE_STRING:
21363 {
21364 Lisp_Object tem = build_string (spec);
21365 props = Ftext_properties_at (make_number (charpos), elt);
21366 /* Should only keep face property in props */
21367 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21368 }
21369 break;
21370 case MODE_LINE_DISPLAY:
21371 {
21372 int nglyphs_before, nwritten;
21373
21374 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21375 nwritten = display_string (spec, string, elt,
21376 charpos, 0, it,
21377 field, prec, 0,
21378 multibyte);
21379
21380 /* Assign to the glyphs written above the
21381 string where the `%x' came from, position
21382 of the `%'. */
21383 if (nwritten > 0)
21384 {
21385 struct glyph *glyph
21386 = (it->glyph_row->glyphs[TEXT_AREA]
21387 + nglyphs_before);
21388 int i;
21389
21390 for (i = 0; i < nwritten; ++i)
21391 {
21392 glyph[i].object = elt;
21393 glyph[i].charpos = charpos;
21394 }
21395
21396 n += nwritten;
21397 }
21398 }
21399 break;
21400 }
21401 }
21402 else /* c == 0 */
21403 break;
21404 }
21405 }
21406 }
21407 break;
21408
21409 case Lisp_Symbol:
21410 /* A symbol: process the value of the symbol recursively
21411 as if it appeared here directly. Avoid error if symbol void.
21412 Special case: if value of symbol is a string, output the string
21413 literally. */
21414 {
21415 register Lisp_Object tem;
21416
21417 /* If the variable is not marked as risky to set
21418 then its contents are risky to use. */
21419 if (NILP (Fget (elt, Qrisky_local_variable)))
21420 risky = 1;
21421
21422 tem = Fboundp (elt);
21423 if (!NILP (tem))
21424 {
21425 tem = Fsymbol_value (elt);
21426 /* If value is a string, output that string literally:
21427 don't check for % within it. */
21428 if (STRINGP (tem))
21429 literal = 1;
21430
21431 if (!EQ (tem, elt))
21432 {
21433 /* Give up right away for nil or t. */
21434 elt = tem;
21435 goto tail_recurse;
21436 }
21437 }
21438 }
21439 break;
21440
21441 case Lisp_Cons:
21442 {
21443 register Lisp_Object car, tem;
21444
21445 /* A cons cell: five distinct cases.
21446 If first element is :eval or :propertize, do something special.
21447 If first element is a string or a cons, process all the elements
21448 and effectively concatenate them.
21449 If first element is a negative number, truncate displaying cdr to
21450 at most that many characters. If positive, pad (with spaces)
21451 to at least that many characters.
21452 If first element is a symbol, process the cadr or caddr recursively
21453 according to whether the symbol's value is non-nil or nil. */
21454 car = XCAR (elt);
21455 if (EQ (car, QCeval))
21456 {
21457 /* An element of the form (:eval FORM) means evaluate FORM
21458 and use the result as mode line elements. */
21459
21460 if (risky)
21461 break;
21462
21463 if (CONSP (XCDR (elt)))
21464 {
21465 Lisp_Object spec;
21466 spec = safe_eval (XCAR (XCDR (elt)));
21467 n += display_mode_element (it, depth, field_width - n,
21468 precision - n, spec, props,
21469 risky);
21470 }
21471 }
21472 else if (EQ (car, QCpropertize))
21473 {
21474 /* An element of the form (:propertize ELT PROPS...)
21475 means display ELT but applying properties PROPS. */
21476
21477 if (risky)
21478 break;
21479
21480 if (CONSP (XCDR (elt)))
21481 n += display_mode_element (it, depth, field_width - n,
21482 precision - n, XCAR (XCDR (elt)),
21483 XCDR (XCDR (elt)), risky);
21484 }
21485 else if (SYMBOLP (car))
21486 {
21487 tem = Fboundp (car);
21488 elt = XCDR (elt);
21489 if (!CONSP (elt))
21490 goto invalid;
21491 /* elt is now the cdr, and we know it is a cons cell.
21492 Use its car if CAR has a non-nil value. */
21493 if (!NILP (tem))
21494 {
21495 tem = Fsymbol_value (car);
21496 if (!NILP (tem))
21497 {
21498 elt = XCAR (elt);
21499 goto tail_recurse;
21500 }
21501 }
21502 /* Symbol's value is nil (or symbol is unbound)
21503 Get the cddr of the original list
21504 and if possible find the caddr and use that. */
21505 elt = XCDR (elt);
21506 if (NILP (elt))
21507 break;
21508 else if (!CONSP (elt))
21509 goto invalid;
21510 elt = XCAR (elt);
21511 goto tail_recurse;
21512 }
21513 else if (INTEGERP (car))
21514 {
21515 register int lim = XINT (car);
21516 elt = XCDR (elt);
21517 if (lim < 0)
21518 {
21519 /* Negative int means reduce maximum width. */
21520 if (precision <= 0)
21521 precision = -lim;
21522 else
21523 precision = min (precision, -lim);
21524 }
21525 else if (lim > 0)
21526 {
21527 /* Padding specified. Don't let it be more than
21528 current maximum. */
21529 if (precision > 0)
21530 lim = min (precision, lim);
21531
21532 /* If that's more padding than already wanted, queue it.
21533 But don't reduce padding already specified even if
21534 that is beyond the current truncation point. */
21535 field_width = max (lim, field_width);
21536 }
21537 goto tail_recurse;
21538 }
21539 else if (STRINGP (car) || CONSP (car))
21540 {
21541 Lisp_Object halftail = elt;
21542 int len = 0;
21543
21544 while (CONSP (elt)
21545 && (precision <= 0 || n < precision))
21546 {
21547 n += display_mode_element (it, depth,
21548 /* Do padding only after the last
21549 element in the list. */
21550 (! CONSP (XCDR (elt))
21551 ? field_width - n
21552 : 0),
21553 precision - n, XCAR (elt),
21554 props, risky);
21555 elt = XCDR (elt);
21556 len++;
21557 if ((len & 1) == 0)
21558 halftail = XCDR (halftail);
21559 /* Check for cycle. */
21560 if (EQ (halftail, elt))
21561 break;
21562 }
21563 }
21564 }
21565 break;
21566
21567 default:
21568 invalid:
21569 elt = build_string ("*invalid*");
21570 goto tail_recurse;
21571 }
21572
21573 /* Pad to FIELD_WIDTH. */
21574 if (field_width > 0 && n < field_width)
21575 {
21576 switch (mode_line_target)
21577 {
21578 case MODE_LINE_NOPROP:
21579 case MODE_LINE_TITLE:
21580 n += store_mode_line_noprop ("", field_width - n, 0);
21581 break;
21582 case MODE_LINE_STRING:
21583 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21584 break;
21585 case MODE_LINE_DISPLAY:
21586 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21587 0, 0, 0);
21588 break;
21589 }
21590 }
21591
21592 return n;
21593 }
21594
21595 /* Store a mode-line string element in mode_line_string_list.
21596
21597 If STRING is non-null, display that C string. Otherwise, the Lisp
21598 string LISP_STRING is displayed.
21599
21600 FIELD_WIDTH is the minimum number of output glyphs to produce.
21601 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21602 with spaces. FIELD_WIDTH <= 0 means don't pad.
21603
21604 PRECISION is the maximum number of characters to output from
21605 STRING. PRECISION <= 0 means don't truncate the string.
21606
21607 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21608 properties to the string.
21609
21610 PROPS are the properties to add to the string.
21611 The mode_line_string_face face property is always added to the string.
21612 */
21613
21614 static int
21615 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21616 int field_width, int precision, Lisp_Object props)
21617 {
21618 ptrdiff_t len;
21619 int n = 0;
21620
21621 if (string != NULL)
21622 {
21623 len = strlen (string);
21624 if (precision > 0 && len > precision)
21625 len = precision;
21626 lisp_string = make_string (string, len);
21627 if (NILP (props))
21628 props = mode_line_string_face_prop;
21629 else if (!NILP (mode_line_string_face))
21630 {
21631 Lisp_Object face = Fplist_get (props, Qface);
21632 props = Fcopy_sequence (props);
21633 if (NILP (face))
21634 face = mode_line_string_face;
21635 else
21636 face = list2 (face, mode_line_string_face);
21637 props = Fplist_put (props, Qface, face);
21638 }
21639 Fadd_text_properties (make_number (0), make_number (len),
21640 props, lisp_string);
21641 }
21642 else
21643 {
21644 len = XFASTINT (Flength (lisp_string));
21645 if (precision > 0 && len > precision)
21646 {
21647 len = precision;
21648 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21649 precision = -1;
21650 }
21651 if (!NILP (mode_line_string_face))
21652 {
21653 Lisp_Object face;
21654 if (NILP (props))
21655 props = Ftext_properties_at (make_number (0), lisp_string);
21656 face = Fplist_get (props, Qface);
21657 if (NILP (face))
21658 face = mode_line_string_face;
21659 else
21660 face = list2 (face, mode_line_string_face);
21661 props = list2 (Qface, face);
21662 if (copy_string)
21663 lisp_string = Fcopy_sequence (lisp_string);
21664 }
21665 if (!NILP (props))
21666 Fadd_text_properties (make_number (0), make_number (len),
21667 props, lisp_string);
21668 }
21669
21670 if (len > 0)
21671 {
21672 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21673 n += len;
21674 }
21675
21676 if (field_width > len)
21677 {
21678 field_width -= len;
21679 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21680 if (!NILP (props))
21681 Fadd_text_properties (make_number (0), make_number (field_width),
21682 props, lisp_string);
21683 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21684 n += field_width;
21685 }
21686
21687 return n;
21688 }
21689
21690
21691 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21692 1, 4, 0,
21693 doc: /* Format a string out of a mode line format specification.
21694 First arg FORMAT specifies the mode line format (see `mode-line-format'
21695 for details) to use.
21696
21697 By default, the format is evaluated for the currently selected window.
21698
21699 Optional second arg FACE specifies the face property to put on all
21700 characters for which no face is specified. The value nil means the
21701 default face. The value t means whatever face the window's mode line
21702 currently uses (either `mode-line' or `mode-line-inactive',
21703 depending on whether the window is the selected window or not).
21704 An integer value means the value string has no text
21705 properties.
21706
21707 Optional third and fourth args WINDOW and BUFFER specify the window
21708 and buffer to use as the context for the formatting (defaults
21709 are the selected window and the WINDOW's buffer). */)
21710 (Lisp_Object format, Lisp_Object face,
21711 Lisp_Object window, Lisp_Object buffer)
21712 {
21713 struct it it;
21714 int len;
21715 struct window *w;
21716 struct buffer *old_buffer = NULL;
21717 int face_id;
21718 int no_props = INTEGERP (face);
21719 ptrdiff_t count = SPECPDL_INDEX ();
21720 Lisp_Object str;
21721 int string_start = 0;
21722
21723 w = decode_any_window (window);
21724 XSETWINDOW (window, w);
21725
21726 if (NILP (buffer))
21727 buffer = w->contents;
21728 CHECK_BUFFER (buffer);
21729
21730 /* Make formatting the modeline a non-op when noninteractive, otherwise
21731 there will be problems later caused by a partially initialized frame. */
21732 if (NILP (format) || noninteractive)
21733 return empty_unibyte_string;
21734
21735 if (no_props)
21736 face = Qnil;
21737
21738 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21739 : EQ (face, Qt) ? (EQ (window, selected_window)
21740 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21741 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21742 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21743 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21744 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21745 : DEFAULT_FACE_ID;
21746
21747 old_buffer = current_buffer;
21748
21749 /* Save things including mode_line_proptrans_alist,
21750 and set that to nil so that we don't alter the outer value. */
21751 record_unwind_protect (unwind_format_mode_line,
21752 format_mode_line_unwind_data
21753 (XFRAME (WINDOW_FRAME (w)),
21754 old_buffer, selected_window, 1));
21755 mode_line_proptrans_alist = Qnil;
21756
21757 Fselect_window (window, Qt);
21758 set_buffer_internal_1 (XBUFFER (buffer));
21759
21760 init_iterator (&it, w, -1, -1, NULL, face_id);
21761
21762 if (no_props)
21763 {
21764 mode_line_target = MODE_LINE_NOPROP;
21765 mode_line_string_face_prop = Qnil;
21766 mode_line_string_list = Qnil;
21767 string_start = MODE_LINE_NOPROP_LEN (0);
21768 }
21769 else
21770 {
21771 mode_line_target = MODE_LINE_STRING;
21772 mode_line_string_list = Qnil;
21773 mode_line_string_face = face;
21774 mode_line_string_face_prop
21775 = NILP (face) ? Qnil : list2 (Qface, face);
21776 }
21777
21778 push_kboard (FRAME_KBOARD (it.f));
21779 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21780 pop_kboard ();
21781
21782 if (no_props)
21783 {
21784 len = MODE_LINE_NOPROP_LEN (string_start);
21785 str = make_string (mode_line_noprop_buf + string_start, len);
21786 }
21787 else
21788 {
21789 mode_line_string_list = Fnreverse (mode_line_string_list);
21790 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21791 empty_unibyte_string);
21792 }
21793
21794 unbind_to (count, Qnil);
21795 return str;
21796 }
21797
21798 /* Write a null-terminated, right justified decimal representation of
21799 the positive integer D to BUF using a minimal field width WIDTH. */
21800
21801 static void
21802 pint2str (register char *buf, register int width, register ptrdiff_t d)
21803 {
21804 register char *p = buf;
21805
21806 if (d <= 0)
21807 *p++ = '0';
21808 else
21809 {
21810 while (d > 0)
21811 {
21812 *p++ = d % 10 + '0';
21813 d /= 10;
21814 }
21815 }
21816
21817 for (width -= (int) (p - buf); width > 0; --width)
21818 *p++ = ' ';
21819 *p-- = '\0';
21820 while (p > buf)
21821 {
21822 d = *buf;
21823 *buf++ = *p;
21824 *p-- = d;
21825 }
21826 }
21827
21828 /* Write a null-terminated, right justified decimal and "human
21829 readable" representation of the nonnegative integer D to BUF using
21830 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21831
21832 static const char power_letter[] =
21833 {
21834 0, /* no letter */
21835 'k', /* kilo */
21836 'M', /* mega */
21837 'G', /* giga */
21838 'T', /* tera */
21839 'P', /* peta */
21840 'E', /* exa */
21841 'Z', /* zetta */
21842 'Y' /* yotta */
21843 };
21844
21845 static void
21846 pint2hrstr (char *buf, int width, ptrdiff_t d)
21847 {
21848 /* We aim to represent the nonnegative integer D as
21849 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21850 ptrdiff_t quotient = d;
21851 int remainder = 0;
21852 /* -1 means: do not use TENTHS. */
21853 int tenths = -1;
21854 int exponent = 0;
21855
21856 /* Length of QUOTIENT.TENTHS as a string. */
21857 int length;
21858
21859 char * psuffix;
21860 char * p;
21861
21862 if (quotient >= 1000)
21863 {
21864 /* Scale to the appropriate EXPONENT. */
21865 do
21866 {
21867 remainder = quotient % 1000;
21868 quotient /= 1000;
21869 exponent++;
21870 }
21871 while (quotient >= 1000);
21872
21873 /* Round to nearest and decide whether to use TENTHS or not. */
21874 if (quotient <= 9)
21875 {
21876 tenths = remainder / 100;
21877 if (remainder % 100 >= 50)
21878 {
21879 if (tenths < 9)
21880 tenths++;
21881 else
21882 {
21883 quotient++;
21884 if (quotient == 10)
21885 tenths = -1;
21886 else
21887 tenths = 0;
21888 }
21889 }
21890 }
21891 else
21892 if (remainder >= 500)
21893 {
21894 if (quotient < 999)
21895 quotient++;
21896 else
21897 {
21898 quotient = 1;
21899 exponent++;
21900 tenths = 0;
21901 }
21902 }
21903 }
21904
21905 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21906 if (tenths == -1 && quotient <= 99)
21907 if (quotient <= 9)
21908 length = 1;
21909 else
21910 length = 2;
21911 else
21912 length = 3;
21913 p = psuffix = buf + max (width, length);
21914
21915 /* Print EXPONENT. */
21916 *psuffix++ = power_letter[exponent];
21917 *psuffix = '\0';
21918
21919 /* Print TENTHS. */
21920 if (tenths >= 0)
21921 {
21922 *--p = '0' + tenths;
21923 *--p = '.';
21924 }
21925
21926 /* Print QUOTIENT. */
21927 do
21928 {
21929 int digit = quotient % 10;
21930 *--p = '0' + digit;
21931 }
21932 while ((quotient /= 10) != 0);
21933
21934 /* Print leading spaces. */
21935 while (buf < p)
21936 *--p = ' ';
21937 }
21938
21939 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21940 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21941 type of CODING_SYSTEM. Return updated pointer into BUF. */
21942
21943 static unsigned char invalid_eol_type[] = "(*invalid*)";
21944
21945 static char *
21946 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21947 {
21948 Lisp_Object val;
21949 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21950 const unsigned char *eol_str;
21951 int eol_str_len;
21952 /* The EOL conversion we are using. */
21953 Lisp_Object eoltype;
21954
21955 val = CODING_SYSTEM_SPEC (coding_system);
21956 eoltype = Qnil;
21957
21958 if (!VECTORP (val)) /* Not yet decided. */
21959 {
21960 *buf++ = multibyte ? '-' : ' ';
21961 if (eol_flag)
21962 eoltype = eol_mnemonic_undecided;
21963 /* Don't mention EOL conversion if it isn't decided. */
21964 }
21965 else
21966 {
21967 Lisp_Object attrs;
21968 Lisp_Object eolvalue;
21969
21970 attrs = AREF (val, 0);
21971 eolvalue = AREF (val, 2);
21972
21973 *buf++ = multibyte
21974 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21975 : ' ';
21976
21977 if (eol_flag)
21978 {
21979 /* The EOL conversion that is normal on this system. */
21980
21981 if (NILP (eolvalue)) /* Not yet decided. */
21982 eoltype = eol_mnemonic_undecided;
21983 else if (VECTORP (eolvalue)) /* Not yet decided. */
21984 eoltype = eol_mnemonic_undecided;
21985 else /* eolvalue is Qunix, Qdos, or Qmac. */
21986 eoltype = (EQ (eolvalue, Qunix)
21987 ? eol_mnemonic_unix
21988 : (EQ (eolvalue, Qdos) == 1
21989 ? eol_mnemonic_dos : eol_mnemonic_mac));
21990 }
21991 }
21992
21993 if (eol_flag)
21994 {
21995 /* Mention the EOL conversion if it is not the usual one. */
21996 if (STRINGP (eoltype))
21997 {
21998 eol_str = SDATA (eoltype);
21999 eol_str_len = SBYTES (eoltype);
22000 }
22001 else if (CHARACTERP (eoltype))
22002 {
22003 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22004 int c = XFASTINT (eoltype);
22005 eol_str_len = CHAR_STRING (c, tmp);
22006 eol_str = tmp;
22007 }
22008 else
22009 {
22010 eol_str = invalid_eol_type;
22011 eol_str_len = sizeof (invalid_eol_type) - 1;
22012 }
22013 memcpy (buf, eol_str, eol_str_len);
22014 buf += eol_str_len;
22015 }
22016
22017 return buf;
22018 }
22019
22020 /* Return a string for the output of a mode line %-spec for window W,
22021 generated by character C. FIELD_WIDTH > 0 means pad the string
22022 returned with spaces to that value. Return a Lisp string in
22023 *STRING if the resulting string is taken from that Lisp string.
22024
22025 Note we operate on the current buffer for most purposes. */
22026
22027 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22028
22029 static const char *
22030 decode_mode_spec (struct window *w, register int c, int field_width,
22031 Lisp_Object *string)
22032 {
22033 Lisp_Object obj;
22034 struct frame *f = XFRAME (WINDOW_FRAME (w));
22035 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22036 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22037 produce strings from numerical values, so limit preposterously
22038 large values of FIELD_WIDTH to avoid overrunning the buffer's
22039 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22040 bytes plus the terminating null. */
22041 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22042 struct buffer *b = current_buffer;
22043
22044 obj = Qnil;
22045 *string = Qnil;
22046
22047 switch (c)
22048 {
22049 case '*':
22050 if (!NILP (BVAR (b, read_only)))
22051 return "%";
22052 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22053 return "*";
22054 return "-";
22055
22056 case '+':
22057 /* This differs from %* only for a modified read-only buffer. */
22058 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22059 return "*";
22060 if (!NILP (BVAR (b, read_only)))
22061 return "%";
22062 return "-";
22063
22064 case '&':
22065 /* This differs from %* in ignoring read-only-ness. */
22066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22067 return "*";
22068 return "-";
22069
22070 case '%':
22071 return "%";
22072
22073 case '[':
22074 {
22075 int i;
22076 char *p;
22077
22078 if (command_loop_level > 5)
22079 return "[[[... ";
22080 p = decode_mode_spec_buf;
22081 for (i = 0; i < command_loop_level; i++)
22082 *p++ = '[';
22083 *p = 0;
22084 return decode_mode_spec_buf;
22085 }
22086
22087 case ']':
22088 {
22089 int i;
22090 char *p;
22091
22092 if (command_loop_level > 5)
22093 return " ...]]]";
22094 p = decode_mode_spec_buf;
22095 for (i = 0; i < command_loop_level; i++)
22096 *p++ = ']';
22097 *p = 0;
22098 return decode_mode_spec_buf;
22099 }
22100
22101 case '-':
22102 {
22103 register int i;
22104
22105 /* Let lots_of_dashes be a string of infinite length. */
22106 if (mode_line_target == MODE_LINE_NOPROP
22107 || mode_line_target == MODE_LINE_STRING)
22108 return "--";
22109 if (field_width <= 0
22110 || field_width > sizeof (lots_of_dashes))
22111 {
22112 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22113 decode_mode_spec_buf[i] = '-';
22114 decode_mode_spec_buf[i] = '\0';
22115 return decode_mode_spec_buf;
22116 }
22117 else
22118 return lots_of_dashes;
22119 }
22120
22121 case 'b':
22122 obj = BVAR (b, name);
22123 break;
22124
22125 case 'c':
22126 /* %c and %l are ignored in `frame-title-format'.
22127 (In redisplay_internal, the frame title is drawn _before_ the
22128 windows are updated, so the stuff which depends on actual
22129 window contents (such as %l) may fail to render properly, or
22130 even crash emacs.) */
22131 if (mode_line_target == MODE_LINE_TITLE)
22132 return "";
22133 else
22134 {
22135 ptrdiff_t col = current_column ();
22136 w->column_number_displayed = col;
22137 pint2str (decode_mode_spec_buf, width, col);
22138 return decode_mode_spec_buf;
22139 }
22140
22141 case 'e':
22142 #ifndef SYSTEM_MALLOC
22143 {
22144 if (NILP (Vmemory_full))
22145 return "";
22146 else
22147 return "!MEM FULL! ";
22148 }
22149 #else
22150 return "";
22151 #endif
22152
22153 case 'F':
22154 /* %F displays the frame name. */
22155 if (!NILP (f->title))
22156 return SSDATA (f->title);
22157 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22158 return SSDATA (f->name);
22159 return "Emacs";
22160
22161 case 'f':
22162 obj = BVAR (b, filename);
22163 break;
22164
22165 case 'i':
22166 {
22167 ptrdiff_t size = ZV - BEGV;
22168 pint2str (decode_mode_spec_buf, width, size);
22169 return decode_mode_spec_buf;
22170 }
22171
22172 case 'I':
22173 {
22174 ptrdiff_t size = ZV - BEGV;
22175 pint2hrstr (decode_mode_spec_buf, width, size);
22176 return decode_mode_spec_buf;
22177 }
22178
22179 case 'l':
22180 {
22181 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22182 ptrdiff_t topline, nlines, height;
22183 ptrdiff_t junk;
22184
22185 /* %c and %l are ignored in `frame-title-format'. */
22186 if (mode_line_target == MODE_LINE_TITLE)
22187 return "";
22188
22189 startpos = marker_position (w->start);
22190 startpos_byte = marker_byte_position (w->start);
22191 height = WINDOW_TOTAL_LINES (w);
22192
22193 /* If we decided that this buffer isn't suitable for line numbers,
22194 don't forget that too fast. */
22195 if (w->base_line_pos == -1)
22196 goto no_value;
22197
22198 /* If the buffer is very big, don't waste time. */
22199 if (INTEGERP (Vline_number_display_limit)
22200 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22201 {
22202 w->base_line_pos = 0;
22203 w->base_line_number = 0;
22204 goto no_value;
22205 }
22206
22207 if (w->base_line_number > 0
22208 && w->base_line_pos > 0
22209 && w->base_line_pos <= startpos)
22210 {
22211 line = w->base_line_number;
22212 linepos = w->base_line_pos;
22213 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22214 }
22215 else
22216 {
22217 line = 1;
22218 linepos = BUF_BEGV (b);
22219 linepos_byte = BUF_BEGV_BYTE (b);
22220 }
22221
22222 /* Count lines from base line to window start position. */
22223 nlines = display_count_lines (linepos_byte,
22224 startpos_byte,
22225 startpos, &junk);
22226
22227 topline = nlines + line;
22228
22229 /* Determine a new base line, if the old one is too close
22230 or too far away, or if we did not have one.
22231 "Too close" means it's plausible a scroll-down would
22232 go back past it. */
22233 if (startpos == BUF_BEGV (b))
22234 {
22235 w->base_line_number = topline;
22236 w->base_line_pos = BUF_BEGV (b);
22237 }
22238 else if (nlines < height + 25 || nlines > height * 3 + 50
22239 || linepos == BUF_BEGV (b))
22240 {
22241 ptrdiff_t limit = BUF_BEGV (b);
22242 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22243 ptrdiff_t position;
22244 ptrdiff_t distance =
22245 (height * 2 + 30) * line_number_display_limit_width;
22246
22247 if (startpos - distance > limit)
22248 {
22249 limit = startpos - distance;
22250 limit_byte = CHAR_TO_BYTE (limit);
22251 }
22252
22253 nlines = display_count_lines (startpos_byte,
22254 limit_byte,
22255 - (height * 2 + 30),
22256 &position);
22257 /* If we couldn't find the lines we wanted within
22258 line_number_display_limit_width chars per line,
22259 give up on line numbers for this window. */
22260 if (position == limit_byte && limit == startpos - distance)
22261 {
22262 w->base_line_pos = -1;
22263 w->base_line_number = 0;
22264 goto no_value;
22265 }
22266
22267 w->base_line_number = topline - nlines;
22268 w->base_line_pos = BYTE_TO_CHAR (position);
22269 }
22270
22271 /* Now count lines from the start pos to point. */
22272 nlines = display_count_lines (startpos_byte,
22273 PT_BYTE, PT, &junk);
22274
22275 /* Record that we did display the line number. */
22276 line_number_displayed = 1;
22277
22278 /* Make the string to show. */
22279 pint2str (decode_mode_spec_buf, width, topline + nlines);
22280 return decode_mode_spec_buf;
22281 no_value:
22282 {
22283 char* p = decode_mode_spec_buf;
22284 int pad = width - 2;
22285 while (pad-- > 0)
22286 *p++ = ' ';
22287 *p++ = '?';
22288 *p++ = '?';
22289 *p = '\0';
22290 return decode_mode_spec_buf;
22291 }
22292 }
22293 break;
22294
22295 case 'm':
22296 obj = BVAR (b, mode_name);
22297 break;
22298
22299 case 'n':
22300 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22301 return " Narrow";
22302 break;
22303
22304 case 'p':
22305 {
22306 ptrdiff_t pos = marker_position (w->start);
22307 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22308
22309 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22310 {
22311 if (pos <= BUF_BEGV (b))
22312 return "All";
22313 else
22314 return "Bottom";
22315 }
22316 else if (pos <= BUF_BEGV (b))
22317 return "Top";
22318 else
22319 {
22320 if (total > 1000000)
22321 /* Do it differently for a large value, to avoid overflow. */
22322 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22323 else
22324 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22325 /* We can't normally display a 3-digit number,
22326 so get us a 2-digit number that is close. */
22327 if (total == 100)
22328 total = 99;
22329 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22330 return decode_mode_spec_buf;
22331 }
22332 }
22333
22334 /* Display percentage of size above the bottom of the screen. */
22335 case 'P':
22336 {
22337 ptrdiff_t toppos = marker_position (w->start);
22338 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22339 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22340
22341 if (botpos >= BUF_ZV (b))
22342 {
22343 if (toppos <= BUF_BEGV (b))
22344 return "All";
22345 else
22346 return "Bottom";
22347 }
22348 else
22349 {
22350 if (total > 1000000)
22351 /* Do it differently for a large value, to avoid overflow. */
22352 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22353 else
22354 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22355 /* We can't normally display a 3-digit number,
22356 so get us a 2-digit number that is close. */
22357 if (total == 100)
22358 total = 99;
22359 if (toppos <= BUF_BEGV (b))
22360 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22361 else
22362 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22363 return decode_mode_spec_buf;
22364 }
22365 }
22366
22367 case 's':
22368 /* status of process */
22369 obj = Fget_buffer_process (Fcurrent_buffer ());
22370 if (NILP (obj))
22371 return "no process";
22372 #ifndef MSDOS
22373 obj = Fsymbol_name (Fprocess_status (obj));
22374 #endif
22375 break;
22376
22377 case '@':
22378 {
22379 ptrdiff_t count = inhibit_garbage_collection ();
22380 Lisp_Object val = call1 (intern ("file-remote-p"),
22381 BVAR (current_buffer, directory));
22382 unbind_to (count, Qnil);
22383
22384 if (NILP (val))
22385 return "-";
22386 else
22387 return "@";
22388 }
22389
22390 case 'z':
22391 /* coding-system (not including end-of-line format) */
22392 case 'Z':
22393 /* coding-system (including end-of-line type) */
22394 {
22395 int eol_flag = (c == 'Z');
22396 char *p = decode_mode_spec_buf;
22397
22398 if (! FRAME_WINDOW_P (f))
22399 {
22400 /* No need to mention EOL here--the terminal never needs
22401 to do EOL conversion. */
22402 p = decode_mode_spec_coding (CODING_ID_NAME
22403 (FRAME_KEYBOARD_CODING (f)->id),
22404 p, 0);
22405 p = decode_mode_spec_coding (CODING_ID_NAME
22406 (FRAME_TERMINAL_CODING (f)->id),
22407 p, 0);
22408 }
22409 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22410 p, eol_flag);
22411
22412 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22413 #ifdef subprocesses
22414 obj = Fget_buffer_process (Fcurrent_buffer ());
22415 if (PROCESSP (obj))
22416 {
22417 p = decode_mode_spec_coding
22418 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22419 p = decode_mode_spec_coding
22420 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22421 }
22422 #endif /* subprocesses */
22423 #endif /* 0 */
22424 *p = 0;
22425 return decode_mode_spec_buf;
22426 }
22427 }
22428
22429 if (STRINGP (obj))
22430 {
22431 *string = obj;
22432 return SSDATA (obj);
22433 }
22434 else
22435 return "";
22436 }
22437
22438
22439 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22440 means count lines back from START_BYTE. But don't go beyond
22441 LIMIT_BYTE. Return the number of lines thus found (always
22442 nonnegative).
22443
22444 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22445 either the position COUNT lines after/before START_BYTE, if we
22446 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22447 COUNT lines. */
22448
22449 static ptrdiff_t
22450 display_count_lines (ptrdiff_t start_byte,
22451 ptrdiff_t limit_byte, ptrdiff_t count,
22452 ptrdiff_t *byte_pos_ptr)
22453 {
22454 register unsigned char *cursor;
22455 unsigned char *base;
22456
22457 register ptrdiff_t ceiling;
22458 register unsigned char *ceiling_addr;
22459 ptrdiff_t orig_count = count;
22460
22461 /* If we are not in selective display mode,
22462 check only for newlines. */
22463 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22464 && !INTEGERP (BVAR (current_buffer, selective_display)));
22465
22466 if (count > 0)
22467 {
22468 while (start_byte < limit_byte)
22469 {
22470 ceiling = BUFFER_CEILING_OF (start_byte);
22471 ceiling = min (limit_byte - 1, ceiling);
22472 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22473 base = (cursor = BYTE_POS_ADDR (start_byte));
22474
22475 do
22476 {
22477 if (selective_display)
22478 {
22479 while (*cursor != '\n' && *cursor != 015
22480 && ++cursor != ceiling_addr)
22481 continue;
22482 if (cursor == ceiling_addr)
22483 break;
22484 }
22485 else
22486 {
22487 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22488 if (! cursor)
22489 break;
22490 }
22491
22492 cursor++;
22493
22494 if (--count == 0)
22495 {
22496 start_byte += cursor - base;
22497 *byte_pos_ptr = start_byte;
22498 return orig_count;
22499 }
22500 }
22501 while (cursor < ceiling_addr);
22502
22503 start_byte += ceiling_addr - base;
22504 }
22505 }
22506 else
22507 {
22508 while (start_byte > limit_byte)
22509 {
22510 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22511 ceiling = max (limit_byte, ceiling);
22512 ceiling_addr = BYTE_POS_ADDR (ceiling);
22513 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22514 while (1)
22515 {
22516 if (selective_display)
22517 {
22518 while (--cursor >= ceiling_addr
22519 && *cursor != '\n' && *cursor != 015)
22520 continue;
22521 if (cursor < ceiling_addr)
22522 break;
22523 }
22524 else
22525 {
22526 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22527 if (! cursor)
22528 break;
22529 }
22530
22531 if (++count == 0)
22532 {
22533 start_byte += cursor - base + 1;
22534 *byte_pos_ptr = start_byte;
22535 /* When scanning backwards, we should
22536 not count the newline posterior to which we stop. */
22537 return - orig_count - 1;
22538 }
22539 }
22540 start_byte += ceiling_addr - base;
22541 }
22542 }
22543
22544 *byte_pos_ptr = limit_byte;
22545
22546 if (count < 0)
22547 return - orig_count + count;
22548 return orig_count - count;
22549
22550 }
22551
22552
22553 \f
22554 /***********************************************************************
22555 Displaying strings
22556 ***********************************************************************/
22557
22558 /* Display a NUL-terminated string, starting with index START.
22559
22560 If STRING is non-null, display that C string. Otherwise, the Lisp
22561 string LISP_STRING is displayed. There's a case that STRING is
22562 non-null and LISP_STRING is not nil. It means STRING is a string
22563 data of LISP_STRING. In that case, we display LISP_STRING while
22564 ignoring its text properties.
22565
22566 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22567 FACE_STRING. Display STRING or LISP_STRING with the face at
22568 FACE_STRING_POS in FACE_STRING:
22569
22570 Display the string in the environment given by IT, but use the
22571 standard display table, temporarily.
22572
22573 FIELD_WIDTH is the minimum number of output glyphs to produce.
22574 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22575 with spaces. If STRING has more characters, more than FIELD_WIDTH
22576 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22577
22578 PRECISION is the maximum number of characters to output from
22579 STRING. PRECISION < 0 means don't truncate the string.
22580
22581 This is roughly equivalent to printf format specifiers:
22582
22583 FIELD_WIDTH PRECISION PRINTF
22584 ----------------------------------------
22585 -1 -1 %s
22586 -1 10 %.10s
22587 10 -1 %10s
22588 20 10 %20.10s
22589
22590 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22591 display them, and < 0 means obey the current buffer's value of
22592 enable_multibyte_characters.
22593
22594 Value is the number of columns displayed. */
22595
22596 static int
22597 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22598 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22599 int field_width, int precision, int max_x, int multibyte)
22600 {
22601 int hpos_at_start = it->hpos;
22602 int saved_face_id = it->face_id;
22603 struct glyph_row *row = it->glyph_row;
22604 ptrdiff_t it_charpos;
22605
22606 /* Initialize the iterator IT for iteration over STRING beginning
22607 with index START. */
22608 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22609 precision, field_width, multibyte);
22610 if (string && STRINGP (lisp_string))
22611 /* LISP_STRING is the one returned by decode_mode_spec. We should
22612 ignore its text properties. */
22613 it->stop_charpos = it->end_charpos;
22614
22615 /* If displaying STRING, set up the face of the iterator from
22616 FACE_STRING, if that's given. */
22617 if (STRINGP (face_string))
22618 {
22619 ptrdiff_t endptr;
22620 struct face *face;
22621
22622 it->face_id
22623 = face_at_string_position (it->w, face_string, face_string_pos,
22624 0, &endptr, it->base_face_id, 0);
22625 face = FACE_FROM_ID (it->f, it->face_id);
22626 it->face_box_p = face->box != FACE_NO_BOX;
22627 }
22628
22629 /* Set max_x to the maximum allowed X position. Don't let it go
22630 beyond the right edge of the window. */
22631 if (max_x <= 0)
22632 max_x = it->last_visible_x;
22633 else
22634 max_x = min (max_x, it->last_visible_x);
22635
22636 /* Skip over display elements that are not visible. because IT->w is
22637 hscrolled. */
22638 if (it->current_x < it->first_visible_x)
22639 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22640 MOVE_TO_POS | MOVE_TO_X);
22641
22642 row->ascent = it->max_ascent;
22643 row->height = it->max_ascent + it->max_descent;
22644 row->phys_ascent = it->max_phys_ascent;
22645 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22646 row->extra_line_spacing = it->max_extra_line_spacing;
22647
22648 if (STRINGP (it->string))
22649 it_charpos = IT_STRING_CHARPOS (*it);
22650 else
22651 it_charpos = IT_CHARPOS (*it);
22652
22653 /* This condition is for the case that we are called with current_x
22654 past last_visible_x. */
22655 while (it->current_x < max_x)
22656 {
22657 int x_before, x, n_glyphs_before, i, nglyphs;
22658
22659 /* Get the next display element. */
22660 if (!get_next_display_element (it))
22661 break;
22662
22663 /* Produce glyphs. */
22664 x_before = it->current_x;
22665 n_glyphs_before = row->used[TEXT_AREA];
22666 PRODUCE_GLYPHS (it);
22667
22668 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22669 i = 0;
22670 x = x_before;
22671 while (i < nglyphs)
22672 {
22673 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22674
22675 if (it->line_wrap != TRUNCATE
22676 && x + glyph->pixel_width > max_x)
22677 {
22678 /* End of continued line or max_x reached. */
22679 if (CHAR_GLYPH_PADDING_P (*glyph))
22680 {
22681 /* A wide character is unbreakable. */
22682 if (row->reversed_p)
22683 unproduce_glyphs (it, row->used[TEXT_AREA]
22684 - n_glyphs_before);
22685 row->used[TEXT_AREA] = n_glyphs_before;
22686 it->current_x = x_before;
22687 }
22688 else
22689 {
22690 if (row->reversed_p)
22691 unproduce_glyphs (it, row->used[TEXT_AREA]
22692 - (n_glyphs_before + i));
22693 row->used[TEXT_AREA] = n_glyphs_before + i;
22694 it->current_x = x;
22695 }
22696 break;
22697 }
22698 else if (x + glyph->pixel_width >= it->first_visible_x)
22699 {
22700 /* Glyph is at least partially visible. */
22701 ++it->hpos;
22702 if (x < it->first_visible_x)
22703 row->x = x - it->first_visible_x;
22704 }
22705 else
22706 {
22707 /* Glyph is off the left margin of the display area.
22708 Should not happen. */
22709 emacs_abort ();
22710 }
22711
22712 row->ascent = max (row->ascent, it->max_ascent);
22713 row->height = max (row->height, it->max_ascent + it->max_descent);
22714 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22715 row->phys_height = max (row->phys_height,
22716 it->max_phys_ascent + it->max_phys_descent);
22717 row->extra_line_spacing = max (row->extra_line_spacing,
22718 it->max_extra_line_spacing);
22719 x += glyph->pixel_width;
22720 ++i;
22721 }
22722
22723 /* Stop if max_x reached. */
22724 if (i < nglyphs)
22725 break;
22726
22727 /* Stop at line ends. */
22728 if (ITERATOR_AT_END_OF_LINE_P (it))
22729 {
22730 it->continuation_lines_width = 0;
22731 break;
22732 }
22733
22734 set_iterator_to_next (it, 1);
22735 if (STRINGP (it->string))
22736 it_charpos = IT_STRING_CHARPOS (*it);
22737 else
22738 it_charpos = IT_CHARPOS (*it);
22739
22740 /* Stop if truncating at the right edge. */
22741 if (it->line_wrap == TRUNCATE
22742 && it->current_x >= it->last_visible_x)
22743 {
22744 /* Add truncation mark, but don't do it if the line is
22745 truncated at a padding space. */
22746 if (it_charpos < it->string_nchars)
22747 {
22748 if (!FRAME_WINDOW_P (it->f))
22749 {
22750 int ii, n;
22751
22752 if (it->current_x > it->last_visible_x)
22753 {
22754 if (!row->reversed_p)
22755 {
22756 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22757 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22758 break;
22759 }
22760 else
22761 {
22762 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22763 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22764 break;
22765 unproduce_glyphs (it, ii + 1);
22766 ii = row->used[TEXT_AREA] - (ii + 1);
22767 }
22768 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22769 {
22770 row->used[TEXT_AREA] = ii;
22771 produce_special_glyphs (it, IT_TRUNCATION);
22772 }
22773 }
22774 produce_special_glyphs (it, IT_TRUNCATION);
22775 }
22776 row->truncated_on_right_p = 1;
22777 }
22778 break;
22779 }
22780 }
22781
22782 /* Maybe insert a truncation at the left. */
22783 if (it->first_visible_x
22784 && it_charpos > 0)
22785 {
22786 if (!FRAME_WINDOW_P (it->f)
22787 || (row->reversed_p
22788 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22789 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22790 insert_left_trunc_glyphs (it);
22791 row->truncated_on_left_p = 1;
22792 }
22793
22794 it->face_id = saved_face_id;
22795
22796 /* Value is number of columns displayed. */
22797 return it->hpos - hpos_at_start;
22798 }
22799
22800
22801 \f
22802 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22803 appears as an element of LIST or as the car of an element of LIST.
22804 If PROPVAL is a list, compare each element against LIST in that
22805 way, and return 1/2 if any element of PROPVAL is found in LIST.
22806 Otherwise return 0. This function cannot quit.
22807 The return value is 2 if the text is invisible but with an ellipsis
22808 and 1 if it's invisible and without an ellipsis. */
22809
22810 int
22811 invisible_p (register Lisp_Object propval, Lisp_Object list)
22812 {
22813 register Lisp_Object tail, proptail;
22814
22815 for (tail = list; CONSP (tail); tail = XCDR (tail))
22816 {
22817 register Lisp_Object tem;
22818 tem = XCAR (tail);
22819 if (EQ (propval, tem))
22820 return 1;
22821 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22822 return NILP (XCDR (tem)) ? 1 : 2;
22823 }
22824
22825 if (CONSP (propval))
22826 {
22827 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22828 {
22829 Lisp_Object propelt;
22830 propelt = XCAR (proptail);
22831 for (tail = list; CONSP (tail); tail = XCDR (tail))
22832 {
22833 register Lisp_Object tem;
22834 tem = XCAR (tail);
22835 if (EQ (propelt, tem))
22836 return 1;
22837 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22838 return NILP (XCDR (tem)) ? 1 : 2;
22839 }
22840 }
22841 }
22842
22843 return 0;
22844 }
22845
22846 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22847 doc: /* Non-nil if the property makes the text invisible.
22848 POS-OR-PROP can be a marker or number, in which case it is taken to be
22849 a position in the current buffer and the value of the `invisible' property
22850 is checked; or it can be some other value, which is then presumed to be the
22851 value of the `invisible' property of the text of interest.
22852 The non-nil value returned can be t for truly invisible text or something
22853 else if the text is replaced by an ellipsis. */)
22854 (Lisp_Object pos_or_prop)
22855 {
22856 Lisp_Object prop
22857 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22858 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22859 : pos_or_prop);
22860 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22861 return (invis == 0 ? Qnil
22862 : invis == 1 ? Qt
22863 : make_number (invis));
22864 }
22865
22866 /* Calculate a width or height in pixels from a specification using
22867 the following elements:
22868
22869 SPEC ::=
22870 NUM - a (fractional) multiple of the default font width/height
22871 (NUM) - specifies exactly NUM pixels
22872 UNIT - a fixed number of pixels, see below.
22873 ELEMENT - size of a display element in pixels, see below.
22874 (NUM . SPEC) - equals NUM * SPEC
22875 (+ SPEC SPEC ...) - add pixel values
22876 (- SPEC SPEC ...) - subtract pixel values
22877 (- SPEC) - negate pixel value
22878
22879 NUM ::=
22880 INT or FLOAT - a number constant
22881 SYMBOL - use symbol's (buffer local) variable binding.
22882
22883 UNIT ::=
22884 in - pixels per inch *)
22885 mm - pixels per 1/1000 meter *)
22886 cm - pixels per 1/100 meter *)
22887 width - width of current font in pixels.
22888 height - height of current font in pixels.
22889
22890 *) using the ratio(s) defined in display-pixels-per-inch.
22891
22892 ELEMENT ::=
22893
22894 left-fringe - left fringe width in pixels
22895 right-fringe - right fringe width in pixels
22896
22897 left-margin - left margin width in pixels
22898 right-margin - right margin width in pixels
22899
22900 scroll-bar - scroll-bar area width in pixels
22901
22902 Examples:
22903
22904 Pixels corresponding to 5 inches:
22905 (5 . in)
22906
22907 Total width of non-text areas on left side of window (if scroll-bar is on left):
22908 '(space :width (+ left-fringe left-margin scroll-bar))
22909
22910 Align to first text column (in header line):
22911 '(space :align-to 0)
22912
22913 Align to middle of text area minus half the width of variable `my-image'
22914 containing a loaded image:
22915 '(space :align-to (0.5 . (- text my-image)))
22916
22917 Width of left margin minus width of 1 character in the default font:
22918 '(space :width (- left-margin 1))
22919
22920 Width of left margin minus width of 2 characters in the current font:
22921 '(space :width (- left-margin (2 . width)))
22922
22923 Center 1 character over left-margin (in header line):
22924 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22925
22926 Different ways to express width of left fringe plus left margin minus one pixel:
22927 '(space :width (- (+ left-fringe left-margin) (1)))
22928 '(space :width (+ left-fringe left-margin (- (1))))
22929 '(space :width (+ left-fringe left-margin (-1)))
22930
22931 */
22932
22933 static int
22934 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22935 struct font *font, int width_p, int *align_to)
22936 {
22937 double pixels;
22938
22939 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22940 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22941
22942 if (NILP (prop))
22943 return OK_PIXELS (0);
22944
22945 eassert (FRAME_LIVE_P (it->f));
22946
22947 if (SYMBOLP (prop))
22948 {
22949 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22950 {
22951 char *unit = SSDATA (SYMBOL_NAME (prop));
22952
22953 if (unit[0] == 'i' && unit[1] == 'n')
22954 pixels = 1.0;
22955 else if (unit[0] == 'm' && unit[1] == 'm')
22956 pixels = 25.4;
22957 else if (unit[0] == 'c' && unit[1] == 'm')
22958 pixels = 2.54;
22959 else
22960 pixels = 0;
22961 if (pixels > 0)
22962 {
22963 double ppi = (width_p ? FRAME_RES_X (it->f)
22964 : FRAME_RES_Y (it->f));
22965
22966 if (ppi > 0)
22967 return OK_PIXELS (ppi / pixels);
22968 return 0;
22969 }
22970 }
22971
22972 #ifdef HAVE_WINDOW_SYSTEM
22973 if (EQ (prop, Qheight))
22974 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22975 if (EQ (prop, Qwidth))
22976 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22977 #else
22978 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22979 return OK_PIXELS (1);
22980 #endif
22981
22982 if (EQ (prop, Qtext))
22983 return OK_PIXELS (width_p
22984 ? window_box_width (it->w, TEXT_AREA)
22985 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22986
22987 if (align_to && *align_to < 0)
22988 {
22989 *res = 0;
22990 if (EQ (prop, Qleft))
22991 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22992 if (EQ (prop, Qright))
22993 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22994 if (EQ (prop, Qcenter))
22995 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22996 + window_box_width (it->w, TEXT_AREA) / 2);
22997 if (EQ (prop, Qleft_fringe))
22998 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22999 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23000 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23001 if (EQ (prop, Qright_fringe))
23002 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23003 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23004 : window_box_right_offset (it->w, TEXT_AREA));
23005 if (EQ (prop, Qleft_margin))
23006 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23007 if (EQ (prop, Qright_margin))
23008 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23009 if (EQ (prop, Qscroll_bar))
23010 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23011 ? 0
23012 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23013 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23014 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23015 : 0)));
23016 }
23017 else
23018 {
23019 if (EQ (prop, Qleft_fringe))
23020 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23021 if (EQ (prop, Qright_fringe))
23022 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23023 if (EQ (prop, Qleft_margin))
23024 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23025 if (EQ (prop, Qright_margin))
23026 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23027 if (EQ (prop, Qscroll_bar))
23028 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23029 }
23030
23031 prop = buffer_local_value_1 (prop, it->w->contents);
23032 if (EQ (prop, Qunbound))
23033 prop = Qnil;
23034 }
23035
23036 if (INTEGERP (prop) || FLOATP (prop))
23037 {
23038 int base_unit = (width_p
23039 ? FRAME_COLUMN_WIDTH (it->f)
23040 : FRAME_LINE_HEIGHT (it->f));
23041 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23042 }
23043
23044 if (CONSP (prop))
23045 {
23046 Lisp_Object car = XCAR (prop);
23047 Lisp_Object cdr = XCDR (prop);
23048
23049 if (SYMBOLP (car))
23050 {
23051 #ifdef HAVE_WINDOW_SYSTEM
23052 if (FRAME_WINDOW_P (it->f)
23053 && valid_image_p (prop))
23054 {
23055 ptrdiff_t id = lookup_image (it->f, prop);
23056 struct image *img = IMAGE_FROM_ID (it->f, id);
23057
23058 return OK_PIXELS (width_p ? img->width : img->height);
23059 }
23060 #endif
23061 if (EQ (car, Qplus) || EQ (car, Qminus))
23062 {
23063 int first = 1;
23064 double px;
23065
23066 pixels = 0;
23067 while (CONSP (cdr))
23068 {
23069 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23070 font, width_p, align_to))
23071 return 0;
23072 if (first)
23073 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23074 else
23075 pixels += px;
23076 cdr = XCDR (cdr);
23077 }
23078 if (EQ (car, Qminus))
23079 pixels = -pixels;
23080 return OK_PIXELS (pixels);
23081 }
23082
23083 car = buffer_local_value_1 (car, it->w->contents);
23084 if (EQ (car, Qunbound))
23085 car = Qnil;
23086 }
23087
23088 if (INTEGERP (car) || FLOATP (car))
23089 {
23090 double fact;
23091 pixels = XFLOATINT (car);
23092 if (NILP (cdr))
23093 return OK_PIXELS (pixels);
23094 if (calc_pixel_width_or_height (&fact, it, cdr,
23095 font, width_p, align_to))
23096 return OK_PIXELS (pixels * fact);
23097 return 0;
23098 }
23099
23100 return 0;
23101 }
23102
23103 return 0;
23104 }
23105
23106 \f
23107 /***********************************************************************
23108 Glyph Display
23109 ***********************************************************************/
23110
23111 #ifdef HAVE_WINDOW_SYSTEM
23112
23113 #ifdef GLYPH_DEBUG
23114
23115 void
23116 dump_glyph_string (struct glyph_string *s)
23117 {
23118 fprintf (stderr, "glyph string\n");
23119 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23120 s->x, s->y, s->width, s->height);
23121 fprintf (stderr, " ybase = %d\n", s->ybase);
23122 fprintf (stderr, " hl = %d\n", s->hl);
23123 fprintf (stderr, " left overhang = %d, right = %d\n",
23124 s->left_overhang, s->right_overhang);
23125 fprintf (stderr, " nchars = %d\n", s->nchars);
23126 fprintf (stderr, " extends to end of line = %d\n",
23127 s->extends_to_end_of_line_p);
23128 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23129 fprintf (stderr, " bg width = %d\n", s->background_width);
23130 }
23131
23132 #endif /* GLYPH_DEBUG */
23133
23134 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23135 of XChar2b structures for S; it can't be allocated in
23136 init_glyph_string because it must be allocated via `alloca'. W
23137 is the window on which S is drawn. ROW and AREA are the glyph row
23138 and area within the row from which S is constructed. START is the
23139 index of the first glyph structure covered by S. HL is a
23140 face-override for drawing S. */
23141
23142 #ifdef HAVE_NTGUI
23143 #define OPTIONAL_HDC(hdc) HDC hdc,
23144 #define DECLARE_HDC(hdc) HDC hdc;
23145 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23146 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23147 #endif
23148
23149 #ifndef OPTIONAL_HDC
23150 #define OPTIONAL_HDC(hdc)
23151 #define DECLARE_HDC(hdc)
23152 #define ALLOCATE_HDC(hdc, f)
23153 #define RELEASE_HDC(hdc, f)
23154 #endif
23155
23156 static void
23157 init_glyph_string (struct glyph_string *s,
23158 OPTIONAL_HDC (hdc)
23159 XChar2b *char2b, struct window *w, struct glyph_row *row,
23160 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23161 {
23162 memset (s, 0, sizeof *s);
23163 s->w = w;
23164 s->f = XFRAME (w->frame);
23165 #ifdef HAVE_NTGUI
23166 s->hdc = hdc;
23167 #endif
23168 s->display = FRAME_X_DISPLAY (s->f);
23169 s->window = FRAME_X_WINDOW (s->f);
23170 s->char2b = char2b;
23171 s->hl = hl;
23172 s->row = row;
23173 s->area = area;
23174 s->first_glyph = row->glyphs[area] + start;
23175 s->height = row->height;
23176 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23177 s->ybase = s->y + row->ascent;
23178 }
23179
23180
23181 /* Append the list of glyph strings with head H and tail T to the list
23182 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23183
23184 static void
23185 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23186 struct glyph_string *h, struct glyph_string *t)
23187 {
23188 if (h)
23189 {
23190 if (*head)
23191 (*tail)->next = h;
23192 else
23193 *head = h;
23194 h->prev = *tail;
23195 *tail = t;
23196 }
23197 }
23198
23199
23200 /* Prepend the list of glyph strings with head H and tail T to the
23201 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23202 result. */
23203
23204 static void
23205 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23206 struct glyph_string *h, struct glyph_string *t)
23207 {
23208 if (h)
23209 {
23210 if (*head)
23211 (*head)->prev = t;
23212 else
23213 *tail = t;
23214 t->next = *head;
23215 *head = h;
23216 }
23217 }
23218
23219
23220 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23221 Set *HEAD and *TAIL to the resulting list. */
23222
23223 static void
23224 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23225 struct glyph_string *s)
23226 {
23227 s->next = s->prev = NULL;
23228 append_glyph_string_lists (head, tail, s, s);
23229 }
23230
23231
23232 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23233 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23234 make sure that X resources for the face returned are allocated.
23235 Value is a pointer to a realized face that is ready for display if
23236 DISPLAY_P is non-zero. */
23237
23238 static struct face *
23239 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23240 XChar2b *char2b, int display_p)
23241 {
23242 struct face *face = FACE_FROM_ID (f, face_id);
23243 unsigned code = 0;
23244
23245 if (face->font)
23246 {
23247 code = face->font->driver->encode_char (face->font, c);
23248
23249 if (code == FONT_INVALID_CODE)
23250 code = 0;
23251 }
23252 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23253
23254 /* Make sure X resources of the face are allocated. */
23255 #ifdef HAVE_X_WINDOWS
23256 if (display_p)
23257 #endif
23258 {
23259 eassert (face != NULL);
23260 PREPARE_FACE_FOR_DISPLAY (f, face);
23261 }
23262
23263 return face;
23264 }
23265
23266
23267 /* Get face and two-byte form of character glyph GLYPH on frame F.
23268 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23269 a pointer to a realized face that is ready for display. */
23270
23271 static struct face *
23272 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23273 XChar2b *char2b, int *two_byte_p)
23274 {
23275 struct face *face;
23276 unsigned code = 0;
23277
23278 eassert (glyph->type == CHAR_GLYPH);
23279 face = FACE_FROM_ID (f, glyph->face_id);
23280
23281 /* Make sure X resources of the face are allocated. */
23282 eassert (face != NULL);
23283 PREPARE_FACE_FOR_DISPLAY (f, face);
23284
23285 if (two_byte_p)
23286 *two_byte_p = 0;
23287
23288 if (face->font)
23289 {
23290 if (CHAR_BYTE8_P (glyph->u.ch))
23291 code = CHAR_TO_BYTE8 (glyph->u.ch);
23292 else
23293 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23294
23295 if (code == FONT_INVALID_CODE)
23296 code = 0;
23297 }
23298
23299 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23300 return face;
23301 }
23302
23303
23304 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23305 Return 1 if FONT has a glyph for C, otherwise return 0. */
23306
23307 static int
23308 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23309 {
23310 unsigned code;
23311
23312 if (CHAR_BYTE8_P (c))
23313 code = CHAR_TO_BYTE8 (c);
23314 else
23315 code = font->driver->encode_char (font, c);
23316
23317 if (code == FONT_INVALID_CODE)
23318 return 0;
23319 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23320 return 1;
23321 }
23322
23323
23324 /* Fill glyph string S with composition components specified by S->cmp.
23325
23326 BASE_FACE is the base face of the composition.
23327 S->cmp_from is the index of the first component for S.
23328
23329 OVERLAPS non-zero means S should draw the foreground only, and use
23330 its physical height for clipping. See also draw_glyphs.
23331
23332 Value is the index of a component not in S. */
23333
23334 static int
23335 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23336 int overlaps)
23337 {
23338 int i;
23339 /* For all glyphs of this composition, starting at the offset
23340 S->cmp_from, until we reach the end of the definition or encounter a
23341 glyph that requires the different face, add it to S. */
23342 struct face *face;
23343
23344 eassert (s);
23345
23346 s->for_overlaps = overlaps;
23347 s->face = NULL;
23348 s->font = NULL;
23349 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23350 {
23351 int c = COMPOSITION_GLYPH (s->cmp, i);
23352
23353 /* TAB in a composition means display glyphs with padding space
23354 on the left or right. */
23355 if (c != '\t')
23356 {
23357 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23358 -1, Qnil);
23359
23360 face = get_char_face_and_encoding (s->f, c, face_id,
23361 s->char2b + i, 1);
23362 if (face)
23363 {
23364 if (! s->face)
23365 {
23366 s->face = face;
23367 s->font = s->face->font;
23368 }
23369 else if (s->face != face)
23370 break;
23371 }
23372 }
23373 ++s->nchars;
23374 }
23375 s->cmp_to = i;
23376
23377 if (s->face == NULL)
23378 {
23379 s->face = base_face->ascii_face;
23380 s->font = s->face->font;
23381 }
23382
23383 /* All glyph strings for the same composition has the same width,
23384 i.e. the width set for the first component of the composition. */
23385 s->width = s->first_glyph->pixel_width;
23386
23387 /* If the specified font could not be loaded, use the frame's
23388 default font, but record the fact that we couldn't load it in
23389 the glyph string so that we can draw rectangles for the
23390 characters of the glyph string. */
23391 if (s->font == NULL)
23392 {
23393 s->font_not_found_p = 1;
23394 s->font = FRAME_FONT (s->f);
23395 }
23396
23397 /* Adjust base line for subscript/superscript text. */
23398 s->ybase += s->first_glyph->voffset;
23399
23400 /* This glyph string must always be drawn with 16-bit functions. */
23401 s->two_byte_p = 1;
23402
23403 return s->cmp_to;
23404 }
23405
23406 static int
23407 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23408 int start, int end, int overlaps)
23409 {
23410 struct glyph *glyph, *last;
23411 Lisp_Object lgstring;
23412 int i;
23413
23414 s->for_overlaps = overlaps;
23415 glyph = s->row->glyphs[s->area] + start;
23416 last = s->row->glyphs[s->area] + end;
23417 s->cmp_id = glyph->u.cmp.id;
23418 s->cmp_from = glyph->slice.cmp.from;
23419 s->cmp_to = glyph->slice.cmp.to + 1;
23420 s->face = FACE_FROM_ID (s->f, face_id);
23421 lgstring = composition_gstring_from_id (s->cmp_id);
23422 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23423 glyph++;
23424 while (glyph < last
23425 && glyph->u.cmp.automatic
23426 && glyph->u.cmp.id == s->cmp_id
23427 && s->cmp_to == glyph->slice.cmp.from)
23428 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23429
23430 for (i = s->cmp_from; i < s->cmp_to; i++)
23431 {
23432 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23433 unsigned code = LGLYPH_CODE (lglyph);
23434
23435 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23436 }
23437 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23438 return glyph - s->row->glyphs[s->area];
23439 }
23440
23441
23442 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23443 See the comment of fill_glyph_string for arguments.
23444 Value is the index of the first glyph not in S. */
23445
23446
23447 static int
23448 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23449 int start, int end, int overlaps)
23450 {
23451 struct glyph *glyph, *last;
23452 int voffset;
23453
23454 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23455 s->for_overlaps = overlaps;
23456 glyph = s->row->glyphs[s->area] + start;
23457 last = s->row->glyphs[s->area] + end;
23458 voffset = glyph->voffset;
23459 s->face = FACE_FROM_ID (s->f, face_id);
23460 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23461 s->nchars = 1;
23462 s->width = glyph->pixel_width;
23463 glyph++;
23464 while (glyph < last
23465 && glyph->type == GLYPHLESS_GLYPH
23466 && glyph->voffset == voffset
23467 && glyph->face_id == face_id)
23468 {
23469 s->nchars++;
23470 s->width += glyph->pixel_width;
23471 glyph++;
23472 }
23473 s->ybase += voffset;
23474 return glyph - s->row->glyphs[s->area];
23475 }
23476
23477
23478 /* Fill glyph string S from a sequence of character glyphs.
23479
23480 FACE_ID is the face id of the string. START is the index of the
23481 first glyph to consider, END is the index of the last + 1.
23482 OVERLAPS non-zero means S should draw the foreground only, and use
23483 its physical height for clipping. See also draw_glyphs.
23484
23485 Value is the index of the first glyph not in S. */
23486
23487 static int
23488 fill_glyph_string (struct glyph_string *s, int face_id,
23489 int start, int end, int overlaps)
23490 {
23491 struct glyph *glyph, *last;
23492 int voffset;
23493 int glyph_not_available_p;
23494
23495 eassert (s->f == XFRAME (s->w->frame));
23496 eassert (s->nchars == 0);
23497 eassert (start >= 0 && end > start);
23498
23499 s->for_overlaps = overlaps;
23500 glyph = s->row->glyphs[s->area] + start;
23501 last = s->row->glyphs[s->area] + end;
23502 voffset = glyph->voffset;
23503 s->padding_p = glyph->padding_p;
23504 glyph_not_available_p = glyph->glyph_not_available_p;
23505
23506 while (glyph < last
23507 && glyph->type == CHAR_GLYPH
23508 && glyph->voffset == voffset
23509 /* Same face id implies same font, nowadays. */
23510 && glyph->face_id == face_id
23511 && glyph->glyph_not_available_p == glyph_not_available_p)
23512 {
23513 int two_byte_p;
23514
23515 s->face = get_glyph_face_and_encoding (s->f, glyph,
23516 s->char2b + s->nchars,
23517 &two_byte_p);
23518 s->two_byte_p = two_byte_p;
23519 ++s->nchars;
23520 eassert (s->nchars <= end - start);
23521 s->width += glyph->pixel_width;
23522 if (glyph++->padding_p != s->padding_p)
23523 break;
23524 }
23525
23526 s->font = s->face->font;
23527
23528 /* If the specified font could not be loaded, use the frame's font,
23529 but record the fact that we couldn't load it in
23530 S->font_not_found_p so that we can draw rectangles for the
23531 characters of the glyph string. */
23532 if (s->font == NULL || glyph_not_available_p)
23533 {
23534 s->font_not_found_p = 1;
23535 s->font = FRAME_FONT (s->f);
23536 }
23537
23538 /* Adjust base line for subscript/superscript text. */
23539 s->ybase += voffset;
23540
23541 eassert (s->face && s->face->gc);
23542 return glyph - s->row->glyphs[s->area];
23543 }
23544
23545
23546 /* Fill glyph string S from image glyph S->first_glyph. */
23547
23548 static void
23549 fill_image_glyph_string (struct glyph_string *s)
23550 {
23551 eassert (s->first_glyph->type == IMAGE_GLYPH);
23552 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23553 eassert (s->img);
23554 s->slice = s->first_glyph->slice.img;
23555 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23556 s->font = s->face->font;
23557 s->width = s->first_glyph->pixel_width;
23558
23559 /* Adjust base line for subscript/superscript text. */
23560 s->ybase += s->first_glyph->voffset;
23561 }
23562
23563
23564 /* Fill glyph string S from a sequence of stretch glyphs.
23565
23566 START is the index of the first glyph to consider,
23567 END is the index of the last + 1.
23568
23569 Value is the index of the first glyph not in S. */
23570
23571 static int
23572 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23573 {
23574 struct glyph *glyph, *last;
23575 int voffset, face_id;
23576
23577 eassert (s->first_glyph->type == STRETCH_GLYPH);
23578
23579 glyph = s->row->glyphs[s->area] + start;
23580 last = s->row->glyphs[s->area] + end;
23581 face_id = glyph->face_id;
23582 s->face = FACE_FROM_ID (s->f, face_id);
23583 s->font = s->face->font;
23584 s->width = glyph->pixel_width;
23585 s->nchars = 1;
23586 voffset = glyph->voffset;
23587
23588 for (++glyph;
23589 (glyph < last
23590 && glyph->type == STRETCH_GLYPH
23591 && glyph->voffset == voffset
23592 && glyph->face_id == face_id);
23593 ++glyph)
23594 s->width += glyph->pixel_width;
23595
23596 /* Adjust base line for subscript/superscript text. */
23597 s->ybase += voffset;
23598
23599 /* The case that face->gc == 0 is handled when drawing the glyph
23600 string by calling PREPARE_FACE_FOR_DISPLAY. */
23601 eassert (s->face);
23602 return glyph - s->row->glyphs[s->area];
23603 }
23604
23605 static struct font_metrics *
23606 get_per_char_metric (struct font *font, XChar2b *char2b)
23607 {
23608 static struct font_metrics metrics;
23609 unsigned code;
23610
23611 if (! font)
23612 return NULL;
23613 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23614 if (code == FONT_INVALID_CODE)
23615 return NULL;
23616 font->driver->text_extents (font, &code, 1, &metrics);
23617 return &metrics;
23618 }
23619
23620 /* EXPORT for RIF:
23621 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23622 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23623 assumed to be zero. */
23624
23625 void
23626 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23627 {
23628 *left = *right = 0;
23629
23630 if (glyph->type == CHAR_GLYPH)
23631 {
23632 struct face *face;
23633 XChar2b char2b;
23634 struct font_metrics *pcm;
23635
23636 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23637 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23638 {
23639 if (pcm->rbearing > pcm->width)
23640 *right = pcm->rbearing - pcm->width;
23641 if (pcm->lbearing < 0)
23642 *left = -pcm->lbearing;
23643 }
23644 }
23645 else if (glyph->type == COMPOSITE_GLYPH)
23646 {
23647 if (! glyph->u.cmp.automatic)
23648 {
23649 struct composition *cmp = composition_table[glyph->u.cmp.id];
23650
23651 if (cmp->rbearing > cmp->pixel_width)
23652 *right = cmp->rbearing - cmp->pixel_width;
23653 if (cmp->lbearing < 0)
23654 *left = - cmp->lbearing;
23655 }
23656 else
23657 {
23658 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23659 struct font_metrics metrics;
23660
23661 composition_gstring_width (gstring, glyph->slice.cmp.from,
23662 glyph->slice.cmp.to + 1, &metrics);
23663 if (metrics.rbearing > metrics.width)
23664 *right = metrics.rbearing - metrics.width;
23665 if (metrics.lbearing < 0)
23666 *left = - metrics.lbearing;
23667 }
23668 }
23669 }
23670
23671
23672 /* Return the index of the first glyph preceding glyph string S that
23673 is overwritten by S because of S's left overhang. Value is -1
23674 if no glyphs are overwritten. */
23675
23676 static int
23677 left_overwritten (struct glyph_string *s)
23678 {
23679 int k;
23680
23681 if (s->left_overhang)
23682 {
23683 int x = 0, i;
23684 struct glyph *glyphs = s->row->glyphs[s->area];
23685 int first = s->first_glyph - glyphs;
23686
23687 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23688 x -= glyphs[i].pixel_width;
23689
23690 k = i + 1;
23691 }
23692 else
23693 k = -1;
23694
23695 return k;
23696 }
23697
23698
23699 /* Return the index of the first glyph preceding glyph string S that
23700 is overwriting S because of its right overhang. Value is -1 if no
23701 glyph in front of S overwrites S. */
23702
23703 static int
23704 left_overwriting (struct glyph_string *s)
23705 {
23706 int i, k, x;
23707 struct glyph *glyphs = s->row->glyphs[s->area];
23708 int first = s->first_glyph - glyphs;
23709
23710 k = -1;
23711 x = 0;
23712 for (i = first - 1; i >= 0; --i)
23713 {
23714 int left, right;
23715 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23716 if (x + right > 0)
23717 k = i;
23718 x -= glyphs[i].pixel_width;
23719 }
23720
23721 return k;
23722 }
23723
23724
23725 /* Return the index of the last glyph following glyph string S that is
23726 overwritten by S because of S's right overhang. Value is -1 if
23727 no such glyph is found. */
23728
23729 static int
23730 right_overwritten (struct glyph_string *s)
23731 {
23732 int k = -1;
23733
23734 if (s->right_overhang)
23735 {
23736 int x = 0, i;
23737 struct glyph *glyphs = s->row->glyphs[s->area];
23738 int first = (s->first_glyph - glyphs
23739 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23740 int end = s->row->used[s->area];
23741
23742 for (i = first; i < end && s->right_overhang > x; ++i)
23743 x += glyphs[i].pixel_width;
23744
23745 k = i;
23746 }
23747
23748 return k;
23749 }
23750
23751
23752 /* Return the index of the last glyph following glyph string S that
23753 overwrites S because of its left overhang. Value is negative
23754 if no such glyph is found. */
23755
23756 static int
23757 right_overwriting (struct glyph_string *s)
23758 {
23759 int i, k, x;
23760 int end = s->row->used[s->area];
23761 struct glyph *glyphs = s->row->glyphs[s->area];
23762 int first = (s->first_glyph - glyphs
23763 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23764
23765 k = -1;
23766 x = 0;
23767 for (i = first; i < end; ++i)
23768 {
23769 int left, right;
23770 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23771 if (x - left < 0)
23772 k = i;
23773 x += glyphs[i].pixel_width;
23774 }
23775
23776 return k;
23777 }
23778
23779
23780 /* Set background width of glyph string S. START is the index of the
23781 first glyph following S. LAST_X is the right-most x-position + 1
23782 in the drawing area. */
23783
23784 static void
23785 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23786 {
23787 /* If the face of this glyph string has to be drawn to the end of
23788 the drawing area, set S->extends_to_end_of_line_p. */
23789
23790 if (start == s->row->used[s->area]
23791 && s->area == TEXT_AREA
23792 && ((s->row->fill_line_p
23793 && (s->hl == DRAW_NORMAL_TEXT
23794 || s->hl == DRAW_IMAGE_RAISED
23795 || s->hl == DRAW_IMAGE_SUNKEN))
23796 || s->hl == DRAW_MOUSE_FACE))
23797 s->extends_to_end_of_line_p = 1;
23798
23799 /* If S extends its face to the end of the line, set its
23800 background_width to the distance to the right edge of the drawing
23801 area. */
23802 if (s->extends_to_end_of_line_p)
23803 s->background_width = last_x - s->x + 1;
23804 else
23805 s->background_width = s->width;
23806 }
23807
23808
23809 /* Compute overhangs and x-positions for glyph string S and its
23810 predecessors, or successors. X is the starting x-position for S.
23811 BACKWARD_P non-zero means process predecessors. */
23812
23813 static void
23814 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23815 {
23816 if (backward_p)
23817 {
23818 while (s)
23819 {
23820 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23821 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23822 x -= s->width;
23823 s->x = x;
23824 s = s->prev;
23825 }
23826 }
23827 else
23828 {
23829 while (s)
23830 {
23831 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23832 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23833 s->x = x;
23834 x += s->width;
23835 s = s->next;
23836 }
23837 }
23838 }
23839
23840
23841
23842 /* The following macros are only called from draw_glyphs below.
23843 They reference the following parameters of that function directly:
23844 `w', `row', `area', and `overlap_p'
23845 as well as the following local variables:
23846 `s', `f', and `hdc' (in W32) */
23847
23848 #ifdef HAVE_NTGUI
23849 /* On W32, silently add local `hdc' variable to argument list of
23850 init_glyph_string. */
23851 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23852 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23853 #else
23854 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23855 init_glyph_string (s, char2b, w, row, area, start, hl)
23856 #endif
23857
23858 /* Add a glyph string for a stretch glyph to the list of strings
23859 between HEAD and TAIL. START is the index of the stretch glyph in
23860 row area AREA of glyph row ROW. END is the index of the last glyph
23861 in that glyph row area. X is the current output position assigned
23862 to the new glyph string constructed. HL overrides that face of the
23863 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23864 is the right-most x-position of the drawing area. */
23865
23866 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23867 and below -- keep them on one line. */
23868 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23869 do \
23870 { \
23871 s = alloca (sizeof *s); \
23872 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23873 START = fill_stretch_glyph_string (s, START, END); \
23874 append_glyph_string (&HEAD, &TAIL, s); \
23875 s->x = (X); \
23876 } \
23877 while (0)
23878
23879
23880 /* Add a glyph string for an image glyph to the list of strings
23881 between HEAD and TAIL. START is the index of the image glyph in
23882 row area AREA of glyph row ROW. END is the index of the last glyph
23883 in that glyph row area. X is the current output position assigned
23884 to the new glyph string constructed. HL overrides that face of the
23885 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23886 is the right-most x-position of the drawing area. */
23887
23888 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23889 do \
23890 { \
23891 s = alloca (sizeof *s); \
23892 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23893 fill_image_glyph_string (s); \
23894 append_glyph_string (&HEAD, &TAIL, s); \
23895 ++START; \
23896 s->x = (X); \
23897 } \
23898 while (0)
23899
23900
23901 /* Add a glyph string for a sequence of character glyphs to the list
23902 of strings between HEAD and TAIL. START is the index of the first
23903 glyph in row area AREA of glyph row ROW that is part of the new
23904 glyph string. END is the index of the last glyph in that glyph row
23905 area. X is the current output position assigned to the new glyph
23906 string constructed. HL overrides that face of the glyph; e.g. it
23907 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23908 right-most x-position of the drawing area. */
23909
23910 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23911 do \
23912 { \
23913 int face_id; \
23914 XChar2b *char2b; \
23915 \
23916 face_id = (row)->glyphs[area][START].face_id; \
23917 \
23918 s = alloca (sizeof *s); \
23919 char2b = alloca ((END - START) * sizeof *char2b); \
23920 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23921 append_glyph_string (&HEAD, &TAIL, s); \
23922 s->x = (X); \
23923 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23924 } \
23925 while (0)
23926
23927
23928 /* Add a glyph string for a composite sequence to the list of strings
23929 between HEAD and TAIL. START is the index of the first glyph in
23930 row area AREA of glyph row ROW that is part of the new glyph
23931 string. END is the index of the last glyph in that glyph row area.
23932 X is the current output position assigned to the new glyph string
23933 constructed. HL overrides that face of the glyph; e.g. it is
23934 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23935 x-position of the drawing area. */
23936
23937 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23938 do { \
23939 int face_id = (row)->glyphs[area][START].face_id; \
23940 struct face *base_face = FACE_FROM_ID (f, face_id); \
23941 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23942 struct composition *cmp = composition_table[cmp_id]; \
23943 XChar2b *char2b; \
23944 struct glyph_string *first_s = NULL; \
23945 int n; \
23946 \
23947 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23948 \
23949 /* Make glyph_strings for each glyph sequence that is drawable by \
23950 the same face, and append them to HEAD/TAIL. */ \
23951 for (n = 0; n < cmp->glyph_len;) \
23952 { \
23953 s = alloca (sizeof *s); \
23954 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23955 append_glyph_string (&(HEAD), &(TAIL), s); \
23956 s->cmp = cmp; \
23957 s->cmp_from = n; \
23958 s->x = (X); \
23959 if (n == 0) \
23960 first_s = s; \
23961 n = fill_composite_glyph_string (s, base_face, overlaps); \
23962 } \
23963 \
23964 ++START; \
23965 s = first_s; \
23966 } while (0)
23967
23968
23969 /* Add a glyph string for a glyph-string sequence to the list of strings
23970 between HEAD and TAIL. */
23971
23972 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23973 do { \
23974 int face_id; \
23975 XChar2b *char2b; \
23976 Lisp_Object gstring; \
23977 \
23978 face_id = (row)->glyphs[area][START].face_id; \
23979 gstring = (composition_gstring_from_id \
23980 ((row)->glyphs[area][START].u.cmp.id)); \
23981 s = alloca (sizeof *s); \
23982 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23983 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23984 append_glyph_string (&(HEAD), &(TAIL), s); \
23985 s->x = (X); \
23986 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23987 } while (0)
23988
23989
23990 /* Add a glyph string for a sequence of glyphless character's glyphs
23991 to the list of strings between HEAD and TAIL. The meanings of
23992 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23993
23994 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23995 do \
23996 { \
23997 int face_id; \
23998 \
23999 face_id = (row)->glyphs[area][START].face_id; \
24000 \
24001 s = alloca (sizeof *s); \
24002 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24003 append_glyph_string (&HEAD, &TAIL, s); \
24004 s->x = (X); \
24005 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24006 overlaps); \
24007 } \
24008 while (0)
24009
24010
24011 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24012 of AREA of glyph row ROW on window W between indices START and END.
24013 HL overrides the face for drawing glyph strings, e.g. it is
24014 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24015 x-positions of the drawing area.
24016
24017 This is an ugly monster macro construct because we must use alloca
24018 to allocate glyph strings (because draw_glyphs can be called
24019 asynchronously). */
24020
24021 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24022 do \
24023 { \
24024 HEAD = TAIL = NULL; \
24025 while (START < END) \
24026 { \
24027 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24028 switch (first_glyph->type) \
24029 { \
24030 case CHAR_GLYPH: \
24031 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24032 HL, X, LAST_X); \
24033 break; \
24034 \
24035 case COMPOSITE_GLYPH: \
24036 if (first_glyph->u.cmp.automatic) \
24037 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24038 HL, X, LAST_X); \
24039 else \
24040 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24041 HL, X, LAST_X); \
24042 break; \
24043 \
24044 case STRETCH_GLYPH: \
24045 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24046 HL, X, LAST_X); \
24047 break; \
24048 \
24049 case IMAGE_GLYPH: \
24050 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24051 HL, X, LAST_X); \
24052 break; \
24053 \
24054 case GLYPHLESS_GLYPH: \
24055 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24056 HL, X, LAST_X); \
24057 break; \
24058 \
24059 default: \
24060 emacs_abort (); \
24061 } \
24062 \
24063 if (s) \
24064 { \
24065 set_glyph_string_background_width (s, START, LAST_X); \
24066 (X) += s->width; \
24067 } \
24068 } \
24069 } while (0)
24070
24071
24072 /* Draw glyphs between START and END in AREA of ROW on window W,
24073 starting at x-position X. X is relative to AREA in W. HL is a
24074 face-override with the following meaning:
24075
24076 DRAW_NORMAL_TEXT draw normally
24077 DRAW_CURSOR draw in cursor face
24078 DRAW_MOUSE_FACE draw in mouse face.
24079 DRAW_INVERSE_VIDEO draw in mode line face
24080 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24081 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24082
24083 If OVERLAPS is non-zero, draw only the foreground of characters and
24084 clip to the physical height of ROW. Non-zero value also defines
24085 the overlapping part to be drawn:
24086
24087 OVERLAPS_PRED overlap with preceding rows
24088 OVERLAPS_SUCC overlap with succeeding rows
24089 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24090 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24091
24092 Value is the x-position reached, relative to AREA of W. */
24093
24094 static int
24095 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24096 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24097 enum draw_glyphs_face hl, int overlaps)
24098 {
24099 struct glyph_string *head, *tail;
24100 struct glyph_string *s;
24101 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24102 int i, j, x_reached, last_x, area_left = 0;
24103 struct frame *f = XFRAME (WINDOW_FRAME (w));
24104 DECLARE_HDC (hdc);
24105
24106 ALLOCATE_HDC (hdc, f);
24107
24108 /* Let's rather be paranoid than getting a SEGV. */
24109 end = min (end, row->used[area]);
24110 start = clip_to_bounds (0, start, end);
24111
24112 /* Translate X to frame coordinates. Set last_x to the right
24113 end of the drawing area. */
24114 if (row->full_width_p)
24115 {
24116 /* X is relative to the left edge of W, without scroll bars
24117 or fringes. */
24118 area_left = WINDOW_LEFT_EDGE_X (w);
24119 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24120 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24121 }
24122 else
24123 {
24124 area_left = window_box_left (w, area);
24125 last_x = area_left + window_box_width (w, area);
24126 }
24127 x += area_left;
24128
24129 /* Build a doubly-linked list of glyph_string structures between
24130 head and tail from what we have to draw. Note that the macro
24131 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24132 the reason we use a separate variable `i'. */
24133 i = start;
24134 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24135 if (tail)
24136 x_reached = tail->x + tail->background_width;
24137 else
24138 x_reached = x;
24139
24140 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24141 the row, redraw some glyphs in front or following the glyph
24142 strings built above. */
24143 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24144 {
24145 struct glyph_string *h, *t;
24146 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24147 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24148 int check_mouse_face = 0;
24149 int dummy_x = 0;
24150
24151 /* If mouse highlighting is on, we may need to draw adjacent
24152 glyphs using mouse-face highlighting. */
24153 if (area == TEXT_AREA && row->mouse_face_p
24154 && hlinfo->mouse_face_beg_row >= 0
24155 && hlinfo->mouse_face_end_row >= 0)
24156 {
24157 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24158
24159 if (row_vpos >= hlinfo->mouse_face_beg_row
24160 && row_vpos <= hlinfo->mouse_face_end_row)
24161 {
24162 check_mouse_face = 1;
24163 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24164 ? hlinfo->mouse_face_beg_col : 0;
24165 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24166 ? hlinfo->mouse_face_end_col
24167 : row->used[TEXT_AREA];
24168 }
24169 }
24170
24171 /* Compute overhangs for all glyph strings. */
24172 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24173 for (s = head; s; s = s->next)
24174 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24175
24176 /* Prepend glyph strings for glyphs in front of the first glyph
24177 string that are overwritten because of the first glyph
24178 string's left overhang. The background of all strings
24179 prepended must be drawn because the first glyph string
24180 draws over it. */
24181 i = left_overwritten (head);
24182 if (i >= 0)
24183 {
24184 enum draw_glyphs_face overlap_hl;
24185
24186 /* If this row contains mouse highlighting, attempt to draw
24187 the overlapped glyphs with the correct highlight. This
24188 code fails if the overlap encompasses more than one glyph
24189 and mouse-highlight spans only some of these glyphs.
24190 However, making it work perfectly involves a lot more
24191 code, and I don't know if the pathological case occurs in
24192 practice, so we'll stick to this for now. --- cyd */
24193 if (check_mouse_face
24194 && mouse_beg_col < start && mouse_end_col > i)
24195 overlap_hl = DRAW_MOUSE_FACE;
24196 else
24197 overlap_hl = DRAW_NORMAL_TEXT;
24198
24199 j = i;
24200 BUILD_GLYPH_STRINGS (j, start, h, t,
24201 overlap_hl, dummy_x, last_x);
24202 start = i;
24203 compute_overhangs_and_x (t, head->x, 1);
24204 prepend_glyph_string_lists (&head, &tail, h, t);
24205 clip_head = head;
24206 }
24207
24208 /* Prepend glyph strings for glyphs in front of the first glyph
24209 string that overwrite that glyph string because of their
24210 right overhang. For these strings, only the foreground must
24211 be drawn, because it draws over the glyph string at `head'.
24212 The background must not be drawn because this would overwrite
24213 right overhangs of preceding glyphs for which no glyph
24214 strings exist. */
24215 i = left_overwriting (head);
24216 if (i >= 0)
24217 {
24218 enum draw_glyphs_face overlap_hl;
24219
24220 if (check_mouse_face
24221 && mouse_beg_col < start && mouse_end_col > i)
24222 overlap_hl = DRAW_MOUSE_FACE;
24223 else
24224 overlap_hl = DRAW_NORMAL_TEXT;
24225
24226 clip_head = head;
24227 BUILD_GLYPH_STRINGS (i, start, h, t,
24228 overlap_hl, dummy_x, last_x);
24229 for (s = h; s; s = s->next)
24230 s->background_filled_p = 1;
24231 compute_overhangs_and_x (t, head->x, 1);
24232 prepend_glyph_string_lists (&head, &tail, h, t);
24233 }
24234
24235 /* Append glyphs strings for glyphs following the last glyph
24236 string tail that are overwritten by tail. The background of
24237 these strings has to be drawn because tail's foreground draws
24238 over it. */
24239 i = right_overwritten (tail);
24240 if (i >= 0)
24241 {
24242 enum draw_glyphs_face overlap_hl;
24243
24244 if (check_mouse_face
24245 && mouse_beg_col < i && mouse_end_col > end)
24246 overlap_hl = DRAW_MOUSE_FACE;
24247 else
24248 overlap_hl = DRAW_NORMAL_TEXT;
24249
24250 BUILD_GLYPH_STRINGS (end, i, h, t,
24251 overlap_hl, x, last_x);
24252 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24253 we don't have `end = i;' here. */
24254 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24255 append_glyph_string_lists (&head, &tail, h, t);
24256 clip_tail = tail;
24257 }
24258
24259 /* Append glyph strings for glyphs following the last glyph
24260 string tail that overwrite tail. The foreground of such
24261 glyphs has to be drawn because it writes into the background
24262 of tail. The background must not be drawn because it could
24263 paint over the foreground of following glyphs. */
24264 i = right_overwriting (tail);
24265 if (i >= 0)
24266 {
24267 enum draw_glyphs_face overlap_hl;
24268 if (check_mouse_face
24269 && mouse_beg_col < i && mouse_end_col > end)
24270 overlap_hl = DRAW_MOUSE_FACE;
24271 else
24272 overlap_hl = DRAW_NORMAL_TEXT;
24273
24274 clip_tail = tail;
24275 i++; /* We must include the Ith glyph. */
24276 BUILD_GLYPH_STRINGS (end, i, h, t,
24277 overlap_hl, x, last_x);
24278 for (s = h; s; s = s->next)
24279 s->background_filled_p = 1;
24280 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24281 append_glyph_string_lists (&head, &tail, h, t);
24282 }
24283 if (clip_head || clip_tail)
24284 for (s = head; s; s = s->next)
24285 {
24286 s->clip_head = clip_head;
24287 s->clip_tail = clip_tail;
24288 }
24289 }
24290
24291 /* Draw all strings. */
24292 for (s = head; s; s = s->next)
24293 FRAME_RIF (f)->draw_glyph_string (s);
24294
24295 #ifndef HAVE_NS
24296 /* When focus a sole frame and move horizontally, this sets on_p to 0
24297 causing a failure to erase prev cursor position. */
24298 if (area == TEXT_AREA
24299 && !row->full_width_p
24300 /* When drawing overlapping rows, only the glyph strings'
24301 foreground is drawn, which doesn't erase a cursor
24302 completely. */
24303 && !overlaps)
24304 {
24305 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24306 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24307 : (tail ? tail->x + tail->background_width : x));
24308 x0 -= area_left;
24309 x1 -= area_left;
24310
24311 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24312 row->y, MATRIX_ROW_BOTTOM_Y (row));
24313 }
24314 #endif
24315
24316 /* Value is the x-position up to which drawn, relative to AREA of W.
24317 This doesn't include parts drawn because of overhangs. */
24318 if (row->full_width_p)
24319 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24320 else
24321 x_reached -= area_left;
24322
24323 RELEASE_HDC (hdc, f);
24324
24325 return x_reached;
24326 }
24327
24328 /* Expand row matrix if too narrow. Don't expand if area
24329 is not present. */
24330
24331 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24332 { \
24333 if (!it->f->fonts_changed \
24334 && (it->glyph_row->glyphs[area] \
24335 < it->glyph_row->glyphs[area + 1])) \
24336 { \
24337 it->w->ncols_scale_factor++; \
24338 it->f->fonts_changed = 1; \
24339 } \
24340 }
24341
24342 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24343 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24344
24345 static void
24346 append_glyph (struct it *it)
24347 {
24348 struct glyph *glyph;
24349 enum glyph_row_area area = it->area;
24350
24351 eassert (it->glyph_row);
24352 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24353
24354 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24355 if (glyph < it->glyph_row->glyphs[area + 1])
24356 {
24357 /* If the glyph row is reversed, we need to prepend the glyph
24358 rather than append it. */
24359 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24360 {
24361 struct glyph *g;
24362
24363 /* Make room for the additional glyph. */
24364 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24365 g[1] = *g;
24366 glyph = it->glyph_row->glyphs[area];
24367 }
24368 glyph->charpos = CHARPOS (it->position);
24369 glyph->object = it->object;
24370 if (it->pixel_width > 0)
24371 {
24372 glyph->pixel_width = it->pixel_width;
24373 glyph->padding_p = 0;
24374 }
24375 else
24376 {
24377 /* Assure at least 1-pixel width. Otherwise, cursor can't
24378 be displayed correctly. */
24379 glyph->pixel_width = 1;
24380 glyph->padding_p = 1;
24381 }
24382 glyph->ascent = it->ascent;
24383 glyph->descent = it->descent;
24384 glyph->voffset = it->voffset;
24385 glyph->type = CHAR_GLYPH;
24386 glyph->avoid_cursor_p = it->avoid_cursor_p;
24387 glyph->multibyte_p = it->multibyte_p;
24388 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24389 {
24390 /* In R2L rows, the left and the right box edges need to be
24391 drawn in reverse direction. */
24392 glyph->right_box_line_p = it->start_of_box_run_p;
24393 glyph->left_box_line_p = it->end_of_box_run_p;
24394 }
24395 else
24396 {
24397 glyph->left_box_line_p = it->start_of_box_run_p;
24398 glyph->right_box_line_p = it->end_of_box_run_p;
24399 }
24400 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24401 || it->phys_descent > it->descent);
24402 glyph->glyph_not_available_p = it->glyph_not_available_p;
24403 glyph->face_id = it->face_id;
24404 glyph->u.ch = it->char_to_display;
24405 glyph->slice.img = null_glyph_slice;
24406 glyph->font_type = FONT_TYPE_UNKNOWN;
24407 if (it->bidi_p)
24408 {
24409 glyph->resolved_level = it->bidi_it.resolved_level;
24410 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24411 emacs_abort ();
24412 glyph->bidi_type = it->bidi_it.type;
24413 }
24414 else
24415 {
24416 glyph->resolved_level = 0;
24417 glyph->bidi_type = UNKNOWN_BT;
24418 }
24419 ++it->glyph_row->used[area];
24420 }
24421 else
24422 IT_EXPAND_MATRIX_WIDTH (it, area);
24423 }
24424
24425 /* Store one glyph for the composition IT->cmp_it.id in
24426 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24427 non-null. */
24428
24429 static void
24430 append_composite_glyph (struct it *it)
24431 {
24432 struct glyph *glyph;
24433 enum glyph_row_area area = it->area;
24434
24435 eassert (it->glyph_row);
24436
24437 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24438 if (glyph < it->glyph_row->glyphs[area + 1])
24439 {
24440 /* If the glyph row is reversed, we need to prepend the glyph
24441 rather than append it. */
24442 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24443 {
24444 struct glyph *g;
24445
24446 /* Make room for the new glyph. */
24447 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24448 g[1] = *g;
24449 glyph = it->glyph_row->glyphs[it->area];
24450 }
24451 glyph->charpos = it->cmp_it.charpos;
24452 glyph->object = it->object;
24453 glyph->pixel_width = it->pixel_width;
24454 glyph->ascent = it->ascent;
24455 glyph->descent = it->descent;
24456 glyph->voffset = it->voffset;
24457 glyph->type = COMPOSITE_GLYPH;
24458 if (it->cmp_it.ch < 0)
24459 {
24460 glyph->u.cmp.automatic = 0;
24461 glyph->u.cmp.id = it->cmp_it.id;
24462 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24463 }
24464 else
24465 {
24466 glyph->u.cmp.automatic = 1;
24467 glyph->u.cmp.id = it->cmp_it.id;
24468 glyph->slice.cmp.from = it->cmp_it.from;
24469 glyph->slice.cmp.to = it->cmp_it.to - 1;
24470 }
24471 glyph->avoid_cursor_p = it->avoid_cursor_p;
24472 glyph->multibyte_p = it->multibyte_p;
24473 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24474 {
24475 /* In R2L rows, the left and the right box edges need to be
24476 drawn in reverse direction. */
24477 glyph->right_box_line_p = it->start_of_box_run_p;
24478 glyph->left_box_line_p = it->end_of_box_run_p;
24479 }
24480 else
24481 {
24482 glyph->left_box_line_p = it->start_of_box_run_p;
24483 glyph->right_box_line_p = it->end_of_box_run_p;
24484 }
24485 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24486 || it->phys_descent > it->descent);
24487 glyph->padding_p = 0;
24488 glyph->glyph_not_available_p = 0;
24489 glyph->face_id = it->face_id;
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 /* Change IT->ascent and IT->height according to the setting of
24506 IT->voffset. */
24507
24508 static void
24509 take_vertical_position_into_account (struct it *it)
24510 {
24511 if (it->voffset)
24512 {
24513 if (it->voffset < 0)
24514 /* Increase the ascent so that we can display the text higher
24515 in the line. */
24516 it->ascent -= it->voffset;
24517 else
24518 /* Increase the descent so that we can display the text lower
24519 in the line. */
24520 it->descent += it->voffset;
24521 }
24522 }
24523
24524
24525 /* Produce glyphs/get display metrics for the image IT is loaded with.
24526 See the description of struct display_iterator in dispextern.h for
24527 an overview of struct display_iterator. */
24528
24529 static void
24530 produce_image_glyph (struct it *it)
24531 {
24532 struct image *img;
24533 struct face *face;
24534 int glyph_ascent, crop;
24535 struct glyph_slice slice;
24536
24537 eassert (it->what == IT_IMAGE);
24538
24539 face = FACE_FROM_ID (it->f, it->face_id);
24540 eassert (face);
24541 /* Make sure X resources of the face is loaded. */
24542 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24543
24544 if (it->image_id < 0)
24545 {
24546 /* Fringe bitmap. */
24547 it->ascent = it->phys_ascent = 0;
24548 it->descent = it->phys_descent = 0;
24549 it->pixel_width = 0;
24550 it->nglyphs = 0;
24551 return;
24552 }
24553
24554 img = IMAGE_FROM_ID (it->f, it->image_id);
24555 eassert (img);
24556 /* Make sure X resources of the image is loaded. */
24557 prepare_image_for_display (it->f, img);
24558
24559 slice.x = slice.y = 0;
24560 slice.width = img->width;
24561 slice.height = img->height;
24562
24563 if (INTEGERP (it->slice.x))
24564 slice.x = XINT (it->slice.x);
24565 else if (FLOATP (it->slice.x))
24566 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24567
24568 if (INTEGERP (it->slice.y))
24569 slice.y = XINT (it->slice.y);
24570 else if (FLOATP (it->slice.y))
24571 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24572
24573 if (INTEGERP (it->slice.width))
24574 slice.width = XINT (it->slice.width);
24575 else if (FLOATP (it->slice.width))
24576 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24577
24578 if (INTEGERP (it->slice.height))
24579 slice.height = XINT (it->slice.height);
24580 else if (FLOATP (it->slice.height))
24581 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24582
24583 if (slice.x >= img->width)
24584 slice.x = img->width;
24585 if (slice.y >= img->height)
24586 slice.y = img->height;
24587 if (slice.x + slice.width >= img->width)
24588 slice.width = img->width - slice.x;
24589 if (slice.y + slice.height > img->height)
24590 slice.height = img->height - slice.y;
24591
24592 if (slice.width == 0 || slice.height == 0)
24593 return;
24594
24595 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24596
24597 it->descent = slice.height - glyph_ascent;
24598 if (slice.y == 0)
24599 it->descent += img->vmargin;
24600 if (slice.y + slice.height == img->height)
24601 it->descent += img->vmargin;
24602 it->phys_descent = it->descent;
24603
24604 it->pixel_width = slice.width;
24605 if (slice.x == 0)
24606 it->pixel_width += img->hmargin;
24607 if (slice.x + slice.width == img->width)
24608 it->pixel_width += img->hmargin;
24609
24610 /* It's quite possible for images to have an ascent greater than
24611 their height, so don't get confused in that case. */
24612 if (it->descent < 0)
24613 it->descent = 0;
24614
24615 it->nglyphs = 1;
24616
24617 if (face->box != FACE_NO_BOX)
24618 {
24619 if (face->box_line_width > 0)
24620 {
24621 if (slice.y == 0)
24622 it->ascent += face->box_line_width;
24623 if (slice.y + slice.height == img->height)
24624 it->descent += face->box_line_width;
24625 }
24626
24627 if (it->start_of_box_run_p && slice.x == 0)
24628 it->pixel_width += eabs (face->box_line_width);
24629 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24630 it->pixel_width += eabs (face->box_line_width);
24631 }
24632
24633 take_vertical_position_into_account (it);
24634
24635 /* Automatically crop wide image glyphs at right edge so we can
24636 draw the cursor on same display row. */
24637 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24638 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24639 {
24640 it->pixel_width -= crop;
24641 slice.width -= crop;
24642 }
24643
24644 if (it->glyph_row)
24645 {
24646 struct glyph *glyph;
24647 enum glyph_row_area area = it->area;
24648
24649 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24650 if (glyph < it->glyph_row->glyphs[area + 1])
24651 {
24652 glyph->charpos = CHARPOS (it->position);
24653 glyph->object = it->object;
24654 glyph->pixel_width = it->pixel_width;
24655 glyph->ascent = glyph_ascent;
24656 glyph->descent = it->descent;
24657 glyph->voffset = it->voffset;
24658 glyph->type = IMAGE_GLYPH;
24659 glyph->avoid_cursor_p = it->avoid_cursor_p;
24660 glyph->multibyte_p = it->multibyte_p;
24661 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24662 {
24663 /* In R2L rows, the left and the right box edges need to be
24664 drawn in reverse direction. */
24665 glyph->right_box_line_p = it->start_of_box_run_p;
24666 glyph->left_box_line_p = it->end_of_box_run_p;
24667 }
24668 else
24669 {
24670 glyph->left_box_line_p = it->start_of_box_run_p;
24671 glyph->right_box_line_p = it->end_of_box_run_p;
24672 }
24673 glyph->overlaps_vertically_p = 0;
24674 glyph->padding_p = 0;
24675 glyph->glyph_not_available_p = 0;
24676 glyph->face_id = it->face_id;
24677 glyph->u.img_id = img->id;
24678 glyph->slice.img = slice;
24679 glyph->font_type = FONT_TYPE_UNKNOWN;
24680 if (it->bidi_p)
24681 {
24682 glyph->resolved_level = it->bidi_it.resolved_level;
24683 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24684 emacs_abort ();
24685 glyph->bidi_type = it->bidi_it.type;
24686 }
24687 ++it->glyph_row->used[area];
24688 }
24689 else
24690 IT_EXPAND_MATRIX_WIDTH (it, area);
24691 }
24692 }
24693
24694
24695 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24696 of the glyph, WIDTH and HEIGHT are the width and height of the
24697 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24698
24699 static void
24700 append_stretch_glyph (struct it *it, Lisp_Object object,
24701 int width, int height, int ascent)
24702 {
24703 struct glyph *glyph;
24704 enum glyph_row_area area = it->area;
24705
24706 eassert (ascent >= 0 && ascent <= height);
24707
24708 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24709 if (glyph < it->glyph_row->glyphs[area + 1])
24710 {
24711 /* If the glyph row is reversed, we need to prepend the glyph
24712 rather than append it. */
24713 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24714 {
24715 struct glyph *g;
24716
24717 /* Make room for the additional glyph. */
24718 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24719 g[1] = *g;
24720 glyph = it->glyph_row->glyphs[area];
24721 }
24722 glyph->charpos = CHARPOS (it->position);
24723 glyph->object = object;
24724 glyph->pixel_width = width;
24725 glyph->ascent = ascent;
24726 glyph->descent = height - ascent;
24727 glyph->voffset = it->voffset;
24728 glyph->type = STRETCH_GLYPH;
24729 glyph->avoid_cursor_p = it->avoid_cursor_p;
24730 glyph->multibyte_p = it->multibyte_p;
24731 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24732 {
24733 /* In R2L rows, the left and the right box edges need to be
24734 drawn in reverse direction. */
24735 glyph->right_box_line_p = it->start_of_box_run_p;
24736 glyph->left_box_line_p = it->end_of_box_run_p;
24737 }
24738 else
24739 {
24740 glyph->left_box_line_p = it->start_of_box_run_p;
24741 glyph->right_box_line_p = it->end_of_box_run_p;
24742 }
24743 glyph->overlaps_vertically_p = 0;
24744 glyph->padding_p = 0;
24745 glyph->glyph_not_available_p = 0;
24746 glyph->face_id = it->face_id;
24747 glyph->u.stretch.ascent = ascent;
24748 glyph->u.stretch.height = height;
24749 glyph->slice.img = null_glyph_slice;
24750 glyph->font_type = FONT_TYPE_UNKNOWN;
24751 if (it->bidi_p)
24752 {
24753 glyph->resolved_level = it->bidi_it.resolved_level;
24754 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24755 emacs_abort ();
24756 glyph->bidi_type = it->bidi_it.type;
24757 }
24758 else
24759 {
24760 glyph->resolved_level = 0;
24761 glyph->bidi_type = UNKNOWN_BT;
24762 }
24763 ++it->glyph_row->used[area];
24764 }
24765 else
24766 IT_EXPAND_MATRIX_WIDTH (it, area);
24767 }
24768
24769 #endif /* HAVE_WINDOW_SYSTEM */
24770
24771 /* Produce a stretch glyph for iterator IT. IT->object is the value
24772 of the glyph property displayed. The value must be a list
24773 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24774 being recognized:
24775
24776 1. `:width WIDTH' specifies that the space should be WIDTH *
24777 canonical char width wide. WIDTH may be an integer or floating
24778 point number.
24779
24780 2. `:relative-width FACTOR' specifies that the width of the stretch
24781 should be computed from the width of the first character having the
24782 `glyph' property, and should be FACTOR times that width.
24783
24784 3. `:align-to HPOS' specifies that the space should be wide enough
24785 to reach HPOS, a value in canonical character units.
24786
24787 Exactly one of the above pairs must be present.
24788
24789 4. `:height HEIGHT' specifies that the height of the stretch produced
24790 should be HEIGHT, measured in canonical character units.
24791
24792 5. `:relative-height FACTOR' specifies that the height of the
24793 stretch should be FACTOR times the height of the characters having
24794 the glyph property.
24795
24796 Either none or exactly one of 4 or 5 must be present.
24797
24798 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24799 of the stretch should be used for the ascent of the stretch.
24800 ASCENT must be in the range 0 <= ASCENT <= 100. */
24801
24802 void
24803 produce_stretch_glyph (struct it *it)
24804 {
24805 /* (space :width WIDTH :height HEIGHT ...) */
24806 Lisp_Object prop, plist;
24807 int width = 0, height = 0, align_to = -1;
24808 int zero_width_ok_p = 0;
24809 double tem;
24810 struct font *font = NULL;
24811
24812 #ifdef HAVE_WINDOW_SYSTEM
24813 int ascent = 0;
24814 int zero_height_ok_p = 0;
24815
24816 if (FRAME_WINDOW_P (it->f))
24817 {
24818 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24819 font = face->font ? face->font : FRAME_FONT (it->f);
24820 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24821 }
24822 #endif
24823
24824 /* List should start with `space'. */
24825 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24826 plist = XCDR (it->object);
24827
24828 /* Compute the width of the stretch. */
24829 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24830 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24831 {
24832 /* Absolute width `:width WIDTH' specified and valid. */
24833 zero_width_ok_p = 1;
24834 width = (int)tem;
24835 }
24836 #ifdef HAVE_WINDOW_SYSTEM
24837 else if (FRAME_WINDOW_P (it->f)
24838 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24839 {
24840 /* Relative width `:relative-width FACTOR' specified and valid.
24841 Compute the width of the characters having the `glyph'
24842 property. */
24843 struct it it2;
24844 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24845
24846 it2 = *it;
24847 if (it->multibyte_p)
24848 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24849 else
24850 {
24851 it2.c = it2.char_to_display = *p, it2.len = 1;
24852 if (! ASCII_CHAR_P (it2.c))
24853 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24854 }
24855
24856 it2.glyph_row = NULL;
24857 it2.what = IT_CHARACTER;
24858 x_produce_glyphs (&it2);
24859 width = NUMVAL (prop) * it2.pixel_width;
24860 }
24861 #endif /* HAVE_WINDOW_SYSTEM */
24862 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24863 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24864 {
24865 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24866 align_to = (align_to < 0
24867 ? 0
24868 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24869 else if (align_to < 0)
24870 align_to = window_box_left_offset (it->w, TEXT_AREA);
24871 width = max (0, (int)tem + align_to - it->current_x);
24872 zero_width_ok_p = 1;
24873 }
24874 else
24875 /* Nothing specified -> width defaults to canonical char width. */
24876 width = FRAME_COLUMN_WIDTH (it->f);
24877
24878 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24879 width = 1;
24880
24881 #ifdef HAVE_WINDOW_SYSTEM
24882 /* Compute height. */
24883 if (FRAME_WINDOW_P (it->f))
24884 {
24885 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24886 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24887 {
24888 height = (int)tem;
24889 zero_height_ok_p = 1;
24890 }
24891 else if (prop = Fplist_get (plist, QCrelative_height),
24892 NUMVAL (prop) > 0)
24893 height = FONT_HEIGHT (font) * NUMVAL (prop);
24894 else
24895 height = FONT_HEIGHT (font);
24896
24897 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24898 height = 1;
24899
24900 /* Compute percentage of height used for ascent. If
24901 `:ascent ASCENT' is present and valid, use that. Otherwise,
24902 derive the ascent from the font in use. */
24903 if (prop = Fplist_get (plist, QCascent),
24904 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24905 ascent = height * NUMVAL (prop) / 100.0;
24906 else if (!NILP (prop)
24907 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24908 ascent = min (max (0, (int)tem), height);
24909 else
24910 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24911 }
24912 else
24913 #endif /* HAVE_WINDOW_SYSTEM */
24914 height = 1;
24915
24916 if (width > 0 && it->line_wrap != TRUNCATE
24917 && it->current_x + width > it->last_visible_x)
24918 {
24919 width = it->last_visible_x - it->current_x;
24920 #ifdef HAVE_WINDOW_SYSTEM
24921 /* Subtract one more pixel from the stretch width, but only on
24922 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24923 width -= FRAME_WINDOW_P (it->f);
24924 #endif
24925 }
24926
24927 if (width > 0 && height > 0 && it->glyph_row)
24928 {
24929 Lisp_Object o_object = it->object;
24930 Lisp_Object object = it->stack[it->sp - 1].string;
24931 int n = width;
24932
24933 if (!STRINGP (object))
24934 object = it->w->contents;
24935 #ifdef HAVE_WINDOW_SYSTEM
24936 if (FRAME_WINDOW_P (it->f))
24937 append_stretch_glyph (it, object, width, height, ascent);
24938 else
24939 #endif
24940 {
24941 it->object = object;
24942 it->char_to_display = ' ';
24943 it->pixel_width = it->len = 1;
24944 while (n--)
24945 tty_append_glyph (it);
24946 it->object = o_object;
24947 }
24948 }
24949
24950 it->pixel_width = width;
24951 #ifdef HAVE_WINDOW_SYSTEM
24952 if (FRAME_WINDOW_P (it->f))
24953 {
24954 it->ascent = it->phys_ascent = ascent;
24955 it->descent = it->phys_descent = height - it->ascent;
24956 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24957 take_vertical_position_into_account (it);
24958 }
24959 else
24960 #endif
24961 it->nglyphs = width;
24962 }
24963
24964 /* Get information about special display element WHAT in an
24965 environment described by IT. WHAT is one of IT_TRUNCATION or
24966 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24967 non-null glyph_row member. This function ensures that fields like
24968 face_id, c, len of IT are left untouched. */
24969
24970 static void
24971 produce_special_glyphs (struct it *it, enum display_element_type what)
24972 {
24973 struct it temp_it;
24974 Lisp_Object gc;
24975 GLYPH glyph;
24976
24977 temp_it = *it;
24978 temp_it.object = make_number (0);
24979 memset (&temp_it.current, 0, sizeof temp_it.current);
24980
24981 if (what == IT_CONTINUATION)
24982 {
24983 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24984 if (it->bidi_it.paragraph_dir == R2L)
24985 SET_GLYPH_FROM_CHAR (glyph, '/');
24986 else
24987 SET_GLYPH_FROM_CHAR (glyph, '\\');
24988 if (it->dp
24989 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24990 {
24991 /* FIXME: Should we mirror GC for R2L lines? */
24992 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24993 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24994 }
24995 }
24996 else if (what == IT_TRUNCATION)
24997 {
24998 /* Truncation glyph. */
24999 SET_GLYPH_FROM_CHAR (glyph, '$');
25000 if (it->dp
25001 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25002 {
25003 /* FIXME: Should we mirror GC for R2L lines? */
25004 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25005 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25006 }
25007 }
25008 else
25009 emacs_abort ();
25010
25011 #ifdef HAVE_WINDOW_SYSTEM
25012 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25013 is turned off, we precede the truncation/continuation glyphs by a
25014 stretch glyph whose width is computed such that these special
25015 glyphs are aligned at the window margin, even when very different
25016 fonts are used in different glyph rows. */
25017 if (FRAME_WINDOW_P (temp_it.f)
25018 /* init_iterator calls this with it->glyph_row == NULL, and it
25019 wants only the pixel width of the truncation/continuation
25020 glyphs. */
25021 && temp_it.glyph_row
25022 /* insert_left_trunc_glyphs calls us at the beginning of the
25023 row, and it has its own calculation of the stretch glyph
25024 width. */
25025 && temp_it.glyph_row->used[TEXT_AREA] > 0
25026 && (temp_it.glyph_row->reversed_p
25027 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25028 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25029 {
25030 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25031
25032 if (stretch_width > 0)
25033 {
25034 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25035 struct font *font =
25036 face->font ? face->font : FRAME_FONT (temp_it.f);
25037 int stretch_ascent =
25038 (((temp_it.ascent + temp_it.descent)
25039 * FONT_BASE (font)) / FONT_HEIGHT (font));
25040
25041 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25042 temp_it.ascent + temp_it.descent,
25043 stretch_ascent);
25044 }
25045 }
25046 #endif
25047
25048 temp_it.dp = NULL;
25049 temp_it.what = IT_CHARACTER;
25050 temp_it.len = 1;
25051 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25052 temp_it.face_id = GLYPH_FACE (glyph);
25053 temp_it.len = CHAR_BYTES (temp_it.c);
25054
25055 PRODUCE_GLYPHS (&temp_it);
25056 it->pixel_width = temp_it.pixel_width;
25057 it->nglyphs = temp_it.pixel_width;
25058 }
25059
25060 #ifdef HAVE_WINDOW_SYSTEM
25061
25062 /* Calculate line-height and line-spacing properties.
25063 An integer value specifies explicit pixel value.
25064 A float value specifies relative value to current face height.
25065 A cons (float . face-name) specifies relative value to
25066 height of specified face font.
25067
25068 Returns height in pixels, or nil. */
25069
25070
25071 static Lisp_Object
25072 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25073 int boff, int override)
25074 {
25075 Lisp_Object face_name = Qnil;
25076 int ascent, descent, height;
25077
25078 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25079 return val;
25080
25081 if (CONSP (val))
25082 {
25083 face_name = XCAR (val);
25084 val = XCDR (val);
25085 if (!NUMBERP (val))
25086 val = make_number (1);
25087 if (NILP (face_name))
25088 {
25089 height = it->ascent + it->descent;
25090 goto scale;
25091 }
25092 }
25093
25094 if (NILP (face_name))
25095 {
25096 font = FRAME_FONT (it->f);
25097 boff = FRAME_BASELINE_OFFSET (it->f);
25098 }
25099 else if (EQ (face_name, Qt))
25100 {
25101 override = 0;
25102 }
25103 else
25104 {
25105 int face_id;
25106 struct face *face;
25107
25108 face_id = lookup_named_face (it->f, face_name, 0);
25109 if (face_id < 0)
25110 return make_number (-1);
25111
25112 face = FACE_FROM_ID (it->f, face_id);
25113 font = face->font;
25114 if (font == NULL)
25115 return make_number (-1);
25116 boff = font->baseline_offset;
25117 if (font->vertical_centering)
25118 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25119 }
25120
25121 ascent = FONT_BASE (font) + boff;
25122 descent = FONT_DESCENT (font) - boff;
25123
25124 if (override)
25125 {
25126 it->override_ascent = ascent;
25127 it->override_descent = descent;
25128 it->override_boff = boff;
25129 }
25130
25131 height = ascent + descent;
25132
25133 scale:
25134 if (FLOATP (val))
25135 height = (int)(XFLOAT_DATA (val) * height);
25136 else if (INTEGERP (val))
25137 height *= XINT (val);
25138
25139 return make_number (height);
25140 }
25141
25142
25143 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25144 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25145 and only if this is for a character for which no font was found.
25146
25147 If the display method (it->glyphless_method) is
25148 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25149 length of the acronym or the hexadecimal string, UPPER_XOFF and
25150 UPPER_YOFF are pixel offsets for the upper part of the string,
25151 LOWER_XOFF and LOWER_YOFF are for the lower part.
25152
25153 For the other display methods, LEN through LOWER_YOFF are zero. */
25154
25155 static void
25156 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25157 short upper_xoff, short upper_yoff,
25158 short lower_xoff, short lower_yoff)
25159 {
25160 struct glyph *glyph;
25161 enum glyph_row_area area = it->area;
25162
25163 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25164 if (glyph < it->glyph_row->glyphs[area + 1])
25165 {
25166 /* If the glyph row is reversed, we need to prepend the glyph
25167 rather than append it. */
25168 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25169 {
25170 struct glyph *g;
25171
25172 /* Make room for the additional glyph. */
25173 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25174 g[1] = *g;
25175 glyph = it->glyph_row->glyphs[area];
25176 }
25177 glyph->charpos = CHARPOS (it->position);
25178 glyph->object = it->object;
25179 glyph->pixel_width = it->pixel_width;
25180 glyph->ascent = it->ascent;
25181 glyph->descent = it->descent;
25182 glyph->voffset = it->voffset;
25183 glyph->type = GLYPHLESS_GLYPH;
25184 glyph->u.glyphless.method = it->glyphless_method;
25185 glyph->u.glyphless.for_no_font = for_no_font;
25186 glyph->u.glyphless.len = len;
25187 glyph->u.glyphless.ch = it->c;
25188 glyph->slice.glyphless.upper_xoff = upper_xoff;
25189 glyph->slice.glyphless.upper_yoff = upper_yoff;
25190 glyph->slice.glyphless.lower_xoff = lower_xoff;
25191 glyph->slice.glyphless.lower_yoff = lower_yoff;
25192 glyph->avoid_cursor_p = it->avoid_cursor_p;
25193 glyph->multibyte_p = it->multibyte_p;
25194 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25195 {
25196 /* In R2L rows, the left and the right box edges need to be
25197 drawn in reverse direction. */
25198 glyph->right_box_line_p = it->start_of_box_run_p;
25199 glyph->left_box_line_p = it->end_of_box_run_p;
25200 }
25201 else
25202 {
25203 glyph->left_box_line_p = it->start_of_box_run_p;
25204 glyph->right_box_line_p = it->end_of_box_run_p;
25205 }
25206 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25207 || it->phys_descent > it->descent);
25208 glyph->padding_p = 0;
25209 glyph->glyph_not_available_p = 0;
25210 glyph->face_id = face_id;
25211 glyph->font_type = FONT_TYPE_UNKNOWN;
25212 if (it->bidi_p)
25213 {
25214 glyph->resolved_level = it->bidi_it.resolved_level;
25215 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25216 emacs_abort ();
25217 glyph->bidi_type = it->bidi_it.type;
25218 }
25219 ++it->glyph_row->used[area];
25220 }
25221 else
25222 IT_EXPAND_MATRIX_WIDTH (it, area);
25223 }
25224
25225
25226 /* Produce a glyph for a glyphless character for iterator IT.
25227 IT->glyphless_method specifies which method to use for displaying
25228 the character. See the description of enum
25229 glyphless_display_method in dispextern.h for the detail.
25230
25231 FOR_NO_FONT is nonzero if and only if this is for a character for
25232 which no font was found. ACRONYM, if non-nil, is an acronym string
25233 for the character. */
25234
25235 static void
25236 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25237 {
25238 int face_id;
25239 struct face *face;
25240 struct font *font;
25241 int base_width, base_height, width, height;
25242 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25243 int len;
25244
25245 /* Get the metrics of the base font. We always refer to the current
25246 ASCII face. */
25247 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25248 font = face->font ? face->font : FRAME_FONT (it->f);
25249 it->ascent = FONT_BASE (font) + font->baseline_offset;
25250 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25251 base_height = it->ascent + it->descent;
25252 base_width = font->average_width;
25253
25254 face_id = merge_glyphless_glyph_face (it);
25255
25256 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25257 {
25258 it->pixel_width = THIN_SPACE_WIDTH;
25259 len = 0;
25260 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25261 }
25262 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25263 {
25264 width = CHAR_WIDTH (it->c);
25265 if (width == 0)
25266 width = 1;
25267 else if (width > 4)
25268 width = 4;
25269 it->pixel_width = base_width * width;
25270 len = 0;
25271 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25272 }
25273 else
25274 {
25275 char buf[7];
25276 const char *str;
25277 unsigned int code[6];
25278 int upper_len;
25279 int ascent, descent;
25280 struct font_metrics metrics_upper, metrics_lower;
25281
25282 face = FACE_FROM_ID (it->f, face_id);
25283 font = face->font ? face->font : FRAME_FONT (it->f);
25284 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25285
25286 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25287 {
25288 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25289 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25290 if (CONSP (acronym))
25291 acronym = XCAR (acronym);
25292 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25293 }
25294 else
25295 {
25296 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25297 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25298 str = buf;
25299 }
25300 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25301 code[len] = font->driver->encode_char (font, str[len]);
25302 upper_len = (len + 1) / 2;
25303 font->driver->text_extents (font, code, upper_len,
25304 &metrics_upper);
25305 font->driver->text_extents (font, code + upper_len, len - upper_len,
25306 &metrics_lower);
25307
25308
25309
25310 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25311 width = max (metrics_upper.width, metrics_lower.width) + 4;
25312 upper_xoff = upper_yoff = 2; /* the typical case */
25313 if (base_width >= width)
25314 {
25315 /* Align the upper to the left, the lower to the right. */
25316 it->pixel_width = base_width;
25317 lower_xoff = base_width - 2 - metrics_lower.width;
25318 }
25319 else
25320 {
25321 /* Center the shorter one. */
25322 it->pixel_width = width;
25323 if (metrics_upper.width >= metrics_lower.width)
25324 lower_xoff = (width - metrics_lower.width) / 2;
25325 else
25326 {
25327 /* FIXME: This code doesn't look right. It formerly was
25328 missing the "lower_xoff = 0;", which couldn't have
25329 been right since it left lower_xoff uninitialized. */
25330 lower_xoff = 0;
25331 upper_xoff = (width - metrics_upper.width) / 2;
25332 }
25333 }
25334
25335 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25336 top, bottom, and between upper and lower strings. */
25337 height = (metrics_upper.ascent + metrics_upper.descent
25338 + metrics_lower.ascent + metrics_lower.descent) + 5;
25339 /* Center vertically.
25340 H:base_height, D:base_descent
25341 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25342
25343 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25344 descent = D - H/2 + h/2;
25345 lower_yoff = descent - 2 - ld;
25346 upper_yoff = lower_yoff - la - 1 - ud; */
25347 ascent = - (it->descent - (base_height + height + 1) / 2);
25348 descent = it->descent - (base_height - height) / 2;
25349 lower_yoff = descent - 2 - metrics_lower.descent;
25350 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25351 - metrics_upper.descent);
25352 /* Don't make the height shorter than the base height. */
25353 if (height > base_height)
25354 {
25355 it->ascent = ascent;
25356 it->descent = descent;
25357 }
25358 }
25359
25360 it->phys_ascent = it->ascent;
25361 it->phys_descent = it->descent;
25362 if (it->glyph_row)
25363 append_glyphless_glyph (it, face_id, for_no_font, len,
25364 upper_xoff, upper_yoff,
25365 lower_xoff, lower_yoff);
25366 it->nglyphs = 1;
25367 take_vertical_position_into_account (it);
25368 }
25369
25370
25371 /* RIF:
25372 Produce glyphs/get display metrics for the display element IT is
25373 loaded with. See the description of struct it in dispextern.h
25374 for an overview of struct it. */
25375
25376 void
25377 x_produce_glyphs (struct it *it)
25378 {
25379 int extra_line_spacing = it->extra_line_spacing;
25380
25381 it->glyph_not_available_p = 0;
25382
25383 if (it->what == IT_CHARACTER)
25384 {
25385 XChar2b char2b;
25386 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25387 struct font *font = face->font;
25388 struct font_metrics *pcm = NULL;
25389 int boff; /* Baseline offset. */
25390
25391 if (font == NULL)
25392 {
25393 /* When no suitable font is found, display this character by
25394 the method specified in the first extra slot of
25395 Vglyphless_char_display. */
25396 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25397
25398 eassert (it->what == IT_GLYPHLESS);
25399 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25400 goto done;
25401 }
25402
25403 boff = font->baseline_offset;
25404 if (font->vertical_centering)
25405 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25406
25407 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25408 {
25409 int stretched_p;
25410
25411 it->nglyphs = 1;
25412
25413 if (it->override_ascent >= 0)
25414 {
25415 it->ascent = it->override_ascent;
25416 it->descent = it->override_descent;
25417 boff = it->override_boff;
25418 }
25419 else
25420 {
25421 it->ascent = FONT_BASE (font) + boff;
25422 it->descent = FONT_DESCENT (font) - boff;
25423 }
25424
25425 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25426 {
25427 pcm = get_per_char_metric (font, &char2b);
25428 if (pcm->width == 0
25429 && pcm->rbearing == 0 && pcm->lbearing == 0)
25430 pcm = NULL;
25431 }
25432
25433 if (pcm)
25434 {
25435 it->phys_ascent = pcm->ascent + boff;
25436 it->phys_descent = pcm->descent - boff;
25437 it->pixel_width = pcm->width;
25438 }
25439 else
25440 {
25441 it->glyph_not_available_p = 1;
25442 it->phys_ascent = it->ascent;
25443 it->phys_descent = it->descent;
25444 it->pixel_width = font->space_width;
25445 }
25446
25447 if (it->constrain_row_ascent_descent_p)
25448 {
25449 if (it->descent > it->max_descent)
25450 {
25451 it->ascent += it->descent - it->max_descent;
25452 it->descent = it->max_descent;
25453 }
25454 if (it->ascent > it->max_ascent)
25455 {
25456 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25457 it->ascent = it->max_ascent;
25458 }
25459 it->phys_ascent = min (it->phys_ascent, it->ascent);
25460 it->phys_descent = min (it->phys_descent, it->descent);
25461 extra_line_spacing = 0;
25462 }
25463
25464 /* If this is a space inside a region of text with
25465 `space-width' property, change its width. */
25466 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25467 if (stretched_p)
25468 it->pixel_width *= XFLOATINT (it->space_width);
25469
25470 /* If face has a box, add the box thickness to the character
25471 height. If character has a box line to the left and/or
25472 right, add the box line width to the character's width. */
25473 if (face->box != FACE_NO_BOX)
25474 {
25475 int thick = face->box_line_width;
25476
25477 if (thick > 0)
25478 {
25479 it->ascent += thick;
25480 it->descent += thick;
25481 }
25482 else
25483 thick = -thick;
25484
25485 if (it->start_of_box_run_p)
25486 it->pixel_width += thick;
25487 if (it->end_of_box_run_p)
25488 it->pixel_width += thick;
25489 }
25490
25491 /* If face has an overline, add the height of the overline
25492 (1 pixel) and a 1 pixel margin to the character height. */
25493 if (face->overline_p)
25494 it->ascent += overline_margin;
25495
25496 if (it->constrain_row_ascent_descent_p)
25497 {
25498 if (it->ascent > it->max_ascent)
25499 it->ascent = it->max_ascent;
25500 if (it->descent > it->max_descent)
25501 it->descent = it->max_descent;
25502 }
25503
25504 take_vertical_position_into_account (it);
25505
25506 /* If we have to actually produce glyphs, do it. */
25507 if (it->glyph_row)
25508 {
25509 if (stretched_p)
25510 {
25511 /* Translate a space with a `space-width' property
25512 into a stretch glyph. */
25513 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25514 / FONT_HEIGHT (font));
25515 append_stretch_glyph (it, it->object, it->pixel_width,
25516 it->ascent + it->descent, ascent);
25517 }
25518 else
25519 append_glyph (it);
25520
25521 /* If characters with lbearing or rbearing are displayed
25522 in this line, record that fact in a flag of the
25523 glyph row. This is used to optimize X output code. */
25524 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25525 it->glyph_row->contains_overlapping_glyphs_p = 1;
25526 }
25527 if (! stretched_p && it->pixel_width == 0)
25528 /* We assure that all visible glyphs have at least 1-pixel
25529 width. */
25530 it->pixel_width = 1;
25531 }
25532 else if (it->char_to_display == '\n')
25533 {
25534 /* A newline has no width, but we need the height of the
25535 line. But if previous part of the line sets a height,
25536 don't increase that height. */
25537
25538 Lisp_Object height;
25539 Lisp_Object total_height = Qnil;
25540
25541 it->override_ascent = -1;
25542 it->pixel_width = 0;
25543 it->nglyphs = 0;
25544
25545 height = get_it_property (it, Qline_height);
25546 /* Split (line-height total-height) list. */
25547 if (CONSP (height)
25548 && CONSP (XCDR (height))
25549 && NILP (XCDR (XCDR (height))))
25550 {
25551 total_height = XCAR (XCDR (height));
25552 height = XCAR (height);
25553 }
25554 height = calc_line_height_property (it, height, font, boff, 1);
25555
25556 if (it->override_ascent >= 0)
25557 {
25558 it->ascent = it->override_ascent;
25559 it->descent = it->override_descent;
25560 boff = it->override_boff;
25561 }
25562 else
25563 {
25564 it->ascent = FONT_BASE (font) + boff;
25565 it->descent = FONT_DESCENT (font) - boff;
25566 }
25567
25568 if (EQ (height, Qt))
25569 {
25570 if (it->descent > it->max_descent)
25571 {
25572 it->ascent += it->descent - it->max_descent;
25573 it->descent = it->max_descent;
25574 }
25575 if (it->ascent > it->max_ascent)
25576 {
25577 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25578 it->ascent = it->max_ascent;
25579 }
25580 it->phys_ascent = min (it->phys_ascent, it->ascent);
25581 it->phys_descent = min (it->phys_descent, it->descent);
25582 it->constrain_row_ascent_descent_p = 1;
25583 extra_line_spacing = 0;
25584 }
25585 else
25586 {
25587 Lisp_Object spacing;
25588
25589 it->phys_ascent = it->ascent;
25590 it->phys_descent = it->descent;
25591
25592 if ((it->max_ascent > 0 || it->max_descent > 0)
25593 && face->box != FACE_NO_BOX
25594 && face->box_line_width > 0)
25595 {
25596 it->ascent += face->box_line_width;
25597 it->descent += face->box_line_width;
25598 }
25599 if (!NILP (height)
25600 && XINT (height) > it->ascent + it->descent)
25601 it->ascent = XINT (height) - it->descent;
25602
25603 if (!NILP (total_height))
25604 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25605 else
25606 {
25607 spacing = get_it_property (it, Qline_spacing);
25608 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25609 }
25610 if (INTEGERP (spacing))
25611 {
25612 extra_line_spacing = XINT (spacing);
25613 if (!NILP (total_height))
25614 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25615 }
25616 }
25617 }
25618 else /* i.e. (it->char_to_display == '\t') */
25619 {
25620 if (font->space_width > 0)
25621 {
25622 int tab_width = it->tab_width * font->space_width;
25623 int x = it->current_x + it->continuation_lines_width;
25624 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25625
25626 /* If the distance from the current position to the next tab
25627 stop is less than a space character width, use the
25628 tab stop after that. */
25629 if (next_tab_x - x < font->space_width)
25630 next_tab_x += tab_width;
25631
25632 it->pixel_width = next_tab_x - x;
25633 it->nglyphs = 1;
25634 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25635 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25636
25637 if (it->glyph_row)
25638 {
25639 append_stretch_glyph (it, it->object, it->pixel_width,
25640 it->ascent + it->descent, it->ascent);
25641 }
25642 }
25643 else
25644 {
25645 it->pixel_width = 0;
25646 it->nglyphs = 1;
25647 }
25648 }
25649 }
25650 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25651 {
25652 /* A static composition.
25653
25654 Note: A composition is represented as one glyph in the
25655 glyph matrix. There are no padding glyphs.
25656
25657 Important note: pixel_width, ascent, and descent are the
25658 values of what is drawn by draw_glyphs (i.e. the values of
25659 the overall glyphs composed). */
25660 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25661 int boff; /* baseline offset */
25662 struct composition *cmp = composition_table[it->cmp_it.id];
25663 int glyph_len = cmp->glyph_len;
25664 struct font *font = face->font;
25665
25666 it->nglyphs = 1;
25667
25668 /* If we have not yet calculated pixel size data of glyphs of
25669 the composition for the current face font, calculate them
25670 now. Theoretically, we have to check all fonts for the
25671 glyphs, but that requires much time and memory space. So,
25672 here we check only the font of the first glyph. This may
25673 lead to incorrect display, but it's very rare, and C-l
25674 (recenter-top-bottom) can correct the display anyway. */
25675 if (! cmp->font || cmp->font != font)
25676 {
25677 /* Ascent and descent of the font of the first character
25678 of this composition (adjusted by baseline offset).
25679 Ascent and descent of overall glyphs should not be less
25680 than these, respectively. */
25681 int font_ascent, font_descent, font_height;
25682 /* Bounding box of the overall glyphs. */
25683 int leftmost, rightmost, lowest, highest;
25684 int lbearing, rbearing;
25685 int i, width, ascent, descent;
25686 int left_padded = 0, right_padded = 0;
25687 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25688 XChar2b char2b;
25689 struct font_metrics *pcm;
25690 int font_not_found_p;
25691 ptrdiff_t pos;
25692
25693 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25694 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25695 break;
25696 if (glyph_len < cmp->glyph_len)
25697 right_padded = 1;
25698 for (i = 0; i < glyph_len; i++)
25699 {
25700 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25701 break;
25702 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25703 }
25704 if (i > 0)
25705 left_padded = 1;
25706
25707 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25708 : IT_CHARPOS (*it));
25709 /* If no suitable font is found, use the default font. */
25710 font_not_found_p = font == NULL;
25711 if (font_not_found_p)
25712 {
25713 face = face->ascii_face;
25714 font = face->font;
25715 }
25716 boff = font->baseline_offset;
25717 if (font->vertical_centering)
25718 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25719 font_ascent = FONT_BASE (font) + boff;
25720 font_descent = FONT_DESCENT (font) - boff;
25721 font_height = FONT_HEIGHT (font);
25722
25723 cmp->font = font;
25724
25725 pcm = NULL;
25726 if (! font_not_found_p)
25727 {
25728 get_char_face_and_encoding (it->f, c, it->face_id,
25729 &char2b, 0);
25730 pcm = get_per_char_metric (font, &char2b);
25731 }
25732
25733 /* Initialize the bounding box. */
25734 if (pcm)
25735 {
25736 width = cmp->glyph_len > 0 ? pcm->width : 0;
25737 ascent = pcm->ascent;
25738 descent = pcm->descent;
25739 lbearing = pcm->lbearing;
25740 rbearing = pcm->rbearing;
25741 }
25742 else
25743 {
25744 width = cmp->glyph_len > 0 ? font->space_width : 0;
25745 ascent = FONT_BASE (font);
25746 descent = FONT_DESCENT (font);
25747 lbearing = 0;
25748 rbearing = width;
25749 }
25750
25751 rightmost = width;
25752 leftmost = 0;
25753 lowest = - descent + boff;
25754 highest = ascent + boff;
25755
25756 if (! font_not_found_p
25757 && font->default_ascent
25758 && CHAR_TABLE_P (Vuse_default_ascent)
25759 && !NILP (Faref (Vuse_default_ascent,
25760 make_number (it->char_to_display))))
25761 highest = font->default_ascent + boff;
25762
25763 /* Draw the first glyph at the normal position. It may be
25764 shifted to right later if some other glyphs are drawn
25765 at the left. */
25766 cmp->offsets[i * 2] = 0;
25767 cmp->offsets[i * 2 + 1] = boff;
25768 cmp->lbearing = lbearing;
25769 cmp->rbearing = rbearing;
25770
25771 /* Set cmp->offsets for the remaining glyphs. */
25772 for (i++; i < glyph_len; i++)
25773 {
25774 int left, right, btm, top;
25775 int ch = COMPOSITION_GLYPH (cmp, i);
25776 int face_id;
25777 struct face *this_face;
25778
25779 if (ch == '\t')
25780 ch = ' ';
25781 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25782 this_face = FACE_FROM_ID (it->f, face_id);
25783 font = this_face->font;
25784
25785 if (font == NULL)
25786 pcm = NULL;
25787 else
25788 {
25789 get_char_face_and_encoding (it->f, ch, face_id,
25790 &char2b, 0);
25791 pcm = get_per_char_metric (font, &char2b);
25792 }
25793 if (! pcm)
25794 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25795 else
25796 {
25797 width = pcm->width;
25798 ascent = pcm->ascent;
25799 descent = pcm->descent;
25800 lbearing = pcm->lbearing;
25801 rbearing = pcm->rbearing;
25802 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25803 {
25804 /* Relative composition with or without
25805 alternate chars. */
25806 left = (leftmost + rightmost - width) / 2;
25807 btm = - descent + boff;
25808 if (font->relative_compose
25809 && (! CHAR_TABLE_P (Vignore_relative_composition)
25810 || NILP (Faref (Vignore_relative_composition,
25811 make_number (ch)))))
25812 {
25813
25814 if (- descent >= font->relative_compose)
25815 /* One extra pixel between two glyphs. */
25816 btm = highest + 1;
25817 else if (ascent <= 0)
25818 /* One extra pixel between two glyphs. */
25819 btm = lowest - 1 - ascent - descent;
25820 }
25821 }
25822 else
25823 {
25824 /* A composition rule is specified by an integer
25825 value that encodes global and new reference
25826 points (GREF and NREF). GREF and NREF are
25827 specified by numbers as below:
25828
25829 0---1---2 -- ascent
25830 | |
25831 | |
25832 | |
25833 9--10--11 -- center
25834 | |
25835 ---3---4---5--- baseline
25836 | |
25837 6---7---8 -- descent
25838 */
25839 int rule = COMPOSITION_RULE (cmp, i);
25840 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25841
25842 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25843 grefx = gref % 3, nrefx = nref % 3;
25844 grefy = gref / 3, nrefy = nref / 3;
25845 if (xoff)
25846 xoff = font_height * (xoff - 128) / 256;
25847 if (yoff)
25848 yoff = font_height * (yoff - 128) / 256;
25849
25850 left = (leftmost
25851 + grefx * (rightmost - leftmost) / 2
25852 - nrefx * width / 2
25853 + xoff);
25854
25855 btm = ((grefy == 0 ? highest
25856 : grefy == 1 ? 0
25857 : grefy == 2 ? lowest
25858 : (highest + lowest) / 2)
25859 - (nrefy == 0 ? ascent + descent
25860 : nrefy == 1 ? descent - boff
25861 : nrefy == 2 ? 0
25862 : (ascent + descent) / 2)
25863 + yoff);
25864 }
25865
25866 cmp->offsets[i * 2] = left;
25867 cmp->offsets[i * 2 + 1] = btm + descent;
25868
25869 /* Update the bounding box of the overall glyphs. */
25870 if (width > 0)
25871 {
25872 right = left + width;
25873 if (left < leftmost)
25874 leftmost = left;
25875 if (right > rightmost)
25876 rightmost = right;
25877 }
25878 top = btm + descent + ascent;
25879 if (top > highest)
25880 highest = top;
25881 if (btm < lowest)
25882 lowest = btm;
25883
25884 if (cmp->lbearing > left + lbearing)
25885 cmp->lbearing = left + lbearing;
25886 if (cmp->rbearing < left + rbearing)
25887 cmp->rbearing = left + rbearing;
25888 }
25889 }
25890
25891 /* If there are glyphs whose x-offsets are negative,
25892 shift all glyphs to the right and make all x-offsets
25893 non-negative. */
25894 if (leftmost < 0)
25895 {
25896 for (i = 0; i < cmp->glyph_len; i++)
25897 cmp->offsets[i * 2] -= leftmost;
25898 rightmost -= leftmost;
25899 cmp->lbearing -= leftmost;
25900 cmp->rbearing -= leftmost;
25901 }
25902
25903 if (left_padded && cmp->lbearing < 0)
25904 {
25905 for (i = 0; i < cmp->glyph_len; i++)
25906 cmp->offsets[i * 2] -= cmp->lbearing;
25907 rightmost -= cmp->lbearing;
25908 cmp->rbearing -= cmp->lbearing;
25909 cmp->lbearing = 0;
25910 }
25911 if (right_padded && rightmost < cmp->rbearing)
25912 {
25913 rightmost = cmp->rbearing;
25914 }
25915
25916 cmp->pixel_width = rightmost;
25917 cmp->ascent = highest;
25918 cmp->descent = - lowest;
25919 if (cmp->ascent < font_ascent)
25920 cmp->ascent = font_ascent;
25921 if (cmp->descent < font_descent)
25922 cmp->descent = font_descent;
25923 }
25924
25925 if (it->glyph_row
25926 && (cmp->lbearing < 0
25927 || cmp->rbearing > cmp->pixel_width))
25928 it->glyph_row->contains_overlapping_glyphs_p = 1;
25929
25930 it->pixel_width = cmp->pixel_width;
25931 it->ascent = it->phys_ascent = cmp->ascent;
25932 it->descent = it->phys_descent = cmp->descent;
25933 if (face->box != FACE_NO_BOX)
25934 {
25935 int thick = face->box_line_width;
25936
25937 if (thick > 0)
25938 {
25939 it->ascent += thick;
25940 it->descent += thick;
25941 }
25942 else
25943 thick = - thick;
25944
25945 if (it->start_of_box_run_p)
25946 it->pixel_width += thick;
25947 if (it->end_of_box_run_p)
25948 it->pixel_width += thick;
25949 }
25950
25951 /* If face has an overline, add the height of the overline
25952 (1 pixel) and a 1 pixel margin to the character height. */
25953 if (face->overline_p)
25954 it->ascent += overline_margin;
25955
25956 take_vertical_position_into_account (it);
25957 if (it->ascent < 0)
25958 it->ascent = 0;
25959 if (it->descent < 0)
25960 it->descent = 0;
25961
25962 if (it->glyph_row && cmp->glyph_len > 0)
25963 append_composite_glyph (it);
25964 }
25965 else if (it->what == IT_COMPOSITION)
25966 {
25967 /* A dynamic (automatic) composition. */
25968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25969 Lisp_Object gstring;
25970 struct font_metrics metrics;
25971
25972 it->nglyphs = 1;
25973
25974 gstring = composition_gstring_from_id (it->cmp_it.id);
25975 it->pixel_width
25976 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25977 &metrics);
25978 if (it->glyph_row
25979 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25980 it->glyph_row->contains_overlapping_glyphs_p = 1;
25981 it->ascent = it->phys_ascent = metrics.ascent;
25982 it->descent = it->phys_descent = metrics.descent;
25983 if (face->box != FACE_NO_BOX)
25984 {
25985 int thick = face->box_line_width;
25986
25987 if (thick > 0)
25988 {
25989 it->ascent += thick;
25990 it->descent += thick;
25991 }
25992 else
25993 thick = - thick;
25994
25995 if (it->start_of_box_run_p)
25996 it->pixel_width += thick;
25997 if (it->end_of_box_run_p)
25998 it->pixel_width += thick;
25999 }
26000 /* If face has an overline, add the height of the overline
26001 (1 pixel) and a 1 pixel margin to the character height. */
26002 if (face->overline_p)
26003 it->ascent += overline_margin;
26004 take_vertical_position_into_account (it);
26005 if (it->ascent < 0)
26006 it->ascent = 0;
26007 if (it->descent < 0)
26008 it->descent = 0;
26009
26010 if (it->glyph_row)
26011 append_composite_glyph (it);
26012 }
26013 else if (it->what == IT_GLYPHLESS)
26014 produce_glyphless_glyph (it, 0, Qnil);
26015 else if (it->what == IT_IMAGE)
26016 produce_image_glyph (it);
26017 else if (it->what == IT_STRETCH)
26018 produce_stretch_glyph (it);
26019
26020 done:
26021 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26022 because this isn't true for images with `:ascent 100'. */
26023 eassert (it->ascent >= 0 && it->descent >= 0);
26024 if (it->area == TEXT_AREA)
26025 it->current_x += it->pixel_width;
26026
26027 if (extra_line_spacing > 0)
26028 {
26029 it->descent += extra_line_spacing;
26030 if (extra_line_spacing > it->max_extra_line_spacing)
26031 it->max_extra_line_spacing = extra_line_spacing;
26032 }
26033
26034 it->max_ascent = max (it->max_ascent, it->ascent);
26035 it->max_descent = max (it->max_descent, it->descent);
26036 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26037 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26038 }
26039
26040 /* EXPORT for RIF:
26041 Output LEN glyphs starting at START at the nominal cursor position.
26042 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26043 being updated, and UPDATED_AREA is the area of that row being updated. */
26044
26045 void
26046 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26047 struct glyph *start, enum glyph_row_area updated_area, int len)
26048 {
26049 int x, hpos, chpos = w->phys_cursor.hpos;
26050
26051 eassert (updated_row);
26052 /* When the window is hscrolled, cursor hpos can legitimately be out
26053 of bounds, but we draw the cursor at the corresponding window
26054 margin in that case. */
26055 if (!updated_row->reversed_p && chpos < 0)
26056 chpos = 0;
26057 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26058 chpos = updated_row->used[TEXT_AREA] - 1;
26059
26060 block_input ();
26061
26062 /* Write glyphs. */
26063
26064 hpos = start - updated_row->glyphs[updated_area];
26065 x = draw_glyphs (w, w->output_cursor.x,
26066 updated_row, updated_area,
26067 hpos, hpos + len,
26068 DRAW_NORMAL_TEXT, 0);
26069
26070 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26071 if (updated_area == TEXT_AREA
26072 && w->phys_cursor_on_p
26073 && w->phys_cursor.vpos == w->output_cursor.vpos
26074 && chpos >= hpos
26075 && chpos < hpos + len)
26076 w->phys_cursor_on_p = 0;
26077
26078 unblock_input ();
26079
26080 /* Advance the output cursor. */
26081 w->output_cursor.hpos += len;
26082 w->output_cursor.x = x;
26083 }
26084
26085
26086 /* EXPORT for RIF:
26087 Insert LEN glyphs from START at the nominal cursor position. */
26088
26089 void
26090 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26091 struct glyph *start, enum glyph_row_area updated_area, int len)
26092 {
26093 struct frame *f;
26094 int line_height, shift_by_width, shifted_region_width;
26095 struct glyph_row *row;
26096 struct glyph *glyph;
26097 int frame_x, frame_y;
26098 ptrdiff_t hpos;
26099
26100 eassert (updated_row);
26101 block_input ();
26102 f = XFRAME (WINDOW_FRAME (w));
26103
26104 /* Get the height of the line we are in. */
26105 row = updated_row;
26106 line_height = row->height;
26107
26108 /* Get the width of the glyphs to insert. */
26109 shift_by_width = 0;
26110 for (glyph = start; glyph < start + len; ++glyph)
26111 shift_by_width += glyph->pixel_width;
26112
26113 /* Get the width of the region to shift right. */
26114 shifted_region_width = (window_box_width (w, updated_area)
26115 - w->output_cursor.x
26116 - shift_by_width);
26117
26118 /* Shift right. */
26119 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26120 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26121
26122 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26123 line_height, shift_by_width);
26124
26125 /* Write the glyphs. */
26126 hpos = start - row->glyphs[updated_area];
26127 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26128 hpos, hpos + len,
26129 DRAW_NORMAL_TEXT, 0);
26130
26131 /* Advance the output cursor. */
26132 w->output_cursor.hpos += len;
26133 w->output_cursor.x += shift_by_width;
26134 unblock_input ();
26135 }
26136
26137
26138 /* EXPORT for RIF:
26139 Erase the current text line from the nominal cursor position
26140 (inclusive) to pixel column TO_X (exclusive). The idea is that
26141 everything from TO_X onward is already erased.
26142
26143 TO_X is a pixel position relative to UPDATED_AREA of currently
26144 updated window W. TO_X == -1 means clear to the end of this area. */
26145
26146 void
26147 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26148 enum glyph_row_area updated_area, int to_x)
26149 {
26150 struct frame *f;
26151 int max_x, min_y, max_y;
26152 int from_x, from_y, to_y;
26153
26154 eassert (updated_row);
26155 f = XFRAME (w->frame);
26156
26157 if (updated_row->full_width_p)
26158 max_x = (WINDOW_PIXEL_WIDTH (w)
26159 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26160 else
26161 max_x = window_box_width (w, updated_area);
26162 max_y = window_text_bottom_y (w);
26163
26164 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26165 of window. For TO_X > 0, truncate to end of drawing area. */
26166 if (to_x == 0)
26167 return;
26168 else if (to_x < 0)
26169 to_x = max_x;
26170 else
26171 to_x = min (to_x, max_x);
26172
26173 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26174
26175 /* Notice if the cursor will be cleared by this operation. */
26176 if (!updated_row->full_width_p)
26177 notice_overwritten_cursor (w, updated_area,
26178 w->output_cursor.x, -1,
26179 updated_row->y,
26180 MATRIX_ROW_BOTTOM_Y (updated_row));
26181
26182 from_x = w->output_cursor.x;
26183
26184 /* Translate to frame coordinates. */
26185 if (updated_row->full_width_p)
26186 {
26187 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26188 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26189 }
26190 else
26191 {
26192 int area_left = window_box_left (w, updated_area);
26193 from_x += area_left;
26194 to_x += area_left;
26195 }
26196
26197 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26198 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26199 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26200
26201 /* Prevent inadvertently clearing to end of the X window. */
26202 if (to_x > from_x && to_y > from_y)
26203 {
26204 block_input ();
26205 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26206 to_x - from_x, to_y - from_y);
26207 unblock_input ();
26208 }
26209 }
26210
26211 #endif /* HAVE_WINDOW_SYSTEM */
26212
26213
26214 \f
26215 /***********************************************************************
26216 Cursor types
26217 ***********************************************************************/
26218
26219 /* Value is the internal representation of the specified cursor type
26220 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26221 of the bar cursor. */
26222
26223 static enum text_cursor_kinds
26224 get_specified_cursor_type (Lisp_Object arg, int *width)
26225 {
26226 enum text_cursor_kinds type;
26227
26228 if (NILP (arg))
26229 return NO_CURSOR;
26230
26231 if (EQ (arg, Qbox))
26232 return FILLED_BOX_CURSOR;
26233
26234 if (EQ (arg, Qhollow))
26235 return HOLLOW_BOX_CURSOR;
26236
26237 if (EQ (arg, Qbar))
26238 {
26239 *width = 2;
26240 return BAR_CURSOR;
26241 }
26242
26243 if (CONSP (arg)
26244 && EQ (XCAR (arg), Qbar)
26245 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26246 {
26247 *width = XINT (XCDR (arg));
26248 return BAR_CURSOR;
26249 }
26250
26251 if (EQ (arg, Qhbar))
26252 {
26253 *width = 2;
26254 return HBAR_CURSOR;
26255 }
26256
26257 if (CONSP (arg)
26258 && EQ (XCAR (arg), Qhbar)
26259 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26260 {
26261 *width = XINT (XCDR (arg));
26262 return HBAR_CURSOR;
26263 }
26264
26265 /* Treat anything unknown as "hollow box cursor".
26266 It was bad to signal an error; people have trouble fixing
26267 .Xdefaults with Emacs, when it has something bad in it. */
26268 type = HOLLOW_BOX_CURSOR;
26269
26270 return type;
26271 }
26272
26273 /* Set the default cursor types for specified frame. */
26274 void
26275 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26276 {
26277 int width = 1;
26278 Lisp_Object tem;
26279
26280 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26281 FRAME_CURSOR_WIDTH (f) = width;
26282
26283 /* By default, set up the blink-off state depending on the on-state. */
26284
26285 tem = Fassoc (arg, Vblink_cursor_alist);
26286 if (!NILP (tem))
26287 {
26288 FRAME_BLINK_OFF_CURSOR (f)
26289 = get_specified_cursor_type (XCDR (tem), &width);
26290 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26291 }
26292 else
26293 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26294
26295 /* Make sure the cursor gets redrawn. */
26296 f->cursor_type_changed = 1;
26297 }
26298
26299
26300 #ifdef HAVE_WINDOW_SYSTEM
26301
26302 /* Return the cursor we want to be displayed in window W. Return
26303 width of bar/hbar cursor through WIDTH arg. Return with
26304 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26305 (i.e. if the `system caret' should track this cursor).
26306
26307 In a mini-buffer window, we want the cursor only to appear if we
26308 are reading input from this window. For the selected window, we
26309 want the cursor type given by the frame parameter or buffer local
26310 setting of cursor-type. If explicitly marked off, draw no cursor.
26311 In all other cases, we want a hollow box cursor. */
26312
26313 static enum text_cursor_kinds
26314 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26315 int *active_cursor)
26316 {
26317 struct frame *f = XFRAME (w->frame);
26318 struct buffer *b = XBUFFER (w->contents);
26319 int cursor_type = DEFAULT_CURSOR;
26320 Lisp_Object alt_cursor;
26321 int non_selected = 0;
26322
26323 *active_cursor = 1;
26324
26325 /* Echo area */
26326 if (cursor_in_echo_area
26327 && FRAME_HAS_MINIBUF_P (f)
26328 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26329 {
26330 if (w == XWINDOW (echo_area_window))
26331 {
26332 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26333 {
26334 *width = FRAME_CURSOR_WIDTH (f);
26335 return FRAME_DESIRED_CURSOR (f);
26336 }
26337 else
26338 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26339 }
26340
26341 *active_cursor = 0;
26342 non_selected = 1;
26343 }
26344
26345 /* Detect a nonselected window or nonselected frame. */
26346 else if (w != XWINDOW (f->selected_window)
26347 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26348 {
26349 *active_cursor = 0;
26350
26351 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26352 return NO_CURSOR;
26353
26354 non_selected = 1;
26355 }
26356
26357 /* Never display a cursor in a window in which cursor-type is nil. */
26358 if (NILP (BVAR (b, cursor_type)))
26359 return NO_CURSOR;
26360
26361 /* Get the normal cursor type for this window. */
26362 if (EQ (BVAR (b, cursor_type), Qt))
26363 {
26364 cursor_type = FRAME_DESIRED_CURSOR (f);
26365 *width = FRAME_CURSOR_WIDTH (f);
26366 }
26367 else
26368 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26369
26370 /* Use cursor-in-non-selected-windows instead
26371 for non-selected window or frame. */
26372 if (non_selected)
26373 {
26374 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26375 if (!EQ (Qt, alt_cursor))
26376 return get_specified_cursor_type (alt_cursor, width);
26377 /* t means modify the normal cursor type. */
26378 if (cursor_type == FILLED_BOX_CURSOR)
26379 cursor_type = HOLLOW_BOX_CURSOR;
26380 else if (cursor_type == BAR_CURSOR && *width > 1)
26381 --*width;
26382 return cursor_type;
26383 }
26384
26385 /* Use normal cursor if not blinked off. */
26386 if (!w->cursor_off_p)
26387 {
26388 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26389 {
26390 if (cursor_type == FILLED_BOX_CURSOR)
26391 {
26392 /* Using a block cursor on large images can be very annoying.
26393 So use a hollow cursor for "large" images.
26394 If image is not transparent (no mask), also use hollow cursor. */
26395 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26396 if (img != NULL && IMAGEP (img->spec))
26397 {
26398 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26399 where N = size of default frame font size.
26400 This should cover most of the "tiny" icons people may use. */
26401 if (!img->mask
26402 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26403 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26404 cursor_type = HOLLOW_BOX_CURSOR;
26405 }
26406 }
26407 else if (cursor_type != NO_CURSOR)
26408 {
26409 /* Display current only supports BOX and HOLLOW cursors for images.
26410 So for now, unconditionally use a HOLLOW cursor when cursor is
26411 not a solid box cursor. */
26412 cursor_type = HOLLOW_BOX_CURSOR;
26413 }
26414 }
26415 return cursor_type;
26416 }
26417
26418 /* Cursor is blinked off, so determine how to "toggle" it. */
26419
26420 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26421 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26422 return get_specified_cursor_type (XCDR (alt_cursor), width);
26423
26424 /* Then see if frame has specified a specific blink off cursor type. */
26425 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26426 {
26427 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26428 return FRAME_BLINK_OFF_CURSOR (f);
26429 }
26430
26431 #if 0
26432 /* Some people liked having a permanently visible blinking cursor,
26433 while others had very strong opinions against it. So it was
26434 decided to remove it. KFS 2003-09-03 */
26435
26436 /* Finally perform built-in cursor blinking:
26437 filled box <-> hollow box
26438 wide [h]bar <-> narrow [h]bar
26439 narrow [h]bar <-> no cursor
26440 other type <-> no cursor */
26441
26442 if (cursor_type == FILLED_BOX_CURSOR)
26443 return HOLLOW_BOX_CURSOR;
26444
26445 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26446 {
26447 *width = 1;
26448 return cursor_type;
26449 }
26450 #endif
26451
26452 return NO_CURSOR;
26453 }
26454
26455
26456 /* Notice when the text cursor of window W has been completely
26457 overwritten by a drawing operation that outputs glyphs in AREA
26458 starting at X0 and ending at X1 in the line starting at Y0 and
26459 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26460 the rest of the line after X0 has been written. Y coordinates
26461 are window-relative. */
26462
26463 static void
26464 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26465 int x0, int x1, int y0, int y1)
26466 {
26467 int cx0, cx1, cy0, cy1;
26468 struct glyph_row *row;
26469
26470 if (!w->phys_cursor_on_p)
26471 return;
26472 if (area != TEXT_AREA)
26473 return;
26474
26475 if (w->phys_cursor.vpos < 0
26476 || w->phys_cursor.vpos >= w->current_matrix->nrows
26477 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26478 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26479 return;
26480
26481 if (row->cursor_in_fringe_p)
26482 {
26483 row->cursor_in_fringe_p = 0;
26484 draw_fringe_bitmap (w, row, row->reversed_p);
26485 w->phys_cursor_on_p = 0;
26486 return;
26487 }
26488
26489 cx0 = w->phys_cursor.x;
26490 cx1 = cx0 + w->phys_cursor_width;
26491 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26492 return;
26493
26494 /* The cursor image will be completely removed from the
26495 screen if the output area intersects the cursor area in
26496 y-direction. When we draw in [y0 y1[, and some part of
26497 the cursor is at y < y0, that part must have been drawn
26498 before. When scrolling, the cursor is erased before
26499 actually scrolling, so we don't come here. When not
26500 scrolling, the rows above the old cursor row must have
26501 changed, and in this case these rows must have written
26502 over the cursor image.
26503
26504 Likewise if part of the cursor is below y1, with the
26505 exception of the cursor being in the first blank row at
26506 the buffer and window end because update_text_area
26507 doesn't draw that row. (Except when it does, but
26508 that's handled in update_text_area.) */
26509
26510 cy0 = w->phys_cursor.y;
26511 cy1 = cy0 + w->phys_cursor_height;
26512 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26513 return;
26514
26515 w->phys_cursor_on_p = 0;
26516 }
26517
26518 #endif /* HAVE_WINDOW_SYSTEM */
26519
26520 \f
26521 /************************************************************************
26522 Mouse Face
26523 ************************************************************************/
26524
26525 #ifdef HAVE_WINDOW_SYSTEM
26526
26527 /* EXPORT for RIF:
26528 Fix the display of area AREA of overlapping row ROW in window W
26529 with respect to the overlapping part OVERLAPS. */
26530
26531 void
26532 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26533 enum glyph_row_area area, int overlaps)
26534 {
26535 int i, x;
26536
26537 block_input ();
26538
26539 x = 0;
26540 for (i = 0; i < row->used[area];)
26541 {
26542 if (row->glyphs[area][i].overlaps_vertically_p)
26543 {
26544 int start = i, start_x = x;
26545
26546 do
26547 {
26548 x += row->glyphs[area][i].pixel_width;
26549 ++i;
26550 }
26551 while (i < row->used[area]
26552 && row->glyphs[area][i].overlaps_vertically_p);
26553
26554 draw_glyphs (w, start_x, row, area,
26555 start, i,
26556 DRAW_NORMAL_TEXT, overlaps);
26557 }
26558 else
26559 {
26560 x += row->glyphs[area][i].pixel_width;
26561 ++i;
26562 }
26563 }
26564
26565 unblock_input ();
26566 }
26567
26568
26569 /* EXPORT:
26570 Draw the cursor glyph of window W in glyph row ROW. See the
26571 comment of draw_glyphs for the meaning of HL. */
26572
26573 void
26574 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26575 enum draw_glyphs_face hl)
26576 {
26577 /* If cursor hpos is out of bounds, don't draw garbage. This can
26578 happen in mini-buffer windows when switching between echo area
26579 glyphs and mini-buffer. */
26580 if ((row->reversed_p
26581 ? (w->phys_cursor.hpos >= 0)
26582 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26583 {
26584 int on_p = w->phys_cursor_on_p;
26585 int x1;
26586 int hpos = w->phys_cursor.hpos;
26587
26588 /* When the window is hscrolled, cursor hpos can legitimately be
26589 out of bounds, but we draw the cursor at the corresponding
26590 window margin in that case. */
26591 if (!row->reversed_p && hpos < 0)
26592 hpos = 0;
26593 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26594 hpos = row->used[TEXT_AREA] - 1;
26595
26596 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26597 hl, 0);
26598 w->phys_cursor_on_p = on_p;
26599
26600 if (hl == DRAW_CURSOR)
26601 w->phys_cursor_width = x1 - w->phys_cursor.x;
26602 /* When we erase the cursor, and ROW is overlapped by other
26603 rows, make sure that these overlapping parts of other rows
26604 are redrawn. */
26605 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26606 {
26607 w->phys_cursor_width = x1 - w->phys_cursor.x;
26608
26609 if (row > w->current_matrix->rows
26610 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26611 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26612 OVERLAPS_ERASED_CURSOR);
26613
26614 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26615 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26616 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26617 OVERLAPS_ERASED_CURSOR);
26618 }
26619 }
26620 }
26621
26622
26623 /* Erase the image of a cursor of window W from the screen. */
26624
26625 #ifndef HAVE_NTGUI
26626 static
26627 #endif
26628 void
26629 erase_phys_cursor (struct window *w)
26630 {
26631 struct frame *f = XFRAME (w->frame);
26632 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26633 int hpos = w->phys_cursor.hpos;
26634 int vpos = w->phys_cursor.vpos;
26635 int mouse_face_here_p = 0;
26636 struct glyph_matrix *active_glyphs = w->current_matrix;
26637 struct glyph_row *cursor_row;
26638 struct glyph *cursor_glyph;
26639 enum draw_glyphs_face hl;
26640
26641 /* No cursor displayed or row invalidated => nothing to do on the
26642 screen. */
26643 if (w->phys_cursor_type == NO_CURSOR)
26644 goto mark_cursor_off;
26645
26646 /* VPOS >= active_glyphs->nrows means that window has been resized.
26647 Don't bother to erase the cursor. */
26648 if (vpos >= active_glyphs->nrows)
26649 goto mark_cursor_off;
26650
26651 /* If row containing cursor is marked invalid, there is nothing we
26652 can do. */
26653 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26654 if (!cursor_row->enabled_p)
26655 goto mark_cursor_off;
26656
26657 /* If line spacing is > 0, old cursor may only be partially visible in
26658 window after split-window. So adjust visible height. */
26659 cursor_row->visible_height = min (cursor_row->visible_height,
26660 window_text_bottom_y (w) - cursor_row->y);
26661
26662 /* If row is completely invisible, don't attempt to delete a cursor which
26663 isn't there. This can happen if cursor is at top of a window, and
26664 we switch to a buffer with a header line in that window. */
26665 if (cursor_row->visible_height <= 0)
26666 goto mark_cursor_off;
26667
26668 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26669 if (cursor_row->cursor_in_fringe_p)
26670 {
26671 cursor_row->cursor_in_fringe_p = 0;
26672 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26673 goto mark_cursor_off;
26674 }
26675
26676 /* This can happen when the new row is shorter than the old one.
26677 In this case, either draw_glyphs or clear_end_of_line
26678 should have cleared the cursor. Note that we wouldn't be
26679 able to erase the cursor in this case because we don't have a
26680 cursor glyph at hand. */
26681 if ((cursor_row->reversed_p
26682 ? (w->phys_cursor.hpos < 0)
26683 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26684 goto mark_cursor_off;
26685
26686 /* When the window is hscrolled, cursor hpos can legitimately be out
26687 of bounds, but we draw the cursor at the corresponding window
26688 margin in that case. */
26689 if (!cursor_row->reversed_p && hpos < 0)
26690 hpos = 0;
26691 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26692 hpos = cursor_row->used[TEXT_AREA] - 1;
26693
26694 /* If the cursor is in the mouse face area, redisplay that when
26695 we clear the cursor. */
26696 if (! NILP (hlinfo->mouse_face_window)
26697 && coords_in_mouse_face_p (w, hpos, vpos)
26698 /* Don't redraw the cursor's spot in mouse face if it is at the
26699 end of a line (on a newline). The cursor appears there, but
26700 mouse highlighting does not. */
26701 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26702 mouse_face_here_p = 1;
26703
26704 /* Maybe clear the display under the cursor. */
26705 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26706 {
26707 int x, y, left_x;
26708 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26709 int width;
26710
26711 cursor_glyph = get_phys_cursor_glyph (w);
26712 if (cursor_glyph == NULL)
26713 goto mark_cursor_off;
26714
26715 width = cursor_glyph->pixel_width;
26716 left_x = window_box_left_offset (w, TEXT_AREA);
26717 x = w->phys_cursor.x;
26718 if (x < left_x)
26719 width -= left_x - x;
26720 width = min (width, window_box_width (w, TEXT_AREA) - x);
26721 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26722 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26723
26724 if (width > 0)
26725 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26726 }
26727
26728 /* Erase the cursor by redrawing the character underneath it. */
26729 if (mouse_face_here_p)
26730 hl = DRAW_MOUSE_FACE;
26731 else
26732 hl = DRAW_NORMAL_TEXT;
26733 draw_phys_cursor_glyph (w, cursor_row, hl);
26734
26735 mark_cursor_off:
26736 w->phys_cursor_on_p = 0;
26737 w->phys_cursor_type = NO_CURSOR;
26738 }
26739
26740
26741 /* EXPORT:
26742 Display or clear cursor of window W. If ON is zero, clear the
26743 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26744 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26745
26746 void
26747 display_and_set_cursor (struct window *w, bool on,
26748 int hpos, int vpos, int x, int y)
26749 {
26750 struct frame *f = XFRAME (w->frame);
26751 int new_cursor_type;
26752 int new_cursor_width;
26753 int active_cursor;
26754 struct glyph_row *glyph_row;
26755 struct glyph *glyph;
26756
26757 /* This is pointless on invisible frames, and dangerous on garbaged
26758 windows and frames; in the latter case, the frame or window may
26759 be in the midst of changing its size, and x and y may be off the
26760 window. */
26761 if (! FRAME_VISIBLE_P (f)
26762 || FRAME_GARBAGED_P (f)
26763 || vpos >= w->current_matrix->nrows
26764 || hpos >= w->current_matrix->matrix_w)
26765 return;
26766
26767 /* If cursor is off and we want it off, return quickly. */
26768 if (!on && !w->phys_cursor_on_p)
26769 return;
26770
26771 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26772 /* If cursor row is not enabled, we don't really know where to
26773 display the cursor. */
26774 if (!glyph_row->enabled_p)
26775 {
26776 w->phys_cursor_on_p = 0;
26777 return;
26778 }
26779
26780 glyph = NULL;
26781 if (!glyph_row->exact_window_width_line_p
26782 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26783 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26784
26785 eassert (input_blocked_p ());
26786
26787 /* Set new_cursor_type to the cursor we want to be displayed. */
26788 new_cursor_type = get_window_cursor_type (w, glyph,
26789 &new_cursor_width, &active_cursor);
26790
26791 /* If cursor is currently being shown and we don't want it to be or
26792 it is in the wrong place, or the cursor type is not what we want,
26793 erase it. */
26794 if (w->phys_cursor_on_p
26795 && (!on
26796 || w->phys_cursor.x != x
26797 || w->phys_cursor.y != y
26798 || new_cursor_type != w->phys_cursor_type
26799 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26800 && new_cursor_width != w->phys_cursor_width)))
26801 erase_phys_cursor (w);
26802
26803 /* Don't check phys_cursor_on_p here because that flag is only set
26804 to zero in some cases where we know that the cursor has been
26805 completely erased, to avoid the extra work of erasing the cursor
26806 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26807 still not be visible, or it has only been partly erased. */
26808 if (on)
26809 {
26810 w->phys_cursor_ascent = glyph_row->ascent;
26811 w->phys_cursor_height = glyph_row->height;
26812
26813 /* Set phys_cursor_.* before x_draw_.* is called because some
26814 of them may need the information. */
26815 w->phys_cursor.x = x;
26816 w->phys_cursor.y = glyph_row->y;
26817 w->phys_cursor.hpos = hpos;
26818 w->phys_cursor.vpos = vpos;
26819 }
26820
26821 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26822 new_cursor_type, new_cursor_width,
26823 on, active_cursor);
26824 }
26825
26826
26827 /* Switch the display of W's cursor on or off, according to the value
26828 of ON. */
26829
26830 static void
26831 update_window_cursor (struct window *w, bool on)
26832 {
26833 /* Don't update cursor in windows whose frame is in the process
26834 of being deleted. */
26835 if (w->current_matrix)
26836 {
26837 int hpos = w->phys_cursor.hpos;
26838 int vpos = w->phys_cursor.vpos;
26839 struct glyph_row *row;
26840
26841 if (vpos >= w->current_matrix->nrows
26842 || hpos >= w->current_matrix->matrix_w)
26843 return;
26844
26845 row = MATRIX_ROW (w->current_matrix, vpos);
26846
26847 /* When the window is hscrolled, cursor hpos can legitimately be
26848 out of bounds, but we draw the cursor at the corresponding
26849 window margin in that case. */
26850 if (!row->reversed_p && hpos < 0)
26851 hpos = 0;
26852 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26853 hpos = row->used[TEXT_AREA] - 1;
26854
26855 block_input ();
26856 display_and_set_cursor (w, on, hpos, vpos,
26857 w->phys_cursor.x, w->phys_cursor.y);
26858 unblock_input ();
26859 }
26860 }
26861
26862
26863 /* Call update_window_cursor with parameter ON_P on all leaf windows
26864 in the window tree rooted at W. */
26865
26866 static void
26867 update_cursor_in_window_tree (struct window *w, bool on_p)
26868 {
26869 while (w)
26870 {
26871 if (WINDOWP (w->contents))
26872 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26873 else
26874 update_window_cursor (w, on_p);
26875
26876 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26877 }
26878 }
26879
26880
26881 /* EXPORT:
26882 Display the cursor on window W, or clear it, according to ON_P.
26883 Don't change the cursor's position. */
26884
26885 void
26886 x_update_cursor (struct frame *f, bool on_p)
26887 {
26888 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26889 }
26890
26891
26892 /* EXPORT:
26893 Clear the cursor of window W to background color, and mark the
26894 cursor as not shown. This is used when the text where the cursor
26895 is about to be rewritten. */
26896
26897 void
26898 x_clear_cursor (struct window *w)
26899 {
26900 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26901 update_window_cursor (w, 0);
26902 }
26903
26904 #endif /* HAVE_WINDOW_SYSTEM */
26905
26906 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26907 and MSDOS. */
26908 static void
26909 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26910 int start_hpos, int end_hpos,
26911 enum draw_glyphs_face draw)
26912 {
26913 #ifdef HAVE_WINDOW_SYSTEM
26914 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26915 {
26916 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26917 return;
26918 }
26919 #endif
26920 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26921 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26922 #endif
26923 }
26924
26925 /* Display the active region described by mouse_face_* according to DRAW. */
26926
26927 static void
26928 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26929 {
26930 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26931 struct frame *f = XFRAME (WINDOW_FRAME (w));
26932
26933 if (/* If window is in the process of being destroyed, don't bother
26934 to do anything. */
26935 w->current_matrix != NULL
26936 /* Don't update mouse highlight if hidden */
26937 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26938 /* Recognize when we are called to operate on rows that don't exist
26939 anymore. This can happen when a window is split. */
26940 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26941 {
26942 int phys_cursor_on_p = w->phys_cursor_on_p;
26943 struct glyph_row *row, *first, *last;
26944
26945 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26946 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26947
26948 for (row = first; row <= last && row->enabled_p; ++row)
26949 {
26950 int start_hpos, end_hpos, start_x;
26951
26952 /* For all but the first row, the highlight starts at column 0. */
26953 if (row == first)
26954 {
26955 /* R2L rows have BEG and END in reversed order, but the
26956 screen drawing geometry is always left to right. So
26957 we need to mirror the beginning and end of the
26958 highlighted area in R2L rows. */
26959 if (!row->reversed_p)
26960 {
26961 start_hpos = hlinfo->mouse_face_beg_col;
26962 start_x = hlinfo->mouse_face_beg_x;
26963 }
26964 else if (row == last)
26965 {
26966 start_hpos = hlinfo->mouse_face_end_col;
26967 start_x = hlinfo->mouse_face_end_x;
26968 }
26969 else
26970 {
26971 start_hpos = 0;
26972 start_x = 0;
26973 }
26974 }
26975 else if (row->reversed_p && row == last)
26976 {
26977 start_hpos = hlinfo->mouse_face_end_col;
26978 start_x = hlinfo->mouse_face_end_x;
26979 }
26980 else
26981 {
26982 start_hpos = 0;
26983 start_x = 0;
26984 }
26985
26986 if (row == last)
26987 {
26988 if (!row->reversed_p)
26989 end_hpos = hlinfo->mouse_face_end_col;
26990 else if (row == first)
26991 end_hpos = hlinfo->mouse_face_beg_col;
26992 else
26993 {
26994 end_hpos = row->used[TEXT_AREA];
26995 if (draw == DRAW_NORMAL_TEXT)
26996 row->fill_line_p = 1; /* Clear to end of line */
26997 }
26998 }
26999 else if (row->reversed_p && row == first)
27000 end_hpos = hlinfo->mouse_face_beg_col;
27001 else
27002 {
27003 end_hpos = row->used[TEXT_AREA];
27004 if (draw == DRAW_NORMAL_TEXT)
27005 row->fill_line_p = 1; /* Clear to end of line */
27006 }
27007
27008 if (end_hpos > start_hpos)
27009 {
27010 draw_row_with_mouse_face (w, start_x, row,
27011 start_hpos, end_hpos, draw);
27012
27013 row->mouse_face_p
27014 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27015 }
27016 }
27017
27018 #ifdef HAVE_WINDOW_SYSTEM
27019 /* When we've written over the cursor, arrange for it to
27020 be displayed again. */
27021 if (FRAME_WINDOW_P (f)
27022 && phys_cursor_on_p && !w->phys_cursor_on_p)
27023 {
27024 int hpos = w->phys_cursor.hpos;
27025
27026 /* When the window is hscrolled, cursor hpos can legitimately be
27027 out of bounds, but we draw the cursor at the corresponding
27028 window margin in that case. */
27029 if (!row->reversed_p && hpos < 0)
27030 hpos = 0;
27031 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27032 hpos = row->used[TEXT_AREA] - 1;
27033
27034 block_input ();
27035 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27036 w->phys_cursor.x, w->phys_cursor.y);
27037 unblock_input ();
27038 }
27039 #endif /* HAVE_WINDOW_SYSTEM */
27040 }
27041
27042 #ifdef HAVE_WINDOW_SYSTEM
27043 /* Change the mouse cursor. */
27044 if (FRAME_WINDOW_P (f))
27045 {
27046 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27047 if (draw == DRAW_NORMAL_TEXT
27048 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27049 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27050 else
27051 #endif
27052 if (draw == DRAW_MOUSE_FACE)
27053 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27054 else
27055 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27056 }
27057 #endif /* HAVE_WINDOW_SYSTEM */
27058 }
27059
27060 /* EXPORT:
27061 Clear out the mouse-highlighted active region.
27062 Redraw it un-highlighted first. Value is non-zero if mouse
27063 face was actually drawn unhighlighted. */
27064
27065 int
27066 clear_mouse_face (Mouse_HLInfo *hlinfo)
27067 {
27068 int cleared = 0;
27069
27070 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27071 {
27072 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27073 cleared = 1;
27074 }
27075
27076 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27077 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27078 hlinfo->mouse_face_window = Qnil;
27079 hlinfo->mouse_face_overlay = Qnil;
27080 return cleared;
27081 }
27082
27083 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27084 within the mouse face on that window. */
27085 static int
27086 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27087 {
27088 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27089
27090 /* Quickly resolve the easy cases. */
27091 if (!(WINDOWP (hlinfo->mouse_face_window)
27092 && XWINDOW (hlinfo->mouse_face_window) == w))
27093 return 0;
27094 if (vpos < hlinfo->mouse_face_beg_row
27095 || vpos > hlinfo->mouse_face_end_row)
27096 return 0;
27097 if (vpos > hlinfo->mouse_face_beg_row
27098 && vpos < hlinfo->mouse_face_end_row)
27099 return 1;
27100
27101 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27102 {
27103 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27104 {
27105 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27106 return 1;
27107 }
27108 else if ((vpos == hlinfo->mouse_face_beg_row
27109 && hpos >= hlinfo->mouse_face_beg_col)
27110 || (vpos == hlinfo->mouse_face_end_row
27111 && hpos < hlinfo->mouse_face_end_col))
27112 return 1;
27113 }
27114 else
27115 {
27116 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27117 {
27118 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27119 return 1;
27120 }
27121 else if ((vpos == hlinfo->mouse_face_beg_row
27122 && hpos <= hlinfo->mouse_face_beg_col)
27123 || (vpos == hlinfo->mouse_face_end_row
27124 && hpos > hlinfo->mouse_face_end_col))
27125 return 1;
27126 }
27127 return 0;
27128 }
27129
27130
27131 /* EXPORT:
27132 Non-zero if physical cursor of window W is within mouse face. */
27133
27134 int
27135 cursor_in_mouse_face_p (struct window *w)
27136 {
27137 int hpos = w->phys_cursor.hpos;
27138 int vpos = w->phys_cursor.vpos;
27139 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27140
27141 /* When the window is hscrolled, cursor hpos can legitimately be out
27142 of bounds, but we draw the cursor at the corresponding window
27143 margin in that case. */
27144 if (!row->reversed_p && hpos < 0)
27145 hpos = 0;
27146 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27147 hpos = row->used[TEXT_AREA] - 1;
27148
27149 return coords_in_mouse_face_p (w, hpos, vpos);
27150 }
27151
27152
27153 \f
27154 /* Find the glyph rows START_ROW and END_ROW of window W that display
27155 characters between buffer positions START_CHARPOS and END_CHARPOS
27156 (excluding END_CHARPOS). DISP_STRING is a display string that
27157 covers these buffer positions. This is similar to
27158 row_containing_pos, but is more accurate when bidi reordering makes
27159 buffer positions change non-linearly with glyph rows. */
27160 static void
27161 rows_from_pos_range (struct window *w,
27162 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27163 Lisp_Object disp_string,
27164 struct glyph_row **start, struct glyph_row **end)
27165 {
27166 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27167 int last_y = window_text_bottom_y (w);
27168 struct glyph_row *row;
27169
27170 *start = NULL;
27171 *end = NULL;
27172
27173 while (!first->enabled_p
27174 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27175 first++;
27176
27177 /* Find the START row. */
27178 for (row = first;
27179 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27180 row++)
27181 {
27182 /* A row can potentially be the START row if the range of the
27183 characters it displays intersects the range
27184 [START_CHARPOS..END_CHARPOS). */
27185 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27186 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27187 /* See the commentary in row_containing_pos, for the
27188 explanation of the complicated way to check whether
27189 some position is beyond the end of the characters
27190 displayed by a row. */
27191 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27192 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27193 && !row->ends_at_zv_p
27194 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27195 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27196 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27197 && !row->ends_at_zv_p
27198 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27199 {
27200 /* Found a candidate row. Now make sure at least one of the
27201 glyphs it displays has a charpos from the range
27202 [START_CHARPOS..END_CHARPOS).
27203
27204 This is not obvious because bidi reordering could make
27205 buffer positions of a row be 1,2,3,102,101,100, and if we
27206 want to highlight characters in [50..60), we don't want
27207 this row, even though [50..60) does intersect [1..103),
27208 the range of character positions given by the row's start
27209 and end positions. */
27210 struct glyph *g = row->glyphs[TEXT_AREA];
27211 struct glyph *e = g + row->used[TEXT_AREA];
27212
27213 while (g < e)
27214 {
27215 if (((BUFFERP (g->object) || INTEGERP (g->object))
27216 && start_charpos <= g->charpos && g->charpos < end_charpos)
27217 /* A glyph that comes from DISP_STRING is by
27218 definition to be highlighted. */
27219 || EQ (g->object, disp_string))
27220 *start = row;
27221 g++;
27222 }
27223 if (*start)
27224 break;
27225 }
27226 }
27227
27228 /* Find the END row. */
27229 if (!*start
27230 /* If the last row is partially visible, start looking for END
27231 from that row, instead of starting from FIRST. */
27232 && !(row->enabled_p
27233 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27234 row = first;
27235 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27236 {
27237 struct glyph_row *next = row + 1;
27238 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27239
27240 if (!next->enabled_p
27241 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27242 /* The first row >= START whose range of displayed characters
27243 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27244 is the row END + 1. */
27245 || (start_charpos < next_start
27246 && end_charpos < next_start)
27247 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27248 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27249 && !next->ends_at_zv_p
27250 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27251 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27252 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27253 && !next->ends_at_zv_p
27254 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27255 {
27256 *end = row;
27257 break;
27258 }
27259 else
27260 {
27261 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27262 but none of the characters it displays are in the range, it is
27263 also END + 1. */
27264 struct glyph *g = next->glyphs[TEXT_AREA];
27265 struct glyph *s = g;
27266 struct glyph *e = g + next->used[TEXT_AREA];
27267
27268 while (g < e)
27269 {
27270 if (((BUFFERP (g->object) || INTEGERP (g->object))
27271 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27272 /* If the buffer position of the first glyph in
27273 the row is equal to END_CHARPOS, it means
27274 the last character to be highlighted is the
27275 newline of ROW, and we must consider NEXT as
27276 END, not END+1. */
27277 || (((!next->reversed_p && g == s)
27278 || (next->reversed_p && g == e - 1))
27279 && (g->charpos == end_charpos
27280 /* Special case for when NEXT is an
27281 empty line at ZV. */
27282 || (g->charpos == -1
27283 && !row->ends_at_zv_p
27284 && next_start == end_charpos)))))
27285 /* A glyph that comes from DISP_STRING is by
27286 definition to be highlighted. */
27287 || EQ (g->object, disp_string))
27288 break;
27289 g++;
27290 }
27291 if (g == e)
27292 {
27293 *end = row;
27294 break;
27295 }
27296 /* The first row that ends at ZV must be the last to be
27297 highlighted. */
27298 else if (next->ends_at_zv_p)
27299 {
27300 *end = next;
27301 break;
27302 }
27303 }
27304 }
27305 }
27306
27307 /* This function sets the mouse_face_* elements of HLINFO, assuming
27308 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27309 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27310 for the overlay or run of text properties specifying the mouse
27311 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27312 before-string and after-string that must also be highlighted.
27313 DISP_STRING, if non-nil, is a display string that may cover some
27314 or all of the highlighted text. */
27315
27316 static void
27317 mouse_face_from_buffer_pos (Lisp_Object window,
27318 Mouse_HLInfo *hlinfo,
27319 ptrdiff_t mouse_charpos,
27320 ptrdiff_t start_charpos,
27321 ptrdiff_t end_charpos,
27322 Lisp_Object before_string,
27323 Lisp_Object after_string,
27324 Lisp_Object disp_string)
27325 {
27326 struct window *w = XWINDOW (window);
27327 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27328 struct glyph_row *r1, *r2;
27329 struct glyph *glyph, *end;
27330 ptrdiff_t ignore, pos;
27331 int x;
27332
27333 eassert (NILP (disp_string) || STRINGP (disp_string));
27334 eassert (NILP (before_string) || STRINGP (before_string));
27335 eassert (NILP (after_string) || STRINGP (after_string));
27336
27337 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27338 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27339 if (r1 == NULL)
27340 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27341 /* If the before-string or display-string contains newlines,
27342 rows_from_pos_range skips to its last row. Move back. */
27343 if (!NILP (before_string) || !NILP (disp_string))
27344 {
27345 struct glyph_row *prev;
27346 while ((prev = r1 - 1, prev >= first)
27347 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27348 && prev->used[TEXT_AREA] > 0)
27349 {
27350 struct glyph *beg = prev->glyphs[TEXT_AREA];
27351 glyph = beg + prev->used[TEXT_AREA];
27352 while (--glyph >= beg && INTEGERP (glyph->object));
27353 if (glyph < beg
27354 || !(EQ (glyph->object, before_string)
27355 || EQ (glyph->object, disp_string)))
27356 break;
27357 r1 = prev;
27358 }
27359 }
27360 if (r2 == NULL)
27361 {
27362 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27363 hlinfo->mouse_face_past_end = 1;
27364 }
27365 else if (!NILP (after_string))
27366 {
27367 /* If the after-string has newlines, advance to its last row. */
27368 struct glyph_row *next;
27369 struct glyph_row *last
27370 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27371
27372 for (next = r2 + 1;
27373 next <= last
27374 && next->used[TEXT_AREA] > 0
27375 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27376 ++next)
27377 r2 = next;
27378 }
27379 /* The rest of the display engine assumes that mouse_face_beg_row is
27380 either above mouse_face_end_row or identical to it. But with
27381 bidi-reordered continued lines, the row for START_CHARPOS could
27382 be below the row for END_CHARPOS. If so, swap the rows and store
27383 them in correct order. */
27384 if (r1->y > r2->y)
27385 {
27386 struct glyph_row *tem = r2;
27387
27388 r2 = r1;
27389 r1 = tem;
27390 }
27391
27392 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27393 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27394
27395 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27396 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27397 could be anywhere in the row and in any order. The strategy
27398 below is to find the leftmost and the rightmost glyph that
27399 belongs to either of these 3 strings, or whose position is
27400 between START_CHARPOS and END_CHARPOS, and highlight all the
27401 glyphs between those two. This may cover more than just the text
27402 between START_CHARPOS and END_CHARPOS if the range of characters
27403 strides the bidi level boundary, e.g. if the beginning is in R2L
27404 text while the end is in L2R text or vice versa. */
27405 if (!r1->reversed_p)
27406 {
27407 /* This row is in a left to right paragraph. Scan it left to
27408 right. */
27409 glyph = r1->glyphs[TEXT_AREA];
27410 end = glyph + r1->used[TEXT_AREA];
27411 x = r1->x;
27412
27413 /* Skip truncation glyphs at the start of the glyph row. */
27414 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27415 for (; glyph < end
27416 && INTEGERP (glyph->object)
27417 && glyph->charpos < 0;
27418 ++glyph)
27419 x += glyph->pixel_width;
27420
27421 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27422 or DISP_STRING, and the first glyph from buffer whose
27423 position is between START_CHARPOS and END_CHARPOS. */
27424 for (; glyph < end
27425 && !INTEGERP (glyph->object)
27426 && !EQ (glyph->object, disp_string)
27427 && !(BUFFERP (glyph->object)
27428 && (glyph->charpos >= start_charpos
27429 && glyph->charpos < end_charpos));
27430 ++glyph)
27431 {
27432 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27433 are present at buffer positions between START_CHARPOS and
27434 END_CHARPOS, or if they come from an overlay. */
27435 if (EQ (glyph->object, before_string))
27436 {
27437 pos = string_buffer_position (before_string,
27438 start_charpos);
27439 /* If pos == 0, it means before_string came from an
27440 overlay, not from a buffer position. */
27441 if (!pos || (pos >= start_charpos && pos < end_charpos))
27442 break;
27443 }
27444 else if (EQ (glyph->object, after_string))
27445 {
27446 pos = string_buffer_position (after_string, end_charpos);
27447 if (!pos || (pos >= start_charpos && pos < end_charpos))
27448 break;
27449 }
27450 x += glyph->pixel_width;
27451 }
27452 hlinfo->mouse_face_beg_x = x;
27453 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27454 }
27455 else
27456 {
27457 /* This row is in a right to left paragraph. Scan it right to
27458 left. */
27459 struct glyph *g;
27460
27461 end = r1->glyphs[TEXT_AREA] - 1;
27462 glyph = end + r1->used[TEXT_AREA];
27463
27464 /* Skip truncation glyphs at the start of the glyph row. */
27465 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27466 for (; glyph > end
27467 && INTEGERP (glyph->object)
27468 && glyph->charpos < 0;
27469 --glyph)
27470 ;
27471
27472 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27473 or DISP_STRING, and the first glyph from buffer whose
27474 position is between START_CHARPOS and END_CHARPOS. */
27475 for (; glyph > end
27476 && !INTEGERP (glyph->object)
27477 && !EQ (glyph->object, disp_string)
27478 && !(BUFFERP (glyph->object)
27479 && (glyph->charpos >= start_charpos
27480 && glyph->charpos < end_charpos));
27481 --glyph)
27482 {
27483 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27484 are present at buffer positions between START_CHARPOS and
27485 END_CHARPOS, or if they come from an overlay. */
27486 if (EQ (glyph->object, before_string))
27487 {
27488 pos = string_buffer_position (before_string, start_charpos);
27489 /* If pos == 0, it means before_string came from an
27490 overlay, not from a buffer position. */
27491 if (!pos || (pos >= start_charpos && pos < end_charpos))
27492 break;
27493 }
27494 else if (EQ (glyph->object, after_string))
27495 {
27496 pos = string_buffer_position (after_string, end_charpos);
27497 if (!pos || (pos >= start_charpos && pos < end_charpos))
27498 break;
27499 }
27500 }
27501
27502 glyph++; /* first glyph to the right of the highlighted area */
27503 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27504 x += g->pixel_width;
27505 hlinfo->mouse_face_beg_x = x;
27506 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27507 }
27508
27509 /* If the highlight ends in a different row, compute GLYPH and END
27510 for the end row. Otherwise, reuse the values computed above for
27511 the row where the highlight begins. */
27512 if (r2 != r1)
27513 {
27514 if (!r2->reversed_p)
27515 {
27516 glyph = r2->glyphs[TEXT_AREA];
27517 end = glyph + r2->used[TEXT_AREA];
27518 x = r2->x;
27519 }
27520 else
27521 {
27522 end = r2->glyphs[TEXT_AREA] - 1;
27523 glyph = end + r2->used[TEXT_AREA];
27524 }
27525 }
27526
27527 if (!r2->reversed_p)
27528 {
27529 /* Skip truncation and continuation glyphs near the end of the
27530 row, and also blanks and stretch glyphs inserted by
27531 extend_face_to_end_of_line. */
27532 while (end > glyph
27533 && INTEGERP ((end - 1)->object))
27534 --end;
27535 /* Scan the rest of the glyph row from the end, looking for the
27536 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27537 DISP_STRING, or whose position is between START_CHARPOS
27538 and END_CHARPOS */
27539 for (--end;
27540 end > glyph
27541 && !INTEGERP (end->object)
27542 && !EQ (end->object, disp_string)
27543 && !(BUFFERP (end->object)
27544 && (end->charpos >= start_charpos
27545 && end->charpos < end_charpos));
27546 --end)
27547 {
27548 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27549 are present at buffer positions between START_CHARPOS and
27550 END_CHARPOS, or if they come from an overlay. */
27551 if (EQ (end->object, before_string))
27552 {
27553 pos = string_buffer_position (before_string, start_charpos);
27554 if (!pos || (pos >= start_charpos && pos < end_charpos))
27555 break;
27556 }
27557 else if (EQ (end->object, after_string))
27558 {
27559 pos = string_buffer_position (after_string, end_charpos);
27560 if (!pos || (pos >= start_charpos && pos < end_charpos))
27561 break;
27562 }
27563 }
27564 /* Find the X coordinate of the last glyph to be highlighted. */
27565 for (; glyph <= end; ++glyph)
27566 x += glyph->pixel_width;
27567
27568 hlinfo->mouse_face_end_x = x;
27569 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27570 }
27571 else
27572 {
27573 /* Skip truncation and continuation glyphs near the end of the
27574 row, and also blanks and stretch glyphs inserted by
27575 extend_face_to_end_of_line. */
27576 x = r2->x;
27577 end++;
27578 while (end < glyph
27579 && INTEGERP (end->object))
27580 {
27581 x += end->pixel_width;
27582 ++end;
27583 }
27584 /* Scan the rest of the glyph row from the end, looking for the
27585 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27586 DISP_STRING, or whose position is between START_CHARPOS
27587 and END_CHARPOS */
27588 for ( ;
27589 end < glyph
27590 && !INTEGERP (end->object)
27591 && !EQ (end->object, disp_string)
27592 && !(BUFFERP (end->object)
27593 && (end->charpos >= start_charpos
27594 && end->charpos < end_charpos));
27595 ++end)
27596 {
27597 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27598 are present at buffer positions between START_CHARPOS and
27599 END_CHARPOS, or if they come from an overlay. */
27600 if (EQ (end->object, before_string))
27601 {
27602 pos = string_buffer_position (before_string, start_charpos);
27603 if (!pos || (pos >= start_charpos && pos < end_charpos))
27604 break;
27605 }
27606 else if (EQ (end->object, after_string))
27607 {
27608 pos = string_buffer_position (after_string, end_charpos);
27609 if (!pos || (pos >= start_charpos && pos < end_charpos))
27610 break;
27611 }
27612 x += end->pixel_width;
27613 }
27614 /* If we exited the above loop because we arrived at the last
27615 glyph of the row, and its buffer position is still not in
27616 range, it means the last character in range is the preceding
27617 newline. Bump the end column and x values to get past the
27618 last glyph. */
27619 if (end == glyph
27620 && BUFFERP (end->object)
27621 && (end->charpos < start_charpos
27622 || end->charpos >= end_charpos))
27623 {
27624 x += end->pixel_width;
27625 ++end;
27626 }
27627 hlinfo->mouse_face_end_x = x;
27628 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27629 }
27630
27631 hlinfo->mouse_face_window = window;
27632 hlinfo->mouse_face_face_id
27633 = face_at_buffer_position (w, mouse_charpos, &ignore,
27634 mouse_charpos + 1,
27635 !hlinfo->mouse_face_hidden, -1);
27636 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27637 }
27638
27639 /* The following function is not used anymore (replaced with
27640 mouse_face_from_string_pos), but I leave it here for the time
27641 being, in case someone would. */
27642
27643 #if 0 /* not used */
27644
27645 /* Find the position of the glyph for position POS in OBJECT in
27646 window W's current matrix, and return in *X, *Y the pixel
27647 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27648
27649 RIGHT_P non-zero means return the position of the right edge of the
27650 glyph, RIGHT_P zero means return the left edge position.
27651
27652 If no glyph for POS exists in the matrix, return the position of
27653 the glyph with the next smaller position that is in the matrix, if
27654 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27655 exists in the matrix, return the position of the glyph with the
27656 next larger position in OBJECT.
27657
27658 Value is non-zero if a glyph was found. */
27659
27660 static int
27661 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27662 int *hpos, int *vpos, int *x, int *y, int right_p)
27663 {
27664 int yb = window_text_bottom_y (w);
27665 struct glyph_row *r;
27666 struct glyph *best_glyph = NULL;
27667 struct glyph_row *best_row = NULL;
27668 int best_x = 0;
27669
27670 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27671 r->enabled_p && r->y < yb;
27672 ++r)
27673 {
27674 struct glyph *g = r->glyphs[TEXT_AREA];
27675 struct glyph *e = g + r->used[TEXT_AREA];
27676 int gx;
27677
27678 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27679 if (EQ (g->object, object))
27680 {
27681 if (g->charpos == pos)
27682 {
27683 best_glyph = g;
27684 best_x = gx;
27685 best_row = r;
27686 goto found;
27687 }
27688 else if (best_glyph == NULL
27689 || ((eabs (g->charpos - pos)
27690 < eabs (best_glyph->charpos - pos))
27691 && (right_p
27692 ? g->charpos < pos
27693 : g->charpos > pos)))
27694 {
27695 best_glyph = g;
27696 best_x = gx;
27697 best_row = r;
27698 }
27699 }
27700 }
27701
27702 found:
27703
27704 if (best_glyph)
27705 {
27706 *x = best_x;
27707 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27708
27709 if (right_p)
27710 {
27711 *x += best_glyph->pixel_width;
27712 ++*hpos;
27713 }
27714
27715 *y = best_row->y;
27716 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27717 }
27718
27719 return best_glyph != NULL;
27720 }
27721 #endif /* not used */
27722
27723 /* Find the positions of the first and the last glyphs in window W's
27724 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27725 (assumed to be a string), and return in HLINFO's mouse_face_*
27726 members the pixel and column/row coordinates of those glyphs. */
27727
27728 static void
27729 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27730 Lisp_Object object,
27731 ptrdiff_t startpos, ptrdiff_t endpos)
27732 {
27733 int yb = window_text_bottom_y (w);
27734 struct glyph_row *r;
27735 struct glyph *g, *e;
27736 int gx;
27737 int found = 0;
27738
27739 /* Find the glyph row with at least one position in the range
27740 [STARTPOS..ENDPOS), and the first glyph in that row whose
27741 position belongs to that range. */
27742 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27743 r->enabled_p && r->y < yb;
27744 ++r)
27745 {
27746 if (!r->reversed_p)
27747 {
27748 g = r->glyphs[TEXT_AREA];
27749 e = g + r->used[TEXT_AREA];
27750 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27751 if (EQ (g->object, object)
27752 && startpos <= g->charpos && g->charpos < endpos)
27753 {
27754 hlinfo->mouse_face_beg_row
27755 = MATRIX_ROW_VPOS (r, w->current_matrix);
27756 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27757 hlinfo->mouse_face_beg_x = gx;
27758 found = 1;
27759 break;
27760 }
27761 }
27762 else
27763 {
27764 struct glyph *g1;
27765
27766 e = r->glyphs[TEXT_AREA];
27767 g = e + r->used[TEXT_AREA];
27768 for ( ; g > e; --g)
27769 if (EQ ((g-1)->object, object)
27770 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27771 {
27772 hlinfo->mouse_face_beg_row
27773 = MATRIX_ROW_VPOS (r, w->current_matrix);
27774 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27775 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27776 gx += g1->pixel_width;
27777 hlinfo->mouse_face_beg_x = gx;
27778 found = 1;
27779 break;
27780 }
27781 }
27782 if (found)
27783 break;
27784 }
27785
27786 if (!found)
27787 return;
27788
27789 /* Starting with the next row, look for the first row which does NOT
27790 include any glyphs whose positions are in the range. */
27791 for (++r; r->enabled_p && r->y < yb; ++r)
27792 {
27793 g = r->glyphs[TEXT_AREA];
27794 e = g + r->used[TEXT_AREA];
27795 found = 0;
27796 for ( ; g < e; ++g)
27797 if (EQ (g->object, object)
27798 && startpos <= g->charpos && g->charpos < endpos)
27799 {
27800 found = 1;
27801 break;
27802 }
27803 if (!found)
27804 break;
27805 }
27806
27807 /* The highlighted region ends on the previous row. */
27808 r--;
27809
27810 /* Set the end row. */
27811 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27812
27813 /* Compute and set the end column and the end column's horizontal
27814 pixel coordinate. */
27815 if (!r->reversed_p)
27816 {
27817 g = r->glyphs[TEXT_AREA];
27818 e = g + r->used[TEXT_AREA];
27819 for ( ; e > g; --e)
27820 if (EQ ((e-1)->object, object)
27821 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27822 break;
27823 hlinfo->mouse_face_end_col = e - g;
27824
27825 for (gx = r->x; g < e; ++g)
27826 gx += g->pixel_width;
27827 hlinfo->mouse_face_end_x = gx;
27828 }
27829 else
27830 {
27831 e = r->glyphs[TEXT_AREA];
27832 g = e + r->used[TEXT_AREA];
27833 for (gx = r->x ; e < g; ++e)
27834 {
27835 if (EQ (e->object, object)
27836 && startpos <= e->charpos && e->charpos < endpos)
27837 break;
27838 gx += e->pixel_width;
27839 }
27840 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27841 hlinfo->mouse_face_end_x = gx;
27842 }
27843 }
27844
27845 #ifdef HAVE_WINDOW_SYSTEM
27846
27847 /* See if position X, Y is within a hot-spot of an image. */
27848
27849 static int
27850 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27851 {
27852 if (!CONSP (hot_spot))
27853 return 0;
27854
27855 if (EQ (XCAR (hot_spot), Qrect))
27856 {
27857 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27858 Lisp_Object rect = XCDR (hot_spot);
27859 Lisp_Object tem;
27860 if (!CONSP (rect))
27861 return 0;
27862 if (!CONSP (XCAR (rect)))
27863 return 0;
27864 if (!CONSP (XCDR (rect)))
27865 return 0;
27866 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27867 return 0;
27868 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27869 return 0;
27870 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27871 return 0;
27872 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27873 return 0;
27874 return 1;
27875 }
27876 else if (EQ (XCAR (hot_spot), Qcircle))
27877 {
27878 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27879 Lisp_Object circ = XCDR (hot_spot);
27880 Lisp_Object lr, lx0, ly0;
27881 if (CONSP (circ)
27882 && CONSP (XCAR (circ))
27883 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27884 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27885 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27886 {
27887 double r = XFLOATINT (lr);
27888 double dx = XINT (lx0) - x;
27889 double dy = XINT (ly0) - y;
27890 return (dx * dx + dy * dy <= r * r);
27891 }
27892 }
27893 else if (EQ (XCAR (hot_spot), Qpoly))
27894 {
27895 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27896 if (VECTORP (XCDR (hot_spot)))
27897 {
27898 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27899 Lisp_Object *poly = v->contents;
27900 ptrdiff_t n = v->header.size;
27901 ptrdiff_t i;
27902 int inside = 0;
27903 Lisp_Object lx, ly;
27904 int x0, y0;
27905
27906 /* Need an even number of coordinates, and at least 3 edges. */
27907 if (n < 6 || n & 1)
27908 return 0;
27909
27910 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27911 If count is odd, we are inside polygon. Pixels on edges
27912 may or may not be included depending on actual geometry of the
27913 polygon. */
27914 if ((lx = poly[n-2], !INTEGERP (lx))
27915 || (ly = poly[n-1], !INTEGERP (lx)))
27916 return 0;
27917 x0 = XINT (lx), y0 = XINT (ly);
27918 for (i = 0; i < n; i += 2)
27919 {
27920 int x1 = x0, y1 = y0;
27921 if ((lx = poly[i], !INTEGERP (lx))
27922 || (ly = poly[i+1], !INTEGERP (ly)))
27923 return 0;
27924 x0 = XINT (lx), y0 = XINT (ly);
27925
27926 /* Does this segment cross the X line? */
27927 if (x0 >= x)
27928 {
27929 if (x1 >= x)
27930 continue;
27931 }
27932 else if (x1 < x)
27933 continue;
27934 if (y > y0 && y > y1)
27935 continue;
27936 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27937 inside = !inside;
27938 }
27939 return inside;
27940 }
27941 }
27942 return 0;
27943 }
27944
27945 Lisp_Object
27946 find_hot_spot (Lisp_Object map, int x, int y)
27947 {
27948 while (CONSP (map))
27949 {
27950 if (CONSP (XCAR (map))
27951 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27952 return XCAR (map);
27953 map = XCDR (map);
27954 }
27955
27956 return Qnil;
27957 }
27958
27959 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27960 3, 3, 0,
27961 doc: /* Lookup in image map MAP coordinates X and Y.
27962 An image map is an alist where each element has the format (AREA ID PLIST).
27963 An AREA is specified as either a rectangle, a circle, or a polygon:
27964 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27965 pixel coordinates of the upper left and bottom right corners.
27966 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27967 and the radius of the circle; r may be a float or integer.
27968 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27969 vector describes one corner in the polygon.
27970 Returns the alist element for the first matching AREA in MAP. */)
27971 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27972 {
27973 if (NILP (map))
27974 return Qnil;
27975
27976 CHECK_NUMBER (x);
27977 CHECK_NUMBER (y);
27978
27979 return find_hot_spot (map,
27980 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27981 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27982 }
27983
27984
27985 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27986 static void
27987 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27988 {
27989 /* Do not change cursor shape while dragging mouse. */
27990 if (!NILP (do_mouse_tracking))
27991 return;
27992
27993 if (!NILP (pointer))
27994 {
27995 if (EQ (pointer, Qarrow))
27996 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27997 else if (EQ (pointer, Qhand))
27998 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27999 else if (EQ (pointer, Qtext))
28000 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28001 else if (EQ (pointer, intern ("hdrag")))
28002 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28003 else if (EQ (pointer, intern ("nhdrag")))
28004 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28005 #ifdef HAVE_X_WINDOWS
28006 else if (EQ (pointer, intern ("vdrag")))
28007 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28008 #endif
28009 else if (EQ (pointer, intern ("hourglass")))
28010 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28011 else if (EQ (pointer, Qmodeline))
28012 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28013 else
28014 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28015 }
28016
28017 if (cursor != No_Cursor)
28018 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28019 }
28020
28021 #endif /* HAVE_WINDOW_SYSTEM */
28022
28023 /* Take proper action when mouse has moved to the mode or header line
28024 or marginal area AREA of window W, x-position X and y-position Y.
28025 X is relative to the start of the text display area of W, so the
28026 width of bitmap areas and scroll bars must be subtracted to get a
28027 position relative to the start of the mode line. */
28028
28029 static void
28030 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28031 enum window_part area)
28032 {
28033 struct window *w = XWINDOW (window);
28034 struct frame *f = XFRAME (w->frame);
28035 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28036 #ifdef HAVE_WINDOW_SYSTEM
28037 Display_Info *dpyinfo;
28038 #endif
28039 Cursor cursor = No_Cursor;
28040 Lisp_Object pointer = Qnil;
28041 int dx, dy, width, height;
28042 ptrdiff_t charpos;
28043 Lisp_Object string, object = Qnil;
28044 Lisp_Object pos IF_LINT (= Qnil), help;
28045
28046 Lisp_Object mouse_face;
28047 int original_x_pixel = x;
28048 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28049 struct glyph_row *row IF_LINT (= 0);
28050
28051 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28052 {
28053 int x0;
28054 struct glyph *end;
28055
28056 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28057 returns them in row/column units! */
28058 string = mode_line_string (w, area, &x, &y, &charpos,
28059 &object, &dx, &dy, &width, &height);
28060
28061 row = (area == ON_MODE_LINE
28062 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28063 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28064
28065 /* Find the glyph under the mouse pointer. */
28066 if (row->mode_line_p && row->enabled_p)
28067 {
28068 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28069 end = glyph + row->used[TEXT_AREA];
28070
28071 for (x0 = original_x_pixel;
28072 glyph < end && x0 >= glyph->pixel_width;
28073 ++glyph)
28074 x0 -= glyph->pixel_width;
28075
28076 if (glyph >= end)
28077 glyph = NULL;
28078 }
28079 }
28080 else
28081 {
28082 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28083 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28084 returns them in row/column units! */
28085 string = marginal_area_string (w, area, &x, &y, &charpos,
28086 &object, &dx, &dy, &width, &height);
28087 }
28088
28089 help = Qnil;
28090
28091 #ifdef HAVE_WINDOW_SYSTEM
28092 if (IMAGEP (object))
28093 {
28094 Lisp_Object image_map, hotspot;
28095 if ((image_map = Fplist_get (XCDR (object), QCmap),
28096 !NILP (image_map))
28097 && (hotspot = find_hot_spot (image_map, dx, dy),
28098 CONSP (hotspot))
28099 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28100 {
28101 Lisp_Object plist;
28102
28103 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28104 If so, we could look for mouse-enter, mouse-leave
28105 properties in PLIST (and do something...). */
28106 hotspot = XCDR (hotspot);
28107 if (CONSP (hotspot)
28108 && (plist = XCAR (hotspot), CONSP (plist)))
28109 {
28110 pointer = Fplist_get (plist, Qpointer);
28111 if (NILP (pointer))
28112 pointer = Qhand;
28113 help = Fplist_get (plist, Qhelp_echo);
28114 if (!NILP (help))
28115 {
28116 help_echo_string = help;
28117 XSETWINDOW (help_echo_window, w);
28118 help_echo_object = w->contents;
28119 help_echo_pos = charpos;
28120 }
28121 }
28122 }
28123 if (NILP (pointer))
28124 pointer = Fplist_get (XCDR (object), QCpointer);
28125 }
28126 #endif /* HAVE_WINDOW_SYSTEM */
28127
28128 if (STRINGP (string))
28129 pos = make_number (charpos);
28130
28131 /* Set the help text and mouse pointer. If the mouse is on a part
28132 of the mode line without any text (e.g. past the right edge of
28133 the mode line text), use the default help text and pointer. */
28134 if (STRINGP (string) || area == ON_MODE_LINE)
28135 {
28136 /* Arrange to display the help by setting the global variables
28137 help_echo_string, help_echo_object, and help_echo_pos. */
28138 if (NILP (help))
28139 {
28140 if (STRINGP (string))
28141 help = Fget_text_property (pos, Qhelp_echo, string);
28142
28143 if (!NILP (help))
28144 {
28145 help_echo_string = help;
28146 XSETWINDOW (help_echo_window, w);
28147 help_echo_object = string;
28148 help_echo_pos = charpos;
28149 }
28150 else if (area == ON_MODE_LINE)
28151 {
28152 Lisp_Object default_help
28153 = buffer_local_value_1 (Qmode_line_default_help_echo,
28154 w->contents);
28155
28156 if (STRINGP (default_help))
28157 {
28158 help_echo_string = default_help;
28159 XSETWINDOW (help_echo_window, w);
28160 help_echo_object = Qnil;
28161 help_echo_pos = -1;
28162 }
28163 }
28164 }
28165
28166 #ifdef HAVE_WINDOW_SYSTEM
28167 /* Change the mouse pointer according to what is under it. */
28168 if (FRAME_WINDOW_P (f))
28169 {
28170 dpyinfo = FRAME_DISPLAY_INFO (f);
28171 if (STRINGP (string))
28172 {
28173 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28174
28175 if (NILP (pointer))
28176 pointer = Fget_text_property (pos, Qpointer, string);
28177
28178 /* Change the mouse pointer according to what is under X/Y. */
28179 if (NILP (pointer)
28180 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28181 {
28182 Lisp_Object map;
28183 map = Fget_text_property (pos, Qlocal_map, string);
28184 if (!KEYMAPP (map))
28185 map = Fget_text_property (pos, Qkeymap, string);
28186 if (!KEYMAPP (map))
28187 cursor = dpyinfo->vertical_scroll_bar_cursor;
28188 }
28189 }
28190 else
28191 /* Default mode-line pointer. */
28192 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28193 }
28194 #endif
28195 }
28196
28197 /* Change the mouse face according to what is under X/Y. */
28198 if (STRINGP (string))
28199 {
28200 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28201 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28202 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28203 && glyph)
28204 {
28205 Lisp_Object b, e;
28206
28207 struct glyph * tmp_glyph;
28208
28209 int gpos;
28210 int gseq_length;
28211 int total_pixel_width;
28212 ptrdiff_t begpos, endpos, ignore;
28213
28214 int vpos, hpos;
28215
28216 b = Fprevious_single_property_change (make_number (charpos + 1),
28217 Qmouse_face, string, Qnil);
28218 if (NILP (b))
28219 begpos = 0;
28220 else
28221 begpos = XINT (b);
28222
28223 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28224 if (NILP (e))
28225 endpos = SCHARS (string);
28226 else
28227 endpos = XINT (e);
28228
28229 /* Calculate the glyph position GPOS of GLYPH in the
28230 displayed string, relative to the beginning of the
28231 highlighted part of the string.
28232
28233 Note: GPOS is different from CHARPOS. CHARPOS is the
28234 position of GLYPH in the internal string object. A mode
28235 line string format has structures which are converted to
28236 a flattened string by the Emacs Lisp interpreter. The
28237 internal string is an element of those structures. The
28238 displayed string is the flattened string. */
28239 tmp_glyph = row_start_glyph;
28240 while (tmp_glyph < glyph
28241 && (!(EQ (tmp_glyph->object, glyph->object)
28242 && begpos <= tmp_glyph->charpos
28243 && tmp_glyph->charpos < endpos)))
28244 tmp_glyph++;
28245 gpos = glyph - tmp_glyph;
28246
28247 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28248 the highlighted part of the displayed string to which
28249 GLYPH belongs. Note: GSEQ_LENGTH is different from
28250 SCHARS (STRING), because the latter returns the length of
28251 the internal string. */
28252 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28253 tmp_glyph > glyph
28254 && (!(EQ (tmp_glyph->object, glyph->object)
28255 && begpos <= tmp_glyph->charpos
28256 && tmp_glyph->charpos < endpos));
28257 tmp_glyph--)
28258 ;
28259 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28260
28261 /* Calculate the total pixel width of all the glyphs between
28262 the beginning of the highlighted area and GLYPH. */
28263 total_pixel_width = 0;
28264 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28265 total_pixel_width += tmp_glyph->pixel_width;
28266
28267 /* Pre calculation of re-rendering position. Note: X is in
28268 column units here, after the call to mode_line_string or
28269 marginal_area_string. */
28270 hpos = x - gpos;
28271 vpos = (area == ON_MODE_LINE
28272 ? (w->current_matrix)->nrows - 1
28273 : 0);
28274
28275 /* If GLYPH's position is included in the region that is
28276 already drawn in mouse face, we have nothing to do. */
28277 if ( EQ (window, hlinfo->mouse_face_window)
28278 && (!row->reversed_p
28279 ? (hlinfo->mouse_face_beg_col <= hpos
28280 && hpos < hlinfo->mouse_face_end_col)
28281 /* In R2L rows we swap BEG and END, see below. */
28282 : (hlinfo->mouse_face_end_col <= hpos
28283 && hpos < hlinfo->mouse_face_beg_col))
28284 && hlinfo->mouse_face_beg_row == vpos )
28285 return;
28286
28287 if (clear_mouse_face (hlinfo))
28288 cursor = No_Cursor;
28289
28290 if (!row->reversed_p)
28291 {
28292 hlinfo->mouse_face_beg_col = hpos;
28293 hlinfo->mouse_face_beg_x = original_x_pixel
28294 - (total_pixel_width + dx);
28295 hlinfo->mouse_face_end_col = hpos + gseq_length;
28296 hlinfo->mouse_face_end_x = 0;
28297 }
28298 else
28299 {
28300 /* In R2L rows, show_mouse_face expects BEG and END
28301 coordinates to be swapped. */
28302 hlinfo->mouse_face_end_col = hpos;
28303 hlinfo->mouse_face_end_x = original_x_pixel
28304 - (total_pixel_width + dx);
28305 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28306 hlinfo->mouse_face_beg_x = 0;
28307 }
28308
28309 hlinfo->mouse_face_beg_row = vpos;
28310 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28311 hlinfo->mouse_face_past_end = 0;
28312 hlinfo->mouse_face_window = window;
28313
28314 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28315 charpos,
28316 0, &ignore,
28317 glyph->face_id,
28318 1);
28319 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28320
28321 if (NILP (pointer))
28322 pointer = Qhand;
28323 }
28324 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28325 clear_mouse_face (hlinfo);
28326 }
28327 #ifdef HAVE_WINDOW_SYSTEM
28328 if (FRAME_WINDOW_P (f))
28329 define_frame_cursor1 (f, cursor, pointer);
28330 #endif
28331 }
28332
28333
28334 /* EXPORT:
28335 Take proper action when the mouse has moved to position X, Y on
28336 frame F with regards to highlighting portions of display that have
28337 mouse-face properties. Also de-highlight portions of display where
28338 the mouse was before, set the mouse pointer shape as appropriate
28339 for the mouse coordinates, and activate help echo (tooltips).
28340 X and Y can be negative or out of range. */
28341
28342 void
28343 note_mouse_highlight (struct frame *f, int x, int y)
28344 {
28345 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28346 enum window_part part = ON_NOTHING;
28347 Lisp_Object window;
28348 struct window *w;
28349 Cursor cursor = No_Cursor;
28350 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28351 struct buffer *b;
28352
28353 /* When a menu is active, don't highlight because this looks odd. */
28354 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28355 if (popup_activated ())
28356 return;
28357 #endif
28358
28359 if (!f->glyphs_initialized_p
28360 || f->pointer_invisible)
28361 return;
28362
28363 hlinfo->mouse_face_mouse_x = x;
28364 hlinfo->mouse_face_mouse_y = y;
28365 hlinfo->mouse_face_mouse_frame = f;
28366
28367 if (hlinfo->mouse_face_defer)
28368 return;
28369
28370 /* Which window is that in? */
28371 window = window_from_coordinates (f, x, y, &part, 1);
28372
28373 /* If displaying active text in another window, clear that. */
28374 if (! EQ (window, hlinfo->mouse_face_window)
28375 /* Also clear if we move out of text area in same window. */
28376 || (!NILP (hlinfo->mouse_face_window)
28377 && !NILP (window)
28378 && part != ON_TEXT
28379 && part != ON_MODE_LINE
28380 && part != ON_HEADER_LINE))
28381 clear_mouse_face (hlinfo);
28382
28383 /* Not on a window -> return. */
28384 if (!WINDOWP (window))
28385 return;
28386
28387 /* Reset help_echo_string. It will get recomputed below. */
28388 help_echo_string = Qnil;
28389
28390 /* Convert to window-relative pixel coordinates. */
28391 w = XWINDOW (window);
28392 frame_to_window_pixel_xy (w, &x, &y);
28393
28394 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28395 /* Handle tool-bar window differently since it doesn't display a
28396 buffer. */
28397 if (EQ (window, f->tool_bar_window))
28398 {
28399 note_tool_bar_highlight (f, x, y);
28400 return;
28401 }
28402 #endif
28403
28404 /* Mouse is on the mode, header line or margin? */
28405 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28406 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28407 {
28408 note_mode_line_or_margin_highlight (window, x, y, part);
28409 return;
28410 }
28411
28412 #ifdef HAVE_WINDOW_SYSTEM
28413 if (part == ON_VERTICAL_BORDER)
28414 {
28415 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28416 help_echo_string = build_string ("drag-mouse-1: resize");
28417 }
28418 else if (part == ON_RIGHT_DIVIDER)
28419 {
28420 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28421 help_echo_string = build_string ("drag-mouse-1: resize");
28422 }
28423 else if (part == ON_BOTTOM_DIVIDER)
28424 {
28425 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28426 help_echo_string = build_string ("drag-mouse-1: resize");
28427 }
28428 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28429 || part == ON_SCROLL_BAR)
28430 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28431 else
28432 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28433 #endif
28434
28435 /* Are we in a window whose display is up to date?
28436 And verify the buffer's text has not changed. */
28437 b = XBUFFER (w->contents);
28438 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28439 {
28440 int hpos, vpos, dx, dy, area = LAST_AREA;
28441 ptrdiff_t pos;
28442 struct glyph *glyph;
28443 Lisp_Object object;
28444 Lisp_Object mouse_face = Qnil, position;
28445 Lisp_Object *overlay_vec = NULL;
28446 ptrdiff_t i, noverlays;
28447 struct buffer *obuf;
28448 ptrdiff_t obegv, ozv;
28449 int same_region;
28450
28451 /* Find the glyph under X/Y. */
28452 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28453
28454 #ifdef HAVE_WINDOW_SYSTEM
28455 /* Look for :pointer property on image. */
28456 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28457 {
28458 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28459 if (img != NULL && IMAGEP (img->spec))
28460 {
28461 Lisp_Object image_map, hotspot;
28462 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28463 !NILP (image_map))
28464 && (hotspot = find_hot_spot (image_map,
28465 glyph->slice.img.x + dx,
28466 glyph->slice.img.y + dy),
28467 CONSP (hotspot))
28468 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28469 {
28470 Lisp_Object plist;
28471
28472 /* Could check XCAR (hotspot) to see if we enter/leave
28473 this hot-spot.
28474 If so, we could look for mouse-enter, mouse-leave
28475 properties in PLIST (and do something...). */
28476 hotspot = XCDR (hotspot);
28477 if (CONSP (hotspot)
28478 && (plist = XCAR (hotspot), CONSP (plist)))
28479 {
28480 pointer = Fplist_get (plist, Qpointer);
28481 if (NILP (pointer))
28482 pointer = Qhand;
28483 help_echo_string = Fplist_get (plist, Qhelp_echo);
28484 if (!NILP (help_echo_string))
28485 {
28486 help_echo_window = window;
28487 help_echo_object = glyph->object;
28488 help_echo_pos = glyph->charpos;
28489 }
28490 }
28491 }
28492 if (NILP (pointer))
28493 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28494 }
28495 }
28496 #endif /* HAVE_WINDOW_SYSTEM */
28497
28498 /* Clear mouse face if X/Y not over text. */
28499 if (glyph == NULL
28500 || area != TEXT_AREA
28501 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28502 /* Glyph's OBJECT is an integer for glyphs inserted by the
28503 display engine for its internal purposes, like truncation
28504 and continuation glyphs and blanks beyond the end of
28505 line's text on text terminals. If we are over such a
28506 glyph, we are not over any text. */
28507 || INTEGERP (glyph->object)
28508 /* R2L rows have a stretch glyph at their front, which
28509 stands for no text, whereas L2R rows have no glyphs at
28510 all beyond the end of text. Treat such stretch glyphs
28511 like we do with NULL glyphs in L2R rows. */
28512 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28513 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28514 && glyph->type == STRETCH_GLYPH
28515 && glyph->avoid_cursor_p))
28516 {
28517 if (clear_mouse_face (hlinfo))
28518 cursor = No_Cursor;
28519 #ifdef HAVE_WINDOW_SYSTEM
28520 if (FRAME_WINDOW_P (f) && NILP (pointer))
28521 {
28522 if (area != TEXT_AREA)
28523 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28524 else
28525 pointer = Vvoid_text_area_pointer;
28526 }
28527 #endif
28528 goto set_cursor;
28529 }
28530
28531 pos = glyph->charpos;
28532 object = glyph->object;
28533 if (!STRINGP (object) && !BUFFERP (object))
28534 goto set_cursor;
28535
28536 /* If we get an out-of-range value, return now; avoid an error. */
28537 if (BUFFERP (object) && pos > BUF_Z (b))
28538 goto set_cursor;
28539
28540 /* Make the window's buffer temporarily current for
28541 overlays_at and compute_char_face. */
28542 obuf = current_buffer;
28543 current_buffer = b;
28544 obegv = BEGV;
28545 ozv = ZV;
28546 BEGV = BEG;
28547 ZV = Z;
28548
28549 /* Is this char mouse-active or does it have help-echo? */
28550 position = make_number (pos);
28551
28552 if (BUFFERP (object))
28553 {
28554 /* Put all the overlays we want in a vector in overlay_vec. */
28555 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28556 /* Sort overlays into increasing priority order. */
28557 noverlays = sort_overlays (overlay_vec, noverlays, w);
28558 }
28559 else
28560 noverlays = 0;
28561
28562 if (NILP (Vmouse_highlight))
28563 {
28564 clear_mouse_face (hlinfo);
28565 goto check_help_echo;
28566 }
28567
28568 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28569
28570 if (same_region)
28571 cursor = No_Cursor;
28572
28573 /* Check mouse-face highlighting. */
28574 if (! same_region
28575 /* If there exists an overlay with mouse-face overlapping
28576 the one we are currently highlighting, we have to
28577 check if we enter the overlapping overlay, and then
28578 highlight only that. */
28579 || (OVERLAYP (hlinfo->mouse_face_overlay)
28580 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28581 {
28582 /* Find the highest priority overlay with a mouse-face. */
28583 Lisp_Object overlay = Qnil;
28584 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28585 {
28586 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28587 if (!NILP (mouse_face))
28588 overlay = overlay_vec[i];
28589 }
28590
28591 /* If we're highlighting the same overlay as before, there's
28592 no need to do that again. */
28593 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28594 goto check_help_echo;
28595 hlinfo->mouse_face_overlay = overlay;
28596
28597 /* Clear the display of the old active region, if any. */
28598 if (clear_mouse_face (hlinfo))
28599 cursor = No_Cursor;
28600
28601 /* If no overlay applies, get a text property. */
28602 if (NILP (overlay))
28603 mouse_face = Fget_text_property (position, Qmouse_face, object);
28604
28605 /* Next, compute the bounds of the mouse highlighting and
28606 display it. */
28607 if (!NILP (mouse_face) && STRINGP (object))
28608 {
28609 /* The mouse-highlighting comes from a display string
28610 with a mouse-face. */
28611 Lisp_Object s, e;
28612 ptrdiff_t ignore;
28613
28614 s = Fprevious_single_property_change
28615 (make_number (pos + 1), Qmouse_face, object, Qnil);
28616 e = Fnext_single_property_change
28617 (position, Qmouse_face, object, Qnil);
28618 if (NILP (s))
28619 s = make_number (0);
28620 if (NILP (e))
28621 e = make_number (SCHARS (object));
28622 mouse_face_from_string_pos (w, hlinfo, object,
28623 XINT (s), XINT (e));
28624 hlinfo->mouse_face_past_end = 0;
28625 hlinfo->mouse_face_window = window;
28626 hlinfo->mouse_face_face_id
28627 = face_at_string_position (w, object, pos, 0, &ignore,
28628 glyph->face_id, 1);
28629 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28630 cursor = No_Cursor;
28631 }
28632 else
28633 {
28634 /* The mouse-highlighting, if any, comes from an overlay
28635 or text property in the buffer. */
28636 Lisp_Object buffer IF_LINT (= Qnil);
28637 Lisp_Object disp_string IF_LINT (= Qnil);
28638
28639 if (STRINGP (object))
28640 {
28641 /* If we are on a display string with no mouse-face,
28642 check if the text under it has one. */
28643 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28644 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28645 pos = string_buffer_position (object, start);
28646 if (pos > 0)
28647 {
28648 mouse_face = get_char_property_and_overlay
28649 (make_number (pos), Qmouse_face, w->contents, &overlay);
28650 buffer = w->contents;
28651 disp_string = object;
28652 }
28653 }
28654 else
28655 {
28656 buffer = object;
28657 disp_string = Qnil;
28658 }
28659
28660 if (!NILP (mouse_face))
28661 {
28662 Lisp_Object before, after;
28663 Lisp_Object before_string, after_string;
28664 /* To correctly find the limits of mouse highlight
28665 in a bidi-reordered buffer, we must not use the
28666 optimization of limiting the search in
28667 previous-single-property-change and
28668 next-single-property-change, because
28669 rows_from_pos_range needs the real start and end
28670 positions to DTRT in this case. That's because
28671 the first row visible in a window does not
28672 necessarily display the character whose position
28673 is the smallest. */
28674 Lisp_Object lim1
28675 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28676 ? Fmarker_position (w->start)
28677 : Qnil;
28678 Lisp_Object lim2
28679 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28680 ? make_number (BUF_Z (XBUFFER (buffer))
28681 - w->window_end_pos)
28682 : Qnil;
28683
28684 if (NILP (overlay))
28685 {
28686 /* Handle the text property case. */
28687 before = Fprevious_single_property_change
28688 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28689 after = Fnext_single_property_change
28690 (make_number (pos), Qmouse_face, buffer, lim2);
28691 before_string = after_string = Qnil;
28692 }
28693 else
28694 {
28695 /* Handle the overlay case. */
28696 before = Foverlay_start (overlay);
28697 after = Foverlay_end (overlay);
28698 before_string = Foverlay_get (overlay, Qbefore_string);
28699 after_string = Foverlay_get (overlay, Qafter_string);
28700
28701 if (!STRINGP (before_string)) before_string = Qnil;
28702 if (!STRINGP (after_string)) after_string = Qnil;
28703 }
28704
28705 mouse_face_from_buffer_pos (window, hlinfo, pos,
28706 NILP (before)
28707 ? 1
28708 : XFASTINT (before),
28709 NILP (after)
28710 ? BUF_Z (XBUFFER (buffer))
28711 : XFASTINT (after),
28712 before_string, after_string,
28713 disp_string);
28714 cursor = No_Cursor;
28715 }
28716 }
28717 }
28718
28719 check_help_echo:
28720
28721 /* Look for a `help-echo' property. */
28722 if (NILP (help_echo_string)) {
28723 Lisp_Object help, overlay;
28724
28725 /* Check overlays first. */
28726 help = overlay = Qnil;
28727 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28728 {
28729 overlay = overlay_vec[i];
28730 help = Foverlay_get (overlay, Qhelp_echo);
28731 }
28732
28733 if (!NILP (help))
28734 {
28735 help_echo_string = help;
28736 help_echo_window = window;
28737 help_echo_object = overlay;
28738 help_echo_pos = pos;
28739 }
28740 else
28741 {
28742 Lisp_Object obj = glyph->object;
28743 ptrdiff_t charpos = glyph->charpos;
28744
28745 /* Try text properties. */
28746 if (STRINGP (obj)
28747 && charpos >= 0
28748 && charpos < SCHARS (obj))
28749 {
28750 help = Fget_text_property (make_number (charpos),
28751 Qhelp_echo, obj);
28752 if (NILP (help))
28753 {
28754 /* If the string itself doesn't specify a help-echo,
28755 see if the buffer text ``under'' it does. */
28756 struct glyph_row *r
28757 = MATRIX_ROW (w->current_matrix, vpos);
28758 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28759 ptrdiff_t p = string_buffer_position (obj, start);
28760 if (p > 0)
28761 {
28762 help = Fget_char_property (make_number (p),
28763 Qhelp_echo, w->contents);
28764 if (!NILP (help))
28765 {
28766 charpos = p;
28767 obj = w->contents;
28768 }
28769 }
28770 }
28771 }
28772 else if (BUFFERP (obj)
28773 && charpos >= BEGV
28774 && charpos < ZV)
28775 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28776 obj);
28777
28778 if (!NILP (help))
28779 {
28780 help_echo_string = help;
28781 help_echo_window = window;
28782 help_echo_object = obj;
28783 help_echo_pos = charpos;
28784 }
28785 }
28786 }
28787
28788 #ifdef HAVE_WINDOW_SYSTEM
28789 /* Look for a `pointer' property. */
28790 if (FRAME_WINDOW_P (f) && NILP (pointer))
28791 {
28792 /* Check overlays first. */
28793 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28794 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28795
28796 if (NILP (pointer))
28797 {
28798 Lisp_Object obj = glyph->object;
28799 ptrdiff_t charpos = glyph->charpos;
28800
28801 /* Try text properties. */
28802 if (STRINGP (obj)
28803 && charpos >= 0
28804 && charpos < SCHARS (obj))
28805 {
28806 pointer = Fget_text_property (make_number (charpos),
28807 Qpointer, obj);
28808 if (NILP (pointer))
28809 {
28810 /* If the string itself doesn't specify a pointer,
28811 see if the buffer text ``under'' it does. */
28812 struct glyph_row *r
28813 = MATRIX_ROW (w->current_matrix, vpos);
28814 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28815 ptrdiff_t p = string_buffer_position (obj, start);
28816 if (p > 0)
28817 pointer = Fget_char_property (make_number (p),
28818 Qpointer, w->contents);
28819 }
28820 }
28821 else if (BUFFERP (obj)
28822 && charpos >= BEGV
28823 && charpos < ZV)
28824 pointer = Fget_text_property (make_number (charpos),
28825 Qpointer, obj);
28826 }
28827 }
28828 #endif /* HAVE_WINDOW_SYSTEM */
28829
28830 BEGV = obegv;
28831 ZV = ozv;
28832 current_buffer = obuf;
28833 }
28834
28835 set_cursor:
28836
28837 #ifdef HAVE_WINDOW_SYSTEM
28838 if (FRAME_WINDOW_P (f))
28839 define_frame_cursor1 (f, cursor, pointer);
28840 #else
28841 /* This is here to prevent a compiler error, about "label at end of
28842 compound statement". */
28843 return;
28844 #endif
28845 }
28846
28847
28848 /* EXPORT for RIF:
28849 Clear any mouse-face on window W. This function is part of the
28850 redisplay interface, and is called from try_window_id and similar
28851 functions to ensure the mouse-highlight is off. */
28852
28853 void
28854 x_clear_window_mouse_face (struct window *w)
28855 {
28856 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28857 Lisp_Object window;
28858
28859 block_input ();
28860 XSETWINDOW (window, w);
28861 if (EQ (window, hlinfo->mouse_face_window))
28862 clear_mouse_face (hlinfo);
28863 unblock_input ();
28864 }
28865
28866
28867 /* EXPORT:
28868 Just discard the mouse face information for frame F, if any.
28869 This is used when the size of F is changed. */
28870
28871 void
28872 cancel_mouse_face (struct frame *f)
28873 {
28874 Lisp_Object window;
28875 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28876
28877 window = hlinfo->mouse_face_window;
28878 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28879 reset_mouse_highlight (hlinfo);
28880 }
28881
28882
28883 \f
28884 /***********************************************************************
28885 Exposure Events
28886 ***********************************************************************/
28887
28888 #ifdef HAVE_WINDOW_SYSTEM
28889
28890 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28891 which intersects rectangle R. R is in window-relative coordinates. */
28892
28893 static void
28894 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28895 enum glyph_row_area area)
28896 {
28897 struct glyph *first = row->glyphs[area];
28898 struct glyph *end = row->glyphs[area] + row->used[area];
28899 struct glyph *last;
28900 int first_x, start_x, x;
28901
28902 if (area == TEXT_AREA && row->fill_line_p)
28903 /* If row extends face to end of line write the whole line. */
28904 draw_glyphs (w, 0, row, area,
28905 0, row->used[area],
28906 DRAW_NORMAL_TEXT, 0);
28907 else
28908 {
28909 /* Set START_X to the window-relative start position for drawing glyphs of
28910 AREA. The first glyph of the text area can be partially visible.
28911 The first glyphs of other areas cannot. */
28912 start_x = window_box_left_offset (w, area);
28913 x = start_x;
28914 if (area == TEXT_AREA)
28915 x += row->x;
28916
28917 /* Find the first glyph that must be redrawn. */
28918 while (first < end
28919 && x + first->pixel_width < r->x)
28920 {
28921 x += first->pixel_width;
28922 ++first;
28923 }
28924
28925 /* Find the last one. */
28926 last = first;
28927 first_x = x;
28928 while (last < end
28929 && x < r->x + r->width)
28930 {
28931 x += last->pixel_width;
28932 ++last;
28933 }
28934
28935 /* Repaint. */
28936 if (last > first)
28937 draw_glyphs (w, first_x - start_x, row, area,
28938 first - row->glyphs[area], last - row->glyphs[area],
28939 DRAW_NORMAL_TEXT, 0);
28940 }
28941 }
28942
28943
28944 /* Redraw the parts of the glyph row ROW on window W intersecting
28945 rectangle R. R is in window-relative coordinates. Value is
28946 non-zero if mouse-face was overwritten. */
28947
28948 static int
28949 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28950 {
28951 eassert (row->enabled_p);
28952
28953 if (row->mode_line_p || w->pseudo_window_p)
28954 draw_glyphs (w, 0, row, TEXT_AREA,
28955 0, row->used[TEXT_AREA],
28956 DRAW_NORMAL_TEXT, 0);
28957 else
28958 {
28959 if (row->used[LEFT_MARGIN_AREA])
28960 expose_area (w, row, r, LEFT_MARGIN_AREA);
28961 if (row->used[TEXT_AREA])
28962 expose_area (w, row, r, TEXT_AREA);
28963 if (row->used[RIGHT_MARGIN_AREA])
28964 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28965 draw_row_fringe_bitmaps (w, row);
28966 }
28967
28968 return row->mouse_face_p;
28969 }
28970
28971
28972 /* Redraw those parts of glyphs rows during expose event handling that
28973 overlap other rows. Redrawing of an exposed line writes over parts
28974 of lines overlapping that exposed line; this function fixes that.
28975
28976 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28977 row in W's current matrix that is exposed and overlaps other rows.
28978 LAST_OVERLAPPING_ROW is the last such row. */
28979
28980 static void
28981 expose_overlaps (struct window *w,
28982 struct glyph_row *first_overlapping_row,
28983 struct glyph_row *last_overlapping_row,
28984 XRectangle *r)
28985 {
28986 struct glyph_row *row;
28987
28988 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28989 if (row->overlapping_p)
28990 {
28991 eassert (row->enabled_p && !row->mode_line_p);
28992
28993 row->clip = r;
28994 if (row->used[LEFT_MARGIN_AREA])
28995 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28996
28997 if (row->used[TEXT_AREA])
28998 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28999
29000 if (row->used[RIGHT_MARGIN_AREA])
29001 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29002 row->clip = NULL;
29003 }
29004 }
29005
29006
29007 /* Return non-zero if W's cursor intersects rectangle R. */
29008
29009 static int
29010 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29011 {
29012 XRectangle cr, result;
29013 struct glyph *cursor_glyph;
29014 struct glyph_row *row;
29015
29016 if (w->phys_cursor.vpos >= 0
29017 && w->phys_cursor.vpos < w->current_matrix->nrows
29018 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29019 row->enabled_p)
29020 && row->cursor_in_fringe_p)
29021 {
29022 /* Cursor is in the fringe. */
29023 cr.x = window_box_right_offset (w,
29024 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29025 ? RIGHT_MARGIN_AREA
29026 : TEXT_AREA));
29027 cr.y = row->y;
29028 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29029 cr.height = row->height;
29030 return x_intersect_rectangles (&cr, r, &result);
29031 }
29032
29033 cursor_glyph = get_phys_cursor_glyph (w);
29034 if (cursor_glyph)
29035 {
29036 /* r is relative to W's box, but w->phys_cursor.x is relative
29037 to left edge of W's TEXT area. Adjust it. */
29038 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29039 cr.y = w->phys_cursor.y;
29040 cr.width = cursor_glyph->pixel_width;
29041 cr.height = w->phys_cursor_height;
29042 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29043 I assume the effect is the same -- and this is portable. */
29044 return x_intersect_rectangles (&cr, r, &result);
29045 }
29046 /* If we don't understand the format, pretend we're not in the hot-spot. */
29047 return 0;
29048 }
29049
29050
29051 /* EXPORT:
29052 Draw a vertical window border to the right of window W if W doesn't
29053 have vertical scroll bars. */
29054
29055 void
29056 x_draw_vertical_border (struct window *w)
29057 {
29058 struct frame *f = XFRAME (WINDOW_FRAME (w));
29059
29060 /* We could do better, if we knew what type of scroll-bar the adjacent
29061 windows (on either side) have... But we don't :-(
29062 However, I think this works ok. ++KFS 2003-04-25 */
29063
29064 /* Redraw borders between horizontally adjacent windows. Don't
29065 do it for frames with vertical scroll bars because either the
29066 right scroll bar of a window, or the left scroll bar of its
29067 neighbor will suffice as a border. */
29068 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29069 return;
29070
29071 /* Note: It is necessary to redraw both the left and the right
29072 borders, for when only this single window W is being
29073 redisplayed. */
29074 if (!WINDOW_RIGHTMOST_P (w)
29075 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29076 {
29077 int x0, x1, y0, y1;
29078
29079 window_box_edges (w, &x0, &y0, &x1, &y1);
29080 y1 -= 1;
29081
29082 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29083 x1 -= 1;
29084
29085 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29086 }
29087
29088 if (!WINDOW_LEFTMOST_P (w)
29089 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29090 {
29091 int x0, x1, y0, y1;
29092
29093 window_box_edges (w, &x0, &y0, &x1, &y1);
29094 y1 -= 1;
29095
29096 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29097 x0 -= 1;
29098
29099 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29100 }
29101 }
29102
29103
29104 /* Draw window dividers for window W. */
29105
29106 void
29107 x_draw_right_divider (struct window *w)
29108 {
29109 struct frame *f = WINDOW_XFRAME (w);
29110
29111 if (w->mini || w->pseudo_window_p)
29112 return;
29113 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29114 {
29115 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29116 int x1 = WINDOW_RIGHT_EDGE_X (w);
29117 int y0 = WINDOW_TOP_EDGE_Y (w);
29118 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29119
29120 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29121 }
29122 }
29123
29124 static void
29125 x_draw_bottom_divider (struct window *w)
29126 {
29127 struct frame *f = XFRAME (WINDOW_FRAME (w));
29128
29129 if (w->mini || w->pseudo_window_p)
29130 return;
29131 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29132 {
29133 int x0 = WINDOW_LEFT_EDGE_X (w);
29134 int x1 = WINDOW_RIGHT_EDGE_X (w);
29135 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29136 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29137
29138 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29139 }
29140 }
29141
29142 /* Redraw the part of window W intersection rectangle FR. Pixel
29143 coordinates in FR are frame-relative. Call this function with
29144 input blocked. Value is non-zero if the exposure overwrites
29145 mouse-face. */
29146
29147 static int
29148 expose_window (struct window *w, XRectangle *fr)
29149 {
29150 struct frame *f = XFRAME (w->frame);
29151 XRectangle wr, r;
29152 int mouse_face_overwritten_p = 0;
29153
29154 /* If window is not yet fully initialized, do nothing. This can
29155 happen when toolkit scroll bars are used and a window is split.
29156 Reconfiguring the scroll bar will generate an expose for a newly
29157 created window. */
29158 if (w->current_matrix == NULL)
29159 return 0;
29160
29161 /* When we're currently updating the window, display and current
29162 matrix usually don't agree. Arrange for a thorough display
29163 later. */
29164 if (w->must_be_updated_p)
29165 {
29166 SET_FRAME_GARBAGED (f);
29167 return 0;
29168 }
29169
29170 /* Frame-relative pixel rectangle of W. */
29171 wr.x = WINDOW_LEFT_EDGE_X (w);
29172 wr.y = WINDOW_TOP_EDGE_Y (w);
29173 wr.width = WINDOW_PIXEL_WIDTH (w);
29174 wr.height = WINDOW_PIXEL_HEIGHT (w);
29175
29176 if (x_intersect_rectangles (fr, &wr, &r))
29177 {
29178 int yb = window_text_bottom_y (w);
29179 struct glyph_row *row;
29180 int cursor_cleared_p, phys_cursor_on_p;
29181 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29182
29183 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29184 r.x, r.y, r.width, r.height));
29185
29186 /* Convert to window coordinates. */
29187 r.x -= WINDOW_LEFT_EDGE_X (w);
29188 r.y -= WINDOW_TOP_EDGE_Y (w);
29189
29190 /* Turn off the cursor. */
29191 if (!w->pseudo_window_p
29192 && phys_cursor_in_rect_p (w, &r))
29193 {
29194 x_clear_cursor (w);
29195 cursor_cleared_p = 1;
29196 }
29197 else
29198 cursor_cleared_p = 0;
29199
29200 /* If the row containing the cursor extends face to end of line,
29201 then expose_area might overwrite the cursor outside the
29202 rectangle and thus notice_overwritten_cursor might clear
29203 w->phys_cursor_on_p. We remember the original value and
29204 check later if it is changed. */
29205 phys_cursor_on_p = w->phys_cursor_on_p;
29206
29207 /* Update lines intersecting rectangle R. */
29208 first_overlapping_row = last_overlapping_row = NULL;
29209 for (row = w->current_matrix->rows;
29210 row->enabled_p;
29211 ++row)
29212 {
29213 int y0 = row->y;
29214 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29215
29216 if ((y0 >= r.y && y0 < r.y + r.height)
29217 || (y1 > r.y && y1 < r.y + r.height)
29218 || (r.y >= y0 && r.y < y1)
29219 || (r.y + r.height > y0 && r.y + r.height < y1))
29220 {
29221 /* A header line may be overlapping, but there is no need
29222 to fix overlapping areas for them. KFS 2005-02-12 */
29223 if (row->overlapping_p && !row->mode_line_p)
29224 {
29225 if (first_overlapping_row == NULL)
29226 first_overlapping_row = row;
29227 last_overlapping_row = row;
29228 }
29229
29230 row->clip = fr;
29231 if (expose_line (w, row, &r))
29232 mouse_face_overwritten_p = 1;
29233 row->clip = NULL;
29234 }
29235 else if (row->overlapping_p)
29236 {
29237 /* We must redraw a row overlapping the exposed area. */
29238 if (y0 < r.y
29239 ? y0 + row->phys_height > r.y
29240 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29241 {
29242 if (first_overlapping_row == NULL)
29243 first_overlapping_row = row;
29244 last_overlapping_row = row;
29245 }
29246 }
29247
29248 if (y1 >= yb)
29249 break;
29250 }
29251
29252 /* Display the mode line if there is one. */
29253 if (WINDOW_WANTS_MODELINE_P (w)
29254 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29255 row->enabled_p)
29256 && row->y < r.y + r.height)
29257 {
29258 if (expose_line (w, row, &r))
29259 mouse_face_overwritten_p = 1;
29260 }
29261
29262 if (!w->pseudo_window_p)
29263 {
29264 /* Fix the display of overlapping rows. */
29265 if (first_overlapping_row)
29266 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29267 fr);
29268
29269 /* Draw border between windows. */
29270 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29271 x_draw_right_divider (w);
29272 else
29273 x_draw_vertical_border (w);
29274
29275 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29276 x_draw_bottom_divider (w);
29277
29278 /* Turn the cursor on again. */
29279 if (cursor_cleared_p
29280 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29281 update_window_cursor (w, 1);
29282 }
29283 }
29284
29285 return mouse_face_overwritten_p;
29286 }
29287
29288
29289
29290 /* Redraw (parts) of all windows in the window tree rooted at W that
29291 intersect R. R contains frame pixel coordinates. Value is
29292 non-zero if the exposure overwrites mouse-face. */
29293
29294 static int
29295 expose_window_tree (struct window *w, XRectangle *r)
29296 {
29297 struct frame *f = XFRAME (w->frame);
29298 int mouse_face_overwritten_p = 0;
29299
29300 while (w && !FRAME_GARBAGED_P (f))
29301 {
29302 if (WINDOWP (w->contents))
29303 mouse_face_overwritten_p
29304 |= expose_window_tree (XWINDOW (w->contents), r);
29305 else
29306 mouse_face_overwritten_p |= expose_window (w, r);
29307
29308 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29309 }
29310
29311 return mouse_face_overwritten_p;
29312 }
29313
29314
29315 /* EXPORT:
29316 Redisplay an exposed area of frame F. X and Y are the upper-left
29317 corner of the exposed rectangle. W and H are width and height of
29318 the exposed area. All are pixel values. W or H zero means redraw
29319 the entire frame. */
29320
29321 void
29322 expose_frame (struct frame *f, int x, int y, int w, int h)
29323 {
29324 XRectangle r;
29325 int mouse_face_overwritten_p = 0;
29326
29327 TRACE ((stderr, "expose_frame "));
29328
29329 /* No need to redraw if frame will be redrawn soon. */
29330 if (FRAME_GARBAGED_P (f))
29331 {
29332 TRACE ((stderr, " garbaged\n"));
29333 return;
29334 }
29335
29336 /* If basic faces haven't been realized yet, there is no point in
29337 trying to redraw anything. This can happen when we get an expose
29338 event while Emacs is starting, e.g. by moving another window. */
29339 if (FRAME_FACE_CACHE (f) == NULL
29340 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29341 {
29342 TRACE ((stderr, " no faces\n"));
29343 return;
29344 }
29345
29346 if (w == 0 || h == 0)
29347 {
29348 r.x = r.y = 0;
29349 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29350 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29351 }
29352 else
29353 {
29354 r.x = x;
29355 r.y = y;
29356 r.width = w;
29357 r.height = h;
29358 }
29359
29360 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29361 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29362
29363 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29364 if (WINDOWP (f->tool_bar_window))
29365 mouse_face_overwritten_p
29366 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29367 #endif
29368
29369 #ifdef HAVE_X_WINDOWS
29370 #ifndef MSDOS
29371 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29372 if (WINDOWP (f->menu_bar_window))
29373 mouse_face_overwritten_p
29374 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29375 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29376 #endif
29377 #endif
29378
29379 /* Some window managers support a focus-follows-mouse style with
29380 delayed raising of frames. Imagine a partially obscured frame,
29381 and moving the mouse into partially obscured mouse-face on that
29382 frame. The visible part of the mouse-face will be highlighted,
29383 then the WM raises the obscured frame. With at least one WM, KDE
29384 2.1, Emacs is not getting any event for the raising of the frame
29385 (even tried with SubstructureRedirectMask), only Expose events.
29386 These expose events will draw text normally, i.e. not
29387 highlighted. Which means we must redo the highlight here.
29388 Subsume it under ``we love X''. --gerd 2001-08-15 */
29389 /* Included in Windows version because Windows most likely does not
29390 do the right thing if any third party tool offers
29391 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29392 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29393 {
29394 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29395 if (f == hlinfo->mouse_face_mouse_frame)
29396 {
29397 int mouse_x = hlinfo->mouse_face_mouse_x;
29398 int mouse_y = hlinfo->mouse_face_mouse_y;
29399 clear_mouse_face (hlinfo);
29400 note_mouse_highlight (f, mouse_x, mouse_y);
29401 }
29402 }
29403 }
29404
29405
29406 /* EXPORT:
29407 Determine the intersection of two rectangles R1 and R2. Return
29408 the intersection in *RESULT. Value is non-zero if RESULT is not
29409 empty. */
29410
29411 int
29412 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29413 {
29414 XRectangle *left, *right;
29415 XRectangle *upper, *lower;
29416 int intersection_p = 0;
29417
29418 /* Rearrange so that R1 is the left-most rectangle. */
29419 if (r1->x < r2->x)
29420 left = r1, right = r2;
29421 else
29422 left = r2, right = r1;
29423
29424 /* X0 of the intersection is right.x0, if this is inside R1,
29425 otherwise there is no intersection. */
29426 if (right->x <= left->x + left->width)
29427 {
29428 result->x = right->x;
29429
29430 /* The right end of the intersection is the minimum of
29431 the right ends of left and right. */
29432 result->width = (min (left->x + left->width, right->x + right->width)
29433 - result->x);
29434
29435 /* Same game for Y. */
29436 if (r1->y < r2->y)
29437 upper = r1, lower = r2;
29438 else
29439 upper = r2, lower = r1;
29440
29441 /* The upper end of the intersection is lower.y0, if this is inside
29442 of upper. Otherwise, there is no intersection. */
29443 if (lower->y <= upper->y + upper->height)
29444 {
29445 result->y = lower->y;
29446
29447 /* The lower end of the intersection is the minimum of the lower
29448 ends of upper and lower. */
29449 result->height = (min (lower->y + lower->height,
29450 upper->y + upper->height)
29451 - result->y);
29452 intersection_p = 1;
29453 }
29454 }
29455
29456 return intersection_p;
29457 }
29458
29459 #endif /* HAVE_WINDOW_SYSTEM */
29460
29461 \f
29462 /***********************************************************************
29463 Initialization
29464 ***********************************************************************/
29465
29466 void
29467 syms_of_xdisp (void)
29468 {
29469 Vwith_echo_area_save_vector = Qnil;
29470 staticpro (&Vwith_echo_area_save_vector);
29471
29472 Vmessage_stack = Qnil;
29473 staticpro (&Vmessage_stack);
29474
29475 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29476 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29477
29478 message_dolog_marker1 = Fmake_marker ();
29479 staticpro (&message_dolog_marker1);
29480 message_dolog_marker2 = Fmake_marker ();
29481 staticpro (&message_dolog_marker2);
29482 message_dolog_marker3 = Fmake_marker ();
29483 staticpro (&message_dolog_marker3);
29484
29485 #ifdef GLYPH_DEBUG
29486 defsubr (&Sdump_frame_glyph_matrix);
29487 defsubr (&Sdump_glyph_matrix);
29488 defsubr (&Sdump_glyph_row);
29489 defsubr (&Sdump_tool_bar_row);
29490 defsubr (&Strace_redisplay);
29491 defsubr (&Strace_to_stderr);
29492 #endif
29493 #ifdef HAVE_WINDOW_SYSTEM
29494 defsubr (&Stool_bar_height);
29495 defsubr (&Slookup_image_map);
29496 #endif
29497 defsubr (&Sline_pixel_height);
29498 defsubr (&Sformat_mode_line);
29499 defsubr (&Sinvisible_p);
29500 defsubr (&Scurrent_bidi_paragraph_direction);
29501 defsubr (&Swindow_text_pixel_size);
29502 defsubr (&Smove_point_visually);
29503
29504 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29505 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29506 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29507 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29508 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29509 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29510 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29511 DEFSYM (Qeval, "eval");
29512 DEFSYM (QCdata, ":data");
29513 DEFSYM (Qdisplay, "display");
29514 DEFSYM (Qspace_width, "space-width");
29515 DEFSYM (Qraise, "raise");
29516 DEFSYM (Qslice, "slice");
29517 DEFSYM (Qspace, "space");
29518 DEFSYM (Qmargin, "margin");
29519 DEFSYM (Qpointer, "pointer");
29520 DEFSYM (Qleft_margin, "left-margin");
29521 DEFSYM (Qright_margin, "right-margin");
29522 DEFSYM (Qcenter, "center");
29523 DEFSYM (Qline_height, "line-height");
29524 DEFSYM (QCalign_to, ":align-to");
29525 DEFSYM (QCrelative_width, ":relative-width");
29526 DEFSYM (QCrelative_height, ":relative-height");
29527 DEFSYM (QCeval, ":eval");
29528 DEFSYM (QCpropertize, ":propertize");
29529 DEFSYM (QCfile, ":file");
29530 DEFSYM (Qfontified, "fontified");
29531 DEFSYM (Qfontification_functions, "fontification-functions");
29532 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29533 DEFSYM (Qescape_glyph, "escape-glyph");
29534 DEFSYM (Qnobreak_space, "nobreak-space");
29535 DEFSYM (Qimage, "image");
29536 DEFSYM (Qtext, "text");
29537 DEFSYM (Qboth, "both");
29538 DEFSYM (Qboth_horiz, "both-horiz");
29539 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29540 DEFSYM (QCmap, ":map");
29541 DEFSYM (QCpointer, ":pointer");
29542 DEFSYM (Qrect, "rect");
29543 DEFSYM (Qcircle, "circle");
29544 DEFSYM (Qpoly, "poly");
29545 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29546 DEFSYM (Qgrow_only, "grow-only");
29547 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29548 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29549 DEFSYM (Qposition, "position");
29550 DEFSYM (Qbuffer_position, "buffer-position");
29551 DEFSYM (Qobject, "object");
29552 DEFSYM (Qbar, "bar");
29553 DEFSYM (Qhbar, "hbar");
29554 DEFSYM (Qbox, "box");
29555 DEFSYM (Qhollow, "hollow");
29556 DEFSYM (Qhand, "hand");
29557 DEFSYM (Qarrow, "arrow");
29558 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29559
29560 list_of_error = list1 (list2 (intern_c_string ("error"),
29561 intern_c_string ("void-variable")));
29562 staticpro (&list_of_error);
29563
29564 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29565 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29566 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29567 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29568
29569 echo_buffer[0] = echo_buffer[1] = Qnil;
29570 staticpro (&echo_buffer[0]);
29571 staticpro (&echo_buffer[1]);
29572
29573 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29574 staticpro (&echo_area_buffer[0]);
29575 staticpro (&echo_area_buffer[1]);
29576
29577 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29578 staticpro (&Vmessages_buffer_name);
29579
29580 mode_line_proptrans_alist = Qnil;
29581 staticpro (&mode_line_proptrans_alist);
29582 mode_line_string_list = Qnil;
29583 staticpro (&mode_line_string_list);
29584 mode_line_string_face = Qnil;
29585 staticpro (&mode_line_string_face);
29586 mode_line_string_face_prop = Qnil;
29587 staticpro (&mode_line_string_face_prop);
29588 Vmode_line_unwind_vector = Qnil;
29589 staticpro (&Vmode_line_unwind_vector);
29590
29591 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29592
29593 help_echo_string = Qnil;
29594 staticpro (&help_echo_string);
29595 help_echo_object = Qnil;
29596 staticpro (&help_echo_object);
29597 help_echo_window = Qnil;
29598 staticpro (&help_echo_window);
29599 previous_help_echo_string = Qnil;
29600 staticpro (&previous_help_echo_string);
29601 help_echo_pos = -1;
29602
29603 DEFSYM (Qright_to_left, "right-to-left");
29604 DEFSYM (Qleft_to_right, "left-to-right");
29605
29606 #ifdef HAVE_WINDOW_SYSTEM
29607 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29608 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29609 For example, if a block cursor is over a tab, it will be drawn as
29610 wide as that tab on the display. */);
29611 x_stretch_cursor_p = 0;
29612 #endif
29613
29614 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29615 doc: /* Non-nil means highlight trailing whitespace.
29616 The face used for trailing whitespace is `trailing-whitespace'. */);
29617 Vshow_trailing_whitespace = Qnil;
29618
29619 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29620 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29621 If the value is t, Emacs highlights non-ASCII chars which have the
29622 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29623 or `escape-glyph' face respectively.
29624
29625 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29626 U+2011 (non-breaking hyphen) are affected.
29627
29628 Any other non-nil value means to display these characters as a escape
29629 glyph followed by an ordinary space or hyphen.
29630
29631 A value of nil means no special handling of these characters. */);
29632 Vnobreak_char_display = Qt;
29633
29634 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29635 doc: /* The pointer shape to show in void text areas.
29636 A value of nil means to show the text pointer. Other options are `arrow',
29637 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29638 Vvoid_text_area_pointer = Qarrow;
29639
29640 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29641 doc: /* Non-nil means don't actually do any redisplay.
29642 This is used for internal purposes. */);
29643 Vinhibit_redisplay = Qnil;
29644
29645 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29646 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29647 Vglobal_mode_string = Qnil;
29648
29649 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29650 doc: /* Marker for where to display an arrow on top of the buffer text.
29651 This must be the beginning of a line in order to work.
29652 See also `overlay-arrow-string'. */);
29653 Voverlay_arrow_position = Qnil;
29654
29655 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29656 doc: /* String to display as an arrow in non-window frames.
29657 See also `overlay-arrow-position'. */);
29658 Voverlay_arrow_string = build_pure_c_string ("=>");
29659
29660 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29661 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29662 The symbols on this list are examined during redisplay to determine
29663 where to display overlay arrows. */);
29664 Voverlay_arrow_variable_list
29665 = list1 (intern_c_string ("overlay-arrow-position"));
29666
29667 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29668 doc: /* The number of lines to try scrolling a window by when point moves out.
29669 If that fails to bring point back on frame, point is centered instead.
29670 If this is zero, point is always centered after it moves off frame.
29671 If you want scrolling to always be a line at a time, you should set
29672 `scroll-conservatively' to a large value rather than set this to 1. */);
29673
29674 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29675 doc: /* Scroll up to this many lines, to bring point back on screen.
29676 If point moves off-screen, redisplay will scroll by up to
29677 `scroll-conservatively' lines in order to bring point just barely
29678 onto the screen again. If that cannot be done, then redisplay
29679 recenters point as usual.
29680
29681 If the value is greater than 100, redisplay will never recenter point,
29682 but will always scroll just enough text to bring point into view, even
29683 if you move far away.
29684
29685 A value of zero means always recenter point if it moves off screen. */);
29686 scroll_conservatively = 0;
29687
29688 DEFVAR_INT ("scroll-margin", scroll_margin,
29689 doc: /* Number of lines of margin at the top and bottom of a window.
29690 Recenter the window whenever point gets within this many lines
29691 of the top or bottom of the window. */);
29692 scroll_margin = 0;
29693
29694 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29695 doc: /* Pixels per inch value for non-window system displays.
29696 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29697 Vdisplay_pixels_per_inch = make_float (72.0);
29698
29699 #ifdef GLYPH_DEBUG
29700 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29701 #endif
29702
29703 DEFVAR_LISP ("truncate-partial-width-windows",
29704 Vtruncate_partial_width_windows,
29705 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29706 For an integer value, truncate lines in each window narrower than the
29707 full frame width, provided the window width is less than that integer;
29708 otherwise, respect the value of `truncate-lines'.
29709
29710 For any other non-nil value, truncate lines in all windows that do
29711 not span the full frame width.
29712
29713 A value of nil means to respect the value of `truncate-lines'.
29714
29715 If `word-wrap' is enabled, you might want to reduce this. */);
29716 Vtruncate_partial_width_windows = make_number (50);
29717
29718 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29719 doc: /* Maximum buffer size for which line number should be displayed.
29720 If the buffer is bigger than this, the line number does not appear
29721 in the mode line. A value of nil means no limit. */);
29722 Vline_number_display_limit = Qnil;
29723
29724 DEFVAR_INT ("line-number-display-limit-width",
29725 line_number_display_limit_width,
29726 doc: /* Maximum line width (in characters) for line number display.
29727 If the average length of the lines near point is bigger than this, then the
29728 line number may be omitted from the mode line. */);
29729 line_number_display_limit_width = 200;
29730
29731 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29732 doc: /* Non-nil means highlight region even in nonselected windows. */);
29733 highlight_nonselected_windows = 0;
29734
29735 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29736 doc: /* Non-nil if more than one frame is visible on this display.
29737 Minibuffer-only frames don't count, but iconified frames do.
29738 This variable is not guaranteed to be accurate except while processing
29739 `frame-title-format' and `icon-title-format'. */);
29740
29741 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29742 doc: /* Template for displaying the title bar of visible frames.
29743 \(Assuming the window manager supports this feature.)
29744
29745 This variable has the same structure as `mode-line-format', except that
29746 the %c and %l constructs are ignored. It is used only on frames for
29747 which no explicit name has been set \(see `modify-frame-parameters'). */);
29748
29749 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29750 doc: /* Template for displaying the title bar of an iconified frame.
29751 \(Assuming the window manager supports this feature.)
29752 This variable has the same structure as `mode-line-format' (which see),
29753 and is used only on frames for which no explicit name has been set
29754 \(see `modify-frame-parameters'). */);
29755 Vicon_title_format
29756 = Vframe_title_format
29757 = listn (CONSTYPE_PURE, 3,
29758 intern_c_string ("multiple-frames"),
29759 build_pure_c_string ("%b"),
29760 listn (CONSTYPE_PURE, 4,
29761 empty_unibyte_string,
29762 intern_c_string ("invocation-name"),
29763 build_pure_c_string ("@"),
29764 intern_c_string ("system-name")));
29765
29766 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29767 doc: /* Maximum number of lines to keep in the message log buffer.
29768 If nil, disable message logging. If t, log messages but don't truncate
29769 the buffer when it becomes large. */);
29770 Vmessage_log_max = make_number (1000);
29771
29772 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29773 doc: /* Functions called before redisplay, if window sizes have changed.
29774 The value should be a list of functions that take one argument.
29775 Just before redisplay, for each frame, if any of its windows have changed
29776 size since the last redisplay, or have been split or deleted,
29777 all the functions in the list are called, with the frame as argument. */);
29778 Vwindow_size_change_functions = Qnil;
29779
29780 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29781 doc: /* List of functions to call before redisplaying a window with scrolling.
29782 Each function is called with two arguments, the window and its new
29783 display-start position. Note that these functions are also called by
29784 `set-window-buffer'. Also note that the value of `window-end' is not
29785 valid when these functions are called.
29786
29787 Warning: Do not use this feature to alter the way the window
29788 is scrolled. It is not designed for that, and such use probably won't
29789 work. */);
29790 Vwindow_scroll_functions = Qnil;
29791
29792 DEFVAR_LISP ("window-text-change-functions",
29793 Vwindow_text_change_functions,
29794 doc: /* Functions to call in redisplay when text in the window might change. */);
29795 Vwindow_text_change_functions = Qnil;
29796
29797 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29798 doc: /* Functions called when redisplay of a window reaches the end trigger.
29799 Each function is called with two arguments, the window and the end trigger value.
29800 See `set-window-redisplay-end-trigger'. */);
29801 Vredisplay_end_trigger_functions = Qnil;
29802
29803 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29804 doc: /* Non-nil means autoselect window with mouse pointer.
29805 If nil, do not autoselect windows.
29806 A positive number means delay autoselection by that many seconds: a
29807 window is autoselected only after the mouse has remained in that
29808 window for the duration of the delay.
29809 A negative number has a similar effect, but causes windows to be
29810 autoselected only after the mouse has stopped moving. \(Because of
29811 the way Emacs compares mouse events, you will occasionally wait twice
29812 that time before the window gets selected.\)
29813 Any other value means to autoselect window instantaneously when the
29814 mouse pointer enters it.
29815
29816 Autoselection selects the minibuffer only if it is active, and never
29817 unselects the minibuffer if it is active.
29818
29819 When customizing this variable make sure that the actual value of
29820 `focus-follows-mouse' matches the behavior of your window manager. */);
29821 Vmouse_autoselect_window = Qnil;
29822
29823 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29824 doc: /* Non-nil means automatically resize tool-bars.
29825 This dynamically changes the tool-bar's height to the minimum height
29826 that is needed to make all tool-bar items visible.
29827 If value is `grow-only', the tool-bar's height is only increased
29828 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29829 Vauto_resize_tool_bars = Qt;
29830
29831 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29832 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29833 auto_raise_tool_bar_buttons_p = 1;
29834
29835 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29836 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29837 make_cursor_line_fully_visible_p = 1;
29838
29839 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29840 doc: /* Border below tool-bar in pixels.
29841 If an integer, use it as the height of the border.
29842 If it is one of `internal-border-width' or `border-width', use the
29843 value of the corresponding frame parameter.
29844 Otherwise, no border is added below the tool-bar. */);
29845 Vtool_bar_border = Qinternal_border_width;
29846
29847 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29848 doc: /* Margin around tool-bar buttons in pixels.
29849 If an integer, use that for both horizontal and vertical margins.
29850 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29851 HORZ specifying the horizontal margin, and VERT specifying the
29852 vertical margin. */);
29853 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29854
29855 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29856 doc: /* Relief thickness of tool-bar buttons. */);
29857 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29858
29859 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29860 doc: /* Tool bar style to use.
29861 It can be one of
29862 image - show images only
29863 text - show text only
29864 both - show both, text below image
29865 both-horiz - show text to the right of the image
29866 text-image-horiz - show text to the left of the image
29867 any other - use system default or image if no system default.
29868
29869 This variable only affects the GTK+ toolkit version of Emacs. */);
29870 Vtool_bar_style = Qnil;
29871
29872 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29873 doc: /* Maximum number of characters a label can have to be shown.
29874 The tool bar style must also show labels for this to have any effect, see
29875 `tool-bar-style'. */);
29876 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29877
29878 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29879 doc: /* List of functions to call to fontify regions of text.
29880 Each function is called with one argument POS. Functions must
29881 fontify a region starting at POS in the current buffer, and give
29882 fontified regions the property `fontified'. */);
29883 Vfontification_functions = Qnil;
29884 Fmake_variable_buffer_local (Qfontification_functions);
29885
29886 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29887 unibyte_display_via_language_environment,
29888 doc: /* Non-nil means display unibyte text according to language environment.
29889 Specifically, this means that raw bytes in the range 160-255 decimal
29890 are displayed by converting them to the equivalent multibyte characters
29891 according to the current language environment. As a result, they are
29892 displayed according to the current fontset.
29893
29894 Note that this variable affects only how these bytes are displayed,
29895 but does not change the fact they are interpreted as raw bytes. */);
29896 unibyte_display_via_language_environment = 0;
29897
29898 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29899 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29900 If a float, it specifies a fraction of the mini-window frame's height.
29901 If an integer, it specifies a number of lines. */);
29902 Vmax_mini_window_height = make_float (0.25);
29903
29904 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29905 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29906 A value of nil means don't automatically resize mini-windows.
29907 A value of t means resize them to fit the text displayed in them.
29908 A value of `grow-only', the default, means let mini-windows grow only;
29909 they return to their normal size when the minibuffer is closed, or the
29910 echo area becomes empty. */);
29911 Vresize_mini_windows = Qgrow_only;
29912
29913 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29914 doc: /* Alist specifying how to blink the cursor off.
29915 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29916 `cursor-type' frame-parameter or variable equals ON-STATE,
29917 comparing using `equal', Emacs uses OFF-STATE to specify
29918 how to blink it off. ON-STATE and OFF-STATE are values for
29919 the `cursor-type' frame parameter.
29920
29921 If a frame's ON-STATE has no entry in this list,
29922 the frame's other specifications determine how to blink the cursor off. */);
29923 Vblink_cursor_alist = Qnil;
29924
29925 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29926 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29927 If non-nil, windows are automatically scrolled horizontally to make
29928 point visible. */);
29929 automatic_hscrolling_p = 1;
29930 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29931
29932 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29933 doc: /* How many columns away from the window edge point is allowed to get
29934 before automatic hscrolling will horizontally scroll the window. */);
29935 hscroll_margin = 5;
29936
29937 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29938 doc: /* How many columns to scroll the window when point gets too close to the edge.
29939 When point is less than `hscroll-margin' columns from the window
29940 edge, automatic hscrolling will scroll the window by the amount of columns
29941 determined by this variable. If its value is a positive integer, scroll that
29942 many columns. If it's a positive floating-point number, it specifies the
29943 fraction of the window's width to scroll. If it's nil or zero, point will be
29944 centered horizontally after the scroll. Any other value, including negative
29945 numbers, are treated as if the value were zero.
29946
29947 Automatic hscrolling always moves point outside the scroll margin, so if
29948 point was more than scroll step columns inside the margin, the window will
29949 scroll more than the value given by the scroll step.
29950
29951 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29952 and `scroll-right' overrides this variable's effect. */);
29953 Vhscroll_step = make_number (0);
29954
29955 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29956 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29957 Bind this around calls to `message' to let it take effect. */);
29958 message_truncate_lines = 0;
29959
29960 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29961 doc: /* Normal hook run to update the menu bar definitions.
29962 Redisplay runs this hook before it redisplays the menu bar.
29963 This is used to update submenus such as Buffers,
29964 whose contents depend on various data. */);
29965 Vmenu_bar_update_hook = Qnil;
29966
29967 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29968 doc: /* Frame for which we are updating a menu.
29969 The enable predicate for a menu binding should check this variable. */);
29970 Vmenu_updating_frame = Qnil;
29971
29972 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29973 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29974 inhibit_menubar_update = 0;
29975
29976 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29977 doc: /* Prefix prepended to all continuation lines at display time.
29978 The value may be a string, an image, or a stretch-glyph; it is
29979 interpreted in the same way as the value of a `display' text property.
29980
29981 This variable is overridden by any `wrap-prefix' text or overlay
29982 property.
29983
29984 To add a prefix to non-continuation lines, use `line-prefix'. */);
29985 Vwrap_prefix = Qnil;
29986 DEFSYM (Qwrap_prefix, "wrap-prefix");
29987 Fmake_variable_buffer_local (Qwrap_prefix);
29988
29989 DEFVAR_LISP ("line-prefix", Vline_prefix,
29990 doc: /* Prefix prepended to all non-continuation lines at display time.
29991 The value may be a string, an image, or a stretch-glyph; it is
29992 interpreted in the same way as the value of a `display' text property.
29993
29994 This variable is overridden by any `line-prefix' text or overlay
29995 property.
29996
29997 To add a prefix to continuation lines, use `wrap-prefix'. */);
29998 Vline_prefix = Qnil;
29999 DEFSYM (Qline_prefix, "line-prefix");
30000 Fmake_variable_buffer_local (Qline_prefix);
30001
30002 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30003 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30004 inhibit_eval_during_redisplay = 0;
30005
30006 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30007 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30008 inhibit_free_realized_faces = 0;
30009
30010 #ifdef GLYPH_DEBUG
30011 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30012 doc: /* Inhibit try_window_id display optimization. */);
30013 inhibit_try_window_id = 0;
30014
30015 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30016 doc: /* Inhibit try_window_reusing display optimization. */);
30017 inhibit_try_window_reusing = 0;
30018
30019 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30020 doc: /* Inhibit try_cursor_movement display optimization. */);
30021 inhibit_try_cursor_movement = 0;
30022 #endif /* GLYPH_DEBUG */
30023
30024 DEFVAR_INT ("overline-margin", overline_margin,
30025 doc: /* Space between overline and text, in pixels.
30026 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30027 margin to the character height. */);
30028 overline_margin = 2;
30029
30030 DEFVAR_INT ("underline-minimum-offset",
30031 underline_minimum_offset,
30032 doc: /* Minimum distance between baseline and underline.
30033 This can improve legibility of underlined text at small font sizes,
30034 particularly when using variable `x-use-underline-position-properties'
30035 with fonts that specify an UNDERLINE_POSITION relatively close to the
30036 baseline. The default value is 1. */);
30037 underline_minimum_offset = 1;
30038
30039 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30040 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30041 This feature only works when on a window system that can change
30042 cursor shapes. */);
30043 display_hourglass_p = 1;
30044
30045 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30046 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30047 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30048
30049 #ifdef HAVE_WINDOW_SYSTEM
30050 hourglass_atimer = NULL;
30051 hourglass_shown_p = 0;
30052 #endif /* HAVE_WINDOW_SYSTEM */
30053
30054 DEFSYM (Qglyphless_char, "glyphless-char");
30055 DEFSYM (Qhex_code, "hex-code");
30056 DEFSYM (Qempty_box, "empty-box");
30057 DEFSYM (Qthin_space, "thin-space");
30058 DEFSYM (Qzero_width, "zero-width");
30059
30060 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30061 doc: /* Function run just before redisplay.
30062 It is called with one argument, which is the set of windows that are to
30063 be redisplayed. This set can be nil (meaning, only the selected window),
30064 or t (meaning all windows). */);
30065 Vpre_redisplay_function = intern ("ignore");
30066
30067 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30068 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30069
30070 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30071 doc: /* Char-table defining glyphless characters.
30072 Each element, if non-nil, should be one of the following:
30073 an ASCII acronym string: display this string in a box
30074 `hex-code': display the hexadecimal code of a character in a box
30075 `empty-box': display as an empty box
30076 `thin-space': display as 1-pixel width space
30077 `zero-width': don't display
30078 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30079 display method for graphical terminals and text terminals respectively.
30080 GRAPHICAL and TEXT should each have one of the values listed above.
30081
30082 The char-table has one extra slot to control the display of a character for
30083 which no font is found. This slot only takes effect on graphical terminals.
30084 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30085 `thin-space'. The default is `empty-box'.
30086
30087 If a character has a non-nil entry in an active display table, the
30088 display table takes effect; in this case, Emacs does not consult
30089 `glyphless-char-display' at all. */);
30090 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30091 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30092 Qempty_box);
30093
30094 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30095 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30096 Vdebug_on_message = Qnil;
30097
30098 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30099 doc: /* */);
30100 Vredisplay__all_windows_cause
30101 = Fmake_vector (make_number (100), make_number (0));
30102
30103 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30104 doc: /* */);
30105 Vredisplay__mode_lines_cause
30106 = Fmake_vector (make_number (100), make_number (0));
30107 }
30108
30109
30110 /* Initialize this module when Emacs starts. */
30111
30112 void
30113 init_xdisp (void)
30114 {
30115 CHARPOS (this_line_start_pos) = 0;
30116
30117 if (!noninteractive)
30118 {
30119 struct window *m = XWINDOW (minibuf_window);
30120 Lisp_Object frame = m->frame;
30121 struct frame *f = XFRAME (frame);
30122 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30123 struct window *r = XWINDOW (root);
30124 int i;
30125
30126 echo_area_window = minibuf_window;
30127
30128 r->top_line = FRAME_TOP_MARGIN (f);
30129 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30130 r->total_cols = FRAME_COLS (f);
30131 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30132 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30133 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30134
30135 m->top_line = FRAME_LINES (f) - 1;
30136 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30137 m->total_cols = FRAME_COLS (f);
30138 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30139 m->total_lines = 1;
30140 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30141
30142 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30143 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30144 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30145
30146 /* The default ellipsis glyphs `...'. */
30147 for (i = 0; i < 3; ++i)
30148 default_invis_vector[i] = make_number ('.');
30149 }
30150
30151 {
30152 /* Allocate the buffer for frame titles.
30153 Also used for `format-mode-line'. */
30154 int size = 100;
30155 mode_line_noprop_buf = xmalloc (size);
30156 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30157 mode_line_noprop_ptr = mode_line_noprop_buf;
30158 mode_line_target = MODE_LINE_DISPLAY;
30159 }
30160
30161 help_echo_showing_p = 0;
30162 }
30163
30164 #ifdef HAVE_WINDOW_SYSTEM
30165
30166 /* Platform-independent portion of hourglass implementation. */
30167
30168 /* Cancel a currently active hourglass timer, and start a new one. */
30169 void
30170 start_hourglass (void)
30171 {
30172 struct timespec delay;
30173
30174 cancel_hourglass ();
30175
30176 if (INTEGERP (Vhourglass_delay)
30177 && XINT (Vhourglass_delay) > 0)
30178 delay = make_timespec (min (XINT (Vhourglass_delay),
30179 TYPE_MAXIMUM (time_t)),
30180 0);
30181 else if (FLOATP (Vhourglass_delay)
30182 && XFLOAT_DATA (Vhourglass_delay) > 0)
30183 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30184 else
30185 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30186
30187 #ifdef HAVE_NTGUI
30188 {
30189 extern void w32_note_current_window (void);
30190 w32_note_current_window ();
30191 }
30192 #endif /* HAVE_NTGUI */
30193
30194 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30195 show_hourglass, NULL);
30196 }
30197
30198
30199 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30200 shown. */
30201 void
30202 cancel_hourglass (void)
30203 {
30204 if (hourglass_atimer)
30205 {
30206 cancel_atimer (hourglass_atimer);
30207 hourglass_atimer = NULL;
30208 }
30209
30210 if (hourglass_shown_p)
30211 hide_hourglass ();
30212 }
30213
30214 #endif /* HAVE_WINDOW_SYSTEM */