* tramp-tests.el (tramp-test19-directory-files-and-attributes):
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
305
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
309
310 #define INFINITY 10000000
311
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
324
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
331
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
334
335 static Lisp_Object Qfontification_functions;
336
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
340
341 /* Non-nil means don't actually do any redisplay. */
342
343 Lisp_Object Qinhibit_redisplay;
344
345 /* Names of text properties relevant for redisplay. */
346
347 Lisp_Object Qdisplay;
348
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
357
358 #ifdef HAVE_WINDOW_SYSTEM
359
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
362
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
370
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
374
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
378
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390
391 /* Name of the face used to highlight trailing whitespace. */
392
393 static Lisp_Object Qtrailing_whitespace;
394
395 /* Name and number of the face used to highlight escape glyphs. */
396
397 static Lisp_Object Qescape_glyph;
398
399 /* Name and number of the face used to highlight non-breaking spaces. */
400
401 static Lisp_Object Qnobreak_space;
402
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
405
406 Lisp_Object Qimage;
407
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
412
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
418
419 bool noninteractive_need_newline;
420
421 /* Non-zero means print newline to message log before next message. */
422
423 static bool message_log_need_newline;
424
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
431 \f
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
436
437 static struct text_pos this_line_start_pos;
438
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
441
442 static struct text_pos this_line_end_pos;
443
444 /* The vertical positions and the height of this line. */
445
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
449
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
452
453 static int this_line_start_x;
454
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
458
459 static struct text_pos this_line_min_pos;
460
461 /* Buffer that this_line_.* variables are referring to. */
462
463 static struct buffer *this_line_buffer;
464
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 Lisp_Object Qmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static bool overlay_arrow_seen;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
491
492 Lisp_Object echo_area_window;
493
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
498
499 static Lisp_Object Vmessage_stack;
500
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
503
504 static bool message_enable_multibyte;
505
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
511
512 int update_mode_lines;
513
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
520
521 int windows_or_buffers_changed;
522
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
525
526 static bool line_number_displayed;
527
528 /* The name of the *Messages* buffer, a string. */
529
530 static Lisp_Object Vmessages_buffer_name;
531
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
534
535 Lisp_Object echo_area_buffer[2];
536
537 /* The buffers referenced from echo_area_buffer. */
538
539 static Lisp_Object echo_buffer[2];
540
541 /* A vector saved used in with_area_buffer to reduce consing. */
542
543 static Lisp_Object Vwith_echo_area_save_vector;
544
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
547
548 static bool display_last_displayed_message_p;
549
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
552
553 static bool message_buf_print;
554
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
556
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
559
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
562
563 static bool message_cleared_p;
564
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
567
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
571
572 /* Ascent and height of the last line processed by move_it_to. */
573
574 static int last_height;
575
576 /* Non-zero if there's a help-echo in the echo area. */
577
578 bool help_echo_showing_p;
579
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
585
586 #define TEXT_PROP_DISTANCE_LIMIT 100
587
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
602
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
610
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
613
614 void
615 redisplay_other_windows (void)
616 {
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
619 }
620
621 void
622 wset_redisplay (struct window *w)
623 {
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
628 }
629
630 void
631 fset_redisplay (struct frame *f)
632 {
633 redisplay_other_windows ();
634 f->redisplay = true;
635 }
636
637 void
638 bset_redisplay (struct buffer *b)
639 {
640 int count = buffer_window_count (b);
641 if (count > 0)
642 {
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
650 }
651 }
652
653 void
654 bset_update_mode_line (struct buffer *b)
655 {
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
659 }
660
661 #ifdef GLYPH_DEBUG
662
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
665
666 bool trace_redisplay_p;
667
668 #endif /* GLYPH_DEBUG */
669
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
673
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
678
679 static Lisp_Object Qauto_hscroll_mode;
680
681 /* Buffer being redisplayed -- for redisplay_window_error. */
682
683 static struct buffer *displayed_buffer;
684
685 /* Value returned from text property handlers (see below). */
686
687 enum prop_handled
688 {
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
693 };
694
695 /* A description of text properties that redisplay is interested
696 in. */
697
698 struct props
699 {
700 /* The name of the property. */
701 Lisp_Object *name;
702
703 /* A unique index for the property. */
704 enum prop_idx idx;
705
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
709 };
710
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
717
718 /* Properties handled by iterators. */
719
720 static struct props it_props[] =
721 {
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
730 };
731
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
734
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736
737 /* Enumeration returned by some move_it_.* functions internally. */
738
739 enum move_it_result
740 {
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
743
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
746
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
749
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
753
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
757
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
760 };
761
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
766
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
769
770 /* Similarly for the image cache. */
771
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
775
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
779
780 /* True while redisplay_internal is in progress. */
781
782 bool redisplaying_p;
783
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Platform-independent portion of hourglass implementation. */
800
801 #ifdef HAVE_WINDOW_SYSTEM
802
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
805
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
809
810 #endif /* HAVE_WINDOW_SYSTEM */
811
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
814
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
817
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
820
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
824
825 #ifdef HAVE_WINDOW_SYSTEM
826
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
829
830 #endif /* HAVE_WINDOW_SYSTEM */
831
832 /* Function prototypes. */
833
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
843
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
845
846 static void handle_line_prefix (struct it *);
847
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
967
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
970
971 #ifdef HAVE_WINDOW_SYSTEM
972
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
989
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 int
1003 window_text_bottom_y (struct window *w)
1004 {
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1006
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1008
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1018
1019 int
1020 window_box_width (struct window *w, enum glyph_row_area area)
1021 {
1022 int width = w->pixel_width;
1023
1024 if (!w->pseudo_window_p)
1025 {
1026 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1028
1029 if (area == TEXT_AREA)
1030 width -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1036 }
1037
1038 /* With wide margins, fringes, etc. we might end up with a negative
1039 width, correct that here. */
1040 return max (0, width);
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 int
1048 window_box_height (struct window *w)
1049 {
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_PIXEL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1056
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1062
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 {
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 }
1074
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 {
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 }
1086
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1090 }
1091
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. ANY_AREA means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1095
1096 int
1097 window_box_left_offset (struct window *w, enum glyph_row_area area)
1098 {
1099 int x;
1100
1101 if (w->pseudo_window_p)
1102 return 0;
1103
1104 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1105
1106 if (area == TEXT_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA));
1109 else if (area == RIGHT_MARGIN_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA)
1112 + window_box_width (w, TEXT_AREA)
1113 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1114 ? 0
1115 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1116 else if (area == LEFT_MARGIN_AREA
1117 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1118 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1119
1120 /* Don't return more than the window's pixel width. */
1121 return min (x, w->pixel_width);
1122 }
1123
1124
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. ANY_AREA means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 int
1130 window_box_right_offset (struct window *w, enum glyph_row_area area)
1131 {
1132 /* Don't return more than the window's pixel width. */
1133 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1134 w->pixel_width);
1135 }
1136
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. ANY_AREA means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1140
1141 int
1142 window_box_left (struct window *w, enum glyph_row_area area)
1143 {
1144 struct frame *f = XFRAME (w->frame);
1145 int x;
1146
1147 if (w->pseudo_window_p)
1148 return FRAME_INTERNAL_BORDER_WIDTH (f);
1149
1150 x = (WINDOW_LEFT_EDGE_X (w)
1151 + window_box_left_offset (w, area));
1152
1153 return x;
1154 }
1155
1156
1157 /* Return the frame-relative coordinate of the right edge of display
1158 area AREA of window W. ANY_AREA means return the right edge of the
1159 whole window, to the left of the right fringe of W. */
1160
1161 int
1162 window_box_right (struct window *w, enum glyph_row_area area)
1163 {
1164 return window_box_left (w, area) + window_box_width (w, area);
1165 }
1166
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines, in frame-relative coordinates. ANY_AREA means the
1169 whole window, not including the left and right fringes of
1170 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1171 coordinates of the upper-left corner of the box. Return in
1172 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1173
1174 void
1175 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1176 int *box_y, int *box_width, int *box_height)
1177 {
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1185 {
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 }
1190 }
1191
1192 #ifdef HAVE_WINDOW_SYSTEM
1193
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1200
1201 static void
1202 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1203 int *bottom_right_x, int *bottom_right_y)
1204 {
1205 window_box (w, ANY_AREA, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y);
1207 *bottom_right_x += *top_left_x;
1208 *bottom_right_y += *top_left_y;
1209 }
1210
1211 #endif /* HAVE_WINDOW_SYSTEM */
1212
1213 /***********************************************************************
1214 Utilities
1215 ***********************************************************************/
1216
1217 /* Return the bottom y-position of the line the iterator IT is in.
1218 This can modify IT's settings. */
1219
1220 int
1221 line_bottom_y (struct it *it)
1222 {
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1225
1226 if (line_height == 0)
1227 {
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1231 {
1232 move_it_by_lines (it, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1236 }
1237 else
1238 {
1239 struct glyph_row *row = it->glyph_row;
1240
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1249 }
1250 }
1251
1252 return line_top_y + line_height;
1253 }
1254
1255 DEFUN ("line-pixel-height", Fline_pixel_height,
1256 Sline_pixel_height, 0, 0, 0,
1257 doc: /* Return height in pixels of text line in the selected window.
1258
1259 Value is the height in pixels of the line at point. */)
1260 (void)
1261 {
1262 struct it it;
1263 struct text_pos pt;
1264 struct window *w = XWINDOW (selected_window);
1265
1266 SET_TEXT_POS (pt, PT, PT_BYTE);
1267 start_display (&it, w, pt);
1268 it.vpos = it.current_y = 0;
1269 last_height = 0;
1270 return make_number (line_bottom_y (&it));
1271 }
1272
1273 /* Return the default pixel height of text lines in window W. The
1274 value is the canonical height of the W frame's default font, plus
1275 any extra space required by the line-spacing variable or frame
1276 parameter.
1277
1278 Implementation note: this ignores any line-spacing text properties
1279 put on the newline characters. This is because those properties
1280 only affect the _screen_ line ending in the newline (i.e., in a
1281 continued line, only the last screen line will be affected), which
1282 means only a small number of lines in a buffer can ever use this
1283 feature. Since this function is used to compute the default pixel
1284 equivalent of text lines in a window, we can safely ignore those
1285 few lines. For the same reasons, we ignore the line-height
1286 properties. */
1287 int
1288 default_line_pixel_height (struct window *w)
1289 {
1290 struct frame *f = WINDOW_XFRAME (w);
1291 int height = FRAME_LINE_HEIGHT (f);
1292
1293 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1294 {
1295 struct buffer *b = XBUFFER (w->contents);
1296 Lisp_Object val = BVAR (b, extra_line_spacing);
1297
1298 if (NILP (val))
1299 val = BVAR (&buffer_defaults, extra_line_spacing);
1300 if (!NILP (val))
1301 {
1302 if (RANGED_INTEGERP (0, val, INT_MAX))
1303 height += XFASTINT (val);
1304 else if (FLOATP (val))
1305 {
1306 int addon = XFLOAT_DATA (val) * height + 0.5;
1307
1308 if (addon >= 0)
1309 height += addon;
1310 }
1311 }
1312 else
1313 height += f->extra_line_spacing;
1314 }
1315
1316 return height;
1317 }
1318
1319 /* Subroutine of pos_visible_p below. Extracts a display string, if
1320 any, from the display spec given as its argument. */
1321 static Lisp_Object
1322 string_from_display_spec (Lisp_Object spec)
1323 {
1324 if (CONSP (spec))
1325 {
1326 while (CONSP (spec))
1327 {
1328 if (STRINGP (XCAR (spec)))
1329 return XCAR (spec);
1330 spec = XCDR (spec);
1331 }
1332 }
1333 else if (VECTORP (spec))
1334 {
1335 ptrdiff_t i;
1336
1337 for (i = 0; i < ASIZE (spec); i++)
1338 {
1339 if (STRINGP (AREF (spec, i)))
1340 return AREF (spec, i);
1341 }
1342 return Qnil;
1343 }
1344
1345 return spec;
1346 }
1347
1348
1349 /* Limit insanely large values of W->hscroll on frame F to the largest
1350 value that will still prevent first_visible_x and last_visible_x of
1351 'struct it' from overflowing an int. */
1352 static int
1353 window_hscroll_limited (struct window *w, struct frame *f)
1354 {
1355 ptrdiff_t window_hscroll = w->hscroll;
1356 int window_text_width = window_box_width (w, TEXT_AREA);
1357 int colwidth = FRAME_COLUMN_WIDTH (f);
1358
1359 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1360 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1361
1362 return window_hscroll;
1363 }
1364
1365 /* Return 1 if position CHARPOS is visible in window W.
1366 CHARPOS < 0 means return info about WINDOW_END position.
1367 If visible, set *X and *Y to pixel coordinates of top left corner.
1368 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1369 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1370
1371 int
1372 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1373 int *rtop, int *rbot, int *rowh, int *vpos)
1374 {
1375 struct it it;
1376 void *itdata = bidi_shelve_cache ();
1377 struct text_pos top;
1378 int visible_p = 0;
1379 struct buffer *old_buffer = NULL;
1380
1381 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1382 return visible_p;
1383
1384 if (XBUFFER (w->contents) != current_buffer)
1385 {
1386 old_buffer = current_buffer;
1387 set_buffer_internal_1 (XBUFFER (w->contents));
1388 }
1389
1390 SET_TEXT_POS_FROM_MARKER (top, w->start);
1391 /* Scrolling a minibuffer window via scroll bar when the echo area
1392 shows long text sometimes resets the minibuffer contents behind
1393 our backs. */
1394 if (CHARPOS (top) > ZV)
1395 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1396
1397 /* Compute exact mode line heights. */
1398 if (WINDOW_WANTS_MODELINE_P (w))
1399 w->mode_line_height
1400 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1401 BVAR (current_buffer, mode_line_format));
1402
1403 if (WINDOW_WANTS_HEADER_LINE_P (w))
1404 w->header_line_height
1405 = display_mode_line (w, HEADER_LINE_FACE_ID,
1406 BVAR (current_buffer, header_line_format));
1407
1408 start_display (&it, w, top);
1409 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1410 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1411
1412 if (charpos >= 0
1413 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1414 && IT_CHARPOS (it) >= charpos)
1415 /* When scanning backwards under bidi iteration, move_it_to
1416 stops at or _before_ CHARPOS, because it stops at or to
1417 the _right_ of the character at CHARPOS. */
1418 || (it.bidi_p && it.bidi_it.scan_dir == -1
1419 && IT_CHARPOS (it) <= charpos)))
1420 {
1421 /* We have reached CHARPOS, or passed it. How the call to
1422 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1423 or covered by a display property, move_it_to stops at the end
1424 of the invisible text, to the right of CHARPOS. (ii) If
1425 CHARPOS is in a display vector, move_it_to stops on its last
1426 glyph. */
1427 int top_x = it.current_x;
1428 int top_y = it.current_y;
1429 /* Calling line_bottom_y may change it.method, it.position, etc. */
1430 enum it_method it_method = it.method;
1431 int bottom_y = (last_height = 0, line_bottom_y (&it));
1432 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1433
1434 if (top_y < window_top_y)
1435 visible_p = bottom_y > window_top_y;
1436 else if (top_y < it.last_visible_y)
1437 visible_p = true;
1438 if (bottom_y >= it.last_visible_y
1439 && it.bidi_p && it.bidi_it.scan_dir == -1
1440 && IT_CHARPOS (it) < charpos)
1441 {
1442 /* When the last line of the window is scanned backwards
1443 under bidi iteration, we could be duped into thinking
1444 that we have passed CHARPOS, when in fact move_it_to
1445 simply stopped short of CHARPOS because it reached
1446 last_visible_y. To see if that's what happened, we call
1447 move_it_to again with a slightly larger vertical limit,
1448 and see if it actually moved vertically; if it did, we
1449 didn't really reach CHARPOS, which is beyond window end. */
1450 struct it save_it = it;
1451 /* Why 10? because we don't know how many canonical lines
1452 will the height of the next line(s) be. So we guess. */
1453 int ten_more_lines = 10 * default_line_pixel_height (w);
1454
1455 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1456 MOVE_TO_POS | MOVE_TO_Y);
1457 if (it.current_y > top_y)
1458 visible_p = 0;
1459
1460 it = save_it;
1461 }
1462 if (visible_p)
1463 {
1464 if (it_method == GET_FROM_DISPLAY_VECTOR)
1465 {
1466 /* We stopped on the last glyph of a display vector.
1467 Try and recompute. Hack alert! */
1468 if (charpos < 2 || top.charpos >= charpos)
1469 top_x = it.glyph_row->x;
1470 else
1471 {
1472 struct it it2, it2_prev;
1473 /* The idea is to get to the previous buffer
1474 position, consume the character there, and use
1475 the pixel coordinates we get after that. But if
1476 the previous buffer position is also displayed
1477 from a display vector, we need to consume all of
1478 the glyphs from that display vector. */
1479 start_display (&it2, w, top);
1480 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1481 /* If we didn't get to CHARPOS - 1, there's some
1482 replacing display property at that position, and
1483 we stopped after it. That is exactly the place
1484 whose coordinates we want. */
1485 if (IT_CHARPOS (it2) != charpos - 1)
1486 it2_prev = it2;
1487 else
1488 {
1489 /* Iterate until we get out of the display
1490 vector that displays the character at
1491 CHARPOS - 1. */
1492 do {
1493 get_next_display_element (&it2);
1494 PRODUCE_GLYPHS (&it2);
1495 it2_prev = it2;
1496 set_iterator_to_next (&it2, 1);
1497 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1498 && IT_CHARPOS (it2) < charpos);
1499 }
1500 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1501 || it2_prev.current_x > it2_prev.last_visible_x)
1502 top_x = it.glyph_row->x;
1503 else
1504 {
1505 top_x = it2_prev.current_x;
1506 top_y = it2_prev.current_y;
1507 }
1508 }
1509 }
1510 else if (IT_CHARPOS (it) != charpos)
1511 {
1512 Lisp_Object cpos = make_number (charpos);
1513 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1514 Lisp_Object string = string_from_display_spec (spec);
1515 struct text_pos tpos;
1516 int replacing_spec_p;
1517 bool newline_in_string
1518 = (STRINGP (string)
1519 && memchr (SDATA (string), '\n', SBYTES (string)));
1520
1521 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1522 replacing_spec_p
1523 = (!NILP (spec)
1524 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1525 charpos, FRAME_WINDOW_P (it.f)));
1526 /* The tricky code below is needed because there's a
1527 discrepancy between move_it_to and how we set cursor
1528 when PT is at the beginning of a portion of text
1529 covered by a display property or an overlay with a
1530 display property, or the display line ends in a
1531 newline from a display string. move_it_to will stop
1532 _after_ such display strings, whereas
1533 set_cursor_from_row conspires with cursor_row_p to
1534 place the cursor on the first glyph produced from the
1535 display string. */
1536
1537 /* We have overshoot PT because it is covered by a
1538 display property that replaces the text it covers.
1539 If the string includes embedded newlines, we are also
1540 in the wrong display line. Backtrack to the correct
1541 line, where the display property begins. */
1542 if (replacing_spec_p)
1543 {
1544 Lisp_Object startpos, endpos;
1545 EMACS_INT start, end;
1546 struct it it3;
1547 int it3_moved;
1548
1549 /* Find the first and the last buffer positions
1550 covered by the display string. */
1551 endpos =
1552 Fnext_single_char_property_change (cpos, Qdisplay,
1553 Qnil, Qnil);
1554 startpos =
1555 Fprevious_single_char_property_change (endpos, Qdisplay,
1556 Qnil, Qnil);
1557 start = XFASTINT (startpos);
1558 end = XFASTINT (endpos);
1559 /* Move to the last buffer position before the
1560 display property. */
1561 start_display (&it3, w, top);
1562 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1563 /* Move forward one more line if the position before
1564 the display string is a newline or if it is the
1565 rightmost character on a line that is
1566 continued or word-wrapped. */
1567 if (it3.method == GET_FROM_BUFFER
1568 && (it3.c == '\n'
1569 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1570 move_it_by_lines (&it3, 1);
1571 else if (move_it_in_display_line_to (&it3, -1,
1572 it3.current_x
1573 + it3.pixel_width,
1574 MOVE_TO_X)
1575 == MOVE_LINE_CONTINUED)
1576 {
1577 move_it_by_lines (&it3, 1);
1578 /* When we are under word-wrap, the #$@%!
1579 move_it_by_lines moves 2 lines, so we need to
1580 fix that up. */
1581 if (it3.line_wrap == WORD_WRAP)
1582 move_it_by_lines (&it3, -1);
1583 }
1584
1585 /* Record the vertical coordinate of the display
1586 line where we wound up. */
1587 top_y = it3.current_y;
1588 if (it3.bidi_p)
1589 {
1590 /* When characters are reordered for display,
1591 the character displayed to the left of the
1592 display string could be _after_ the display
1593 property in the logical order. Use the
1594 smallest vertical position of these two. */
1595 start_display (&it3, w, top);
1596 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1597 if (it3.current_y < top_y)
1598 top_y = it3.current_y;
1599 }
1600 /* Move from the top of the window to the beginning
1601 of the display line where the display string
1602 begins. */
1603 start_display (&it3, w, top);
1604 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1605 /* If it3_moved stays zero after the 'while' loop
1606 below, that means we already were at a newline
1607 before the loop (e.g., the display string begins
1608 with a newline), so we don't need to (and cannot)
1609 inspect the glyphs of it3.glyph_row, because
1610 PRODUCE_GLYPHS will not produce anything for a
1611 newline, and thus it3.glyph_row stays at its
1612 stale content it got at top of the window. */
1613 it3_moved = 0;
1614 /* Finally, advance the iterator until we hit the
1615 first display element whose character position is
1616 CHARPOS, or until the first newline from the
1617 display string, which signals the end of the
1618 display line. */
1619 while (get_next_display_element (&it3))
1620 {
1621 PRODUCE_GLYPHS (&it3);
1622 if (IT_CHARPOS (it3) == charpos
1623 || ITERATOR_AT_END_OF_LINE_P (&it3))
1624 break;
1625 it3_moved = 1;
1626 set_iterator_to_next (&it3, 0);
1627 }
1628 top_x = it3.current_x - it3.pixel_width;
1629 /* Normally, we would exit the above loop because we
1630 found the display element whose character
1631 position is CHARPOS. For the contingency that we
1632 didn't, and stopped at the first newline from the
1633 display string, move back over the glyphs
1634 produced from the string, until we find the
1635 rightmost glyph not from the string. */
1636 if (it3_moved
1637 && newline_in_string
1638 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1639 {
1640 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1641 + it3.glyph_row->used[TEXT_AREA];
1642
1643 while (EQ ((g - 1)->object, string))
1644 {
1645 --g;
1646 top_x -= g->pixel_width;
1647 }
1648 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1649 + it3.glyph_row->used[TEXT_AREA]);
1650 }
1651 }
1652 }
1653
1654 *x = top_x;
1655 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1656 *rtop = max (0, window_top_y - top_y);
1657 *rbot = max (0, bottom_y - it.last_visible_y);
1658 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1659 - max (top_y, window_top_y)));
1660 *vpos = it.vpos;
1661 }
1662 }
1663 else
1664 {
1665 /* We were asked to provide info about WINDOW_END. */
1666 struct it it2;
1667 void *it2data = NULL;
1668
1669 SAVE_IT (it2, it, it2data);
1670 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1671 move_it_by_lines (&it, 1);
1672 if (charpos < IT_CHARPOS (it)
1673 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1674 {
1675 visible_p = true;
1676 RESTORE_IT (&it2, &it2, it2data);
1677 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1678 *x = it2.current_x;
1679 *y = it2.current_y + it2.max_ascent - it2.ascent;
1680 *rtop = max (0, -it2.current_y);
1681 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1682 - it.last_visible_y));
1683 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1684 it.last_visible_y)
1685 - max (it2.current_y,
1686 WINDOW_HEADER_LINE_HEIGHT (w))));
1687 *vpos = it2.vpos;
1688 }
1689 else
1690 bidi_unshelve_cache (it2data, 1);
1691 }
1692 bidi_unshelve_cache (itdata, 0);
1693
1694 if (old_buffer)
1695 set_buffer_internal_1 (old_buffer);
1696
1697 if (visible_p && w->hscroll > 0)
1698 *x -=
1699 window_hscroll_limited (w, WINDOW_XFRAME (w))
1700 * WINDOW_FRAME_COLUMN_WIDTH (w);
1701
1702 #if 0
1703 /* Debugging code. */
1704 if (visible_p)
1705 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1706 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1707 else
1708 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1709 #endif
1710
1711 return visible_p;
1712 }
1713
1714
1715 /* Return the next character from STR. Return in *LEN the length of
1716 the character. This is like STRING_CHAR_AND_LENGTH but never
1717 returns an invalid character. If we find one, we return a `?', but
1718 with the length of the invalid character. */
1719
1720 static int
1721 string_char_and_length (const unsigned char *str, int *len)
1722 {
1723 int c;
1724
1725 c = STRING_CHAR_AND_LENGTH (str, *len);
1726 if (!CHAR_VALID_P (c))
1727 /* We may not change the length here because other places in Emacs
1728 don't use this function, i.e. they silently accept invalid
1729 characters. */
1730 c = '?';
1731
1732 return c;
1733 }
1734
1735
1736
1737 /* Given a position POS containing a valid character and byte position
1738 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1739
1740 static struct text_pos
1741 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1742 {
1743 eassert (STRINGP (string) && nchars >= 0);
1744
1745 if (STRING_MULTIBYTE (string))
1746 {
1747 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1748 int len;
1749
1750 while (nchars--)
1751 {
1752 string_char_and_length (p, &len);
1753 p += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1756 }
1757 }
1758 else
1759 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1760
1761 return pos;
1762 }
1763
1764
1765 /* Value is the text position, i.e. character and byte position,
1766 for character position CHARPOS in STRING. */
1767
1768 static struct text_pos
1769 string_pos (ptrdiff_t charpos, Lisp_Object string)
1770 {
1771 struct text_pos pos;
1772 eassert (STRINGP (string));
1773 eassert (charpos >= 0);
1774 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1775 return pos;
1776 }
1777
1778
1779 /* Value is a text position, i.e. character and byte position, for
1780 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1781 means recognize multibyte characters. */
1782
1783 static struct text_pos
1784 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1785 {
1786 struct text_pos pos;
1787
1788 eassert (s != NULL);
1789 eassert (charpos >= 0);
1790
1791 if (multibyte_p)
1792 {
1793 int len;
1794
1795 SET_TEXT_POS (pos, 0, 0);
1796 while (charpos--)
1797 {
1798 string_char_and_length ((const unsigned char *) s, &len);
1799 s += len;
1800 CHARPOS (pos) += 1;
1801 BYTEPOS (pos) += len;
1802 }
1803 }
1804 else
1805 SET_TEXT_POS (pos, charpos, charpos);
1806
1807 return pos;
1808 }
1809
1810
1811 /* Value is the number of characters in C string S. MULTIBYTE_P
1812 non-zero means recognize multibyte characters. */
1813
1814 static ptrdiff_t
1815 number_of_chars (const char *s, bool multibyte_p)
1816 {
1817 ptrdiff_t nchars;
1818
1819 if (multibyte_p)
1820 {
1821 ptrdiff_t rest = strlen (s);
1822 int len;
1823 const unsigned char *p = (const unsigned char *) s;
1824
1825 for (nchars = 0; rest > 0; ++nchars)
1826 {
1827 string_char_and_length (p, &len);
1828 rest -= len, p += len;
1829 }
1830 }
1831 else
1832 nchars = strlen (s);
1833
1834 return nchars;
1835 }
1836
1837
1838 /* Compute byte position NEWPOS->bytepos corresponding to
1839 NEWPOS->charpos. POS is a known position in string STRING.
1840 NEWPOS->charpos must be >= POS.charpos. */
1841
1842 static void
1843 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1844 {
1845 eassert (STRINGP (string));
1846 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1847
1848 if (STRING_MULTIBYTE (string))
1849 *newpos = string_pos_nchars_ahead (pos, string,
1850 CHARPOS (*newpos) - CHARPOS (pos));
1851 else
1852 BYTEPOS (*newpos) = CHARPOS (*newpos);
1853 }
1854
1855 /* EXPORT:
1856 Return an estimation of the pixel height of mode or header lines on
1857 frame F. FACE_ID specifies what line's height to estimate. */
1858
1859 int
1860 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1861 {
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1864 {
1865 int height = FONT_HEIGHT (FRAME_FONT (f));
1866
1867 /* This function is called so early when Emacs starts that the face
1868 cache and mode line face are not yet initialized. */
1869 if (FRAME_FACE_CACHE (f))
1870 {
1871 struct face *face = FACE_FROM_ID (f, face_id);
1872 if (face)
1873 {
1874 if (face->font)
1875 height = FONT_HEIGHT (face->font);
1876 if (face->box_line_width > 0)
1877 height += 2 * face->box_line_width;
1878 }
1879 }
1880
1881 return height;
1882 }
1883 #endif
1884
1885 return 1;
1886 }
1887
1888 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1889 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1890 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1891 not force the value into range. */
1892
1893 void
1894 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1895 int *x, int *y, NativeRectangle *bounds, int noclip)
1896 {
1897
1898 #ifdef HAVE_WINDOW_SYSTEM
1899 if (FRAME_WINDOW_P (f))
1900 {
1901 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1902 even for negative values. */
1903 if (pix_x < 0)
1904 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1905 if (pix_y < 0)
1906 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1907
1908 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1909 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1910
1911 if (bounds)
1912 STORE_NATIVE_RECT (*bounds,
1913 FRAME_COL_TO_PIXEL_X (f, pix_x),
1914 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1915 FRAME_COLUMN_WIDTH (f) - 1,
1916 FRAME_LINE_HEIGHT (f) - 1);
1917
1918 /* PXW: Should we clip pixels before converting to columns/lines? */
1919 if (!noclip)
1920 {
1921 if (pix_x < 0)
1922 pix_x = 0;
1923 else if (pix_x > FRAME_TOTAL_COLS (f))
1924 pix_x = FRAME_TOTAL_COLS (f);
1925
1926 if (pix_y < 0)
1927 pix_y = 0;
1928 else if (pix_y > FRAME_LINES (f))
1929 pix_y = FRAME_LINES (f);
1930 }
1931 }
1932 #endif
1933
1934 *x = pix_x;
1935 *y = pix_y;
1936 }
1937
1938
1939 /* Find the glyph under window-relative coordinates X/Y in window W.
1940 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1941 strings. Return in *HPOS and *VPOS the row and column number of
1942 the glyph found. Return in *AREA the glyph area containing X.
1943 Value is a pointer to the glyph found or null if X/Y is not on
1944 text, or we can't tell because W's current matrix is not up to
1945 date. */
1946
1947 static struct glyph *
1948 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1949 int *dx, int *dy, int *area)
1950 {
1951 struct glyph *glyph, *end;
1952 struct glyph_row *row = NULL;
1953 int x0, i;
1954
1955 /* Find row containing Y. Give up if some row is not enabled. */
1956 for (i = 0; i < w->current_matrix->nrows; ++i)
1957 {
1958 row = MATRIX_ROW (w->current_matrix, i);
1959 if (!row->enabled_p)
1960 return NULL;
1961 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1962 break;
1963 }
1964
1965 *vpos = i;
1966 *hpos = 0;
1967
1968 /* Give up if Y is not in the window. */
1969 if (i == w->current_matrix->nrows)
1970 return NULL;
1971
1972 /* Get the glyph area containing X. */
1973 if (w->pseudo_window_p)
1974 {
1975 *area = TEXT_AREA;
1976 x0 = 0;
1977 }
1978 else
1979 {
1980 if (x < window_box_left_offset (w, TEXT_AREA))
1981 {
1982 *area = LEFT_MARGIN_AREA;
1983 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1984 }
1985 else if (x < window_box_right_offset (w, TEXT_AREA))
1986 {
1987 *area = TEXT_AREA;
1988 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1989 }
1990 else
1991 {
1992 *area = RIGHT_MARGIN_AREA;
1993 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1994 }
1995 }
1996
1997 /* Find glyph containing X. */
1998 glyph = row->glyphs[*area];
1999 end = glyph + row->used[*area];
2000 x -= x0;
2001 while (glyph < end && x >= glyph->pixel_width)
2002 {
2003 x -= glyph->pixel_width;
2004 ++glyph;
2005 }
2006
2007 if (glyph == end)
2008 return NULL;
2009
2010 if (dx)
2011 {
2012 *dx = x;
2013 *dy = y - (row->y + row->ascent - glyph->ascent);
2014 }
2015
2016 *hpos = glyph - row->glyphs[*area];
2017 return glyph;
2018 }
2019
2020 /* Convert frame-relative x/y to coordinates relative to window W.
2021 Takes pseudo-windows into account. */
2022
2023 static void
2024 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2025 {
2026 if (w->pseudo_window_p)
2027 {
2028 /* A pseudo-window is always full-width, and starts at the
2029 left edge of the frame, plus a frame border. */
2030 struct frame *f = XFRAME (w->frame);
2031 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2033 }
2034 else
2035 {
2036 *x -= WINDOW_LEFT_EDGE_X (w);
2037 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2038 }
2039 }
2040
2041 #ifdef HAVE_WINDOW_SYSTEM
2042
2043 /* EXPORT:
2044 Return in RECTS[] at most N clipping rectangles for glyph string S.
2045 Return the number of stored rectangles. */
2046
2047 int
2048 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2049 {
2050 XRectangle r;
2051
2052 if (n <= 0)
2053 return 0;
2054
2055 if (s->row->full_width_p)
2056 {
2057 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2058 r.x = WINDOW_LEFT_EDGE_X (s->w);
2059 if (s->row->mode_line_p)
2060 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2061 else
2062 r.width = WINDOW_PIXEL_WIDTH (s->w);
2063
2064 /* Unless displaying a mode or menu bar line, which are always
2065 fully visible, clip to the visible part of the row. */
2066 if (s->w->pseudo_window_p)
2067 r.height = s->row->visible_height;
2068 else
2069 r.height = s->height;
2070 }
2071 else
2072 {
2073 /* This is a text line that may be partially visible. */
2074 r.x = window_box_left (s->w, s->area);
2075 r.width = window_box_width (s->w, s->area);
2076 r.height = s->row->visible_height;
2077 }
2078
2079 if (s->clip_head)
2080 if (r.x < s->clip_head->x)
2081 {
2082 if (r.width >= s->clip_head->x - r.x)
2083 r.width -= s->clip_head->x - r.x;
2084 else
2085 r.width = 0;
2086 r.x = s->clip_head->x;
2087 }
2088 if (s->clip_tail)
2089 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2090 {
2091 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2092 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2093 else
2094 r.width = 0;
2095 }
2096
2097 /* If S draws overlapping rows, it's sufficient to use the top and
2098 bottom of the window for clipping because this glyph string
2099 intentionally draws over other lines. */
2100 if (s->for_overlaps)
2101 {
2102 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2103 r.height = window_text_bottom_y (s->w) - r.y;
2104
2105 /* Alas, the above simple strategy does not work for the
2106 environments with anti-aliased text: if the same text is
2107 drawn onto the same place multiple times, it gets thicker.
2108 If the overlap we are processing is for the erased cursor, we
2109 take the intersection with the rectangle of the cursor. */
2110 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2111 {
2112 XRectangle rc, r_save = r;
2113
2114 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2115 rc.y = s->w->phys_cursor.y;
2116 rc.width = s->w->phys_cursor_width;
2117 rc.height = s->w->phys_cursor_height;
2118
2119 x_intersect_rectangles (&r_save, &rc, &r);
2120 }
2121 }
2122 else
2123 {
2124 /* Don't use S->y for clipping because it doesn't take partially
2125 visible lines into account. For example, it can be negative for
2126 partially visible lines at the top of a window. */
2127 if (!s->row->full_width_p
2128 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2129 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2130 else
2131 r.y = max (0, s->row->y);
2132 }
2133
2134 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2135
2136 /* If drawing the cursor, don't let glyph draw outside its
2137 advertised boundaries. Cleartype does this under some circumstances. */
2138 if (s->hl == DRAW_CURSOR)
2139 {
2140 struct glyph *glyph = s->first_glyph;
2141 int height, max_y;
2142
2143 if (s->x > r.x)
2144 {
2145 r.width -= s->x - r.x;
2146 r.x = s->x;
2147 }
2148 r.width = min (r.width, glyph->pixel_width);
2149
2150 /* If r.y is below window bottom, ensure that we still see a cursor. */
2151 height = min (glyph->ascent + glyph->descent,
2152 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2153 max_y = window_text_bottom_y (s->w) - height;
2154 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2155 if (s->ybase - glyph->ascent > max_y)
2156 {
2157 r.y = max_y;
2158 r.height = height;
2159 }
2160 else
2161 {
2162 /* Don't draw cursor glyph taller than our actual glyph. */
2163 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2164 if (height < r.height)
2165 {
2166 max_y = r.y + r.height;
2167 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2168 r.height = min (max_y - r.y, height);
2169 }
2170 }
2171 }
2172
2173 if (s->row->clip)
2174 {
2175 XRectangle r_save = r;
2176
2177 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2178 r.width = 0;
2179 }
2180
2181 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2182 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2183 {
2184 #ifdef CONVERT_FROM_XRECT
2185 CONVERT_FROM_XRECT (r, *rects);
2186 #else
2187 *rects = r;
2188 #endif
2189 return 1;
2190 }
2191 else
2192 {
2193 /* If we are processing overlapping and allowed to return
2194 multiple clipping rectangles, we exclude the row of the glyph
2195 string from the clipping rectangle. This is to avoid drawing
2196 the same text on the environment with anti-aliasing. */
2197 #ifdef CONVERT_FROM_XRECT
2198 XRectangle rs[2];
2199 #else
2200 XRectangle *rs = rects;
2201 #endif
2202 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2203
2204 if (s->for_overlaps & OVERLAPS_PRED)
2205 {
2206 rs[i] = r;
2207 if (r.y + r.height > row_y)
2208 {
2209 if (r.y < row_y)
2210 rs[i].height = row_y - r.y;
2211 else
2212 rs[i].height = 0;
2213 }
2214 i++;
2215 }
2216 if (s->for_overlaps & OVERLAPS_SUCC)
2217 {
2218 rs[i] = r;
2219 if (r.y < row_y + s->row->visible_height)
2220 {
2221 if (r.y + r.height > row_y + s->row->visible_height)
2222 {
2223 rs[i].y = row_y + s->row->visible_height;
2224 rs[i].height = r.y + r.height - rs[i].y;
2225 }
2226 else
2227 rs[i].height = 0;
2228 }
2229 i++;
2230 }
2231
2232 n = i;
2233 #ifdef CONVERT_FROM_XRECT
2234 for (i = 0; i < n; i++)
2235 CONVERT_FROM_XRECT (rs[i], rects[i]);
2236 #endif
2237 return n;
2238 }
2239 }
2240
2241 /* EXPORT:
2242 Return in *NR the clipping rectangle for glyph string S. */
2243
2244 void
2245 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2246 {
2247 get_glyph_string_clip_rects (s, nr, 1);
2248 }
2249
2250
2251 /* EXPORT:
2252 Return the position and height of the phys cursor in window W.
2253 Set w->phys_cursor_width to width of phys cursor.
2254 */
2255
2256 void
2257 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2258 struct glyph *glyph, int *xp, int *yp, int *heightp)
2259 {
2260 struct frame *f = XFRAME (WINDOW_FRAME (w));
2261 int x, y, wd, h, h0, y0;
2262
2263 /* Compute the width of the rectangle to draw. If on a stretch
2264 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2265 rectangle as wide as the glyph, but use a canonical character
2266 width instead. */
2267 wd = glyph->pixel_width - 1;
2268 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2269 wd++; /* Why? */
2270 #endif
2271
2272 x = w->phys_cursor.x;
2273 if (x < 0)
2274 {
2275 wd += x;
2276 x = 0;
2277 }
2278
2279 if (glyph->type == STRETCH_GLYPH
2280 && !x_stretch_cursor_p)
2281 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2282 w->phys_cursor_width = wd;
2283
2284 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2285
2286 /* If y is below window bottom, ensure that we still see a cursor. */
2287 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2288
2289 h = max (h0, glyph->ascent + glyph->descent);
2290 h0 = min (h0, glyph->ascent + glyph->descent);
2291
2292 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2293 if (y < y0)
2294 {
2295 h = max (h - (y0 - y) + 1, h0);
2296 y = y0 - 1;
2297 }
2298 else
2299 {
2300 y0 = window_text_bottom_y (w) - h0;
2301 if (y > y0)
2302 {
2303 h += y - y0;
2304 y = y0;
2305 }
2306 }
2307
2308 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2309 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2310 *heightp = h;
2311 }
2312
2313 /*
2314 * Remember which glyph the mouse is over.
2315 */
2316
2317 void
2318 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2319 {
2320 Lisp_Object window;
2321 struct window *w;
2322 struct glyph_row *r, *gr, *end_row;
2323 enum window_part part;
2324 enum glyph_row_area area;
2325 int x, y, width, height;
2326
2327 /* Try to determine frame pixel position and size of the glyph under
2328 frame pixel coordinates X/Y on frame F. */
2329
2330 if (window_resize_pixelwise)
2331 {
2332 width = height = 1;
2333 goto virtual_glyph;
2334 }
2335 else if (!f->glyphs_initialized_p
2336 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2337 NILP (window)))
2338 {
2339 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2340 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2341 goto virtual_glyph;
2342 }
2343
2344 w = XWINDOW (window);
2345 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2346 height = WINDOW_FRAME_LINE_HEIGHT (w);
2347
2348 x = window_relative_x_coord (w, part, gx);
2349 y = gy - WINDOW_TOP_EDGE_Y (w);
2350
2351 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2352 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2353
2354 if (w->pseudo_window_p)
2355 {
2356 area = TEXT_AREA;
2357 part = ON_MODE_LINE; /* Don't adjust margin. */
2358 goto text_glyph;
2359 }
2360
2361 switch (part)
2362 {
2363 case ON_LEFT_MARGIN:
2364 area = LEFT_MARGIN_AREA;
2365 goto text_glyph;
2366
2367 case ON_RIGHT_MARGIN:
2368 area = RIGHT_MARGIN_AREA;
2369 goto text_glyph;
2370
2371 case ON_HEADER_LINE:
2372 case ON_MODE_LINE:
2373 gr = (part == ON_HEADER_LINE
2374 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2375 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2376 gy = gr->y;
2377 area = TEXT_AREA;
2378 goto text_glyph_row_found;
2379
2380 case ON_TEXT:
2381 area = TEXT_AREA;
2382
2383 text_glyph:
2384 gr = 0; gy = 0;
2385 for (; r <= end_row && r->enabled_p; ++r)
2386 if (r->y + r->height > y)
2387 {
2388 gr = r; gy = r->y;
2389 break;
2390 }
2391
2392 text_glyph_row_found:
2393 if (gr && gy <= y)
2394 {
2395 struct glyph *g = gr->glyphs[area];
2396 struct glyph *end = g + gr->used[area];
2397
2398 height = gr->height;
2399 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2400 if (gx + g->pixel_width > x)
2401 break;
2402
2403 if (g < end)
2404 {
2405 if (g->type == IMAGE_GLYPH)
2406 {
2407 /* Don't remember when mouse is over image, as
2408 image may have hot-spots. */
2409 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2410 return;
2411 }
2412 width = g->pixel_width;
2413 }
2414 else
2415 {
2416 /* Use nominal char spacing at end of line. */
2417 x -= gx;
2418 gx += (x / width) * width;
2419 }
2420
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2422 gx += window_box_left_offset (w, area);
2423 }
2424 else
2425 {
2426 /* Use nominal line height at end of window. */
2427 gx = (x / width) * width;
2428 y -= gy;
2429 gy += (y / height) * height;
2430 }
2431 break;
2432
2433 case ON_LEFT_FRINGE:
2434 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2435 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2436 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2437 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2438 goto row_glyph;
2439
2440 case ON_RIGHT_FRINGE:
2441 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2442 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2443 : window_box_right_offset (w, TEXT_AREA));
2444 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2445 goto row_glyph;
2446
2447 case ON_SCROLL_BAR:
2448 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2449 ? 0
2450 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2451 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2452 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2453 : 0)));
2454 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2455
2456 row_glyph:
2457 gr = 0, gy = 0;
2458 for (; r <= end_row && r->enabled_p; ++r)
2459 if (r->y + r->height > y)
2460 {
2461 gr = r; gy = r->y;
2462 break;
2463 }
2464
2465 if (gr && gy <= y)
2466 height = gr->height;
2467 else
2468 {
2469 /* Use nominal line height at end of window. */
2470 y -= gy;
2471 gy += (y / height) * height;
2472 }
2473 break;
2474
2475 default:
2476 ;
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2481
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2488
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2491
2492 goto store_rect;
2493 }
2494
2495 gx += WINDOW_LEFT_EDGE_X (w);
2496 gy += WINDOW_TOP_EDGE_Y (w);
2497
2498 store_rect:
2499 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2500
2501 /* Visible feedback for debugging. */
2502 #if 0
2503 #if HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2508 #endif
2509 }
2510
2511
2512 #endif /* HAVE_WINDOW_SYSTEM */
2513
2514 static void
2515 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 {
2517 eassert (w);
2518 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2519 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2520 w->window_end_vpos
2521 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2522 }
2523
2524 /***********************************************************************
2525 Lisp form evaluation
2526 ***********************************************************************/
2527
2528 /* Error handler for safe_eval and safe_call. */
2529
2530 static Lisp_Object
2531 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 {
2533 add_to_log ("Error during redisplay: %S signaled %S",
2534 Flist (nargs, args), arg);
2535 return Qnil;
2536 }
2537
2538 /* Call function FUNC with the rest of NARGS - 1 arguments
2539 following. Return the result, or nil if something went
2540 wrong. Prevent redisplay during the evaluation. */
2541
2542 Lisp_Object
2543 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2544 {
2545 Lisp_Object val;
2546
2547 if (inhibit_eval_during_redisplay)
2548 val = Qnil;
2549 else
2550 {
2551 va_list ap;
2552 ptrdiff_t i;
2553 ptrdiff_t count = SPECPDL_INDEX ();
2554 struct gcpro gcpro1;
2555 Lisp_Object *args = alloca (nargs * word_size);
2556
2557 args[0] = func;
2558 va_start (ap, func);
2559 for (i = 1; i < nargs; i++)
2560 args[i] = va_arg (ap, Lisp_Object);
2561 va_end (ap);
2562
2563 GCPRO1 (args[0]);
2564 gcpro1.nvars = nargs;
2565 specbind (Qinhibit_redisplay, Qt);
2566 /* Use Qt to ensure debugger does not run,
2567 so there is no possibility of wanting to redisplay. */
2568 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2569 safe_eval_handler);
2570 UNGCPRO;
2571 val = unbind_to (count, val);
2572 }
2573
2574 return val;
2575 }
2576
2577
2578 /* Call function FN with one argument ARG.
2579 Return the result, or nil if something went wrong. */
2580
2581 Lisp_Object
2582 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2583 {
2584 return safe_call (2, fn, arg);
2585 }
2586
2587 static Lisp_Object Qeval;
2588
2589 Lisp_Object
2590 safe_eval (Lisp_Object sexpr)
2591 {
2592 return safe_call1 (Qeval, sexpr);
2593 }
2594
2595 /* Call function FN with two arguments ARG1 and ARG2.
2596 Return the result, or nil if something went wrong. */
2597
2598 Lisp_Object
2599 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2600 {
2601 return safe_call (3, fn, arg1, arg2);
2602 }
2603
2604
2605 \f
2606 /***********************************************************************
2607 Debugging
2608 ***********************************************************************/
2609
2610 #if 0
2611
2612 /* Define CHECK_IT to perform sanity checks on iterators.
2613 This is for debugging. It is too slow to do unconditionally. */
2614
2615 static void
2616 check_it (struct it *it)
2617 {
2618 if (it->method == GET_FROM_STRING)
2619 {
2620 eassert (STRINGP (it->string));
2621 eassert (IT_STRING_CHARPOS (*it) >= 0);
2622 }
2623 else
2624 {
2625 eassert (IT_STRING_CHARPOS (*it) < 0);
2626 if (it->method == GET_FROM_BUFFER)
2627 {
2628 /* Check that character and byte positions agree. */
2629 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2630 }
2631 }
2632
2633 if (it->dpvec)
2634 eassert (it->current.dpvec_index >= 0);
2635 else
2636 eassert (it->current.dpvec_index < 0);
2637 }
2638
2639 #define CHECK_IT(IT) check_it ((IT))
2640
2641 #else /* not 0 */
2642
2643 #define CHECK_IT(IT) (void) 0
2644
2645 #endif /* not 0 */
2646
2647
2648 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2649
2650 /* Check that the window end of window W is what we expect it
2651 to be---the last row in the current matrix displaying text. */
2652
2653 static void
2654 check_window_end (struct window *w)
2655 {
2656 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2657 {
2658 struct glyph_row *row;
2659 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2660 !row->enabled_p
2661 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2662 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2663 }
2664 }
2665
2666 #define CHECK_WINDOW_END(W) check_window_end ((W))
2667
2668 #else
2669
2670 #define CHECK_WINDOW_END(W) (void) 0
2671
2672 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2673
2674 /***********************************************************************
2675 Iterator initialization
2676 ***********************************************************************/
2677
2678 /* Initialize IT for displaying current_buffer in window W, starting
2679 at character position CHARPOS. CHARPOS < 0 means that no buffer
2680 position is specified which is useful when the iterator is assigned
2681 a position later. BYTEPOS is the byte position corresponding to
2682 CHARPOS.
2683
2684 If ROW is not null, calls to produce_glyphs with IT as parameter
2685 will produce glyphs in that row.
2686
2687 BASE_FACE_ID is the id of a base face to use. It must be one of
2688 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2689 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2690 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2691
2692 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2694 will be initialized to use the corresponding mode line glyph row of
2695 the desired matrix of W. */
2696
2697 void
2698 init_iterator (struct it *it, struct window *w,
2699 ptrdiff_t charpos, ptrdiff_t bytepos,
2700 struct glyph_row *row, enum face_id base_face_id)
2701 {
2702 enum face_id remapped_base_face_id = base_face_id;
2703
2704 /* Some precondition checks. */
2705 eassert (w != NULL && it != NULL);
2706 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2707 && charpos <= ZV));
2708
2709 /* If face attributes have been changed since the last redisplay,
2710 free realized faces now because they depend on face definitions
2711 that might have changed. Don't free faces while there might be
2712 desired matrices pending which reference these faces. */
2713 if (face_change_count && !inhibit_free_realized_faces)
2714 {
2715 face_change_count = 0;
2716 free_all_realized_faces (Qnil);
2717 }
2718
2719 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2720 if (! NILP (Vface_remapping_alist))
2721 remapped_base_face_id
2722 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2723
2724 /* Use one of the mode line rows of W's desired matrix if
2725 appropriate. */
2726 if (row == NULL)
2727 {
2728 if (base_face_id == MODE_LINE_FACE_ID
2729 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2730 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2731 else if (base_face_id == HEADER_LINE_FACE_ID)
2732 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2733 }
2734
2735 /* Clear IT. */
2736 memset (it, 0, sizeof *it);
2737 it->current.overlay_string_index = -1;
2738 it->current.dpvec_index = -1;
2739 it->base_face_id = remapped_base_face_id;
2740 it->string = Qnil;
2741 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2742 it->paragraph_embedding = L2R;
2743 it->bidi_it.string.lstring = Qnil;
2744 it->bidi_it.string.s = NULL;
2745 it->bidi_it.string.bufpos = 0;
2746 it->bidi_it.w = w;
2747
2748 /* The window in which we iterate over current_buffer: */
2749 XSETWINDOW (it->window, w);
2750 it->w = w;
2751 it->f = XFRAME (w->frame);
2752
2753 it->cmp_it.id = -1;
2754
2755 /* Extra space between lines (on window systems only). */
2756 if (base_face_id == DEFAULT_FACE_ID
2757 && FRAME_WINDOW_P (it->f))
2758 {
2759 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2760 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2761 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2762 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2763 * FRAME_LINE_HEIGHT (it->f));
2764 else if (it->f->extra_line_spacing > 0)
2765 it->extra_line_spacing = it->f->extra_line_spacing;
2766 it->max_extra_line_spacing = 0;
2767 }
2768
2769 /* If realized faces have been removed, e.g. because of face
2770 attribute changes of named faces, recompute them. When running
2771 in batch mode, the face cache of the initial frame is null. If
2772 we happen to get called, make a dummy face cache. */
2773 if (FRAME_FACE_CACHE (it->f) == NULL)
2774 init_frame_faces (it->f);
2775 if (FRAME_FACE_CACHE (it->f)->used == 0)
2776 recompute_basic_faces (it->f);
2777
2778 /* Current value of the `slice', `space-width', and 'height' properties. */
2779 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2780 it->space_width = Qnil;
2781 it->font_height = Qnil;
2782 it->override_ascent = -1;
2783
2784 /* Are control characters displayed as `^C'? */
2785 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2786
2787 /* -1 means everything between a CR and the following line end
2788 is invisible. >0 means lines indented more than this value are
2789 invisible. */
2790 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2791 ? (clip_to_bounds
2792 (-1, XINT (BVAR (current_buffer, selective_display)),
2793 PTRDIFF_MAX))
2794 : (!NILP (BVAR (current_buffer, selective_display))
2795 ? -1 : 0));
2796 it->selective_display_ellipsis_p
2797 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2798
2799 /* Display table to use. */
2800 it->dp = window_display_table (w);
2801
2802 /* Are multibyte characters enabled in current_buffer? */
2803 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2804
2805 /* Get the position at which the redisplay_end_trigger hook should
2806 be run, if it is to be run at all. */
2807 if (MARKERP (w->redisplay_end_trigger)
2808 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2809 it->redisplay_end_trigger_charpos
2810 = marker_position (w->redisplay_end_trigger);
2811 else if (INTEGERP (w->redisplay_end_trigger))
2812 it->redisplay_end_trigger_charpos
2813 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2814 PTRDIFF_MAX);
2815
2816 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2817
2818 /* Are lines in the display truncated? */
2819 if (base_face_id != DEFAULT_FACE_ID
2820 || it->w->hscroll
2821 || (! WINDOW_FULL_WIDTH_P (it->w)
2822 && ((!NILP (Vtruncate_partial_width_windows)
2823 && !INTEGERP (Vtruncate_partial_width_windows))
2824 || (INTEGERP (Vtruncate_partial_width_windows)
2825 /* PXW: Shall we do something about this? */
2826 && (WINDOW_TOTAL_COLS (it->w)
2827 < XINT (Vtruncate_partial_width_windows))))))
2828 it->line_wrap = TRUNCATE;
2829 else if (NILP (BVAR (current_buffer, truncate_lines)))
2830 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2831 ? WINDOW_WRAP : WORD_WRAP;
2832 else
2833 it->line_wrap = TRUNCATE;
2834
2835 /* Get dimensions of truncation and continuation glyphs. These are
2836 displayed as fringe bitmaps under X, but we need them for such
2837 frames when the fringes are turned off. But leave the dimensions
2838 zero for tooltip frames, as these glyphs look ugly there and also
2839 sabotage calculations of tooltip dimensions in x-show-tip. */
2840 #ifdef HAVE_WINDOW_SYSTEM
2841 if (!(FRAME_WINDOW_P (it->f)
2842 && FRAMEP (tip_frame)
2843 && it->f == XFRAME (tip_frame)))
2844 #endif
2845 {
2846 if (it->line_wrap == TRUNCATE)
2847 {
2848 /* We will need the truncation glyph. */
2849 eassert (it->glyph_row == NULL);
2850 produce_special_glyphs (it, IT_TRUNCATION);
2851 it->truncation_pixel_width = it->pixel_width;
2852 }
2853 else
2854 {
2855 /* We will need the continuation glyph. */
2856 eassert (it->glyph_row == NULL);
2857 produce_special_glyphs (it, IT_CONTINUATION);
2858 it->continuation_pixel_width = it->pixel_width;
2859 }
2860 }
2861
2862 /* Reset these values to zero because the produce_special_glyphs
2863 above has changed them. */
2864 it->pixel_width = it->ascent = it->descent = 0;
2865 it->phys_ascent = it->phys_descent = 0;
2866
2867 /* Set this after getting the dimensions of truncation and
2868 continuation glyphs, so that we don't produce glyphs when calling
2869 produce_special_glyphs, above. */
2870 it->glyph_row = row;
2871 it->area = TEXT_AREA;
2872
2873 /* Forget any previous info about this row being reversed. */
2874 if (it->glyph_row)
2875 it->glyph_row->reversed_p = 0;
2876
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2882 {
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2886 }
2887 else
2888 {
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2893
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no fringes. Since we
2897 don't know at this point whether there will be any R2L lines in
2898 the window, we reserve space for truncation/continuation glyphs
2899 even if only one of the fringes is absent. */
2900 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2901 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2902 {
2903 if (it->line_wrap == TRUNCATE)
2904 it->last_visible_x -= it->truncation_pixel_width;
2905 else
2906 it->last_visible_x -= it->continuation_pixel_width;
2907 }
2908
2909 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2910 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 }
2912
2913 /* Leave room for a border glyph. */
2914 if (!FRAME_WINDOW_P (it->f)
2915 && !WINDOW_RIGHTMOST_P (it->w))
2916 it->last_visible_x -= 1;
2917
2918 it->last_visible_y = window_text_bottom_y (w);
2919
2920 /* For mode lines and alike, arrange for the first glyph having a
2921 left box line if the face specifies a box. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2923 {
2924 struct face *face;
2925
2926 it->face_id = remapped_base_face_id;
2927
2928 /* If we have a boxed mode line, make the first character appear
2929 with a left box line. */
2930 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2931 if (face && face->box != FACE_NO_BOX)
2932 it->start_of_box_run_p = true;
2933 }
2934
2935 /* If a buffer position was specified, set the iterator there,
2936 getting overlays and face properties from that position. */
2937 if (charpos >= BUF_BEG (current_buffer))
2938 {
2939 it->end_charpos = ZV;
2940 eassert (charpos == BYTE_TO_CHAR (bytepos));
2941 IT_CHARPOS (*it) = charpos;
2942 IT_BYTEPOS (*it) = bytepos;
2943
2944 /* We will rely on `reseat' to set this up properly, via
2945 handle_face_prop. */
2946 it->face_id = it->base_face_id;
2947
2948 it->start = it->current;
2949 /* Do we need to reorder bidirectional text? Not if this is a
2950 unibyte buffer: by definition, none of the single-byte
2951 characters are strong R2L, so no reordering is needed. And
2952 bidi.c doesn't support unibyte buffers anyway. Also, don't
2953 reorder while we are loading loadup.el, since the tables of
2954 character properties needed for reordering are not yet
2955 available. */
2956 it->bidi_p =
2957 NILP (Vpurify_flag)
2958 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2959 && it->multibyte_p;
2960
2961 /* If we are to reorder bidirectional text, init the bidi
2962 iterator. */
2963 if (it->bidi_p)
2964 {
2965 /* Note the paragraph direction that this buffer wants to
2966 use. */
2967 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2968 Qleft_to_right))
2969 it->paragraph_embedding = L2R;
2970 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2971 Qright_to_left))
2972 it->paragraph_embedding = R2L;
2973 else
2974 it->paragraph_embedding = NEUTRAL_DIR;
2975 bidi_unshelve_cache (NULL, 0);
2976 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2977 &it->bidi_it);
2978 }
2979
2980 /* Compute faces etc. */
2981 reseat (it, it->current.pos, 1);
2982 }
2983
2984 CHECK_IT (it);
2985 }
2986
2987
2988 /* Initialize IT for the display of window W with window start POS. */
2989
2990 void
2991 start_display (struct it *it, struct window *w, struct text_pos pos)
2992 {
2993 struct glyph_row *row;
2994 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2995
2996 row = w->desired_matrix->rows + first_vpos;
2997 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2998 it->first_vpos = first_vpos;
2999
3000 /* Don't reseat to previous visible line start if current start
3001 position is in a string or image. */
3002 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3003 {
3004 int start_at_line_beg_p;
3005 int first_y = it->current_y;
3006
3007 /* If window start is not at a line start, skip forward to POS to
3008 get the correct continuation lines width. */
3009 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3010 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3011 if (!start_at_line_beg_p)
3012 {
3013 int new_x;
3014
3015 reseat_at_previous_visible_line_start (it);
3016 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3017
3018 new_x = it->current_x + it->pixel_width;
3019
3020 /* If lines are continued, this line may end in the middle
3021 of a multi-glyph character (e.g. a control character
3022 displayed as \003, or in the middle of an overlay
3023 string). In this case move_it_to above will not have
3024 taken us to the start of the continuation line but to the
3025 end of the continued line. */
3026 if (it->current_x > 0
3027 && it->line_wrap != TRUNCATE /* Lines are continued. */
3028 && (/* And glyph doesn't fit on the line. */
3029 new_x > it->last_visible_x
3030 /* Or it fits exactly and we're on a window
3031 system frame. */
3032 || (new_x == it->last_visible_x
3033 && FRAME_WINDOW_P (it->f)
3034 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3035 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3036 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3037 {
3038 if ((it->current.dpvec_index >= 0
3039 || it->current.overlay_string_index >= 0)
3040 /* If we are on a newline from a display vector or
3041 overlay string, then we are already at the end of
3042 a screen line; no need to go to the next line in
3043 that case, as this line is not really continued.
3044 (If we do go to the next line, C-e will not DTRT.) */
3045 && it->c != '\n')
3046 {
3047 set_iterator_to_next (it, 1);
3048 move_it_in_display_line_to (it, -1, -1, 0);
3049 }
3050
3051 it->continuation_lines_width += it->current_x;
3052 }
3053 /* If the character at POS is displayed via a display
3054 vector, move_it_to above stops at the final glyph of
3055 IT->dpvec. To make the caller redisplay that character
3056 again (a.k.a. start at POS), we need to reset the
3057 dpvec_index to the beginning of IT->dpvec. */
3058 else if (it->current.dpvec_index >= 0)
3059 it->current.dpvec_index = 0;
3060
3061 /* We're starting a new display line, not affected by the
3062 height of the continued line, so clear the appropriate
3063 fields in the iterator structure. */
3064 it->max_ascent = it->max_descent = 0;
3065 it->max_phys_ascent = it->max_phys_descent = 0;
3066
3067 it->current_y = first_y;
3068 it->vpos = 0;
3069 it->current_x = it->hpos = 0;
3070 }
3071 }
3072 }
3073
3074
3075 /* Return 1 if POS is a position in ellipses displayed for invisible
3076 text. W is the window we display, for text property lookup. */
3077
3078 static int
3079 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3080 {
3081 Lisp_Object prop, window;
3082 int ellipses_p = 0;
3083 ptrdiff_t charpos = CHARPOS (pos->pos);
3084
3085 /* If POS specifies a position in a display vector, this might
3086 be for an ellipsis displayed for invisible text. We won't
3087 get the iterator set up for delivering that ellipsis unless
3088 we make sure that it gets aware of the invisible text. */
3089 if (pos->dpvec_index >= 0
3090 && pos->overlay_string_index < 0
3091 && CHARPOS (pos->string_pos) < 0
3092 && charpos > BEGV
3093 && (XSETWINDOW (window, w),
3094 prop = Fget_char_property (make_number (charpos),
3095 Qinvisible, window),
3096 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3097 {
3098 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3099 window);
3100 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3101 }
3102
3103 return ellipses_p;
3104 }
3105
3106
3107 /* Initialize IT for stepping through current_buffer in window W,
3108 starting at position POS that includes overlay string and display
3109 vector/ control character translation position information. Value
3110 is zero if there are overlay strings with newlines at POS. */
3111
3112 static int
3113 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3114 {
3115 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3116 int i, overlay_strings_with_newlines = 0;
3117
3118 /* If POS specifies a position in a display vector, this might
3119 be for an ellipsis displayed for invisible text. We won't
3120 get the iterator set up for delivering that ellipsis unless
3121 we make sure that it gets aware of the invisible text. */
3122 if (in_ellipses_for_invisible_text_p (pos, w))
3123 {
3124 --charpos;
3125 bytepos = 0;
3126 }
3127
3128 /* Keep in mind: the call to reseat in init_iterator skips invisible
3129 text, so we might end up at a position different from POS. This
3130 is only a problem when POS is a row start after a newline and an
3131 overlay starts there with an after-string, and the overlay has an
3132 invisible property. Since we don't skip invisible text in
3133 display_line and elsewhere immediately after consuming the
3134 newline before the row start, such a POS will not be in a string,
3135 but the call to init_iterator below will move us to the
3136 after-string. */
3137 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3138
3139 /* This only scans the current chunk -- it should scan all chunks.
3140 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3141 to 16 in 22.1 to make this a lesser problem. */
3142 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3143 {
3144 const char *s = SSDATA (it->overlay_strings[i]);
3145 const char *e = s + SBYTES (it->overlay_strings[i]);
3146
3147 while (s < e && *s != '\n')
3148 ++s;
3149
3150 if (s < e)
3151 {
3152 overlay_strings_with_newlines = 1;
3153 break;
3154 }
3155 }
3156
3157 /* If position is within an overlay string, set up IT to the right
3158 overlay string. */
3159 if (pos->overlay_string_index >= 0)
3160 {
3161 int relative_index;
3162
3163 /* If the first overlay string happens to have a `display'
3164 property for an image, the iterator will be set up for that
3165 image, and we have to undo that setup first before we can
3166 correct the overlay string index. */
3167 if (it->method == GET_FROM_IMAGE)
3168 pop_it (it);
3169
3170 /* We already have the first chunk of overlay strings in
3171 IT->overlay_strings. Load more until the one for
3172 pos->overlay_string_index is in IT->overlay_strings. */
3173 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3174 {
3175 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3176 it->current.overlay_string_index = 0;
3177 while (n--)
3178 {
3179 load_overlay_strings (it, 0);
3180 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3181 }
3182 }
3183
3184 it->current.overlay_string_index = pos->overlay_string_index;
3185 relative_index = (it->current.overlay_string_index
3186 % OVERLAY_STRING_CHUNK_SIZE);
3187 it->string = it->overlay_strings[relative_index];
3188 eassert (STRINGP (it->string));
3189 it->current.string_pos = pos->string_pos;
3190 it->method = GET_FROM_STRING;
3191 it->end_charpos = SCHARS (it->string);
3192 /* Set up the bidi iterator for this overlay string. */
3193 if (it->bidi_p)
3194 {
3195 it->bidi_it.string.lstring = it->string;
3196 it->bidi_it.string.s = NULL;
3197 it->bidi_it.string.schars = SCHARS (it->string);
3198 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3199 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3200 it->bidi_it.string.unibyte = !it->multibyte_p;
3201 it->bidi_it.w = it->w;
3202 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3203 FRAME_WINDOW_P (it->f), &it->bidi_it);
3204
3205 /* Synchronize the state of the bidi iterator with
3206 pos->string_pos. For any string position other than
3207 zero, this will be done automagically when we resume
3208 iteration over the string and get_visually_first_element
3209 is called. But if string_pos is zero, and the string is
3210 to be reordered for display, we need to resync manually,
3211 since it could be that the iteration state recorded in
3212 pos ended at string_pos of 0 moving backwards in string. */
3213 if (CHARPOS (pos->string_pos) == 0)
3214 {
3215 get_visually_first_element (it);
3216 if (IT_STRING_CHARPOS (*it) != 0)
3217 do {
3218 /* Paranoia. */
3219 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3220 bidi_move_to_visually_next (&it->bidi_it);
3221 } while (it->bidi_it.charpos != 0);
3222 }
3223 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3224 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3225 }
3226 }
3227
3228 if (CHARPOS (pos->string_pos) >= 0)
3229 {
3230 /* Recorded position is not in an overlay string, but in another
3231 string. This can only be a string from a `display' property.
3232 IT should already be filled with that string. */
3233 it->current.string_pos = pos->string_pos;
3234 eassert (STRINGP (it->string));
3235 if (it->bidi_p)
3236 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3237 FRAME_WINDOW_P (it->f), &it->bidi_it);
3238 }
3239
3240 /* Restore position in display vector translations, control
3241 character translations or ellipses. */
3242 if (pos->dpvec_index >= 0)
3243 {
3244 if (it->dpvec == NULL)
3245 get_next_display_element (it);
3246 eassert (it->dpvec && it->current.dpvec_index == 0);
3247 it->current.dpvec_index = pos->dpvec_index;
3248 }
3249
3250 CHECK_IT (it);
3251 return !overlay_strings_with_newlines;
3252 }
3253
3254
3255 /* Initialize IT for stepping through current_buffer in window W
3256 starting at ROW->start. */
3257
3258 static void
3259 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3260 {
3261 init_from_display_pos (it, w, &row->start);
3262 it->start = row->start;
3263 it->continuation_lines_width = row->continuation_lines_width;
3264 CHECK_IT (it);
3265 }
3266
3267
3268 /* Initialize IT for stepping through current_buffer in window W
3269 starting in the line following ROW, i.e. starting at ROW->end.
3270 Value is zero if there are overlay strings with newlines at ROW's
3271 end position. */
3272
3273 static int
3274 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3275 {
3276 int success = 0;
3277
3278 if (init_from_display_pos (it, w, &row->end))
3279 {
3280 if (row->continued_p)
3281 it->continuation_lines_width
3282 = row->continuation_lines_width + row->pixel_width;
3283 CHECK_IT (it);
3284 success = 1;
3285 }
3286
3287 return success;
3288 }
3289
3290
3291
3292 \f
3293 /***********************************************************************
3294 Text properties
3295 ***********************************************************************/
3296
3297 /* Called when IT reaches IT->stop_charpos. Handle text property and
3298 overlay changes. Set IT->stop_charpos to the next position where
3299 to stop. */
3300
3301 static void
3302 handle_stop (struct it *it)
3303 {
3304 enum prop_handled handled;
3305 int handle_overlay_change_p;
3306 struct props *p;
3307
3308 it->dpvec = NULL;
3309 it->current.dpvec_index = -1;
3310 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3311 it->ignore_overlay_strings_at_pos_p = 0;
3312 it->ellipsis_p = 0;
3313
3314 /* Use face of preceding text for ellipsis (if invisible) */
3315 if (it->selective_display_ellipsis_p)
3316 it->saved_face_id = it->face_id;
3317
3318 do
3319 {
3320 handled = HANDLED_NORMALLY;
3321
3322 /* Call text property handlers. */
3323 for (p = it_props; p->handler; ++p)
3324 {
3325 handled = p->handler (it);
3326
3327 if (handled == HANDLED_RECOMPUTE_PROPS)
3328 break;
3329 else if (handled == HANDLED_RETURN)
3330 {
3331 /* We still want to show before and after strings from
3332 overlays even if the actual buffer text is replaced. */
3333 if (!handle_overlay_change_p
3334 || it->sp > 1
3335 /* Don't call get_overlay_strings_1 if we already
3336 have overlay strings loaded, because doing so
3337 will load them again and push the iterator state
3338 onto the stack one more time, which is not
3339 expected by the rest of the code that processes
3340 overlay strings. */
3341 || (it->current.overlay_string_index < 0
3342 ? !get_overlay_strings_1 (it, 0, 0)
3343 : 0))
3344 {
3345 if (it->ellipsis_p)
3346 setup_for_ellipsis (it, 0);
3347 /* When handling a display spec, we might load an
3348 empty string. In that case, discard it here. We
3349 used to discard it in handle_single_display_spec,
3350 but that causes get_overlay_strings_1, above, to
3351 ignore overlay strings that we must check. */
3352 if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 return;
3355 }
3356 else if (STRINGP (it->string) && !SCHARS (it->string))
3357 pop_it (it);
3358 else
3359 {
3360 it->ignore_overlay_strings_at_pos_p = true;
3361 it->string_from_display_prop_p = 0;
3362 it->from_disp_prop_p = 0;
3363 handle_overlay_change_p = 0;
3364 }
3365 handled = HANDLED_RECOMPUTE_PROPS;
3366 break;
3367 }
3368 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3369 handle_overlay_change_p = 0;
3370 }
3371
3372 if (handled != HANDLED_RECOMPUTE_PROPS)
3373 {
3374 /* Don't check for overlay strings below when set to deliver
3375 characters from a display vector. */
3376 if (it->method == GET_FROM_DISPLAY_VECTOR)
3377 handle_overlay_change_p = 0;
3378
3379 /* Handle overlay changes.
3380 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3381 if it finds overlays. */
3382 if (handle_overlay_change_p)
3383 handled = handle_overlay_change (it);
3384 }
3385
3386 if (it->ellipsis_p)
3387 {
3388 setup_for_ellipsis (it, 0);
3389 break;
3390 }
3391 }
3392 while (handled == HANDLED_RECOMPUTE_PROPS);
3393
3394 /* Determine where to stop next. */
3395 if (handled == HANDLED_NORMALLY)
3396 compute_stop_pos (it);
3397 }
3398
3399
3400 /* Compute IT->stop_charpos from text property and overlay change
3401 information for IT's current position. */
3402
3403 static void
3404 compute_stop_pos (struct it *it)
3405 {
3406 register INTERVAL iv, next_iv;
3407 Lisp_Object object, limit, position;
3408 ptrdiff_t charpos, bytepos;
3409
3410 if (STRINGP (it->string))
3411 {
3412 /* Strings are usually short, so don't limit the search for
3413 properties. */
3414 it->stop_charpos = it->end_charpos;
3415 object = it->string;
3416 limit = Qnil;
3417 charpos = IT_STRING_CHARPOS (*it);
3418 bytepos = IT_STRING_BYTEPOS (*it);
3419 }
3420 else
3421 {
3422 ptrdiff_t pos;
3423
3424 /* If end_charpos is out of range for some reason, such as a
3425 misbehaving display function, rationalize it (Bug#5984). */
3426 if (it->end_charpos > ZV)
3427 it->end_charpos = ZV;
3428 it->stop_charpos = it->end_charpos;
3429
3430 /* If next overlay change is in front of the current stop pos
3431 (which is IT->end_charpos), stop there. Note: value of
3432 next_overlay_change is point-max if no overlay change
3433 follows. */
3434 charpos = IT_CHARPOS (*it);
3435 bytepos = IT_BYTEPOS (*it);
3436 pos = next_overlay_change (charpos);
3437 if (pos < it->stop_charpos)
3438 it->stop_charpos = pos;
3439
3440 /* Set up variables for computing the stop position from text
3441 property changes. */
3442 XSETBUFFER (object, current_buffer);
3443 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3444 }
3445
3446 /* Get the interval containing IT's position. Value is a null
3447 interval if there isn't such an interval. */
3448 position = make_number (charpos);
3449 iv = validate_interval_range (object, &position, &position, 0);
3450 if (iv)
3451 {
3452 Lisp_Object values_here[LAST_PROP_IDX];
3453 struct props *p;
3454
3455 /* Get properties here. */
3456 for (p = it_props; p->handler; ++p)
3457 values_here[p->idx] = textget (iv->plist, *p->name);
3458
3459 /* Look for an interval following iv that has different
3460 properties. */
3461 for (next_iv = next_interval (iv);
3462 (next_iv
3463 && (NILP (limit)
3464 || XFASTINT (limit) > next_iv->position));
3465 next_iv = next_interval (next_iv))
3466 {
3467 for (p = it_props; p->handler; ++p)
3468 {
3469 Lisp_Object new_value;
3470
3471 new_value = textget (next_iv->plist, *p->name);
3472 if (!EQ (values_here[p->idx], new_value))
3473 break;
3474 }
3475
3476 if (p->handler)
3477 break;
3478 }
3479
3480 if (next_iv)
3481 {
3482 if (INTEGERP (limit)
3483 && next_iv->position >= XFASTINT (limit))
3484 /* No text property change up to limit. */
3485 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3486 else
3487 /* Text properties change in next_iv. */
3488 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3489 }
3490 }
3491
3492 if (it->cmp_it.id < 0)
3493 {
3494 ptrdiff_t stoppos = it->end_charpos;
3495
3496 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3497 stoppos = -1;
3498 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3499 stoppos, it->string);
3500 }
3501
3502 eassert (STRINGP (it->string)
3503 || (it->stop_charpos >= BEGV
3504 && it->stop_charpos >= IT_CHARPOS (*it)));
3505 }
3506
3507
3508 /* Return the position of the next overlay change after POS in
3509 current_buffer. Value is point-max if no overlay change
3510 follows. This is like `next-overlay-change' but doesn't use
3511 xmalloc. */
3512
3513 static ptrdiff_t
3514 next_overlay_change (ptrdiff_t pos)
3515 {
3516 ptrdiff_t i, noverlays;
3517 ptrdiff_t endpos;
3518 Lisp_Object *overlays;
3519
3520 /* Get all overlays at the given position. */
3521 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3522
3523 /* If any of these overlays ends before endpos,
3524 use its ending point instead. */
3525 for (i = 0; i < noverlays; ++i)
3526 {
3527 Lisp_Object oend;
3528 ptrdiff_t oendpos;
3529
3530 oend = OVERLAY_END (overlays[i]);
3531 oendpos = OVERLAY_POSITION (oend);
3532 endpos = min (endpos, oendpos);
3533 }
3534
3535 return endpos;
3536 }
3537
3538 /* How many characters forward to search for a display property or
3539 display string. Searching too far forward makes the bidi display
3540 sluggish, especially in small windows. */
3541 #define MAX_DISP_SCAN 250
3542
3543 /* Return the character position of a display string at or after
3544 position specified by POSITION. If no display string exists at or
3545 after POSITION, return ZV. A display string is either an overlay
3546 with `display' property whose value is a string, or a `display'
3547 text property whose value is a string. STRING is data about the
3548 string to iterate; if STRING->lstring is nil, we are iterating a
3549 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3550 on a GUI frame. DISP_PROP is set to zero if we searched
3551 MAX_DISP_SCAN characters forward without finding any display
3552 strings, non-zero otherwise. It is set to 2 if the display string
3553 uses any kind of `(space ...)' spec that will produce a stretch of
3554 white space in the text area. */
3555 ptrdiff_t
3556 compute_display_string_pos (struct text_pos *position,
3557 struct bidi_string_data *string,
3558 struct window *w,
3559 int frame_window_p, int *disp_prop)
3560 {
3561 /* OBJECT = nil means current buffer. */
3562 Lisp_Object object, object1;
3563 Lisp_Object pos, spec, limpos;
3564 int string_p = (string && (STRINGP (string->lstring) || string->s));
3565 ptrdiff_t eob = string_p ? string->schars : ZV;
3566 ptrdiff_t begb = string_p ? 0 : BEGV;
3567 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3568 ptrdiff_t lim =
3569 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3570 struct text_pos tpos;
3571 int rv = 0;
3572
3573 if (string && STRINGP (string->lstring))
3574 object1 = object = string->lstring;
3575 else if (w && !string_p)
3576 {
3577 XSETWINDOW (object, w);
3578 object1 = Qnil;
3579 }
3580 else
3581 object1 = object = Qnil;
3582
3583 *disp_prop = 1;
3584
3585 if (charpos >= eob
3586 /* We don't support display properties whose values are strings
3587 that have display string properties. */
3588 || string->from_disp_str
3589 /* C strings cannot have display properties. */
3590 || (string->s && !STRINGP (object)))
3591 {
3592 *disp_prop = 0;
3593 return eob;
3594 }
3595
3596 /* If the character at CHARPOS is where the display string begins,
3597 return CHARPOS. */
3598 pos = make_number (charpos);
3599 if (STRINGP (object))
3600 bufpos = string->bufpos;
3601 else
3602 bufpos = charpos;
3603 tpos = *position;
3604 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3605 && (charpos <= begb
3606 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3607 object),
3608 spec))
3609 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3610 frame_window_p)))
3611 {
3612 if (rv == 2)
3613 *disp_prop = 2;
3614 return charpos;
3615 }
3616
3617 /* Look forward for the first character with a `display' property
3618 that will replace the underlying text when displayed. */
3619 limpos = make_number (lim);
3620 do {
3621 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3622 CHARPOS (tpos) = XFASTINT (pos);
3623 if (CHARPOS (tpos) >= lim)
3624 {
3625 *disp_prop = 0;
3626 break;
3627 }
3628 if (STRINGP (object))
3629 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3630 else
3631 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3632 spec = Fget_char_property (pos, Qdisplay, object);
3633 if (!STRINGP (object))
3634 bufpos = CHARPOS (tpos);
3635 } while (NILP (spec)
3636 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3637 bufpos, frame_window_p)));
3638 if (rv == 2)
3639 *disp_prop = 2;
3640
3641 return CHARPOS (tpos);
3642 }
3643
3644 /* Return the character position of the end of the display string that
3645 started at CHARPOS. If there's no display string at CHARPOS,
3646 return -1. A display string is either an overlay with `display'
3647 property whose value is a string or a `display' text property whose
3648 value is a string. */
3649 ptrdiff_t
3650 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3651 {
3652 /* OBJECT = nil means current buffer. */
3653 Lisp_Object object =
3654 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3655 Lisp_Object pos = make_number (charpos);
3656 ptrdiff_t eob =
3657 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3658
3659 if (charpos >= eob || (string->s && !STRINGP (object)))
3660 return eob;
3661
3662 /* It could happen that the display property or overlay was removed
3663 since we found it in compute_display_string_pos above. One way
3664 this can happen is if JIT font-lock was called (through
3665 handle_fontified_prop), and jit-lock-functions remove text
3666 properties or overlays from the portion of buffer that includes
3667 CHARPOS. Muse mode is known to do that, for example. In this
3668 case, we return -1 to the caller, to signal that no display
3669 string is actually present at CHARPOS. See bidi_fetch_char for
3670 how this is handled.
3671
3672 An alternative would be to never look for display properties past
3673 it->stop_charpos. But neither compute_display_string_pos nor
3674 bidi_fetch_char that calls it know or care where the next
3675 stop_charpos is. */
3676 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3677 return -1;
3678
3679 /* Look forward for the first character where the `display' property
3680 changes. */
3681 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3682
3683 return XFASTINT (pos);
3684 }
3685
3686
3687 \f
3688 /***********************************************************************
3689 Fontification
3690 ***********************************************************************/
3691
3692 /* Handle changes in the `fontified' property of the current buffer by
3693 calling hook functions from Qfontification_functions to fontify
3694 regions of text. */
3695
3696 static enum prop_handled
3697 handle_fontified_prop (struct it *it)
3698 {
3699 Lisp_Object prop, pos;
3700 enum prop_handled handled = HANDLED_NORMALLY;
3701
3702 if (!NILP (Vmemory_full))
3703 return handled;
3704
3705 /* Get the value of the `fontified' property at IT's current buffer
3706 position. (The `fontified' property doesn't have a special
3707 meaning in strings.) If the value is nil, call functions from
3708 Qfontification_functions. */
3709 if (!STRINGP (it->string)
3710 && it->s == NULL
3711 && !NILP (Vfontification_functions)
3712 && !NILP (Vrun_hooks)
3713 && (pos = make_number (IT_CHARPOS (*it)),
3714 prop = Fget_char_property (pos, Qfontified, Qnil),
3715 /* Ignore the special cased nil value always present at EOB since
3716 no amount of fontifying will be able to change it. */
3717 NILP (prop) && IT_CHARPOS (*it) < Z))
3718 {
3719 ptrdiff_t count = SPECPDL_INDEX ();
3720 Lisp_Object val;
3721 struct buffer *obuf = current_buffer;
3722 ptrdiff_t begv = BEGV, zv = ZV;
3723 bool old_clip_changed = current_buffer->clip_changed;
3724
3725 val = Vfontification_functions;
3726 specbind (Qfontification_functions, Qnil);
3727
3728 eassert (it->end_charpos == ZV);
3729
3730 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3731 safe_call1 (val, pos);
3732 else
3733 {
3734 Lisp_Object fns, fn;
3735 struct gcpro gcpro1, gcpro2;
3736
3737 fns = Qnil;
3738 GCPRO2 (val, fns);
3739
3740 for (; CONSP (val); val = XCDR (val))
3741 {
3742 fn = XCAR (val);
3743
3744 if (EQ (fn, Qt))
3745 {
3746 /* A value of t indicates this hook has a local
3747 binding; it means to run the global binding too.
3748 In a global value, t should not occur. If it
3749 does, we must ignore it to avoid an endless
3750 loop. */
3751 for (fns = Fdefault_value (Qfontification_functions);
3752 CONSP (fns);
3753 fns = XCDR (fns))
3754 {
3755 fn = XCAR (fns);
3756 if (!EQ (fn, Qt))
3757 safe_call1 (fn, pos);
3758 }
3759 }
3760 else
3761 safe_call1 (fn, pos);
3762 }
3763
3764 UNGCPRO;
3765 }
3766
3767 unbind_to (count, Qnil);
3768
3769 /* Fontification functions routinely call `save-restriction'.
3770 Normally, this tags clip_changed, which can confuse redisplay
3771 (see discussion in Bug#6671). Since we don't perform any
3772 special handling of fontification changes in the case where
3773 `save-restriction' isn't called, there's no point doing so in
3774 this case either. So, if the buffer's restrictions are
3775 actually left unchanged, reset clip_changed. */
3776 if (obuf == current_buffer)
3777 {
3778 if (begv == BEGV && zv == ZV)
3779 current_buffer->clip_changed = old_clip_changed;
3780 }
3781 /* There isn't much we can reasonably do to protect against
3782 misbehaving fontification, but here's a fig leaf. */
3783 else if (BUFFER_LIVE_P (obuf))
3784 set_buffer_internal_1 (obuf);
3785
3786 /* The fontification code may have added/removed text.
3787 It could do even a lot worse, but let's at least protect against
3788 the most obvious case where only the text past `pos' gets changed',
3789 as is/was done in grep.el where some escapes sequences are turned
3790 into face properties (bug#7876). */
3791 it->end_charpos = ZV;
3792
3793 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3794 something. This avoids an endless loop if they failed to
3795 fontify the text for which reason ever. */
3796 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3797 handled = HANDLED_RECOMPUTE_PROPS;
3798 }
3799
3800 return handled;
3801 }
3802
3803
3804 \f
3805 /***********************************************************************
3806 Faces
3807 ***********************************************************************/
3808
3809 /* Set up iterator IT from face properties at its current position.
3810 Called from handle_stop. */
3811
3812 static enum prop_handled
3813 handle_face_prop (struct it *it)
3814 {
3815 int new_face_id;
3816 ptrdiff_t next_stop;
3817
3818 if (!STRINGP (it->string))
3819 {
3820 new_face_id
3821 = face_at_buffer_position (it->w,
3822 IT_CHARPOS (*it),
3823 &next_stop,
3824 (IT_CHARPOS (*it)
3825 + TEXT_PROP_DISTANCE_LIMIT),
3826 0, it->base_face_id);
3827
3828 /* Is this a start of a run of characters with box face?
3829 Caveat: this can be called for a freshly initialized
3830 iterator; face_id is -1 in this case. We know that the new
3831 face will not change until limit, i.e. if the new face has a
3832 box, all characters up to limit will have one. But, as
3833 usual, we don't know whether limit is really the end. */
3834 if (new_face_id != it->face_id)
3835 {
3836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3837 /* If it->face_id is -1, old_face below will be NULL, see
3838 the definition of FACE_FROM_ID. This will happen if this
3839 is the initial call that gets the face. */
3840 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3841
3842 /* If the value of face_id of the iterator is -1, we have to
3843 look in front of IT's position and see whether there is a
3844 face there that's different from new_face_id. */
3845 if (!old_face && IT_CHARPOS (*it) > BEG)
3846 {
3847 int prev_face_id = face_before_it_pos (it);
3848
3849 old_face = FACE_FROM_ID (it->f, prev_face_id);
3850 }
3851
3852 /* If the new face has a box, but the old face does not,
3853 this is the start of a run of characters with box face,
3854 i.e. this character has a shadow on the left side. */
3855 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3856 && (old_face == NULL || !old_face->box));
3857 it->face_box_p = new_face->box != FACE_NO_BOX;
3858 }
3859 }
3860 else
3861 {
3862 int base_face_id;
3863 ptrdiff_t bufpos;
3864 int i;
3865 Lisp_Object from_overlay
3866 = (it->current.overlay_string_index >= 0
3867 ? it->string_overlays[it->current.overlay_string_index
3868 % OVERLAY_STRING_CHUNK_SIZE]
3869 : Qnil);
3870
3871 /* See if we got to this string directly or indirectly from
3872 an overlay property. That includes the before-string or
3873 after-string of an overlay, strings in display properties
3874 provided by an overlay, their text properties, etc.
3875
3876 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3877 if (! NILP (from_overlay))
3878 for (i = it->sp - 1; i >= 0; i--)
3879 {
3880 if (it->stack[i].current.overlay_string_index >= 0)
3881 from_overlay
3882 = it->string_overlays[it->stack[i].current.overlay_string_index
3883 % OVERLAY_STRING_CHUNK_SIZE];
3884 else if (! NILP (it->stack[i].from_overlay))
3885 from_overlay = it->stack[i].from_overlay;
3886
3887 if (!NILP (from_overlay))
3888 break;
3889 }
3890
3891 if (! NILP (from_overlay))
3892 {
3893 bufpos = IT_CHARPOS (*it);
3894 /* For a string from an overlay, the base face depends
3895 only on text properties and ignores overlays. */
3896 base_face_id
3897 = face_for_overlay_string (it->w,
3898 IT_CHARPOS (*it),
3899 &next_stop,
3900 (IT_CHARPOS (*it)
3901 + TEXT_PROP_DISTANCE_LIMIT),
3902 0,
3903 from_overlay);
3904 }
3905 else
3906 {
3907 bufpos = 0;
3908
3909 /* For strings from a `display' property, use the face at
3910 IT's current buffer position as the base face to merge
3911 with, so that overlay strings appear in the same face as
3912 surrounding text, unless they specify their own faces.
3913 For strings from wrap-prefix and line-prefix properties,
3914 use the default face, possibly remapped via
3915 Vface_remapping_alist. */
3916 base_face_id = it->string_from_prefix_prop_p
3917 ? (!NILP (Vface_remapping_alist)
3918 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3919 : DEFAULT_FACE_ID)
3920 : underlying_face_id (it);
3921 }
3922
3923 new_face_id = face_at_string_position (it->w,
3924 it->string,
3925 IT_STRING_CHARPOS (*it),
3926 bufpos,
3927 &next_stop,
3928 base_face_id, 0);
3929
3930 /* Is this a start of a run of characters with box? Caveat:
3931 this can be called for a freshly allocated iterator; face_id
3932 is -1 is this case. We know that the new face will not
3933 change until the next check pos, i.e. if the new face has a
3934 box, all characters up to that position will have a
3935 box. But, as usual, we don't know whether that position
3936 is really the end. */
3937 if (new_face_id != it->face_id)
3938 {
3939 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3940 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3941
3942 /* If new face has a box but old face hasn't, this is the
3943 start of a run of characters with box, i.e. it has a
3944 shadow on the left side. */
3945 it->start_of_box_run_p
3946 = new_face->box && (old_face == NULL || !old_face->box);
3947 it->face_box_p = new_face->box != FACE_NO_BOX;
3948 }
3949 }
3950
3951 it->face_id = new_face_id;
3952 return HANDLED_NORMALLY;
3953 }
3954
3955
3956 /* Return the ID of the face ``underlying'' IT's current position,
3957 which is in a string. If the iterator is associated with a
3958 buffer, return the face at IT's current buffer position.
3959 Otherwise, use the iterator's base_face_id. */
3960
3961 static int
3962 underlying_face_id (struct it *it)
3963 {
3964 int face_id = it->base_face_id, i;
3965
3966 eassert (STRINGP (it->string));
3967
3968 for (i = it->sp - 1; i >= 0; --i)
3969 if (NILP (it->stack[i].string))
3970 face_id = it->stack[i].face_id;
3971
3972 return face_id;
3973 }
3974
3975
3976 /* Compute the face one character before or after the current position
3977 of IT, in the visual order. BEFORE_P non-zero means get the face
3978 in front (to the left in L2R paragraphs, to the right in R2L
3979 paragraphs) of IT's screen position. Value is the ID of the face. */
3980
3981 static int
3982 face_before_or_after_it_pos (struct it *it, int before_p)
3983 {
3984 int face_id, limit;
3985 ptrdiff_t next_check_charpos;
3986 struct it it_copy;
3987 void *it_copy_data = NULL;
3988
3989 eassert (it->s == NULL);
3990
3991 if (STRINGP (it->string))
3992 {
3993 ptrdiff_t bufpos, charpos;
3994 int base_face_id;
3995
3996 /* No face change past the end of the string (for the case
3997 we are padding with spaces). No face change before the
3998 string start. */
3999 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4000 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4001 return it->face_id;
4002
4003 if (!it->bidi_p)
4004 {
4005 /* Set charpos to the position before or after IT's current
4006 position, in the logical order, which in the non-bidi
4007 case is the same as the visual order. */
4008 if (before_p)
4009 charpos = IT_STRING_CHARPOS (*it) - 1;
4010 else if (it->what == IT_COMPOSITION)
4011 /* For composition, we must check the character after the
4012 composition. */
4013 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4014 else
4015 charpos = IT_STRING_CHARPOS (*it) + 1;
4016 }
4017 else
4018 {
4019 if (before_p)
4020 {
4021 /* With bidi iteration, the character before the current
4022 in the visual order cannot be found by simple
4023 iteration, because "reverse" reordering is not
4024 supported. Instead, we need to use the move_it_*
4025 family of functions. */
4026 /* Ignore face changes before the first visible
4027 character on this display line. */
4028 if (it->current_x <= it->first_visible_x)
4029 return it->face_id;
4030 SAVE_IT (it_copy, *it, it_copy_data);
4031 /* Implementation note: Since move_it_in_display_line
4032 works in the iterator geometry, and thinks the first
4033 character is always the leftmost, even in R2L lines,
4034 we don't need to distinguish between the R2L and L2R
4035 cases here. */
4036 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4037 it_copy.current_x - 1, MOVE_TO_X);
4038 charpos = IT_STRING_CHARPOS (it_copy);
4039 RESTORE_IT (it, it, it_copy_data);
4040 }
4041 else
4042 {
4043 /* Set charpos to the string position of the character
4044 that comes after IT's current position in the visual
4045 order. */
4046 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4047
4048 it_copy = *it;
4049 while (n--)
4050 bidi_move_to_visually_next (&it_copy.bidi_it);
4051
4052 charpos = it_copy.bidi_it.charpos;
4053 }
4054 }
4055 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4056
4057 if (it->current.overlay_string_index >= 0)
4058 bufpos = IT_CHARPOS (*it);
4059 else
4060 bufpos = 0;
4061
4062 base_face_id = underlying_face_id (it);
4063
4064 /* Get the face for ASCII, or unibyte. */
4065 face_id = face_at_string_position (it->w,
4066 it->string,
4067 charpos,
4068 bufpos,
4069 &next_check_charpos,
4070 base_face_id, 0);
4071
4072 /* Correct the face for charsets different from ASCII. Do it
4073 for the multibyte case only. The face returned above is
4074 suitable for unibyte text if IT->string is unibyte. */
4075 if (STRING_MULTIBYTE (it->string))
4076 {
4077 struct text_pos pos1 = string_pos (charpos, it->string);
4078 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4079 int c, len;
4080 struct face *face = FACE_FROM_ID (it->f, face_id);
4081
4082 c = string_char_and_length (p, &len);
4083 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4084 }
4085 }
4086 else
4087 {
4088 struct text_pos pos;
4089
4090 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4091 || (IT_CHARPOS (*it) <= BEGV && before_p))
4092 return it->face_id;
4093
4094 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4095 pos = it->current.pos;
4096
4097 if (!it->bidi_p)
4098 {
4099 if (before_p)
4100 DEC_TEXT_POS (pos, it->multibyte_p);
4101 else
4102 {
4103 if (it->what == IT_COMPOSITION)
4104 {
4105 /* For composition, we must check the position after
4106 the composition. */
4107 pos.charpos += it->cmp_it.nchars;
4108 pos.bytepos += it->len;
4109 }
4110 else
4111 INC_TEXT_POS (pos, it->multibyte_p);
4112 }
4113 }
4114 else
4115 {
4116 if (before_p)
4117 {
4118 /* With bidi iteration, the character before the current
4119 in the visual order cannot be found by simple
4120 iteration, because "reverse" reordering is not
4121 supported. Instead, we need to use the move_it_*
4122 family of functions. */
4123 /* Ignore face changes before the first visible
4124 character on this display line. */
4125 if (it->current_x <= it->first_visible_x)
4126 return it->face_id;
4127 SAVE_IT (it_copy, *it, it_copy_data);
4128 /* Implementation note: Since move_it_in_display_line
4129 works in the iterator geometry, and thinks the first
4130 character is always the leftmost, even in R2L lines,
4131 we don't need to distinguish between the R2L and L2R
4132 cases here. */
4133 move_it_in_display_line (&it_copy, ZV,
4134 it_copy.current_x - 1, MOVE_TO_X);
4135 pos = it_copy.current.pos;
4136 RESTORE_IT (it, it, it_copy_data);
4137 }
4138 else
4139 {
4140 /* Set charpos to the buffer position of the character
4141 that comes after IT's current position in the visual
4142 order. */
4143 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4144
4145 it_copy = *it;
4146 while (n--)
4147 bidi_move_to_visually_next (&it_copy.bidi_it);
4148
4149 SET_TEXT_POS (pos,
4150 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4151 }
4152 }
4153 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4154
4155 /* Determine face for CHARSET_ASCII, or unibyte. */
4156 face_id = face_at_buffer_position (it->w,
4157 CHARPOS (pos),
4158 &next_check_charpos,
4159 limit, 0, -1);
4160
4161 /* Correct the face for charsets different from ASCII. Do it
4162 for the multibyte case only. The face returned above is
4163 suitable for unibyte text if current_buffer is unibyte. */
4164 if (it->multibyte_p)
4165 {
4166 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4167 struct face *face = FACE_FROM_ID (it->f, face_id);
4168 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4169 }
4170 }
4171
4172 return face_id;
4173 }
4174
4175
4176 \f
4177 /***********************************************************************
4178 Invisible text
4179 ***********************************************************************/
4180
4181 /* Set up iterator IT from invisible properties at its current
4182 position. Called from handle_stop. */
4183
4184 static enum prop_handled
4185 handle_invisible_prop (struct it *it)
4186 {
4187 enum prop_handled handled = HANDLED_NORMALLY;
4188 int invis_p;
4189 Lisp_Object prop;
4190
4191 if (STRINGP (it->string))
4192 {
4193 Lisp_Object end_charpos, limit, charpos;
4194
4195 /* Get the value of the invisible text property at the
4196 current position. Value will be nil if there is no such
4197 property. */
4198 charpos = make_number (IT_STRING_CHARPOS (*it));
4199 prop = Fget_text_property (charpos, Qinvisible, it->string);
4200 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4201
4202 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4203 {
4204 /* Record whether we have to display an ellipsis for the
4205 invisible text. */
4206 int display_ellipsis_p = (invis_p == 2);
4207 ptrdiff_t len, endpos;
4208
4209 handled = HANDLED_RECOMPUTE_PROPS;
4210
4211 /* Get the position at which the next visible text can be
4212 found in IT->string, if any. */
4213 endpos = len = SCHARS (it->string);
4214 XSETINT (limit, len);
4215 do
4216 {
4217 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4218 it->string, limit);
4219 if (INTEGERP (end_charpos))
4220 {
4221 endpos = XFASTINT (end_charpos);
4222 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4223 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4224 if (invis_p == 2)
4225 display_ellipsis_p = true;
4226 }
4227 }
4228 while (invis_p && endpos < len);
4229
4230 if (display_ellipsis_p)
4231 it->ellipsis_p = true;
4232
4233 if (endpos < len)
4234 {
4235 /* Text at END_CHARPOS is visible. Move IT there. */
4236 struct text_pos old;
4237 ptrdiff_t oldpos;
4238
4239 old = it->current.string_pos;
4240 oldpos = CHARPOS (old);
4241 if (it->bidi_p)
4242 {
4243 if (it->bidi_it.first_elt
4244 && it->bidi_it.charpos < SCHARS (it->string))
4245 bidi_paragraph_init (it->paragraph_embedding,
4246 &it->bidi_it, 1);
4247 /* Bidi-iterate out of the invisible text. */
4248 do
4249 {
4250 bidi_move_to_visually_next (&it->bidi_it);
4251 }
4252 while (oldpos <= it->bidi_it.charpos
4253 && it->bidi_it.charpos < endpos);
4254
4255 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4256 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4257 if (IT_CHARPOS (*it) >= endpos)
4258 it->prev_stop = endpos;
4259 }
4260 else
4261 {
4262 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4263 compute_string_pos (&it->current.string_pos, old, it->string);
4264 }
4265 }
4266 else
4267 {
4268 /* The rest of the string is invisible. If this is an
4269 overlay string, proceed with the next overlay string
4270 or whatever comes and return a character from there. */
4271 if (it->current.overlay_string_index >= 0
4272 && !display_ellipsis_p)
4273 {
4274 next_overlay_string (it);
4275 /* Don't check for overlay strings when we just
4276 finished processing them. */
4277 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4278 }
4279 else
4280 {
4281 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4282 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4283 }
4284 }
4285 }
4286 }
4287 else
4288 {
4289 ptrdiff_t newpos, next_stop, start_charpos, tem;
4290 Lisp_Object pos, overlay;
4291
4292 /* First of all, is there invisible text at this position? */
4293 tem = start_charpos = IT_CHARPOS (*it);
4294 pos = make_number (tem);
4295 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4296 &overlay);
4297 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4298
4299 /* If we are on invisible text, skip over it. */
4300 if (invis_p && start_charpos < it->end_charpos)
4301 {
4302 /* Record whether we have to display an ellipsis for the
4303 invisible text. */
4304 int display_ellipsis_p = invis_p == 2;
4305
4306 handled = HANDLED_RECOMPUTE_PROPS;
4307
4308 /* Loop skipping over invisible text. The loop is left at
4309 ZV or with IT on the first char being visible again. */
4310 do
4311 {
4312 /* Try to skip some invisible text. Return value is the
4313 position reached which can be equal to where we start
4314 if there is nothing invisible there. This skips both
4315 over invisible text properties and overlays with
4316 invisible property. */
4317 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4318
4319 /* If we skipped nothing at all we weren't at invisible
4320 text in the first place. If everything to the end of
4321 the buffer was skipped, end the loop. */
4322 if (newpos == tem || newpos >= ZV)
4323 invis_p = 0;
4324 else
4325 {
4326 /* We skipped some characters but not necessarily
4327 all there are. Check if we ended up on visible
4328 text. Fget_char_property returns the property of
4329 the char before the given position, i.e. if we
4330 get invis_p = 0, this means that the char at
4331 newpos is visible. */
4332 pos = make_number (newpos);
4333 prop = Fget_char_property (pos, Qinvisible, it->window);
4334 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4335 }
4336
4337 /* If we ended up on invisible text, proceed to
4338 skip starting with next_stop. */
4339 if (invis_p)
4340 tem = next_stop;
4341
4342 /* If there are adjacent invisible texts, don't lose the
4343 second one's ellipsis. */
4344 if (invis_p == 2)
4345 display_ellipsis_p = true;
4346 }
4347 while (invis_p);
4348
4349 /* The position newpos is now either ZV or on visible text. */
4350 if (it->bidi_p)
4351 {
4352 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4353 int on_newline
4354 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4355 int after_newline
4356 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4357
4358 /* If the invisible text ends on a newline or on a
4359 character after a newline, we can avoid the costly,
4360 character by character, bidi iteration to NEWPOS, and
4361 instead simply reseat the iterator there. That's
4362 because all bidi reordering information is tossed at
4363 the newline. This is a big win for modes that hide
4364 complete lines, like Outline, Org, etc. */
4365 if (on_newline || after_newline)
4366 {
4367 struct text_pos tpos;
4368 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4369
4370 SET_TEXT_POS (tpos, newpos, bpos);
4371 reseat_1 (it, tpos, 0);
4372 /* If we reseat on a newline/ZV, we need to prep the
4373 bidi iterator for advancing to the next character
4374 after the newline/EOB, keeping the current paragraph
4375 direction (so that PRODUCE_GLYPHS does TRT wrt
4376 prepending/appending glyphs to a glyph row). */
4377 if (on_newline)
4378 {
4379 it->bidi_it.first_elt = 0;
4380 it->bidi_it.paragraph_dir = pdir;
4381 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4382 it->bidi_it.nchars = 1;
4383 it->bidi_it.ch_len = 1;
4384 }
4385 }
4386 else /* Must use the slow method. */
4387 {
4388 /* With bidi iteration, the region of invisible text
4389 could start and/or end in the middle of a
4390 non-base embedding level. Therefore, we need to
4391 skip invisible text using the bidi iterator,
4392 starting at IT's current position, until we find
4393 ourselves outside of the invisible text.
4394 Skipping invisible text _after_ bidi iteration
4395 avoids affecting the visual order of the
4396 displayed text when invisible properties are
4397 added or removed. */
4398 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4399 {
4400 /* If we were `reseat'ed to a new paragraph,
4401 determine the paragraph base direction. We
4402 need to do it now because
4403 next_element_from_buffer may not have a
4404 chance to do it, if we are going to skip any
4405 text at the beginning, which resets the
4406 FIRST_ELT flag. */
4407 bidi_paragraph_init (it->paragraph_embedding,
4408 &it->bidi_it, 1);
4409 }
4410 do
4411 {
4412 bidi_move_to_visually_next (&it->bidi_it);
4413 }
4414 while (it->stop_charpos <= it->bidi_it.charpos
4415 && it->bidi_it.charpos < newpos);
4416 IT_CHARPOS (*it) = it->bidi_it.charpos;
4417 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4418 /* If we overstepped NEWPOS, record its position in
4419 the iterator, so that we skip invisible text if
4420 later the bidi iteration lands us in the
4421 invisible region again. */
4422 if (IT_CHARPOS (*it) >= newpos)
4423 it->prev_stop = newpos;
4424 }
4425 }
4426 else
4427 {
4428 IT_CHARPOS (*it) = newpos;
4429 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4430 }
4431
4432 /* If there are before-strings at the start of invisible
4433 text, and the text is invisible because of a text
4434 property, arrange to show before-strings because 20.x did
4435 it that way. (If the text is invisible because of an
4436 overlay property instead of a text property, this is
4437 already handled in the overlay code.) */
4438 if (NILP (overlay)
4439 && get_overlay_strings (it, it->stop_charpos))
4440 {
4441 handled = HANDLED_RECOMPUTE_PROPS;
4442 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4443 }
4444 else if (display_ellipsis_p)
4445 {
4446 /* Make sure that the glyphs of the ellipsis will get
4447 correct `charpos' values. If we would not update
4448 it->position here, the glyphs would belong to the
4449 last visible character _before_ the invisible
4450 text, which confuses `set_cursor_from_row'.
4451
4452 We use the last invisible position instead of the
4453 first because this way the cursor is always drawn on
4454 the first "." of the ellipsis, whenever PT is inside
4455 the invisible text. Otherwise the cursor would be
4456 placed _after_ the ellipsis when the point is after the
4457 first invisible character. */
4458 if (!STRINGP (it->object))
4459 {
4460 it->position.charpos = newpos - 1;
4461 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4462 }
4463 it->ellipsis_p = true;
4464 /* Let the ellipsis display before
4465 considering any properties of the following char.
4466 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4467 handled = HANDLED_RETURN;
4468 }
4469 }
4470 }
4471
4472 return handled;
4473 }
4474
4475
4476 /* Make iterator IT return `...' next.
4477 Replaces LEN characters from buffer. */
4478
4479 static void
4480 setup_for_ellipsis (struct it *it, int len)
4481 {
4482 /* Use the display table definition for `...'. Invalid glyphs
4483 will be handled by the method returning elements from dpvec. */
4484 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4485 {
4486 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4487 it->dpvec = v->contents;
4488 it->dpend = v->contents + v->header.size;
4489 }
4490 else
4491 {
4492 /* Default `...'. */
4493 it->dpvec = default_invis_vector;
4494 it->dpend = default_invis_vector + 3;
4495 }
4496
4497 it->dpvec_char_len = len;
4498 it->current.dpvec_index = 0;
4499 it->dpvec_face_id = -1;
4500
4501 /* Remember the current face id in case glyphs specify faces.
4502 IT's face is restored in set_iterator_to_next.
4503 saved_face_id was set to preceding char's face in handle_stop. */
4504 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4505 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4506
4507 it->method = GET_FROM_DISPLAY_VECTOR;
4508 it->ellipsis_p = true;
4509 }
4510
4511
4512 \f
4513 /***********************************************************************
4514 'display' property
4515 ***********************************************************************/
4516
4517 /* Set up iterator IT from `display' property at its current position.
4518 Called from handle_stop.
4519 We return HANDLED_RETURN if some part of the display property
4520 overrides the display of the buffer text itself.
4521 Otherwise we return HANDLED_NORMALLY. */
4522
4523 static enum prop_handled
4524 handle_display_prop (struct it *it)
4525 {
4526 Lisp_Object propval, object, overlay;
4527 struct text_pos *position;
4528 ptrdiff_t bufpos;
4529 /* Nonzero if some property replaces the display of the text itself. */
4530 int display_replaced_p = 0;
4531
4532 if (STRINGP (it->string))
4533 {
4534 object = it->string;
4535 position = &it->current.string_pos;
4536 bufpos = CHARPOS (it->current.pos);
4537 }
4538 else
4539 {
4540 XSETWINDOW (object, it->w);
4541 position = &it->current.pos;
4542 bufpos = CHARPOS (*position);
4543 }
4544
4545 /* Reset those iterator values set from display property values. */
4546 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4547 it->space_width = Qnil;
4548 it->font_height = Qnil;
4549 it->voffset = 0;
4550
4551 /* We don't support recursive `display' properties, i.e. string
4552 values that have a string `display' property, that have a string
4553 `display' property etc. */
4554 if (!it->string_from_display_prop_p)
4555 it->area = TEXT_AREA;
4556
4557 propval = get_char_property_and_overlay (make_number (position->charpos),
4558 Qdisplay, object, &overlay);
4559 if (NILP (propval))
4560 return HANDLED_NORMALLY;
4561 /* Now OVERLAY is the overlay that gave us this property, or nil
4562 if it was a text property. */
4563
4564 if (!STRINGP (it->string))
4565 object = it->w->contents;
4566
4567 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4568 position, bufpos,
4569 FRAME_WINDOW_P (it->f));
4570
4571 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4572 }
4573
4574 /* Subroutine of handle_display_prop. Returns non-zero if the display
4575 specification in SPEC is a replacing specification, i.e. it would
4576 replace the text covered by `display' property with something else,
4577 such as an image or a display string. If SPEC includes any kind or
4578 `(space ...) specification, the value is 2; this is used by
4579 compute_display_string_pos, which see.
4580
4581 See handle_single_display_spec for documentation of arguments.
4582 frame_window_p is non-zero if the window being redisplayed is on a
4583 GUI frame; this argument is used only if IT is NULL, see below.
4584
4585 IT can be NULL, if this is called by the bidi reordering code
4586 through compute_display_string_pos, which see. In that case, this
4587 function only examines SPEC, but does not otherwise "handle" it, in
4588 the sense that it doesn't set up members of IT from the display
4589 spec. */
4590 static int
4591 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4592 Lisp_Object overlay, struct text_pos *position,
4593 ptrdiff_t bufpos, int frame_window_p)
4594 {
4595 int replacing_p = 0;
4596 int rv;
4597
4598 if (CONSP (spec)
4599 /* Simple specifications. */
4600 && !EQ (XCAR (spec), Qimage)
4601 && !EQ (XCAR (spec), Qspace)
4602 && !EQ (XCAR (spec), Qwhen)
4603 && !EQ (XCAR (spec), Qslice)
4604 && !EQ (XCAR (spec), Qspace_width)
4605 && !EQ (XCAR (spec), Qheight)
4606 && !EQ (XCAR (spec), Qraise)
4607 /* Marginal area specifications. */
4608 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4609 && !EQ (XCAR (spec), Qleft_fringe)
4610 && !EQ (XCAR (spec), Qright_fringe)
4611 && !NILP (XCAR (spec)))
4612 {
4613 for (; CONSP (spec); spec = XCDR (spec))
4614 {
4615 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4616 overlay, position, bufpos,
4617 replacing_p, frame_window_p)))
4618 {
4619 replacing_p = rv;
4620 /* If some text in a string is replaced, `position' no
4621 longer points to the position of `object'. */
4622 if (!it || STRINGP (object))
4623 break;
4624 }
4625 }
4626 }
4627 else if (VECTORP (spec))
4628 {
4629 ptrdiff_t i;
4630 for (i = 0; i < ASIZE (spec); ++i)
4631 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4632 overlay, position, bufpos,
4633 replacing_p, frame_window_p)))
4634 {
4635 replacing_p = rv;
4636 /* If some text in a string is replaced, `position' no
4637 longer points to the position of `object'. */
4638 if (!it || STRINGP (object))
4639 break;
4640 }
4641 }
4642 else
4643 {
4644 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4645 position, bufpos, 0,
4646 frame_window_p)))
4647 replacing_p = rv;
4648 }
4649
4650 return replacing_p;
4651 }
4652
4653 /* Value is the position of the end of the `display' property starting
4654 at START_POS in OBJECT. */
4655
4656 static struct text_pos
4657 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4658 {
4659 Lisp_Object end;
4660 struct text_pos end_pos;
4661
4662 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4663 Qdisplay, object, Qnil);
4664 CHARPOS (end_pos) = XFASTINT (end);
4665 if (STRINGP (object))
4666 compute_string_pos (&end_pos, start_pos, it->string);
4667 else
4668 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4669
4670 return end_pos;
4671 }
4672
4673
4674 /* Set up IT from a single `display' property specification SPEC. OBJECT
4675 is the object in which the `display' property was found. *POSITION
4676 is the position in OBJECT at which the `display' property was found.
4677 BUFPOS is the buffer position of OBJECT (different from POSITION if
4678 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4679 previously saw a display specification which already replaced text
4680 display with something else, for example an image; we ignore such
4681 properties after the first one has been processed.
4682
4683 OVERLAY is the overlay this `display' property came from,
4684 or nil if it was a text property.
4685
4686 If SPEC is a `space' or `image' specification, and in some other
4687 cases too, set *POSITION to the position where the `display'
4688 property ends.
4689
4690 If IT is NULL, only examine the property specification in SPEC, but
4691 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4692 is intended to be displayed in a window on a GUI frame.
4693
4694 Value is non-zero if something was found which replaces the display
4695 of buffer or string text. */
4696
4697 static int
4698 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4699 Lisp_Object overlay, struct text_pos *position,
4700 ptrdiff_t bufpos, int display_replaced_p,
4701 int frame_window_p)
4702 {
4703 Lisp_Object form;
4704 Lisp_Object location, value;
4705 struct text_pos start_pos = *position;
4706 int valid_p;
4707
4708 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4709 If the result is non-nil, use VALUE instead of SPEC. */
4710 form = Qt;
4711 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4712 {
4713 spec = XCDR (spec);
4714 if (!CONSP (spec))
4715 return 0;
4716 form = XCAR (spec);
4717 spec = XCDR (spec);
4718 }
4719
4720 if (!NILP (form) && !EQ (form, Qt))
4721 {
4722 ptrdiff_t count = SPECPDL_INDEX ();
4723 struct gcpro gcpro1;
4724
4725 /* Bind `object' to the object having the `display' property, a
4726 buffer or string. Bind `position' to the position in the
4727 object where the property was found, and `buffer-position'
4728 to the current position in the buffer. */
4729
4730 if (NILP (object))
4731 XSETBUFFER (object, current_buffer);
4732 specbind (Qobject, object);
4733 specbind (Qposition, make_number (CHARPOS (*position)));
4734 specbind (Qbuffer_position, make_number (bufpos));
4735 GCPRO1 (form);
4736 form = safe_eval (form);
4737 UNGCPRO;
4738 unbind_to (count, Qnil);
4739 }
4740
4741 if (NILP (form))
4742 return 0;
4743
4744 /* Handle `(height HEIGHT)' specifications. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qheight)
4747 && CONSP (XCDR (spec)))
4748 {
4749 if (it)
4750 {
4751 if (!FRAME_WINDOW_P (it->f))
4752 return 0;
4753
4754 it->font_height = XCAR (XCDR (spec));
4755 if (!NILP (it->font_height))
4756 {
4757 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4758 int new_height = -1;
4759
4760 if (CONSP (it->font_height)
4761 && (EQ (XCAR (it->font_height), Qplus)
4762 || EQ (XCAR (it->font_height), Qminus))
4763 && CONSP (XCDR (it->font_height))
4764 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4765 {
4766 /* `(+ N)' or `(- N)' where N is an integer. */
4767 int steps = XINT (XCAR (XCDR (it->font_height)));
4768 if (EQ (XCAR (it->font_height), Qplus))
4769 steps = - steps;
4770 it->face_id = smaller_face (it->f, it->face_id, steps);
4771 }
4772 else if (FUNCTIONP (it->font_height))
4773 {
4774 /* Call function with current height as argument.
4775 Value is the new height. */
4776 Lisp_Object height;
4777 height = safe_call1 (it->font_height,
4778 face->lface[LFACE_HEIGHT_INDEX]);
4779 if (NUMBERP (height))
4780 new_height = XFLOATINT (height);
4781 }
4782 else if (NUMBERP (it->font_height))
4783 {
4784 /* Value is a multiple of the canonical char height. */
4785 struct face *f;
4786
4787 f = FACE_FROM_ID (it->f,
4788 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4789 new_height = (XFLOATINT (it->font_height)
4790 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4791 }
4792 else
4793 {
4794 /* Evaluate IT->font_height with `height' bound to the
4795 current specified height to get the new height. */
4796 ptrdiff_t count = SPECPDL_INDEX ();
4797
4798 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4799 value = safe_eval (it->font_height);
4800 unbind_to (count, Qnil);
4801
4802 if (NUMBERP (value))
4803 new_height = XFLOATINT (value);
4804 }
4805
4806 if (new_height > 0)
4807 it->face_id = face_with_height (it->f, it->face_id, new_height);
4808 }
4809 }
4810
4811 return 0;
4812 }
4813
4814 /* Handle `(space-width WIDTH)'. */
4815 if (CONSP (spec)
4816 && EQ (XCAR (spec), Qspace_width)
4817 && CONSP (XCDR (spec)))
4818 {
4819 if (it)
4820 {
4821 if (!FRAME_WINDOW_P (it->f))
4822 return 0;
4823
4824 value = XCAR (XCDR (spec));
4825 if (NUMBERP (value) && XFLOATINT (value) > 0)
4826 it->space_width = value;
4827 }
4828
4829 return 0;
4830 }
4831
4832 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4833 if (CONSP (spec)
4834 && EQ (XCAR (spec), Qslice))
4835 {
4836 Lisp_Object tem;
4837
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4842
4843 if (tem = XCDR (spec), CONSP (tem))
4844 {
4845 it->slice.x = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 {
4848 it->slice.y = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4850 {
4851 it->slice.width = XCAR (tem);
4852 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.height = XCAR (tem);
4854 }
4855 }
4856 }
4857 }
4858
4859 return 0;
4860 }
4861
4862 /* Handle `(raise FACTOR)'. */
4863 if (CONSP (spec)
4864 && EQ (XCAR (spec), Qraise)
4865 && CONSP (XCDR (spec)))
4866 {
4867 if (it)
4868 {
4869 if (!FRAME_WINDOW_P (it->f))
4870 return 0;
4871
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 value = XCAR (XCDR (spec));
4874 if (NUMBERP (value))
4875 {
4876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4877 it->voffset = - (XFLOATINT (value)
4878 * (FONT_HEIGHT (face->font)));
4879 }
4880 #endif /* HAVE_WINDOW_SYSTEM */
4881 }
4882
4883 return 0;
4884 }
4885
4886 /* Don't handle the other kinds of display specifications
4887 inside a string that we got from a `display' property. */
4888 if (it && it->string_from_display_prop_p)
4889 return 0;
4890
4891 /* Characters having this form of property are not displayed, so
4892 we have to find the end of the property. */
4893 if (it)
4894 {
4895 start_pos = *position;
4896 *position = display_prop_end (it, object, start_pos);
4897 }
4898 value = Qnil;
4899
4900 /* Stop the scan at that end position--we assume that all
4901 text properties change there. */
4902 if (it)
4903 it->stop_charpos = position->charpos;
4904
4905 /* Handle `(left-fringe BITMAP [FACE])'
4906 and `(right-fringe BITMAP [FACE])'. */
4907 if (CONSP (spec)
4908 && (EQ (XCAR (spec), Qleft_fringe)
4909 || EQ (XCAR (spec), Qright_fringe))
4910 && CONSP (XCDR (spec)))
4911 {
4912 int fringe_bitmap;
4913
4914 if (it)
4915 {
4916 if (!FRAME_WINDOW_P (it->f))
4917 /* If we return here, POSITION has been advanced
4918 across the text with this property. */
4919 {
4920 /* Synchronize the bidi iterator with POSITION. This is
4921 needed because we are not going to push the iterator
4922 on behalf of this display property, so there will be
4923 no pop_it call to do this synchronization for us. */
4924 if (it->bidi_p)
4925 {
4926 it->position = *position;
4927 iterate_out_of_display_property (it);
4928 *position = it->position;
4929 }
4930 return 1;
4931 }
4932 }
4933 else if (!frame_window_p)
4934 return 1;
4935
4936 #ifdef HAVE_WINDOW_SYSTEM
4937 value = XCAR (XCDR (spec));
4938 if (!SYMBOLP (value)
4939 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4940 /* If we return here, POSITION has been advanced
4941 across the text with this property. */
4942 {
4943 if (it && it->bidi_p)
4944 {
4945 it->position = *position;
4946 iterate_out_of_display_property (it);
4947 *position = it->position;
4948 }
4949 return 1;
4950 }
4951
4952 if (it)
4953 {
4954 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4955
4956 if (CONSP (XCDR (XCDR (spec))))
4957 {
4958 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4959 int face_id2 = lookup_derived_face (it->f, face_name,
4960 FRINGE_FACE_ID, 0);
4961 if (face_id2 >= 0)
4962 face_id = face_id2;
4963 }
4964
4965 /* Save current settings of IT so that we can restore them
4966 when we are finished with the glyph property value. */
4967 push_it (it, position);
4968
4969 it->area = TEXT_AREA;
4970 it->what = IT_IMAGE;
4971 it->image_id = -1; /* no image */
4972 it->position = start_pos;
4973 it->object = NILP (object) ? it->w->contents : object;
4974 it->method = GET_FROM_IMAGE;
4975 it->from_overlay = Qnil;
4976 it->face_id = face_id;
4977 it->from_disp_prop_p = true;
4978
4979 /* Say that we haven't consumed the characters with
4980 `display' property yet. The call to pop_it in
4981 set_iterator_to_next will clean this up. */
4982 *position = start_pos;
4983
4984 if (EQ (XCAR (spec), Qleft_fringe))
4985 {
4986 it->left_user_fringe_bitmap = fringe_bitmap;
4987 it->left_user_fringe_face_id = face_id;
4988 }
4989 else
4990 {
4991 it->right_user_fringe_bitmap = fringe_bitmap;
4992 it->right_user_fringe_face_id = face_id;
4993 }
4994 }
4995 #endif /* HAVE_WINDOW_SYSTEM */
4996 return 1;
4997 }
4998
4999 /* Prepare to handle `((margin left-margin) ...)',
5000 `((margin right-margin) ...)' and `((margin nil) ...)'
5001 prefixes for display specifications. */
5002 location = Qunbound;
5003 if (CONSP (spec) && CONSP (XCAR (spec)))
5004 {
5005 Lisp_Object tem;
5006
5007 value = XCDR (spec);
5008 if (CONSP (value))
5009 value = XCAR (value);
5010
5011 tem = XCAR (spec);
5012 if (EQ (XCAR (tem), Qmargin)
5013 && (tem = XCDR (tem),
5014 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5015 (NILP (tem)
5016 || EQ (tem, Qleft_margin)
5017 || EQ (tem, Qright_margin))))
5018 location = tem;
5019 }
5020
5021 if (EQ (location, Qunbound))
5022 {
5023 location = Qnil;
5024 value = spec;
5025 }
5026
5027 /* After this point, VALUE is the property after any
5028 margin prefix has been stripped. It must be a string,
5029 an image specification, or `(space ...)'.
5030
5031 LOCATION specifies where to display: `left-margin',
5032 `right-margin' or nil. */
5033
5034 valid_p = (STRINGP (value)
5035 #ifdef HAVE_WINDOW_SYSTEM
5036 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5037 && valid_image_p (value))
5038 #endif /* not HAVE_WINDOW_SYSTEM */
5039 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5040
5041 if (valid_p && !display_replaced_p)
5042 {
5043 int retval = 1;
5044
5045 if (!it)
5046 {
5047 /* Callers need to know whether the display spec is any kind
5048 of `(space ...)' spec that is about to affect text-area
5049 display. */
5050 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5051 retval = 2;
5052 return retval;
5053 }
5054
5055 /* Save current settings of IT so that we can restore them
5056 when we are finished with the glyph property value. */
5057 push_it (it, position);
5058 it->from_overlay = overlay;
5059 it->from_disp_prop_p = true;
5060
5061 if (NILP (location))
5062 it->area = TEXT_AREA;
5063 else if (EQ (location, Qleft_margin))
5064 it->area = LEFT_MARGIN_AREA;
5065 else
5066 it->area = RIGHT_MARGIN_AREA;
5067
5068 if (STRINGP (value))
5069 {
5070 it->string = value;
5071 it->multibyte_p = STRING_MULTIBYTE (it->string);
5072 it->current.overlay_string_index = -1;
5073 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5074 it->end_charpos = it->string_nchars = SCHARS (it->string);
5075 it->method = GET_FROM_STRING;
5076 it->stop_charpos = 0;
5077 it->prev_stop = 0;
5078 it->base_level_stop = 0;
5079 it->string_from_display_prop_p = true;
5080 /* Say that we haven't consumed the characters with
5081 `display' property yet. The call to pop_it in
5082 set_iterator_to_next will clean this up. */
5083 if (BUFFERP (object))
5084 *position = start_pos;
5085
5086 /* Force paragraph direction to be that of the parent
5087 object. If the parent object's paragraph direction is
5088 not yet determined, default to L2R. */
5089 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5090 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5091 else
5092 it->paragraph_embedding = L2R;
5093
5094 /* Set up the bidi iterator for this display string. */
5095 if (it->bidi_p)
5096 {
5097 it->bidi_it.string.lstring = it->string;
5098 it->bidi_it.string.s = NULL;
5099 it->bidi_it.string.schars = it->end_charpos;
5100 it->bidi_it.string.bufpos = bufpos;
5101 it->bidi_it.string.from_disp_str = 1;
5102 it->bidi_it.string.unibyte = !it->multibyte_p;
5103 it->bidi_it.w = it->w;
5104 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5105 }
5106 }
5107 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5108 {
5109 it->method = GET_FROM_STRETCH;
5110 it->object = value;
5111 *position = it->position = start_pos;
5112 retval = 1 + (it->area == TEXT_AREA);
5113 }
5114 #ifdef HAVE_WINDOW_SYSTEM
5115 else
5116 {
5117 it->what = IT_IMAGE;
5118 it->image_id = lookup_image (it->f, value);
5119 it->position = start_pos;
5120 it->object = NILP (object) ? it->w->contents : object;
5121 it->method = GET_FROM_IMAGE;
5122
5123 /* Say that we haven't consumed the characters with
5124 `display' property yet. The call to pop_it in
5125 set_iterator_to_next will clean this up. */
5126 *position = start_pos;
5127 }
5128 #endif /* HAVE_WINDOW_SYSTEM */
5129
5130 return retval;
5131 }
5132
5133 /* Invalid property or property not supported. Restore
5134 POSITION to what it was before. */
5135 *position = start_pos;
5136 return 0;
5137 }
5138
5139 /* Check if PROP is a display property value whose text should be
5140 treated as intangible. OVERLAY is the overlay from which PROP
5141 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5142 specify the buffer position covered by PROP. */
5143
5144 int
5145 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5146 ptrdiff_t charpos, ptrdiff_t bytepos)
5147 {
5148 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5149 struct text_pos position;
5150
5151 SET_TEXT_POS (position, charpos, bytepos);
5152 return handle_display_spec (NULL, prop, Qnil, overlay,
5153 &position, charpos, frame_window_p);
5154 }
5155
5156
5157 /* Return 1 if PROP is a display sub-property value containing STRING.
5158
5159 Implementation note: this and the following function are really
5160 special cases of handle_display_spec and
5161 handle_single_display_spec, and should ideally use the same code.
5162 Until they do, these two pairs must be consistent and must be
5163 modified in sync. */
5164
5165 static int
5166 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5167 {
5168 if (EQ (string, prop))
5169 return 1;
5170
5171 /* Skip over `when FORM'. */
5172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5173 {
5174 prop = XCDR (prop);
5175 if (!CONSP (prop))
5176 return 0;
5177 /* Actually, the condition following `when' should be eval'ed,
5178 like handle_single_display_spec does, and we should return
5179 zero if it evaluates to nil. However, this function is
5180 called only when the buffer was already displayed and some
5181 glyph in the glyph matrix was found to come from a display
5182 string. Therefore, the condition was already evaluated, and
5183 the result was non-nil, otherwise the display string wouldn't
5184 have been displayed and we would have never been called for
5185 this property. Thus, we can skip the evaluation and assume
5186 its result is non-nil. */
5187 prop = XCDR (prop);
5188 }
5189
5190 if (CONSP (prop))
5191 /* Skip over `margin LOCATION'. */
5192 if (EQ (XCAR (prop), Qmargin))
5193 {
5194 prop = XCDR (prop);
5195 if (!CONSP (prop))
5196 return 0;
5197
5198 prop = XCDR (prop);
5199 if (!CONSP (prop))
5200 return 0;
5201 }
5202
5203 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5204 }
5205
5206
5207 /* Return 1 if STRING appears in the `display' property PROP. */
5208
5209 static int
5210 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5211 {
5212 if (CONSP (prop)
5213 && !EQ (XCAR (prop), Qwhen)
5214 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5215 {
5216 /* A list of sub-properties. */
5217 while (CONSP (prop))
5218 {
5219 if (single_display_spec_string_p (XCAR (prop), string))
5220 return 1;
5221 prop = XCDR (prop);
5222 }
5223 }
5224 else if (VECTORP (prop))
5225 {
5226 /* A vector of sub-properties. */
5227 ptrdiff_t i;
5228 for (i = 0; i < ASIZE (prop); ++i)
5229 if (single_display_spec_string_p (AREF (prop, i), string))
5230 return 1;
5231 }
5232 else
5233 return single_display_spec_string_p (prop, string);
5234
5235 return 0;
5236 }
5237
5238 /* Look for STRING in overlays and text properties in the current
5239 buffer, between character positions FROM and TO (excluding TO).
5240 BACK_P non-zero means look back (in this case, TO is supposed to be
5241 less than FROM).
5242 Value is the first character position where STRING was found, or
5243 zero if it wasn't found before hitting TO.
5244
5245 This function may only use code that doesn't eval because it is
5246 called asynchronously from note_mouse_highlight. */
5247
5248 static ptrdiff_t
5249 string_buffer_position_lim (Lisp_Object string,
5250 ptrdiff_t from, ptrdiff_t to, int back_p)
5251 {
5252 Lisp_Object limit, prop, pos;
5253 int found = 0;
5254
5255 pos = make_number (max (from, BEGV));
5256
5257 if (!back_p) /* looking forward */
5258 {
5259 limit = make_number (min (to, ZV));
5260 while (!found && !EQ (pos, limit))
5261 {
5262 prop = Fget_char_property (pos, Qdisplay, Qnil);
5263 if (!NILP (prop) && display_prop_string_p (prop, string))
5264 found = 1;
5265 else
5266 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5267 limit);
5268 }
5269 }
5270 else /* looking back */
5271 {
5272 limit = make_number (max (to, BEGV));
5273 while (!found && !EQ (pos, limit))
5274 {
5275 prop = Fget_char_property (pos, Qdisplay, Qnil);
5276 if (!NILP (prop) && display_prop_string_p (prop, string))
5277 found = 1;
5278 else
5279 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5280 limit);
5281 }
5282 }
5283
5284 return found ? XINT (pos) : 0;
5285 }
5286
5287 /* Determine which buffer position in current buffer STRING comes from.
5288 AROUND_CHARPOS is an approximate position where it could come from.
5289 Value is the buffer position or 0 if it couldn't be determined.
5290
5291 This function is necessary because we don't record buffer positions
5292 in glyphs generated from strings (to keep struct glyph small).
5293 This function may only use code that doesn't eval because it is
5294 called asynchronously from note_mouse_highlight. */
5295
5296 static ptrdiff_t
5297 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5298 {
5299 const int MAX_DISTANCE = 1000;
5300 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5301 around_charpos + MAX_DISTANCE,
5302 0);
5303
5304 if (!found)
5305 found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos - MAX_DISTANCE, 1);
5307 return found;
5308 }
5309
5310
5311 \f
5312 /***********************************************************************
5313 `composition' property
5314 ***********************************************************************/
5315
5316 /* Set up iterator IT from `composition' property at its current
5317 position. Called from handle_stop. */
5318
5319 static enum prop_handled
5320 handle_composition_prop (struct it *it)
5321 {
5322 Lisp_Object prop, string;
5323 ptrdiff_t pos, pos_byte, start, end;
5324
5325 if (STRINGP (it->string))
5326 {
5327 unsigned char *s;
5328
5329 pos = IT_STRING_CHARPOS (*it);
5330 pos_byte = IT_STRING_BYTEPOS (*it);
5331 string = it->string;
5332 s = SDATA (string) + pos_byte;
5333 it->c = STRING_CHAR (s);
5334 }
5335 else
5336 {
5337 pos = IT_CHARPOS (*it);
5338 pos_byte = IT_BYTEPOS (*it);
5339 string = Qnil;
5340 it->c = FETCH_CHAR (pos_byte);
5341 }
5342
5343 /* If there's a valid composition and point is not inside of the
5344 composition (in the case that the composition is from the current
5345 buffer), draw a glyph composed from the composition components. */
5346 if (find_composition (pos, -1, &start, &end, &prop, string)
5347 && composition_valid_p (start, end, prop)
5348 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5349 {
5350 if (start < pos)
5351 /* As we can't handle this situation (perhaps font-lock added
5352 a new composition), we just return here hoping that next
5353 redisplay will detect this composition much earlier. */
5354 return HANDLED_NORMALLY;
5355 if (start != pos)
5356 {
5357 if (STRINGP (it->string))
5358 pos_byte = string_char_to_byte (it->string, start);
5359 else
5360 pos_byte = CHAR_TO_BYTE (start);
5361 }
5362 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5363 prop, string);
5364
5365 if (it->cmp_it.id >= 0)
5366 {
5367 it->cmp_it.ch = -1;
5368 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5369 it->cmp_it.nglyphs = -1;
5370 }
5371 }
5372
5373 return HANDLED_NORMALLY;
5374 }
5375
5376
5377 \f
5378 /***********************************************************************
5379 Overlay strings
5380 ***********************************************************************/
5381
5382 /* The following structure is used to record overlay strings for
5383 later sorting in load_overlay_strings. */
5384
5385 struct overlay_entry
5386 {
5387 Lisp_Object overlay;
5388 Lisp_Object string;
5389 EMACS_INT priority;
5390 int after_string_p;
5391 };
5392
5393
5394 /* Set up iterator IT from overlay strings at its current position.
5395 Called from handle_stop. */
5396
5397 static enum prop_handled
5398 handle_overlay_change (struct it *it)
5399 {
5400 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5401 return HANDLED_RECOMPUTE_PROPS;
5402 else
5403 return HANDLED_NORMALLY;
5404 }
5405
5406
5407 /* Set up the next overlay string for delivery by IT, if there is an
5408 overlay string to deliver. Called by set_iterator_to_next when the
5409 end of the current overlay string is reached. If there are more
5410 overlay strings to display, IT->string and
5411 IT->current.overlay_string_index are set appropriately here.
5412 Otherwise IT->string is set to nil. */
5413
5414 static void
5415 next_overlay_string (struct it *it)
5416 {
5417 ++it->current.overlay_string_index;
5418 if (it->current.overlay_string_index == it->n_overlay_strings)
5419 {
5420 /* No more overlay strings. Restore IT's settings to what
5421 they were before overlay strings were processed, and
5422 continue to deliver from current_buffer. */
5423
5424 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5425 pop_it (it);
5426 eassert (it->sp > 0
5427 || (NILP (it->string)
5428 && it->method == GET_FROM_BUFFER
5429 && it->stop_charpos >= BEGV
5430 && it->stop_charpos <= it->end_charpos));
5431 it->current.overlay_string_index = -1;
5432 it->n_overlay_strings = 0;
5433 it->overlay_strings_charpos = -1;
5434 /* If there's an empty display string on the stack, pop the
5435 stack, to resync the bidi iterator with IT's position. Such
5436 empty strings are pushed onto the stack in
5437 get_overlay_strings_1. */
5438 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5439 pop_it (it);
5440
5441 /* If we're at the end of the buffer, record that we have
5442 processed the overlay strings there already, so that
5443 next_element_from_buffer doesn't try it again. */
5444 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5445 it->overlay_strings_at_end_processed_p = true;
5446 }
5447 else
5448 {
5449 /* There are more overlay strings to process. If
5450 IT->current.overlay_string_index has advanced to a position
5451 where we must load IT->overlay_strings with more strings, do
5452 it. We must load at the IT->overlay_strings_charpos where
5453 IT->n_overlay_strings was originally computed; when invisible
5454 text is present, this might not be IT_CHARPOS (Bug#7016). */
5455 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5456
5457 if (it->current.overlay_string_index && i == 0)
5458 load_overlay_strings (it, it->overlay_strings_charpos);
5459
5460 /* Initialize IT to deliver display elements from the overlay
5461 string. */
5462 it->string = it->overlay_strings[i];
5463 it->multibyte_p = STRING_MULTIBYTE (it->string);
5464 SET_TEXT_POS (it->current.string_pos, 0, 0);
5465 it->method = GET_FROM_STRING;
5466 it->stop_charpos = 0;
5467 it->end_charpos = SCHARS (it->string);
5468 if (it->cmp_it.stop_pos >= 0)
5469 it->cmp_it.stop_pos = 0;
5470 it->prev_stop = 0;
5471 it->base_level_stop = 0;
5472
5473 /* Set up the bidi iterator for this overlay string. */
5474 if (it->bidi_p)
5475 {
5476 it->bidi_it.string.lstring = it->string;
5477 it->bidi_it.string.s = NULL;
5478 it->bidi_it.string.schars = SCHARS (it->string);
5479 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5480 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5481 it->bidi_it.string.unibyte = !it->multibyte_p;
5482 it->bidi_it.w = it->w;
5483 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5484 }
5485 }
5486
5487 CHECK_IT (it);
5488 }
5489
5490
5491 /* Compare two overlay_entry structures E1 and E2. Used as a
5492 comparison function for qsort in load_overlay_strings. Overlay
5493 strings for the same position are sorted so that
5494
5495 1. All after-strings come in front of before-strings, except
5496 when they come from the same overlay.
5497
5498 2. Within after-strings, strings are sorted so that overlay strings
5499 from overlays with higher priorities come first.
5500
5501 2. Within before-strings, strings are sorted so that overlay
5502 strings from overlays with higher priorities come last.
5503
5504 Value is analogous to strcmp. */
5505
5506
5507 static int
5508 compare_overlay_entries (const void *e1, const void *e2)
5509 {
5510 struct overlay_entry const *entry1 = e1;
5511 struct overlay_entry const *entry2 = e2;
5512 int result;
5513
5514 if (entry1->after_string_p != entry2->after_string_p)
5515 {
5516 /* Let after-strings appear in front of before-strings if
5517 they come from different overlays. */
5518 if (EQ (entry1->overlay, entry2->overlay))
5519 result = entry1->after_string_p ? 1 : -1;
5520 else
5521 result = entry1->after_string_p ? -1 : 1;
5522 }
5523 else if (entry1->priority != entry2->priority)
5524 {
5525 if (entry1->after_string_p)
5526 /* After-strings sorted in order of decreasing priority. */
5527 result = entry2->priority < entry1->priority ? -1 : 1;
5528 else
5529 /* Before-strings sorted in order of increasing priority. */
5530 result = entry1->priority < entry2->priority ? -1 : 1;
5531 }
5532 else
5533 result = 0;
5534
5535 return result;
5536 }
5537
5538
5539 /* Load the vector IT->overlay_strings with overlay strings from IT's
5540 current buffer position, or from CHARPOS if that is > 0. Set
5541 IT->n_overlays to the total number of overlay strings found.
5542
5543 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5544 a time. On entry into load_overlay_strings,
5545 IT->current.overlay_string_index gives the number of overlay
5546 strings that have already been loaded by previous calls to this
5547 function.
5548
5549 IT->add_overlay_start contains an additional overlay start
5550 position to consider for taking overlay strings from, if non-zero.
5551 This position comes into play when the overlay has an `invisible'
5552 property, and both before and after-strings. When we've skipped to
5553 the end of the overlay, because of its `invisible' property, we
5554 nevertheless want its before-string to appear.
5555 IT->add_overlay_start will contain the overlay start position
5556 in this case.
5557
5558 Overlay strings are sorted so that after-string strings come in
5559 front of before-string strings. Within before and after-strings,
5560 strings are sorted by overlay priority. See also function
5561 compare_overlay_entries. */
5562
5563 static void
5564 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5565 {
5566 Lisp_Object overlay, window, str, invisible;
5567 struct Lisp_Overlay *ov;
5568 ptrdiff_t start, end;
5569 ptrdiff_t size = 20;
5570 ptrdiff_t n = 0, i, j;
5571 int invis_p;
5572 struct overlay_entry *entries = alloca (size * sizeof *entries);
5573 USE_SAFE_ALLOCA;
5574
5575 if (charpos <= 0)
5576 charpos = IT_CHARPOS (*it);
5577
5578 /* Append the overlay string STRING of overlay OVERLAY to vector
5579 `entries' which has size `size' and currently contains `n'
5580 elements. AFTER_P non-zero means STRING is an after-string of
5581 OVERLAY. */
5582 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5583 do \
5584 { \
5585 Lisp_Object priority; \
5586 \
5587 if (n == size) \
5588 { \
5589 struct overlay_entry *old = entries; \
5590 SAFE_NALLOCA (entries, 2, size); \
5591 memcpy (entries, old, size * sizeof *entries); \
5592 size *= 2; \
5593 } \
5594 \
5595 entries[n].string = (STRING); \
5596 entries[n].overlay = (OVERLAY); \
5597 priority = Foverlay_get ((OVERLAY), Qpriority); \
5598 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5599 entries[n].after_string_p = (AFTER_P); \
5600 ++n; \
5601 } \
5602 while (0)
5603
5604 /* Process overlay before the overlay center. */
5605 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5606 {
5607 XSETMISC (overlay, ov);
5608 eassert (OVERLAYP (overlay));
5609 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5610 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5611
5612 if (end < charpos)
5613 break;
5614
5615 /* Skip this overlay if it doesn't start or end at IT's current
5616 position. */
5617 if (end != charpos && start != charpos)
5618 continue;
5619
5620 /* Skip this overlay if it doesn't apply to IT->w. */
5621 window = Foverlay_get (overlay, Qwindow);
5622 if (WINDOWP (window) && XWINDOW (window) != it->w)
5623 continue;
5624
5625 /* If the text ``under'' the overlay is invisible, both before-
5626 and after-strings from this overlay are visible; start and
5627 end position are indistinguishable. */
5628 invisible = Foverlay_get (overlay, Qinvisible);
5629 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5630
5631 /* If overlay has a non-empty before-string, record it. */
5632 if ((start == charpos || (end == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 0);
5636
5637 /* If overlay has a non-empty after-string, record it. */
5638 if ((end == charpos || (start == charpos && invis_p))
5639 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5640 && SCHARS (str))
5641 RECORD_OVERLAY_STRING (overlay, str, 1);
5642 }
5643
5644 /* Process overlays after the overlay center. */
5645 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5646 {
5647 XSETMISC (overlay, ov);
5648 eassert (OVERLAYP (overlay));
5649 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5650 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5651
5652 if (start > charpos)
5653 break;
5654
5655 /* Skip this overlay if it doesn't start or end at IT's current
5656 position. */
5657 if (end != charpos && start != charpos)
5658 continue;
5659
5660 /* Skip this overlay if it doesn't apply to IT->w. */
5661 window = Foverlay_get (overlay, Qwindow);
5662 if (WINDOWP (window) && XWINDOW (window) != it->w)
5663 continue;
5664
5665 /* If the text ``under'' the overlay is invisible, it has a zero
5666 dimension, and both before- and after-strings apply. */
5667 invisible = Foverlay_get (overlay, Qinvisible);
5668 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5669
5670 /* If overlay has a non-empty before-string, record it. */
5671 if ((start == charpos || (end == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 0);
5675
5676 /* If overlay has a non-empty after-string, record it. */
5677 if ((end == charpos || (start == charpos && invis_p))
5678 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5679 && SCHARS (str))
5680 RECORD_OVERLAY_STRING (overlay, str, 1);
5681 }
5682
5683 #undef RECORD_OVERLAY_STRING
5684
5685 /* Sort entries. */
5686 if (n > 1)
5687 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5688
5689 /* Record number of overlay strings, and where we computed it. */
5690 it->n_overlay_strings = n;
5691 it->overlay_strings_charpos = charpos;
5692
5693 /* IT->current.overlay_string_index is the number of overlay strings
5694 that have already been consumed by IT. Copy some of the
5695 remaining overlay strings to IT->overlay_strings. */
5696 i = 0;
5697 j = it->current.overlay_string_index;
5698 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5699 {
5700 it->overlay_strings[i] = entries[j].string;
5701 it->string_overlays[i++] = entries[j++].overlay;
5702 }
5703
5704 CHECK_IT (it);
5705 SAFE_FREE ();
5706 }
5707
5708
5709 /* Get the first chunk of overlay strings at IT's current buffer
5710 position, or at CHARPOS if that is > 0. Value is non-zero if at
5711 least one overlay string was found. */
5712
5713 static int
5714 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5715 {
5716 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5717 process. This fills IT->overlay_strings with strings, and sets
5718 IT->n_overlay_strings to the total number of strings to process.
5719 IT->pos.overlay_string_index has to be set temporarily to zero
5720 because load_overlay_strings needs this; it must be set to -1
5721 when no overlay strings are found because a zero value would
5722 indicate a position in the first overlay string. */
5723 it->current.overlay_string_index = 0;
5724 load_overlay_strings (it, charpos);
5725
5726 /* If we found overlay strings, set up IT to deliver display
5727 elements from the first one. Otherwise set up IT to deliver
5728 from current_buffer. */
5729 if (it->n_overlay_strings)
5730 {
5731 /* Make sure we know settings in current_buffer, so that we can
5732 restore meaningful values when we're done with the overlay
5733 strings. */
5734 if (compute_stop_p)
5735 compute_stop_pos (it);
5736 eassert (it->face_id >= 0);
5737
5738 /* Save IT's settings. They are restored after all overlay
5739 strings have been processed. */
5740 eassert (!compute_stop_p || it->sp == 0);
5741
5742 /* When called from handle_stop, there might be an empty display
5743 string loaded. In that case, don't bother saving it. But
5744 don't use this optimization with the bidi iterator, since we
5745 need the corresponding pop_it call to resync the bidi
5746 iterator's position with IT's position, after we are done
5747 with the overlay strings. (The corresponding call to pop_it
5748 in case of an empty display string is in
5749 next_overlay_string.) */
5750 if (!(!it->bidi_p
5751 && STRINGP (it->string) && !SCHARS (it->string)))
5752 push_it (it, NULL);
5753
5754 /* Set up IT to deliver display elements from the first overlay
5755 string. */
5756 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5757 it->string = it->overlay_strings[0];
5758 it->from_overlay = Qnil;
5759 it->stop_charpos = 0;
5760 eassert (STRINGP (it->string));
5761 it->end_charpos = SCHARS (it->string);
5762 it->prev_stop = 0;
5763 it->base_level_stop = 0;
5764 it->multibyte_p = STRING_MULTIBYTE (it->string);
5765 it->method = GET_FROM_STRING;
5766 it->from_disp_prop_p = 0;
5767
5768 /* Force paragraph direction to be that of the parent
5769 buffer. */
5770 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5771 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5772 else
5773 it->paragraph_embedding = L2R;
5774
5775 /* Set up the bidi iterator for this overlay string. */
5776 if (it->bidi_p)
5777 {
5778 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5779
5780 it->bidi_it.string.lstring = it->string;
5781 it->bidi_it.string.s = NULL;
5782 it->bidi_it.string.schars = SCHARS (it->string);
5783 it->bidi_it.string.bufpos = pos;
5784 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5785 it->bidi_it.string.unibyte = !it->multibyte_p;
5786 it->bidi_it.w = it->w;
5787 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5788 }
5789 return 1;
5790 }
5791
5792 it->current.overlay_string_index = -1;
5793 return 0;
5794 }
5795
5796 static int
5797 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5798 {
5799 it->string = Qnil;
5800 it->method = GET_FROM_BUFFER;
5801
5802 (void) get_overlay_strings_1 (it, charpos, 1);
5803
5804 CHECK_IT (it);
5805
5806 /* Value is non-zero if we found at least one overlay string. */
5807 return STRINGP (it->string);
5808 }
5809
5810
5811 \f
5812 /***********************************************************************
5813 Saving and restoring state
5814 ***********************************************************************/
5815
5816 /* Save current settings of IT on IT->stack. Called, for example,
5817 before setting up IT for an overlay string, to be able to restore
5818 IT's settings to what they were after the overlay string has been
5819 processed. If POSITION is non-NULL, it is the position to save on
5820 the stack instead of IT->position. */
5821
5822 static void
5823 push_it (struct it *it, struct text_pos *position)
5824 {
5825 struct iterator_stack_entry *p;
5826
5827 eassert (it->sp < IT_STACK_SIZE);
5828 p = it->stack + it->sp;
5829
5830 p->stop_charpos = it->stop_charpos;
5831 p->prev_stop = it->prev_stop;
5832 p->base_level_stop = it->base_level_stop;
5833 p->cmp_it = it->cmp_it;
5834 eassert (it->face_id >= 0);
5835 p->face_id = it->face_id;
5836 p->string = it->string;
5837 p->method = it->method;
5838 p->from_overlay = it->from_overlay;
5839 switch (p->method)
5840 {
5841 case GET_FROM_IMAGE:
5842 p->u.image.object = it->object;
5843 p->u.image.image_id = it->image_id;
5844 p->u.image.slice = it->slice;
5845 break;
5846 case GET_FROM_STRETCH:
5847 p->u.stretch.object = it->object;
5848 break;
5849 }
5850 p->position = position ? *position : it->position;
5851 p->current = it->current;
5852 p->end_charpos = it->end_charpos;
5853 p->string_nchars = it->string_nchars;
5854 p->area = it->area;
5855 p->multibyte_p = it->multibyte_p;
5856 p->avoid_cursor_p = it->avoid_cursor_p;
5857 p->space_width = it->space_width;
5858 p->font_height = it->font_height;
5859 p->voffset = it->voffset;
5860 p->string_from_display_prop_p = it->string_from_display_prop_p;
5861 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5862 p->display_ellipsis_p = 0;
5863 p->line_wrap = it->line_wrap;
5864 p->bidi_p = it->bidi_p;
5865 p->paragraph_embedding = it->paragraph_embedding;
5866 p->from_disp_prop_p = it->from_disp_prop_p;
5867 ++it->sp;
5868
5869 /* Save the state of the bidi iterator as well. */
5870 if (it->bidi_p)
5871 bidi_push_it (&it->bidi_it);
5872 }
5873
5874 static void
5875 iterate_out_of_display_property (struct it *it)
5876 {
5877 int buffer_p = !STRINGP (it->string);
5878 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5879 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5880
5881 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5882
5883 /* Maybe initialize paragraph direction. If we are at the beginning
5884 of a new paragraph, next_element_from_buffer may not have a
5885 chance to do that. */
5886 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5887 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5888 /* prev_stop can be zero, so check against BEGV as well. */
5889 while (it->bidi_it.charpos >= bob
5890 && it->prev_stop <= it->bidi_it.charpos
5891 && it->bidi_it.charpos < CHARPOS (it->position)
5892 && it->bidi_it.charpos < eob)
5893 bidi_move_to_visually_next (&it->bidi_it);
5894 /* Record the stop_pos we just crossed, for when we cross it
5895 back, maybe. */
5896 if (it->bidi_it.charpos > CHARPOS (it->position))
5897 it->prev_stop = CHARPOS (it->position);
5898 /* If we ended up not where pop_it put us, resync IT's
5899 positional members with the bidi iterator. */
5900 if (it->bidi_it.charpos != CHARPOS (it->position))
5901 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5902 if (buffer_p)
5903 it->current.pos = it->position;
5904 else
5905 it->current.string_pos = it->position;
5906 }
5907
5908 /* Restore IT's settings from IT->stack. Called, for example, when no
5909 more overlay strings must be processed, and we return to delivering
5910 display elements from a buffer, or when the end of a string from a
5911 `display' property is reached and we return to delivering display
5912 elements from an overlay string, or from a buffer. */
5913
5914 static void
5915 pop_it (struct it *it)
5916 {
5917 struct iterator_stack_entry *p;
5918 int from_display_prop = it->from_disp_prop_p;
5919
5920 eassert (it->sp > 0);
5921 --it->sp;
5922 p = it->stack + it->sp;
5923 it->stop_charpos = p->stop_charpos;
5924 it->prev_stop = p->prev_stop;
5925 it->base_level_stop = p->base_level_stop;
5926 it->cmp_it = p->cmp_it;
5927 it->face_id = p->face_id;
5928 it->current = p->current;
5929 it->position = p->position;
5930 it->string = p->string;
5931 it->from_overlay = p->from_overlay;
5932 if (NILP (it->string))
5933 SET_TEXT_POS (it->current.string_pos, -1, -1);
5934 it->method = p->method;
5935 switch (it->method)
5936 {
5937 case GET_FROM_IMAGE:
5938 it->image_id = p->u.image.image_id;
5939 it->object = p->u.image.object;
5940 it->slice = p->u.image.slice;
5941 break;
5942 case GET_FROM_STRETCH:
5943 it->object = p->u.stretch.object;
5944 break;
5945 case GET_FROM_BUFFER:
5946 it->object = it->w->contents;
5947 break;
5948 case GET_FROM_STRING:
5949 it->object = it->string;
5950 break;
5951 case GET_FROM_DISPLAY_VECTOR:
5952 if (it->s)
5953 it->method = GET_FROM_C_STRING;
5954 else if (STRINGP (it->string))
5955 it->method = GET_FROM_STRING;
5956 else
5957 {
5958 it->method = GET_FROM_BUFFER;
5959 it->object = it->w->contents;
5960 }
5961 }
5962 it->end_charpos = p->end_charpos;
5963 it->string_nchars = p->string_nchars;
5964 it->area = p->area;
5965 it->multibyte_p = p->multibyte_p;
5966 it->avoid_cursor_p = p->avoid_cursor_p;
5967 it->space_width = p->space_width;
5968 it->font_height = p->font_height;
5969 it->voffset = p->voffset;
5970 it->string_from_display_prop_p = p->string_from_display_prop_p;
5971 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5972 it->line_wrap = p->line_wrap;
5973 it->bidi_p = p->bidi_p;
5974 it->paragraph_embedding = p->paragraph_embedding;
5975 it->from_disp_prop_p = p->from_disp_prop_p;
5976 if (it->bidi_p)
5977 {
5978 bidi_pop_it (&it->bidi_it);
5979 /* Bidi-iterate until we get out of the portion of text, if any,
5980 covered by a `display' text property or by an overlay with
5981 `display' property. (We cannot just jump there, because the
5982 internal coherency of the bidi iterator state can not be
5983 preserved across such jumps.) We also must determine the
5984 paragraph base direction if the overlay we just processed is
5985 at the beginning of a new paragraph. */
5986 if (from_display_prop
5987 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5988 iterate_out_of_display_property (it);
5989
5990 eassert ((BUFFERP (it->object)
5991 && IT_CHARPOS (*it) == it->bidi_it.charpos
5992 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5993 || (STRINGP (it->object)
5994 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5995 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5996 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5997 }
5998 }
5999
6000
6001 \f
6002 /***********************************************************************
6003 Moving over lines
6004 ***********************************************************************/
6005
6006 /* Set IT's current position to the previous line start. */
6007
6008 static void
6009 back_to_previous_line_start (struct it *it)
6010 {
6011 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6012
6013 DEC_BOTH (cp, bp);
6014 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6015 }
6016
6017
6018 /* Move IT to the next line start.
6019
6020 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6021 we skipped over part of the text (as opposed to moving the iterator
6022 continuously over the text). Otherwise, don't change the value
6023 of *SKIPPED_P.
6024
6025 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6026 iterator on the newline, if it was found.
6027
6028 Newlines may come from buffer text, overlay strings, or strings
6029 displayed via the `display' property. That's the reason we can't
6030 simply use find_newline_no_quit.
6031
6032 Note that this function may not skip over invisible text that is so
6033 because of text properties and immediately follows a newline. If
6034 it would, function reseat_at_next_visible_line_start, when called
6035 from set_iterator_to_next, would effectively make invisible
6036 characters following a newline part of the wrong glyph row, which
6037 leads to wrong cursor motion. */
6038
6039 static int
6040 forward_to_next_line_start (struct it *it, int *skipped_p,
6041 struct bidi_it *bidi_it_prev)
6042 {
6043 ptrdiff_t old_selective;
6044 int newline_found_p, n;
6045 const int MAX_NEWLINE_DISTANCE = 500;
6046
6047 /* If already on a newline, just consume it to avoid unintended
6048 skipping over invisible text below. */
6049 if (it->what == IT_CHARACTER
6050 && it->c == '\n'
6051 && CHARPOS (it->position) == IT_CHARPOS (*it))
6052 {
6053 if (it->bidi_p && bidi_it_prev)
6054 *bidi_it_prev = it->bidi_it;
6055 set_iterator_to_next (it, 0);
6056 it->c = 0;
6057 return 1;
6058 }
6059
6060 /* Don't handle selective display in the following. It's (a)
6061 unnecessary because it's done by the caller, and (b) leads to an
6062 infinite recursion because next_element_from_ellipsis indirectly
6063 calls this function. */
6064 old_selective = it->selective;
6065 it->selective = 0;
6066
6067 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6068 from buffer text. */
6069 for (n = newline_found_p = 0;
6070 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6071 n += STRINGP (it->string) ? 0 : 1)
6072 {
6073 if (!get_next_display_element (it))
6074 return 0;
6075 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6076 if (newline_found_p && it->bidi_p && bidi_it_prev)
6077 *bidi_it_prev = it->bidi_it;
6078 set_iterator_to_next (it, 0);
6079 }
6080
6081 /* If we didn't find a newline near enough, see if we can use a
6082 short-cut. */
6083 if (!newline_found_p)
6084 {
6085 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6086 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6087 1, &bytepos);
6088 Lisp_Object pos;
6089
6090 eassert (!STRINGP (it->string));
6091
6092 /* If there isn't any `display' property in sight, and no
6093 overlays, we can just use the position of the newline in
6094 buffer text. */
6095 if (it->stop_charpos >= limit
6096 || ((pos = Fnext_single_property_change (make_number (start),
6097 Qdisplay, Qnil,
6098 make_number (limit)),
6099 NILP (pos))
6100 && next_overlay_change (start) == ZV))
6101 {
6102 if (!it->bidi_p)
6103 {
6104 IT_CHARPOS (*it) = limit;
6105 IT_BYTEPOS (*it) = bytepos;
6106 }
6107 else
6108 {
6109 struct bidi_it bprev;
6110
6111 /* Help bidi.c avoid expensive searches for display
6112 properties and overlays, by telling it that there are
6113 none up to `limit'. */
6114 if (it->bidi_it.disp_pos < limit)
6115 {
6116 it->bidi_it.disp_pos = limit;
6117 it->bidi_it.disp_prop = 0;
6118 }
6119 do {
6120 bprev = it->bidi_it;
6121 bidi_move_to_visually_next (&it->bidi_it);
6122 } while (it->bidi_it.charpos != limit);
6123 IT_CHARPOS (*it) = limit;
6124 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6125 if (bidi_it_prev)
6126 *bidi_it_prev = bprev;
6127 }
6128 *skipped_p = newline_found_p = true;
6129 }
6130 else
6131 {
6132 while (get_next_display_element (it)
6133 && !newline_found_p)
6134 {
6135 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6136 if (newline_found_p && it->bidi_p && bidi_it_prev)
6137 *bidi_it_prev = it->bidi_it;
6138 set_iterator_to_next (it, 0);
6139 }
6140 }
6141 }
6142
6143 it->selective = old_selective;
6144 return newline_found_p;
6145 }
6146
6147
6148 /* Set IT's current position to the previous visible line start. Skip
6149 invisible text that is so either due to text properties or due to
6150 selective display. Caution: this does not change IT->current_x and
6151 IT->hpos. */
6152
6153 static void
6154 back_to_previous_visible_line_start (struct it *it)
6155 {
6156 while (IT_CHARPOS (*it) > BEGV)
6157 {
6158 back_to_previous_line_start (it);
6159
6160 if (IT_CHARPOS (*it) <= BEGV)
6161 break;
6162
6163 /* If selective > 0, then lines indented more than its value are
6164 invisible. */
6165 if (it->selective > 0
6166 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6167 it->selective))
6168 continue;
6169
6170 /* Check the newline before point for invisibility. */
6171 {
6172 Lisp_Object prop;
6173 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6174 Qinvisible, it->window);
6175 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6176 continue;
6177 }
6178
6179 if (IT_CHARPOS (*it) <= BEGV)
6180 break;
6181
6182 {
6183 struct it it2;
6184 void *it2data = NULL;
6185 ptrdiff_t pos;
6186 ptrdiff_t beg, end;
6187 Lisp_Object val, overlay;
6188
6189 SAVE_IT (it2, *it, it2data);
6190
6191 /* If newline is part of a composition, continue from start of composition */
6192 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6193 && beg < IT_CHARPOS (*it))
6194 goto replaced;
6195
6196 /* If newline is replaced by a display property, find start of overlay
6197 or interval and continue search from that point. */
6198 pos = --IT_CHARPOS (it2);
6199 --IT_BYTEPOS (it2);
6200 it2.sp = 0;
6201 bidi_unshelve_cache (NULL, 0);
6202 it2.string_from_display_prop_p = 0;
6203 it2.from_disp_prop_p = 0;
6204 if (handle_display_prop (&it2) == HANDLED_RETURN
6205 && !NILP (val = get_char_property_and_overlay
6206 (make_number (pos), Qdisplay, Qnil, &overlay))
6207 && (OVERLAYP (overlay)
6208 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6209 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6210 {
6211 RESTORE_IT (it, it, it2data);
6212 goto replaced;
6213 }
6214
6215 /* Newline is not replaced by anything -- so we are done. */
6216 RESTORE_IT (it, it, it2data);
6217 break;
6218
6219 replaced:
6220 if (beg < BEGV)
6221 beg = BEGV;
6222 IT_CHARPOS (*it) = beg;
6223 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6224 }
6225 }
6226
6227 it->continuation_lines_width = 0;
6228
6229 eassert (IT_CHARPOS (*it) >= BEGV);
6230 eassert (IT_CHARPOS (*it) == BEGV
6231 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6232 CHECK_IT (it);
6233 }
6234
6235
6236 /* Reseat iterator IT at the previous visible line start. Skip
6237 invisible text that is so either due to text properties or due to
6238 selective display. At the end, update IT's overlay information,
6239 face information etc. */
6240
6241 void
6242 reseat_at_previous_visible_line_start (struct it *it)
6243 {
6244 back_to_previous_visible_line_start (it);
6245 reseat (it, it->current.pos, 1);
6246 CHECK_IT (it);
6247 }
6248
6249
6250 /* Reseat iterator IT on the next visible line start in the current
6251 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6252 preceding the line start. Skip over invisible text that is so
6253 because of selective display. Compute faces, overlays etc at the
6254 new position. Note that this function does not skip over text that
6255 is invisible because of text properties. */
6256
6257 static void
6258 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6259 {
6260 int newline_found_p, skipped_p = 0;
6261 struct bidi_it bidi_it_prev;
6262
6263 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6264
6265 /* Skip over lines that are invisible because they are indented
6266 more than the value of IT->selective. */
6267 if (it->selective > 0)
6268 while (IT_CHARPOS (*it) < ZV
6269 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6270 it->selective))
6271 {
6272 eassert (IT_BYTEPOS (*it) == BEGV
6273 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6274 newline_found_p =
6275 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6276 }
6277
6278 /* Position on the newline if that's what's requested. */
6279 if (on_newline_p && newline_found_p)
6280 {
6281 if (STRINGP (it->string))
6282 {
6283 if (IT_STRING_CHARPOS (*it) > 0)
6284 {
6285 if (!it->bidi_p)
6286 {
6287 --IT_STRING_CHARPOS (*it);
6288 --IT_STRING_BYTEPOS (*it);
6289 }
6290 else
6291 {
6292 /* We need to restore the bidi iterator to the state
6293 it had on the newline, and resync the IT's
6294 position with that. */
6295 it->bidi_it = bidi_it_prev;
6296 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6297 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6298 }
6299 }
6300 }
6301 else if (IT_CHARPOS (*it) > BEGV)
6302 {
6303 if (!it->bidi_p)
6304 {
6305 --IT_CHARPOS (*it);
6306 --IT_BYTEPOS (*it);
6307 }
6308 else
6309 {
6310 /* We need to restore the bidi iterator to the state it
6311 had on the newline and resync IT with that. */
6312 it->bidi_it = bidi_it_prev;
6313 IT_CHARPOS (*it) = it->bidi_it.charpos;
6314 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6315 }
6316 reseat (it, it->current.pos, 0);
6317 }
6318 }
6319 else if (skipped_p)
6320 reseat (it, it->current.pos, 0);
6321
6322 CHECK_IT (it);
6323 }
6324
6325
6326 \f
6327 /***********************************************************************
6328 Changing an iterator's position
6329 ***********************************************************************/
6330
6331 /* Change IT's current position to POS in current_buffer. If FORCE_P
6332 is non-zero, always check for text properties at the new position.
6333 Otherwise, text properties are only looked up if POS >=
6334 IT->check_charpos of a property. */
6335
6336 static void
6337 reseat (struct it *it, struct text_pos pos, int force_p)
6338 {
6339 ptrdiff_t original_pos = IT_CHARPOS (*it);
6340
6341 reseat_1 (it, pos, 0);
6342
6343 /* Determine where to check text properties. Avoid doing it
6344 where possible because text property lookup is very expensive. */
6345 if (force_p
6346 || CHARPOS (pos) > it->stop_charpos
6347 || CHARPOS (pos) < original_pos)
6348 {
6349 if (it->bidi_p)
6350 {
6351 /* For bidi iteration, we need to prime prev_stop and
6352 base_level_stop with our best estimations. */
6353 /* Implementation note: Of course, POS is not necessarily a
6354 stop position, so assigning prev_pos to it is a lie; we
6355 should have called compute_stop_backwards. However, if
6356 the current buffer does not include any R2L characters,
6357 that call would be a waste of cycles, because the
6358 iterator will never move back, and thus never cross this
6359 "fake" stop position. So we delay that backward search
6360 until the time we really need it, in next_element_from_buffer. */
6361 if (CHARPOS (pos) != it->prev_stop)
6362 it->prev_stop = CHARPOS (pos);
6363 if (CHARPOS (pos) < it->base_level_stop)
6364 it->base_level_stop = 0; /* meaning it's unknown */
6365 handle_stop (it);
6366 }
6367 else
6368 {
6369 handle_stop (it);
6370 it->prev_stop = it->base_level_stop = 0;
6371 }
6372
6373 }
6374
6375 CHECK_IT (it);
6376 }
6377
6378
6379 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6380 IT->stop_pos to POS, also. */
6381
6382 static void
6383 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6384 {
6385 /* Don't call this function when scanning a C string. */
6386 eassert (it->s == NULL);
6387
6388 /* POS must be a reasonable value. */
6389 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6390
6391 it->current.pos = it->position = pos;
6392 it->end_charpos = ZV;
6393 it->dpvec = NULL;
6394 it->current.dpvec_index = -1;
6395 it->current.overlay_string_index = -1;
6396 IT_STRING_CHARPOS (*it) = -1;
6397 IT_STRING_BYTEPOS (*it) = -1;
6398 it->string = Qnil;
6399 it->method = GET_FROM_BUFFER;
6400 it->object = it->w->contents;
6401 it->area = TEXT_AREA;
6402 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6403 it->sp = 0;
6404 it->string_from_display_prop_p = 0;
6405 it->string_from_prefix_prop_p = 0;
6406
6407 it->from_disp_prop_p = 0;
6408 it->face_before_selective_p = 0;
6409 if (it->bidi_p)
6410 {
6411 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6412 &it->bidi_it);
6413 bidi_unshelve_cache (NULL, 0);
6414 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6415 it->bidi_it.string.s = NULL;
6416 it->bidi_it.string.lstring = Qnil;
6417 it->bidi_it.string.bufpos = 0;
6418 it->bidi_it.string.from_disp_str = 0;
6419 it->bidi_it.string.unibyte = 0;
6420 it->bidi_it.w = it->w;
6421 }
6422
6423 if (set_stop_p)
6424 {
6425 it->stop_charpos = CHARPOS (pos);
6426 it->base_level_stop = CHARPOS (pos);
6427 }
6428 /* This make the information stored in it->cmp_it invalidate. */
6429 it->cmp_it.id = -1;
6430 }
6431
6432
6433 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6434 If S is non-null, it is a C string to iterate over. Otherwise,
6435 STRING gives a Lisp string to iterate over.
6436
6437 If PRECISION > 0, don't return more then PRECISION number of
6438 characters from the string.
6439
6440 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6441 characters have been returned. FIELD_WIDTH < 0 means an infinite
6442 field width.
6443
6444 MULTIBYTE = 0 means disable processing of multibyte characters,
6445 MULTIBYTE > 0 means enable it,
6446 MULTIBYTE < 0 means use IT->multibyte_p.
6447
6448 IT must be initialized via a prior call to init_iterator before
6449 calling this function. */
6450
6451 static void
6452 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6453 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6454 int multibyte)
6455 {
6456 /* No text property checks performed by default, but see below. */
6457 it->stop_charpos = -1;
6458
6459 /* Set iterator position and end position. */
6460 memset (&it->current, 0, sizeof it->current);
6461 it->current.overlay_string_index = -1;
6462 it->current.dpvec_index = -1;
6463 eassert (charpos >= 0);
6464
6465 /* If STRING is specified, use its multibyteness, otherwise use the
6466 setting of MULTIBYTE, if specified. */
6467 if (multibyte >= 0)
6468 it->multibyte_p = multibyte > 0;
6469
6470 /* Bidirectional reordering of strings is controlled by the default
6471 value of bidi-display-reordering. Don't try to reorder while
6472 loading loadup.el, as the necessary character property tables are
6473 not yet available. */
6474 it->bidi_p =
6475 NILP (Vpurify_flag)
6476 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6477
6478 if (s == NULL)
6479 {
6480 eassert (STRINGP (string));
6481 it->string = string;
6482 it->s = NULL;
6483 it->end_charpos = it->string_nchars = SCHARS (string);
6484 it->method = GET_FROM_STRING;
6485 it->current.string_pos = string_pos (charpos, string);
6486
6487 if (it->bidi_p)
6488 {
6489 it->bidi_it.string.lstring = string;
6490 it->bidi_it.string.s = NULL;
6491 it->bidi_it.string.schars = it->end_charpos;
6492 it->bidi_it.string.bufpos = 0;
6493 it->bidi_it.string.from_disp_str = 0;
6494 it->bidi_it.string.unibyte = !it->multibyte_p;
6495 it->bidi_it.w = it->w;
6496 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6497 FRAME_WINDOW_P (it->f), &it->bidi_it);
6498 }
6499 }
6500 else
6501 {
6502 it->s = (const unsigned char *) s;
6503 it->string = Qnil;
6504
6505 /* Note that we use IT->current.pos, not it->current.string_pos,
6506 for displaying C strings. */
6507 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6508 if (it->multibyte_p)
6509 {
6510 it->current.pos = c_string_pos (charpos, s, 1);
6511 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6512 }
6513 else
6514 {
6515 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6516 it->end_charpos = it->string_nchars = strlen (s);
6517 }
6518
6519 if (it->bidi_p)
6520 {
6521 it->bidi_it.string.lstring = Qnil;
6522 it->bidi_it.string.s = (const unsigned char *) s;
6523 it->bidi_it.string.schars = it->end_charpos;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = !it->multibyte_p;
6527 it->bidi_it.w = it->w;
6528 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6529 &it->bidi_it);
6530 }
6531 it->method = GET_FROM_C_STRING;
6532 }
6533
6534 /* PRECISION > 0 means don't return more than PRECISION characters
6535 from the string. */
6536 if (precision > 0 && it->end_charpos - charpos > precision)
6537 {
6538 it->end_charpos = it->string_nchars = charpos + precision;
6539 if (it->bidi_p)
6540 it->bidi_it.string.schars = it->end_charpos;
6541 }
6542
6543 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6544 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6545 FIELD_WIDTH < 0 means infinite field width. This is useful for
6546 padding with `-' at the end of a mode line. */
6547 if (field_width < 0)
6548 field_width = INFINITY;
6549 /* Implementation note: We deliberately don't enlarge
6550 it->bidi_it.string.schars here to fit it->end_charpos, because
6551 the bidi iterator cannot produce characters out of thin air. */
6552 if (field_width > it->end_charpos - charpos)
6553 it->end_charpos = charpos + field_width;
6554
6555 /* Use the standard display table for displaying strings. */
6556 if (DISP_TABLE_P (Vstandard_display_table))
6557 it->dp = XCHAR_TABLE (Vstandard_display_table);
6558
6559 it->stop_charpos = charpos;
6560 it->prev_stop = charpos;
6561 it->base_level_stop = 0;
6562 if (it->bidi_p)
6563 {
6564 it->bidi_it.first_elt = 1;
6565 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6566 it->bidi_it.disp_pos = -1;
6567 }
6568 if (s == NULL && it->multibyte_p)
6569 {
6570 ptrdiff_t endpos = SCHARS (it->string);
6571 if (endpos > it->end_charpos)
6572 endpos = it->end_charpos;
6573 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6574 it->string);
6575 }
6576 CHECK_IT (it);
6577 }
6578
6579
6580 \f
6581 /***********************************************************************
6582 Iteration
6583 ***********************************************************************/
6584
6585 /* Map enum it_method value to corresponding next_element_from_* function. */
6586
6587 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6588 {
6589 next_element_from_buffer,
6590 next_element_from_display_vector,
6591 next_element_from_string,
6592 next_element_from_c_string,
6593 next_element_from_image,
6594 next_element_from_stretch
6595 };
6596
6597 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6598
6599
6600 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6601 (possibly with the following characters). */
6602
6603 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6604 ((IT)->cmp_it.id >= 0 \
6605 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6606 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6607 END_CHARPOS, (IT)->w, \
6608 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6609 (IT)->string)))
6610
6611
6612 /* Lookup the char-table Vglyphless_char_display for character C (-1
6613 if we want information for no-font case), and return the display
6614 method symbol. By side-effect, update it->what and
6615 it->glyphless_method. This function is called from
6616 get_next_display_element for each character element, and from
6617 x_produce_glyphs when no suitable font was found. */
6618
6619 Lisp_Object
6620 lookup_glyphless_char_display (int c, struct it *it)
6621 {
6622 Lisp_Object glyphless_method = Qnil;
6623
6624 if (CHAR_TABLE_P (Vglyphless_char_display)
6625 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6626 {
6627 if (c >= 0)
6628 {
6629 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6630 if (CONSP (glyphless_method))
6631 glyphless_method = FRAME_WINDOW_P (it->f)
6632 ? XCAR (glyphless_method)
6633 : XCDR (glyphless_method);
6634 }
6635 else
6636 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6637 }
6638
6639 retry:
6640 if (NILP (glyphless_method))
6641 {
6642 if (c >= 0)
6643 /* The default is to display the character by a proper font. */
6644 return Qnil;
6645 /* The default for the no-font case is to display an empty box. */
6646 glyphless_method = Qempty_box;
6647 }
6648 if (EQ (glyphless_method, Qzero_width))
6649 {
6650 if (c >= 0)
6651 return glyphless_method;
6652 /* This method can't be used for the no-font case. */
6653 glyphless_method = Qempty_box;
6654 }
6655 if (EQ (glyphless_method, Qthin_space))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6657 else if (EQ (glyphless_method, Qempty_box))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6659 else if (EQ (glyphless_method, Qhex_code))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6661 else if (STRINGP (glyphless_method))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6663 else
6664 {
6665 /* Invalid value. We use the default method. */
6666 glyphless_method = Qnil;
6667 goto retry;
6668 }
6669 it->what = IT_GLYPHLESS;
6670 return glyphless_method;
6671 }
6672
6673 /* Merge escape glyph face and cache the result. */
6674
6675 static struct frame *last_escape_glyph_frame = NULL;
6676 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6677 static int last_escape_glyph_merged_face_id = 0;
6678
6679 static int
6680 merge_escape_glyph_face (struct it *it)
6681 {
6682 int face_id;
6683
6684 if (it->f == last_escape_glyph_frame
6685 && it->face_id == last_escape_glyph_face_id)
6686 face_id = last_escape_glyph_merged_face_id;
6687 else
6688 {
6689 /* Merge the `escape-glyph' face into the current face. */
6690 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6691 last_escape_glyph_frame = it->f;
6692 last_escape_glyph_face_id = it->face_id;
6693 last_escape_glyph_merged_face_id = face_id;
6694 }
6695 return face_id;
6696 }
6697
6698 /* Likewise for glyphless glyph face. */
6699
6700 static struct frame *last_glyphless_glyph_frame = NULL;
6701 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6702 static int last_glyphless_glyph_merged_face_id = 0;
6703
6704 int
6705 merge_glyphless_glyph_face (struct it *it)
6706 {
6707 int face_id;
6708
6709 if (it->f == last_glyphless_glyph_frame
6710 && it->face_id == last_glyphless_glyph_face_id)
6711 face_id = last_glyphless_glyph_merged_face_id;
6712 else
6713 {
6714 /* Merge the `glyphless-char' face into the current face. */
6715 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6716 last_glyphless_glyph_frame = it->f;
6717 last_glyphless_glyph_face_id = it->face_id;
6718 last_glyphless_glyph_merged_face_id = face_id;
6719 }
6720 return face_id;
6721 }
6722
6723 /* Load IT's display element fields with information about the next
6724 display element from the current position of IT. Value is zero if
6725 end of buffer (or C string) is reached. */
6726
6727 static int
6728 get_next_display_element (struct it *it)
6729 {
6730 /* Non-zero means that we found a display element. Zero means that
6731 we hit the end of what we iterate over. Performance note: the
6732 function pointer `method' used here turns out to be faster than
6733 using a sequence of if-statements. */
6734 int success_p;
6735
6736 get_next:
6737 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6738
6739 if (it->what == IT_CHARACTER)
6740 {
6741 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6742 and only if (a) the resolved directionality of that character
6743 is R..." */
6744 /* FIXME: Do we need an exception for characters from display
6745 tables? */
6746 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6747 it->c = bidi_mirror_char (it->c);
6748 /* Map via display table or translate control characters.
6749 IT->c, IT->len etc. have been set to the next character by
6750 the function call above. If we have a display table, and it
6751 contains an entry for IT->c, translate it. Don't do this if
6752 IT->c itself comes from a display table, otherwise we could
6753 end up in an infinite recursion. (An alternative could be to
6754 count the recursion depth of this function and signal an
6755 error when a certain maximum depth is reached.) Is it worth
6756 it? */
6757 if (success_p && it->dpvec == NULL)
6758 {
6759 Lisp_Object dv;
6760 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6761 int nonascii_space_p = 0;
6762 int nonascii_hyphen_p = 0;
6763 int c = it->c; /* This is the character to display. */
6764
6765 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6766 {
6767 eassert (SINGLE_BYTE_CHAR_P (c));
6768 if (unibyte_display_via_language_environment)
6769 {
6770 c = DECODE_CHAR (unibyte, c);
6771 if (c < 0)
6772 c = BYTE8_TO_CHAR (it->c);
6773 }
6774 else
6775 c = BYTE8_TO_CHAR (it->c);
6776 }
6777
6778 if (it->dp
6779 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6780 VECTORP (dv)))
6781 {
6782 struct Lisp_Vector *v = XVECTOR (dv);
6783
6784 /* Return the first character from the display table
6785 entry, if not empty. If empty, don't display the
6786 current character. */
6787 if (v->header.size)
6788 {
6789 it->dpvec_char_len = it->len;
6790 it->dpvec = v->contents;
6791 it->dpend = v->contents + v->header.size;
6792 it->current.dpvec_index = 0;
6793 it->dpvec_face_id = -1;
6794 it->saved_face_id = it->face_id;
6795 it->method = GET_FROM_DISPLAY_VECTOR;
6796 it->ellipsis_p = 0;
6797 }
6798 else
6799 {
6800 set_iterator_to_next (it, 0);
6801 }
6802 goto get_next;
6803 }
6804
6805 if (! NILP (lookup_glyphless_char_display (c, it)))
6806 {
6807 if (it->what == IT_GLYPHLESS)
6808 goto done;
6809 /* Don't display this character. */
6810 set_iterator_to_next (it, 0);
6811 goto get_next;
6812 }
6813
6814 /* If `nobreak-char-display' is non-nil, we display
6815 non-ASCII spaces and hyphens specially. */
6816 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6817 {
6818 if (c == 0xA0)
6819 nonascii_space_p = true;
6820 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6821 nonascii_hyphen_p = true;
6822 }
6823
6824 /* Translate control characters into `\003' or `^C' form.
6825 Control characters coming from a display table entry are
6826 currently not translated because we use IT->dpvec to hold
6827 the translation. This could easily be changed but I
6828 don't believe that it is worth doing.
6829
6830 The characters handled by `nobreak-char-display' must be
6831 translated too.
6832
6833 Non-printable characters and raw-byte characters are also
6834 translated to octal form. */
6835 if (((c < ' ' || c == 127) /* ASCII control chars. */
6836 ? (it->area != TEXT_AREA
6837 /* In mode line, treat \n, \t like other crl chars. */
6838 || (c != '\t'
6839 && it->glyph_row
6840 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6841 || (c != '\n' && c != '\t'))
6842 : (nonascii_space_p
6843 || nonascii_hyphen_p
6844 || CHAR_BYTE8_P (c)
6845 || ! CHAR_PRINTABLE_P (c))))
6846 {
6847 /* C is a control character, non-ASCII space/hyphen,
6848 raw-byte, or a non-printable character which must be
6849 displayed either as '\003' or as `^C' where the '\\'
6850 and '^' can be defined in the display table. Fill
6851 IT->ctl_chars with glyphs for what we have to
6852 display. Then, set IT->dpvec to these glyphs. */
6853 Lisp_Object gc;
6854 int ctl_len;
6855 int face_id;
6856 int lface_id = 0;
6857 int escape_glyph;
6858
6859 /* Handle control characters with ^. */
6860
6861 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6862 {
6863 int g;
6864
6865 g = '^'; /* default glyph for Control */
6866 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6867 if (it->dp
6868 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6869 {
6870 g = GLYPH_CODE_CHAR (gc);
6871 lface_id = GLYPH_CODE_FACE (gc);
6872 }
6873
6874 face_id = (lface_id
6875 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6876 : merge_escape_glyph_face (it));
6877
6878 XSETINT (it->ctl_chars[0], g);
6879 XSETINT (it->ctl_chars[1], c ^ 0100);
6880 ctl_len = 2;
6881 goto display_control;
6882 }
6883
6884 /* Handle non-ascii space in the mode where it only gets
6885 highlighting. */
6886
6887 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6888 {
6889 /* Merge `nobreak-space' into the current face. */
6890 face_id = merge_faces (it->f, Qnobreak_space, 0,
6891 it->face_id);
6892 XSETINT (it->ctl_chars[0], ' ');
6893 ctl_len = 1;
6894 goto display_control;
6895 }
6896
6897 /* Handle sequences that start with the "escape glyph". */
6898
6899 /* the default escape glyph is \. */
6900 escape_glyph = '\\';
6901
6902 if (it->dp
6903 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6904 {
6905 escape_glyph = GLYPH_CODE_CHAR (gc);
6906 lface_id = GLYPH_CODE_FACE (gc);
6907 }
6908
6909 face_id = (lface_id
6910 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6911 : merge_escape_glyph_face (it));
6912
6913 /* Draw non-ASCII hyphen with just highlighting: */
6914
6915 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6916 {
6917 XSETINT (it->ctl_chars[0], '-');
6918 ctl_len = 1;
6919 goto display_control;
6920 }
6921
6922 /* Draw non-ASCII space/hyphen with escape glyph: */
6923
6924 if (nonascii_space_p || nonascii_hyphen_p)
6925 {
6926 XSETINT (it->ctl_chars[0], escape_glyph);
6927 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6928 ctl_len = 2;
6929 goto display_control;
6930 }
6931
6932 {
6933 char str[10];
6934 int len, i;
6935
6936 if (CHAR_BYTE8_P (c))
6937 /* Display \200 instead of \17777600. */
6938 c = CHAR_TO_BYTE8 (c);
6939 len = sprintf (str, "%03o", c);
6940
6941 XSETINT (it->ctl_chars[0], escape_glyph);
6942 for (i = 0; i < len; i++)
6943 XSETINT (it->ctl_chars[i + 1], str[i]);
6944 ctl_len = len + 1;
6945 }
6946
6947 display_control:
6948 /* Set up IT->dpvec and return first character from it. */
6949 it->dpvec_char_len = it->len;
6950 it->dpvec = it->ctl_chars;
6951 it->dpend = it->dpvec + ctl_len;
6952 it->current.dpvec_index = 0;
6953 it->dpvec_face_id = face_id;
6954 it->saved_face_id = it->face_id;
6955 it->method = GET_FROM_DISPLAY_VECTOR;
6956 it->ellipsis_p = 0;
6957 goto get_next;
6958 }
6959 it->char_to_display = c;
6960 }
6961 else if (success_p)
6962 {
6963 it->char_to_display = it->c;
6964 }
6965 }
6966
6967 #ifdef HAVE_WINDOW_SYSTEM
6968 /* Adjust face id for a multibyte character. There are no multibyte
6969 character in unibyte text. */
6970 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6971 && it->multibyte_p
6972 && success_p
6973 && FRAME_WINDOW_P (it->f))
6974 {
6975 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6976
6977 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6978 {
6979 /* Automatic composition with glyph-string. */
6980 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6981
6982 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6983 }
6984 else
6985 {
6986 ptrdiff_t pos = (it->s ? -1
6987 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6988 : IT_CHARPOS (*it));
6989 int c;
6990
6991 if (it->what == IT_CHARACTER)
6992 c = it->char_to_display;
6993 else
6994 {
6995 struct composition *cmp = composition_table[it->cmp_it.id];
6996 int i;
6997
6998 c = ' ';
6999 for (i = 0; i < cmp->glyph_len; i++)
7000 /* TAB in a composition means display glyphs with
7001 padding space on the left or right. */
7002 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7003 break;
7004 }
7005 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7006 }
7007 }
7008 #endif /* HAVE_WINDOW_SYSTEM */
7009
7010 done:
7011 /* Is this character the last one of a run of characters with
7012 box? If yes, set IT->end_of_box_run_p to 1. */
7013 if (it->face_box_p
7014 && it->s == NULL)
7015 {
7016 if (it->method == GET_FROM_STRING && it->sp)
7017 {
7018 int face_id = underlying_face_id (it);
7019 struct face *face = FACE_FROM_ID (it->f, face_id);
7020
7021 if (face)
7022 {
7023 if (face->box == FACE_NO_BOX)
7024 {
7025 /* If the box comes from face properties in a
7026 display string, check faces in that string. */
7027 int string_face_id = face_after_it_pos (it);
7028 it->end_of_box_run_p
7029 = (FACE_FROM_ID (it->f, string_face_id)->box
7030 == FACE_NO_BOX);
7031 }
7032 /* Otherwise, the box comes from the underlying face.
7033 If this is the last string character displayed, check
7034 the next buffer location. */
7035 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7036 && (it->current.overlay_string_index
7037 == it->n_overlay_strings - 1))
7038 {
7039 ptrdiff_t ignore;
7040 int next_face_id;
7041 struct text_pos pos = it->current.pos;
7042 INC_TEXT_POS (pos, it->multibyte_p);
7043
7044 next_face_id = face_at_buffer_position
7045 (it->w, CHARPOS (pos), &ignore,
7046 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7047 -1);
7048 it->end_of_box_run_p
7049 = (FACE_FROM_ID (it->f, next_face_id)->box
7050 == FACE_NO_BOX);
7051 }
7052 }
7053 }
7054 /* next_element_from_display_vector sets this flag according to
7055 faces of the display vector glyphs, see there. */
7056 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7057 {
7058 int face_id = face_after_it_pos (it);
7059 it->end_of_box_run_p
7060 = (face_id != it->face_id
7061 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7062 }
7063 }
7064 /* If we reached the end of the object we've been iterating (e.g., a
7065 display string or an overlay string), and there's something on
7066 IT->stack, proceed with what's on the stack. It doesn't make
7067 sense to return zero if there's unprocessed stuff on the stack,
7068 because otherwise that stuff will never be displayed. */
7069 if (!success_p && it->sp > 0)
7070 {
7071 set_iterator_to_next (it, 0);
7072 success_p = get_next_display_element (it);
7073 }
7074
7075 /* Value is 0 if end of buffer or string reached. */
7076 return success_p;
7077 }
7078
7079
7080 /* Move IT to the next display element.
7081
7082 RESEAT_P non-zero means if called on a newline in buffer text,
7083 skip to the next visible line start.
7084
7085 Functions get_next_display_element and set_iterator_to_next are
7086 separate because I find this arrangement easier to handle than a
7087 get_next_display_element function that also increments IT's
7088 position. The way it is we can first look at an iterator's current
7089 display element, decide whether it fits on a line, and if it does,
7090 increment the iterator position. The other way around we probably
7091 would either need a flag indicating whether the iterator has to be
7092 incremented the next time, or we would have to implement a
7093 decrement position function which would not be easy to write. */
7094
7095 void
7096 set_iterator_to_next (struct it *it, int reseat_p)
7097 {
7098 /* Reset flags indicating start and end of a sequence of characters
7099 with box. Reset them at the start of this function because
7100 moving the iterator to a new position might set them. */
7101 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7102
7103 switch (it->method)
7104 {
7105 case GET_FROM_BUFFER:
7106 /* The current display element of IT is a character from
7107 current_buffer. Advance in the buffer, and maybe skip over
7108 invisible lines that are so because of selective display. */
7109 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7110 reseat_at_next_visible_line_start (it, 0);
7111 else if (it->cmp_it.id >= 0)
7112 {
7113 /* We are currently getting glyphs from a composition. */
7114 int i;
7115
7116 if (! it->bidi_p)
7117 {
7118 IT_CHARPOS (*it) += it->cmp_it.nchars;
7119 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7120 if (it->cmp_it.to < it->cmp_it.nglyphs)
7121 {
7122 it->cmp_it.from = it->cmp_it.to;
7123 }
7124 else
7125 {
7126 it->cmp_it.id = -1;
7127 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7128 IT_BYTEPOS (*it),
7129 it->end_charpos, Qnil);
7130 }
7131 }
7132 else if (! it->cmp_it.reversed_p)
7133 {
7134 /* Composition created while scanning forward. */
7135 /* Update IT's char/byte positions to point to the first
7136 character of the next grapheme cluster, or to the
7137 character visually after the current composition. */
7138 for (i = 0; i < it->cmp_it.nchars; i++)
7139 bidi_move_to_visually_next (&it->bidi_it);
7140 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7141 IT_CHARPOS (*it) = it->bidi_it.charpos;
7142
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7144 {
7145 /* Proceed to the next grapheme cluster. */
7146 it->cmp_it.from = it->cmp_it.to;
7147 }
7148 else
7149 {
7150 /* No more grapheme clusters in this composition.
7151 Find the next stop position. */
7152 ptrdiff_t stop = it->end_charpos;
7153 if (it->bidi_it.scan_dir < 0)
7154 /* Now we are scanning backward and don't know
7155 where to stop. */
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7158 IT_BYTEPOS (*it), stop, Qnil);
7159 }
7160 }
7161 else
7162 {
7163 /* Composition created while scanning backward. */
7164 /* Update IT's char/byte positions to point to the last
7165 character of the previous grapheme cluster, or the
7166 character visually after the current composition. */
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7172 {
7173 /* Proceed to the previous grapheme cluster. */
7174 it->cmp_it.to = it->cmp_it.from;
7175 }
7176 else
7177 {
7178 /* No more grapheme clusters in this composition.
7179 Find the next stop position. */
7180 ptrdiff_t stop = it->end_charpos;
7181 if (it->bidi_it.scan_dir < 0)
7182 /* Now we are scanning backward and don't know
7183 where to stop. */
7184 stop = -1;
7185 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7186 IT_BYTEPOS (*it), stop, Qnil);
7187 }
7188 }
7189 }
7190 else
7191 {
7192 eassert (it->len != 0);
7193
7194 if (!it->bidi_p)
7195 {
7196 IT_BYTEPOS (*it) += it->len;
7197 IT_CHARPOS (*it) += 1;
7198 }
7199 else
7200 {
7201 int prev_scan_dir = it->bidi_it.scan_dir;
7202 /* If this is a new paragraph, determine its base
7203 direction (a.k.a. its base embedding level). */
7204 if (it->bidi_it.new_paragraph)
7205 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7206 bidi_move_to_visually_next (&it->bidi_it);
7207 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7208 IT_CHARPOS (*it) = it->bidi_it.charpos;
7209 if (prev_scan_dir != it->bidi_it.scan_dir)
7210 {
7211 /* As the scan direction was changed, we must
7212 re-compute the stop position for composition. */
7213 ptrdiff_t stop = it->end_charpos;
7214 if (it->bidi_it.scan_dir < 0)
7215 stop = -1;
7216 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7217 IT_BYTEPOS (*it), stop, Qnil);
7218 }
7219 }
7220 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7221 }
7222 break;
7223
7224 case GET_FROM_C_STRING:
7225 /* Current display element of IT is from a C string. */
7226 if (!it->bidi_p
7227 /* If the string position is beyond string's end, it means
7228 next_element_from_c_string is padding the string with
7229 blanks, in which case we bypass the bidi iterator,
7230 because it cannot deal with such virtual characters. */
7231 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7232 {
7233 IT_BYTEPOS (*it) += it->len;
7234 IT_CHARPOS (*it) += 1;
7235 }
7236 else
7237 {
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_CHARPOS (*it) = it->bidi_it.charpos;
7241 }
7242 break;
7243
7244 case GET_FROM_DISPLAY_VECTOR:
7245 /* Current display element of IT is from a display table entry.
7246 Advance in the display table definition. Reset it to null if
7247 end reached, and continue with characters from buffers/
7248 strings. */
7249 ++it->current.dpvec_index;
7250
7251 /* Restore face of the iterator to what they were before the
7252 display vector entry (these entries may contain faces). */
7253 it->face_id = it->saved_face_id;
7254
7255 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7256 {
7257 int recheck_faces = it->ellipsis_p;
7258
7259 if (it->s)
7260 it->method = GET_FROM_C_STRING;
7261 else if (STRINGP (it->string))
7262 it->method = GET_FROM_STRING;
7263 else
7264 {
7265 it->method = GET_FROM_BUFFER;
7266 it->object = it->w->contents;
7267 }
7268
7269 it->dpvec = NULL;
7270 it->current.dpvec_index = -1;
7271
7272 /* Skip over characters which were displayed via IT->dpvec. */
7273 if (it->dpvec_char_len < 0)
7274 reseat_at_next_visible_line_start (it, 1);
7275 else if (it->dpvec_char_len > 0)
7276 {
7277 if (it->method == GET_FROM_STRING
7278 && it->current.overlay_string_index >= 0
7279 && it->n_overlay_strings > 0)
7280 it->ignore_overlay_strings_at_pos_p = true;
7281 it->len = it->dpvec_char_len;
7282 set_iterator_to_next (it, reseat_p);
7283 }
7284
7285 /* Maybe recheck faces after display vector. */
7286 if (recheck_faces)
7287 it->stop_charpos = IT_CHARPOS (*it);
7288 }
7289 break;
7290
7291 case GET_FROM_STRING:
7292 /* Current display element is a character from a Lisp string. */
7293 eassert (it->s == NULL && STRINGP (it->string));
7294 /* Don't advance past string end. These conditions are true
7295 when set_iterator_to_next is called at the end of
7296 get_next_display_element, in which case the Lisp string is
7297 already exhausted, and all we want is pop the iterator
7298 stack. */
7299 if (it->current.overlay_string_index >= 0)
7300 {
7301 /* This is an overlay string, so there's no padding with
7302 spaces, and the number of characters in the string is
7303 where the string ends. */
7304 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7305 goto consider_string_end;
7306 }
7307 else
7308 {
7309 /* Not an overlay string. There could be padding, so test
7310 against it->end_charpos. */
7311 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7312 goto consider_string_end;
7313 }
7314 if (it->cmp_it.id >= 0)
7315 {
7316 int i;
7317
7318 if (! it->bidi_p)
7319 {
7320 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7321 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7322 if (it->cmp_it.to < it->cmp_it.nglyphs)
7323 it->cmp_it.from = it->cmp_it.to;
7324 else
7325 {
7326 it->cmp_it.id = -1;
7327 composition_compute_stop_pos (&it->cmp_it,
7328 IT_STRING_CHARPOS (*it),
7329 IT_STRING_BYTEPOS (*it),
7330 it->end_charpos, it->string);
7331 }
7332 }
7333 else if (! it->cmp_it.reversed_p)
7334 {
7335 for (i = 0; i < it->cmp_it.nchars; i++)
7336 bidi_move_to_visually_next (&it->bidi_it);
7337 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7338 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7339
7340 if (it->cmp_it.to < it->cmp_it.nglyphs)
7341 it->cmp_it.from = it->cmp_it.to;
7342 else
7343 {
7344 ptrdiff_t stop = it->end_charpos;
7345 if (it->bidi_it.scan_dir < 0)
7346 stop = -1;
7347 composition_compute_stop_pos (&it->cmp_it,
7348 IT_STRING_CHARPOS (*it),
7349 IT_STRING_BYTEPOS (*it), stop,
7350 it->string);
7351 }
7352 }
7353 else
7354 {
7355 for (i = 0; i < it->cmp_it.nchars; i++)
7356 bidi_move_to_visually_next (&it->bidi_it);
7357 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7358 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7359 if (it->cmp_it.from > 0)
7360 it->cmp_it.to = it->cmp_it.from;
7361 else
7362 {
7363 ptrdiff_t stop = it->end_charpos;
7364 if (it->bidi_it.scan_dir < 0)
7365 stop = -1;
7366 composition_compute_stop_pos (&it->cmp_it,
7367 IT_STRING_CHARPOS (*it),
7368 IT_STRING_BYTEPOS (*it), stop,
7369 it->string);
7370 }
7371 }
7372 }
7373 else
7374 {
7375 if (!it->bidi_p
7376 /* If the string position is beyond string's end, it
7377 means next_element_from_string is padding the string
7378 with blanks, in which case we bypass the bidi
7379 iterator, because it cannot deal with such virtual
7380 characters. */
7381 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7382 {
7383 IT_STRING_BYTEPOS (*it) += it->len;
7384 IT_STRING_CHARPOS (*it) += 1;
7385 }
7386 else
7387 {
7388 int prev_scan_dir = it->bidi_it.scan_dir;
7389
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7393 if (prev_scan_dir != it->bidi_it.scan_dir)
7394 {
7395 ptrdiff_t stop = it->end_charpos;
7396
7397 if (it->bidi_it.scan_dir < 0)
7398 stop = -1;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7403 }
7404 }
7405 }
7406
7407 consider_string_end:
7408
7409 if (it->current.overlay_string_index >= 0)
7410 {
7411 /* IT->string is an overlay string. Advance to the
7412 next, if there is one. */
7413 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7414 {
7415 it->ellipsis_p = 0;
7416 next_overlay_string (it);
7417 if (it->ellipsis_p)
7418 setup_for_ellipsis (it, 0);
7419 }
7420 }
7421 else
7422 {
7423 /* IT->string is not an overlay string. If we reached
7424 its end, and there is something on IT->stack, proceed
7425 with what is on the stack. This can be either another
7426 string, this time an overlay string, or a buffer. */
7427 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7428 && it->sp > 0)
7429 {
7430 pop_it (it);
7431 if (it->method == GET_FROM_STRING)
7432 goto consider_string_end;
7433 }
7434 }
7435 break;
7436
7437 case GET_FROM_IMAGE:
7438 case GET_FROM_STRETCH:
7439 /* The position etc with which we have to proceed are on
7440 the stack. The position may be at the end of a string,
7441 if the `display' property takes up the whole string. */
7442 eassert (it->sp > 0);
7443 pop_it (it);
7444 if (it->method == GET_FROM_STRING)
7445 goto consider_string_end;
7446 break;
7447
7448 default:
7449 /* There are no other methods defined, so this should be a bug. */
7450 emacs_abort ();
7451 }
7452
7453 eassert (it->method != GET_FROM_STRING
7454 || (STRINGP (it->string)
7455 && IT_STRING_CHARPOS (*it) >= 0));
7456 }
7457
7458 /* Load IT's display element fields with information about the next
7459 display element which comes from a display table entry or from the
7460 result of translating a control character to one of the forms `^C'
7461 or `\003'.
7462
7463 IT->dpvec holds the glyphs to return as characters.
7464 IT->saved_face_id holds the face id before the display vector--it
7465 is restored into IT->face_id in set_iterator_to_next. */
7466
7467 static int
7468 next_element_from_display_vector (struct it *it)
7469 {
7470 Lisp_Object gc;
7471 int prev_face_id = it->face_id;
7472 int next_face_id;
7473
7474 /* Precondition. */
7475 eassert (it->dpvec && it->current.dpvec_index >= 0);
7476
7477 it->face_id = it->saved_face_id;
7478
7479 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7480 That seemed totally bogus - so I changed it... */
7481 gc = it->dpvec[it->current.dpvec_index];
7482
7483 if (GLYPH_CODE_P (gc))
7484 {
7485 struct face *this_face, *prev_face, *next_face;
7486
7487 it->c = GLYPH_CODE_CHAR (gc);
7488 it->len = CHAR_BYTES (it->c);
7489
7490 /* The entry may contain a face id to use. Such a face id is
7491 the id of a Lisp face, not a realized face. A face id of
7492 zero means no face is specified. */
7493 if (it->dpvec_face_id >= 0)
7494 it->face_id = it->dpvec_face_id;
7495 else
7496 {
7497 int lface_id = GLYPH_CODE_FACE (gc);
7498 if (lface_id > 0)
7499 it->face_id = merge_faces (it->f, Qt, lface_id,
7500 it->saved_face_id);
7501 }
7502
7503 /* Glyphs in the display vector could have the box face, so we
7504 need to set the related flags in the iterator, as
7505 appropriate. */
7506 this_face = FACE_FROM_ID (it->f, it->face_id);
7507 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7508
7509 /* Is this character the first character of a box-face run? */
7510 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7511 && (!prev_face
7512 || prev_face->box == FACE_NO_BOX));
7513
7514 /* For the last character of the box-face run, we need to look
7515 either at the next glyph from the display vector, or at the
7516 face we saw before the display vector. */
7517 next_face_id = it->saved_face_id;
7518 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7519 {
7520 if (it->dpvec_face_id >= 0)
7521 next_face_id = it->dpvec_face_id;
7522 else
7523 {
7524 int lface_id =
7525 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7526
7527 if (lface_id > 0)
7528 next_face_id = merge_faces (it->f, Qt, lface_id,
7529 it->saved_face_id);
7530 }
7531 }
7532 next_face = FACE_FROM_ID (it->f, next_face_id);
7533 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7534 && (!next_face
7535 || next_face->box == FACE_NO_BOX));
7536 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7537 }
7538 else
7539 /* Display table entry is invalid. Return a space. */
7540 it->c = ' ', it->len = 1;
7541
7542 /* Don't change position and object of the iterator here. They are
7543 still the values of the character that had this display table
7544 entry or was translated, and that's what we want. */
7545 it->what = IT_CHARACTER;
7546 return 1;
7547 }
7548
7549 /* Get the first element of string/buffer in the visual order, after
7550 being reseated to a new position in a string or a buffer. */
7551 static void
7552 get_visually_first_element (struct it *it)
7553 {
7554 int string_p = STRINGP (it->string) || it->s;
7555 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7556 ptrdiff_t bob = (string_p ? 0 : BEGV);
7557
7558 if (STRINGP (it->string))
7559 {
7560 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7562 }
7563 else
7564 {
7565 it->bidi_it.charpos = IT_CHARPOS (*it);
7566 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7567 }
7568
7569 if (it->bidi_it.charpos == eob)
7570 {
7571 /* Nothing to do, but reset the FIRST_ELT flag, like
7572 bidi_paragraph_init does, because we are not going to
7573 call it. */
7574 it->bidi_it.first_elt = 0;
7575 }
7576 else if (it->bidi_it.charpos == bob
7577 || (!string_p
7578 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7579 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7580 {
7581 /* If we are at the beginning of a line/string, we can produce
7582 the next element right away. */
7583 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7584 bidi_move_to_visually_next (&it->bidi_it);
7585 }
7586 else
7587 {
7588 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7589
7590 /* We need to prime the bidi iterator starting at the line's or
7591 string's beginning, before we will be able to produce the
7592 next element. */
7593 if (string_p)
7594 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7595 else
7596 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7597 IT_BYTEPOS (*it), -1,
7598 &it->bidi_it.bytepos);
7599 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7600 do
7601 {
7602 /* Now return to buffer/string position where we were asked
7603 to get the next display element, and produce that. */
7604 bidi_move_to_visually_next (&it->bidi_it);
7605 }
7606 while (it->bidi_it.bytepos != orig_bytepos
7607 && it->bidi_it.charpos < eob);
7608 }
7609
7610 /* Adjust IT's position information to where we ended up. */
7611 if (STRINGP (it->string))
7612 {
7613 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 }
7616 else
7617 {
7618 IT_CHARPOS (*it) = it->bidi_it.charpos;
7619 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7620 }
7621
7622 if (STRINGP (it->string) || !it->s)
7623 {
7624 ptrdiff_t stop, charpos, bytepos;
7625
7626 if (STRINGP (it->string))
7627 {
7628 eassert (!it->s);
7629 stop = SCHARS (it->string);
7630 if (stop > it->end_charpos)
7631 stop = it->end_charpos;
7632 charpos = IT_STRING_CHARPOS (*it);
7633 bytepos = IT_STRING_BYTEPOS (*it);
7634 }
7635 else
7636 {
7637 stop = it->end_charpos;
7638 charpos = IT_CHARPOS (*it);
7639 bytepos = IT_BYTEPOS (*it);
7640 }
7641 if (it->bidi_it.scan_dir < 0)
7642 stop = -1;
7643 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7644 it->string);
7645 }
7646 }
7647
7648 /* Load IT with the next display element from Lisp string IT->string.
7649 IT->current.string_pos is the current position within the string.
7650 If IT->current.overlay_string_index >= 0, the Lisp string is an
7651 overlay string. */
7652
7653 static int
7654 next_element_from_string (struct it *it)
7655 {
7656 struct text_pos position;
7657
7658 eassert (STRINGP (it->string));
7659 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7660 eassert (IT_STRING_CHARPOS (*it) >= 0);
7661 position = it->current.string_pos;
7662
7663 /* With bidi reordering, the character to display might not be the
7664 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7665 that we were reseat()ed to a new string, whose paragraph
7666 direction is not known. */
7667 if (it->bidi_p && it->bidi_it.first_elt)
7668 {
7669 get_visually_first_element (it);
7670 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7671 }
7672
7673 /* Time to check for invisible text? */
7674 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7675 {
7676 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7677 {
7678 if (!(!it->bidi_p
7679 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7680 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7681 {
7682 /* With bidi non-linear iteration, we could find
7683 ourselves far beyond the last computed stop_charpos,
7684 with several other stop positions in between that we
7685 missed. Scan them all now, in buffer's logical
7686 order, until we find and handle the last stop_charpos
7687 that precedes our current position. */
7688 handle_stop_backwards (it, it->stop_charpos);
7689 return GET_NEXT_DISPLAY_ELEMENT (it);
7690 }
7691 else
7692 {
7693 if (it->bidi_p)
7694 {
7695 /* Take note of the stop position we just moved
7696 across, for when we will move back across it. */
7697 it->prev_stop = it->stop_charpos;
7698 /* If we are at base paragraph embedding level, take
7699 note of the last stop position seen at this
7700 level. */
7701 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7702 it->base_level_stop = it->stop_charpos;
7703 }
7704 handle_stop (it);
7705
7706 /* Since a handler may have changed IT->method, we must
7707 recurse here. */
7708 return GET_NEXT_DISPLAY_ELEMENT (it);
7709 }
7710 }
7711 else if (it->bidi_p
7712 /* If we are before prev_stop, we may have overstepped
7713 on our way backwards a stop_pos, and if so, we need
7714 to handle that stop_pos. */
7715 && IT_STRING_CHARPOS (*it) < it->prev_stop
7716 /* We can sometimes back up for reasons that have nothing
7717 to do with bidi reordering. E.g., compositions. The
7718 code below is only needed when we are above the base
7719 embedding level, so test for that explicitly. */
7720 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7721 {
7722 /* If we lost track of base_level_stop, we have no better
7723 place for handle_stop_backwards to start from than string
7724 beginning. This happens, e.g., when we were reseated to
7725 the previous screenful of text by vertical-motion. */
7726 if (it->base_level_stop <= 0
7727 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7728 it->base_level_stop = 0;
7729 handle_stop_backwards (it, it->base_level_stop);
7730 return GET_NEXT_DISPLAY_ELEMENT (it);
7731 }
7732 }
7733
7734 if (it->current.overlay_string_index >= 0)
7735 {
7736 /* Get the next character from an overlay string. In overlay
7737 strings, there is no field width or padding with spaces to
7738 do. */
7739 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7740 {
7741 it->what = IT_EOB;
7742 return 0;
7743 }
7744 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7745 IT_STRING_BYTEPOS (*it),
7746 it->bidi_it.scan_dir < 0
7747 ? -1
7748 : SCHARS (it->string))
7749 && next_element_from_composition (it))
7750 {
7751 return 1;
7752 }
7753 else if (STRING_MULTIBYTE (it->string))
7754 {
7755 const unsigned char *s = (SDATA (it->string)
7756 + IT_STRING_BYTEPOS (*it));
7757 it->c = string_char_and_length (s, &it->len);
7758 }
7759 else
7760 {
7761 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7762 it->len = 1;
7763 }
7764 }
7765 else
7766 {
7767 /* Get the next character from a Lisp string that is not an
7768 overlay string. Such strings come from the mode line, for
7769 example. We may have to pad with spaces, or truncate the
7770 string. See also next_element_from_c_string. */
7771 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7772 {
7773 it->what = IT_EOB;
7774 return 0;
7775 }
7776 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7777 {
7778 /* Pad with spaces. */
7779 it->c = ' ', it->len = 1;
7780 CHARPOS (position) = BYTEPOS (position) = -1;
7781 }
7782 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7783 IT_STRING_BYTEPOS (*it),
7784 it->bidi_it.scan_dir < 0
7785 ? -1
7786 : it->string_nchars)
7787 && next_element_from_composition (it))
7788 {
7789 return 1;
7790 }
7791 else if (STRING_MULTIBYTE (it->string))
7792 {
7793 const unsigned char *s = (SDATA (it->string)
7794 + IT_STRING_BYTEPOS (*it));
7795 it->c = string_char_and_length (s, &it->len);
7796 }
7797 else
7798 {
7799 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7800 it->len = 1;
7801 }
7802 }
7803
7804 /* Record what we have and where it came from. */
7805 it->what = IT_CHARACTER;
7806 it->object = it->string;
7807 it->position = position;
7808 return 1;
7809 }
7810
7811
7812 /* Load IT with next display element from C string IT->s.
7813 IT->string_nchars is the maximum number of characters to return
7814 from the string. IT->end_charpos may be greater than
7815 IT->string_nchars when this function is called, in which case we
7816 may have to return padding spaces. Value is zero if end of string
7817 reached, including padding spaces. */
7818
7819 static int
7820 next_element_from_c_string (struct it *it)
7821 {
7822 bool success_p = true;
7823
7824 eassert (it->s);
7825 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7826 it->what = IT_CHARACTER;
7827 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7828 it->object = Qnil;
7829
7830 /* With bidi reordering, the character to display might not be the
7831 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7832 we were reseated to a new string, whose paragraph direction is
7833 not known. */
7834 if (it->bidi_p && it->bidi_it.first_elt)
7835 get_visually_first_element (it);
7836
7837 /* IT's position can be greater than IT->string_nchars in case a
7838 field width or precision has been specified when the iterator was
7839 initialized. */
7840 if (IT_CHARPOS (*it) >= it->end_charpos)
7841 {
7842 /* End of the game. */
7843 it->what = IT_EOB;
7844 success_p = 0;
7845 }
7846 else if (IT_CHARPOS (*it) >= it->string_nchars)
7847 {
7848 /* Pad with spaces. */
7849 it->c = ' ', it->len = 1;
7850 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7851 }
7852 else if (it->multibyte_p)
7853 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7854 else
7855 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7856
7857 return success_p;
7858 }
7859
7860
7861 /* Set up IT to return characters from an ellipsis, if appropriate.
7862 The definition of the ellipsis glyphs may come from a display table
7863 entry. This function fills IT with the first glyph from the
7864 ellipsis if an ellipsis is to be displayed. */
7865
7866 static int
7867 next_element_from_ellipsis (struct it *it)
7868 {
7869 if (it->selective_display_ellipsis_p)
7870 setup_for_ellipsis (it, it->len);
7871 else
7872 {
7873 /* The face at the current position may be different from the
7874 face we find after the invisible text. Remember what it
7875 was in IT->saved_face_id, and signal that it's there by
7876 setting face_before_selective_p. */
7877 it->saved_face_id = it->face_id;
7878 it->method = GET_FROM_BUFFER;
7879 it->object = it->w->contents;
7880 reseat_at_next_visible_line_start (it, 1);
7881 it->face_before_selective_p = true;
7882 }
7883
7884 return GET_NEXT_DISPLAY_ELEMENT (it);
7885 }
7886
7887
7888 /* Deliver an image display element. The iterator IT is already
7889 filled with image information (done in handle_display_prop). Value
7890 is always 1. */
7891
7892
7893 static int
7894 next_element_from_image (struct it *it)
7895 {
7896 it->what = IT_IMAGE;
7897 it->ignore_overlay_strings_at_pos_p = 0;
7898 return 1;
7899 }
7900
7901
7902 /* Fill iterator IT with next display element from a stretch glyph
7903 property. IT->object is the value of the text property. Value is
7904 always 1. */
7905
7906 static int
7907 next_element_from_stretch (struct it *it)
7908 {
7909 it->what = IT_STRETCH;
7910 return 1;
7911 }
7912
7913 /* Scan backwards from IT's current position until we find a stop
7914 position, or until BEGV. This is called when we find ourself
7915 before both the last known prev_stop and base_level_stop while
7916 reordering bidirectional text. */
7917
7918 static void
7919 compute_stop_pos_backwards (struct it *it)
7920 {
7921 const int SCAN_BACK_LIMIT = 1000;
7922 struct text_pos pos;
7923 struct display_pos save_current = it->current;
7924 struct text_pos save_position = it->position;
7925 ptrdiff_t charpos = IT_CHARPOS (*it);
7926 ptrdiff_t where_we_are = charpos;
7927 ptrdiff_t save_stop_pos = it->stop_charpos;
7928 ptrdiff_t save_end_pos = it->end_charpos;
7929
7930 eassert (NILP (it->string) && !it->s);
7931 eassert (it->bidi_p);
7932 it->bidi_p = 0;
7933 do
7934 {
7935 it->end_charpos = min (charpos + 1, ZV);
7936 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7937 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7938 reseat_1 (it, pos, 0);
7939 compute_stop_pos (it);
7940 /* We must advance forward, right? */
7941 if (it->stop_charpos <= charpos)
7942 emacs_abort ();
7943 }
7944 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7945
7946 if (it->stop_charpos <= where_we_are)
7947 it->prev_stop = it->stop_charpos;
7948 else
7949 it->prev_stop = BEGV;
7950 it->bidi_p = true;
7951 it->current = save_current;
7952 it->position = save_position;
7953 it->stop_charpos = save_stop_pos;
7954 it->end_charpos = save_end_pos;
7955 }
7956
7957 /* Scan forward from CHARPOS in the current buffer/string, until we
7958 find a stop position > current IT's position. Then handle the stop
7959 position before that. This is called when we bump into a stop
7960 position while reordering bidirectional text. CHARPOS should be
7961 the last previously processed stop_pos (or BEGV/0, if none were
7962 processed yet) whose position is less that IT's current
7963 position. */
7964
7965 static void
7966 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7967 {
7968 int bufp = !STRINGP (it->string);
7969 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7970 struct display_pos save_current = it->current;
7971 struct text_pos save_position = it->position;
7972 struct text_pos pos1;
7973 ptrdiff_t next_stop;
7974
7975 /* Scan in strict logical order. */
7976 eassert (it->bidi_p);
7977 it->bidi_p = 0;
7978 do
7979 {
7980 it->prev_stop = charpos;
7981 if (bufp)
7982 {
7983 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7984 reseat_1 (it, pos1, 0);
7985 }
7986 else
7987 it->current.string_pos = string_pos (charpos, it->string);
7988 compute_stop_pos (it);
7989 /* We must advance forward, right? */
7990 if (it->stop_charpos <= it->prev_stop)
7991 emacs_abort ();
7992 charpos = it->stop_charpos;
7993 }
7994 while (charpos <= where_we_are);
7995
7996 it->bidi_p = true;
7997 it->current = save_current;
7998 it->position = save_position;
7999 next_stop = it->stop_charpos;
8000 it->stop_charpos = it->prev_stop;
8001 handle_stop (it);
8002 it->stop_charpos = next_stop;
8003 }
8004
8005 /* Load IT with the next display element from current_buffer. Value
8006 is zero if end of buffer reached. IT->stop_charpos is the next
8007 position at which to stop and check for text properties or buffer
8008 end. */
8009
8010 static int
8011 next_element_from_buffer (struct it *it)
8012 {
8013 bool success_p = true;
8014
8015 eassert (IT_CHARPOS (*it) >= BEGV);
8016 eassert (NILP (it->string) && !it->s);
8017 eassert (!it->bidi_p
8018 || (EQ (it->bidi_it.string.lstring, Qnil)
8019 && it->bidi_it.string.s == NULL));
8020
8021 /* With bidi reordering, the character to display might not be the
8022 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8023 we were reseat()ed to a new buffer position, which is potentially
8024 a different paragraph. */
8025 if (it->bidi_p && it->bidi_it.first_elt)
8026 {
8027 get_visually_first_element (it);
8028 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8029 }
8030
8031 if (IT_CHARPOS (*it) >= it->stop_charpos)
8032 {
8033 if (IT_CHARPOS (*it) >= it->end_charpos)
8034 {
8035 int overlay_strings_follow_p;
8036
8037 /* End of the game, except when overlay strings follow that
8038 haven't been returned yet. */
8039 if (it->overlay_strings_at_end_processed_p)
8040 overlay_strings_follow_p = 0;
8041 else
8042 {
8043 it->overlay_strings_at_end_processed_p = true;
8044 overlay_strings_follow_p = get_overlay_strings (it, 0);
8045 }
8046
8047 if (overlay_strings_follow_p)
8048 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8049 else
8050 {
8051 it->what = IT_EOB;
8052 it->position = it->current.pos;
8053 success_p = 0;
8054 }
8055 }
8056 else if (!(!it->bidi_p
8057 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8058 || IT_CHARPOS (*it) == it->stop_charpos))
8059 {
8060 /* With bidi non-linear iteration, we could find ourselves
8061 far beyond the last computed stop_charpos, with several
8062 other stop positions in between that we missed. Scan
8063 them all now, in buffer's logical order, until we find
8064 and handle the last stop_charpos that precedes our
8065 current position. */
8066 handle_stop_backwards (it, it->stop_charpos);
8067 return GET_NEXT_DISPLAY_ELEMENT (it);
8068 }
8069 else
8070 {
8071 if (it->bidi_p)
8072 {
8073 /* Take note of the stop position we just moved across,
8074 for when we will move back across it. */
8075 it->prev_stop = it->stop_charpos;
8076 /* If we are at base paragraph embedding level, take
8077 note of the last stop position seen at this
8078 level. */
8079 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8080 it->base_level_stop = it->stop_charpos;
8081 }
8082 handle_stop (it);
8083 return GET_NEXT_DISPLAY_ELEMENT (it);
8084 }
8085 }
8086 else if (it->bidi_p
8087 /* If we are before prev_stop, we may have overstepped on
8088 our way backwards a stop_pos, and if so, we need to
8089 handle that stop_pos. */
8090 && IT_CHARPOS (*it) < it->prev_stop
8091 /* We can sometimes back up for reasons that have nothing
8092 to do with bidi reordering. E.g., compositions. The
8093 code below is only needed when we are above the base
8094 embedding level, so test for that explicitly. */
8095 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8096 {
8097 if (it->base_level_stop <= 0
8098 || IT_CHARPOS (*it) < it->base_level_stop)
8099 {
8100 /* If we lost track of base_level_stop, we need to find
8101 prev_stop by looking backwards. This happens, e.g., when
8102 we were reseated to the previous screenful of text by
8103 vertical-motion. */
8104 it->base_level_stop = BEGV;
8105 compute_stop_pos_backwards (it);
8106 handle_stop_backwards (it, it->prev_stop);
8107 }
8108 else
8109 handle_stop_backwards (it, it->base_level_stop);
8110 return GET_NEXT_DISPLAY_ELEMENT (it);
8111 }
8112 else
8113 {
8114 /* No face changes, overlays etc. in sight, so just return a
8115 character from current_buffer. */
8116 unsigned char *p;
8117 ptrdiff_t stop;
8118
8119 /* Maybe run the redisplay end trigger hook. Performance note:
8120 This doesn't seem to cost measurable time. */
8121 if (it->redisplay_end_trigger_charpos
8122 && it->glyph_row
8123 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8124 run_redisplay_end_trigger_hook (it);
8125
8126 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8127 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8128 stop)
8129 && next_element_from_composition (it))
8130 {
8131 return 1;
8132 }
8133
8134 /* Get the next character, maybe multibyte. */
8135 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8136 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8137 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8138 else
8139 it->c = *p, it->len = 1;
8140
8141 /* Record what we have and where it came from. */
8142 it->what = IT_CHARACTER;
8143 it->object = it->w->contents;
8144 it->position = it->current.pos;
8145
8146 /* Normally we return the character found above, except when we
8147 really want to return an ellipsis for selective display. */
8148 if (it->selective)
8149 {
8150 if (it->c == '\n')
8151 {
8152 /* A value of selective > 0 means hide lines indented more
8153 than that number of columns. */
8154 if (it->selective > 0
8155 && IT_CHARPOS (*it) + 1 < ZV
8156 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8157 IT_BYTEPOS (*it) + 1,
8158 it->selective))
8159 {
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8162 }
8163 }
8164 else if (it->c == '\r' && it->selective == -1)
8165 {
8166 /* A value of selective == -1 means that everything from the
8167 CR to the end of the line is invisible, with maybe an
8168 ellipsis displayed for it. */
8169 success_p = next_element_from_ellipsis (it);
8170 it->dpvec_char_len = -1;
8171 }
8172 }
8173 }
8174
8175 /* Value is zero if end of buffer reached. */
8176 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8177 return success_p;
8178 }
8179
8180
8181 /* Run the redisplay end trigger hook for IT. */
8182
8183 static void
8184 run_redisplay_end_trigger_hook (struct it *it)
8185 {
8186 Lisp_Object args[3];
8187
8188 /* IT->glyph_row should be non-null, i.e. we should be actually
8189 displaying something, or otherwise we should not run the hook. */
8190 eassert (it->glyph_row);
8191
8192 /* Set up hook arguments. */
8193 args[0] = Qredisplay_end_trigger_functions;
8194 args[1] = it->window;
8195 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8196 it->redisplay_end_trigger_charpos = 0;
8197
8198 /* Since we are *trying* to run these functions, don't try to run
8199 them again, even if they get an error. */
8200 wset_redisplay_end_trigger (it->w, Qnil);
8201 Frun_hook_with_args (3, args);
8202
8203 /* Notice if it changed the face of the character we are on. */
8204 handle_face_prop (it);
8205 }
8206
8207
8208 /* Deliver a composition display element. Unlike the other
8209 next_element_from_XXX, this function is not registered in the array
8210 get_next_element[]. It is called from next_element_from_buffer and
8211 next_element_from_string when necessary. */
8212
8213 static int
8214 next_element_from_composition (struct it *it)
8215 {
8216 it->what = IT_COMPOSITION;
8217 it->len = it->cmp_it.nbytes;
8218 if (STRINGP (it->string))
8219 {
8220 if (it->c < 0)
8221 {
8222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8224 return 0;
8225 }
8226 it->position = it->current.string_pos;
8227 it->object = it->string;
8228 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8229 IT_STRING_BYTEPOS (*it), it->string);
8230 }
8231 else
8232 {
8233 if (it->c < 0)
8234 {
8235 IT_CHARPOS (*it) += it->cmp_it.nchars;
8236 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8237 if (it->bidi_p)
8238 {
8239 if (it->bidi_it.new_paragraph)
8240 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8241 /* Resync the bidi iterator with IT's new position.
8242 FIXME: this doesn't support bidirectional text. */
8243 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8244 bidi_move_to_visually_next (&it->bidi_it);
8245 }
8246 return 0;
8247 }
8248 it->position = it->current.pos;
8249 it->object = it->w->contents;
8250 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8251 IT_BYTEPOS (*it), Qnil);
8252 }
8253 return 1;
8254 }
8255
8256
8257 \f
8258 /***********************************************************************
8259 Moving an iterator without producing glyphs
8260 ***********************************************************************/
8261
8262 /* Check if iterator is at a position corresponding to a valid buffer
8263 position after some move_it_ call. */
8264
8265 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8266 ((it)->method == GET_FROM_STRING \
8267 ? IT_STRING_CHARPOS (*it) == 0 \
8268 : 1)
8269
8270
8271 /* Move iterator IT to a specified buffer or X position within one
8272 line on the display without producing glyphs.
8273
8274 OP should be a bit mask including some or all of these bits:
8275 MOVE_TO_X: Stop upon reaching x-position TO_X.
8276 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8277 Regardless of OP's value, stop upon reaching the end of the display line.
8278
8279 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8280 This means, in particular, that TO_X includes window's horizontal
8281 scroll amount.
8282
8283 The return value has several possible values that
8284 say what condition caused the scan to stop:
8285
8286 MOVE_POS_MATCH_OR_ZV
8287 - when TO_POS or ZV was reached.
8288
8289 MOVE_X_REACHED
8290 -when TO_X was reached before TO_POS or ZV were reached.
8291
8292 MOVE_LINE_CONTINUED
8293 - when we reached the end of the display area and the line must
8294 be continued.
8295
8296 MOVE_LINE_TRUNCATED
8297 - when we reached the end of the display area and the line is
8298 truncated.
8299
8300 MOVE_NEWLINE_OR_CR
8301 - when we stopped at a line end, i.e. a newline or a CR and selective
8302 display is on. */
8303
8304 static enum move_it_result
8305 move_it_in_display_line_to (struct it *it,
8306 ptrdiff_t to_charpos, int to_x,
8307 enum move_operation_enum op)
8308 {
8309 enum move_it_result result = MOVE_UNDEFINED;
8310 struct glyph_row *saved_glyph_row;
8311 struct it wrap_it, atpos_it, atx_it, ppos_it;
8312 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8313 void *ppos_data = NULL;
8314 int may_wrap = 0;
8315 enum it_method prev_method = it->method;
8316 ptrdiff_t closest_pos, prev_pos = IT_CHARPOS (*it);
8317 int saw_smaller_pos = prev_pos < to_charpos;
8318
8319 /* Don't produce glyphs in produce_glyphs. */
8320 saved_glyph_row = it->glyph_row;
8321 it->glyph_row = NULL;
8322
8323 /* Use wrap_it to save a copy of IT wherever a word wrap could
8324 occur. Use atpos_it to save a copy of IT at the desired buffer
8325 position, if found, so that we can scan ahead and check if the
8326 word later overshoots the window edge. Use atx_it similarly, for
8327 pixel positions. */
8328 wrap_it.sp = -1;
8329 atpos_it.sp = -1;
8330 atx_it.sp = -1;
8331
8332 /* Use ppos_it under bidi reordering to save a copy of IT for the
8333 initial position. We restore that position in IT when we have
8334 scanned the entire display line without finding a match for
8335 TO_CHARPOS and all the character positions are greater than
8336 TO_CHARPOS. We then restart the scan from the initial position,
8337 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8338 the closest to TO_CHARPOS. */
8339 if (it->bidi_p)
8340 {
8341 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8342 {
8343 SAVE_IT (ppos_it, *it, ppos_data);
8344 closest_pos = IT_CHARPOS (*it);
8345 }
8346 else
8347 closest_pos = ZV;
8348 }
8349
8350 #define BUFFER_POS_REACHED_P() \
8351 ((op & MOVE_TO_POS) != 0 \
8352 && BUFFERP (it->object) \
8353 && (IT_CHARPOS (*it) == to_charpos \
8354 || ((!it->bidi_p \
8355 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8356 && IT_CHARPOS (*it) > to_charpos) \
8357 || (it->what == IT_COMPOSITION \
8358 && ((IT_CHARPOS (*it) > to_charpos \
8359 && to_charpos >= it->cmp_it.charpos) \
8360 || (IT_CHARPOS (*it) < to_charpos \
8361 && to_charpos <= it->cmp_it.charpos)))) \
8362 && (it->method == GET_FROM_BUFFER \
8363 || (it->method == GET_FROM_DISPLAY_VECTOR \
8364 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8365
8366 /* If there's a line-/wrap-prefix, handle it. */
8367 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8368 && it->current_y < it->last_visible_y)
8369 handle_line_prefix (it);
8370
8371 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8372 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8373
8374 while (1)
8375 {
8376 int x, i, ascent = 0, descent = 0;
8377
8378 /* Utility macro to reset an iterator with x, ascent, and descent. */
8379 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8380 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8381 (IT)->max_descent = descent)
8382
8383 /* Stop if we move beyond TO_CHARPOS (after an image or a
8384 display string or stretch glyph). */
8385 if ((op & MOVE_TO_POS) != 0
8386 && BUFFERP (it->object)
8387 && it->method == GET_FROM_BUFFER
8388 && (((!it->bidi_p
8389 /* When the iterator is at base embedding level, we
8390 are guaranteed that characters are delivered for
8391 display in strictly increasing order of their
8392 buffer positions. */
8393 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8394 && IT_CHARPOS (*it) > to_charpos)
8395 || (it->bidi_p
8396 && (prev_method == GET_FROM_IMAGE
8397 || prev_method == GET_FROM_STRETCH
8398 || prev_method == GET_FROM_STRING)
8399 /* Passed TO_CHARPOS from left to right. */
8400 && ((prev_pos < to_charpos
8401 && IT_CHARPOS (*it) > to_charpos)
8402 /* Passed TO_CHARPOS from right to left. */
8403 || (prev_pos > to_charpos
8404 && IT_CHARPOS (*it) < to_charpos)))))
8405 {
8406 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8407 {
8408 result = MOVE_POS_MATCH_OR_ZV;
8409 break;
8410 }
8411 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8412 /* If wrap_it is valid, the current position might be in a
8413 word that is wrapped. So, save the iterator in
8414 atpos_it and continue to see if wrapping happens. */
8415 SAVE_IT (atpos_it, *it, atpos_data);
8416 }
8417
8418 /* Stop when ZV reached.
8419 We used to stop here when TO_CHARPOS reached as well, but that is
8420 too soon if this glyph does not fit on this line. So we handle it
8421 explicitly below. */
8422 if (!get_next_display_element (it))
8423 {
8424 result = MOVE_POS_MATCH_OR_ZV;
8425 break;
8426 }
8427
8428 if (it->line_wrap == TRUNCATE)
8429 {
8430 if (BUFFER_POS_REACHED_P ())
8431 {
8432 result = MOVE_POS_MATCH_OR_ZV;
8433 break;
8434 }
8435 }
8436 else
8437 {
8438 if (it->line_wrap == WORD_WRAP)
8439 {
8440 if (IT_DISPLAYING_WHITESPACE (it))
8441 may_wrap = 1;
8442 else if (may_wrap)
8443 {
8444 /* We have reached a glyph that follows one or more
8445 whitespace characters. If the position is
8446 already found, we are done. */
8447 if (atpos_it.sp >= 0)
8448 {
8449 RESTORE_IT (it, &atpos_it, atpos_data);
8450 result = MOVE_POS_MATCH_OR_ZV;
8451 goto done;
8452 }
8453 if (atx_it.sp >= 0)
8454 {
8455 RESTORE_IT (it, &atx_it, atx_data);
8456 result = MOVE_X_REACHED;
8457 goto done;
8458 }
8459 /* Otherwise, we can wrap here. */
8460 SAVE_IT (wrap_it, *it, wrap_data);
8461 may_wrap = 0;
8462 }
8463 }
8464 }
8465
8466 /* Remember the line height for the current line, in case
8467 the next element doesn't fit on the line. */
8468 ascent = it->max_ascent;
8469 descent = it->max_descent;
8470
8471 /* The call to produce_glyphs will get the metrics of the
8472 display element IT is loaded with. Record the x-position
8473 before this display element, in case it doesn't fit on the
8474 line. */
8475 x = it->current_x;
8476
8477 PRODUCE_GLYPHS (it);
8478
8479 if (it->area != TEXT_AREA)
8480 {
8481 prev_method = it->method;
8482 if (it->method == GET_FROM_BUFFER)
8483 prev_pos = IT_CHARPOS (*it);
8484 set_iterator_to_next (it, 1);
8485 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8486 SET_TEXT_POS (this_line_min_pos,
8487 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8488 if (it->bidi_p
8489 && (op & MOVE_TO_POS)
8490 && IT_CHARPOS (*it) > to_charpos
8491 && IT_CHARPOS (*it) < closest_pos)
8492 closest_pos = IT_CHARPOS (*it);
8493 continue;
8494 }
8495
8496 /* The number of glyphs we get back in IT->nglyphs will normally
8497 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8498 character on a terminal frame, or (iii) a line end. For the
8499 second case, IT->nglyphs - 1 padding glyphs will be present.
8500 (On X frames, there is only one glyph produced for a
8501 composite character.)
8502
8503 The behavior implemented below means, for continuation lines,
8504 that as many spaces of a TAB as fit on the current line are
8505 displayed there. For terminal frames, as many glyphs of a
8506 multi-glyph character are displayed in the current line, too.
8507 This is what the old redisplay code did, and we keep it that
8508 way. Under X, the whole shape of a complex character must
8509 fit on the line or it will be completely displayed in the
8510 next line.
8511
8512 Note that both for tabs and padding glyphs, all glyphs have
8513 the same width. */
8514 if (it->nglyphs)
8515 {
8516 /* More than one glyph or glyph doesn't fit on line. All
8517 glyphs have the same width. */
8518 int single_glyph_width = it->pixel_width / it->nglyphs;
8519 int new_x;
8520 int x_before_this_char = x;
8521 int hpos_before_this_char = it->hpos;
8522
8523 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8524 {
8525 new_x = x + single_glyph_width;
8526
8527 /* We want to leave anything reaching TO_X to the caller. */
8528 if ((op & MOVE_TO_X) && new_x > to_x)
8529 {
8530 if (BUFFER_POS_REACHED_P ())
8531 {
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8533 goto buffer_pos_reached;
8534 if (atpos_it.sp < 0)
8535 {
8536 SAVE_IT (atpos_it, *it, atpos_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8538 }
8539 }
8540 else
8541 {
8542 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8543 {
8544 it->current_x = x;
8545 result = MOVE_X_REACHED;
8546 break;
8547 }
8548 if (atx_it.sp < 0)
8549 {
8550 SAVE_IT (atx_it, *it, atx_data);
8551 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8552 }
8553 }
8554 }
8555
8556 if (/* Lines are continued. */
8557 it->line_wrap != TRUNCATE
8558 && (/* And glyph doesn't fit on the line. */
8559 new_x > it->last_visible_x
8560 /* Or it fits exactly and we're on a window
8561 system frame. */
8562 || (new_x == it->last_visible_x
8563 && FRAME_WINDOW_P (it->f)
8564 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8565 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8566 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8567 {
8568 if (/* IT->hpos == 0 means the very first glyph
8569 doesn't fit on the line, e.g. a wide image. */
8570 it->hpos == 0
8571 || (new_x == it->last_visible_x
8572 && FRAME_WINDOW_P (it->f)))
8573 {
8574 ++it->hpos;
8575 it->current_x = new_x;
8576
8577 /* The character's last glyph just barely fits
8578 in this row. */
8579 if (i == it->nglyphs - 1)
8580 {
8581 /* If this is the destination position,
8582 return a position *before* it in this row,
8583 now that we know it fits in this row. */
8584 if (BUFFER_POS_REACHED_P ())
8585 {
8586 if (it->line_wrap != WORD_WRAP
8587 || wrap_it.sp < 0)
8588 {
8589 it->hpos = hpos_before_this_char;
8590 it->current_x = x_before_this_char;
8591 result = MOVE_POS_MATCH_OR_ZV;
8592 break;
8593 }
8594 if (it->line_wrap == WORD_WRAP
8595 && atpos_it.sp < 0)
8596 {
8597 SAVE_IT (atpos_it, *it, atpos_data);
8598 atpos_it.current_x = x_before_this_char;
8599 atpos_it.hpos = hpos_before_this_char;
8600 }
8601 }
8602
8603 prev_method = it->method;
8604 if (it->method == GET_FROM_BUFFER)
8605 prev_pos = IT_CHARPOS (*it);
8606 set_iterator_to_next (it, 1);
8607 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8608 SET_TEXT_POS (this_line_min_pos,
8609 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8610 /* On graphical terminals, newlines may
8611 "overflow" into the fringe if
8612 overflow-newline-into-fringe is non-nil.
8613 On text terminals, and on graphical
8614 terminals with no right margin, newlines
8615 may overflow into the last glyph on the
8616 display line.*/
8617 if (!FRAME_WINDOW_P (it->f)
8618 || ((it->bidi_p
8619 && it->bidi_it.paragraph_dir == R2L)
8620 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8621 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8622 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8623 {
8624 if (!get_next_display_element (it))
8625 {
8626 result = MOVE_POS_MATCH_OR_ZV;
8627 break;
8628 }
8629 if (BUFFER_POS_REACHED_P ())
8630 {
8631 if (ITERATOR_AT_END_OF_LINE_P (it))
8632 result = MOVE_POS_MATCH_OR_ZV;
8633 else
8634 result = MOVE_LINE_CONTINUED;
8635 break;
8636 }
8637 if (ITERATOR_AT_END_OF_LINE_P (it)
8638 && (it->line_wrap != WORD_WRAP
8639 || wrap_it.sp < 0))
8640 {
8641 result = MOVE_NEWLINE_OR_CR;
8642 break;
8643 }
8644 }
8645 }
8646 }
8647 else
8648 IT_RESET_X_ASCENT_DESCENT (it);
8649
8650 if (wrap_it.sp >= 0)
8651 {
8652 RESTORE_IT (it, &wrap_it, wrap_data);
8653 atpos_it.sp = -1;
8654 atx_it.sp = -1;
8655 }
8656
8657 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8658 IT_CHARPOS (*it)));
8659 result = MOVE_LINE_CONTINUED;
8660 break;
8661 }
8662
8663 if (BUFFER_POS_REACHED_P ())
8664 {
8665 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8666 goto buffer_pos_reached;
8667 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8668 {
8669 SAVE_IT (atpos_it, *it, atpos_data);
8670 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8671 }
8672 }
8673
8674 if (new_x > it->first_visible_x)
8675 {
8676 /* Glyph is visible. Increment number of glyphs that
8677 would be displayed. */
8678 ++it->hpos;
8679 }
8680 }
8681
8682 if (result != MOVE_UNDEFINED)
8683 break;
8684 }
8685 else if (BUFFER_POS_REACHED_P ())
8686 {
8687 buffer_pos_reached:
8688 IT_RESET_X_ASCENT_DESCENT (it);
8689 result = MOVE_POS_MATCH_OR_ZV;
8690 break;
8691 }
8692 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8693 {
8694 /* Stop when TO_X specified and reached. This check is
8695 necessary here because of lines consisting of a line end,
8696 only. The line end will not produce any glyphs and we
8697 would never get MOVE_X_REACHED. */
8698 eassert (it->nglyphs == 0);
8699 result = MOVE_X_REACHED;
8700 break;
8701 }
8702
8703 /* Is this a line end? If yes, we're done. */
8704 if (ITERATOR_AT_END_OF_LINE_P (it))
8705 {
8706 /* If we are past TO_CHARPOS, but never saw any character
8707 positions smaller than TO_CHARPOS, return
8708 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8709 did. */
8710 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8711 {
8712 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8713 {
8714 if (closest_pos < ZV)
8715 {
8716 RESTORE_IT (it, &ppos_it, ppos_data);
8717 move_it_in_display_line_to (it, closest_pos, -1,
8718 MOVE_TO_POS);
8719 result = MOVE_POS_MATCH_OR_ZV;
8720 }
8721 else
8722 goto buffer_pos_reached;
8723 }
8724 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8725 && IT_CHARPOS (*it) > to_charpos)
8726 goto buffer_pos_reached;
8727 else
8728 result = MOVE_NEWLINE_OR_CR;
8729 }
8730 else
8731 result = MOVE_NEWLINE_OR_CR;
8732 break;
8733 }
8734
8735 prev_method = it->method;
8736 if (it->method == GET_FROM_BUFFER)
8737 prev_pos = IT_CHARPOS (*it);
8738 /* The current display element has been consumed. Advance
8739 to the next. */
8740 set_iterator_to_next (it, 1);
8741 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8742 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8743 if (IT_CHARPOS (*it) < to_charpos)
8744 saw_smaller_pos = 1;
8745 if (it->bidi_p
8746 && (op & MOVE_TO_POS)
8747 && IT_CHARPOS (*it) >= to_charpos
8748 && IT_CHARPOS (*it) < closest_pos)
8749 closest_pos = IT_CHARPOS (*it);
8750
8751 /* Stop if lines are truncated and IT's current x-position is
8752 past the right edge of the window now. */
8753 if (it->line_wrap == TRUNCATE
8754 && it->current_x >= it->last_visible_x)
8755 {
8756 if (!FRAME_WINDOW_P (it->f)
8757 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8758 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8759 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8760 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8761 {
8762 int at_eob_p = 0;
8763
8764 if ((at_eob_p = !get_next_display_element (it))
8765 || BUFFER_POS_REACHED_P ()
8766 /* If we are past TO_CHARPOS, but never saw any
8767 character positions smaller than TO_CHARPOS,
8768 return MOVE_POS_MATCH_OR_ZV, like the
8769 unidirectional display did. */
8770 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8771 && !saw_smaller_pos
8772 && IT_CHARPOS (*it) > to_charpos))
8773 {
8774 if (it->bidi_p
8775 && !BUFFER_POS_REACHED_P ()
8776 && !at_eob_p && closest_pos < ZV)
8777 {
8778 RESTORE_IT (it, &ppos_it, ppos_data);
8779 move_it_in_display_line_to (it, closest_pos, -1,
8780 MOVE_TO_POS);
8781 }
8782 result = MOVE_POS_MATCH_OR_ZV;
8783 break;
8784 }
8785 if (ITERATOR_AT_END_OF_LINE_P (it))
8786 {
8787 result = MOVE_NEWLINE_OR_CR;
8788 break;
8789 }
8790 }
8791 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8792 && !saw_smaller_pos
8793 && IT_CHARPOS (*it) > to_charpos)
8794 {
8795 if (closest_pos < ZV)
8796 {
8797 RESTORE_IT (it, &ppos_it, ppos_data);
8798 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8799 }
8800 result = MOVE_POS_MATCH_OR_ZV;
8801 break;
8802 }
8803 result = MOVE_LINE_TRUNCATED;
8804 break;
8805 }
8806 #undef IT_RESET_X_ASCENT_DESCENT
8807 }
8808
8809 #undef BUFFER_POS_REACHED_P
8810
8811 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8812 restore the saved iterator. */
8813 if (atpos_it.sp >= 0)
8814 RESTORE_IT (it, &atpos_it, atpos_data);
8815 else if (atx_it.sp >= 0)
8816 RESTORE_IT (it, &atx_it, atx_data);
8817
8818 done:
8819
8820 if (atpos_data)
8821 bidi_unshelve_cache (atpos_data, 1);
8822 if (atx_data)
8823 bidi_unshelve_cache (atx_data, 1);
8824 if (wrap_data)
8825 bidi_unshelve_cache (wrap_data, 1);
8826 if (ppos_data)
8827 bidi_unshelve_cache (ppos_data, 1);
8828
8829 /* Restore the iterator settings altered at the beginning of this
8830 function. */
8831 it->glyph_row = saved_glyph_row;
8832 return result;
8833 }
8834
8835 /* For external use. */
8836 void
8837 move_it_in_display_line (struct it *it,
8838 ptrdiff_t to_charpos, int to_x,
8839 enum move_operation_enum op)
8840 {
8841 if (it->line_wrap == WORD_WRAP
8842 && (op & MOVE_TO_X))
8843 {
8844 struct it save_it;
8845 void *save_data = NULL;
8846 int skip;
8847
8848 SAVE_IT (save_it, *it, save_data);
8849 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8850 /* When word-wrap is on, TO_X may lie past the end
8851 of a wrapped line. Then it->current is the
8852 character on the next line, so backtrack to the
8853 space before the wrap point. */
8854 if (skip == MOVE_LINE_CONTINUED)
8855 {
8856 int prev_x = max (it->current_x - 1, 0);
8857 RESTORE_IT (it, &save_it, save_data);
8858 move_it_in_display_line_to
8859 (it, -1, prev_x, MOVE_TO_X);
8860 }
8861 else
8862 bidi_unshelve_cache (save_data, 1);
8863 }
8864 else
8865 move_it_in_display_line_to (it, to_charpos, to_x, op);
8866 }
8867
8868
8869 /* Move IT forward until it satisfies one or more of the criteria in
8870 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8871
8872 OP is a bit-mask that specifies where to stop, and in particular,
8873 which of those four position arguments makes a difference. See the
8874 description of enum move_operation_enum.
8875
8876 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8877 screen line, this function will set IT to the next position that is
8878 displayed to the right of TO_CHARPOS on the screen.
8879
8880 Return the maximum pixel length of any line scanned but never more
8881 than it.last_visible_x. */
8882
8883 int
8884 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8885 {
8886 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8887 int line_height, line_start_x = 0, reached = 0;
8888 int max_current_x = 0;
8889 void *backup_data = NULL;
8890
8891 for (;;)
8892 {
8893 if (op & MOVE_TO_VPOS)
8894 {
8895 /* If no TO_CHARPOS and no TO_X specified, stop at the
8896 start of the line TO_VPOS. */
8897 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8898 {
8899 if (it->vpos == to_vpos)
8900 {
8901 reached = 1;
8902 break;
8903 }
8904 else
8905 skip = move_it_in_display_line_to (it, -1, -1, 0);
8906 }
8907 else
8908 {
8909 /* TO_VPOS >= 0 means stop at TO_X in the line at
8910 TO_VPOS, or at TO_POS, whichever comes first. */
8911 if (it->vpos == to_vpos)
8912 {
8913 reached = 2;
8914 break;
8915 }
8916
8917 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8918
8919 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8920 {
8921 reached = 3;
8922 break;
8923 }
8924 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8925 {
8926 /* We have reached TO_X but not in the line we want. */
8927 skip = move_it_in_display_line_to (it, to_charpos,
8928 -1, MOVE_TO_POS);
8929 if (skip == MOVE_POS_MATCH_OR_ZV)
8930 {
8931 reached = 4;
8932 break;
8933 }
8934 }
8935 }
8936 }
8937 else if (op & MOVE_TO_Y)
8938 {
8939 struct it it_backup;
8940
8941 if (it->line_wrap == WORD_WRAP)
8942 SAVE_IT (it_backup, *it, backup_data);
8943
8944 /* TO_Y specified means stop at TO_X in the line containing
8945 TO_Y---or at TO_CHARPOS if this is reached first. The
8946 problem is that we can't really tell whether the line
8947 contains TO_Y before we have completely scanned it, and
8948 this may skip past TO_X. What we do is to first scan to
8949 TO_X.
8950
8951 If TO_X is not specified, use a TO_X of zero. The reason
8952 is to make the outcome of this function more predictable.
8953 If we didn't use TO_X == 0, we would stop at the end of
8954 the line which is probably not what a caller would expect
8955 to happen. */
8956 skip = move_it_in_display_line_to
8957 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8958 (MOVE_TO_X | (op & MOVE_TO_POS)));
8959
8960 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8961 if (skip == MOVE_POS_MATCH_OR_ZV)
8962 reached = 5;
8963 else if (skip == MOVE_X_REACHED)
8964 {
8965 /* If TO_X was reached, we want to know whether TO_Y is
8966 in the line. We know this is the case if the already
8967 scanned glyphs make the line tall enough. Otherwise,
8968 we must check by scanning the rest of the line. */
8969 line_height = it->max_ascent + it->max_descent;
8970 if (to_y >= it->current_y
8971 && to_y < it->current_y + line_height)
8972 {
8973 reached = 6;
8974 break;
8975 }
8976 SAVE_IT (it_backup, *it, backup_data);
8977 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8978 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8979 op & MOVE_TO_POS);
8980 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8981 line_height = it->max_ascent + it->max_descent;
8982 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8983
8984 if (to_y >= it->current_y
8985 && to_y < it->current_y + line_height)
8986 {
8987 /* If TO_Y is in this line and TO_X was reached
8988 above, we scanned too far. We have to restore
8989 IT's settings to the ones before skipping. But
8990 keep the more accurate values of max_ascent and
8991 max_descent we've found while skipping the rest
8992 of the line, for the sake of callers, such as
8993 pos_visible_p, that need to know the line
8994 height. */
8995 int max_ascent = it->max_ascent;
8996 int max_descent = it->max_descent;
8997
8998 RESTORE_IT (it, &it_backup, backup_data);
8999 it->max_ascent = max_ascent;
9000 it->max_descent = max_descent;
9001 reached = 6;
9002 }
9003 else
9004 {
9005 skip = skip2;
9006 if (skip == MOVE_POS_MATCH_OR_ZV)
9007 reached = 7;
9008 }
9009 }
9010 else
9011 {
9012 /* Check whether TO_Y is in this line. */
9013 line_height = it->max_ascent + it->max_descent;
9014 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9015
9016 if (to_y >= it->current_y
9017 && to_y < it->current_y + line_height)
9018 {
9019 if (to_y > it->current_y)
9020 max_current_x = max (it->current_x, max_current_x);
9021
9022 /* When word-wrap is on, TO_X may lie past the end
9023 of a wrapped line. Then it->current is the
9024 character on the next line, so backtrack to the
9025 space before the wrap point. */
9026 if (skip == MOVE_LINE_CONTINUED
9027 && it->line_wrap == WORD_WRAP)
9028 {
9029 int prev_x = max (it->current_x - 1, 0);
9030 RESTORE_IT (it, &it_backup, backup_data);
9031 skip = move_it_in_display_line_to
9032 (it, -1, prev_x, MOVE_TO_X);
9033 }
9034
9035 reached = 6;
9036 }
9037 }
9038
9039 if (reached)
9040 {
9041 max_current_x = max (it->current_x, max_current_x);
9042 break;
9043 }
9044 }
9045 else if (BUFFERP (it->object)
9046 && (it->method == GET_FROM_BUFFER
9047 || it->method == GET_FROM_STRETCH)
9048 && IT_CHARPOS (*it) >= to_charpos
9049 /* Under bidi iteration, a call to set_iterator_to_next
9050 can scan far beyond to_charpos if the initial
9051 portion of the next line needs to be reordered. In
9052 that case, give move_it_in_display_line_to another
9053 chance below. */
9054 && !(it->bidi_p
9055 && it->bidi_it.scan_dir == -1))
9056 skip = MOVE_POS_MATCH_OR_ZV;
9057 else
9058 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9059
9060 switch (skip)
9061 {
9062 case MOVE_POS_MATCH_OR_ZV:
9063 max_current_x = max (it->current_x, max_current_x);
9064 reached = 8;
9065 goto out;
9066
9067 case MOVE_NEWLINE_OR_CR:
9068 max_current_x = max (it->current_x, max_current_x);
9069 set_iterator_to_next (it, 1);
9070 it->continuation_lines_width = 0;
9071 break;
9072
9073 case MOVE_LINE_TRUNCATED:
9074 max_current_x = it->last_visible_x;
9075 it->continuation_lines_width = 0;
9076 reseat_at_next_visible_line_start (it, 0);
9077 if ((op & MOVE_TO_POS) != 0
9078 && IT_CHARPOS (*it) > to_charpos)
9079 {
9080 reached = 9;
9081 goto out;
9082 }
9083 break;
9084
9085 case MOVE_LINE_CONTINUED:
9086 max_current_x = it->last_visible_x;
9087 /* For continued lines ending in a tab, some of the glyphs
9088 associated with the tab are displayed on the current
9089 line. Since it->current_x does not include these glyphs,
9090 we use it->last_visible_x instead. */
9091 if (it->c == '\t')
9092 {
9093 it->continuation_lines_width += it->last_visible_x;
9094 /* When moving by vpos, ensure that the iterator really
9095 advances to the next line (bug#847, bug#969). Fixme:
9096 do we need to do this in other circumstances? */
9097 if (it->current_x != it->last_visible_x
9098 && (op & MOVE_TO_VPOS)
9099 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9100 {
9101 line_start_x = it->current_x + it->pixel_width
9102 - it->last_visible_x;
9103 set_iterator_to_next (it, 0);
9104 }
9105 }
9106 else
9107 it->continuation_lines_width += it->current_x;
9108 break;
9109
9110 default:
9111 emacs_abort ();
9112 }
9113
9114 /* Reset/increment for the next run. */
9115 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9116 it->current_x = line_start_x;
9117 line_start_x = 0;
9118 it->hpos = 0;
9119 it->current_y += it->max_ascent + it->max_descent;
9120 ++it->vpos;
9121 last_height = it->max_ascent + it->max_descent;
9122 it->max_ascent = it->max_descent = 0;
9123 }
9124
9125 out:
9126
9127 /* On text terminals, we may stop at the end of a line in the middle
9128 of a multi-character glyph. If the glyph itself is continued,
9129 i.e. it is actually displayed on the next line, don't treat this
9130 stopping point as valid; move to the next line instead (unless
9131 that brings us offscreen). */
9132 if (!FRAME_WINDOW_P (it->f)
9133 && op & MOVE_TO_POS
9134 && IT_CHARPOS (*it) == to_charpos
9135 && it->what == IT_CHARACTER
9136 && it->nglyphs > 1
9137 && it->line_wrap == WINDOW_WRAP
9138 && it->current_x == it->last_visible_x - 1
9139 && it->c != '\n'
9140 && it->c != '\t'
9141 && it->vpos < it->w->window_end_vpos)
9142 {
9143 it->continuation_lines_width += it->current_x;
9144 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9145 it->current_y += it->max_ascent + it->max_descent;
9146 ++it->vpos;
9147 last_height = it->max_ascent + it->max_descent;
9148 }
9149
9150 if (backup_data)
9151 bidi_unshelve_cache (backup_data, 1);
9152
9153 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9154
9155 return max_current_x;
9156 }
9157
9158
9159 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9160
9161 If DY > 0, move IT backward at least that many pixels. DY = 0
9162 means move IT backward to the preceding line start or BEGV. This
9163 function may move over more than DY pixels if IT->current_y - DY
9164 ends up in the middle of a line; in this case IT->current_y will be
9165 set to the top of the line moved to. */
9166
9167 void
9168 move_it_vertically_backward (struct it *it, int dy)
9169 {
9170 int nlines, h;
9171 struct it it2, it3;
9172 void *it2data = NULL, *it3data = NULL;
9173 ptrdiff_t start_pos;
9174 int nchars_per_row
9175 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9176 ptrdiff_t pos_limit;
9177
9178 move_further_back:
9179 eassert (dy >= 0);
9180
9181 start_pos = IT_CHARPOS (*it);
9182
9183 /* Estimate how many newlines we must move back. */
9184 nlines = max (1, dy / default_line_pixel_height (it->w));
9185 if (it->line_wrap == TRUNCATE)
9186 pos_limit = BEGV;
9187 else
9188 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9189
9190 /* Set the iterator's position that many lines back. But don't go
9191 back more than NLINES full screen lines -- this wins a day with
9192 buffers which have very long lines. */
9193 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9194 back_to_previous_visible_line_start (it);
9195
9196 /* Reseat the iterator here. When moving backward, we don't want
9197 reseat to skip forward over invisible text, set up the iterator
9198 to deliver from overlay strings at the new position etc. So,
9199 use reseat_1 here. */
9200 reseat_1 (it, it->current.pos, 1);
9201
9202 /* We are now surely at a line start. */
9203 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9204 reordering is in effect. */
9205 it->continuation_lines_width = 0;
9206
9207 /* Move forward and see what y-distance we moved. First move to the
9208 start of the next line so that we get its height. We need this
9209 height to be able to tell whether we reached the specified
9210 y-distance. */
9211 SAVE_IT (it2, *it, it2data);
9212 it2.max_ascent = it2.max_descent = 0;
9213 do
9214 {
9215 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9216 MOVE_TO_POS | MOVE_TO_VPOS);
9217 }
9218 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9219 /* If we are in a display string which starts at START_POS,
9220 and that display string includes a newline, and we are
9221 right after that newline (i.e. at the beginning of a
9222 display line), exit the loop, because otherwise we will
9223 infloop, since move_it_to will see that it is already at
9224 START_POS and will not move. */
9225 || (it2.method == GET_FROM_STRING
9226 && IT_CHARPOS (it2) == start_pos
9227 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9228 eassert (IT_CHARPOS (*it) >= BEGV);
9229 SAVE_IT (it3, it2, it3data);
9230
9231 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9232 eassert (IT_CHARPOS (*it) >= BEGV);
9233 /* H is the actual vertical distance from the position in *IT
9234 and the starting position. */
9235 h = it2.current_y - it->current_y;
9236 /* NLINES is the distance in number of lines. */
9237 nlines = it2.vpos - it->vpos;
9238
9239 /* Correct IT's y and vpos position
9240 so that they are relative to the starting point. */
9241 it->vpos -= nlines;
9242 it->current_y -= h;
9243
9244 if (dy == 0)
9245 {
9246 /* DY == 0 means move to the start of the screen line. The
9247 value of nlines is > 0 if continuation lines were involved,
9248 or if the original IT position was at start of a line. */
9249 RESTORE_IT (it, it, it2data);
9250 if (nlines > 0)
9251 move_it_by_lines (it, nlines);
9252 /* The above code moves us to some position NLINES down,
9253 usually to its first glyph (leftmost in an L2R line), but
9254 that's not necessarily the start of the line, under bidi
9255 reordering. We want to get to the character position
9256 that is immediately after the newline of the previous
9257 line. */
9258 if (it->bidi_p
9259 && !it->continuation_lines_width
9260 && !STRINGP (it->string)
9261 && IT_CHARPOS (*it) > BEGV
9262 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9263 {
9264 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9265
9266 DEC_BOTH (cp, bp);
9267 cp = find_newline_no_quit (cp, bp, -1, NULL);
9268 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9269 }
9270 bidi_unshelve_cache (it3data, 1);
9271 }
9272 else
9273 {
9274 /* The y-position we try to reach, relative to *IT.
9275 Note that H has been subtracted in front of the if-statement. */
9276 int target_y = it->current_y + h - dy;
9277 int y0 = it3.current_y;
9278 int y1;
9279 int line_height;
9280
9281 RESTORE_IT (&it3, &it3, it3data);
9282 y1 = line_bottom_y (&it3);
9283 line_height = y1 - y0;
9284 RESTORE_IT (it, it, it2data);
9285 /* If we did not reach target_y, try to move further backward if
9286 we can. If we moved too far backward, try to move forward. */
9287 if (target_y < it->current_y
9288 /* This is heuristic. In a window that's 3 lines high, with
9289 a line height of 13 pixels each, recentering with point
9290 on the bottom line will try to move -39/2 = 19 pixels
9291 backward. Try to avoid moving into the first line. */
9292 && (it->current_y - target_y
9293 > min (window_box_height (it->w), line_height * 2 / 3))
9294 && IT_CHARPOS (*it) > BEGV)
9295 {
9296 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9297 target_y - it->current_y));
9298 dy = it->current_y - target_y;
9299 goto move_further_back;
9300 }
9301 else if (target_y >= it->current_y + line_height
9302 && IT_CHARPOS (*it) < ZV)
9303 {
9304 /* Should move forward by at least one line, maybe more.
9305
9306 Note: Calling move_it_by_lines can be expensive on
9307 terminal frames, where compute_motion is used (via
9308 vmotion) to do the job, when there are very long lines
9309 and truncate-lines is nil. That's the reason for
9310 treating terminal frames specially here. */
9311
9312 if (!FRAME_WINDOW_P (it->f))
9313 move_it_vertically (it, target_y - (it->current_y + line_height));
9314 else
9315 {
9316 do
9317 {
9318 move_it_by_lines (it, 1);
9319 }
9320 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9321 }
9322 }
9323 }
9324 }
9325
9326
9327 /* Move IT by a specified amount of pixel lines DY. DY negative means
9328 move backwards. DY = 0 means move to start of screen line. At the
9329 end, IT will be on the start of a screen line. */
9330
9331 void
9332 move_it_vertically (struct it *it, int dy)
9333 {
9334 if (dy <= 0)
9335 move_it_vertically_backward (it, -dy);
9336 else
9337 {
9338 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9339 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9340 MOVE_TO_POS | MOVE_TO_Y);
9341 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9342
9343 /* If buffer ends in ZV without a newline, move to the start of
9344 the line to satisfy the post-condition. */
9345 if (IT_CHARPOS (*it) == ZV
9346 && ZV > BEGV
9347 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9348 move_it_by_lines (it, 0);
9349 }
9350 }
9351
9352
9353 /* Move iterator IT past the end of the text line it is in. */
9354
9355 void
9356 move_it_past_eol (struct it *it)
9357 {
9358 enum move_it_result rc;
9359
9360 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9361 if (rc == MOVE_NEWLINE_OR_CR)
9362 set_iterator_to_next (it, 0);
9363 }
9364
9365
9366 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9367 negative means move up. DVPOS == 0 means move to the start of the
9368 screen line.
9369
9370 Optimization idea: If we would know that IT->f doesn't use
9371 a face with proportional font, we could be faster for
9372 truncate-lines nil. */
9373
9374 void
9375 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9376 {
9377
9378 /* The commented-out optimization uses vmotion on terminals. This
9379 gives bad results, because elements like it->what, on which
9380 callers such as pos_visible_p rely, aren't updated. */
9381 /* struct position pos;
9382 if (!FRAME_WINDOW_P (it->f))
9383 {
9384 struct text_pos textpos;
9385
9386 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9387 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9388 reseat (it, textpos, 1);
9389 it->vpos += pos.vpos;
9390 it->current_y += pos.vpos;
9391 }
9392 else */
9393
9394 if (dvpos == 0)
9395 {
9396 /* DVPOS == 0 means move to the start of the screen line. */
9397 move_it_vertically_backward (it, 0);
9398 /* Let next call to line_bottom_y calculate real line height. */
9399 last_height = 0;
9400 }
9401 else if (dvpos > 0)
9402 {
9403 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9404 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9405 {
9406 /* Only move to the next buffer position if we ended up in a
9407 string from display property, not in an overlay string
9408 (before-string or after-string). That is because the
9409 latter don't conceal the underlying buffer position, so
9410 we can ask to move the iterator to the exact position we
9411 are interested in. Note that, even if we are already at
9412 IT_CHARPOS (*it), the call below is not a no-op, as it
9413 will detect that we are at the end of the string, pop the
9414 iterator, and compute it->current_x and it->hpos
9415 correctly. */
9416 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9417 -1, -1, -1, MOVE_TO_POS);
9418 }
9419 }
9420 else
9421 {
9422 struct it it2;
9423 void *it2data = NULL;
9424 ptrdiff_t start_charpos, i;
9425 int nchars_per_row
9426 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9427 ptrdiff_t pos_limit;
9428
9429 /* Start at the beginning of the screen line containing IT's
9430 position. This may actually move vertically backwards,
9431 in case of overlays, so adjust dvpos accordingly. */
9432 dvpos += it->vpos;
9433 move_it_vertically_backward (it, 0);
9434 dvpos -= it->vpos;
9435
9436 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9437 screen lines, and reseat the iterator there. */
9438 start_charpos = IT_CHARPOS (*it);
9439 if (it->line_wrap == TRUNCATE)
9440 pos_limit = BEGV;
9441 else
9442 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9443 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9444 back_to_previous_visible_line_start (it);
9445 reseat (it, it->current.pos, 1);
9446
9447 /* Move further back if we end up in a string or an image. */
9448 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9449 {
9450 /* First try to move to start of display line. */
9451 dvpos += it->vpos;
9452 move_it_vertically_backward (it, 0);
9453 dvpos -= it->vpos;
9454 if (IT_POS_VALID_AFTER_MOVE_P (it))
9455 break;
9456 /* If start of line is still in string or image,
9457 move further back. */
9458 back_to_previous_visible_line_start (it);
9459 reseat (it, it->current.pos, 1);
9460 dvpos--;
9461 }
9462
9463 it->current_x = it->hpos = 0;
9464
9465 /* Above call may have moved too far if continuation lines
9466 are involved. Scan forward and see if it did. */
9467 SAVE_IT (it2, *it, it2data);
9468 it2.vpos = it2.current_y = 0;
9469 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9470 it->vpos -= it2.vpos;
9471 it->current_y -= it2.current_y;
9472 it->current_x = it->hpos = 0;
9473
9474 /* If we moved too far back, move IT some lines forward. */
9475 if (it2.vpos > -dvpos)
9476 {
9477 int delta = it2.vpos + dvpos;
9478
9479 RESTORE_IT (&it2, &it2, it2data);
9480 SAVE_IT (it2, *it, it2data);
9481 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9482 /* Move back again if we got too far ahead. */
9483 if (IT_CHARPOS (*it) >= start_charpos)
9484 RESTORE_IT (it, &it2, it2data);
9485 else
9486 bidi_unshelve_cache (it2data, 1);
9487 }
9488 else
9489 RESTORE_IT (it, it, it2data);
9490 }
9491 }
9492
9493 /* Return true if IT points into the middle of a display vector. */
9494
9495 bool
9496 in_display_vector_p (struct it *it)
9497 {
9498 return (it->method == GET_FROM_DISPLAY_VECTOR
9499 && it->current.dpvec_index > 0
9500 && it->dpvec + it->current.dpvec_index != it->dpend);
9501 }
9502
9503 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9504 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9505 WINDOW must be a live window and defaults to the selected one. The
9506 return value is a cons of the maximum pixel-width of any text line and
9507 the maximum pixel-height of all text lines.
9508
9509 The optional argument FROM, if non-nil, specifies the first text
9510 position and defaults to the minimum accessible position of the buffer.
9511 If FROM is t, use the minimum accessible position that is not a newline
9512 character. TO, if non-nil, specifies the last text position and
9513 defaults to the maximum accessible position of the buffer. If TO is t,
9514 use the maximum accessible position that is not a newline character.
9515
9516 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9517 width that can be returned. X_LIMIT nil or omitted, means to use the
9518 pixel-width of WINDOW's body; use this if you do not intend to change
9519 the width of WINDOW. Use the maximum width WINDOW may assume if you
9520 intend to change WINDOW's width.
9521
9522 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9523 height that can be returned. Text lines whose y-coordinate is beyond
9524 Y_LIMIT are ignored. Since calculating the text height of a large
9525 buffer can take some time, it makes sense to specify this argument if
9526 the size of the buffer is unknown.
9527
9528 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9529 include the height of the mode- or header-line of WINDOW in the return
9530 value. If it is either the symbol `mode-line' or `header-line', include
9531 only the height of that line, if present, in the return value. If t,
9532 include the height of both, if present, in the return value. */)
9533 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9534 Lisp_Object mode_and_header_line)
9535 {
9536 struct window *w = decode_live_window (window);
9537 Lisp_Object buf;
9538 struct buffer *b;
9539 struct it it;
9540 struct buffer *old_buffer = NULL;
9541 ptrdiff_t start, end, pos;
9542 struct text_pos startp;
9543 void *itdata = NULL;
9544 int c, max_y = -1, x = 0, y = 0;
9545
9546 buf = w->contents;
9547 CHECK_BUFFER (buf);
9548 b = XBUFFER (buf);
9549
9550 if (b != current_buffer)
9551 {
9552 old_buffer = current_buffer;
9553 set_buffer_internal (b);
9554 }
9555
9556 if (NILP (from))
9557 start = BEGV;
9558 else if (EQ (from, Qt))
9559 {
9560 start = pos = BEGV;
9561 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9562 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9563 start = pos;
9564 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9565 start = pos;
9566 }
9567 else
9568 {
9569 CHECK_NUMBER_COERCE_MARKER (from);
9570 start = min (max (XINT (from), BEGV), ZV);
9571 }
9572
9573 if (NILP (to))
9574 end = ZV;
9575 else if (EQ (to, Qt))
9576 {
9577 end = pos = ZV;
9578 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9579 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9580 end = pos;
9581 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9582 end = pos;
9583 }
9584 else
9585 {
9586 CHECK_NUMBER_COERCE_MARKER (to);
9587 end = max (start, min (XINT (to), ZV));
9588 }
9589
9590 if (!NILP (y_limit))
9591 {
9592 CHECK_NUMBER (y_limit);
9593 max_y = min (XINT (y_limit), INT_MAX);
9594 }
9595
9596 itdata = bidi_shelve_cache ();
9597 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9598 start_display (&it, w, startp);
9599
9600 if (NILP (x_limit))
9601 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9602 else
9603 {
9604 CHECK_NUMBER (x_limit);
9605 it.last_visible_x = min (XINT (x_limit), INFINITY);
9606 /* Actually, we never want move_it_to stop at to_x. But to make
9607 sure that move_it_in_display_line_to always moves far enough,
9608 we set it to INT_MAX and specify MOVE_TO_X. */
9609 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9610 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9611 }
9612
9613 y = it.current_y + it.max_ascent + it.max_descent;
9614
9615 if (!EQ (mode_and_header_line, Qheader_line)
9616 && !EQ (mode_and_header_line, Qt))
9617 /* Do not count the header-line which was counted automatically by
9618 start_display. */
9619 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9620
9621 if (EQ (mode_and_header_line, Qmode_line)
9622 || EQ (mode_and_header_line, Qt))
9623 /* Do count the mode-line which is not included automatically by
9624 start_display. */
9625 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9626
9627 bidi_unshelve_cache (itdata, 0);
9628
9629 if (old_buffer)
9630 set_buffer_internal (old_buffer);
9631
9632 return Fcons (make_number (x), make_number (y));
9633 }
9634 \f
9635 /***********************************************************************
9636 Messages
9637 ***********************************************************************/
9638
9639
9640 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9641 to *Messages*. */
9642
9643 void
9644 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9645 {
9646 Lisp_Object args[3];
9647 Lisp_Object msg, fmt;
9648 char *buffer;
9649 ptrdiff_t len;
9650 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9651 USE_SAFE_ALLOCA;
9652
9653 fmt = msg = Qnil;
9654 GCPRO4 (fmt, msg, arg1, arg2);
9655
9656 args[0] = fmt = build_string (format);
9657 args[1] = arg1;
9658 args[2] = arg2;
9659 msg = Fformat (3, args);
9660
9661 len = SBYTES (msg) + 1;
9662 buffer = SAFE_ALLOCA (len);
9663 memcpy (buffer, SDATA (msg), len);
9664
9665 message_dolog (buffer, len - 1, 1, 0);
9666 SAFE_FREE ();
9667
9668 UNGCPRO;
9669 }
9670
9671
9672 /* Output a newline in the *Messages* buffer if "needs" one. */
9673
9674 void
9675 message_log_maybe_newline (void)
9676 {
9677 if (message_log_need_newline)
9678 message_dolog ("", 0, 1, 0);
9679 }
9680
9681
9682 /* Add a string M of length NBYTES to the message log, optionally
9683 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9684 true, means interpret the contents of M as multibyte. This
9685 function calls low-level routines in order to bypass text property
9686 hooks, etc. which might not be safe to run.
9687
9688 This may GC (insert may run before/after change hooks),
9689 so the buffer M must NOT point to a Lisp string. */
9690
9691 void
9692 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9693 {
9694 const unsigned char *msg = (const unsigned char *) m;
9695
9696 if (!NILP (Vmemory_full))
9697 return;
9698
9699 if (!NILP (Vmessage_log_max))
9700 {
9701 struct buffer *oldbuf;
9702 Lisp_Object oldpoint, oldbegv, oldzv;
9703 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9704 ptrdiff_t point_at_end = 0;
9705 ptrdiff_t zv_at_end = 0;
9706 Lisp_Object old_deactivate_mark;
9707 struct gcpro gcpro1;
9708
9709 old_deactivate_mark = Vdeactivate_mark;
9710 oldbuf = current_buffer;
9711
9712 /* Ensure the Messages buffer exists, and switch to it.
9713 If we created it, set the major-mode. */
9714 {
9715 int newbuffer = 0;
9716 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9717
9718 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9719
9720 if (newbuffer
9721 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9722 call0 (intern ("messages-buffer-mode"));
9723 }
9724
9725 bset_undo_list (current_buffer, Qt);
9726 bset_cache_long_scans (current_buffer, Qnil);
9727
9728 oldpoint = message_dolog_marker1;
9729 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9730 oldbegv = message_dolog_marker2;
9731 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9732 oldzv = message_dolog_marker3;
9733 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9734 GCPRO1 (old_deactivate_mark);
9735
9736 if (PT == Z)
9737 point_at_end = 1;
9738 if (ZV == Z)
9739 zv_at_end = 1;
9740
9741 BEGV = BEG;
9742 BEGV_BYTE = BEG_BYTE;
9743 ZV = Z;
9744 ZV_BYTE = Z_BYTE;
9745 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9746
9747 /* Insert the string--maybe converting multibyte to single byte
9748 or vice versa, so that all the text fits the buffer. */
9749 if (multibyte
9750 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9751 {
9752 ptrdiff_t i;
9753 int c, char_bytes;
9754 char work[1];
9755
9756 /* Convert a multibyte string to single-byte
9757 for the *Message* buffer. */
9758 for (i = 0; i < nbytes; i += char_bytes)
9759 {
9760 c = string_char_and_length (msg + i, &char_bytes);
9761 work[0] = (ASCII_CHAR_P (c)
9762 ? c
9763 : multibyte_char_to_unibyte (c));
9764 insert_1_both (work, 1, 1, 1, 0, 0);
9765 }
9766 }
9767 else if (! multibyte
9768 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9769 {
9770 ptrdiff_t i;
9771 int c, char_bytes;
9772 unsigned char str[MAX_MULTIBYTE_LENGTH];
9773 /* Convert a single-byte string to multibyte
9774 for the *Message* buffer. */
9775 for (i = 0; i < nbytes; i++)
9776 {
9777 c = msg[i];
9778 MAKE_CHAR_MULTIBYTE (c);
9779 char_bytes = CHAR_STRING (c, str);
9780 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9781 }
9782 }
9783 else if (nbytes)
9784 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9785
9786 if (nlflag)
9787 {
9788 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9789 printmax_t dups;
9790
9791 insert_1_both ("\n", 1, 1, 1, 0, 0);
9792
9793 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9794 this_bol = PT;
9795 this_bol_byte = PT_BYTE;
9796
9797 /* See if this line duplicates the previous one.
9798 If so, combine duplicates. */
9799 if (this_bol > BEG)
9800 {
9801 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9802 prev_bol = PT;
9803 prev_bol_byte = PT_BYTE;
9804
9805 dups = message_log_check_duplicate (prev_bol_byte,
9806 this_bol_byte);
9807 if (dups)
9808 {
9809 del_range_both (prev_bol, prev_bol_byte,
9810 this_bol, this_bol_byte, 0);
9811 if (dups > 1)
9812 {
9813 char dupstr[sizeof " [ times]"
9814 + INT_STRLEN_BOUND (printmax_t)];
9815
9816 /* If you change this format, don't forget to also
9817 change message_log_check_duplicate. */
9818 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9819 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9820 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9821 }
9822 }
9823 }
9824
9825 /* If we have more than the desired maximum number of lines
9826 in the *Messages* buffer now, delete the oldest ones.
9827 This is safe because we don't have undo in this buffer. */
9828
9829 if (NATNUMP (Vmessage_log_max))
9830 {
9831 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9832 -XFASTINT (Vmessage_log_max) - 1, 0);
9833 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9834 }
9835 }
9836 BEGV = marker_position (oldbegv);
9837 BEGV_BYTE = marker_byte_position (oldbegv);
9838
9839 if (zv_at_end)
9840 {
9841 ZV = Z;
9842 ZV_BYTE = Z_BYTE;
9843 }
9844 else
9845 {
9846 ZV = marker_position (oldzv);
9847 ZV_BYTE = marker_byte_position (oldzv);
9848 }
9849
9850 if (point_at_end)
9851 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9852 else
9853 /* We can't do Fgoto_char (oldpoint) because it will run some
9854 Lisp code. */
9855 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9856 marker_byte_position (oldpoint));
9857
9858 UNGCPRO;
9859 unchain_marker (XMARKER (oldpoint));
9860 unchain_marker (XMARKER (oldbegv));
9861 unchain_marker (XMARKER (oldzv));
9862
9863 /* We called insert_1_both above with its 5th argument (PREPARE)
9864 zero, which prevents insert_1_both from calling
9865 prepare_to_modify_buffer, which in turns prevents us from
9866 incrementing windows_or_buffers_changed even if *Messages* is
9867 shown in some window. So we must manually set
9868 windows_or_buffers_changed here to make up for that. */
9869 windows_or_buffers_changed = old_windows_or_buffers_changed;
9870 bset_redisplay (current_buffer);
9871
9872 set_buffer_internal (oldbuf);
9873
9874 message_log_need_newline = !nlflag;
9875 Vdeactivate_mark = old_deactivate_mark;
9876 }
9877 }
9878
9879
9880 /* We are at the end of the buffer after just having inserted a newline.
9881 (Note: We depend on the fact we won't be crossing the gap.)
9882 Check to see if the most recent message looks a lot like the previous one.
9883 Return 0 if different, 1 if the new one should just replace it, or a
9884 value N > 1 if we should also append " [N times]". */
9885
9886 static intmax_t
9887 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9888 {
9889 ptrdiff_t i;
9890 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9891 int seen_dots = 0;
9892 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9893 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9894
9895 for (i = 0; i < len; i++)
9896 {
9897 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9898 seen_dots = 1;
9899 if (p1[i] != p2[i])
9900 return seen_dots;
9901 }
9902 p1 += len;
9903 if (*p1 == '\n')
9904 return 2;
9905 if (*p1++ == ' ' && *p1++ == '[')
9906 {
9907 char *pend;
9908 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9909 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9910 return n + 1;
9911 }
9912 return 0;
9913 }
9914 \f
9915
9916 /* Display an echo area message M with a specified length of NBYTES
9917 bytes. The string may include null characters. If M is not a
9918 string, clear out any existing message, and let the mini-buffer
9919 text show through.
9920
9921 This function cancels echoing. */
9922
9923 void
9924 message3 (Lisp_Object m)
9925 {
9926 struct gcpro gcpro1;
9927
9928 GCPRO1 (m);
9929 clear_message (true, true);
9930 cancel_echoing ();
9931
9932 /* First flush out any partial line written with print. */
9933 message_log_maybe_newline ();
9934 if (STRINGP (m))
9935 {
9936 ptrdiff_t nbytes = SBYTES (m);
9937 bool multibyte = STRING_MULTIBYTE (m);
9938 USE_SAFE_ALLOCA;
9939 char *buffer = SAFE_ALLOCA (nbytes);
9940 memcpy (buffer, SDATA (m), nbytes);
9941 message_dolog (buffer, nbytes, 1, multibyte);
9942 SAFE_FREE ();
9943 }
9944 message3_nolog (m);
9945
9946 UNGCPRO;
9947 }
9948
9949
9950 /* The non-logging version of message3.
9951 This does not cancel echoing, because it is used for echoing.
9952 Perhaps we need to make a separate function for echoing
9953 and make this cancel echoing. */
9954
9955 void
9956 message3_nolog (Lisp_Object m)
9957 {
9958 struct frame *sf = SELECTED_FRAME ();
9959
9960 if (FRAME_INITIAL_P (sf))
9961 {
9962 if (noninteractive_need_newline)
9963 putc ('\n', stderr);
9964 noninteractive_need_newline = 0;
9965 if (STRINGP (m))
9966 {
9967 Lisp_Object s = ENCODE_SYSTEM (m);
9968
9969 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9970 }
9971 if (cursor_in_echo_area == 0)
9972 fprintf (stderr, "\n");
9973 fflush (stderr);
9974 }
9975 /* Error messages get reported properly by cmd_error, so this must be just an
9976 informative message; if the frame hasn't really been initialized yet, just
9977 toss it. */
9978 else if (INTERACTIVE && sf->glyphs_initialized_p)
9979 {
9980 /* Get the frame containing the mini-buffer
9981 that the selected frame is using. */
9982 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9983 Lisp_Object frame = XWINDOW (mini_window)->frame;
9984 struct frame *f = XFRAME (frame);
9985
9986 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9987 Fmake_frame_visible (frame);
9988
9989 if (STRINGP (m) && SCHARS (m) > 0)
9990 {
9991 set_message (m);
9992 if (minibuffer_auto_raise)
9993 Fraise_frame (frame);
9994 /* Assume we are not echoing.
9995 (If we are, echo_now will override this.) */
9996 echo_message_buffer = Qnil;
9997 }
9998 else
9999 clear_message (true, true);
10000
10001 do_pending_window_change (0);
10002 echo_area_display (1);
10003 do_pending_window_change (0);
10004 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10005 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10006 }
10007 }
10008
10009
10010 /* Display a null-terminated echo area message M. If M is 0, clear
10011 out any existing message, and let the mini-buffer text show through.
10012
10013 The buffer M must continue to exist until after the echo area gets
10014 cleared or some other message gets displayed there. Do not pass
10015 text that is stored in a Lisp string. Do not pass text in a buffer
10016 that was alloca'd. */
10017
10018 void
10019 message1 (const char *m)
10020 {
10021 message3 (m ? build_unibyte_string (m) : Qnil);
10022 }
10023
10024
10025 /* The non-logging counterpart of message1. */
10026
10027 void
10028 message1_nolog (const char *m)
10029 {
10030 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10031 }
10032
10033 /* Display a message M which contains a single %s
10034 which gets replaced with STRING. */
10035
10036 void
10037 message_with_string (const char *m, Lisp_Object string, int log)
10038 {
10039 CHECK_STRING (string);
10040
10041 if (noninteractive)
10042 {
10043 if (m)
10044 {
10045 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10046 String whose data pointer might be passed to us in M. So
10047 we use a local copy. */
10048 char *fmt = xstrdup (m);
10049
10050 if (noninteractive_need_newline)
10051 putc ('\n', stderr);
10052 noninteractive_need_newline = 0;
10053 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10054 if (!cursor_in_echo_area)
10055 fprintf (stderr, "\n");
10056 fflush (stderr);
10057 xfree (fmt);
10058 }
10059 }
10060 else if (INTERACTIVE)
10061 {
10062 /* The frame whose minibuffer we're going to display the message on.
10063 It may be larger than the selected frame, so we need
10064 to use its buffer, not the selected frame's buffer. */
10065 Lisp_Object mini_window;
10066 struct frame *f, *sf = SELECTED_FRAME ();
10067
10068 /* Get the frame containing the minibuffer
10069 that the selected frame is using. */
10070 mini_window = FRAME_MINIBUF_WINDOW (sf);
10071 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10072
10073 /* Error messages get reported properly by cmd_error, so this must be
10074 just an informative message; if the frame hasn't really been
10075 initialized yet, just toss it. */
10076 if (f->glyphs_initialized_p)
10077 {
10078 Lisp_Object args[2], msg;
10079 struct gcpro gcpro1, gcpro2;
10080
10081 args[0] = build_string (m);
10082 args[1] = msg = string;
10083 GCPRO2 (args[0], msg);
10084 gcpro1.nvars = 2;
10085
10086 msg = Fformat (2, args);
10087
10088 if (log)
10089 message3 (msg);
10090 else
10091 message3_nolog (msg);
10092
10093 UNGCPRO;
10094
10095 /* Print should start at the beginning of the message
10096 buffer next time. */
10097 message_buf_print = 0;
10098 }
10099 }
10100 }
10101
10102
10103 /* Dump an informative message to the minibuf. If M is 0, clear out
10104 any existing message, and let the mini-buffer text show through. */
10105
10106 static void
10107 vmessage (const char *m, va_list ap)
10108 {
10109 if (noninteractive)
10110 {
10111 if (m)
10112 {
10113 if (noninteractive_need_newline)
10114 putc ('\n', stderr);
10115 noninteractive_need_newline = 0;
10116 vfprintf (stderr, m, ap);
10117 if (cursor_in_echo_area == 0)
10118 fprintf (stderr, "\n");
10119 fflush (stderr);
10120 }
10121 }
10122 else if (INTERACTIVE)
10123 {
10124 /* The frame whose mini-buffer we're going to display the message
10125 on. It may be larger than the selected frame, so we need to
10126 use its buffer, not the selected frame's buffer. */
10127 Lisp_Object mini_window;
10128 struct frame *f, *sf = SELECTED_FRAME ();
10129
10130 /* Get the frame containing the mini-buffer
10131 that the selected frame is using. */
10132 mini_window = FRAME_MINIBUF_WINDOW (sf);
10133 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10134
10135 /* Error messages get reported properly by cmd_error, so this must be
10136 just an informative message; if the frame hasn't really been
10137 initialized yet, just toss it. */
10138 if (f->glyphs_initialized_p)
10139 {
10140 if (m)
10141 {
10142 ptrdiff_t len;
10143 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10144 char *message_buf = alloca (maxsize + 1);
10145
10146 len = doprnt (message_buf, maxsize, m, 0, ap);
10147
10148 message3 (make_string (message_buf, len));
10149 }
10150 else
10151 message1 (0);
10152
10153 /* Print should start at the beginning of the message
10154 buffer next time. */
10155 message_buf_print = 0;
10156 }
10157 }
10158 }
10159
10160 void
10161 message (const char *m, ...)
10162 {
10163 va_list ap;
10164 va_start (ap, m);
10165 vmessage (m, ap);
10166 va_end (ap);
10167 }
10168
10169
10170 #if 0
10171 /* The non-logging version of message. */
10172
10173 void
10174 message_nolog (const char *m, ...)
10175 {
10176 Lisp_Object old_log_max;
10177 va_list ap;
10178 va_start (ap, m);
10179 old_log_max = Vmessage_log_max;
10180 Vmessage_log_max = Qnil;
10181 vmessage (m, ap);
10182 Vmessage_log_max = old_log_max;
10183 va_end (ap);
10184 }
10185 #endif
10186
10187
10188 /* Display the current message in the current mini-buffer. This is
10189 only called from error handlers in process.c, and is not time
10190 critical. */
10191
10192 void
10193 update_echo_area (void)
10194 {
10195 if (!NILP (echo_area_buffer[0]))
10196 {
10197 Lisp_Object string;
10198 string = Fcurrent_message ();
10199 message3 (string);
10200 }
10201 }
10202
10203
10204 /* Make sure echo area buffers in `echo_buffers' are live.
10205 If they aren't, make new ones. */
10206
10207 static void
10208 ensure_echo_area_buffers (void)
10209 {
10210 int i;
10211
10212 for (i = 0; i < 2; ++i)
10213 if (!BUFFERP (echo_buffer[i])
10214 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10215 {
10216 char name[30];
10217 Lisp_Object old_buffer;
10218 int j;
10219
10220 old_buffer = echo_buffer[i];
10221 echo_buffer[i] = Fget_buffer_create
10222 (make_formatted_string (name, " *Echo Area %d*", i));
10223 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10224 /* to force word wrap in echo area -
10225 it was decided to postpone this*/
10226 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10227
10228 for (j = 0; j < 2; ++j)
10229 if (EQ (old_buffer, echo_area_buffer[j]))
10230 echo_area_buffer[j] = echo_buffer[i];
10231 }
10232 }
10233
10234
10235 /* Call FN with args A1..A2 with either the current or last displayed
10236 echo_area_buffer as current buffer.
10237
10238 WHICH zero means use the current message buffer
10239 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10240 from echo_buffer[] and clear it.
10241
10242 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10243 suitable buffer from echo_buffer[] and clear it.
10244
10245 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10246 that the current message becomes the last displayed one, make
10247 choose a suitable buffer for echo_area_buffer[0], and clear it.
10248
10249 Value is what FN returns. */
10250
10251 static int
10252 with_echo_area_buffer (struct window *w, int which,
10253 int (*fn) (ptrdiff_t, Lisp_Object),
10254 ptrdiff_t a1, Lisp_Object a2)
10255 {
10256 Lisp_Object buffer;
10257 int this_one, the_other, clear_buffer_p, rc;
10258 ptrdiff_t count = SPECPDL_INDEX ();
10259
10260 /* If buffers aren't live, make new ones. */
10261 ensure_echo_area_buffers ();
10262
10263 clear_buffer_p = 0;
10264
10265 if (which == 0)
10266 this_one = 0, the_other = 1;
10267 else if (which > 0)
10268 this_one = 1, the_other = 0;
10269 else
10270 {
10271 this_one = 0, the_other = 1;
10272 clear_buffer_p = true;
10273
10274 /* We need a fresh one in case the current echo buffer equals
10275 the one containing the last displayed echo area message. */
10276 if (!NILP (echo_area_buffer[this_one])
10277 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10278 echo_area_buffer[this_one] = Qnil;
10279 }
10280
10281 /* Choose a suitable buffer from echo_buffer[] is we don't
10282 have one. */
10283 if (NILP (echo_area_buffer[this_one]))
10284 {
10285 echo_area_buffer[this_one]
10286 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10287 ? echo_buffer[the_other]
10288 : echo_buffer[this_one]);
10289 clear_buffer_p = true;
10290 }
10291
10292 buffer = echo_area_buffer[this_one];
10293
10294 /* Don't get confused by reusing the buffer used for echoing
10295 for a different purpose. */
10296 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10297 cancel_echoing ();
10298
10299 record_unwind_protect (unwind_with_echo_area_buffer,
10300 with_echo_area_buffer_unwind_data (w));
10301
10302 /* Make the echo area buffer current. Note that for display
10303 purposes, it is not necessary that the displayed window's buffer
10304 == current_buffer, except for text property lookup. So, let's
10305 only set that buffer temporarily here without doing a full
10306 Fset_window_buffer. We must also change w->pointm, though,
10307 because otherwise an assertions in unshow_buffer fails, and Emacs
10308 aborts. */
10309 set_buffer_internal_1 (XBUFFER (buffer));
10310 if (w)
10311 {
10312 wset_buffer (w, buffer);
10313 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10314 }
10315
10316 bset_undo_list (current_buffer, Qt);
10317 bset_read_only (current_buffer, Qnil);
10318 specbind (Qinhibit_read_only, Qt);
10319 specbind (Qinhibit_modification_hooks, Qt);
10320
10321 if (clear_buffer_p && Z > BEG)
10322 del_range (BEG, Z);
10323
10324 eassert (BEGV >= BEG);
10325 eassert (ZV <= Z && ZV >= BEGV);
10326
10327 rc = fn (a1, a2);
10328
10329 eassert (BEGV >= BEG);
10330 eassert (ZV <= Z && ZV >= BEGV);
10331
10332 unbind_to (count, Qnil);
10333 return rc;
10334 }
10335
10336
10337 /* Save state that should be preserved around the call to the function
10338 FN called in with_echo_area_buffer. */
10339
10340 static Lisp_Object
10341 with_echo_area_buffer_unwind_data (struct window *w)
10342 {
10343 int i = 0;
10344 Lisp_Object vector, tmp;
10345
10346 /* Reduce consing by keeping one vector in
10347 Vwith_echo_area_save_vector. */
10348 vector = Vwith_echo_area_save_vector;
10349 Vwith_echo_area_save_vector = Qnil;
10350
10351 if (NILP (vector))
10352 vector = Fmake_vector (make_number (9), Qnil);
10353
10354 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10355 ASET (vector, i, Vdeactivate_mark); ++i;
10356 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10357
10358 if (w)
10359 {
10360 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10361 ASET (vector, i, w->contents); ++i;
10362 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10363 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10364 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10365 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10366 }
10367 else
10368 {
10369 int end = i + 6;
10370 for (; i < end; ++i)
10371 ASET (vector, i, Qnil);
10372 }
10373
10374 eassert (i == ASIZE (vector));
10375 return vector;
10376 }
10377
10378
10379 /* Restore global state from VECTOR which was created by
10380 with_echo_area_buffer_unwind_data. */
10381
10382 static void
10383 unwind_with_echo_area_buffer (Lisp_Object vector)
10384 {
10385 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10386 Vdeactivate_mark = AREF (vector, 1);
10387 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10388
10389 if (WINDOWP (AREF (vector, 3)))
10390 {
10391 struct window *w;
10392 Lisp_Object buffer;
10393
10394 w = XWINDOW (AREF (vector, 3));
10395 buffer = AREF (vector, 4);
10396
10397 wset_buffer (w, buffer);
10398 set_marker_both (w->pointm, buffer,
10399 XFASTINT (AREF (vector, 5)),
10400 XFASTINT (AREF (vector, 6)));
10401 set_marker_both (w->start, buffer,
10402 XFASTINT (AREF (vector, 7)),
10403 XFASTINT (AREF (vector, 8)));
10404 }
10405
10406 Vwith_echo_area_save_vector = vector;
10407 }
10408
10409
10410 /* Set up the echo area for use by print functions. MULTIBYTE_P
10411 non-zero means we will print multibyte. */
10412
10413 void
10414 setup_echo_area_for_printing (int multibyte_p)
10415 {
10416 /* If we can't find an echo area any more, exit. */
10417 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10418 Fkill_emacs (Qnil);
10419
10420 ensure_echo_area_buffers ();
10421
10422 if (!message_buf_print)
10423 {
10424 /* A message has been output since the last time we printed.
10425 Choose a fresh echo area buffer. */
10426 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10427 echo_area_buffer[0] = echo_buffer[1];
10428 else
10429 echo_area_buffer[0] = echo_buffer[0];
10430
10431 /* Switch to that buffer and clear it. */
10432 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10433 bset_truncate_lines (current_buffer, Qnil);
10434
10435 if (Z > BEG)
10436 {
10437 ptrdiff_t count = SPECPDL_INDEX ();
10438 specbind (Qinhibit_read_only, Qt);
10439 /* Note that undo recording is always disabled. */
10440 del_range (BEG, Z);
10441 unbind_to (count, Qnil);
10442 }
10443 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10444
10445 /* Set up the buffer for the multibyteness we need. */
10446 if (multibyte_p
10447 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10448 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10449
10450 /* Raise the frame containing the echo area. */
10451 if (minibuffer_auto_raise)
10452 {
10453 struct frame *sf = SELECTED_FRAME ();
10454 Lisp_Object mini_window;
10455 mini_window = FRAME_MINIBUF_WINDOW (sf);
10456 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10457 }
10458
10459 message_log_maybe_newline ();
10460 message_buf_print = 1;
10461 }
10462 else
10463 {
10464 if (NILP (echo_area_buffer[0]))
10465 {
10466 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10467 echo_area_buffer[0] = echo_buffer[1];
10468 else
10469 echo_area_buffer[0] = echo_buffer[0];
10470 }
10471
10472 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10473 {
10474 /* Someone switched buffers between print requests. */
10475 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10476 bset_truncate_lines (current_buffer, Qnil);
10477 }
10478 }
10479 }
10480
10481
10482 /* Display an echo area message in window W. Value is non-zero if W's
10483 height is changed. If display_last_displayed_message_p is
10484 non-zero, display the message that was last displayed, otherwise
10485 display the current message. */
10486
10487 static int
10488 display_echo_area (struct window *w)
10489 {
10490 int i, no_message_p, window_height_changed_p;
10491
10492 /* Temporarily disable garbage collections while displaying the echo
10493 area. This is done because a GC can print a message itself.
10494 That message would modify the echo area buffer's contents while a
10495 redisplay of the buffer is going on, and seriously confuse
10496 redisplay. */
10497 ptrdiff_t count = inhibit_garbage_collection ();
10498
10499 /* If there is no message, we must call display_echo_area_1
10500 nevertheless because it resizes the window. But we will have to
10501 reset the echo_area_buffer in question to nil at the end because
10502 with_echo_area_buffer will sets it to an empty buffer. */
10503 i = display_last_displayed_message_p ? 1 : 0;
10504 no_message_p = NILP (echo_area_buffer[i]);
10505
10506 window_height_changed_p
10507 = with_echo_area_buffer (w, display_last_displayed_message_p,
10508 display_echo_area_1,
10509 (intptr_t) w, Qnil);
10510
10511 if (no_message_p)
10512 echo_area_buffer[i] = Qnil;
10513
10514 unbind_to (count, Qnil);
10515 return window_height_changed_p;
10516 }
10517
10518
10519 /* Helper for display_echo_area. Display the current buffer which
10520 contains the current echo area message in window W, a mini-window,
10521 a pointer to which is passed in A1. A2..A4 are currently not used.
10522 Change the height of W so that all of the message is displayed.
10523 Value is non-zero if height of W was changed. */
10524
10525 static int
10526 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10527 {
10528 intptr_t i1 = a1;
10529 struct window *w = (struct window *) i1;
10530 Lisp_Object window;
10531 struct text_pos start;
10532 int window_height_changed_p = 0;
10533
10534 /* Do this before displaying, so that we have a large enough glyph
10535 matrix for the display. If we can't get enough space for the
10536 whole text, display the last N lines. That works by setting w->start. */
10537 window_height_changed_p = resize_mini_window (w, 0);
10538
10539 /* Use the starting position chosen by resize_mini_window. */
10540 SET_TEXT_POS_FROM_MARKER (start, w->start);
10541
10542 /* Display. */
10543 clear_glyph_matrix (w->desired_matrix);
10544 XSETWINDOW (window, w);
10545 try_window (window, start, 0);
10546
10547 return window_height_changed_p;
10548 }
10549
10550
10551 /* Resize the echo area window to exactly the size needed for the
10552 currently displayed message, if there is one. If a mini-buffer
10553 is active, don't shrink it. */
10554
10555 void
10556 resize_echo_area_exactly (void)
10557 {
10558 if (BUFFERP (echo_area_buffer[0])
10559 && WINDOWP (echo_area_window))
10560 {
10561 struct window *w = XWINDOW (echo_area_window);
10562 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10563 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10564 (intptr_t) w, resize_exactly);
10565 if (resized_p)
10566 {
10567 windows_or_buffers_changed = 42;
10568 update_mode_lines = 30;
10569 redisplay_internal ();
10570 }
10571 }
10572 }
10573
10574
10575 /* Callback function for with_echo_area_buffer, when used from
10576 resize_echo_area_exactly. A1 contains a pointer to the window to
10577 resize, EXACTLY non-nil means resize the mini-window exactly to the
10578 size of the text displayed. A3 and A4 are not used. Value is what
10579 resize_mini_window returns. */
10580
10581 static int
10582 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10583 {
10584 intptr_t i1 = a1;
10585 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10586 }
10587
10588
10589 /* Resize mini-window W to fit the size of its contents. EXACT_P
10590 means size the window exactly to the size needed. Otherwise, it's
10591 only enlarged until W's buffer is empty.
10592
10593 Set W->start to the right place to begin display. If the whole
10594 contents fit, start at the beginning. Otherwise, start so as
10595 to make the end of the contents appear. This is particularly
10596 important for y-or-n-p, but seems desirable generally.
10597
10598 Value is non-zero if the window height has been changed. */
10599
10600 int
10601 resize_mini_window (struct window *w, int exact_p)
10602 {
10603 struct frame *f = XFRAME (w->frame);
10604 int window_height_changed_p = 0;
10605
10606 eassert (MINI_WINDOW_P (w));
10607
10608 /* By default, start display at the beginning. */
10609 set_marker_both (w->start, w->contents,
10610 BUF_BEGV (XBUFFER (w->contents)),
10611 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10612
10613 /* Don't resize windows while redisplaying a window; it would
10614 confuse redisplay functions when the size of the window they are
10615 displaying changes from under them. Such a resizing can happen,
10616 for instance, when which-func prints a long message while
10617 we are running fontification-functions. We're running these
10618 functions with safe_call which binds inhibit-redisplay to t. */
10619 if (!NILP (Vinhibit_redisplay))
10620 return 0;
10621
10622 /* Nil means don't try to resize. */
10623 if (NILP (Vresize_mini_windows)
10624 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10625 return 0;
10626
10627 if (!FRAME_MINIBUF_ONLY_P (f))
10628 {
10629 struct it it;
10630 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10631 + WINDOW_PIXEL_HEIGHT (w));
10632 int unit = FRAME_LINE_HEIGHT (f);
10633 int height, max_height;
10634 struct text_pos start;
10635 struct buffer *old_current_buffer = NULL;
10636
10637 if (current_buffer != XBUFFER (w->contents))
10638 {
10639 old_current_buffer = current_buffer;
10640 set_buffer_internal (XBUFFER (w->contents));
10641 }
10642
10643 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10644
10645 /* Compute the max. number of lines specified by the user. */
10646 if (FLOATP (Vmax_mini_window_height))
10647 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10648 else if (INTEGERP (Vmax_mini_window_height))
10649 max_height = XINT (Vmax_mini_window_height) * unit;
10650 else
10651 max_height = total_height / 4;
10652
10653 /* Correct that max. height if it's bogus. */
10654 max_height = clip_to_bounds (unit, max_height, total_height);
10655
10656 /* Find out the height of the text in the window. */
10657 if (it.line_wrap == TRUNCATE)
10658 height = unit;
10659 else
10660 {
10661 last_height = 0;
10662 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10663 if (it.max_ascent == 0 && it.max_descent == 0)
10664 height = it.current_y + last_height;
10665 else
10666 height = it.current_y + it.max_ascent + it.max_descent;
10667 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10668 }
10669
10670 /* Compute a suitable window start. */
10671 if (height > max_height)
10672 {
10673 height = (max_height / unit) * unit;
10674 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10675 move_it_vertically_backward (&it, height - unit);
10676 start = it.current.pos;
10677 }
10678 else
10679 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10680 SET_MARKER_FROM_TEXT_POS (w->start, start);
10681
10682 if (EQ (Vresize_mini_windows, Qgrow_only))
10683 {
10684 /* Let it grow only, until we display an empty message, in which
10685 case the window shrinks again. */
10686 if (height > WINDOW_PIXEL_HEIGHT (w))
10687 {
10688 int old_height = WINDOW_PIXEL_HEIGHT (w);
10689
10690 FRAME_WINDOWS_FROZEN (f) = 1;
10691 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10692 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10693 }
10694 else if (height < WINDOW_PIXEL_HEIGHT (w)
10695 && (exact_p || BEGV == ZV))
10696 {
10697 int old_height = WINDOW_PIXEL_HEIGHT (w);
10698
10699 FRAME_WINDOWS_FROZEN (f) = 0;
10700 shrink_mini_window (w, 1);
10701 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10702 }
10703 }
10704 else
10705 {
10706 /* Always resize to exact size needed. */
10707 if (height > WINDOW_PIXEL_HEIGHT (w))
10708 {
10709 int old_height = WINDOW_PIXEL_HEIGHT (w);
10710
10711 FRAME_WINDOWS_FROZEN (f) = 1;
10712 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10713 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10714 }
10715 else if (height < WINDOW_PIXEL_HEIGHT (w))
10716 {
10717 int old_height = WINDOW_PIXEL_HEIGHT (w);
10718
10719 FRAME_WINDOWS_FROZEN (f) = 0;
10720 shrink_mini_window (w, 1);
10721
10722 if (height)
10723 {
10724 FRAME_WINDOWS_FROZEN (f) = 1;
10725 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10726 }
10727
10728 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10729 }
10730 }
10731
10732 if (old_current_buffer)
10733 set_buffer_internal (old_current_buffer);
10734 }
10735
10736 return window_height_changed_p;
10737 }
10738
10739
10740 /* Value is the current message, a string, or nil if there is no
10741 current message. */
10742
10743 Lisp_Object
10744 current_message (void)
10745 {
10746 Lisp_Object msg;
10747
10748 if (!BUFFERP (echo_area_buffer[0]))
10749 msg = Qnil;
10750 else
10751 {
10752 with_echo_area_buffer (0, 0, current_message_1,
10753 (intptr_t) &msg, Qnil);
10754 if (NILP (msg))
10755 echo_area_buffer[0] = Qnil;
10756 }
10757
10758 return msg;
10759 }
10760
10761
10762 static int
10763 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10764 {
10765 intptr_t i1 = a1;
10766 Lisp_Object *msg = (Lisp_Object *) i1;
10767
10768 if (Z > BEG)
10769 *msg = make_buffer_string (BEG, Z, 1);
10770 else
10771 *msg = Qnil;
10772 return 0;
10773 }
10774
10775
10776 /* Push the current message on Vmessage_stack for later restoration
10777 by restore_message. Value is non-zero if the current message isn't
10778 empty. This is a relatively infrequent operation, so it's not
10779 worth optimizing. */
10780
10781 bool
10782 push_message (void)
10783 {
10784 Lisp_Object msg = current_message ();
10785 Vmessage_stack = Fcons (msg, Vmessage_stack);
10786 return STRINGP (msg);
10787 }
10788
10789
10790 /* Restore message display from the top of Vmessage_stack. */
10791
10792 void
10793 restore_message (void)
10794 {
10795 eassert (CONSP (Vmessage_stack));
10796 message3_nolog (XCAR (Vmessage_stack));
10797 }
10798
10799
10800 /* Handler for unwind-protect calling pop_message. */
10801
10802 void
10803 pop_message_unwind (void)
10804 {
10805 /* Pop the top-most entry off Vmessage_stack. */
10806 eassert (CONSP (Vmessage_stack));
10807 Vmessage_stack = XCDR (Vmessage_stack);
10808 }
10809
10810
10811 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10812 exits. If the stack is not empty, we have a missing pop_message
10813 somewhere. */
10814
10815 void
10816 check_message_stack (void)
10817 {
10818 if (!NILP (Vmessage_stack))
10819 emacs_abort ();
10820 }
10821
10822
10823 /* Truncate to NCHARS what will be displayed in the echo area the next
10824 time we display it---but don't redisplay it now. */
10825
10826 void
10827 truncate_echo_area (ptrdiff_t nchars)
10828 {
10829 if (nchars == 0)
10830 echo_area_buffer[0] = Qnil;
10831 else if (!noninteractive
10832 && INTERACTIVE
10833 && !NILP (echo_area_buffer[0]))
10834 {
10835 struct frame *sf = SELECTED_FRAME ();
10836 /* Error messages get reported properly by cmd_error, so this must be
10837 just an informative message; if the frame hasn't really been
10838 initialized yet, just toss it. */
10839 if (sf->glyphs_initialized_p)
10840 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10841 }
10842 }
10843
10844
10845 /* Helper function for truncate_echo_area. Truncate the current
10846 message to at most NCHARS characters. */
10847
10848 static int
10849 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10850 {
10851 if (BEG + nchars < Z)
10852 del_range (BEG + nchars, Z);
10853 if (Z == BEG)
10854 echo_area_buffer[0] = Qnil;
10855 return 0;
10856 }
10857
10858 /* Set the current message to STRING. */
10859
10860 static void
10861 set_message (Lisp_Object string)
10862 {
10863 eassert (STRINGP (string));
10864
10865 message_enable_multibyte = STRING_MULTIBYTE (string);
10866
10867 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10868 message_buf_print = 0;
10869 help_echo_showing_p = 0;
10870
10871 if (STRINGP (Vdebug_on_message)
10872 && STRINGP (string)
10873 && fast_string_match (Vdebug_on_message, string) >= 0)
10874 call_debugger (list2 (Qerror, string));
10875 }
10876
10877
10878 /* Helper function for set_message. First argument is ignored and second
10879 argument has the same meaning as for set_message.
10880 This function is called with the echo area buffer being current. */
10881
10882 static int
10883 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10884 {
10885 eassert (STRINGP (string));
10886
10887 /* Change multibyteness of the echo buffer appropriately. */
10888 if (message_enable_multibyte
10889 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10890 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10891
10892 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10893 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10894 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10895
10896 /* Insert new message at BEG. */
10897 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10898
10899 /* This function takes care of single/multibyte conversion.
10900 We just have to ensure that the echo area buffer has the right
10901 setting of enable_multibyte_characters. */
10902 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10903
10904 return 0;
10905 }
10906
10907
10908 /* Clear messages. CURRENT_P non-zero means clear the current
10909 message. LAST_DISPLAYED_P non-zero means clear the message
10910 last displayed. */
10911
10912 void
10913 clear_message (bool current_p, bool last_displayed_p)
10914 {
10915 if (current_p)
10916 {
10917 echo_area_buffer[0] = Qnil;
10918 message_cleared_p = true;
10919 }
10920
10921 if (last_displayed_p)
10922 echo_area_buffer[1] = Qnil;
10923
10924 message_buf_print = 0;
10925 }
10926
10927 /* Clear garbaged frames.
10928
10929 This function is used where the old redisplay called
10930 redraw_garbaged_frames which in turn called redraw_frame which in
10931 turn called clear_frame. The call to clear_frame was a source of
10932 flickering. I believe a clear_frame is not necessary. It should
10933 suffice in the new redisplay to invalidate all current matrices,
10934 and ensure a complete redisplay of all windows. */
10935
10936 static void
10937 clear_garbaged_frames (void)
10938 {
10939 if (frame_garbaged)
10940 {
10941 Lisp_Object tail, frame;
10942
10943 FOR_EACH_FRAME (tail, frame)
10944 {
10945 struct frame *f = XFRAME (frame);
10946
10947 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10948 {
10949 if (f->resized_p)
10950 redraw_frame (f);
10951 else
10952 clear_current_matrices (f);
10953 fset_redisplay (f);
10954 f->garbaged = false;
10955 f->resized_p = false;
10956 }
10957 }
10958
10959 frame_garbaged = false;
10960 }
10961 }
10962
10963
10964 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10965 is non-zero update selected_frame. Value is non-zero if the
10966 mini-windows height has been changed. */
10967
10968 static int
10969 echo_area_display (int update_frame_p)
10970 {
10971 Lisp_Object mini_window;
10972 struct window *w;
10973 struct frame *f;
10974 int window_height_changed_p = 0;
10975 struct frame *sf = SELECTED_FRAME ();
10976
10977 mini_window = FRAME_MINIBUF_WINDOW (sf);
10978 w = XWINDOW (mini_window);
10979 f = XFRAME (WINDOW_FRAME (w));
10980
10981 /* Don't display if frame is invisible or not yet initialized. */
10982 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10983 return 0;
10984
10985 #ifdef HAVE_WINDOW_SYSTEM
10986 /* When Emacs starts, selected_frame may be the initial terminal
10987 frame. If we let this through, a message would be displayed on
10988 the terminal. */
10989 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10990 return 0;
10991 #endif /* HAVE_WINDOW_SYSTEM */
10992
10993 /* Redraw garbaged frames. */
10994 clear_garbaged_frames ();
10995
10996 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10997 {
10998 echo_area_window = mini_window;
10999 window_height_changed_p = display_echo_area (w);
11000 w->must_be_updated_p = true;
11001
11002 /* Update the display, unless called from redisplay_internal.
11003 Also don't update the screen during redisplay itself. The
11004 update will happen at the end of redisplay, and an update
11005 here could cause confusion. */
11006 if (update_frame_p && !redisplaying_p)
11007 {
11008 int n = 0;
11009
11010 /* If the display update has been interrupted by pending
11011 input, update mode lines in the frame. Due to the
11012 pending input, it might have been that redisplay hasn't
11013 been called, so that mode lines above the echo area are
11014 garbaged. This looks odd, so we prevent it here. */
11015 if (!display_completed)
11016 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11017
11018 if (window_height_changed_p
11019 /* Don't do this if Emacs is shutting down. Redisplay
11020 needs to run hooks. */
11021 && !NILP (Vrun_hooks))
11022 {
11023 /* Must update other windows. Likewise as in other
11024 cases, don't let this update be interrupted by
11025 pending input. */
11026 ptrdiff_t count = SPECPDL_INDEX ();
11027 specbind (Qredisplay_dont_pause, Qt);
11028 windows_or_buffers_changed = 44;
11029 redisplay_internal ();
11030 unbind_to (count, Qnil);
11031 }
11032 else if (FRAME_WINDOW_P (f) && n == 0)
11033 {
11034 /* Window configuration is the same as before.
11035 Can do with a display update of the echo area,
11036 unless we displayed some mode lines. */
11037 update_single_window (w, 1);
11038 flush_frame (f);
11039 }
11040 else
11041 update_frame (f, 1, 1);
11042
11043 /* If cursor is in the echo area, make sure that the next
11044 redisplay displays the minibuffer, so that the cursor will
11045 be replaced with what the minibuffer wants. */
11046 if (cursor_in_echo_area)
11047 wset_redisplay (XWINDOW (mini_window));
11048 }
11049 }
11050 else if (!EQ (mini_window, selected_window))
11051 wset_redisplay (XWINDOW (mini_window));
11052
11053 /* Last displayed message is now the current message. */
11054 echo_area_buffer[1] = echo_area_buffer[0];
11055 /* Inform read_char that we're not echoing. */
11056 echo_message_buffer = Qnil;
11057
11058 /* Prevent redisplay optimization in redisplay_internal by resetting
11059 this_line_start_pos. This is done because the mini-buffer now
11060 displays the message instead of its buffer text. */
11061 if (EQ (mini_window, selected_window))
11062 CHARPOS (this_line_start_pos) = 0;
11063
11064 return window_height_changed_p;
11065 }
11066
11067 /* Nonzero if W's buffer was changed but not saved. */
11068
11069 static int
11070 window_buffer_changed (struct window *w)
11071 {
11072 struct buffer *b = XBUFFER (w->contents);
11073
11074 eassert (BUFFER_LIVE_P (b));
11075
11076 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11077 }
11078
11079 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11080
11081 static int
11082 mode_line_update_needed (struct window *w)
11083 {
11084 return (w->column_number_displayed != -1
11085 && !(PT == w->last_point && !window_outdated (w))
11086 && (w->column_number_displayed != current_column ()));
11087 }
11088
11089 /* Nonzero if window start of W is frozen and may not be changed during
11090 redisplay. */
11091
11092 static bool
11093 window_frozen_p (struct window *w)
11094 {
11095 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11096 {
11097 Lisp_Object window;
11098
11099 XSETWINDOW (window, w);
11100 if (MINI_WINDOW_P (w))
11101 return 0;
11102 else if (EQ (window, selected_window))
11103 return 0;
11104 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11105 && EQ (window, Vminibuf_scroll_window))
11106 /* This special window can't be frozen too. */
11107 return 0;
11108 else
11109 return 1;
11110 }
11111 return 0;
11112 }
11113
11114 /***********************************************************************
11115 Mode Lines and Frame Titles
11116 ***********************************************************************/
11117
11118 /* A buffer for constructing non-propertized mode-line strings and
11119 frame titles in it; allocated from the heap in init_xdisp and
11120 resized as needed in store_mode_line_noprop_char. */
11121
11122 static char *mode_line_noprop_buf;
11123
11124 /* The buffer's end, and a current output position in it. */
11125
11126 static char *mode_line_noprop_buf_end;
11127 static char *mode_line_noprop_ptr;
11128
11129 #define MODE_LINE_NOPROP_LEN(start) \
11130 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11131
11132 static enum {
11133 MODE_LINE_DISPLAY = 0,
11134 MODE_LINE_TITLE,
11135 MODE_LINE_NOPROP,
11136 MODE_LINE_STRING
11137 } mode_line_target;
11138
11139 /* Alist that caches the results of :propertize.
11140 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11141 static Lisp_Object mode_line_proptrans_alist;
11142
11143 /* List of strings making up the mode-line. */
11144 static Lisp_Object mode_line_string_list;
11145
11146 /* Base face property when building propertized mode line string. */
11147 static Lisp_Object mode_line_string_face;
11148 static Lisp_Object mode_line_string_face_prop;
11149
11150
11151 /* Unwind data for mode line strings */
11152
11153 static Lisp_Object Vmode_line_unwind_vector;
11154
11155 static Lisp_Object
11156 format_mode_line_unwind_data (struct frame *target_frame,
11157 struct buffer *obuf,
11158 Lisp_Object owin,
11159 int save_proptrans)
11160 {
11161 Lisp_Object vector, tmp;
11162
11163 /* Reduce consing by keeping one vector in
11164 Vwith_echo_area_save_vector. */
11165 vector = Vmode_line_unwind_vector;
11166 Vmode_line_unwind_vector = Qnil;
11167
11168 if (NILP (vector))
11169 vector = Fmake_vector (make_number (10), Qnil);
11170
11171 ASET (vector, 0, make_number (mode_line_target));
11172 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11173 ASET (vector, 2, mode_line_string_list);
11174 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11175 ASET (vector, 4, mode_line_string_face);
11176 ASET (vector, 5, mode_line_string_face_prop);
11177
11178 if (obuf)
11179 XSETBUFFER (tmp, obuf);
11180 else
11181 tmp = Qnil;
11182 ASET (vector, 6, tmp);
11183 ASET (vector, 7, owin);
11184 if (target_frame)
11185 {
11186 /* Similarly to `with-selected-window', if the operation selects
11187 a window on another frame, we must restore that frame's
11188 selected window, and (for a tty) the top-frame. */
11189 ASET (vector, 8, target_frame->selected_window);
11190 if (FRAME_TERMCAP_P (target_frame))
11191 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11192 }
11193
11194 return vector;
11195 }
11196
11197 static void
11198 unwind_format_mode_line (Lisp_Object vector)
11199 {
11200 Lisp_Object old_window = AREF (vector, 7);
11201 Lisp_Object target_frame_window = AREF (vector, 8);
11202 Lisp_Object old_top_frame = AREF (vector, 9);
11203
11204 mode_line_target = XINT (AREF (vector, 0));
11205 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11206 mode_line_string_list = AREF (vector, 2);
11207 if (! EQ (AREF (vector, 3), Qt))
11208 mode_line_proptrans_alist = AREF (vector, 3);
11209 mode_line_string_face = AREF (vector, 4);
11210 mode_line_string_face_prop = AREF (vector, 5);
11211
11212 /* Select window before buffer, since it may change the buffer. */
11213 if (!NILP (old_window))
11214 {
11215 /* If the operation that we are unwinding had selected a window
11216 on a different frame, reset its frame-selected-window. For a
11217 text terminal, reset its top-frame if necessary. */
11218 if (!NILP (target_frame_window))
11219 {
11220 Lisp_Object frame
11221 = WINDOW_FRAME (XWINDOW (target_frame_window));
11222
11223 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11224 Fselect_window (target_frame_window, Qt);
11225
11226 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11227 Fselect_frame (old_top_frame, Qt);
11228 }
11229
11230 Fselect_window (old_window, Qt);
11231 }
11232
11233 if (!NILP (AREF (vector, 6)))
11234 {
11235 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11236 ASET (vector, 6, Qnil);
11237 }
11238
11239 Vmode_line_unwind_vector = vector;
11240 }
11241
11242
11243 /* Store a single character C for the frame title in mode_line_noprop_buf.
11244 Re-allocate mode_line_noprop_buf if necessary. */
11245
11246 static void
11247 store_mode_line_noprop_char (char c)
11248 {
11249 /* If output position has reached the end of the allocated buffer,
11250 increase the buffer's size. */
11251 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11252 {
11253 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11254 ptrdiff_t size = len;
11255 mode_line_noprop_buf =
11256 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11257 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11258 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11259 }
11260
11261 *mode_line_noprop_ptr++ = c;
11262 }
11263
11264
11265 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11266 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11267 characters that yield more columns than PRECISION; PRECISION <= 0
11268 means copy the whole string. Pad with spaces until FIELD_WIDTH
11269 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11270 pad. Called from display_mode_element when it is used to build a
11271 frame title. */
11272
11273 static int
11274 store_mode_line_noprop (const char *string, int field_width, int precision)
11275 {
11276 const unsigned char *str = (const unsigned char *) string;
11277 int n = 0;
11278 ptrdiff_t dummy, nbytes;
11279
11280 /* Copy at most PRECISION chars from STR. */
11281 nbytes = strlen (string);
11282 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11283 while (nbytes--)
11284 store_mode_line_noprop_char (*str++);
11285
11286 /* Fill up with spaces until FIELD_WIDTH reached. */
11287 while (field_width > 0
11288 && n < field_width)
11289 {
11290 store_mode_line_noprop_char (' ');
11291 ++n;
11292 }
11293
11294 return n;
11295 }
11296
11297 /***********************************************************************
11298 Frame Titles
11299 ***********************************************************************/
11300
11301 #ifdef HAVE_WINDOW_SYSTEM
11302
11303 /* Set the title of FRAME, if it has changed. The title format is
11304 Vicon_title_format if FRAME is iconified, otherwise it is
11305 frame_title_format. */
11306
11307 static void
11308 x_consider_frame_title (Lisp_Object frame)
11309 {
11310 struct frame *f = XFRAME (frame);
11311
11312 if (FRAME_WINDOW_P (f)
11313 || FRAME_MINIBUF_ONLY_P (f)
11314 || f->explicit_name)
11315 {
11316 /* Do we have more than one visible frame on this X display? */
11317 Lisp_Object tail, other_frame, fmt;
11318 ptrdiff_t title_start;
11319 char *title;
11320 ptrdiff_t len;
11321 struct it it;
11322 ptrdiff_t count = SPECPDL_INDEX ();
11323
11324 FOR_EACH_FRAME (tail, other_frame)
11325 {
11326 struct frame *tf = XFRAME (other_frame);
11327
11328 if (tf != f
11329 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11330 && !FRAME_MINIBUF_ONLY_P (tf)
11331 && !EQ (other_frame, tip_frame)
11332 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11333 break;
11334 }
11335
11336 /* Set global variable indicating that multiple frames exist. */
11337 multiple_frames = CONSP (tail);
11338
11339 /* Switch to the buffer of selected window of the frame. Set up
11340 mode_line_target so that display_mode_element will output into
11341 mode_line_noprop_buf; then display the title. */
11342 record_unwind_protect (unwind_format_mode_line,
11343 format_mode_line_unwind_data
11344 (f, current_buffer, selected_window, 0));
11345
11346 Fselect_window (f->selected_window, Qt);
11347 set_buffer_internal_1
11348 (XBUFFER (XWINDOW (f->selected_window)->contents));
11349 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11350
11351 mode_line_target = MODE_LINE_TITLE;
11352 title_start = MODE_LINE_NOPROP_LEN (0);
11353 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11354 NULL, DEFAULT_FACE_ID);
11355 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11356 len = MODE_LINE_NOPROP_LEN (title_start);
11357 title = mode_line_noprop_buf + title_start;
11358 unbind_to (count, Qnil);
11359
11360 /* Set the title only if it's changed. This avoids consing in
11361 the common case where it hasn't. (If it turns out that we've
11362 already wasted too much time by walking through the list with
11363 display_mode_element, then we might need to optimize at a
11364 higher level than this.) */
11365 if (! STRINGP (f->name)
11366 || SBYTES (f->name) != len
11367 || memcmp (title, SDATA (f->name), len) != 0)
11368 x_implicitly_set_name (f, make_string (title, len), Qnil);
11369 }
11370 }
11371
11372 #endif /* not HAVE_WINDOW_SYSTEM */
11373
11374 \f
11375 /***********************************************************************
11376 Menu Bars
11377 ***********************************************************************/
11378
11379 /* Non-zero if we will not redisplay all visible windows. */
11380 #define REDISPLAY_SOME_P() \
11381 ((windows_or_buffers_changed == 0 \
11382 || windows_or_buffers_changed == REDISPLAY_SOME) \
11383 && (update_mode_lines == 0 \
11384 || update_mode_lines == REDISPLAY_SOME))
11385
11386 /* Prepare for redisplay by updating menu-bar item lists when
11387 appropriate. This can call eval. */
11388
11389 static void
11390 prepare_menu_bars (void)
11391 {
11392 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11393 bool some_windows = REDISPLAY_SOME_P ();
11394 struct gcpro gcpro1, gcpro2;
11395 Lisp_Object tooltip_frame;
11396
11397 #ifdef HAVE_WINDOW_SYSTEM
11398 tooltip_frame = tip_frame;
11399 #else
11400 tooltip_frame = Qnil;
11401 #endif
11402
11403 if (FUNCTIONP (Vpre_redisplay_function))
11404 {
11405 Lisp_Object windows = all_windows ? Qt : Qnil;
11406 if (all_windows && some_windows)
11407 {
11408 Lisp_Object ws = window_list ();
11409 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11410 {
11411 Lisp_Object this = XCAR (ws);
11412 struct window *w = XWINDOW (this);
11413 if (w->redisplay
11414 || XFRAME (w->frame)->redisplay
11415 || XBUFFER (w->contents)->text->redisplay)
11416 {
11417 windows = Fcons (this, windows);
11418 }
11419 }
11420 }
11421 safe_call1 (Vpre_redisplay_function, windows);
11422 }
11423
11424 /* Update all frame titles based on their buffer names, etc. We do
11425 this before the menu bars so that the buffer-menu will show the
11426 up-to-date frame titles. */
11427 #ifdef HAVE_WINDOW_SYSTEM
11428 if (all_windows)
11429 {
11430 Lisp_Object tail, frame;
11431
11432 FOR_EACH_FRAME (tail, frame)
11433 {
11434 struct frame *f = XFRAME (frame);
11435 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11436 if (some_windows
11437 && !f->redisplay
11438 && !w->redisplay
11439 && !XBUFFER (w->contents)->text->redisplay)
11440 continue;
11441
11442 if (!EQ (frame, tooltip_frame)
11443 && (FRAME_ICONIFIED_P (f)
11444 || FRAME_VISIBLE_P (f) == 1
11445 /* Exclude TTY frames that are obscured because they
11446 are not the top frame on their console. This is
11447 because x_consider_frame_title actually switches
11448 to the frame, which for TTY frames means it is
11449 marked as garbaged, and will be completely
11450 redrawn on the next redisplay cycle. This causes
11451 TTY frames to be completely redrawn, when there
11452 are more than one of them, even though nothing
11453 should be changed on display. */
11454 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11455 x_consider_frame_title (frame);
11456 }
11457 }
11458 #endif /* HAVE_WINDOW_SYSTEM */
11459
11460 /* Update the menu bar item lists, if appropriate. This has to be
11461 done before any actual redisplay or generation of display lines. */
11462
11463 if (all_windows)
11464 {
11465 Lisp_Object tail, frame;
11466 ptrdiff_t count = SPECPDL_INDEX ();
11467 /* 1 means that update_menu_bar has run its hooks
11468 so any further calls to update_menu_bar shouldn't do so again. */
11469 int menu_bar_hooks_run = 0;
11470
11471 record_unwind_save_match_data ();
11472
11473 FOR_EACH_FRAME (tail, frame)
11474 {
11475 struct frame *f = XFRAME (frame);
11476 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11477
11478 /* Ignore tooltip frame. */
11479 if (EQ (frame, tooltip_frame))
11480 continue;
11481
11482 if (some_windows
11483 && !f->redisplay
11484 && !w->redisplay
11485 && !XBUFFER (w->contents)->text->redisplay)
11486 continue;
11487
11488 /* If a window on this frame changed size, report that to
11489 the user and clear the size-change flag. */
11490 if (FRAME_WINDOW_SIZES_CHANGED (f))
11491 {
11492 Lisp_Object functions;
11493
11494 /* Clear flag first in case we get an error below. */
11495 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11496 functions = Vwindow_size_change_functions;
11497 GCPRO2 (tail, functions);
11498
11499 while (CONSP (functions))
11500 {
11501 if (!EQ (XCAR (functions), Qt))
11502 call1 (XCAR (functions), frame);
11503 functions = XCDR (functions);
11504 }
11505 UNGCPRO;
11506 }
11507
11508 GCPRO1 (tail);
11509 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11510 #ifdef HAVE_WINDOW_SYSTEM
11511 update_tool_bar (f, 0);
11512 #endif
11513 #ifdef HAVE_NS
11514 if (windows_or_buffers_changed
11515 && FRAME_NS_P (f))
11516 ns_set_doc_edited
11517 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11518 #endif
11519 UNGCPRO;
11520 }
11521
11522 unbind_to (count, Qnil);
11523 }
11524 else
11525 {
11526 struct frame *sf = SELECTED_FRAME ();
11527 update_menu_bar (sf, 1, 0);
11528 #ifdef HAVE_WINDOW_SYSTEM
11529 update_tool_bar (sf, 1);
11530 #endif
11531 }
11532 }
11533
11534
11535 /* Update the menu bar item list for frame F. This has to be done
11536 before we start to fill in any display lines, because it can call
11537 eval.
11538
11539 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11540
11541 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11542 already ran the menu bar hooks for this redisplay, so there
11543 is no need to run them again. The return value is the
11544 updated value of this flag, to pass to the next call. */
11545
11546 static int
11547 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11548 {
11549 Lisp_Object window;
11550 register struct window *w;
11551
11552 /* If called recursively during a menu update, do nothing. This can
11553 happen when, for instance, an activate-menubar-hook causes a
11554 redisplay. */
11555 if (inhibit_menubar_update)
11556 return hooks_run;
11557
11558 window = FRAME_SELECTED_WINDOW (f);
11559 w = XWINDOW (window);
11560
11561 if (FRAME_WINDOW_P (f)
11562 ?
11563 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11564 || defined (HAVE_NS) || defined (USE_GTK)
11565 FRAME_EXTERNAL_MENU_BAR (f)
11566 #else
11567 FRAME_MENU_BAR_LINES (f) > 0
11568 #endif
11569 : FRAME_MENU_BAR_LINES (f) > 0)
11570 {
11571 /* If the user has switched buffers or windows, we need to
11572 recompute to reflect the new bindings. But we'll
11573 recompute when update_mode_lines is set too; that means
11574 that people can use force-mode-line-update to request
11575 that the menu bar be recomputed. The adverse effect on
11576 the rest of the redisplay algorithm is about the same as
11577 windows_or_buffers_changed anyway. */
11578 if (windows_or_buffers_changed
11579 /* This used to test w->update_mode_line, but we believe
11580 there is no need to recompute the menu in that case. */
11581 || update_mode_lines
11582 || window_buffer_changed (w))
11583 {
11584 struct buffer *prev = current_buffer;
11585 ptrdiff_t count = SPECPDL_INDEX ();
11586
11587 specbind (Qinhibit_menubar_update, Qt);
11588
11589 set_buffer_internal_1 (XBUFFER (w->contents));
11590 if (save_match_data)
11591 record_unwind_save_match_data ();
11592 if (NILP (Voverriding_local_map_menu_flag))
11593 {
11594 specbind (Qoverriding_terminal_local_map, Qnil);
11595 specbind (Qoverriding_local_map, Qnil);
11596 }
11597
11598 if (!hooks_run)
11599 {
11600 /* Run the Lucid hook. */
11601 safe_run_hooks (Qactivate_menubar_hook);
11602
11603 /* If it has changed current-menubar from previous value,
11604 really recompute the menu-bar from the value. */
11605 if (! NILP (Vlucid_menu_bar_dirty_flag))
11606 call0 (Qrecompute_lucid_menubar);
11607
11608 safe_run_hooks (Qmenu_bar_update_hook);
11609
11610 hooks_run = 1;
11611 }
11612
11613 XSETFRAME (Vmenu_updating_frame, f);
11614 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11615
11616 /* Redisplay the menu bar in case we changed it. */
11617 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11618 || defined (HAVE_NS) || defined (USE_GTK)
11619 if (FRAME_WINDOW_P (f))
11620 {
11621 #if defined (HAVE_NS)
11622 /* All frames on Mac OS share the same menubar. So only
11623 the selected frame should be allowed to set it. */
11624 if (f == SELECTED_FRAME ())
11625 #endif
11626 set_frame_menubar (f, 0, 0);
11627 }
11628 else
11629 /* On a terminal screen, the menu bar is an ordinary screen
11630 line, and this makes it get updated. */
11631 w->update_mode_line = 1;
11632 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11633 /* In the non-toolkit version, the menu bar is an ordinary screen
11634 line, and this makes it get updated. */
11635 w->update_mode_line = 1;
11636 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11637
11638 unbind_to (count, Qnil);
11639 set_buffer_internal_1 (prev);
11640 }
11641 }
11642
11643 return hooks_run;
11644 }
11645
11646 /***********************************************************************
11647 Tool-bars
11648 ***********************************************************************/
11649
11650 #ifdef HAVE_WINDOW_SYSTEM
11651
11652 /* Tool-bar item index of the item on which a mouse button was pressed
11653 or -1. */
11654
11655 int last_tool_bar_item;
11656
11657 /* Select `frame' temporarily without running all the code in
11658 do_switch_frame.
11659 FIXME: Maybe do_switch_frame should be trimmed down similarly
11660 when `norecord' is set. */
11661 static void
11662 fast_set_selected_frame (Lisp_Object frame)
11663 {
11664 if (!EQ (selected_frame, frame))
11665 {
11666 selected_frame = frame;
11667 selected_window = XFRAME (frame)->selected_window;
11668 }
11669 }
11670
11671 /* Update the tool-bar item list for frame F. This has to be done
11672 before we start to fill in any display lines. Called from
11673 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11674 and restore it here. */
11675
11676 static void
11677 update_tool_bar (struct frame *f, int save_match_data)
11678 {
11679 #if defined (USE_GTK) || defined (HAVE_NS)
11680 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11681 #else
11682 int do_update = (WINDOWP (f->tool_bar_window)
11683 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11684 #endif
11685
11686 if (do_update)
11687 {
11688 Lisp_Object window;
11689 struct window *w;
11690
11691 window = FRAME_SELECTED_WINDOW (f);
11692 w = XWINDOW (window);
11693
11694 /* If the user has switched buffers or windows, we need to
11695 recompute to reflect the new bindings. But we'll
11696 recompute when update_mode_lines is set too; that means
11697 that people can use force-mode-line-update to request
11698 that the menu bar be recomputed. The adverse effect on
11699 the rest of the redisplay algorithm is about the same as
11700 windows_or_buffers_changed anyway. */
11701 if (windows_or_buffers_changed
11702 || w->update_mode_line
11703 || update_mode_lines
11704 || window_buffer_changed (w))
11705 {
11706 struct buffer *prev = current_buffer;
11707 ptrdiff_t count = SPECPDL_INDEX ();
11708 Lisp_Object frame, new_tool_bar;
11709 int new_n_tool_bar;
11710 struct gcpro gcpro1;
11711
11712 /* Set current_buffer to the buffer of the selected
11713 window of the frame, so that we get the right local
11714 keymaps. */
11715 set_buffer_internal_1 (XBUFFER (w->contents));
11716
11717 /* Save match data, if we must. */
11718 if (save_match_data)
11719 record_unwind_save_match_data ();
11720
11721 /* Make sure that we don't accidentally use bogus keymaps. */
11722 if (NILP (Voverriding_local_map_menu_flag))
11723 {
11724 specbind (Qoverriding_terminal_local_map, Qnil);
11725 specbind (Qoverriding_local_map, Qnil);
11726 }
11727
11728 GCPRO1 (new_tool_bar);
11729
11730 /* We must temporarily set the selected frame to this frame
11731 before calling tool_bar_items, because the calculation of
11732 the tool-bar keymap uses the selected frame (see
11733 `tool-bar-make-keymap' in tool-bar.el). */
11734 eassert (EQ (selected_window,
11735 /* Since we only explicitly preserve selected_frame,
11736 check that selected_window would be redundant. */
11737 XFRAME (selected_frame)->selected_window));
11738 record_unwind_protect (fast_set_selected_frame, selected_frame);
11739 XSETFRAME (frame, f);
11740 fast_set_selected_frame (frame);
11741
11742 /* Build desired tool-bar items from keymaps. */
11743 new_tool_bar
11744 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11745 &new_n_tool_bar);
11746
11747 /* Redisplay the tool-bar if we changed it. */
11748 if (new_n_tool_bar != f->n_tool_bar_items
11749 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11750 {
11751 /* Redisplay that happens asynchronously due to an expose event
11752 may access f->tool_bar_items. Make sure we update both
11753 variables within BLOCK_INPUT so no such event interrupts. */
11754 block_input ();
11755 fset_tool_bar_items (f, new_tool_bar);
11756 f->n_tool_bar_items = new_n_tool_bar;
11757 w->update_mode_line = 1;
11758 unblock_input ();
11759 }
11760
11761 UNGCPRO;
11762
11763 unbind_to (count, Qnil);
11764 set_buffer_internal_1 (prev);
11765 }
11766 }
11767 }
11768
11769 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11770
11771 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11772 F's desired tool-bar contents. F->tool_bar_items must have
11773 been set up previously by calling prepare_menu_bars. */
11774
11775 static void
11776 build_desired_tool_bar_string (struct frame *f)
11777 {
11778 int i, size, size_needed;
11779 struct gcpro gcpro1, gcpro2, gcpro3;
11780 Lisp_Object image, plist, props;
11781
11782 image = plist = props = Qnil;
11783 GCPRO3 (image, plist, props);
11784
11785 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11786 Otherwise, make a new string. */
11787
11788 /* The size of the string we might be able to reuse. */
11789 size = (STRINGP (f->desired_tool_bar_string)
11790 ? SCHARS (f->desired_tool_bar_string)
11791 : 0);
11792
11793 /* We need one space in the string for each image. */
11794 size_needed = f->n_tool_bar_items;
11795
11796 /* Reuse f->desired_tool_bar_string, if possible. */
11797 if (size < size_needed || NILP (f->desired_tool_bar_string))
11798 fset_desired_tool_bar_string
11799 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11800 else
11801 {
11802 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11803 Fremove_text_properties (make_number (0), make_number (size),
11804 props, f->desired_tool_bar_string);
11805 }
11806
11807 /* Put a `display' property on the string for the images to display,
11808 put a `menu_item' property on tool-bar items with a value that
11809 is the index of the item in F's tool-bar item vector. */
11810 for (i = 0; i < f->n_tool_bar_items; ++i)
11811 {
11812 #define PROP(IDX) \
11813 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11814
11815 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11816 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11817 int hmargin, vmargin, relief, idx, end;
11818
11819 /* If image is a vector, choose the image according to the
11820 button state. */
11821 image = PROP (TOOL_BAR_ITEM_IMAGES);
11822 if (VECTORP (image))
11823 {
11824 if (enabled_p)
11825 idx = (selected_p
11826 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11827 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11828 else
11829 idx = (selected_p
11830 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11831 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11832
11833 eassert (ASIZE (image) >= idx);
11834 image = AREF (image, idx);
11835 }
11836 else
11837 idx = -1;
11838
11839 /* Ignore invalid image specifications. */
11840 if (!valid_image_p (image))
11841 continue;
11842
11843 /* Display the tool-bar button pressed, or depressed. */
11844 plist = Fcopy_sequence (XCDR (image));
11845
11846 /* Compute margin and relief to draw. */
11847 relief = (tool_bar_button_relief >= 0
11848 ? tool_bar_button_relief
11849 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11850 hmargin = vmargin = relief;
11851
11852 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11853 INT_MAX - max (hmargin, vmargin)))
11854 {
11855 hmargin += XFASTINT (Vtool_bar_button_margin);
11856 vmargin += XFASTINT (Vtool_bar_button_margin);
11857 }
11858 else if (CONSP (Vtool_bar_button_margin))
11859 {
11860 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11861 INT_MAX - hmargin))
11862 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11863
11864 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11865 INT_MAX - vmargin))
11866 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11867 }
11868
11869 if (auto_raise_tool_bar_buttons_p)
11870 {
11871 /* Add a `:relief' property to the image spec if the item is
11872 selected. */
11873 if (selected_p)
11874 {
11875 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11876 hmargin -= relief;
11877 vmargin -= relief;
11878 }
11879 }
11880 else
11881 {
11882 /* If image is selected, display it pressed, i.e. with a
11883 negative relief. If it's not selected, display it with a
11884 raised relief. */
11885 plist = Fplist_put (plist, QCrelief,
11886 (selected_p
11887 ? make_number (-relief)
11888 : make_number (relief)));
11889 hmargin -= relief;
11890 vmargin -= relief;
11891 }
11892
11893 /* Put a margin around the image. */
11894 if (hmargin || vmargin)
11895 {
11896 if (hmargin == vmargin)
11897 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11898 else
11899 plist = Fplist_put (plist, QCmargin,
11900 Fcons (make_number (hmargin),
11901 make_number (vmargin)));
11902 }
11903
11904 /* If button is not enabled, and we don't have special images
11905 for the disabled state, make the image appear disabled by
11906 applying an appropriate algorithm to it. */
11907 if (!enabled_p && idx < 0)
11908 plist = Fplist_put (plist, QCconversion, Qdisabled);
11909
11910 /* Put a `display' text property on the string for the image to
11911 display. Put a `menu-item' property on the string that gives
11912 the start of this item's properties in the tool-bar items
11913 vector. */
11914 image = Fcons (Qimage, plist);
11915 props = list4 (Qdisplay, image,
11916 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11917
11918 /* Let the last image hide all remaining spaces in the tool bar
11919 string. The string can be longer than needed when we reuse a
11920 previous string. */
11921 if (i + 1 == f->n_tool_bar_items)
11922 end = SCHARS (f->desired_tool_bar_string);
11923 else
11924 end = i + 1;
11925 Fadd_text_properties (make_number (i), make_number (end),
11926 props, f->desired_tool_bar_string);
11927 #undef PROP
11928 }
11929
11930 UNGCPRO;
11931 }
11932
11933
11934 /* Display one line of the tool-bar of frame IT->f.
11935
11936 HEIGHT specifies the desired height of the tool-bar line.
11937 If the actual height of the glyph row is less than HEIGHT, the
11938 row's height is increased to HEIGHT, and the icons are centered
11939 vertically in the new height.
11940
11941 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11942 count a final empty row in case the tool-bar width exactly matches
11943 the window width.
11944 */
11945
11946 static void
11947 display_tool_bar_line (struct it *it, int height)
11948 {
11949 struct glyph_row *row = it->glyph_row;
11950 int max_x = it->last_visible_x;
11951 struct glyph *last;
11952
11953 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11954 clear_glyph_row (row);
11955 row->enabled_p = true;
11956 row->y = it->current_y;
11957
11958 /* Note that this isn't made use of if the face hasn't a box,
11959 so there's no need to check the face here. */
11960 it->start_of_box_run_p = 1;
11961
11962 while (it->current_x < max_x)
11963 {
11964 int x, n_glyphs_before, i, nglyphs;
11965 struct it it_before;
11966
11967 /* Get the next display element. */
11968 if (!get_next_display_element (it))
11969 {
11970 /* Don't count empty row if we are counting needed tool-bar lines. */
11971 if (height < 0 && !it->hpos)
11972 return;
11973 break;
11974 }
11975
11976 /* Produce glyphs. */
11977 n_glyphs_before = row->used[TEXT_AREA];
11978 it_before = *it;
11979
11980 PRODUCE_GLYPHS (it);
11981
11982 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11983 i = 0;
11984 x = it_before.current_x;
11985 while (i < nglyphs)
11986 {
11987 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11988
11989 if (x + glyph->pixel_width > max_x)
11990 {
11991 /* Glyph doesn't fit on line. Backtrack. */
11992 row->used[TEXT_AREA] = n_glyphs_before;
11993 *it = it_before;
11994 /* If this is the only glyph on this line, it will never fit on the
11995 tool-bar, so skip it. But ensure there is at least one glyph,
11996 so we don't accidentally disable the tool-bar. */
11997 if (n_glyphs_before == 0
11998 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11999 break;
12000 goto out;
12001 }
12002
12003 ++it->hpos;
12004 x += glyph->pixel_width;
12005 ++i;
12006 }
12007
12008 /* Stop at line end. */
12009 if (ITERATOR_AT_END_OF_LINE_P (it))
12010 break;
12011
12012 set_iterator_to_next (it, 1);
12013 }
12014
12015 out:;
12016
12017 row->displays_text_p = row->used[TEXT_AREA] != 0;
12018
12019 /* Use default face for the border below the tool bar.
12020
12021 FIXME: When auto-resize-tool-bars is grow-only, there is
12022 no additional border below the possibly empty tool-bar lines.
12023 So to make the extra empty lines look "normal", we have to
12024 use the tool-bar face for the border too. */
12025 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12026 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12027 it->face_id = DEFAULT_FACE_ID;
12028
12029 extend_face_to_end_of_line (it);
12030 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12031 last->right_box_line_p = 1;
12032 if (last == row->glyphs[TEXT_AREA])
12033 last->left_box_line_p = 1;
12034
12035 /* Make line the desired height and center it vertically. */
12036 if ((height -= it->max_ascent + it->max_descent) > 0)
12037 {
12038 /* Don't add more than one line height. */
12039 height %= FRAME_LINE_HEIGHT (it->f);
12040 it->max_ascent += height / 2;
12041 it->max_descent += (height + 1) / 2;
12042 }
12043
12044 compute_line_metrics (it);
12045
12046 /* If line is empty, make it occupy the rest of the tool-bar. */
12047 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12048 {
12049 row->height = row->phys_height = it->last_visible_y - row->y;
12050 row->visible_height = row->height;
12051 row->ascent = row->phys_ascent = 0;
12052 row->extra_line_spacing = 0;
12053 }
12054
12055 row->full_width_p = 1;
12056 row->continued_p = 0;
12057 row->truncated_on_left_p = 0;
12058 row->truncated_on_right_p = 0;
12059
12060 it->current_x = it->hpos = 0;
12061 it->current_y += row->height;
12062 ++it->vpos;
12063 ++it->glyph_row;
12064 }
12065
12066
12067 /* Max tool-bar height. Basically, this is what makes all other windows
12068 disappear when the frame gets too small. Rethink this! */
12069
12070 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12071 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12072
12073 /* Value is the number of pixels needed to make all tool-bar items of
12074 frame F visible. The actual number of glyph rows needed is
12075 returned in *N_ROWS if non-NULL. */
12076
12077 static int
12078 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12079 {
12080 struct window *w = XWINDOW (f->tool_bar_window);
12081 struct it it;
12082 /* tool_bar_height is called from redisplay_tool_bar after building
12083 the desired matrix, so use (unused) mode-line row as temporary row to
12084 avoid destroying the first tool-bar row. */
12085 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12086
12087 /* Initialize an iterator for iteration over
12088 F->desired_tool_bar_string in the tool-bar window of frame F. */
12089 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12090 it.first_visible_x = 0;
12091 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12092 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12093 it.paragraph_embedding = L2R;
12094
12095 while (!ITERATOR_AT_END_P (&it))
12096 {
12097 clear_glyph_row (temp_row);
12098 it.glyph_row = temp_row;
12099 display_tool_bar_line (&it, -1);
12100 }
12101 clear_glyph_row (temp_row);
12102
12103 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12104 if (n_rows)
12105 *n_rows = it.vpos > 0 ? it.vpos : -1;
12106
12107 if (pixelwise)
12108 return it.current_y;
12109 else
12110 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12111 }
12112
12113 #endif /* !USE_GTK && !HAVE_NS */
12114
12115 #if defined USE_GTK || defined HAVE_NS
12116 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12117 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12118 #endif
12119
12120 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12121 0, 2, 0,
12122 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12123 If FRAME is nil or omitted, use the selected frame. Optional argument
12124 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12125 (Lisp_Object frame, Lisp_Object pixelwise)
12126 {
12127 int height = 0;
12128
12129 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12130 struct frame *f = decode_any_frame (frame);
12131
12132 if (WINDOWP (f->tool_bar_window)
12133 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12134 {
12135 update_tool_bar (f, 1);
12136 if (f->n_tool_bar_items)
12137 {
12138 build_desired_tool_bar_string (f);
12139 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12140 }
12141 }
12142 #endif
12143
12144 return make_number (height);
12145 }
12146
12147
12148 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12149 height should be changed. */
12150
12151 static int
12152 redisplay_tool_bar (struct frame *f)
12153 {
12154 #if defined (USE_GTK) || defined (HAVE_NS)
12155
12156 if (FRAME_EXTERNAL_TOOL_BAR (f))
12157 update_frame_tool_bar (f);
12158 return 0;
12159
12160 #else /* !USE_GTK && !HAVE_NS */
12161
12162 struct window *w;
12163 struct it it;
12164 struct glyph_row *row;
12165
12166 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12167 do anything. This means you must start with tool-bar-lines
12168 non-zero to get the auto-sizing effect. Or in other words, you
12169 can turn off tool-bars by specifying tool-bar-lines zero. */
12170 if (!WINDOWP (f->tool_bar_window)
12171 || (w = XWINDOW (f->tool_bar_window),
12172 WINDOW_PIXEL_HEIGHT (w) == 0))
12173 return 0;
12174
12175 /* Set up an iterator for the tool-bar window. */
12176 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12177 it.first_visible_x = 0;
12178 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12179 row = it.glyph_row;
12180
12181 /* Build a string that represents the contents of the tool-bar. */
12182 build_desired_tool_bar_string (f);
12183 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12184 /* FIXME: This should be controlled by a user option. But it
12185 doesn't make sense to have an R2L tool bar if the menu bar cannot
12186 be drawn also R2L, and making the menu bar R2L is tricky due
12187 toolkit-specific code that implements it. If an R2L tool bar is
12188 ever supported, display_tool_bar_line should also be augmented to
12189 call unproduce_glyphs like display_line and display_string
12190 do. */
12191 it.paragraph_embedding = L2R;
12192
12193 if (f->n_tool_bar_rows == 0)
12194 {
12195 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12196
12197 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12198 {
12199 Lisp_Object frame;
12200 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12201 / FRAME_LINE_HEIGHT (f));
12202
12203 XSETFRAME (frame, f);
12204 Fmodify_frame_parameters (frame,
12205 list1 (Fcons (Qtool_bar_lines,
12206 make_number (new_lines))));
12207 /* Always do that now. */
12208 clear_glyph_matrix (w->desired_matrix);
12209 f->fonts_changed = 1;
12210 return 1;
12211 }
12212 }
12213
12214 /* Display as many lines as needed to display all tool-bar items. */
12215
12216 if (f->n_tool_bar_rows > 0)
12217 {
12218 int border, rows, height, extra;
12219
12220 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12221 border = XINT (Vtool_bar_border);
12222 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12223 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12224 else if (EQ (Vtool_bar_border, Qborder_width))
12225 border = f->border_width;
12226 else
12227 border = 0;
12228 if (border < 0)
12229 border = 0;
12230
12231 rows = f->n_tool_bar_rows;
12232 height = max (1, (it.last_visible_y - border) / rows);
12233 extra = it.last_visible_y - border - height * rows;
12234
12235 while (it.current_y < it.last_visible_y)
12236 {
12237 int h = 0;
12238 if (extra > 0 && rows-- > 0)
12239 {
12240 h = (extra + rows - 1) / rows;
12241 extra -= h;
12242 }
12243 display_tool_bar_line (&it, height + h);
12244 }
12245 }
12246 else
12247 {
12248 while (it.current_y < it.last_visible_y)
12249 display_tool_bar_line (&it, 0);
12250 }
12251
12252 /* It doesn't make much sense to try scrolling in the tool-bar
12253 window, so don't do it. */
12254 w->desired_matrix->no_scrolling_p = 1;
12255 w->must_be_updated_p = 1;
12256
12257 if (!NILP (Vauto_resize_tool_bars))
12258 {
12259 /* Do we really allow the toolbar to occupy the whole frame? */
12260 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12261 int change_height_p = 0;
12262
12263 /* If we couldn't display everything, change the tool-bar's
12264 height if there is room for more. */
12265 if (IT_STRING_CHARPOS (it) < it.end_charpos
12266 && it.current_y < max_tool_bar_height)
12267 change_height_p = 1;
12268
12269 /* We subtract 1 because display_tool_bar_line advances the
12270 glyph_row pointer before returning to its caller. We want to
12271 examine the last glyph row produced by
12272 display_tool_bar_line. */
12273 row = it.glyph_row - 1;
12274
12275 /* If there are blank lines at the end, except for a partially
12276 visible blank line at the end that is smaller than
12277 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12278 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12279 && row->height >= FRAME_LINE_HEIGHT (f))
12280 change_height_p = 1;
12281
12282 /* If row displays tool-bar items, but is partially visible,
12283 change the tool-bar's height. */
12284 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12285 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12286 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12287 change_height_p = 1;
12288
12289 /* Resize windows as needed by changing the `tool-bar-lines'
12290 frame parameter. */
12291 if (change_height_p)
12292 {
12293 Lisp_Object frame;
12294 int nrows;
12295 int new_height = tool_bar_height (f, &nrows, 1);
12296
12297 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12298 && !f->minimize_tool_bar_window_p)
12299 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12300 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12301 f->minimize_tool_bar_window_p = 0;
12302
12303 if (change_height_p)
12304 {
12305 /* Current size of the tool-bar window in canonical line
12306 units. */
12307 int old_lines = WINDOW_TOTAL_LINES (w);
12308 /* Required size of the tool-bar window in canonical
12309 line units. */
12310 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12311 / FRAME_LINE_HEIGHT (f));
12312 /* Maximum size of the tool-bar window in canonical line
12313 units that this frame can allow. */
12314 int max_lines =
12315 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12316
12317 /* Don't try to change the tool-bar window size and set
12318 the fonts_changed flag unless really necessary. That
12319 flag causes redisplay to give up and retry
12320 redisplaying the frame from scratch, so setting it
12321 unnecessarily can lead to nasty redisplay loops. */
12322 if (new_lines <= max_lines
12323 && eabs (new_lines - old_lines) >= 1)
12324 {
12325 XSETFRAME (frame, f);
12326 Fmodify_frame_parameters (frame,
12327 list1 (Fcons (Qtool_bar_lines,
12328 make_number (new_lines))));
12329 clear_glyph_matrix (w->desired_matrix);
12330 f->n_tool_bar_rows = nrows;
12331 f->fonts_changed = 1;
12332 return 1;
12333 }
12334 }
12335 }
12336 }
12337
12338 f->minimize_tool_bar_window_p = 0;
12339 return 0;
12340
12341 #endif /* USE_GTK || HAVE_NS */
12342 }
12343
12344 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12345
12346 /* Get information about the tool-bar item which is displayed in GLYPH
12347 on frame F. Return in *PROP_IDX the index where tool-bar item
12348 properties start in F->tool_bar_items. Value is zero if
12349 GLYPH doesn't display a tool-bar item. */
12350
12351 static int
12352 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12353 {
12354 Lisp_Object prop;
12355 int success_p;
12356 int charpos;
12357
12358 /* This function can be called asynchronously, which means we must
12359 exclude any possibility that Fget_text_property signals an
12360 error. */
12361 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12362 charpos = max (0, charpos);
12363
12364 /* Get the text property `menu-item' at pos. The value of that
12365 property is the start index of this item's properties in
12366 F->tool_bar_items. */
12367 prop = Fget_text_property (make_number (charpos),
12368 Qmenu_item, f->current_tool_bar_string);
12369 if (INTEGERP (prop))
12370 {
12371 *prop_idx = XINT (prop);
12372 success_p = 1;
12373 }
12374 else
12375 success_p = 0;
12376
12377 return success_p;
12378 }
12379
12380 \f
12381 /* Get information about the tool-bar item at position X/Y on frame F.
12382 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12383 the current matrix of the tool-bar window of F, or NULL if not
12384 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12385 item in F->tool_bar_items. Value is
12386
12387 -1 if X/Y is not on a tool-bar item
12388 0 if X/Y is on the same item that was highlighted before.
12389 1 otherwise. */
12390
12391 static int
12392 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12393 int *hpos, int *vpos, int *prop_idx)
12394 {
12395 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12396 struct window *w = XWINDOW (f->tool_bar_window);
12397 int area;
12398
12399 /* Find the glyph under X/Y. */
12400 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12401 if (*glyph == NULL)
12402 return -1;
12403
12404 /* Get the start of this tool-bar item's properties in
12405 f->tool_bar_items. */
12406 if (!tool_bar_item_info (f, *glyph, prop_idx))
12407 return -1;
12408
12409 /* Is mouse on the highlighted item? */
12410 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12411 && *vpos >= hlinfo->mouse_face_beg_row
12412 && *vpos <= hlinfo->mouse_face_end_row
12413 && (*vpos > hlinfo->mouse_face_beg_row
12414 || *hpos >= hlinfo->mouse_face_beg_col)
12415 && (*vpos < hlinfo->mouse_face_end_row
12416 || *hpos < hlinfo->mouse_face_end_col
12417 || hlinfo->mouse_face_past_end))
12418 return 0;
12419
12420 return 1;
12421 }
12422
12423
12424 /* EXPORT:
12425 Handle mouse button event on the tool-bar of frame F, at
12426 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12427 0 for button release. MODIFIERS is event modifiers for button
12428 release. */
12429
12430 void
12431 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12432 int modifiers)
12433 {
12434 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12435 struct window *w = XWINDOW (f->tool_bar_window);
12436 int hpos, vpos, prop_idx;
12437 struct glyph *glyph;
12438 Lisp_Object enabled_p;
12439 int ts;
12440
12441 /* If not on the highlighted tool-bar item, and mouse-highlight is
12442 non-nil, return. This is so we generate the tool-bar button
12443 click only when the mouse button is released on the same item as
12444 where it was pressed. However, when mouse-highlight is disabled,
12445 generate the click when the button is released regardless of the
12446 highlight, since tool-bar items are not highlighted in that
12447 case. */
12448 frame_to_window_pixel_xy (w, &x, &y);
12449 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12450 if (ts == -1
12451 || (ts != 0 && !NILP (Vmouse_highlight)))
12452 return;
12453
12454 /* When mouse-highlight is off, generate the click for the item
12455 where the button was pressed, disregarding where it was
12456 released. */
12457 if (NILP (Vmouse_highlight) && !down_p)
12458 prop_idx = last_tool_bar_item;
12459
12460 /* If item is disabled, do nothing. */
12461 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12462 if (NILP (enabled_p))
12463 return;
12464
12465 if (down_p)
12466 {
12467 /* Show item in pressed state. */
12468 if (!NILP (Vmouse_highlight))
12469 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12470 last_tool_bar_item = prop_idx;
12471 }
12472 else
12473 {
12474 Lisp_Object key, frame;
12475 struct input_event event;
12476 EVENT_INIT (event);
12477
12478 /* Show item in released state. */
12479 if (!NILP (Vmouse_highlight))
12480 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12481
12482 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12483
12484 XSETFRAME (frame, f);
12485 event.kind = TOOL_BAR_EVENT;
12486 event.frame_or_window = frame;
12487 event.arg = frame;
12488 kbd_buffer_store_event (&event);
12489
12490 event.kind = TOOL_BAR_EVENT;
12491 event.frame_or_window = frame;
12492 event.arg = key;
12493 event.modifiers = modifiers;
12494 kbd_buffer_store_event (&event);
12495 last_tool_bar_item = -1;
12496 }
12497 }
12498
12499
12500 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12501 tool-bar window-relative coordinates X/Y. Called from
12502 note_mouse_highlight. */
12503
12504 static void
12505 note_tool_bar_highlight (struct frame *f, int x, int y)
12506 {
12507 Lisp_Object window = f->tool_bar_window;
12508 struct window *w = XWINDOW (window);
12509 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12510 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12511 int hpos, vpos;
12512 struct glyph *glyph;
12513 struct glyph_row *row;
12514 int i;
12515 Lisp_Object enabled_p;
12516 int prop_idx;
12517 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12518 int mouse_down_p, rc;
12519
12520 /* Function note_mouse_highlight is called with negative X/Y
12521 values when mouse moves outside of the frame. */
12522 if (x <= 0 || y <= 0)
12523 {
12524 clear_mouse_face (hlinfo);
12525 return;
12526 }
12527
12528 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12529 if (rc < 0)
12530 {
12531 /* Not on tool-bar item. */
12532 clear_mouse_face (hlinfo);
12533 return;
12534 }
12535 else if (rc == 0)
12536 /* On same tool-bar item as before. */
12537 goto set_help_echo;
12538
12539 clear_mouse_face (hlinfo);
12540
12541 /* Mouse is down, but on different tool-bar item? */
12542 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12543 && f == dpyinfo->last_mouse_frame);
12544
12545 if (mouse_down_p
12546 && last_tool_bar_item != prop_idx)
12547 return;
12548
12549 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12550
12551 /* If tool-bar item is not enabled, don't highlight it. */
12552 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12553 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12554 {
12555 /* Compute the x-position of the glyph. In front and past the
12556 image is a space. We include this in the highlighted area. */
12557 row = MATRIX_ROW (w->current_matrix, vpos);
12558 for (i = x = 0; i < hpos; ++i)
12559 x += row->glyphs[TEXT_AREA][i].pixel_width;
12560
12561 /* Record this as the current active region. */
12562 hlinfo->mouse_face_beg_col = hpos;
12563 hlinfo->mouse_face_beg_row = vpos;
12564 hlinfo->mouse_face_beg_x = x;
12565 hlinfo->mouse_face_past_end = 0;
12566
12567 hlinfo->mouse_face_end_col = hpos + 1;
12568 hlinfo->mouse_face_end_row = vpos;
12569 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12570 hlinfo->mouse_face_window = window;
12571 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12572
12573 /* Display it as active. */
12574 show_mouse_face (hlinfo, draw);
12575 }
12576
12577 set_help_echo:
12578
12579 /* Set help_echo_string to a help string to display for this tool-bar item.
12580 XTread_socket does the rest. */
12581 help_echo_object = help_echo_window = Qnil;
12582 help_echo_pos = -1;
12583 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12584 if (NILP (help_echo_string))
12585 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12586 }
12587
12588 #endif /* !USE_GTK && !HAVE_NS */
12589
12590 #endif /* HAVE_WINDOW_SYSTEM */
12591
12592
12593 \f
12594 /************************************************************************
12595 Horizontal scrolling
12596 ************************************************************************/
12597
12598 static int hscroll_window_tree (Lisp_Object);
12599 static int hscroll_windows (Lisp_Object);
12600
12601 /* For all leaf windows in the window tree rooted at WINDOW, set their
12602 hscroll value so that PT is (i) visible in the window, and (ii) so
12603 that it is not within a certain margin at the window's left and
12604 right border. Value is non-zero if any window's hscroll has been
12605 changed. */
12606
12607 static int
12608 hscroll_window_tree (Lisp_Object window)
12609 {
12610 int hscrolled_p = 0;
12611 int hscroll_relative_p = FLOATP (Vhscroll_step);
12612 int hscroll_step_abs = 0;
12613 double hscroll_step_rel = 0;
12614
12615 if (hscroll_relative_p)
12616 {
12617 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12618 if (hscroll_step_rel < 0)
12619 {
12620 hscroll_relative_p = 0;
12621 hscroll_step_abs = 0;
12622 }
12623 }
12624 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12625 {
12626 hscroll_step_abs = XINT (Vhscroll_step);
12627 if (hscroll_step_abs < 0)
12628 hscroll_step_abs = 0;
12629 }
12630 else
12631 hscroll_step_abs = 0;
12632
12633 while (WINDOWP (window))
12634 {
12635 struct window *w = XWINDOW (window);
12636
12637 if (WINDOWP (w->contents))
12638 hscrolled_p |= hscroll_window_tree (w->contents);
12639 else if (w->cursor.vpos >= 0)
12640 {
12641 int h_margin;
12642 int text_area_width;
12643 struct glyph_row *cursor_row;
12644 struct glyph_row *bottom_row;
12645 int row_r2l_p;
12646
12647 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12648 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12649 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12650 else
12651 cursor_row = bottom_row - 1;
12652
12653 if (!cursor_row->enabled_p)
12654 {
12655 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12656 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12657 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12658 else
12659 cursor_row = bottom_row - 1;
12660 }
12661 row_r2l_p = cursor_row->reversed_p;
12662
12663 text_area_width = window_box_width (w, TEXT_AREA);
12664
12665 /* Scroll when cursor is inside this scroll margin. */
12666 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12667
12668 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12669 /* For left-to-right rows, hscroll when cursor is either
12670 (i) inside the right hscroll margin, or (ii) if it is
12671 inside the left margin and the window is already
12672 hscrolled. */
12673 && ((!row_r2l_p
12674 && ((w->hscroll
12675 && w->cursor.x <= h_margin)
12676 || (cursor_row->enabled_p
12677 && cursor_row->truncated_on_right_p
12678 && (w->cursor.x >= text_area_width - h_margin))))
12679 /* For right-to-left rows, the logic is similar,
12680 except that rules for scrolling to left and right
12681 are reversed. E.g., if cursor.x <= h_margin, we
12682 need to hscroll "to the right" unconditionally,
12683 and that will scroll the screen to the left so as
12684 to reveal the next portion of the row. */
12685 || (row_r2l_p
12686 && ((cursor_row->enabled_p
12687 /* FIXME: It is confusing to set the
12688 truncated_on_right_p flag when R2L rows
12689 are actually truncated on the left. */
12690 && cursor_row->truncated_on_right_p
12691 && w->cursor.x <= h_margin)
12692 || (w->hscroll
12693 && (w->cursor.x >= text_area_width - h_margin))))))
12694 {
12695 struct it it;
12696 ptrdiff_t hscroll;
12697 struct buffer *saved_current_buffer;
12698 ptrdiff_t pt;
12699 int wanted_x;
12700
12701 /* Find point in a display of infinite width. */
12702 saved_current_buffer = current_buffer;
12703 current_buffer = XBUFFER (w->contents);
12704
12705 if (w == XWINDOW (selected_window))
12706 pt = PT;
12707 else
12708 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12709
12710 /* Move iterator to pt starting at cursor_row->start in
12711 a line with infinite width. */
12712 init_to_row_start (&it, w, cursor_row);
12713 it.last_visible_x = INFINITY;
12714 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12715 current_buffer = saved_current_buffer;
12716
12717 /* Position cursor in window. */
12718 if (!hscroll_relative_p && hscroll_step_abs == 0)
12719 hscroll = max (0, (it.current_x
12720 - (ITERATOR_AT_END_OF_LINE_P (&it)
12721 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12722 : (text_area_width / 2))))
12723 / FRAME_COLUMN_WIDTH (it.f);
12724 else if ((!row_r2l_p
12725 && w->cursor.x >= text_area_width - h_margin)
12726 || (row_r2l_p && w->cursor.x <= h_margin))
12727 {
12728 if (hscroll_relative_p)
12729 wanted_x = text_area_width * (1 - hscroll_step_rel)
12730 - h_margin;
12731 else
12732 wanted_x = text_area_width
12733 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12734 - h_margin;
12735 hscroll
12736 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12737 }
12738 else
12739 {
12740 if (hscroll_relative_p)
12741 wanted_x = text_area_width * hscroll_step_rel
12742 + h_margin;
12743 else
12744 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12745 + h_margin;
12746 hscroll
12747 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12748 }
12749 hscroll = max (hscroll, w->min_hscroll);
12750
12751 /* Don't prevent redisplay optimizations if hscroll
12752 hasn't changed, as it will unnecessarily slow down
12753 redisplay. */
12754 if (w->hscroll != hscroll)
12755 {
12756 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12757 w->hscroll = hscroll;
12758 hscrolled_p = 1;
12759 }
12760 }
12761 }
12762
12763 window = w->next;
12764 }
12765
12766 /* Value is non-zero if hscroll of any leaf window has been changed. */
12767 return hscrolled_p;
12768 }
12769
12770
12771 /* Set hscroll so that cursor is visible and not inside horizontal
12772 scroll margins for all windows in the tree rooted at WINDOW. See
12773 also hscroll_window_tree above. Value is non-zero if any window's
12774 hscroll has been changed. If it has, desired matrices on the frame
12775 of WINDOW are cleared. */
12776
12777 static int
12778 hscroll_windows (Lisp_Object window)
12779 {
12780 int hscrolled_p = hscroll_window_tree (window);
12781 if (hscrolled_p)
12782 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12783 return hscrolled_p;
12784 }
12785
12786
12787 \f
12788 /************************************************************************
12789 Redisplay
12790 ************************************************************************/
12791
12792 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12793 to a non-zero value. This is sometimes handy to have in a debugger
12794 session. */
12795
12796 #ifdef GLYPH_DEBUG
12797
12798 /* First and last unchanged row for try_window_id. */
12799
12800 static int debug_first_unchanged_at_end_vpos;
12801 static int debug_last_unchanged_at_beg_vpos;
12802
12803 /* Delta vpos and y. */
12804
12805 static int debug_dvpos, debug_dy;
12806
12807 /* Delta in characters and bytes for try_window_id. */
12808
12809 static ptrdiff_t debug_delta, debug_delta_bytes;
12810
12811 /* Values of window_end_pos and window_end_vpos at the end of
12812 try_window_id. */
12813
12814 static ptrdiff_t debug_end_vpos;
12815
12816 /* Append a string to W->desired_matrix->method. FMT is a printf
12817 format string. If trace_redisplay_p is true also printf the
12818 resulting string to stderr. */
12819
12820 static void debug_method_add (struct window *, char const *, ...)
12821 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12822
12823 static void
12824 debug_method_add (struct window *w, char const *fmt, ...)
12825 {
12826 void *ptr = w;
12827 char *method = w->desired_matrix->method;
12828 int len = strlen (method);
12829 int size = sizeof w->desired_matrix->method;
12830 int remaining = size - len - 1;
12831 va_list ap;
12832
12833 if (len && remaining)
12834 {
12835 method[len] = '|';
12836 --remaining, ++len;
12837 }
12838
12839 va_start (ap, fmt);
12840 vsnprintf (method + len, remaining + 1, fmt, ap);
12841 va_end (ap);
12842
12843 if (trace_redisplay_p)
12844 fprintf (stderr, "%p (%s): %s\n",
12845 ptr,
12846 ((BUFFERP (w->contents)
12847 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12848 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12849 : "no buffer"),
12850 method + len);
12851 }
12852
12853 #endif /* GLYPH_DEBUG */
12854
12855
12856 /* Value is non-zero if all changes in window W, which displays
12857 current_buffer, are in the text between START and END. START is a
12858 buffer position, END is given as a distance from Z. Used in
12859 redisplay_internal for display optimization. */
12860
12861 static int
12862 text_outside_line_unchanged_p (struct window *w,
12863 ptrdiff_t start, ptrdiff_t end)
12864 {
12865 int unchanged_p = 1;
12866
12867 /* If text or overlays have changed, see where. */
12868 if (window_outdated (w))
12869 {
12870 /* Gap in the line? */
12871 if (GPT < start || Z - GPT < end)
12872 unchanged_p = 0;
12873
12874 /* Changes start in front of the line, or end after it? */
12875 if (unchanged_p
12876 && (BEG_UNCHANGED < start - 1
12877 || END_UNCHANGED < end))
12878 unchanged_p = 0;
12879
12880 /* If selective display, can't optimize if changes start at the
12881 beginning of the line. */
12882 if (unchanged_p
12883 && INTEGERP (BVAR (current_buffer, selective_display))
12884 && XINT (BVAR (current_buffer, selective_display)) > 0
12885 && (BEG_UNCHANGED < start || GPT <= start))
12886 unchanged_p = 0;
12887
12888 /* If there are overlays at the start or end of the line, these
12889 may have overlay strings with newlines in them. A change at
12890 START, for instance, may actually concern the display of such
12891 overlay strings as well, and they are displayed on different
12892 lines. So, quickly rule out this case. (For the future, it
12893 might be desirable to implement something more telling than
12894 just BEG/END_UNCHANGED.) */
12895 if (unchanged_p)
12896 {
12897 if (BEG + BEG_UNCHANGED == start
12898 && overlay_touches_p (start))
12899 unchanged_p = 0;
12900 if (END_UNCHANGED == end
12901 && overlay_touches_p (Z - end))
12902 unchanged_p = 0;
12903 }
12904
12905 /* Under bidi reordering, adding or deleting a character in the
12906 beginning of a paragraph, before the first strong directional
12907 character, can change the base direction of the paragraph (unless
12908 the buffer specifies a fixed paragraph direction), which will
12909 require to redisplay the whole paragraph. It might be worthwhile
12910 to find the paragraph limits and widen the range of redisplayed
12911 lines to that, but for now just give up this optimization. */
12912 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12913 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12914 unchanged_p = 0;
12915 }
12916
12917 return unchanged_p;
12918 }
12919
12920
12921 /* Do a frame update, taking possible shortcuts into account. This is
12922 the main external entry point for redisplay.
12923
12924 If the last redisplay displayed an echo area message and that message
12925 is no longer requested, we clear the echo area or bring back the
12926 mini-buffer if that is in use. */
12927
12928 void
12929 redisplay (void)
12930 {
12931 redisplay_internal ();
12932 }
12933
12934
12935 static Lisp_Object
12936 overlay_arrow_string_or_property (Lisp_Object var)
12937 {
12938 Lisp_Object val;
12939
12940 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12941 return val;
12942
12943 return Voverlay_arrow_string;
12944 }
12945
12946 /* Return 1 if there are any overlay-arrows in current_buffer. */
12947 static int
12948 overlay_arrow_in_current_buffer_p (void)
12949 {
12950 Lisp_Object vlist;
12951
12952 for (vlist = Voverlay_arrow_variable_list;
12953 CONSP (vlist);
12954 vlist = XCDR (vlist))
12955 {
12956 Lisp_Object var = XCAR (vlist);
12957 Lisp_Object val;
12958
12959 if (!SYMBOLP (var))
12960 continue;
12961 val = find_symbol_value (var);
12962 if (MARKERP (val)
12963 && current_buffer == XMARKER (val)->buffer)
12964 return 1;
12965 }
12966 return 0;
12967 }
12968
12969
12970 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12971 has changed. */
12972
12973 static int
12974 overlay_arrows_changed_p (void)
12975 {
12976 Lisp_Object vlist;
12977
12978 for (vlist = Voverlay_arrow_variable_list;
12979 CONSP (vlist);
12980 vlist = XCDR (vlist))
12981 {
12982 Lisp_Object var = XCAR (vlist);
12983 Lisp_Object val, pstr;
12984
12985 if (!SYMBOLP (var))
12986 continue;
12987 val = find_symbol_value (var);
12988 if (!MARKERP (val))
12989 continue;
12990 if (! EQ (COERCE_MARKER (val),
12991 Fget (var, Qlast_arrow_position))
12992 || ! (pstr = overlay_arrow_string_or_property (var),
12993 EQ (pstr, Fget (var, Qlast_arrow_string))))
12994 return 1;
12995 }
12996 return 0;
12997 }
12998
12999 /* Mark overlay arrows to be updated on next redisplay. */
13000
13001 static void
13002 update_overlay_arrows (int up_to_date)
13003 {
13004 Lisp_Object vlist;
13005
13006 for (vlist = Voverlay_arrow_variable_list;
13007 CONSP (vlist);
13008 vlist = XCDR (vlist))
13009 {
13010 Lisp_Object var = XCAR (vlist);
13011
13012 if (!SYMBOLP (var))
13013 continue;
13014
13015 if (up_to_date > 0)
13016 {
13017 Lisp_Object val = find_symbol_value (var);
13018 Fput (var, Qlast_arrow_position,
13019 COERCE_MARKER (val));
13020 Fput (var, Qlast_arrow_string,
13021 overlay_arrow_string_or_property (var));
13022 }
13023 else if (up_to_date < 0
13024 || !NILP (Fget (var, Qlast_arrow_position)))
13025 {
13026 Fput (var, Qlast_arrow_position, Qt);
13027 Fput (var, Qlast_arrow_string, Qt);
13028 }
13029 }
13030 }
13031
13032
13033 /* Return overlay arrow string to display at row.
13034 Return integer (bitmap number) for arrow bitmap in left fringe.
13035 Return nil if no overlay arrow. */
13036
13037 static Lisp_Object
13038 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13039 {
13040 Lisp_Object vlist;
13041
13042 for (vlist = Voverlay_arrow_variable_list;
13043 CONSP (vlist);
13044 vlist = XCDR (vlist))
13045 {
13046 Lisp_Object var = XCAR (vlist);
13047 Lisp_Object val;
13048
13049 if (!SYMBOLP (var))
13050 continue;
13051
13052 val = find_symbol_value (var);
13053
13054 if (MARKERP (val)
13055 && current_buffer == XMARKER (val)->buffer
13056 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13057 {
13058 if (FRAME_WINDOW_P (it->f)
13059 /* FIXME: if ROW->reversed_p is set, this should test
13060 the right fringe, not the left one. */
13061 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13062 {
13063 #ifdef HAVE_WINDOW_SYSTEM
13064 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13065 {
13066 int fringe_bitmap;
13067 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13068 return make_number (fringe_bitmap);
13069 }
13070 #endif
13071 return make_number (-1); /* Use default arrow bitmap. */
13072 }
13073 return overlay_arrow_string_or_property (var);
13074 }
13075 }
13076
13077 return Qnil;
13078 }
13079
13080 /* Return 1 if point moved out of or into a composition. Otherwise
13081 return 0. PREV_BUF and PREV_PT are the last point buffer and
13082 position. BUF and PT are the current point buffer and position. */
13083
13084 static int
13085 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13086 struct buffer *buf, ptrdiff_t pt)
13087 {
13088 ptrdiff_t start, end;
13089 Lisp_Object prop;
13090 Lisp_Object buffer;
13091
13092 XSETBUFFER (buffer, buf);
13093 /* Check a composition at the last point if point moved within the
13094 same buffer. */
13095 if (prev_buf == buf)
13096 {
13097 if (prev_pt == pt)
13098 /* Point didn't move. */
13099 return 0;
13100
13101 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13102 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13103 && composition_valid_p (start, end, prop)
13104 && start < prev_pt && end > prev_pt)
13105 /* The last point was within the composition. Return 1 iff
13106 point moved out of the composition. */
13107 return (pt <= start || pt >= end);
13108 }
13109
13110 /* Check a composition at the current point. */
13111 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13112 && find_composition (pt, -1, &start, &end, &prop, buffer)
13113 && composition_valid_p (start, end, prop)
13114 && start < pt && end > pt);
13115 }
13116
13117 /* Reconsider the clip changes of buffer which is displayed in W. */
13118
13119 static void
13120 reconsider_clip_changes (struct window *w)
13121 {
13122 struct buffer *b = XBUFFER (w->contents);
13123
13124 if (b->clip_changed
13125 && w->window_end_valid
13126 && w->current_matrix->buffer == b
13127 && w->current_matrix->zv == BUF_ZV (b)
13128 && w->current_matrix->begv == BUF_BEGV (b))
13129 b->clip_changed = 0;
13130
13131 /* If display wasn't paused, and W is not a tool bar window, see if
13132 point has been moved into or out of a composition. In that case,
13133 we set b->clip_changed to 1 to force updating the screen. If
13134 b->clip_changed has already been set to 1, we can skip this
13135 check. */
13136 if (!b->clip_changed && w->window_end_valid)
13137 {
13138 ptrdiff_t pt = (w == XWINDOW (selected_window)
13139 ? PT : marker_position (w->pointm));
13140
13141 if ((w->current_matrix->buffer != b || pt != w->last_point)
13142 && check_point_in_composition (w->current_matrix->buffer,
13143 w->last_point, b, pt))
13144 b->clip_changed = 1;
13145 }
13146 }
13147
13148 static void
13149 propagate_buffer_redisplay (void)
13150 { /* Resetting b->text->redisplay is problematic!
13151 We can't just reset it in the case that some window that displays
13152 it has not been redisplayed; and such a window can stay
13153 unredisplayed for a long time if it's currently invisible.
13154 But we do want to reset it at the end of redisplay otherwise
13155 its displayed windows will keep being redisplayed over and over
13156 again.
13157 So we copy all b->text->redisplay flags up to their windows here,
13158 such that mark_window_display_accurate can safely reset
13159 b->text->redisplay. */
13160 Lisp_Object ws = window_list ();
13161 for (; CONSP (ws); ws = XCDR (ws))
13162 {
13163 struct window *thisw = XWINDOW (XCAR (ws));
13164 struct buffer *thisb = XBUFFER (thisw->contents);
13165 if (thisb->text->redisplay)
13166 thisw->redisplay = true;
13167 }
13168 }
13169
13170 #define STOP_POLLING \
13171 do { if (! polling_stopped_here) stop_polling (); \
13172 polling_stopped_here = 1; } while (0)
13173
13174 #define RESUME_POLLING \
13175 do { if (polling_stopped_here) start_polling (); \
13176 polling_stopped_here = 0; } while (0)
13177
13178
13179 /* Perhaps in the future avoid recentering windows if it
13180 is not necessary; currently that causes some problems. */
13181
13182 static void
13183 redisplay_internal (void)
13184 {
13185 struct window *w = XWINDOW (selected_window);
13186 struct window *sw;
13187 struct frame *fr;
13188 int pending;
13189 bool must_finish = 0, match_p;
13190 struct text_pos tlbufpos, tlendpos;
13191 int number_of_visible_frames;
13192 ptrdiff_t count;
13193 struct frame *sf;
13194 int polling_stopped_here = 0;
13195 Lisp_Object tail, frame;
13196
13197 /* True means redisplay has to consider all windows on all
13198 frames. False, only selected_window is considered. */
13199 bool consider_all_windows_p;
13200
13201 /* True means redisplay has to redisplay the miniwindow. */
13202 bool update_miniwindow_p = false;
13203
13204 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13205
13206 /* No redisplay if running in batch mode or frame is not yet fully
13207 initialized, or redisplay is explicitly turned off by setting
13208 Vinhibit_redisplay. */
13209 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13210 || !NILP (Vinhibit_redisplay))
13211 return;
13212
13213 /* Don't examine these until after testing Vinhibit_redisplay.
13214 When Emacs is shutting down, perhaps because its connection to
13215 X has dropped, we should not look at them at all. */
13216 fr = XFRAME (w->frame);
13217 sf = SELECTED_FRAME ();
13218
13219 if (!fr->glyphs_initialized_p)
13220 return;
13221
13222 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13223 if (popup_activated ())
13224 return;
13225 #endif
13226
13227 /* I don't think this happens but let's be paranoid. */
13228 if (redisplaying_p)
13229 return;
13230
13231 /* Record a function that clears redisplaying_p
13232 when we leave this function. */
13233 count = SPECPDL_INDEX ();
13234 record_unwind_protect_void (unwind_redisplay);
13235 redisplaying_p = 1;
13236 specbind (Qinhibit_free_realized_faces, Qnil);
13237
13238 /* Record this function, so it appears on the profiler's backtraces. */
13239 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13240
13241 FOR_EACH_FRAME (tail, frame)
13242 XFRAME (frame)->already_hscrolled_p = 0;
13243
13244 retry:
13245 /* Remember the currently selected window. */
13246 sw = w;
13247
13248 pending = 0;
13249 last_escape_glyph_frame = NULL;
13250 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13251 last_glyphless_glyph_frame = NULL;
13252 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13253
13254 /* If face_change_count is non-zero, init_iterator will free all
13255 realized faces, which includes the faces referenced from current
13256 matrices. So, we can't reuse current matrices in this case. */
13257 if (face_change_count)
13258 windows_or_buffers_changed = 47;
13259
13260 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13261 && FRAME_TTY (sf)->previous_frame != sf)
13262 {
13263 /* Since frames on a single ASCII terminal share the same
13264 display area, displaying a different frame means redisplay
13265 the whole thing. */
13266 SET_FRAME_GARBAGED (sf);
13267 #ifndef DOS_NT
13268 set_tty_color_mode (FRAME_TTY (sf), sf);
13269 #endif
13270 FRAME_TTY (sf)->previous_frame = sf;
13271 }
13272
13273 /* Set the visible flags for all frames. Do this before checking for
13274 resized or garbaged frames; they want to know if their frames are
13275 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13276 number_of_visible_frames = 0;
13277
13278 FOR_EACH_FRAME (tail, frame)
13279 {
13280 struct frame *f = XFRAME (frame);
13281
13282 if (FRAME_VISIBLE_P (f))
13283 {
13284 ++number_of_visible_frames;
13285 /* Adjust matrices for visible frames only. */
13286 if (f->fonts_changed)
13287 {
13288 adjust_frame_glyphs (f);
13289 f->fonts_changed = 0;
13290 }
13291 /* If cursor type has been changed on the frame
13292 other than selected, consider all frames. */
13293 if (f != sf && f->cursor_type_changed)
13294 update_mode_lines = 31;
13295 }
13296 clear_desired_matrices (f);
13297 }
13298
13299 /* Notice any pending interrupt request to change frame size. */
13300 do_pending_window_change (1);
13301
13302 /* do_pending_window_change could change the selected_window due to
13303 frame resizing which makes the selected window too small. */
13304 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13305 sw = w;
13306
13307 /* Clear frames marked as garbaged. */
13308 clear_garbaged_frames ();
13309
13310 /* Build menubar and tool-bar items. */
13311 if (NILP (Vmemory_full))
13312 prepare_menu_bars ();
13313
13314 reconsider_clip_changes (w);
13315
13316 /* In most cases selected window displays current buffer. */
13317 match_p = XBUFFER (w->contents) == current_buffer;
13318 if (match_p)
13319 {
13320 /* Detect case that we need to write or remove a star in the mode line. */
13321 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13322 w->update_mode_line = 1;
13323
13324 if (mode_line_update_needed (w))
13325 w->update_mode_line = 1;
13326 }
13327
13328 /* Normally the message* functions will have already displayed and
13329 updated the echo area, but the frame may have been trashed, or
13330 the update may have been preempted, so display the echo area
13331 again here. Checking message_cleared_p captures the case that
13332 the echo area should be cleared. */
13333 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13334 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13335 || (message_cleared_p
13336 && minibuf_level == 0
13337 /* If the mini-window is currently selected, this means the
13338 echo-area doesn't show through. */
13339 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13340 {
13341 int window_height_changed_p = echo_area_display (0);
13342
13343 if (message_cleared_p)
13344 update_miniwindow_p = true;
13345
13346 must_finish = 1;
13347
13348 /* If we don't display the current message, don't clear the
13349 message_cleared_p flag, because, if we did, we wouldn't clear
13350 the echo area in the next redisplay which doesn't preserve
13351 the echo area. */
13352 if (!display_last_displayed_message_p)
13353 message_cleared_p = 0;
13354
13355 if (window_height_changed_p)
13356 {
13357 windows_or_buffers_changed = 50;
13358
13359 /* If window configuration was changed, frames may have been
13360 marked garbaged. Clear them or we will experience
13361 surprises wrt scrolling. */
13362 clear_garbaged_frames ();
13363 }
13364 }
13365 else if (EQ (selected_window, minibuf_window)
13366 && (current_buffer->clip_changed || window_outdated (w))
13367 && resize_mini_window (w, 0))
13368 {
13369 /* Resized active mini-window to fit the size of what it is
13370 showing if its contents might have changed. */
13371 must_finish = 1;
13372
13373 /* If window configuration was changed, frames may have been
13374 marked garbaged. Clear them or we will experience
13375 surprises wrt scrolling. */
13376 clear_garbaged_frames ();
13377 }
13378
13379 if (windows_or_buffers_changed && !update_mode_lines)
13380 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13381 only the windows's contents needs to be refreshed, or whether the
13382 mode-lines also need a refresh. */
13383 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13384 ? REDISPLAY_SOME : 32);
13385
13386 /* If specs for an arrow have changed, do thorough redisplay
13387 to ensure we remove any arrow that should no longer exist. */
13388 if (overlay_arrows_changed_p ())
13389 /* Apparently, this is the only case where we update other windows,
13390 without updating other mode-lines. */
13391 windows_or_buffers_changed = 49;
13392
13393 consider_all_windows_p = (update_mode_lines
13394 || windows_or_buffers_changed);
13395
13396 #define AINC(a,i) \
13397 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13398 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13399
13400 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13401 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13402
13403 /* Optimize the case that only the line containing the cursor in the
13404 selected window has changed. Variables starting with this_ are
13405 set in display_line and record information about the line
13406 containing the cursor. */
13407 tlbufpos = this_line_start_pos;
13408 tlendpos = this_line_end_pos;
13409 if (!consider_all_windows_p
13410 && CHARPOS (tlbufpos) > 0
13411 && !w->update_mode_line
13412 && !current_buffer->clip_changed
13413 && !current_buffer->prevent_redisplay_optimizations_p
13414 && FRAME_VISIBLE_P (XFRAME (w->frame))
13415 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13416 && !XFRAME (w->frame)->cursor_type_changed
13417 /* Make sure recorded data applies to current buffer, etc. */
13418 && this_line_buffer == current_buffer
13419 && match_p
13420 && !w->force_start
13421 && !w->optional_new_start
13422 /* Point must be on the line that we have info recorded about. */
13423 && PT >= CHARPOS (tlbufpos)
13424 && PT <= Z - CHARPOS (tlendpos)
13425 /* All text outside that line, including its final newline,
13426 must be unchanged. */
13427 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13428 CHARPOS (tlendpos)))
13429 {
13430 if (CHARPOS (tlbufpos) > BEGV
13431 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13432 && (CHARPOS (tlbufpos) == ZV
13433 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13434 /* Former continuation line has disappeared by becoming empty. */
13435 goto cancel;
13436 else if (window_outdated (w) || MINI_WINDOW_P (w))
13437 {
13438 /* We have to handle the case of continuation around a
13439 wide-column character (see the comment in indent.c around
13440 line 1340).
13441
13442 For instance, in the following case:
13443
13444 -------- Insert --------
13445 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13446 J_I_ ==> J_I_ `^^' are cursors.
13447 ^^ ^^
13448 -------- --------
13449
13450 As we have to redraw the line above, we cannot use this
13451 optimization. */
13452
13453 struct it it;
13454 int line_height_before = this_line_pixel_height;
13455
13456 /* Note that start_display will handle the case that the
13457 line starting at tlbufpos is a continuation line. */
13458 start_display (&it, w, tlbufpos);
13459
13460 /* Implementation note: It this still necessary? */
13461 if (it.current_x != this_line_start_x)
13462 goto cancel;
13463
13464 TRACE ((stderr, "trying display optimization 1\n"));
13465 w->cursor.vpos = -1;
13466 overlay_arrow_seen = 0;
13467 it.vpos = this_line_vpos;
13468 it.current_y = this_line_y;
13469 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13470 display_line (&it);
13471
13472 /* If line contains point, is not continued,
13473 and ends at same distance from eob as before, we win. */
13474 if (w->cursor.vpos >= 0
13475 /* Line is not continued, otherwise this_line_start_pos
13476 would have been set to 0 in display_line. */
13477 && CHARPOS (this_line_start_pos)
13478 /* Line ends as before. */
13479 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13480 /* Line has same height as before. Otherwise other lines
13481 would have to be shifted up or down. */
13482 && this_line_pixel_height == line_height_before)
13483 {
13484 /* If this is not the window's last line, we must adjust
13485 the charstarts of the lines below. */
13486 if (it.current_y < it.last_visible_y)
13487 {
13488 struct glyph_row *row
13489 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13490 ptrdiff_t delta, delta_bytes;
13491
13492 /* We used to distinguish between two cases here,
13493 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13494 when the line ends in a newline or the end of the
13495 buffer's accessible portion. But both cases did
13496 the same, so they were collapsed. */
13497 delta = (Z
13498 - CHARPOS (tlendpos)
13499 - MATRIX_ROW_START_CHARPOS (row));
13500 delta_bytes = (Z_BYTE
13501 - BYTEPOS (tlendpos)
13502 - MATRIX_ROW_START_BYTEPOS (row));
13503
13504 increment_matrix_positions (w->current_matrix,
13505 this_line_vpos + 1,
13506 w->current_matrix->nrows,
13507 delta, delta_bytes);
13508 }
13509
13510 /* If this row displays text now but previously didn't,
13511 or vice versa, w->window_end_vpos may have to be
13512 adjusted. */
13513 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13514 {
13515 if (w->window_end_vpos < this_line_vpos)
13516 w->window_end_vpos = this_line_vpos;
13517 }
13518 else if (w->window_end_vpos == this_line_vpos
13519 && this_line_vpos > 0)
13520 w->window_end_vpos = this_line_vpos - 1;
13521 w->window_end_valid = 0;
13522
13523 /* Update hint: No need to try to scroll in update_window. */
13524 w->desired_matrix->no_scrolling_p = 1;
13525
13526 #ifdef GLYPH_DEBUG
13527 *w->desired_matrix->method = 0;
13528 debug_method_add (w, "optimization 1");
13529 #endif
13530 #ifdef HAVE_WINDOW_SYSTEM
13531 update_window_fringes (w, 0);
13532 #endif
13533 goto update;
13534 }
13535 else
13536 goto cancel;
13537 }
13538 else if (/* Cursor position hasn't changed. */
13539 PT == w->last_point
13540 /* Make sure the cursor was last displayed
13541 in this window. Otherwise we have to reposition it. */
13542
13543 /* PXW: Must be converted to pixels, probably. */
13544 && 0 <= w->cursor.vpos
13545 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13546 {
13547 if (!must_finish)
13548 {
13549 do_pending_window_change (1);
13550 /* If selected_window changed, redisplay again. */
13551 if (WINDOWP (selected_window)
13552 && (w = XWINDOW (selected_window)) != sw)
13553 goto retry;
13554
13555 /* We used to always goto end_of_redisplay here, but this
13556 isn't enough if we have a blinking cursor. */
13557 if (w->cursor_off_p == w->last_cursor_off_p)
13558 goto end_of_redisplay;
13559 }
13560 goto update;
13561 }
13562 /* If highlighting the region, or if the cursor is in the echo area,
13563 then we can't just move the cursor. */
13564 else if (NILP (Vshow_trailing_whitespace)
13565 && !cursor_in_echo_area)
13566 {
13567 struct it it;
13568 struct glyph_row *row;
13569
13570 /* Skip from tlbufpos to PT and see where it is. Note that
13571 PT may be in invisible text. If so, we will end at the
13572 next visible position. */
13573 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13574 NULL, DEFAULT_FACE_ID);
13575 it.current_x = this_line_start_x;
13576 it.current_y = this_line_y;
13577 it.vpos = this_line_vpos;
13578
13579 /* The call to move_it_to stops in front of PT, but
13580 moves over before-strings. */
13581 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13582
13583 if (it.vpos == this_line_vpos
13584 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13585 row->enabled_p))
13586 {
13587 eassert (this_line_vpos == it.vpos);
13588 eassert (this_line_y == it.current_y);
13589 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13590 #ifdef GLYPH_DEBUG
13591 *w->desired_matrix->method = 0;
13592 debug_method_add (w, "optimization 3");
13593 #endif
13594 goto update;
13595 }
13596 else
13597 goto cancel;
13598 }
13599
13600 cancel:
13601 /* Text changed drastically or point moved off of line. */
13602 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13603 }
13604
13605 CHARPOS (this_line_start_pos) = 0;
13606 ++clear_face_cache_count;
13607 #ifdef HAVE_WINDOW_SYSTEM
13608 ++clear_image_cache_count;
13609 #endif
13610
13611 /* Build desired matrices, and update the display. If
13612 consider_all_windows_p is non-zero, do it for all windows on all
13613 frames. Otherwise do it for selected_window, only. */
13614
13615 if (consider_all_windows_p)
13616 {
13617 FOR_EACH_FRAME (tail, frame)
13618 XFRAME (frame)->updated_p = 0;
13619
13620 propagate_buffer_redisplay ();
13621
13622 FOR_EACH_FRAME (tail, frame)
13623 {
13624 struct frame *f = XFRAME (frame);
13625
13626 /* We don't have to do anything for unselected terminal
13627 frames. */
13628 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13629 && !EQ (FRAME_TTY (f)->top_frame, frame))
13630 continue;
13631
13632 retry_frame:
13633
13634 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13635 {
13636 bool gcscrollbars
13637 /* Only GC scrollbars when we redisplay the whole frame. */
13638 = f->redisplay || !REDISPLAY_SOME_P ();
13639 /* Mark all the scroll bars to be removed; we'll redeem
13640 the ones we want when we redisplay their windows. */
13641 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13642 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13643
13644 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13645 redisplay_windows (FRAME_ROOT_WINDOW (f));
13646 /* Remember that the invisible frames need to be redisplayed next
13647 time they're visible. */
13648 else if (!REDISPLAY_SOME_P ())
13649 f->redisplay = true;
13650
13651 /* The X error handler may have deleted that frame. */
13652 if (!FRAME_LIVE_P (f))
13653 continue;
13654
13655 /* Any scroll bars which redisplay_windows should have
13656 nuked should now go away. */
13657 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13658 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13659
13660 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13661 {
13662 /* If fonts changed on visible frame, display again. */
13663 if (f->fonts_changed)
13664 {
13665 adjust_frame_glyphs (f);
13666 f->fonts_changed = 0;
13667 goto retry_frame;
13668 }
13669
13670 /* See if we have to hscroll. */
13671 if (!f->already_hscrolled_p)
13672 {
13673 f->already_hscrolled_p = 1;
13674 if (hscroll_windows (f->root_window))
13675 goto retry_frame;
13676 }
13677
13678 /* Prevent various kinds of signals during display
13679 update. stdio is not robust about handling
13680 signals, which can cause an apparent I/O error. */
13681 if (interrupt_input)
13682 unrequest_sigio ();
13683 STOP_POLLING;
13684
13685 pending |= update_frame (f, 0, 0);
13686 f->cursor_type_changed = 0;
13687 f->updated_p = 1;
13688 }
13689 }
13690 }
13691
13692 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13693
13694 if (!pending)
13695 {
13696 /* Do the mark_window_display_accurate after all windows have
13697 been redisplayed because this call resets flags in buffers
13698 which are needed for proper redisplay. */
13699 FOR_EACH_FRAME (tail, frame)
13700 {
13701 struct frame *f = XFRAME (frame);
13702 if (f->updated_p)
13703 {
13704 f->redisplay = false;
13705 mark_window_display_accurate (f->root_window, 1);
13706 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13707 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13708 }
13709 }
13710 }
13711 }
13712 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13713 {
13714 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13715 struct frame *mini_frame;
13716
13717 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13718 /* Use list_of_error, not Qerror, so that
13719 we catch only errors and don't run the debugger. */
13720 internal_condition_case_1 (redisplay_window_1, selected_window,
13721 list_of_error,
13722 redisplay_window_error);
13723 if (update_miniwindow_p)
13724 internal_condition_case_1 (redisplay_window_1, mini_window,
13725 list_of_error,
13726 redisplay_window_error);
13727
13728 /* Compare desired and current matrices, perform output. */
13729
13730 update:
13731 /* If fonts changed, display again. */
13732 if (sf->fonts_changed)
13733 goto retry;
13734
13735 /* Prevent various kinds of signals during display update.
13736 stdio is not robust about handling signals,
13737 which can cause an apparent I/O error. */
13738 if (interrupt_input)
13739 unrequest_sigio ();
13740 STOP_POLLING;
13741
13742 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13743 {
13744 if (hscroll_windows (selected_window))
13745 goto retry;
13746
13747 XWINDOW (selected_window)->must_be_updated_p = true;
13748 pending = update_frame (sf, 0, 0);
13749 sf->cursor_type_changed = 0;
13750 }
13751
13752 /* We may have called echo_area_display at the top of this
13753 function. If the echo area is on another frame, that may
13754 have put text on a frame other than the selected one, so the
13755 above call to update_frame would not have caught it. Catch
13756 it here. */
13757 mini_window = FRAME_MINIBUF_WINDOW (sf);
13758 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13759
13760 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13761 {
13762 XWINDOW (mini_window)->must_be_updated_p = true;
13763 pending |= update_frame (mini_frame, 0, 0);
13764 mini_frame->cursor_type_changed = 0;
13765 if (!pending && hscroll_windows (mini_window))
13766 goto retry;
13767 }
13768 }
13769
13770 /* If display was paused because of pending input, make sure we do a
13771 thorough update the next time. */
13772 if (pending)
13773 {
13774 /* Prevent the optimization at the beginning of
13775 redisplay_internal that tries a single-line update of the
13776 line containing the cursor in the selected window. */
13777 CHARPOS (this_line_start_pos) = 0;
13778
13779 /* Let the overlay arrow be updated the next time. */
13780 update_overlay_arrows (0);
13781
13782 /* If we pause after scrolling, some rows in the current
13783 matrices of some windows are not valid. */
13784 if (!WINDOW_FULL_WIDTH_P (w)
13785 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13786 update_mode_lines = 36;
13787 }
13788 else
13789 {
13790 if (!consider_all_windows_p)
13791 {
13792 /* This has already been done above if
13793 consider_all_windows_p is set. */
13794 if (XBUFFER (w->contents)->text->redisplay
13795 && buffer_window_count (XBUFFER (w->contents)) > 1)
13796 /* This can happen if b->text->redisplay was set during
13797 jit-lock. */
13798 propagate_buffer_redisplay ();
13799 mark_window_display_accurate_1 (w, 1);
13800
13801 /* Say overlay arrows are up to date. */
13802 update_overlay_arrows (1);
13803
13804 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13805 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13806 }
13807
13808 update_mode_lines = 0;
13809 windows_or_buffers_changed = 0;
13810 }
13811
13812 /* Start SIGIO interrupts coming again. Having them off during the
13813 code above makes it less likely one will discard output, but not
13814 impossible, since there might be stuff in the system buffer here.
13815 But it is much hairier to try to do anything about that. */
13816 if (interrupt_input)
13817 request_sigio ();
13818 RESUME_POLLING;
13819
13820 /* If a frame has become visible which was not before, redisplay
13821 again, so that we display it. Expose events for such a frame
13822 (which it gets when becoming visible) don't call the parts of
13823 redisplay constructing glyphs, so simply exposing a frame won't
13824 display anything in this case. So, we have to display these
13825 frames here explicitly. */
13826 if (!pending)
13827 {
13828 int new_count = 0;
13829
13830 FOR_EACH_FRAME (tail, frame)
13831 {
13832 if (XFRAME (frame)->visible)
13833 new_count++;
13834 }
13835
13836 if (new_count != number_of_visible_frames)
13837 windows_or_buffers_changed = 52;
13838 }
13839
13840 /* Change frame size now if a change is pending. */
13841 do_pending_window_change (1);
13842
13843 /* If we just did a pending size change, or have additional
13844 visible frames, or selected_window changed, redisplay again. */
13845 if ((windows_or_buffers_changed && !pending)
13846 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13847 goto retry;
13848
13849 /* Clear the face and image caches.
13850
13851 We used to do this only if consider_all_windows_p. But the cache
13852 needs to be cleared if a timer creates images in the current
13853 buffer (e.g. the test case in Bug#6230). */
13854
13855 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13856 {
13857 clear_face_cache (0);
13858 clear_face_cache_count = 0;
13859 }
13860
13861 #ifdef HAVE_WINDOW_SYSTEM
13862 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13863 {
13864 clear_image_caches (Qnil);
13865 clear_image_cache_count = 0;
13866 }
13867 #endif /* HAVE_WINDOW_SYSTEM */
13868
13869 end_of_redisplay:
13870 if (interrupt_input && interrupts_deferred)
13871 request_sigio ();
13872
13873 unbind_to (count, Qnil);
13874 RESUME_POLLING;
13875 }
13876
13877
13878 /* Redisplay, but leave alone any recent echo area message unless
13879 another message has been requested in its place.
13880
13881 This is useful in situations where you need to redisplay but no
13882 user action has occurred, making it inappropriate for the message
13883 area to be cleared. See tracking_off and
13884 wait_reading_process_output for examples of these situations.
13885
13886 FROM_WHERE is an integer saying from where this function was
13887 called. This is useful for debugging. */
13888
13889 void
13890 redisplay_preserve_echo_area (int from_where)
13891 {
13892 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13893
13894 if (!NILP (echo_area_buffer[1]))
13895 {
13896 /* We have a previously displayed message, but no current
13897 message. Redisplay the previous message. */
13898 display_last_displayed_message_p = 1;
13899 redisplay_internal ();
13900 display_last_displayed_message_p = 0;
13901 }
13902 else
13903 redisplay_internal ();
13904
13905 flush_frame (SELECTED_FRAME ());
13906 }
13907
13908
13909 /* Function registered with record_unwind_protect in redisplay_internal. */
13910
13911 static void
13912 unwind_redisplay (void)
13913 {
13914 redisplaying_p = 0;
13915 }
13916
13917
13918 /* Mark the display of leaf window W as accurate or inaccurate.
13919 If ACCURATE_P is non-zero mark display of W as accurate. If
13920 ACCURATE_P is zero, arrange for W to be redisplayed the next
13921 time redisplay_internal is called. */
13922
13923 static void
13924 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13925 {
13926 struct buffer *b = XBUFFER (w->contents);
13927
13928 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13929 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13930 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13931
13932 if (accurate_p)
13933 {
13934 b->clip_changed = false;
13935 b->prevent_redisplay_optimizations_p = false;
13936 eassert (buffer_window_count (b) > 0);
13937 /* Resetting b->text->redisplay is problematic!
13938 In order to make it safer to do it here, redisplay_internal must
13939 have copied all b->text->redisplay to their respective windows. */
13940 b->text->redisplay = false;
13941
13942 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13943 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13944 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13945 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13946
13947 w->current_matrix->buffer = b;
13948 w->current_matrix->begv = BUF_BEGV (b);
13949 w->current_matrix->zv = BUF_ZV (b);
13950
13951 w->last_cursor_vpos = w->cursor.vpos;
13952 w->last_cursor_off_p = w->cursor_off_p;
13953
13954 if (w == XWINDOW (selected_window))
13955 w->last_point = BUF_PT (b);
13956 else
13957 w->last_point = marker_position (w->pointm);
13958
13959 w->window_end_valid = true;
13960 w->update_mode_line = false;
13961 }
13962
13963 w->redisplay = !accurate_p;
13964 }
13965
13966
13967 /* Mark the display of windows in the window tree rooted at WINDOW as
13968 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13969 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13970 be redisplayed the next time redisplay_internal is called. */
13971
13972 void
13973 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13974 {
13975 struct window *w;
13976
13977 for (; !NILP (window); window = w->next)
13978 {
13979 w = XWINDOW (window);
13980 if (WINDOWP (w->contents))
13981 mark_window_display_accurate (w->contents, accurate_p);
13982 else
13983 mark_window_display_accurate_1 (w, accurate_p);
13984 }
13985
13986 if (accurate_p)
13987 update_overlay_arrows (1);
13988 else
13989 /* Force a thorough redisplay the next time by setting
13990 last_arrow_position and last_arrow_string to t, which is
13991 unequal to any useful value of Voverlay_arrow_... */
13992 update_overlay_arrows (-1);
13993 }
13994
13995
13996 /* Return value in display table DP (Lisp_Char_Table *) for character
13997 C. Since a display table doesn't have any parent, we don't have to
13998 follow parent. Do not call this function directly but use the
13999 macro DISP_CHAR_VECTOR. */
14000
14001 Lisp_Object
14002 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14003 {
14004 Lisp_Object val;
14005
14006 if (ASCII_CHAR_P (c))
14007 {
14008 val = dp->ascii;
14009 if (SUB_CHAR_TABLE_P (val))
14010 val = XSUB_CHAR_TABLE (val)->contents[c];
14011 }
14012 else
14013 {
14014 Lisp_Object table;
14015
14016 XSETCHAR_TABLE (table, dp);
14017 val = char_table_ref (table, c);
14018 }
14019 if (NILP (val))
14020 val = dp->defalt;
14021 return val;
14022 }
14023
14024
14025 \f
14026 /***********************************************************************
14027 Window Redisplay
14028 ***********************************************************************/
14029
14030 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14031
14032 static void
14033 redisplay_windows (Lisp_Object window)
14034 {
14035 while (!NILP (window))
14036 {
14037 struct window *w = XWINDOW (window);
14038
14039 if (WINDOWP (w->contents))
14040 redisplay_windows (w->contents);
14041 else if (BUFFERP (w->contents))
14042 {
14043 displayed_buffer = XBUFFER (w->contents);
14044 /* Use list_of_error, not Qerror, so that
14045 we catch only errors and don't run the debugger. */
14046 internal_condition_case_1 (redisplay_window_0, window,
14047 list_of_error,
14048 redisplay_window_error);
14049 }
14050
14051 window = w->next;
14052 }
14053 }
14054
14055 static Lisp_Object
14056 redisplay_window_error (Lisp_Object ignore)
14057 {
14058 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14059 return Qnil;
14060 }
14061
14062 static Lisp_Object
14063 redisplay_window_0 (Lisp_Object window)
14064 {
14065 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14066 redisplay_window (window, false);
14067 return Qnil;
14068 }
14069
14070 static Lisp_Object
14071 redisplay_window_1 (Lisp_Object window)
14072 {
14073 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14074 redisplay_window (window, true);
14075 return Qnil;
14076 }
14077 \f
14078
14079 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14080 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14081 which positions recorded in ROW differ from current buffer
14082 positions.
14083
14084 Return 0 if cursor is not on this row, 1 otherwise. */
14085
14086 static int
14087 set_cursor_from_row (struct window *w, struct glyph_row *row,
14088 struct glyph_matrix *matrix,
14089 ptrdiff_t delta, ptrdiff_t delta_bytes,
14090 int dy, int dvpos)
14091 {
14092 struct glyph *glyph = row->glyphs[TEXT_AREA];
14093 struct glyph *end = glyph + row->used[TEXT_AREA];
14094 struct glyph *cursor = NULL;
14095 /* The last known character position in row. */
14096 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14097 int x = row->x;
14098 ptrdiff_t pt_old = PT - delta;
14099 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14100 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14101 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14102 /* A glyph beyond the edge of TEXT_AREA which we should never
14103 touch. */
14104 struct glyph *glyphs_end = end;
14105 /* Non-zero means we've found a match for cursor position, but that
14106 glyph has the avoid_cursor_p flag set. */
14107 int match_with_avoid_cursor = 0;
14108 /* Non-zero means we've seen at least one glyph that came from a
14109 display string. */
14110 int string_seen = 0;
14111 /* Largest and smallest buffer positions seen so far during scan of
14112 glyph row. */
14113 ptrdiff_t bpos_max = pos_before;
14114 ptrdiff_t bpos_min = pos_after;
14115 /* Last buffer position covered by an overlay string with an integer
14116 `cursor' property. */
14117 ptrdiff_t bpos_covered = 0;
14118 /* Non-zero means the display string on which to display the cursor
14119 comes from a text property, not from an overlay. */
14120 int string_from_text_prop = 0;
14121
14122 /* Don't even try doing anything if called for a mode-line or
14123 header-line row, since the rest of the code isn't prepared to
14124 deal with such calamities. */
14125 eassert (!row->mode_line_p);
14126 if (row->mode_line_p)
14127 return 0;
14128
14129 /* Skip over glyphs not having an object at the start and the end of
14130 the row. These are special glyphs like truncation marks on
14131 terminal frames. */
14132 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14133 {
14134 if (!row->reversed_p)
14135 {
14136 while (glyph < end
14137 && INTEGERP (glyph->object)
14138 && glyph->charpos < 0)
14139 {
14140 x += glyph->pixel_width;
14141 ++glyph;
14142 }
14143 while (end > glyph
14144 && INTEGERP ((end - 1)->object)
14145 /* CHARPOS is zero for blanks and stretch glyphs
14146 inserted by extend_face_to_end_of_line. */
14147 && (end - 1)->charpos <= 0)
14148 --end;
14149 glyph_before = glyph - 1;
14150 glyph_after = end;
14151 }
14152 else
14153 {
14154 struct glyph *g;
14155
14156 /* If the glyph row is reversed, we need to process it from back
14157 to front, so swap the edge pointers. */
14158 glyphs_end = end = glyph - 1;
14159 glyph += row->used[TEXT_AREA] - 1;
14160
14161 while (glyph > end + 1
14162 && INTEGERP (glyph->object)
14163 && glyph->charpos < 0)
14164 {
14165 --glyph;
14166 x -= glyph->pixel_width;
14167 }
14168 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14169 --glyph;
14170 /* By default, in reversed rows we put the cursor on the
14171 rightmost (first in the reading order) glyph. */
14172 for (g = end + 1; g < glyph; g++)
14173 x += g->pixel_width;
14174 while (end < glyph
14175 && INTEGERP ((end + 1)->object)
14176 && (end + 1)->charpos <= 0)
14177 ++end;
14178 glyph_before = glyph + 1;
14179 glyph_after = end;
14180 }
14181 }
14182 else if (row->reversed_p)
14183 {
14184 /* In R2L rows that don't display text, put the cursor on the
14185 rightmost glyph. Case in point: an empty last line that is
14186 part of an R2L paragraph. */
14187 cursor = end - 1;
14188 /* Avoid placing the cursor on the last glyph of the row, where
14189 on terminal frames we hold the vertical border between
14190 adjacent windows. */
14191 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14192 && !WINDOW_RIGHTMOST_P (w)
14193 && cursor == row->glyphs[LAST_AREA] - 1)
14194 cursor--;
14195 x = -1; /* will be computed below, at label compute_x */
14196 }
14197
14198 /* Step 1: Try to find the glyph whose character position
14199 corresponds to point. If that's not possible, find 2 glyphs
14200 whose character positions are the closest to point, one before
14201 point, the other after it. */
14202 if (!row->reversed_p)
14203 while (/* not marched to end of glyph row */
14204 glyph < end
14205 /* glyph was not inserted by redisplay for internal purposes */
14206 && !INTEGERP (glyph->object))
14207 {
14208 if (BUFFERP (glyph->object))
14209 {
14210 ptrdiff_t dpos = glyph->charpos - pt_old;
14211
14212 if (glyph->charpos > bpos_max)
14213 bpos_max = glyph->charpos;
14214 if (glyph->charpos < bpos_min)
14215 bpos_min = glyph->charpos;
14216 if (!glyph->avoid_cursor_p)
14217 {
14218 /* If we hit point, we've found the glyph on which to
14219 display the cursor. */
14220 if (dpos == 0)
14221 {
14222 match_with_avoid_cursor = 0;
14223 break;
14224 }
14225 /* See if we've found a better approximation to
14226 POS_BEFORE or to POS_AFTER. */
14227 if (0 > dpos && dpos > pos_before - pt_old)
14228 {
14229 pos_before = glyph->charpos;
14230 glyph_before = glyph;
14231 }
14232 else if (0 < dpos && dpos < pos_after - pt_old)
14233 {
14234 pos_after = glyph->charpos;
14235 glyph_after = glyph;
14236 }
14237 }
14238 else if (dpos == 0)
14239 match_with_avoid_cursor = 1;
14240 }
14241 else if (STRINGP (glyph->object))
14242 {
14243 Lisp_Object chprop;
14244 ptrdiff_t glyph_pos = glyph->charpos;
14245
14246 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14247 glyph->object);
14248 if (!NILP (chprop))
14249 {
14250 /* If the string came from a `display' text property,
14251 look up the buffer position of that property and
14252 use that position to update bpos_max, as if we
14253 actually saw such a position in one of the row's
14254 glyphs. This helps with supporting integer values
14255 of `cursor' property on the display string in
14256 situations where most or all of the row's buffer
14257 text is completely covered by display properties,
14258 so that no glyph with valid buffer positions is
14259 ever seen in the row. */
14260 ptrdiff_t prop_pos =
14261 string_buffer_position_lim (glyph->object, pos_before,
14262 pos_after, 0);
14263
14264 if (prop_pos >= pos_before)
14265 bpos_max = prop_pos - 1;
14266 }
14267 if (INTEGERP (chprop))
14268 {
14269 bpos_covered = bpos_max + XINT (chprop);
14270 /* If the `cursor' property covers buffer positions up
14271 to and including point, we should display cursor on
14272 this glyph. Note that, if a `cursor' property on one
14273 of the string's characters has an integer value, we
14274 will break out of the loop below _before_ we get to
14275 the position match above. IOW, integer values of
14276 the `cursor' property override the "exact match for
14277 point" strategy of positioning the cursor. */
14278 /* Implementation note: bpos_max == pt_old when, e.g.,
14279 we are in an empty line, where bpos_max is set to
14280 MATRIX_ROW_START_CHARPOS, see above. */
14281 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14282 {
14283 cursor = glyph;
14284 break;
14285 }
14286 }
14287
14288 string_seen = 1;
14289 }
14290 x += glyph->pixel_width;
14291 ++glyph;
14292 }
14293 else if (glyph > end) /* row is reversed */
14294 while (!INTEGERP (glyph->object))
14295 {
14296 if (BUFFERP (glyph->object))
14297 {
14298 ptrdiff_t dpos = glyph->charpos - pt_old;
14299
14300 if (glyph->charpos > bpos_max)
14301 bpos_max = glyph->charpos;
14302 if (glyph->charpos < bpos_min)
14303 bpos_min = glyph->charpos;
14304 if (!glyph->avoid_cursor_p)
14305 {
14306 if (dpos == 0)
14307 {
14308 match_with_avoid_cursor = 0;
14309 break;
14310 }
14311 if (0 > dpos && dpos > pos_before - pt_old)
14312 {
14313 pos_before = glyph->charpos;
14314 glyph_before = glyph;
14315 }
14316 else if (0 < dpos && dpos < pos_after - pt_old)
14317 {
14318 pos_after = glyph->charpos;
14319 glyph_after = glyph;
14320 }
14321 }
14322 else if (dpos == 0)
14323 match_with_avoid_cursor = 1;
14324 }
14325 else if (STRINGP (glyph->object))
14326 {
14327 Lisp_Object chprop;
14328 ptrdiff_t glyph_pos = glyph->charpos;
14329
14330 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14331 glyph->object);
14332 if (!NILP (chprop))
14333 {
14334 ptrdiff_t prop_pos =
14335 string_buffer_position_lim (glyph->object, pos_before,
14336 pos_after, 0);
14337
14338 if (prop_pos >= pos_before)
14339 bpos_max = prop_pos - 1;
14340 }
14341 if (INTEGERP (chprop))
14342 {
14343 bpos_covered = bpos_max + XINT (chprop);
14344 /* If the `cursor' property covers buffer positions up
14345 to and including point, we should display cursor on
14346 this glyph. */
14347 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14348 {
14349 cursor = glyph;
14350 break;
14351 }
14352 }
14353 string_seen = 1;
14354 }
14355 --glyph;
14356 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14357 {
14358 x--; /* can't use any pixel_width */
14359 break;
14360 }
14361 x -= glyph->pixel_width;
14362 }
14363
14364 /* Step 2: If we didn't find an exact match for point, we need to
14365 look for a proper place to put the cursor among glyphs between
14366 GLYPH_BEFORE and GLYPH_AFTER. */
14367 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14368 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14369 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14370 {
14371 /* An empty line has a single glyph whose OBJECT is zero and
14372 whose CHARPOS is the position of a newline on that line.
14373 Note that on a TTY, there are more glyphs after that, which
14374 were produced by extend_face_to_end_of_line, but their
14375 CHARPOS is zero or negative. */
14376 int empty_line_p =
14377 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14378 && INTEGERP (glyph->object) && glyph->charpos > 0
14379 /* On a TTY, continued and truncated rows also have a glyph at
14380 their end whose OBJECT is zero and whose CHARPOS is
14381 positive (the continuation and truncation glyphs), but such
14382 rows are obviously not "empty". */
14383 && !(row->continued_p || row->truncated_on_right_p);
14384
14385 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14386 {
14387 ptrdiff_t ellipsis_pos;
14388
14389 /* Scan back over the ellipsis glyphs. */
14390 if (!row->reversed_p)
14391 {
14392 ellipsis_pos = (glyph - 1)->charpos;
14393 while (glyph > row->glyphs[TEXT_AREA]
14394 && (glyph - 1)->charpos == ellipsis_pos)
14395 glyph--, x -= glyph->pixel_width;
14396 /* That loop always goes one position too far, including
14397 the glyph before the ellipsis. So scan forward over
14398 that one. */
14399 x += glyph->pixel_width;
14400 glyph++;
14401 }
14402 else /* row is reversed */
14403 {
14404 ellipsis_pos = (glyph + 1)->charpos;
14405 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14406 && (glyph + 1)->charpos == ellipsis_pos)
14407 glyph++, x += glyph->pixel_width;
14408 x -= glyph->pixel_width;
14409 glyph--;
14410 }
14411 }
14412 else if (match_with_avoid_cursor)
14413 {
14414 cursor = glyph_after;
14415 x = -1;
14416 }
14417 else if (string_seen)
14418 {
14419 int incr = row->reversed_p ? -1 : +1;
14420
14421 /* Need to find the glyph that came out of a string which is
14422 present at point. That glyph is somewhere between
14423 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14424 positioned between POS_BEFORE and POS_AFTER in the
14425 buffer. */
14426 struct glyph *start, *stop;
14427 ptrdiff_t pos = pos_before;
14428
14429 x = -1;
14430
14431 /* If the row ends in a newline from a display string,
14432 reordering could have moved the glyphs belonging to the
14433 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14434 in this case we extend the search to the last glyph in
14435 the row that was not inserted by redisplay. */
14436 if (row->ends_in_newline_from_string_p)
14437 {
14438 glyph_after = end;
14439 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14440 }
14441
14442 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14443 correspond to POS_BEFORE and POS_AFTER, respectively. We
14444 need START and STOP in the order that corresponds to the
14445 row's direction as given by its reversed_p flag. If the
14446 directionality of characters between POS_BEFORE and
14447 POS_AFTER is the opposite of the row's base direction,
14448 these characters will have been reordered for display,
14449 and we need to reverse START and STOP. */
14450 if (!row->reversed_p)
14451 {
14452 start = min (glyph_before, glyph_after);
14453 stop = max (glyph_before, glyph_after);
14454 }
14455 else
14456 {
14457 start = max (glyph_before, glyph_after);
14458 stop = min (glyph_before, glyph_after);
14459 }
14460 for (glyph = start + incr;
14461 row->reversed_p ? glyph > stop : glyph < stop; )
14462 {
14463
14464 /* Any glyphs that come from the buffer are here because
14465 of bidi reordering. Skip them, and only pay
14466 attention to glyphs that came from some string. */
14467 if (STRINGP (glyph->object))
14468 {
14469 Lisp_Object str;
14470 ptrdiff_t tem;
14471 /* If the display property covers the newline, we
14472 need to search for it one position farther. */
14473 ptrdiff_t lim = pos_after
14474 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14475
14476 string_from_text_prop = 0;
14477 str = glyph->object;
14478 tem = string_buffer_position_lim (str, pos, lim, 0);
14479 if (tem == 0 /* from overlay */
14480 || pos <= tem)
14481 {
14482 /* If the string from which this glyph came is
14483 found in the buffer at point, or at position
14484 that is closer to point than pos_after, then
14485 we've found the glyph we've been looking for.
14486 If it comes from an overlay (tem == 0), and
14487 it has the `cursor' property on one of its
14488 glyphs, record that glyph as a candidate for
14489 displaying the cursor. (As in the
14490 unidirectional version, we will display the
14491 cursor on the last candidate we find.) */
14492 if (tem == 0
14493 || tem == pt_old
14494 || (tem - pt_old > 0 && tem < pos_after))
14495 {
14496 /* The glyphs from this string could have
14497 been reordered. Find the one with the
14498 smallest string position. Or there could
14499 be a character in the string with the
14500 `cursor' property, which means display
14501 cursor on that character's glyph. */
14502 ptrdiff_t strpos = glyph->charpos;
14503
14504 if (tem)
14505 {
14506 cursor = glyph;
14507 string_from_text_prop = 1;
14508 }
14509 for ( ;
14510 (row->reversed_p ? glyph > stop : glyph < stop)
14511 && EQ (glyph->object, str);
14512 glyph += incr)
14513 {
14514 Lisp_Object cprop;
14515 ptrdiff_t gpos = glyph->charpos;
14516
14517 cprop = Fget_char_property (make_number (gpos),
14518 Qcursor,
14519 glyph->object);
14520 if (!NILP (cprop))
14521 {
14522 cursor = glyph;
14523 break;
14524 }
14525 if (tem && glyph->charpos < strpos)
14526 {
14527 strpos = glyph->charpos;
14528 cursor = glyph;
14529 }
14530 }
14531
14532 if (tem == pt_old
14533 || (tem - pt_old > 0 && tem < pos_after))
14534 goto compute_x;
14535 }
14536 if (tem)
14537 pos = tem + 1; /* don't find previous instances */
14538 }
14539 /* This string is not what we want; skip all of the
14540 glyphs that came from it. */
14541 while ((row->reversed_p ? glyph > stop : glyph < stop)
14542 && EQ (glyph->object, str))
14543 glyph += incr;
14544 }
14545 else
14546 glyph += incr;
14547 }
14548
14549 /* If we reached the end of the line, and END was from a string,
14550 the cursor is not on this line. */
14551 if (cursor == NULL
14552 && (row->reversed_p ? glyph <= end : glyph >= end)
14553 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14554 && STRINGP (end->object)
14555 && row->continued_p)
14556 return 0;
14557 }
14558 /* A truncated row may not include PT among its character positions.
14559 Setting the cursor inside the scroll margin will trigger
14560 recalculation of hscroll in hscroll_window_tree. But if a
14561 display string covers point, defer to the string-handling
14562 code below to figure this out. */
14563 else if (row->truncated_on_left_p && pt_old < bpos_min)
14564 {
14565 cursor = glyph_before;
14566 x = -1;
14567 }
14568 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14569 /* Zero-width characters produce no glyphs. */
14570 || (!empty_line_p
14571 && (row->reversed_p
14572 ? glyph_after > glyphs_end
14573 : glyph_after < glyphs_end)))
14574 {
14575 cursor = glyph_after;
14576 x = -1;
14577 }
14578 }
14579
14580 compute_x:
14581 if (cursor != NULL)
14582 glyph = cursor;
14583 else if (glyph == glyphs_end
14584 && pos_before == pos_after
14585 && STRINGP ((row->reversed_p
14586 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14587 : row->glyphs[TEXT_AREA])->object))
14588 {
14589 /* If all the glyphs of this row came from strings, put the
14590 cursor on the first glyph of the row. This avoids having the
14591 cursor outside of the text area in this very rare and hard
14592 use case. */
14593 glyph =
14594 row->reversed_p
14595 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14596 : row->glyphs[TEXT_AREA];
14597 }
14598 if (x < 0)
14599 {
14600 struct glyph *g;
14601
14602 /* Need to compute x that corresponds to GLYPH. */
14603 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14604 {
14605 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14606 emacs_abort ();
14607 x += g->pixel_width;
14608 }
14609 }
14610
14611 /* ROW could be part of a continued line, which, under bidi
14612 reordering, might have other rows whose start and end charpos
14613 occlude point. Only set w->cursor if we found a better
14614 approximation to the cursor position than we have from previously
14615 examined candidate rows belonging to the same continued line. */
14616 if (/* We already have a candidate row. */
14617 w->cursor.vpos >= 0
14618 /* That candidate is not the row we are processing. */
14619 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14620 /* Make sure cursor.vpos specifies a row whose start and end
14621 charpos occlude point, and it is valid candidate for being a
14622 cursor-row. This is because some callers of this function
14623 leave cursor.vpos at the row where the cursor was displayed
14624 during the last redisplay cycle. */
14625 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14626 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14627 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14628 {
14629 struct glyph *g1
14630 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14631
14632 /* Don't consider glyphs that are outside TEXT_AREA. */
14633 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14634 return 0;
14635 /* Keep the candidate whose buffer position is the closest to
14636 point or has the `cursor' property. */
14637 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14638 w->cursor.hpos >= 0
14639 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14640 && ((BUFFERP (g1->object)
14641 && (g1->charpos == pt_old /* An exact match always wins. */
14642 || (BUFFERP (glyph->object)
14643 && eabs (g1->charpos - pt_old)
14644 < eabs (glyph->charpos - pt_old))))
14645 /* Previous candidate is a glyph from a string that has
14646 a non-nil `cursor' property. */
14647 || (STRINGP (g1->object)
14648 && (!NILP (Fget_char_property (make_number (g1->charpos),
14649 Qcursor, g1->object))
14650 /* Previous candidate is from the same display
14651 string as this one, and the display string
14652 came from a text property. */
14653 || (EQ (g1->object, glyph->object)
14654 && string_from_text_prop)
14655 /* this candidate is from newline and its
14656 position is not an exact match */
14657 || (INTEGERP (glyph->object)
14658 && glyph->charpos != pt_old)))))
14659 return 0;
14660 /* If this candidate gives an exact match, use that. */
14661 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14662 /* If this candidate is a glyph created for the
14663 terminating newline of a line, and point is on that
14664 newline, it wins because it's an exact match. */
14665 || (!row->continued_p
14666 && INTEGERP (glyph->object)
14667 && glyph->charpos == 0
14668 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14669 /* Otherwise, keep the candidate that comes from a row
14670 spanning less buffer positions. This may win when one or
14671 both candidate positions are on glyphs that came from
14672 display strings, for which we cannot compare buffer
14673 positions. */
14674 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14675 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14676 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14677 return 0;
14678 }
14679 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14680 w->cursor.x = x;
14681 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14682 w->cursor.y = row->y + dy;
14683
14684 if (w == XWINDOW (selected_window))
14685 {
14686 if (!row->continued_p
14687 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14688 && row->x == 0)
14689 {
14690 this_line_buffer = XBUFFER (w->contents);
14691
14692 CHARPOS (this_line_start_pos)
14693 = MATRIX_ROW_START_CHARPOS (row) + delta;
14694 BYTEPOS (this_line_start_pos)
14695 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14696
14697 CHARPOS (this_line_end_pos)
14698 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14699 BYTEPOS (this_line_end_pos)
14700 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14701
14702 this_line_y = w->cursor.y;
14703 this_line_pixel_height = row->height;
14704 this_line_vpos = w->cursor.vpos;
14705 this_line_start_x = row->x;
14706 }
14707 else
14708 CHARPOS (this_line_start_pos) = 0;
14709 }
14710
14711 return 1;
14712 }
14713
14714
14715 /* Run window scroll functions, if any, for WINDOW with new window
14716 start STARTP. Sets the window start of WINDOW to that position.
14717
14718 We assume that the window's buffer is really current. */
14719
14720 static struct text_pos
14721 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14722 {
14723 struct window *w = XWINDOW (window);
14724 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14725
14726 eassert (current_buffer == XBUFFER (w->contents));
14727
14728 if (!NILP (Vwindow_scroll_functions))
14729 {
14730 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14731 make_number (CHARPOS (startp)));
14732 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14733 /* In case the hook functions switch buffers. */
14734 set_buffer_internal (XBUFFER (w->contents));
14735 }
14736
14737 return startp;
14738 }
14739
14740
14741 /* Make sure the line containing the cursor is fully visible.
14742 A value of 1 means there is nothing to be done.
14743 (Either the line is fully visible, or it cannot be made so,
14744 or we cannot tell.)
14745
14746 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14747 is higher than window.
14748
14749 A value of 0 means the caller should do scrolling
14750 as if point had gone off the screen. */
14751
14752 static int
14753 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14754 {
14755 struct glyph_matrix *matrix;
14756 struct glyph_row *row;
14757 int window_height;
14758
14759 if (!make_cursor_line_fully_visible_p)
14760 return 1;
14761
14762 /* It's not always possible to find the cursor, e.g, when a window
14763 is full of overlay strings. Don't do anything in that case. */
14764 if (w->cursor.vpos < 0)
14765 return 1;
14766
14767 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14768 row = MATRIX_ROW (matrix, w->cursor.vpos);
14769
14770 /* If the cursor row is not partially visible, there's nothing to do. */
14771 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14772 return 1;
14773
14774 /* If the row the cursor is in is taller than the window's height,
14775 it's not clear what to do, so do nothing. */
14776 window_height = window_box_height (w);
14777 if (row->height >= window_height)
14778 {
14779 if (!force_p || MINI_WINDOW_P (w)
14780 || w->vscroll || w->cursor.vpos == 0)
14781 return 1;
14782 }
14783 return 0;
14784 }
14785
14786
14787 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14788 non-zero means only WINDOW is redisplayed in redisplay_internal.
14789 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14790 in redisplay_window to bring a partially visible line into view in
14791 the case that only the cursor has moved.
14792
14793 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14794 last screen line's vertical height extends past the end of the screen.
14795
14796 Value is
14797
14798 1 if scrolling succeeded
14799
14800 0 if scrolling didn't find point.
14801
14802 -1 if new fonts have been loaded so that we must interrupt
14803 redisplay, adjust glyph matrices, and try again. */
14804
14805 enum
14806 {
14807 SCROLLING_SUCCESS,
14808 SCROLLING_FAILED,
14809 SCROLLING_NEED_LARGER_MATRICES
14810 };
14811
14812 /* If scroll-conservatively is more than this, never recenter.
14813
14814 If you change this, don't forget to update the doc string of
14815 `scroll-conservatively' and the Emacs manual. */
14816 #define SCROLL_LIMIT 100
14817
14818 static int
14819 try_scrolling (Lisp_Object window, int just_this_one_p,
14820 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14821 int temp_scroll_step, int last_line_misfit)
14822 {
14823 struct window *w = XWINDOW (window);
14824 struct frame *f = XFRAME (w->frame);
14825 struct text_pos pos, startp;
14826 struct it it;
14827 int this_scroll_margin, scroll_max, rc, height;
14828 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14829 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14830 Lisp_Object aggressive;
14831 /* We will never try scrolling more than this number of lines. */
14832 int scroll_limit = SCROLL_LIMIT;
14833 int frame_line_height = default_line_pixel_height (w);
14834 int window_total_lines
14835 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14836
14837 #ifdef GLYPH_DEBUG
14838 debug_method_add (w, "try_scrolling");
14839 #endif
14840
14841 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14842
14843 /* Compute scroll margin height in pixels. We scroll when point is
14844 within this distance from the top or bottom of the window. */
14845 if (scroll_margin > 0)
14846 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14847 * frame_line_height;
14848 else
14849 this_scroll_margin = 0;
14850
14851 /* Force arg_scroll_conservatively to have a reasonable value, to
14852 avoid scrolling too far away with slow move_it_* functions. Note
14853 that the user can supply scroll-conservatively equal to
14854 `most-positive-fixnum', which can be larger than INT_MAX. */
14855 if (arg_scroll_conservatively > scroll_limit)
14856 {
14857 arg_scroll_conservatively = scroll_limit + 1;
14858 scroll_max = scroll_limit * frame_line_height;
14859 }
14860 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14861 /* Compute how much we should try to scroll maximally to bring
14862 point into view. */
14863 scroll_max = (max (scroll_step,
14864 max (arg_scroll_conservatively, temp_scroll_step))
14865 * frame_line_height);
14866 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14867 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14868 /* We're trying to scroll because of aggressive scrolling but no
14869 scroll_step is set. Choose an arbitrary one. */
14870 scroll_max = 10 * frame_line_height;
14871 else
14872 scroll_max = 0;
14873
14874 too_near_end:
14875
14876 /* Decide whether to scroll down. */
14877 if (PT > CHARPOS (startp))
14878 {
14879 int scroll_margin_y;
14880
14881 /* Compute the pixel ypos of the scroll margin, then move IT to
14882 either that ypos or PT, whichever comes first. */
14883 start_display (&it, w, startp);
14884 scroll_margin_y = it.last_visible_y - this_scroll_margin
14885 - frame_line_height * extra_scroll_margin_lines;
14886 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14887 (MOVE_TO_POS | MOVE_TO_Y));
14888
14889 if (PT > CHARPOS (it.current.pos))
14890 {
14891 int y0 = line_bottom_y (&it);
14892 /* Compute how many pixels below window bottom to stop searching
14893 for PT. This avoids costly search for PT that is far away if
14894 the user limited scrolling by a small number of lines, but
14895 always finds PT if scroll_conservatively is set to a large
14896 number, such as most-positive-fixnum. */
14897 int slack = max (scroll_max, 10 * frame_line_height);
14898 int y_to_move = it.last_visible_y + slack;
14899
14900 /* Compute the distance from the scroll margin to PT or to
14901 the scroll limit, whichever comes first. This should
14902 include the height of the cursor line, to make that line
14903 fully visible. */
14904 move_it_to (&it, PT, -1, y_to_move,
14905 -1, MOVE_TO_POS | MOVE_TO_Y);
14906 dy = line_bottom_y (&it) - y0;
14907
14908 if (dy > scroll_max)
14909 return SCROLLING_FAILED;
14910
14911 if (dy > 0)
14912 scroll_down_p = 1;
14913 }
14914 }
14915
14916 if (scroll_down_p)
14917 {
14918 /* Point is in or below the bottom scroll margin, so move the
14919 window start down. If scrolling conservatively, move it just
14920 enough down to make point visible. If scroll_step is set,
14921 move it down by scroll_step. */
14922 if (arg_scroll_conservatively)
14923 amount_to_scroll
14924 = min (max (dy, frame_line_height),
14925 frame_line_height * arg_scroll_conservatively);
14926 else if (scroll_step || temp_scroll_step)
14927 amount_to_scroll = scroll_max;
14928 else
14929 {
14930 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14931 height = WINDOW_BOX_TEXT_HEIGHT (w);
14932 if (NUMBERP (aggressive))
14933 {
14934 double float_amount = XFLOATINT (aggressive) * height;
14935 int aggressive_scroll = float_amount;
14936 if (aggressive_scroll == 0 && float_amount > 0)
14937 aggressive_scroll = 1;
14938 /* Don't let point enter the scroll margin near top of
14939 the window. This could happen if the value of
14940 scroll_up_aggressively is too large and there are
14941 non-zero margins, because scroll_up_aggressively
14942 means put point that fraction of window height
14943 _from_the_bottom_margin_. */
14944 if (aggressive_scroll + 2*this_scroll_margin > height)
14945 aggressive_scroll = height - 2*this_scroll_margin;
14946 amount_to_scroll = dy + aggressive_scroll;
14947 }
14948 }
14949
14950 if (amount_to_scroll <= 0)
14951 return SCROLLING_FAILED;
14952
14953 start_display (&it, w, startp);
14954 if (arg_scroll_conservatively <= scroll_limit)
14955 move_it_vertically (&it, amount_to_scroll);
14956 else
14957 {
14958 /* Extra precision for users who set scroll-conservatively
14959 to a large number: make sure the amount we scroll
14960 the window start is never less than amount_to_scroll,
14961 which was computed as distance from window bottom to
14962 point. This matters when lines at window top and lines
14963 below window bottom have different height. */
14964 struct it it1;
14965 void *it1data = NULL;
14966 /* We use a temporary it1 because line_bottom_y can modify
14967 its argument, if it moves one line down; see there. */
14968 int start_y;
14969
14970 SAVE_IT (it1, it, it1data);
14971 start_y = line_bottom_y (&it1);
14972 do {
14973 RESTORE_IT (&it, &it, it1data);
14974 move_it_by_lines (&it, 1);
14975 SAVE_IT (it1, it, it1data);
14976 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14977 }
14978
14979 /* If STARTP is unchanged, move it down another screen line. */
14980 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14981 move_it_by_lines (&it, 1);
14982 startp = it.current.pos;
14983 }
14984 else
14985 {
14986 struct text_pos scroll_margin_pos = startp;
14987 int y_offset = 0;
14988
14989 /* See if point is inside the scroll margin at the top of the
14990 window. */
14991 if (this_scroll_margin)
14992 {
14993 int y_start;
14994
14995 start_display (&it, w, startp);
14996 y_start = it.current_y;
14997 move_it_vertically (&it, this_scroll_margin);
14998 scroll_margin_pos = it.current.pos;
14999 /* If we didn't move enough before hitting ZV, request
15000 additional amount of scroll, to move point out of the
15001 scroll margin. */
15002 if (IT_CHARPOS (it) == ZV
15003 && it.current_y - y_start < this_scroll_margin)
15004 y_offset = this_scroll_margin - (it.current_y - y_start);
15005 }
15006
15007 if (PT < CHARPOS (scroll_margin_pos))
15008 {
15009 /* Point is in the scroll margin at the top of the window or
15010 above what is displayed in the window. */
15011 int y0, y_to_move;
15012
15013 /* Compute the vertical distance from PT to the scroll
15014 margin position. Move as far as scroll_max allows, or
15015 one screenful, or 10 screen lines, whichever is largest.
15016 Give up if distance is greater than scroll_max or if we
15017 didn't reach the scroll margin position. */
15018 SET_TEXT_POS (pos, PT, PT_BYTE);
15019 start_display (&it, w, pos);
15020 y0 = it.current_y;
15021 y_to_move = max (it.last_visible_y,
15022 max (scroll_max, 10 * frame_line_height));
15023 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15024 y_to_move, -1,
15025 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15026 dy = it.current_y - y0;
15027 if (dy > scroll_max
15028 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15029 return SCROLLING_FAILED;
15030
15031 /* Additional scroll for when ZV was too close to point. */
15032 dy += y_offset;
15033
15034 /* Compute new window start. */
15035 start_display (&it, w, startp);
15036
15037 if (arg_scroll_conservatively)
15038 amount_to_scroll = max (dy, frame_line_height *
15039 max (scroll_step, temp_scroll_step));
15040 else if (scroll_step || temp_scroll_step)
15041 amount_to_scroll = scroll_max;
15042 else
15043 {
15044 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15045 height = WINDOW_BOX_TEXT_HEIGHT (w);
15046 if (NUMBERP (aggressive))
15047 {
15048 double float_amount = XFLOATINT (aggressive) * height;
15049 int aggressive_scroll = float_amount;
15050 if (aggressive_scroll == 0 && float_amount > 0)
15051 aggressive_scroll = 1;
15052 /* Don't let point enter the scroll margin near
15053 bottom of the window, if the value of
15054 scroll_down_aggressively happens to be too
15055 large. */
15056 if (aggressive_scroll + 2*this_scroll_margin > height)
15057 aggressive_scroll = height - 2*this_scroll_margin;
15058 amount_to_scroll = dy + aggressive_scroll;
15059 }
15060 }
15061
15062 if (amount_to_scroll <= 0)
15063 return SCROLLING_FAILED;
15064
15065 move_it_vertically_backward (&it, amount_to_scroll);
15066 startp = it.current.pos;
15067 }
15068 }
15069
15070 /* Run window scroll functions. */
15071 startp = run_window_scroll_functions (window, startp);
15072
15073 /* Display the window. Give up if new fonts are loaded, or if point
15074 doesn't appear. */
15075 if (!try_window (window, startp, 0))
15076 rc = SCROLLING_NEED_LARGER_MATRICES;
15077 else if (w->cursor.vpos < 0)
15078 {
15079 clear_glyph_matrix (w->desired_matrix);
15080 rc = SCROLLING_FAILED;
15081 }
15082 else
15083 {
15084 /* Maybe forget recorded base line for line number display. */
15085 if (!just_this_one_p
15086 || current_buffer->clip_changed
15087 || BEG_UNCHANGED < CHARPOS (startp))
15088 w->base_line_number = 0;
15089
15090 /* If cursor ends up on a partially visible line,
15091 treat that as being off the bottom of the screen. */
15092 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15093 /* It's possible that the cursor is on the first line of the
15094 buffer, which is partially obscured due to a vscroll
15095 (Bug#7537). In that case, avoid looping forever. */
15096 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15097 {
15098 clear_glyph_matrix (w->desired_matrix);
15099 ++extra_scroll_margin_lines;
15100 goto too_near_end;
15101 }
15102 rc = SCROLLING_SUCCESS;
15103 }
15104
15105 return rc;
15106 }
15107
15108
15109 /* Compute a suitable window start for window W if display of W starts
15110 on a continuation line. Value is non-zero if a new window start
15111 was computed.
15112
15113 The new window start will be computed, based on W's width, starting
15114 from the start of the continued line. It is the start of the
15115 screen line with the minimum distance from the old start W->start. */
15116
15117 static int
15118 compute_window_start_on_continuation_line (struct window *w)
15119 {
15120 struct text_pos pos, start_pos;
15121 int window_start_changed_p = 0;
15122
15123 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15124
15125 /* If window start is on a continuation line... Window start may be
15126 < BEGV in case there's invisible text at the start of the
15127 buffer (M-x rmail, for example). */
15128 if (CHARPOS (start_pos) > BEGV
15129 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15130 {
15131 struct it it;
15132 struct glyph_row *row;
15133
15134 /* Handle the case that the window start is out of range. */
15135 if (CHARPOS (start_pos) < BEGV)
15136 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15137 else if (CHARPOS (start_pos) > ZV)
15138 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15139
15140 /* Find the start of the continued line. This should be fast
15141 because find_newline is fast (newline cache). */
15142 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15143 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15144 row, DEFAULT_FACE_ID);
15145 reseat_at_previous_visible_line_start (&it);
15146
15147 /* If the line start is "too far" away from the window start,
15148 say it takes too much time to compute a new window start. */
15149 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15150 /* PXW: Do we need upper bounds here? */
15151 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15152 {
15153 int min_distance, distance;
15154
15155 /* Move forward by display lines to find the new window
15156 start. If window width was enlarged, the new start can
15157 be expected to be > the old start. If window width was
15158 decreased, the new window start will be < the old start.
15159 So, we're looking for the display line start with the
15160 minimum distance from the old window start. */
15161 pos = it.current.pos;
15162 min_distance = INFINITY;
15163 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15164 distance < min_distance)
15165 {
15166 min_distance = distance;
15167 pos = it.current.pos;
15168 if (it.line_wrap == WORD_WRAP)
15169 {
15170 /* Under WORD_WRAP, move_it_by_lines is likely to
15171 overshoot and stop not at the first, but the
15172 second character from the left margin. So in
15173 that case, we need a more tight control on the X
15174 coordinate of the iterator than move_it_by_lines
15175 promises in its contract. The method is to first
15176 go to the last (rightmost) visible character of a
15177 line, then move to the leftmost character on the
15178 next line in a separate call. */
15179 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15180 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15181 move_it_to (&it, ZV, 0,
15182 it.current_y + it.max_ascent + it.max_descent, -1,
15183 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15184 }
15185 else
15186 move_it_by_lines (&it, 1);
15187 }
15188
15189 /* Set the window start there. */
15190 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15191 window_start_changed_p = 1;
15192 }
15193 }
15194
15195 return window_start_changed_p;
15196 }
15197
15198
15199 /* Try cursor movement in case text has not changed in window WINDOW,
15200 with window start STARTP. Value is
15201
15202 CURSOR_MOVEMENT_SUCCESS if successful
15203
15204 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15205
15206 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15207 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15208 we want to scroll as if scroll-step were set to 1. See the code.
15209
15210 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15211 which case we have to abort this redisplay, and adjust matrices
15212 first. */
15213
15214 enum
15215 {
15216 CURSOR_MOVEMENT_SUCCESS,
15217 CURSOR_MOVEMENT_CANNOT_BE_USED,
15218 CURSOR_MOVEMENT_MUST_SCROLL,
15219 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15220 };
15221
15222 static int
15223 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15224 {
15225 struct window *w = XWINDOW (window);
15226 struct frame *f = XFRAME (w->frame);
15227 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15228
15229 #ifdef GLYPH_DEBUG
15230 if (inhibit_try_cursor_movement)
15231 return rc;
15232 #endif
15233
15234 /* Previously, there was a check for Lisp integer in the
15235 if-statement below. Now, this field is converted to
15236 ptrdiff_t, thus zero means invalid position in a buffer. */
15237 eassert (w->last_point > 0);
15238 /* Likewise there was a check whether window_end_vpos is nil or larger
15239 than the window. Now window_end_vpos is int and so never nil, but
15240 let's leave eassert to check whether it fits in the window. */
15241 eassert (w->window_end_vpos < w->current_matrix->nrows);
15242
15243 /* Handle case where text has not changed, only point, and it has
15244 not moved off the frame. */
15245 if (/* Point may be in this window. */
15246 PT >= CHARPOS (startp)
15247 /* Selective display hasn't changed. */
15248 && !current_buffer->clip_changed
15249 /* Function force-mode-line-update is used to force a thorough
15250 redisplay. It sets either windows_or_buffers_changed or
15251 update_mode_lines. So don't take a shortcut here for these
15252 cases. */
15253 && !update_mode_lines
15254 && !windows_or_buffers_changed
15255 && !f->cursor_type_changed
15256 && NILP (Vshow_trailing_whitespace)
15257 /* This code is not used for mini-buffer for the sake of the case
15258 of redisplaying to replace an echo area message; since in
15259 that case the mini-buffer contents per se are usually
15260 unchanged. This code is of no real use in the mini-buffer
15261 since the handling of this_line_start_pos, etc., in redisplay
15262 handles the same cases. */
15263 && !EQ (window, minibuf_window)
15264 && (FRAME_WINDOW_P (f)
15265 || !overlay_arrow_in_current_buffer_p ()))
15266 {
15267 int this_scroll_margin, top_scroll_margin;
15268 struct glyph_row *row = NULL;
15269 int frame_line_height = default_line_pixel_height (w);
15270 int window_total_lines
15271 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15272
15273 #ifdef GLYPH_DEBUG
15274 debug_method_add (w, "cursor movement");
15275 #endif
15276
15277 /* Scroll if point within this distance from the top or bottom
15278 of the window. This is a pixel value. */
15279 if (scroll_margin > 0)
15280 {
15281 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15282 this_scroll_margin *= frame_line_height;
15283 }
15284 else
15285 this_scroll_margin = 0;
15286
15287 top_scroll_margin = this_scroll_margin;
15288 if (WINDOW_WANTS_HEADER_LINE_P (w))
15289 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15290
15291 /* Start with the row the cursor was displayed during the last
15292 not paused redisplay. Give up if that row is not valid. */
15293 if (w->last_cursor_vpos < 0
15294 || w->last_cursor_vpos >= w->current_matrix->nrows)
15295 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15296 else
15297 {
15298 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15299 if (row->mode_line_p)
15300 ++row;
15301 if (!row->enabled_p)
15302 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15303 }
15304
15305 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15306 {
15307 int scroll_p = 0, must_scroll = 0;
15308 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15309
15310 if (PT > w->last_point)
15311 {
15312 /* Point has moved forward. */
15313 while (MATRIX_ROW_END_CHARPOS (row) < PT
15314 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15315 {
15316 eassert (row->enabled_p);
15317 ++row;
15318 }
15319
15320 /* If the end position of a row equals the start
15321 position of the next row, and PT is at that position,
15322 we would rather display cursor in the next line. */
15323 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15324 && MATRIX_ROW_END_CHARPOS (row) == PT
15325 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15326 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15327 && !cursor_row_p (row))
15328 ++row;
15329
15330 /* If within the scroll margin, scroll. Note that
15331 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15332 the next line would be drawn, and that
15333 this_scroll_margin can be zero. */
15334 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15335 || PT > MATRIX_ROW_END_CHARPOS (row)
15336 /* Line is completely visible last line in window
15337 and PT is to be set in the next line. */
15338 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15339 && PT == MATRIX_ROW_END_CHARPOS (row)
15340 && !row->ends_at_zv_p
15341 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15342 scroll_p = 1;
15343 }
15344 else if (PT < w->last_point)
15345 {
15346 /* Cursor has to be moved backward. Note that PT >=
15347 CHARPOS (startp) because of the outer if-statement. */
15348 while (!row->mode_line_p
15349 && (MATRIX_ROW_START_CHARPOS (row) > PT
15350 || (MATRIX_ROW_START_CHARPOS (row) == PT
15351 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15352 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15353 row > w->current_matrix->rows
15354 && (row-1)->ends_in_newline_from_string_p))))
15355 && (row->y > top_scroll_margin
15356 || CHARPOS (startp) == BEGV))
15357 {
15358 eassert (row->enabled_p);
15359 --row;
15360 }
15361
15362 /* Consider the following case: Window starts at BEGV,
15363 there is invisible, intangible text at BEGV, so that
15364 display starts at some point START > BEGV. It can
15365 happen that we are called with PT somewhere between
15366 BEGV and START. Try to handle that case. */
15367 if (row < w->current_matrix->rows
15368 || row->mode_line_p)
15369 {
15370 row = w->current_matrix->rows;
15371 if (row->mode_line_p)
15372 ++row;
15373 }
15374
15375 /* Due to newlines in overlay strings, we may have to
15376 skip forward over overlay strings. */
15377 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15378 && MATRIX_ROW_END_CHARPOS (row) == PT
15379 && !cursor_row_p (row))
15380 ++row;
15381
15382 /* If within the scroll margin, scroll. */
15383 if (row->y < top_scroll_margin
15384 && CHARPOS (startp) != BEGV)
15385 scroll_p = 1;
15386 }
15387 else
15388 {
15389 /* Cursor did not move. So don't scroll even if cursor line
15390 is partially visible, as it was so before. */
15391 rc = CURSOR_MOVEMENT_SUCCESS;
15392 }
15393
15394 if (PT < MATRIX_ROW_START_CHARPOS (row)
15395 || PT > MATRIX_ROW_END_CHARPOS (row))
15396 {
15397 /* if PT is not in the glyph row, give up. */
15398 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15399 must_scroll = 1;
15400 }
15401 else if (rc != CURSOR_MOVEMENT_SUCCESS
15402 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15403 {
15404 struct glyph_row *row1;
15405
15406 /* If rows are bidi-reordered and point moved, back up
15407 until we find a row that does not belong to a
15408 continuation line. This is because we must consider
15409 all rows of a continued line as candidates for the
15410 new cursor positioning, since row start and end
15411 positions change non-linearly with vertical position
15412 in such rows. */
15413 /* FIXME: Revisit this when glyph ``spilling'' in
15414 continuation lines' rows is implemented for
15415 bidi-reordered rows. */
15416 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15417 MATRIX_ROW_CONTINUATION_LINE_P (row);
15418 --row)
15419 {
15420 /* If we hit the beginning of the displayed portion
15421 without finding the first row of a continued
15422 line, give up. */
15423 if (row <= row1)
15424 {
15425 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15426 break;
15427 }
15428 eassert (row->enabled_p);
15429 }
15430 }
15431 if (must_scroll)
15432 ;
15433 else if (rc != CURSOR_MOVEMENT_SUCCESS
15434 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15435 /* Make sure this isn't a header line by any chance, since
15436 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15437 && !row->mode_line_p
15438 && make_cursor_line_fully_visible_p)
15439 {
15440 if (PT == MATRIX_ROW_END_CHARPOS (row)
15441 && !row->ends_at_zv_p
15442 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15443 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15444 else if (row->height > window_box_height (w))
15445 {
15446 /* If we end up in a partially visible line, let's
15447 make it fully visible, except when it's taller
15448 than the window, in which case we can't do much
15449 about it. */
15450 *scroll_step = 1;
15451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15452 }
15453 else
15454 {
15455 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15456 if (!cursor_row_fully_visible_p (w, 0, 1))
15457 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15458 else
15459 rc = CURSOR_MOVEMENT_SUCCESS;
15460 }
15461 }
15462 else if (scroll_p)
15463 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15464 else if (rc != CURSOR_MOVEMENT_SUCCESS
15465 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15466 {
15467 /* With bidi-reordered rows, there could be more than
15468 one candidate row whose start and end positions
15469 occlude point. We need to let set_cursor_from_row
15470 find the best candidate. */
15471 /* FIXME: Revisit this when glyph ``spilling'' in
15472 continuation lines' rows is implemented for
15473 bidi-reordered rows. */
15474 int rv = 0;
15475
15476 do
15477 {
15478 int at_zv_p = 0, exact_match_p = 0;
15479
15480 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15481 && PT <= MATRIX_ROW_END_CHARPOS (row)
15482 && cursor_row_p (row))
15483 rv |= set_cursor_from_row (w, row, w->current_matrix,
15484 0, 0, 0, 0);
15485 /* As soon as we've found the exact match for point,
15486 or the first suitable row whose ends_at_zv_p flag
15487 is set, we are done. */
15488 if (rv)
15489 {
15490 at_zv_p = MATRIX_ROW (w->current_matrix,
15491 w->cursor.vpos)->ends_at_zv_p;
15492 if (!at_zv_p
15493 && w->cursor.hpos >= 0
15494 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15495 w->cursor.vpos))
15496 {
15497 struct glyph_row *candidate =
15498 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15499 struct glyph *g =
15500 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15501 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15502
15503 exact_match_p =
15504 (BUFFERP (g->object) && g->charpos == PT)
15505 || (INTEGERP (g->object)
15506 && (g->charpos == PT
15507 || (g->charpos == 0 && endpos - 1 == PT)));
15508 }
15509 if (at_zv_p || exact_match_p)
15510 {
15511 rc = CURSOR_MOVEMENT_SUCCESS;
15512 break;
15513 }
15514 }
15515 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15516 break;
15517 ++row;
15518 }
15519 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15520 || row->continued_p)
15521 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15522 || (MATRIX_ROW_START_CHARPOS (row) == PT
15523 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15524 /* If we didn't find any candidate rows, or exited the
15525 loop before all the candidates were examined, signal
15526 to the caller that this method failed. */
15527 if (rc != CURSOR_MOVEMENT_SUCCESS
15528 && !(rv
15529 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15530 && !row->continued_p))
15531 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15532 else if (rv)
15533 rc = CURSOR_MOVEMENT_SUCCESS;
15534 }
15535 else
15536 {
15537 do
15538 {
15539 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15540 {
15541 rc = CURSOR_MOVEMENT_SUCCESS;
15542 break;
15543 }
15544 ++row;
15545 }
15546 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15547 && MATRIX_ROW_START_CHARPOS (row) == PT
15548 && cursor_row_p (row));
15549 }
15550 }
15551 }
15552
15553 return rc;
15554 }
15555
15556 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15557 static
15558 #endif
15559 void
15560 set_vertical_scroll_bar (struct window *w)
15561 {
15562 ptrdiff_t start, end, whole;
15563
15564 /* Calculate the start and end positions for the current window.
15565 At some point, it would be nice to choose between scrollbars
15566 which reflect the whole buffer size, with special markers
15567 indicating narrowing, and scrollbars which reflect only the
15568 visible region.
15569
15570 Note that mini-buffers sometimes aren't displaying any text. */
15571 if (!MINI_WINDOW_P (w)
15572 || (w == XWINDOW (minibuf_window)
15573 && NILP (echo_area_buffer[0])))
15574 {
15575 struct buffer *buf = XBUFFER (w->contents);
15576 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15577 start = marker_position (w->start) - BUF_BEGV (buf);
15578 /* I don't think this is guaranteed to be right. For the
15579 moment, we'll pretend it is. */
15580 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15581
15582 if (end < start)
15583 end = start;
15584 if (whole < (end - start))
15585 whole = end - start;
15586 }
15587 else
15588 start = end = whole = 0;
15589
15590 /* Indicate what this scroll bar ought to be displaying now. */
15591 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15592 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15593 (w, end - start, whole, start);
15594 }
15595
15596
15597 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15598 selected_window is redisplayed.
15599
15600 We can return without actually redisplaying the window if fonts has been
15601 changed on window's frame. In that case, redisplay_internal will retry. */
15602
15603 static void
15604 redisplay_window (Lisp_Object window, bool just_this_one_p)
15605 {
15606 struct window *w = XWINDOW (window);
15607 struct frame *f = XFRAME (w->frame);
15608 struct buffer *buffer = XBUFFER (w->contents);
15609 struct buffer *old = current_buffer;
15610 struct text_pos lpoint, opoint, startp;
15611 int update_mode_line;
15612 int tem;
15613 struct it it;
15614 /* Record it now because it's overwritten. */
15615 bool current_matrix_up_to_date_p = false;
15616 bool used_current_matrix_p = false;
15617 /* This is less strict than current_matrix_up_to_date_p.
15618 It indicates that the buffer contents and narrowing are unchanged. */
15619 bool buffer_unchanged_p = false;
15620 int temp_scroll_step = 0;
15621 ptrdiff_t count = SPECPDL_INDEX ();
15622 int rc;
15623 int centering_position = -1;
15624 int last_line_misfit = 0;
15625 ptrdiff_t beg_unchanged, end_unchanged;
15626 int frame_line_height;
15627
15628 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15629 opoint = lpoint;
15630
15631 #ifdef GLYPH_DEBUG
15632 *w->desired_matrix->method = 0;
15633 #endif
15634
15635 if (!just_this_one_p
15636 && REDISPLAY_SOME_P ()
15637 && !w->redisplay
15638 && !f->redisplay
15639 && !buffer->text->redisplay
15640 && BUF_PT (buffer) == w->last_point)
15641 return;
15642
15643 /* Make sure that both W's markers are valid. */
15644 eassert (XMARKER (w->start)->buffer == buffer);
15645 eassert (XMARKER (w->pointm)->buffer == buffer);
15646
15647 restart:
15648 reconsider_clip_changes (w);
15649 frame_line_height = default_line_pixel_height (w);
15650
15651 /* Has the mode line to be updated? */
15652 update_mode_line = (w->update_mode_line
15653 || update_mode_lines
15654 || buffer->clip_changed
15655 || buffer->prevent_redisplay_optimizations_p);
15656
15657 if (!just_this_one_p)
15658 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15659 cleverly elsewhere. */
15660 w->must_be_updated_p = true;
15661
15662 if (MINI_WINDOW_P (w))
15663 {
15664 if (w == XWINDOW (echo_area_window)
15665 && !NILP (echo_area_buffer[0]))
15666 {
15667 if (update_mode_line)
15668 /* We may have to update a tty frame's menu bar or a
15669 tool-bar. Example `M-x C-h C-h C-g'. */
15670 goto finish_menu_bars;
15671 else
15672 /* We've already displayed the echo area glyphs in this window. */
15673 goto finish_scroll_bars;
15674 }
15675 else if ((w != XWINDOW (minibuf_window)
15676 || minibuf_level == 0)
15677 /* When buffer is nonempty, redisplay window normally. */
15678 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15679 /* Quail displays non-mini buffers in minibuffer window.
15680 In that case, redisplay the window normally. */
15681 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15682 {
15683 /* W is a mini-buffer window, but it's not active, so clear
15684 it. */
15685 int yb = window_text_bottom_y (w);
15686 struct glyph_row *row;
15687 int y;
15688
15689 for (y = 0, row = w->desired_matrix->rows;
15690 y < yb;
15691 y += row->height, ++row)
15692 blank_row (w, row, y);
15693 goto finish_scroll_bars;
15694 }
15695
15696 clear_glyph_matrix (w->desired_matrix);
15697 }
15698
15699 /* Otherwise set up data on this window; select its buffer and point
15700 value. */
15701 /* Really select the buffer, for the sake of buffer-local
15702 variables. */
15703 set_buffer_internal_1 (XBUFFER (w->contents));
15704
15705 current_matrix_up_to_date_p
15706 = (w->window_end_valid
15707 && !current_buffer->clip_changed
15708 && !current_buffer->prevent_redisplay_optimizations_p
15709 && !window_outdated (w));
15710
15711 /* Run the window-bottom-change-functions
15712 if it is possible that the text on the screen has changed
15713 (either due to modification of the text, or any other reason). */
15714 if (!current_matrix_up_to_date_p
15715 && !NILP (Vwindow_text_change_functions))
15716 {
15717 safe_run_hooks (Qwindow_text_change_functions);
15718 goto restart;
15719 }
15720
15721 beg_unchanged = BEG_UNCHANGED;
15722 end_unchanged = END_UNCHANGED;
15723
15724 SET_TEXT_POS (opoint, PT, PT_BYTE);
15725
15726 specbind (Qinhibit_point_motion_hooks, Qt);
15727
15728 buffer_unchanged_p
15729 = (w->window_end_valid
15730 && !current_buffer->clip_changed
15731 && !window_outdated (w));
15732
15733 /* When windows_or_buffers_changed is non-zero, we can't rely
15734 on the window end being valid, so set it to zero there. */
15735 if (windows_or_buffers_changed)
15736 {
15737 /* If window starts on a continuation line, maybe adjust the
15738 window start in case the window's width changed. */
15739 if (XMARKER (w->start)->buffer == current_buffer)
15740 compute_window_start_on_continuation_line (w);
15741
15742 w->window_end_valid = false;
15743 /* If so, we also can't rely on current matrix
15744 and should not fool try_cursor_movement below. */
15745 current_matrix_up_to_date_p = false;
15746 }
15747
15748 /* Some sanity checks. */
15749 CHECK_WINDOW_END (w);
15750 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15751 emacs_abort ();
15752 if (BYTEPOS (opoint) < CHARPOS (opoint))
15753 emacs_abort ();
15754
15755 if (mode_line_update_needed (w))
15756 update_mode_line = 1;
15757
15758 /* Point refers normally to the selected window. For any other
15759 window, set up appropriate value. */
15760 if (!EQ (window, selected_window))
15761 {
15762 ptrdiff_t new_pt = marker_position (w->pointm);
15763 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15764 if (new_pt < BEGV)
15765 {
15766 new_pt = BEGV;
15767 new_pt_byte = BEGV_BYTE;
15768 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15769 }
15770 else if (new_pt > (ZV - 1))
15771 {
15772 new_pt = ZV;
15773 new_pt_byte = ZV_BYTE;
15774 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15775 }
15776
15777 /* We don't use SET_PT so that the point-motion hooks don't run. */
15778 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15779 }
15780
15781 /* If any of the character widths specified in the display table
15782 have changed, invalidate the width run cache. It's true that
15783 this may be a bit late to catch such changes, but the rest of
15784 redisplay goes (non-fatally) haywire when the display table is
15785 changed, so why should we worry about doing any better? */
15786 if (current_buffer->width_run_cache
15787 || (current_buffer->base_buffer
15788 && current_buffer->base_buffer->width_run_cache))
15789 {
15790 struct Lisp_Char_Table *disptab = buffer_display_table ();
15791
15792 if (! disptab_matches_widthtab
15793 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15794 {
15795 struct buffer *buf = current_buffer;
15796
15797 if (buf->base_buffer)
15798 buf = buf->base_buffer;
15799 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15800 recompute_width_table (current_buffer, disptab);
15801 }
15802 }
15803
15804 /* If window-start is screwed up, choose a new one. */
15805 if (XMARKER (w->start)->buffer != current_buffer)
15806 goto recenter;
15807
15808 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15809
15810 /* If someone specified a new starting point but did not insist,
15811 check whether it can be used. */
15812 if (w->optional_new_start
15813 && CHARPOS (startp) >= BEGV
15814 && CHARPOS (startp) <= ZV)
15815 {
15816 w->optional_new_start = 0;
15817 start_display (&it, w, startp);
15818 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15819 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15820 if (IT_CHARPOS (it) == PT)
15821 w->force_start = 1;
15822 /* IT may overshoot PT if text at PT is invisible. */
15823 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15824 w->force_start = 1;
15825 }
15826
15827 force_start:
15828
15829 /* Handle case where place to start displaying has been specified,
15830 unless the specified location is outside the accessible range. */
15831 if (w->force_start || window_frozen_p (w))
15832 {
15833 /* We set this later on if we have to adjust point. */
15834 int new_vpos = -1;
15835
15836 w->force_start = 0;
15837 w->vscroll = 0;
15838 w->window_end_valid = 0;
15839
15840 /* Forget any recorded base line for line number display. */
15841 if (!buffer_unchanged_p)
15842 w->base_line_number = 0;
15843
15844 /* Redisplay the mode line. Select the buffer properly for that.
15845 Also, run the hook window-scroll-functions
15846 because we have scrolled. */
15847 /* Note, we do this after clearing force_start because
15848 if there's an error, it is better to forget about force_start
15849 than to get into an infinite loop calling the hook functions
15850 and having them get more errors. */
15851 if (!update_mode_line
15852 || ! NILP (Vwindow_scroll_functions))
15853 {
15854 update_mode_line = 1;
15855 w->update_mode_line = 1;
15856 startp = run_window_scroll_functions (window, startp);
15857 }
15858
15859 if (CHARPOS (startp) < BEGV)
15860 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15861 else if (CHARPOS (startp) > ZV)
15862 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15863
15864 /* Redisplay, then check if cursor has been set during the
15865 redisplay. Give up if new fonts were loaded. */
15866 /* We used to issue a CHECK_MARGINS argument to try_window here,
15867 but this causes scrolling to fail when point begins inside
15868 the scroll margin (bug#148) -- cyd */
15869 if (!try_window (window, startp, 0))
15870 {
15871 w->force_start = 1;
15872 clear_glyph_matrix (w->desired_matrix);
15873 goto need_larger_matrices;
15874 }
15875
15876 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15877 {
15878 /* If point does not appear, try to move point so it does
15879 appear. The desired matrix has been built above, so we
15880 can use it here. */
15881 new_vpos = window_box_height (w) / 2;
15882 }
15883
15884 if (!cursor_row_fully_visible_p (w, 0, 0))
15885 {
15886 /* Point does appear, but on a line partly visible at end of window.
15887 Move it back to a fully-visible line. */
15888 new_vpos = window_box_height (w);
15889 }
15890 else if (w->cursor.vpos >= 0)
15891 {
15892 /* Some people insist on not letting point enter the scroll
15893 margin, even though this part handles windows that didn't
15894 scroll at all. */
15895 int window_total_lines
15896 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15897 int margin = min (scroll_margin, window_total_lines / 4);
15898 int pixel_margin = margin * frame_line_height;
15899 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15900
15901 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15902 below, which finds the row to move point to, advances by
15903 the Y coordinate of the _next_ row, see the definition of
15904 MATRIX_ROW_BOTTOM_Y. */
15905 if (w->cursor.vpos < margin + header_line)
15906 {
15907 w->cursor.vpos = -1;
15908 clear_glyph_matrix (w->desired_matrix);
15909 goto try_to_scroll;
15910 }
15911 else
15912 {
15913 int window_height = window_box_height (w);
15914
15915 if (header_line)
15916 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15917 if (w->cursor.y >= window_height - pixel_margin)
15918 {
15919 w->cursor.vpos = -1;
15920 clear_glyph_matrix (w->desired_matrix);
15921 goto try_to_scroll;
15922 }
15923 }
15924 }
15925
15926 /* If we need to move point for either of the above reasons,
15927 now actually do it. */
15928 if (new_vpos >= 0)
15929 {
15930 struct glyph_row *row;
15931
15932 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15933 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15934 ++row;
15935
15936 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15937 MATRIX_ROW_START_BYTEPOS (row));
15938
15939 if (w != XWINDOW (selected_window))
15940 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15941 else if (current_buffer == old)
15942 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15943
15944 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15945
15946 /* If we are highlighting the region, then we just changed
15947 the region, so redisplay to show it. */
15948 /* FIXME: We need to (re)run pre-redisplay-function! */
15949 /* if (markpos_of_region () >= 0)
15950 {
15951 clear_glyph_matrix (w->desired_matrix);
15952 if (!try_window (window, startp, 0))
15953 goto need_larger_matrices;
15954 }
15955 */
15956 }
15957
15958 #ifdef GLYPH_DEBUG
15959 debug_method_add (w, "forced window start");
15960 #endif
15961 goto done;
15962 }
15963
15964 /* Handle case where text has not changed, only point, and it has
15965 not moved off the frame, and we are not retrying after hscroll.
15966 (current_matrix_up_to_date_p is nonzero when retrying.) */
15967 if (current_matrix_up_to_date_p
15968 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15969 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15970 {
15971 switch (rc)
15972 {
15973 case CURSOR_MOVEMENT_SUCCESS:
15974 used_current_matrix_p = 1;
15975 goto done;
15976
15977 case CURSOR_MOVEMENT_MUST_SCROLL:
15978 goto try_to_scroll;
15979
15980 default:
15981 emacs_abort ();
15982 }
15983 }
15984 /* If current starting point was originally the beginning of a line
15985 but no longer is, find a new starting point. */
15986 else if (w->start_at_line_beg
15987 && !(CHARPOS (startp) <= BEGV
15988 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15989 {
15990 #ifdef GLYPH_DEBUG
15991 debug_method_add (w, "recenter 1");
15992 #endif
15993 goto recenter;
15994 }
15995
15996 /* Try scrolling with try_window_id. Value is > 0 if update has
15997 been done, it is -1 if we know that the same window start will
15998 not work. It is 0 if unsuccessful for some other reason. */
15999 else if ((tem = try_window_id (w)) != 0)
16000 {
16001 #ifdef GLYPH_DEBUG
16002 debug_method_add (w, "try_window_id %d", tem);
16003 #endif
16004
16005 if (f->fonts_changed)
16006 goto need_larger_matrices;
16007 if (tem > 0)
16008 goto done;
16009
16010 /* Otherwise try_window_id has returned -1 which means that we
16011 don't want the alternative below this comment to execute. */
16012 }
16013 else if (CHARPOS (startp) >= BEGV
16014 && CHARPOS (startp) <= ZV
16015 && PT >= CHARPOS (startp)
16016 && (CHARPOS (startp) < ZV
16017 /* Avoid starting at end of buffer. */
16018 || CHARPOS (startp) == BEGV
16019 || !window_outdated (w)))
16020 {
16021 int d1, d2, d3, d4, d5, d6;
16022
16023 /* If first window line is a continuation line, and window start
16024 is inside the modified region, but the first change is before
16025 current window start, we must select a new window start.
16026
16027 However, if this is the result of a down-mouse event (e.g. by
16028 extending the mouse-drag-overlay), we don't want to select a
16029 new window start, since that would change the position under
16030 the mouse, resulting in an unwanted mouse-movement rather
16031 than a simple mouse-click. */
16032 if (!w->start_at_line_beg
16033 && NILP (do_mouse_tracking)
16034 && CHARPOS (startp) > BEGV
16035 && CHARPOS (startp) > BEG + beg_unchanged
16036 && CHARPOS (startp) <= Z - end_unchanged
16037 /* Even if w->start_at_line_beg is nil, a new window may
16038 start at a line_beg, since that's how set_buffer_window
16039 sets it. So, we need to check the return value of
16040 compute_window_start_on_continuation_line. (See also
16041 bug#197). */
16042 && XMARKER (w->start)->buffer == current_buffer
16043 && compute_window_start_on_continuation_line (w)
16044 /* It doesn't make sense to force the window start like we
16045 do at label force_start if it is already known that point
16046 will not be visible in the resulting window, because
16047 doing so will move point from its correct position
16048 instead of scrolling the window to bring point into view.
16049 See bug#9324. */
16050 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16051 {
16052 w->force_start = 1;
16053 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16054 goto force_start;
16055 }
16056
16057 #ifdef GLYPH_DEBUG
16058 debug_method_add (w, "same window start");
16059 #endif
16060
16061 /* Try to redisplay starting at same place as before.
16062 If point has not moved off frame, accept the results. */
16063 if (!current_matrix_up_to_date_p
16064 /* Don't use try_window_reusing_current_matrix in this case
16065 because a window scroll function can have changed the
16066 buffer. */
16067 || !NILP (Vwindow_scroll_functions)
16068 || MINI_WINDOW_P (w)
16069 || !(used_current_matrix_p
16070 = try_window_reusing_current_matrix (w)))
16071 {
16072 IF_DEBUG (debug_method_add (w, "1"));
16073 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16074 /* -1 means we need to scroll.
16075 0 means we need new matrices, but fonts_changed
16076 is set in that case, so we will detect it below. */
16077 goto try_to_scroll;
16078 }
16079
16080 if (f->fonts_changed)
16081 goto need_larger_matrices;
16082
16083 if (w->cursor.vpos >= 0)
16084 {
16085 if (!just_this_one_p
16086 || current_buffer->clip_changed
16087 || BEG_UNCHANGED < CHARPOS (startp))
16088 /* Forget any recorded base line for line number display. */
16089 w->base_line_number = 0;
16090
16091 if (!cursor_row_fully_visible_p (w, 1, 0))
16092 {
16093 clear_glyph_matrix (w->desired_matrix);
16094 last_line_misfit = 1;
16095 }
16096 /* Drop through and scroll. */
16097 else
16098 goto done;
16099 }
16100 else
16101 clear_glyph_matrix (w->desired_matrix);
16102 }
16103
16104 try_to_scroll:
16105
16106 /* Redisplay the mode line. Select the buffer properly for that. */
16107 if (!update_mode_line)
16108 {
16109 update_mode_line = 1;
16110 w->update_mode_line = 1;
16111 }
16112
16113 /* Try to scroll by specified few lines. */
16114 if ((scroll_conservatively
16115 || emacs_scroll_step
16116 || temp_scroll_step
16117 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16118 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16119 && CHARPOS (startp) >= BEGV
16120 && CHARPOS (startp) <= ZV)
16121 {
16122 /* The function returns -1 if new fonts were loaded, 1 if
16123 successful, 0 if not successful. */
16124 int ss = try_scrolling (window, just_this_one_p,
16125 scroll_conservatively,
16126 emacs_scroll_step,
16127 temp_scroll_step, last_line_misfit);
16128 switch (ss)
16129 {
16130 case SCROLLING_SUCCESS:
16131 goto done;
16132
16133 case SCROLLING_NEED_LARGER_MATRICES:
16134 goto need_larger_matrices;
16135
16136 case SCROLLING_FAILED:
16137 break;
16138
16139 default:
16140 emacs_abort ();
16141 }
16142 }
16143
16144 /* Finally, just choose a place to start which positions point
16145 according to user preferences. */
16146
16147 recenter:
16148
16149 #ifdef GLYPH_DEBUG
16150 debug_method_add (w, "recenter");
16151 #endif
16152
16153 /* Forget any previously recorded base line for line number display. */
16154 if (!buffer_unchanged_p)
16155 w->base_line_number = 0;
16156
16157 /* Determine the window start relative to point. */
16158 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16159 it.current_y = it.last_visible_y;
16160 if (centering_position < 0)
16161 {
16162 int window_total_lines
16163 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16164 int margin =
16165 scroll_margin > 0
16166 ? min (scroll_margin, window_total_lines / 4)
16167 : 0;
16168 ptrdiff_t margin_pos = CHARPOS (startp);
16169 Lisp_Object aggressive;
16170 int scrolling_up;
16171
16172 /* If there is a scroll margin at the top of the window, find
16173 its character position. */
16174 if (margin
16175 /* Cannot call start_display if startp is not in the
16176 accessible region of the buffer. This can happen when we
16177 have just switched to a different buffer and/or changed
16178 its restriction. In that case, startp is initialized to
16179 the character position 1 (BEGV) because we did not yet
16180 have chance to display the buffer even once. */
16181 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16182 {
16183 struct it it1;
16184 void *it1data = NULL;
16185
16186 SAVE_IT (it1, it, it1data);
16187 start_display (&it1, w, startp);
16188 move_it_vertically (&it1, margin * frame_line_height);
16189 margin_pos = IT_CHARPOS (it1);
16190 RESTORE_IT (&it, &it, it1data);
16191 }
16192 scrolling_up = PT > margin_pos;
16193 aggressive =
16194 scrolling_up
16195 ? BVAR (current_buffer, scroll_up_aggressively)
16196 : BVAR (current_buffer, scroll_down_aggressively);
16197
16198 if (!MINI_WINDOW_P (w)
16199 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16200 {
16201 int pt_offset = 0;
16202
16203 /* Setting scroll-conservatively overrides
16204 scroll-*-aggressively. */
16205 if (!scroll_conservatively && NUMBERP (aggressive))
16206 {
16207 double float_amount = XFLOATINT (aggressive);
16208
16209 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16210 if (pt_offset == 0 && float_amount > 0)
16211 pt_offset = 1;
16212 if (pt_offset && margin > 0)
16213 margin -= 1;
16214 }
16215 /* Compute how much to move the window start backward from
16216 point so that point will be displayed where the user
16217 wants it. */
16218 if (scrolling_up)
16219 {
16220 centering_position = it.last_visible_y;
16221 if (pt_offset)
16222 centering_position -= pt_offset;
16223 centering_position -=
16224 frame_line_height * (1 + margin + (last_line_misfit != 0))
16225 + WINDOW_HEADER_LINE_HEIGHT (w);
16226 /* Don't let point enter the scroll margin near top of
16227 the window. */
16228 if (centering_position < margin * frame_line_height)
16229 centering_position = margin * frame_line_height;
16230 }
16231 else
16232 centering_position = margin * frame_line_height + pt_offset;
16233 }
16234 else
16235 /* Set the window start half the height of the window backward
16236 from point. */
16237 centering_position = window_box_height (w) / 2;
16238 }
16239 move_it_vertically_backward (&it, centering_position);
16240
16241 eassert (IT_CHARPOS (it) >= BEGV);
16242
16243 /* The function move_it_vertically_backward may move over more
16244 than the specified y-distance. If it->w is small, e.g. a
16245 mini-buffer window, we may end up in front of the window's
16246 display area. Start displaying at the start of the line
16247 containing PT in this case. */
16248 if (it.current_y <= 0)
16249 {
16250 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16251 move_it_vertically_backward (&it, 0);
16252 it.current_y = 0;
16253 }
16254
16255 it.current_x = it.hpos = 0;
16256
16257 /* Set the window start position here explicitly, to avoid an
16258 infinite loop in case the functions in window-scroll-functions
16259 get errors. */
16260 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16261
16262 /* Run scroll hooks. */
16263 startp = run_window_scroll_functions (window, it.current.pos);
16264
16265 /* Redisplay the window. */
16266 if (!current_matrix_up_to_date_p
16267 || windows_or_buffers_changed
16268 || f->cursor_type_changed
16269 /* Don't use try_window_reusing_current_matrix in this case
16270 because it can have changed the buffer. */
16271 || !NILP (Vwindow_scroll_functions)
16272 || !just_this_one_p
16273 || MINI_WINDOW_P (w)
16274 || !(used_current_matrix_p
16275 = try_window_reusing_current_matrix (w)))
16276 try_window (window, startp, 0);
16277
16278 /* If new fonts have been loaded (due to fontsets), give up. We
16279 have to start a new redisplay since we need to re-adjust glyph
16280 matrices. */
16281 if (f->fonts_changed)
16282 goto need_larger_matrices;
16283
16284 /* If cursor did not appear assume that the middle of the window is
16285 in the first line of the window. Do it again with the next line.
16286 (Imagine a window of height 100, displaying two lines of height
16287 60. Moving back 50 from it->last_visible_y will end in the first
16288 line.) */
16289 if (w->cursor.vpos < 0)
16290 {
16291 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16292 {
16293 clear_glyph_matrix (w->desired_matrix);
16294 move_it_by_lines (&it, 1);
16295 try_window (window, it.current.pos, 0);
16296 }
16297 else if (PT < IT_CHARPOS (it))
16298 {
16299 clear_glyph_matrix (w->desired_matrix);
16300 move_it_by_lines (&it, -1);
16301 try_window (window, it.current.pos, 0);
16302 }
16303 else
16304 {
16305 /* Not much we can do about it. */
16306 }
16307 }
16308
16309 /* Consider the following case: Window starts at BEGV, there is
16310 invisible, intangible text at BEGV, so that display starts at
16311 some point START > BEGV. It can happen that we are called with
16312 PT somewhere between BEGV and START. Try to handle that case. */
16313 if (w->cursor.vpos < 0)
16314 {
16315 struct glyph_row *row = w->current_matrix->rows;
16316 if (row->mode_line_p)
16317 ++row;
16318 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16319 }
16320
16321 if (!cursor_row_fully_visible_p (w, 0, 0))
16322 {
16323 /* If vscroll is enabled, disable it and try again. */
16324 if (w->vscroll)
16325 {
16326 w->vscroll = 0;
16327 clear_glyph_matrix (w->desired_matrix);
16328 goto recenter;
16329 }
16330
16331 /* Users who set scroll-conservatively to a large number want
16332 point just above/below the scroll margin. If we ended up
16333 with point's row partially visible, move the window start to
16334 make that row fully visible and out of the margin. */
16335 if (scroll_conservatively > SCROLL_LIMIT)
16336 {
16337 int window_total_lines
16338 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16339 int margin =
16340 scroll_margin > 0
16341 ? min (scroll_margin, window_total_lines / 4)
16342 : 0;
16343 int move_down = w->cursor.vpos >= window_total_lines / 2;
16344
16345 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16346 clear_glyph_matrix (w->desired_matrix);
16347 if (1 == try_window (window, it.current.pos,
16348 TRY_WINDOW_CHECK_MARGINS))
16349 goto done;
16350 }
16351
16352 /* If centering point failed to make the whole line visible,
16353 put point at the top instead. That has to make the whole line
16354 visible, if it can be done. */
16355 if (centering_position == 0)
16356 goto done;
16357
16358 clear_glyph_matrix (w->desired_matrix);
16359 centering_position = 0;
16360 goto recenter;
16361 }
16362
16363 done:
16364
16365 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16366 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16367 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16368
16369 /* Display the mode line, if we must. */
16370 if ((update_mode_line
16371 /* If window not full width, must redo its mode line
16372 if (a) the window to its side is being redone and
16373 (b) we do a frame-based redisplay. This is a consequence
16374 of how inverted lines are drawn in frame-based redisplay. */
16375 || (!just_this_one_p
16376 && !FRAME_WINDOW_P (f)
16377 && !WINDOW_FULL_WIDTH_P (w))
16378 /* Line number to display. */
16379 || w->base_line_pos > 0
16380 /* Column number is displayed and different from the one displayed. */
16381 || (w->column_number_displayed != -1
16382 && (w->column_number_displayed != current_column ())))
16383 /* This means that the window has a mode line. */
16384 && (WINDOW_WANTS_MODELINE_P (w)
16385 || WINDOW_WANTS_HEADER_LINE_P (w)))
16386 {
16387
16388 display_mode_lines (w);
16389
16390 /* If mode line height has changed, arrange for a thorough
16391 immediate redisplay using the correct mode line height. */
16392 if (WINDOW_WANTS_MODELINE_P (w)
16393 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16394 {
16395 f->fonts_changed = 1;
16396 w->mode_line_height = -1;
16397 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16398 = DESIRED_MODE_LINE_HEIGHT (w);
16399 }
16400
16401 /* If header line height has changed, arrange for a thorough
16402 immediate redisplay using the correct header line height. */
16403 if (WINDOW_WANTS_HEADER_LINE_P (w)
16404 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16405 {
16406 f->fonts_changed = 1;
16407 w->header_line_height = -1;
16408 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16409 = DESIRED_HEADER_LINE_HEIGHT (w);
16410 }
16411
16412 if (f->fonts_changed)
16413 goto need_larger_matrices;
16414 }
16415
16416 if (!line_number_displayed && w->base_line_pos != -1)
16417 {
16418 w->base_line_pos = 0;
16419 w->base_line_number = 0;
16420 }
16421
16422 finish_menu_bars:
16423
16424 /* When we reach a frame's selected window, redo the frame's menu bar. */
16425 if (update_mode_line
16426 && EQ (FRAME_SELECTED_WINDOW (f), window))
16427 {
16428 int redisplay_menu_p = 0;
16429
16430 if (FRAME_WINDOW_P (f))
16431 {
16432 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16433 || defined (HAVE_NS) || defined (USE_GTK)
16434 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16435 #else
16436 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16437 #endif
16438 }
16439 else
16440 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16441
16442 if (redisplay_menu_p)
16443 display_menu_bar (w);
16444
16445 #ifdef HAVE_WINDOW_SYSTEM
16446 if (FRAME_WINDOW_P (f))
16447 {
16448 #if defined (USE_GTK) || defined (HAVE_NS)
16449 if (FRAME_EXTERNAL_TOOL_BAR (f))
16450 redisplay_tool_bar (f);
16451 #else
16452 if (WINDOWP (f->tool_bar_window)
16453 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16454 || !NILP (Vauto_resize_tool_bars))
16455 && redisplay_tool_bar (f))
16456 ignore_mouse_drag_p = 1;
16457 #endif
16458 }
16459 #endif
16460 }
16461
16462 #ifdef HAVE_WINDOW_SYSTEM
16463 if (FRAME_WINDOW_P (f)
16464 && update_window_fringes (w, (just_this_one_p
16465 || (!used_current_matrix_p && !overlay_arrow_seen)
16466 || w->pseudo_window_p)))
16467 {
16468 update_begin (f);
16469 block_input ();
16470 if (draw_window_fringes (w, 1))
16471 {
16472 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16473 x_draw_right_divider (w);
16474 else
16475 x_draw_vertical_border (w);
16476 }
16477 unblock_input ();
16478 update_end (f);
16479 }
16480
16481 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16482 x_draw_bottom_divider (w);
16483 #endif /* HAVE_WINDOW_SYSTEM */
16484
16485 /* We go to this label, with fonts_changed set, if it is
16486 necessary to try again using larger glyph matrices.
16487 We have to redeem the scroll bar even in this case,
16488 because the loop in redisplay_internal expects that. */
16489 need_larger_matrices:
16490 ;
16491 finish_scroll_bars:
16492
16493 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16494 {
16495 /* Set the thumb's position and size. */
16496 set_vertical_scroll_bar (w);
16497
16498 /* Note that we actually used the scroll bar attached to this
16499 window, so it shouldn't be deleted at the end of redisplay. */
16500 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16501 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16502 }
16503
16504 /* Restore current_buffer and value of point in it. The window
16505 update may have changed the buffer, so first make sure `opoint'
16506 is still valid (Bug#6177). */
16507 if (CHARPOS (opoint) < BEGV)
16508 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16509 else if (CHARPOS (opoint) > ZV)
16510 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16511 else
16512 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16513
16514 set_buffer_internal_1 (old);
16515 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16516 shorter. This can be caused by log truncation in *Messages*. */
16517 if (CHARPOS (lpoint) <= ZV)
16518 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16519
16520 unbind_to (count, Qnil);
16521 }
16522
16523
16524 /* Build the complete desired matrix of WINDOW with a window start
16525 buffer position POS.
16526
16527 Value is 1 if successful. It is zero if fonts were loaded during
16528 redisplay which makes re-adjusting glyph matrices necessary, and -1
16529 if point would appear in the scroll margins.
16530 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16531 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16532 set in FLAGS.) */
16533
16534 int
16535 try_window (Lisp_Object window, struct text_pos pos, int flags)
16536 {
16537 struct window *w = XWINDOW (window);
16538 struct it it;
16539 struct glyph_row *last_text_row = NULL;
16540 struct frame *f = XFRAME (w->frame);
16541 int frame_line_height = default_line_pixel_height (w);
16542
16543 /* Make POS the new window start. */
16544 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16545
16546 /* Mark cursor position as unknown. No overlay arrow seen. */
16547 w->cursor.vpos = -1;
16548 overlay_arrow_seen = 0;
16549
16550 /* Initialize iterator and info to start at POS. */
16551 start_display (&it, w, pos);
16552
16553 /* Display all lines of W. */
16554 while (it.current_y < it.last_visible_y)
16555 {
16556 if (display_line (&it))
16557 last_text_row = it.glyph_row - 1;
16558 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16559 return 0;
16560 }
16561
16562 /* Don't let the cursor end in the scroll margins. */
16563 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16564 && !MINI_WINDOW_P (w))
16565 {
16566 int this_scroll_margin;
16567 int window_total_lines
16568 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16569
16570 if (scroll_margin > 0)
16571 {
16572 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16573 this_scroll_margin *= frame_line_height;
16574 }
16575 else
16576 this_scroll_margin = 0;
16577
16578 if ((w->cursor.y >= 0 /* not vscrolled */
16579 && w->cursor.y < this_scroll_margin
16580 && CHARPOS (pos) > BEGV
16581 && IT_CHARPOS (it) < ZV)
16582 /* rms: considering make_cursor_line_fully_visible_p here
16583 seems to give wrong results. We don't want to recenter
16584 when the last line is partly visible, we want to allow
16585 that case to be handled in the usual way. */
16586 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16587 {
16588 w->cursor.vpos = -1;
16589 clear_glyph_matrix (w->desired_matrix);
16590 return -1;
16591 }
16592 }
16593
16594 /* If bottom moved off end of frame, change mode line percentage. */
16595 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16596 w->update_mode_line = 1;
16597
16598 /* Set window_end_pos to the offset of the last character displayed
16599 on the window from the end of current_buffer. Set
16600 window_end_vpos to its row number. */
16601 if (last_text_row)
16602 {
16603 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16604 adjust_window_ends (w, last_text_row, 0);
16605 eassert
16606 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16607 w->window_end_vpos)));
16608 }
16609 else
16610 {
16611 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16612 w->window_end_pos = Z - ZV;
16613 w->window_end_vpos = 0;
16614 }
16615
16616 /* But that is not valid info until redisplay finishes. */
16617 w->window_end_valid = 0;
16618 return 1;
16619 }
16620
16621
16622 \f
16623 /************************************************************************
16624 Window redisplay reusing current matrix when buffer has not changed
16625 ************************************************************************/
16626
16627 /* Try redisplay of window W showing an unchanged buffer with a
16628 different window start than the last time it was displayed by
16629 reusing its current matrix. Value is non-zero if successful.
16630 W->start is the new window start. */
16631
16632 static int
16633 try_window_reusing_current_matrix (struct window *w)
16634 {
16635 struct frame *f = XFRAME (w->frame);
16636 struct glyph_row *bottom_row;
16637 struct it it;
16638 struct run run;
16639 struct text_pos start, new_start;
16640 int nrows_scrolled, i;
16641 struct glyph_row *last_text_row;
16642 struct glyph_row *last_reused_text_row;
16643 struct glyph_row *start_row;
16644 int start_vpos, min_y, max_y;
16645
16646 #ifdef GLYPH_DEBUG
16647 if (inhibit_try_window_reusing)
16648 return 0;
16649 #endif
16650
16651 if (/* This function doesn't handle terminal frames. */
16652 !FRAME_WINDOW_P (f)
16653 /* Don't try to reuse the display if windows have been split
16654 or such. */
16655 || windows_or_buffers_changed
16656 || f->cursor_type_changed)
16657 return 0;
16658
16659 /* Can't do this if showing trailing whitespace. */
16660 if (!NILP (Vshow_trailing_whitespace))
16661 return 0;
16662
16663 /* If top-line visibility has changed, give up. */
16664 if (WINDOW_WANTS_HEADER_LINE_P (w)
16665 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16666 return 0;
16667
16668 /* Give up if old or new display is scrolled vertically. We could
16669 make this function handle this, but right now it doesn't. */
16670 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16671 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16672 return 0;
16673
16674 /* The variable new_start now holds the new window start. The old
16675 start `start' can be determined from the current matrix. */
16676 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16677 start = start_row->minpos;
16678 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16679
16680 /* Clear the desired matrix for the display below. */
16681 clear_glyph_matrix (w->desired_matrix);
16682
16683 if (CHARPOS (new_start) <= CHARPOS (start))
16684 {
16685 /* Don't use this method if the display starts with an ellipsis
16686 displayed for invisible text. It's not easy to handle that case
16687 below, and it's certainly not worth the effort since this is
16688 not a frequent case. */
16689 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16690 return 0;
16691
16692 IF_DEBUG (debug_method_add (w, "twu1"));
16693
16694 /* Display up to a row that can be reused. The variable
16695 last_text_row is set to the last row displayed that displays
16696 text. Note that it.vpos == 0 if or if not there is a
16697 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16698 start_display (&it, w, new_start);
16699 w->cursor.vpos = -1;
16700 last_text_row = last_reused_text_row = NULL;
16701
16702 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16703 {
16704 /* If we have reached into the characters in the START row,
16705 that means the line boundaries have changed. So we
16706 can't start copying with the row START. Maybe it will
16707 work to start copying with the following row. */
16708 while (IT_CHARPOS (it) > CHARPOS (start))
16709 {
16710 /* Advance to the next row as the "start". */
16711 start_row++;
16712 start = start_row->minpos;
16713 /* If there are no more rows to try, or just one, give up. */
16714 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16715 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16716 || CHARPOS (start) == ZV)
16717 {
16718 clear_glyph_matrix (w->desired_matrix);
16719 return 0;
16720 }
16721
16722 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16723 }
16724 /* If we have reached alignment, we can copy the rest of the
16725 rows. */
16726 if (IT_CHARPOS (it) == CHARPOS (start)
16727 /* Don't accept "alignment" inside a display vector,
16728 since start_row could have started in the middle of
16729 that same display vector (thus their character
16730 positions match), and we have no way of telling if
16731 that is the case. */
16732 && it.current.dpvec_index < 0)
16733 break;
16734
16735 if (display_line (&it))
16736 last_text_row = it.glyph_row - 1;
16737
16738 }
16739
16740 /* A value of current_y < last_visible_y means that we stopped
16741 at the previous window start, which in turn means that we
16742 have at least one reusable row. */
16743 if (it.current_y < it.last_visible_y)
16744 {
16745 struct glyph_row *row;
16746
16747 /* IT.vpos always starts from 0; it counts text lines. */
16748 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16749
16750 /* Find PT if not already found in the lines displayed. */
16751 if (w->cursor.vpos < 0)
16752 {
16753 int dy = it.current_y - start_row->y;
16754
16755 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16756 row = row_containing_pos (w, PT, row, NULL, dy);
16757 if (row)
16758 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16759 dy, nrows_scrolled);
16760 else
16761 {
16762 clear_glyph_matrix (w->desired_matrix);
16763 return 0;
16764 }
16765 }
16766
16767 /* Scroll the display. Do it before the current matrix is
16768 changed. The problem here is that update has not yet
16769 run, i.e. part of the current matrix is not up to date.
16770 scroll_run_hook will clear the cursor, and use the
16771 current matrix to get the height of the row the cursor is
16772 in. */
16773 run.current_y = start_row->y;
16774 run.desired_y = it.current_y;
16775 run.height = it.last_visible_y - it.current_y;
16776
16777 if (run.height > 0 && run.current_y != run.desired_y)
16778 {
16779 update_begin (f);
16780 FRAME_RIF (f)->update_window_begin_hook (w);
16781 FRAME_RIF (f)->clear_window_mouse_face (w);
16782 FRAME_RIF (f)->scroll_run_hook (w, &run);
16783 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16784 update_end (f);
16785 }
16786
16787 /* Shift current matrix down by nrows_scrolled lines. */
16788 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16789 rotate_matrix (w->current_matrix,
16790 start_vpos,
16791 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16792 nrows_scrolled);
16793
16794 /* Disable lines that must be updated. */
16795 for (i = 0; i < nrows_scrolled; ++i)
16796 (start_row + i)->enabled_p = false;
16797
16798 /* Re-compute Y positions. */
16799 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16800 max_y = it.last_visible_y;
16801 for (row = start_row + nrows_scrolled;
16802 row < bottom_row;
16803 ++row)
16804 {
16805 row->y = it.current_y;
16806 row->visible_height = row->height;
16807
16808 if (row->y < min_y)
16809 row->visible_height -= min_y - row->y;
16810 if (row->y + row->height > max_y)
16811 row->visible_height -= row->y + row->height - max_y;
16812 if (row->fringe_bitmap_periodic_p)
16813 row->redraw_fringe_bitmaps_p = 1;
16814
16815 it.current_y += row->height;
16816
16817 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16818 last_reused_text_row = row;
16819 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16820 break;
16821 }
16822
16823 /* Disable lines in the current matrix which are now
16824 below the window. */
16825 for (++row; row < bottom_row; ++row)
16826 row->enabled_p = row->mode_line_p = 0;
16827 }
16828
16829 /* Update window_end_pos etc.; last_reused_text_row is the last
16830 reused row from the current matrix containing text, if any.
16831 The value of last_text_row is the last displayed line
16832 containing text. */
16833 if (last_reused_text_row)
16834 adjust_window_ends (w, last_reused_text_row, 1);
16835 else if (last_text_row)
16836 adjust_window_ends (w, last_text_row, 0);
16837 else
16838 {
16839 /* This window must be completely empty. */
16840 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16841 w->window_end_pos = Z - ZV;
16842 w->window_end_vpos = 0;
16843 }
16844 w->window_end_valid = 0;
16845
16846 /* Update hint: don't try scrolling again in update_window. */
16847 w->desired_matrix->no_scrolling_p = 1;
16848
16849 #ifdef GLYPH_DEBUG
16850 debug_method_add (w, "try_window_reusing_current_matrix 1");
16851 #endif
16852 return 1;
16853 }
16854 else if (CHARPOS (new_start) > CHARPOS (start))
16855 {
16856 struct glyph_row *pt_row, *row;
16857 struct glyph_row *first_reusable_row;
16858 struct glyph_row *first_row_to_display;
16859 int dy;
16860 int yb = window_text_bottom_y (w);
16861
16862 /* Find the row starting at new_start, if there is one. Don't
16863 reuse a partially visible line at the end. */
16864 first_reusable_row = start_row;
16865 while (first_reusable_row->enabled_p
16866 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16867 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16868 < CHARPOS (new_start)))
16869 ++first_reusable_row;
16870
16871 /* Give up if there is no row to reuse. */
16872 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16873 || !first_reusable_row->enabled_p
16874 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16875 != CHARPOS (new_start)))
16876 return 0;
16877
16878 /* We can reuse fully visible rows beginning with
16879 first_reusable_row to the end of the window. Set
16880 first_row_to_display to the first row that cannot be reused.
16881 Set pt_row to the row containing point, if there is any. */
16882 pt_row = NULL;
16883 for (first_row_to_display = first_reusable_row;
16884 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16885 ++first_row_to_display)
16886 {
16887 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16888 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16889 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16890 && first_row_to_display->ends_at_zv_p
16891 && pt_row == NULL)))
16892 pt_row = first_row_to_display;
16893 }
16894
16895 /* Start displaying at the start of first_row_to_display. */
16896 eassert (first_row_to_display->y < yb);
16897 init_to_row_start (&it, w, first_row_to_display);
16898
16899 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16900 - start_vpos);
16901 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16902 - nrows_scrolled);
16903 it.current_y = (first_row_to_display->y - first_reusable_row->y
16904 + WINDOW_HEADER_LINE_HEIGHT (w));
16905
16906 /* Display lines beginning with first_row_to_display in the
16907 desired matrix. Set last_text_row to the last row displayed
16908 that displays text. */
16909 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16910 if (pt_row == NULL)
16911 w->cursor.vpos = -1;
16912 last_text_row = NULL;
16913 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16914 if (display_line (&it))
16915 last_text_row = it.glyph_row - 1;
16916
16917 /* If point is in a reused row, adjust y and vpos of the cursor
16918 position. */
16919 if (pt_row)
16920 {
16921 w->cursor.vpos -= nrows_scrolled;
16922 w->cursor.y -= first_reusable_row->y - start_row->y;
16923 }
16924
16925 /* Give up if point isn't in a row displayed or reused. (This
16926 also handles the case where w->cursor.vpos < nrows_scrolled
16927 after the calls to display_line, which can happen with scroll
16928 margins. See bug#1295.) */
16929 if (w->cursor.vpos < 0)
16930 {
16931 clear_glyph_matrix (w->desired_matrix);
16932 return 0;
16933 }
16934
16935 /* Scroll the display. */
16936 run.current_y = first_reusable_row->y;
16937 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16938 run.height = it.last_visible_y - run.current_y;
16939 dy = run.current_y - run.desired_y;
16940
16941 if (run.height)
16942 {
16943 update_begin (f);
16944 FRAME_RIF (f)->update_window_begin_hook (w);
16945 FRAME_RIF (f)->clear_window_mouse_face (w);
16946 FRAME_RIF (f)->scroll_run_hook (w, &run);
16947 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16948 update_end (f);
16949 }
16950
16951 /* Adjust Y positions of reused rows. */
16952 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16953 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16954 max_y = it.last_visible_y;
16955 for (row = first_reusable_row; row < first_row_to_display; ++row)
16956 {
16957 row->y -= dy;
16958 row->visible_height = row->height;
16959 if (row->y < min_y)
16960 row->visible_height -= min_y - row->y;
16961 if (row->y + row->height > max_y)
16962 row->visible_height -= row->y + row->height - max_y;
16963 if (row->fringe_bitmap_periodic_p)
16964 row->redraw_fringe_bitmaps_p = 1;
16965 }
16966
16967 /* Scroll the current matrix. */
16968 eassert (nrows_scrolled > 0);
16969 rotate_matrix (w->current_matrix,
16970 start_vpos,
16971 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16972 -nrows_scrolled);
16973
16974 /* Disable rows not reused. */
16975 for (row -= nrows_scrolled; row < bottom_row; ++row)
16976 row->enabled_p = false;
16977
16978 /* Point may have moved to a different line, so we cannot assume that
16979 the previous cursor position is valid; locate the correct row. */
16980 if (pt_row)
16981 {
16982 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16983 row < bottom_row
16984 && PT >= MATRIX_ROW_END_CHARPOS (row)
16985 && !row->ends_at_zv_p;
16986 row++)
16987 {
16988 w->cursor.vpos++;
16989 w->cursor.y = row->y;
16990 }
16991 if (row < bottom_row)
16992 {
16993 /* Can't simply scan the row for point with
16994 bidi-reordered glyph rows. Let set_cursor_from_row
16995 figure out where to put the cursor, and if it fails,
16996 give up. */
16997 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16998 {
16999 if (!set_cursor_from_row (w, row, w->current_matrix,
17000 0, 0, 0, 0))
17001 {
17002 clear_glyph_matrix (w->desired_matrix);
17003 return 0;
17004 }
17005 }
17006 else
17007 {
17008 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17009 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17010
17011 for (; glyph < end
17012 && (!BUFFERP (glyph->object)
17013 || glyph->charpos < PT);
17014 glyph++)
17015 {
17016 w->cursor.hpos++;
17017 w->cursor.x += glyph->pixel_width;
17018 }
17019 }
17020 }
17021 }
17022
17023 /* Adjust window end. A null value of last_text_row means that
17024 the window end is in reused rows which in turn means that
17025 only its vpos can have changed. */
17026 if (last_text_row)
17027 adjust_window_ends (w, last_text_row, 0);
17028 else
17029 w->window_end_vpos -= nrows_scrolled;
17030
17031 w->window_end_valid = 0;
17032 w->desired_matrix->no_scrolling_p = 1;
17033
17034 #ifdef GLYPH_DEBUG
17035 debug_method_add (w, "try_window_reusing_current_matrix 2");
17036 #endif
17037 return 1;
17038 }
17039
17040 return 0;
17041 }
17042
17043
17044 \f
17045 /************************************************************************
17046 Window redisplay reusing current matrix when buffer has changed
17047 ************************************************************************/
17048
17049 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17050 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17051 ptrdiff_t *, ptrdiff_t *);
17052 static struct glyph_row *
17053 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17054 struct glyph_row *);
17055
17056
17057 /* Return the last row in MATRIX displaying text. If row START is
17058 non-null, start searching with that row. IT gives the dimensions
17059 of the display. Value is null if matrix is empty; otherwise it is
17060 a pointer to the row found. */
17061
17062 static struct glyph_row *
17063 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17064 struct glyph_row *start)
17065 {
17066 struct glyph_row *row, *row_found;
17067
17068 /* Set row_found to the last row in IT->w's current matrix
17069 displaying text. The loop looks funny but think of partially
17070 visible lines. */
17071 row_found = NULL;
17072 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17073 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17074 {
17075 eassert (row->enabled_p);
17076 row_found = row;
17077 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17078 break;
17079 ++row;
17080 }
17081
17082 return row_found;
17083 }
17084
17085
17086 /* Return the last row in the current matrix of W that is not affected
17087 by changes at the start of current_buffer that occurred since W's
17088 current matrix was built. Value is null if no such row exists.
17089
17090 BEG_UNCHANGED us the number of characters unchanged at the start of
17091 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17092 first changed character in current_buffer. Characters at positions <
17093 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17094 when the current matrix was built. */
17095
17096 static struct glyph_row *
17097 find_last_unchanged_at_beg_row (struct window *w)
17098 {
17099 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17100 struct glyph_row *row;
17101 struct glyph_row *row_found = NULL;
17102 int yb = window_text_bottom_y (w);
17103
17104 /* Find the last row displaying unchanged text. */
17105 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17106 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17107 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17108 ++row)
17109 {
17110 if (/* If row ends before first_changed_pos, it is unchanged,
17111 except in some case. */
17112 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17113 /* When row ends in ZV and we write at ZV it is not
17114 unchanged. */
17115 && !row->ends_at_zv_p
17116 /* When first_changed_pos is the end of a continued line,
17117 row is not unchanged because it may be no longer
17118 continued. */
17119 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17120 && (row->continued_p
17121 || row->exact_window_width_line_p))
17122 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17123 needs to be recomputed, so don't consider this row as
17124 unchanged. This happens when the last line was
17125 bidi-reordered and was killed immediately before this
17126 redisplay cycle. In that case, ROW->end stores the
17127 buffer position of the first visual-order character of
17128 the killed text, which is now beyond ZV. */
17129 && CHARPOS (row->end.pos) <= ZV)
17130 row_found = row;
17131
17132 /* Stop if last visible row. */
17133 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17134 break;
17135 }
17136
17137 return row_found;
17138 }
17139
17140
17141 /* Find the first glyph row in the current matrix of W that is not
17142 affected by changes at the end of current_buffer since the
17143 time W's current matrix was built.
17144
17145 Return in *DELTA the number of chars by which buffer positions in
17146 unchanged text at the end of current_buffer must be adjusted.
17147
17148 Return in *DELTA_BYTES the corresponding number of bytes.
17149
17150 Value is null if no such row exists, i.e. all rows are affected by
17151 changes. */
17152
17153 static struct glyph_row *
17154 find_first_unchanged_at_end_row (struct window *w,
17155 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17156 {
17157 struct glyph_row *row;
17158 struct glyph_row *row_found = NULL;
17159
17160 *delta = *delta_bytes = 0;
17161
17162 /* Display must not have been paused, otherwise the current matrix
17163 is not up to date. */
17164 eassert (w->window_end_valid);
17165
17166 /* A value of window_end_pos >= END_UNCHANGED means that the window
17167 end is in the range of changed text. If so, there is no
17168 unchanged row at the end of W's current matrix. */
17169 if (w->window_end_pos >= END_UNCHANGED)
17170 return NULL;
17171
17172 /* Set row to the last row in W's current matrix displaying text. */
17173 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17174
17175 /* If matrix is entirely empty, no unchanged row exists. */
17176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17177 {
17178 /* The value of row is the last glyph row in the matrix having a
17179 meaningful buffer position in it. The end position of row
17180 corresponds to window_end_pos. This allows us to translate
17181 buffer positions in the current matrix to current buffer
17182 positions for characters not in changed text. */
17183 ptrdiff_t Z_old =
17184 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17185 ptrdiff_t Z_BYTE_old =
17186 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17187 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17188 struct glyph_row *first_text_row
17189 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17190
17191 *delta = Z - Z_old;
17192 *delta_bytes = Z_BYTE - Z_BYTE_old;
17193
17194 /* Set last_unchanged_pos to the buffer position of the last
17195 character in the buffer that has not been changed. Z is the
17196 index + 1 of the last character in current_buffer, i.e. by
17197 subtracting END_UNCHANGED we get the index of the last
17198 unchanged character, and we have to add BEG to get its buffer
17199 position. */
17200 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17201 last_unchanged_pos_old = last_unchanged_pos - *delta;
17202
17203 /* Search backward from ROW for a row displaying a line that
17204 starts at a minimum position >= last_unchanged_pos_old. */
17205 for (; row > first_text_row; --row)
17206 {
17207 /* This used to abort, but it can happen.
17208 It is ok to just stop the search instead here. KFS. */
17209 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17210 break;
17211
17212 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17213 row_found = row;
17214 }
17215 }
17216
17217 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17218
17219 return row_found;
17220 }
17221
17222
17223 /* Make sure that glyph rows in the current matrix of window W
17224 reference the same glyph memory as corresponding rows in the
17225 frame's frame matrix. This function is called after scrolling W's
17226 current matrix on a terminal frame in try_window_id and
17227 try_window_reusing_current_matrix. */
17228
17229 static void
17230 sync_frame_with_window_matrix_rows (struct window *w)
17231 {
17232 struct frame *f = XFRAME (w->frame);
17233 struct glyph_row *window_row, *window_row_end, *frame_row;
17234
17235 /* Preconditions: W must be a leaf window and full-width. Its frame
17236 must have a frame matrix. */
17237 eassert (BUFFERP (w->contents));
17238 eassert (WINDOW_FULL_WIDTH_P (w));
17239 eassert (!FRAME_WINDOW_P (f));
17240
17241 /* If W is a full-width window, glyph pointers in W's current matrix
17242 have, by definition, to be the same as glyph pointers in the
17243 corresponding frame matrix. Note that frame matrices have no
17244 marginal areas (see build_frame_matrix). */
17245 window_row = w->current_matrix->rows;
17246 window_row_end = window_row + w->current_matrix->nrows;
17247 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17248 while (window_row < window_row_end)
17249 {
17250 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17251 struct glyph *end = window_row->glyphs[LAST_AREA];
17252
17253 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17254 frame_row->glyphs[TEXT_AREA] = start;
17255 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17256 frame_row->glyphs[LAST_AREA] = end;
17257
17258 /* Disable frame rows whose corresponding window rows have
17259 been disabled in try_window_id. */
17260 if (!window_row->enabled_p)
17261 frame_row->enabled_p = false;
17262
17263 ++window_row, ++frame_row;
17264 }
17265 }
17266
17267
17268 /* Find the glyph row in window W containing CHARPOS. Consider all
17269 rows between START and END (not inclusive). END null means search
17270 all rows to the end of the display area of W. Value is the row
17271 containing CHARPOS or null. */
17272
17273 struct glyph_row *
17274 row_containing_pos (struct window *w, ptrdiff_t charpos,
17275 struct glyph_row *start, struct glyph_row *end, int dy)
17276 {
17277 struct glyph_row *row = start;
17278 struct glyph_row *best_row = NULL;
17279 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17280 int last_y;
17281
17282 /* If we happen to start on a header-line, skip that. */
17283 if (row->mode_line_p)
17284 ++row;
17285
17286 if ((end && row >= end) || !row->enabled_p)
17287 return NULL;
17288
17289 last_y = window_text_bottom_y (w) - dy;
17290
17291 while (1)
17292 {
17293 /* Give up if we have gone too far. */
17294 if (end && row >= end)
17295 return NULL;
17296 /* This formerly returned if they were equal.
17297 I think that both quantities are of a "last plus one" type;
17298 if so, when they are equal, the row is within the screen. -- rms. */
17299 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17300 return NULL;
17301
17302 /* If it is in this row, return this row. */
17303 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17304 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17305 /* The end position of a row equals the start
17306 position of the next row. If CHARPOS is there, we
17307 would rather consider it displayed in the next
17308 line, except when this line ends in ZV. */
17309 && !row_for_charpos_p (row, charpos)))
17310 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17311 {
17312 struct glyph *g;
17313
17314 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17315 || (!best_row && !row->continued_p))
17316 return row;
17317 /* In bidi-reordered rows, there could be several rows whose
17318 edges surround CHARPOS, all of these rows belonging to
17319 the same continued line. We need to find the row which
17320 fits CHARPOS the best. */
17321 for (g = row->glyphs[TEXT_AREA];
17322 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17323 g++)
17324 {
17325 if (!STRINGP (g->object))
17326 {
17327 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17328 {
17329 mindif = eabs (g->charpos - charpos);
17330 best_row = row;
17331 /* Exact match always wins. */
17332 if (mindif == 0)
17333 return best_row;
17334 }
17335 }
17336 }
17337 }
17338 else if (best_row && !row->continued_p)
17339 return best_row;
17340 ++row;
17341 }
17342 }
17343
17344
17345 /* Try to redisplay window W by reusing its existing display. W's
17346 current matrix must be up to date when this function is called,
17347 i.e. window_end_valid must be nonzero.
17348
17349 Value is
17350
17351 >= 1 if successful, i.e. display has been updated
17352 specifically:
17353 1 means the changes were in front of a newline that precedes
17354 the window start, and the whole current matrix was reused
17355 2 means the changes were after the last position displayed
17356 in the window, and the whole current matrix was reused
17357 3 means portions of the current matrix were reused, while
17358 some of the screen lines were redrawn
17359 -1 if redisplay with same window start is known not to succeed
17360 0 if otherwise unsuccessful
17361
17362 The following steps are performed:
17363
17364 1. Find the last row in the current matrix of W that is not
17365 affected by changes at the start of current_buffer. If no such row
17366 is found, give up.
17367
17368 2. Find the first row in W's current matrix that is not affected by
17369 changes at the end of current_buffer. Maybe there is no such row.
17370
17371 3. Display lines beginning with the row + 1 found in step 1 to the
17372 row found in step 2 or, if step 2 didn't find a row, to the end of
17373 the window.
17374
17375 4. If cursor is not known to appear on the window, give up.
17376
17377 5. If display stopped at the row found in step 2, scroll the
17378 display and current matrix as needed.
17379
17380 6. Maybe display some lines at the end of W, if we must. This can
17381 happen under various circumstances, like a partially visible line
17382 becoming fully visible, or because newly displayed lines are displayed
17383 in smaller font sizes.
17384
17385 7. Update W's window end information. */
17386
17387 static int
17388 try_window_id (struct window *w)
17389 {
17390 struct frame *f = XFRAME (w->frame);
17391 struct glyph_matrix *current_matrix = w->current_matrix;
17392 struct glyph_matrix *desired_matrix = w->desired_matrix;
17393 struct glyph_row *last_unchanged_at_beg_row;
17394 struct glyph_row *first_unchanged_at_end_row;
17395 struct glyph_row *row;
17396 struct glyph_row *bottom_row;
17397 int bottom_vpos;
17398 struct it it;
17399 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17400 int dvpos, dy;
17401 struct text_pos start_pos;
17402 struct run run;
17403 int first_unchanged_at_end_vpos = 0;
17404 struct glyph_row *last_text_row, *last_text_row_at_end;
17405 struct text_pos start;
17406 ptrdiff_t first_changed_charpos, last_changed_charpos;
17407
17408 #ifdef GLYPH_DEBUG
17409 if (inhibit_try_window_id)
17410 return 0;
17411 #endif
17412
17413 /* This is handy for debugging. */
17414 #if 0
17415 #define GIVE_UP(X) \
17416 do { \
17417 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17418 return 0; \
17419 } while (0)
17420 #else
17421 #define GIVE_UP(X) return 0
17422 #endif
17423
17424 SET_TEXT_POS_FROM_MARKER (start, w->start);
17425
17426 /* Don't use this for mini-windows because these can show
17427 messages and mini-buffers, and we don't handle that here. */
17428 if (MINI_WINDOW_P (w))
17429 GIVE_UP (1);
17430
17431 /* This flag is used to prevent redisplay optimizations. */
17432 if (windows_or_buffers_changed || f->cursor_type_changed)
17433 GIVE_UP (2);
17434
17435 /* This function's optimizations cannot be used if overlays have
17436 changed in the buffer displayed by the window, so give up if they
17437 have. */
17438 if (w->last_overlay_modified != OVERLAY_MODIFF)
17439 GIVE_UP (21);
17440
17441 /* Verify that narrowing has not changed.
17442 Also verify that we were not told to prevent redisplay optimizations.
17443 It would be nice to further
17444 reduce the number of cases where this prevents try_window_id. */
17445 if (current_buffer->clip_changed
17446 || current_buffer->prevent_redisplay_optimizations_p)
17447 GIVE_UP (3);
17448
17449 /* Window must either use window-based redisplay or be full width. */
17450 if (!FRAME_WINDOW_P (f)
17451 && (!FRAME_LINE_INS_DEL_OK (f)
17452 || !WINDOW_FULL_WIDTH_P (w)))
17453 GIVE_UP (4);
17454
17455 /* Give up if point is known NOT to appear in W. */
17456 if (PT < CHARPOS (start))
17457 GIVE_UP (5);
17458
17459 /* Another way to prevent redisplay optimizations. */
17460 if (w->last_modified == 0)
17461 GIVE_UP (6);
17462
17463 /* Verify that window is not hscrolled. */
17464 if (w->hscroll != 0)
17465 GIVE_UP (7);
17466
17467 /* Verify that display wasn't paused. */
17468 if (!w->window_end_valid)
17469 GIVE_UP (8);
17470
17471 /* Likewise if highlighting trailing whitespace. */
17472 if (!NILP (Vshow_trailing_whitespace))
17473 GIVE_UP (11);
17474
17475 /* Can't use this if overlay arrow position and/or string have
17476 changed. */
17477 if (overlay_arrows_changed_p ())
17478 GIVE_UP (12);
17479
17480 /* When word-wrap is on, adding a space to the first word of a
17481 wrapped line can change the wrap position, altering the line
17482 above it. It might be worthwhile to handle this more
17483 intelligently, but for now just redisplay from scratch. */
17484 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17485 GIVE_UP (21);
17486
17487 /* Under bidi reordering, adding or deleting a character in the
17488 beginning of a paragraph, before the first strong directional
17489 character, can change the base direction of the paragraph (unless
17490 the buffer specifies a fixed paragraph direction), which will
17491 require to redisplay the whole paragraph. It might be worthwhile
17492 to find the paragraph limits and widen the range of redisplayed
17493 lines to that, but for now just give up this optimization and
17494 redisplay from scratch. */
17495 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17496 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17497 GIVE_UP (22);
17498
17499 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17500 only if buffer has really changed. The reason is that the gap is
17501 initially at Z for freshly visited files. The code below would
17502 set end_unchanged to 0 in that case. */
17503 if (MODIFF > SAVE_MODIFF
17504 /* This seems to happen sometimes after saving a buffer. */
17505 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17506 {
17507 if (GPT - BEG < BEG_UNCHANGED)
17508 BEG_UNCHANGED = GPT - BEG;
17509 if (Z - GPT < END_UNCHANGED)
17510 END_UNCHANGED = Z - GPT;
17511 }
17512
17513 /* The position of the first and last character that has been changed. */
17514 first_changed_charpos = BEG + BEG_UNCHANGED;
17515 last_changed_charpos = Z - END_UNCHANGED;
17516
17517 /* If window starts after a line end, and the last change is in
17518 front of that newline, then changes don't affect the display.
17519 This case happens with stealth-fontification. Note that although
17520 the display is unchanged, glyph positions in the matrix have to
17521 be adjusted, of course. */
17522 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17523 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17524 && ((last_changed_charpos < CHARPOS (start)
17525 && CHARPOS (start) == BEGV)
17526 || (last_changed_charpos < CHARPOS (start) - 1
17527 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17528 {
17529 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17530 struct glyph_row *r0;
17531
17532 /* Compute how many chars/bytes have been added to or removed
17533 from the buffer. */
17534 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17535 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17536 Z_delta = Z - Z_old;
17537 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17538
17539 /* Give up if PT is not in the window. Note that it already has
17540 been checked at the start of try_window_id that PT is not in
17541 front of the window start. */
17542 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17543 GIVE_UP (13);
17544
17545 /* If window start is unchanged, we can reuse the whole matrix
17546 as is, after adjusting glyph positions. No need to compute
17547 the window end again, since its offset from Z hasn't changed. */
17548 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17549 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17550 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17551 /* PT must not be in a partially visible line. */
17552 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17553 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17554 {
17555 /* Adjust positions in the glyph matrix. */
17556 if (Z_delta || Z_delta_bytes)
17557 {
17558 struct glyph_row *r1
17559 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17560 increment_matrix_positions (w->current_matrix,
17561 MATRIX_ROW_VPOS (r0, current_matrix),
17562 MATRIX_ROW_VPOS (r1, current_matrix),
17563 Z_delta, Z_delta_bytes);
17564 }
17565
17566 /* Set the cursor. */
17567 row = row_containing_pos (w, PT, r0, NULL, 0);
17568 if (row)
17569 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17570 return 1;
17571 }
17572 }
17573
17574 /* Handle the case that changes are all below what is displayed in
17575 the window, and that PT is in the window. This shortcut cannot
17576 be taken if ZV is visible in the window, and text has been added
17577 there that is visible in the window. */
17578 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17579 /* ZV is not visible in the window, or there are no
17580 changes at ZV, actually. */
17581 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17582 || first_changed_charpos == last_changed_charpos))
17583 {
17584 struct glyph_row *r0;
17585
17586 /* Give up if PT is not in the window. Note that it already has
17587 been checked at the start of try_window_id that PT is not in
17588 front of the window start. */
17589 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17590 GIVE_UP (14);
17591
17592 /* If window start is unchanged, we can reuse the whole matrix
17593 as is, without changing glyph positions since no text has
17594 been added/removed in front of the window end. */
17595 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17596 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17597 /* PT must not be in a partially visible line. */
17598 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17599 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17600 {
17601 /* We have to compute the window end anew since text
17602 could have been added/removed after it. */
17603 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17604 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17605
17606 /* Set the cursor. */
17607 row = row_containing_pos (w, PT, r0, NULL, 0);
17608 if (row)
17609 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17610 return 2;
17611 }
17612 }
17613
17614 /* Give up if window start is in the changed area.
17615
17616 The condition used to read
17617
17618 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17619
17620 but why that was tested escapes me at the moment. */
17621 if (CHARPOS (start) >= first_changed_charpos
17622 && CHARPOS (start) <= last_changed_charpos)
17623 GIVE_UP (15);
17624
17625 /* Check that window start agrees with the start of the first glyph
17626 row in its current matrix. Check this after we know the window
17627 start is not in changed text, otherwise positions would not be
17628 comparable. */
17629 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17630 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17631 GIVE_UP (16);
17632
17633 /* Give up if the window ends in strings. Overlay strings
17634 at the end are difficult to handle, so don't try. */
17635 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17636 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17637 GIVE_UP (20);
17638
17639 /* Compute the position at which we have to start displaying new
17640 lines. Some of the lines at the top of the window might be
17641 reusable because they are not displaying changed text. Find the
17642 last row in W's current matrix not affected by changes at the
17643 start of current_buffer. Value is null if changes start in the
17644 first line of window. */
17645 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17646 if (last_unchanged_at_beg_row)
17647 {
17648 /* Avoid starting to display in the middle of a character, a TAB
17649 for instance. This is easier than to set up the iterator
17650 exactly, and it's not a frequent case, so the additional
17651 effort wouldn't really pay off. */
17652 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17653 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17654 && last_unchanged_at_beg_row > w->current_matrix->rows)
17655 --last_unchanged_at_beg_row;
17656
17657 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17658 GIVE_UP (17);
17659
17660 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17661 GIVE_UP (18);
17662 start_pos = it.current.pos;
17663
17664 /* Start displaying new lines in the desired matrix at the same
17665 vpos we would use in the current matrix, i.e. below
17666 last_unchanged_at_beg_row. */
17667 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17668 current_matrix);
17669 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17670 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17671
17672 eassert (it.hpos == 0 && it.current_x == 0);
17673 }
17674 else
17675 {
17676 /* There are no reusable lines at the start of the window.
17677 Start displaying in the first text line. */
17678 start_display (&it, w, start);
17679 it.vpos = it.first_vpos;
17680 start_pos = it.current.pos;
17681 }
17682
17683 /* Find the first row that is not affected by changes at the end of
17684 the buffer. Value will be null if there is no unchanged row, in
17685 which case we must redisplay to the end of the window. delta
17686 will be set to the value by which buffer positions beginning with
17687 first_unchanged_at_end_row have to be adjusted due to text
17688 changes. */
17689 first_unchanged_at_end_row
17690 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17691 IF_DEBUG (debug_delta = delta);
17692 IF_DEBUG (debug_delta_bytes = delta_bytes);
17693
17694 /* Set stop_pos to the buffer position up to which we will have to
17695 display new lines. If first_unchanged_at_end_row != NULL, this
17696 is the buffer position of the start of the line displayed in that
17697 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17698 that we don't stop at a buffer position. */
17699 stop_pos = 0;
17700 if (first_unchanged_at_end_row)
17701 {
17702 eassert (last_unchanged_at_beg_row == NULL
17703 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17704
17705 /* If this is a continuation line, move forward to the next one
17706 that isn't. Changes in lines above affect this line.
17707 Caution: this may move first_unchanged_at_end_row to a row
17708 not displaying text. */
17709 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17710 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17711 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17712 < it.last_visible_y))
17713 ++first_unchanged_at_end_row;
17714
17715 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17716 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17717 >= it.last_visible_y))
17718 first_unchanged_at_end_row = NULL;
17719 else
17720 {
17721 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17722 + delta);
17723 first_unchanged_at_end_vpos
17724 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17725 eassert (stop_pos >= Z - END_UNCHANGED);
17726 }
17727 }
17728 else if (last_unchanged_at_beg_row == NULL)
17729 GIVE_UP (19);
17730
17731
17732 #ifdef GLYPH_DEBUG
17733
17734 /* Either there is no unchanged row at the end, or the one we have
17735 now displays text. This is a necessary condition for the window
17736 end pos calculation at the end of this function. */
17737 eassert (first_unchanged_at_end_row == NULL
17738 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17739
17740 debug_last_unchanged_at_beg_vpos
17741 = (last_unchanged_at_beg_row
17742 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17743 : -1);
17744 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17745
17746 #endif /* GLYPH_DEBUG */
17747
17748
17749 /* Display new lines. Set last_text_row to the last new line
17750 displayed which has text on it, i.e. might end up as being the
17751 line where the window_end_vpos is. */
17752 w->cursor.vpos = -1;
17753 last_text_row = NULL;
17754 overlay_arrow_seen = 0;
17755 while (it.current_y < it.last_visible_y
17756 && !f->fonts_changed
17757 && (first_unchanged_at_end_row == NULL
17758 || IT_CHARPOS (it) < stop_pos))
17759 {
17760 if (display_line (&it))
17761 last_text_row = it.glyph_row - 1;
17762 }
17763
17764 if (f->fonts_changed)
17765 return -1;
17766
17767
17768 /* Compute differences in buffer positions, y-positions etc. for
17769 lines reused at the bottom of the window. Compute what we can
17770 scroll. */
17771 if (first_unchanged_at_end_row
17772 /* No lines reused because we displayed everything up to the
17773 bottom of the window. */
17774 && it.current_y < it.last_visible_y)
17775 {
17776 dvpos = (it.vpos
17777 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17778 current_matrix));
17779 dy = it.current_y - first_unchanged_at_end_row->y;
17780 run.current_y = first_unchanged_at_end_row->y;
17781 run.desired_y = run.current_y + dy;
17782 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17783 }
17784 else
17785 {
17786 delta = delta_bytes = dvpos = dy
17787 = run.current_y = run.desired_y = run.height = 0;
17788 first_unchanged_at_end_row = NULL;
17789 }
17790 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17791
17792
17793 /* Find the cursor if not already found. We have to decide whether
17794 PT will appear on this window (it sometimes doesn't, but this is
17795 not a very frequent case.) This decision has to be made before
17796 the current matrix is altered. A value of cursor.vpos < 0 means
17797 that PT is either in one of the lines beginning at
17798 first_unchanged_at_end_row or below the window. Don't care for
17799 lines that might be displayed later at the window end; as
17800 mentioned, this is not a frequent case. */
17801 if (w->cursor.vpos < 0)
17802 {
17803 /* Cursor in unchanged rows at the top? */
17804 if (PT < CHARPOS (start_pos)
17805 && last_unchanged_at_beg_row)
17806 {
17807 row = row_containing_pos (w, PT,
17808 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17809 last_unchanged_at_beg_row + 1, 0);
17810 if (row)
17811 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17812 }
17813
17814 /* Start from first_unchanged_at_end_row looking for PT. */
17815 else if (first_unchanged_at_end_row)
17816 {
17817 row = row_containing_pos (w, PT - delta,
17818 first_unchanged_at_end_row, NULL, 0);
17819 if (row)
17820 set_cursor_from_row (w, row, w->current_matrix, delta,
17821 delta_bytes, dy, dvpos);
17822 }
17823
17824 /* Give up if cursor was not found. */
17825 if (w->cursor.vpos < 0)
17826 {
17827 clear_glyph_matrix (w->desired_matrix);
17828 return -1;
17829 }
17830 }
17831
17832 /* Don't let the cursor end in the scroll margins. */
17833 {
17834 int this_scroll_margin, cursor_height;
17835 int frame_line_height = default_line_pixel_height (w);
17836 int window_total_lines
17837 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17838
17839 this_scroll_margin =
17840 max (0, min (scroll_margin, window_total_lines / 4));
17841 this_scroll_margin *= frame_line_height;
17842 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17843
17844 if ((w->cursor.y < this_scroll_margin
17845 && CHARPOS (start) > BEGV)
17846 /* Old redisplay didn't take scroll margin into account at the bottom,
17847 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17848 || (w->cursor.y + (make_cursor_line_fully_visible_p
17849 ? cursor_height + this_scroll_margin
17850 : 1)) > it.last_visible_y)
17851 {
17852 w->cursor.vpos = -1;
17853 clear_glyph_matrix (w->desired_matrix);
17854 return -1;
17855 }
17856 }
17857
17858 /* Scroll the display. Do it before changing the current matrix so
17859 that xterm.c doesn't get confused about where the cursor glyph is
17860 found. */
17861 if (dy && run.height)
17862 {
17863 update_begin (f);
17864
17865 if (FRAME_WINDOW_P (f))
17866 {
17867 FRAME_RIF (f)->update_window_begin_hook (w);
17868 FRAME_RIF (f)->clear_window_mouse_face (w);
17869 FRAME_RIF (f)->scroll_run_hook (w, &run);
17870 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17871 }
17872 else
17873 {
17874 /* Terminal frame. In this case, dvpos gives the number of
17875 lines to scroll by; dvpos < 0 means scroll up. */
17876 int from_vpos
17877 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17878 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17879 int end = (WINDOW_TOP_EDGE_LINE (w)
17880 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17881 + window_internal_height (w));
17882
17883 #if defined (HAVE_GPM) || defined (MSDOS)
17884 x_clear_window_mouse_face (w);
17885 #endif
17886 /* Perform the operation on the screen. */
17887 if (dvpos > 0)
17888 {
17889 /* Scroll last_unchanged_at_beg_row to the end of the
17890 window down dvpos lines. */
17891 set_terminal_window (f, end);
17892
17893 /* On dumb terminals delete dvpos lines at the end
17894 before inserting dvpos empty lines. */
17895 if (!FRAME_SCROLL_REGION_OK (f))
17896 ins_del_lines (f, end - dvpos, -dvpos);
17897
17898 /* Insert dvpos empty lines in front of
17899 last_unchanged_at_beg_row. */
17900 ins_del_lines (f, from, dvpos);
17901 }
17902 else if (dvpos < 0)
17903 {
17904 /* Scroll up last_unchanged_at_beg_vpos to the end of
17905 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17906 set_terminal_window (f, end);
17907
17908 /* Delete dvpos lines in front of
17909 last_unchanged_at_beg_vpos. ins_del_lines will set
17910 the cursor to the given vpos and emit |dvpos| delete
17911 line sequences. */
17912 ins_del_lines (f, from + dvpos, dvpos);
17913
17914 /* On a dumb terminal insert dvpos empty lines at the
17915 end. */
17916 if (!FRAME_SCROLL_REGION_OK (f))
17917 ins_del_lines (f, end + dvpos, -dvpos);
17918 }
17919
17920 set_terminal_window (f, 0);
17921 }
17922
17923 update_end (f);
17924 }
17925
17926 /* Shift reused rows of the current matrix to the right position.
17927 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17928 text. */
17929 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17930 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17931 if (dvpos < 0)
17932 {
17933 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17934 bottom_vpos, dvpos);
17935 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17936 bottom_vpos);
17937 }
17938 else if (dvpos > 0)
17939 {
17940 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17941 bottom_vpos, dvpos);
17942 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17943 first_unchanged_at_end_vpos + dvpos);
17944 }
17945
17946 /* For frame-based redisplay, make sure that current frame and window
17947 matrix are in sync with respect to glyph memory. */
17948 if (!FRAME_WINDOW_P (f))
17949 sync_frame_with_window_matrix_rows (w);
17950
17951 /* Adjust buffer positions in reused rows. */
17952 if (delta || delta_bytes)
17953 increment_matrix_positions (current_matrix,
17954 first_unchanged_at_end_vpos + dvpos,
17955 bottom_vpos, delta, delta_bytes);
17956
17957 /* Adjust Y positions. */
17958 if (dy)
17959 shift_glyph_matrix (w, current_matrix,
17960 first_unchanged_at_end_vpos + dvpos,
17961 bottom_vpos, dy);
17962
17963 if (first_unchanged_at_end_row)
17964 {
17965 first_unchanged_at_end_row += dvpos;
17966 if (first_unchanged_at_end_row->y >= it.last_visible_y
17967 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17968 first_unchanged_at_end_row = NULL;
17969 }
17970
17971 /* If scrolling up, there may be some lines to display at the end of
17972 the window. */
17973 last_text_row_at_end = NULL;
17974 if (dy < 0)
17975 {
17976 /* Scrolling up can leave for example a partially visible line
17977 at the end of the window to be redisplayed. */
17978 /* Set last_row to the glyph row in the current matrix where the
17979 window end line is found. It has been moved up or down in
17980 the matrix by dvpos. */
17981 int last_vpos = w->window_end_vpos + dvpos;
17982 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17983
17984 /* If last_row is the window end line, it should display text. */
17985 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17986
17987 /* If window end line was partially visible before, begin
17988 displaying at that line. Otherwise begin displaying with the
17989 line following it. */
17990 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17991 {
17992 init_to_row_start (&it, w, last_row);
17993 it.vpos = last_vpos;
17994 it.current_y = last_row->y;
17995 }
17996 else
17997 {
17998 init_to_row_end (&it, w, last_row);
17999 it.vpos = 1 + last_vpos;
18000 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18001 ++last_row;
18002 }
18003
18004 /* We may start in a continuation line. If so, we have to
18005 get the right continuation_lines_width and current_x. */
18006 it.continuation_lines_width = last_row->continuation_lines_width;
18007 it.hpos = it.current_x = 0;
18008
18009 /* Display the rest of the lines at the window end. */
18010 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18011 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18012 {
18013 /* Is it always sure that the display agrees with lines in
18014 the current matrix? I don't think so, so we mark rows
18015 displayed invalid in the current matrix by setting their
18016 enabled_p flag to zero. */
18017 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18018 if (display_line (&it))
18019 last_text_row_at_end = it.glyph_row - 1;
18020 }
18021 }
18022
18023 /* Update window_end_pos and window_end_vpos. */
18024 if (first_unchanged_at_end_row && !last_text_row_at_end)
18025 {
18026 /* Window end line if one of the preserved rows from the current
18027 matrix. Set row to the last row displaying text in current
18028 matrix starting at first_unchanged_at_end_row, after
18029 scrolling. */
18030 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18031 row = find_last_row_displaying_text (w->current_matrix, &it,
18032 first_unchanged_at_end_row);
18033 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18034 adjust_window_ends (w, row, 1);
18035 eassert (w->window_end_bytepos >= 0);
18036 IF_DEBUG (debug_method_add (w, "A"));
18037 }
18038 else if (last_text_row_at_end)
18039 {
18040 adjust_window_ends (w, last_text_row_at_end, 0);
18041 eassert (w->window_end_bytepos >= 0);
18042 IF_DEBUG (debug_method_add (w, "B"));
18043 }
18044 else if (last_text_row)
18045 {
18046 /* We have displayed either to the end of the window or at the
18047 end of the window, i.e. the last row with text is to be found
18048 in the desired matrix. */
18049 adjust_window_ends (w, last_text_row, 0);
18050 eassert (w->window_end_bytepos >= 0);
18051 }
18052 else if (first_unchanged_at_end_row == NULL
18053 && last_text_row == NULL
18054 && last_text_row_at_end == NULL)
18055 {
18056 /* Displayed to end of window, but no line containing text was
18057 displayed. Lines were deleted at the end of the window. */
18058 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18059 int vpos = w->window_end_vpos;
18060 struct glyph_row *current_row = current_matrix->rows + vpos;
18061 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18062
18063 for (row = NULL;
18064 row == NULL && vpos >= first_vpos;
18065 --vpos, --current_row, --desired_row)
18066 {
18067 if (desired_row->enabled_p)
18068 {
18069 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18070 row = desired_row;
18071 }
18072 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18073 row = current_row;
18074 }
18075
18076 eassert (row != NULL);
18077 w->window_end_vpos = vpos + 1;
18078 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18079 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18080 eassert (w->window_end_bytepos >= 0);
18081 IF_DEBUG (debug_method_add (w, "C"));
18082 }
18083 else
18084 emacs_abort ();
18085
18086 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18087 debug_end_vpos = w->window_end_vpos));
18088
18089 /* Record that display has not been completed. */
18090 w->window_end_valid = 0;
18091 w->desired_matrix->no_scrolling_p = 1;
18092 return 3;
18093
18094 #undef GIVE_UP
18095 }
18096
18097
18098 \f
18099 /***********************************************************************
18100 More debugging support
18101 ***********************************************************************/
18102
18103 #ifdef GLYPH_DEBUG
18104
18105 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18106 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18107 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18108
18109
18110 /* Dump the contents of glyph matrix MATRIX on stderr.
18111
18112 GLYPHS 0 means don't show glyph contents.
18113 GLYPHS 1 means show glyphs in short form
18114 GLYPHS > 1 means show glyphs in long form. */
18115
18116 void
18117 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18118 {
18119 int i;
18120 for (i = 0; i < matrix->nrows; ++i)
18121 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18122 }
18123
18124
18125 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18126 the glyph row and area where the glyph comes from. */
18127
18128 void
18129 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18130 {
18131 if (glyph->type == CHAR_GLYPH
18132 || glyph->type == GLYPHLESS_GLYPH)
18133 {
18134 fprintf (stderr,
18135 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18136 glyph - row->glyphs[TEXT_AREA],
18137 (glyph->type == CHAR_GLYPH
18138 ? 'C'
18139 : 'G'),
18140 glyph->charpos,
18141 (BUFFERP (glyph->object)
18142 ? 'B'
18143 : (STRINGP (glyph->object)
18144 ? 'S'
18145 : (INTEGERP (glyph->object)
18146 ? '0'
18147 : '-'))),
18148 glyph->pixel_width,
18149 glyph->u.ch,
18150 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18151 ? glyph->u.ch
18152 : '.'),
18153 glyph->face_id,
18154 glyph->left_box_line_p,
18155 glyph->right_box_line_p);
18156 }
18157 else if (glyph->type == STRETCH_GLYPH)
18158 {
18159 fprintf (stderr,
18160 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18161 glyph - row->glyphs[TEXT_AREA],
18162 'S',
18163 glyph->charpos,
18164 (BUFFERP (glyph->object)
18165 ? 'B'
18166 : (STRINGP (glyph->object)
18167 ? 'S'
18168 : (INTEGERP (glyph->object)
18169 ? '0'
18170 : '-'))),
18171 glyph->pixel_width,
18172 0,
18173 ' ',
18174 glyph->face_id,
18175 glyph->left_box_line_p,
18176 glyph->right_box_line_p);
18177 }
18178 else if (glyph->type == IMAGE_GLYPH)
18179 {
18180 fprintf (stderr,
18181 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18182 glyph - row->glyphs[TEXT_AREA],
18183 'I',
18184 glyph->charpos,
18185 (BUFFERP (glyph->object)
18186 ? 'B'
18187 : (STRINGP (glyph->object)
18188 ? 'S'
18189 : (INTEGERP (glyph->object)
18190 ? '0'
18191 : '-'))),
18192 glyph->pixel_width,
18193 glyph->u.img_id,
18194 '.',
18195 glyph->face_id,
18196 glyph->left_box_line_p,
18197 glyph->right_box_line_p);
18198 }
18199 else if (glyph->type == COMPOSITE_GLYPH)
18200 {
18201 fprintf (stderr,
18202 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18203 glyph - row->glyphs[TEXT_AREA],
18204 '+',
18205 glyph->charpos,
18206 (BUFFERP (glyph->object)
18207 ? 'B'
18208 : (STRINGP (glyph->object)
18209 ? 'S'
18210 : (INTEGERP (glyph->object)
18211 ? '0'
18212 : '-'))),
18213 glyph->pixel_width,
18214 glyph->u.cmp.id);
18215 if (glyph->u.cmp.automatic)
18216 fprintf (stderr,
18217 "[%d-%d]",
18218 glyph->slice.cmp.from, glyph->slice.cmp.to);
18219 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18220 glyph->face_id,
18221 glyph->left_box_line_p,
18222 glyph->right_box_line_p);
18223 }
18224 }
18225
18226
18227 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18228 GLYPHS 0 means don't show glyph contents.
18229 GLYPHS 1 means show glyphs in short form
18230 GLYPHS > 1 means show glyphs in long form. */
18231
18232 void
18233 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18234 {
18235 if (glyphs != 1)
18236 {
18237 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18238 fprintf (stderr, "==============================================================================\n");
18239
18240 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18241 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18242 vpos,
18243 MATRIX_ROW_START_CHARPOS (row),
18244 MATRIX_ROW_END_CHARPOS (row),
18245 row->used[TEXT_AREA],
18246 row->contains_overlapping_glyphs_p,
18247 row->enabled_p,
18248 row->truncated_on_left_p,
18249 row->truncated_on_right_p,
18250 row->continued_p,
18251 MATRIX_ROW_CONTINUATION_LINE_P (row),
18252 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18253 row->ends_at_zv_p,
18254 row->fill_line_p,
18255 row->ends_in_middle_of_char_p,
18256 row->starts_in_middle_of_char_p,
18257 row->mouse_face_p,
18258 row->x,
18259 row->y,
18260 row->pixel_width,
18261 row->height,
18262 row->visible_height,
18263 row->ascent,
18264 row->phys_ascent);
18265 /* The next 3 lines should align to "Start" in the header. */
18266 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18267 row->end.overlay_string_index,
18268 row->continuation_lines_width);
18269 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18270 CHARPOS (row->start.string_pos),
18271 CHARPOS (row->end.string_pos));
18272 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18273 row->end.dpvec_index);
18274 }
18275
18276 if (glyphs > 1)
18277 {
18278 int area;
18279
18280 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18281 {
18282 struct glyph *glyph = row->glyphs[area];
18283 struct glyph *glyph_end = glyph + row->used[area];
18284
18285 /* Glyph for a line end in text. */
18286 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18287 ++glyph_end;
18288
18289 if (glyph < glyph_end)
18290 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18291
18292 for (; glyph < glyph_end; ++glyph)
18293 dump_glyph (row, glyph, area);
18294 }
18295 }
18296 else if (glyphs == 1)
18297 {
18298 int area;
18299
18300 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18301 {
18302 char *s = alloca (row->used[area] + 4);
18303 int i;
18304
18305 for (i = 0; i < row->used[area]; ++i)
18306 {
18307 struct glyph *glyph = row->glyphs[area] + i;
18308 if (i == row->used[area] - 1
18309 && area == TEXT_AREA
18310 && INTEGERP (glyph->object)
18311 && glyph->type == CHAR_GLYPH
18312 && glyph->u.ch == ' ')
18313 {
18314 strcpy (&s[i], "[\\n]");
18315 i += 4;
18316 }
18317 else if (glyph->type == CHAR_GLYPH
18318 && glyph->u.ch < 0x80
18319 && glyph->u.ch >= ' ')
18320 s[i] = glyph->u.ch;
18321 else
18322 s[i] = '.';
18323 }
18324
18325 s[i] = '\0';
18326 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18327 }
18328 }
18329 }
18330
18331
18332 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18333 Sdump_glyph_matrix, 0, 1, "p",
18334 doc: /* Dump the current matrix of the selected window to stderr.
18335 Shows contents of glyph row structures. With non-nil
18336 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18337 glyphs in short form, otherwise show glyphs in long form. */)
18338 (Lisp_Object glyphs)
18339 {
18340 struct window *w = XWINDOW (selected_window);
18341 struct buffer *buffer = XBUFFER (w->contents);
18342
18343 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18344 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18345 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18346 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18347 fprintf (stderr, "=============================================\n");
18348 dump_glyph_matrix (w->current_matrix,
18349 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18350 return Qnil;
18351 }
18352
18353
18354 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18355 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18356 (void)
18357 {
18358 struct frame *f = XFRAME (selected_frame);
18359 dump_glyph_matrix (f->current_matrix, 1);
18360 return Qnil;
18361 }
18362
18363
18364 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18365 doc: /* Dump glyph row ROW to stderr.
18366 GLYPH 0 means don't dump glyphs.
18367 GLYPH 1 means dump glyphs in short form.
18368 GLYPH > 1 or omitted means dump glyphs in long form. */)
18369 (Lisp_Object row, Lisp_Object glyphs)
18370 {
18371 struct glyph_matrix *matrix;
18372 EMACS_INT vpos;
18373
18374 CHECK_NUMBER (row);
18375 matrix = XWINDOW (selected_window)->current_matrix;
18376 vpos = XINT (row);
18377 if (vpos >= 0 && vpos < matrix->nrows)
18378 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18379 vpos,
18380 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18381 return Qnil;
18382 }
18383
18384
18385 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18386 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18387 GLYPH 0 means don't dump glyphs.
18388 GLYPH 1 means dump glyphs in short form.
18389 GLYPH > 1 or omitted means dump glyphs in long form.
18390
18391 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18392 do nothing. */)
18393 (Lisp_Object row, Lisp_Object glyphs)
18394 {
18395 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18396 struct frame *sf = SELECTED_FRAME ();
18397 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18398 EMACS_INT vpos;
18399
18400 CHECK_NUMBER (row);
18401 vpos = XINT (row);
18402 if (vpos >= 0 && vpos < m->nrows)
18403 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18404 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18405 #endif
18406 return Qnil;
18407 }
18408
18409
18410 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18411 doc: /* Toggle tracing of redisplay.
18412 With ARG, turn tracing on if and only if ARG is positive. */)
18413 (Lisp_Object arg)
18414 {
18415 if (NILP (arg))
18416 trace_redisplay_p = !trace_redisplay_p;
18417 else
18418 {
18419 arg = Fprefix_numeric_value (arg);
18420 trace_redisplay_p = XINT (arg) > 0;
18421 }
18422
18423 return Qnil;
18424 }
18425
18426
18427 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18428 doc: /* Like `format', but print result to stderr.
18429 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18430 (ptrdiff_t nargs, Lisp_Object *args)
18431 {
18432 Lisp_Object s = Fformat (nargs, args);
18433 fprintf (stderr, "%s", SDATA (s));
18434 return Qnil;
18435 }
18436
18437 #endif /* GLYPH_DEBUG */
18438
18439
18440 \f
18441 /***********************************************************************
18442 Building Desired Matrix Rows
18443 ***********************************************************************/
18444
18445 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18446 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18447
18448 static struct glyph_row *
18449 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18450 {
18451 struct frame *f = XFRAME (WINDOW_FRAME (w));
18452 struct buffer *buffer = XBUFFER (w->contents);
18453 struct buffer *old = current_buffer;
18454 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18455 int arrow_len = SCHARS (overlay_arrow_string);
18456 const unsigned char *arrow_end = arrow_string + arrow_len;
18457 const unsigned char *p;
18458 struct it it;
18459 bool multibyte_p;
18460 int n_glyphs_before;
18461
18462 set_buffer_temp (buffer);
18463 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18464 it.glyph_row->used[TEXT_AREA] = 0;
18465 SET_TEXT_POS (it.position, 0, 0);
18466
18467 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18468 p = arrow_string;
18469 while (p < arrow_end)
18470 {
18471 Lisp_Object face, ilisp;
18472
18473 /* Get the next character. */
18474 if (multibyte_p)
18475 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18476 else
18477 {
18478 it.c = it.char_to_display = *p, it.len = 1;
18479 if (! ASCII_CHAR_P (it.c))
18480 it.char_to_display = BYTE8_TO_CHAR (it.c);
18481 }
18482 p += it.len;
18483
18484 /* Get its face. */
18485 ilisp = make_number (p - arrow_string);
18486 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18487 it.face_id = compute_char_face (f, it.char_to_display, face);
18488
18489 /* Compute its width, get its glyphs. */
18490 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18491 SET_TEXT_POS (it.position, -1, -1);
18492 PRODUCE_GLYPHS (&it);
18493
18494 /* If this character doesn't fit any more in the line, we have
18495 to remove some glyphs. */
18496 if (it.current_x > it.last_visible_x)
18497 {
18498 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18499 break;
18500 }
18501 }
18502
18503 set_buffer_temp (old);
18504 return it.glyph_row;
18505 }
18506
18507
18508 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18509 glyphs to insert is determined by produce_special_glyphs. */
18510
18511 static void
18512 insert_left_trunc_glyphs (struct it *it)
18513 {
18514 struct it truncate_it;
18515 struct glyph *from, *end, *to, *toend;
18516
18517 eassert (!FRAME_WINDOW_P (it->f)
18518 || (!it->glyph_row->reversed_p
18519 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18520 || (it->glyph_row->reversed_p
18521 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18522
18523 /* Get the truncation glyphs. */
18524 truncate_it = *it;
18525 truncate_it.current_x = 0;
18526 truncate_it.face_id = DEFAULT_FACE_ID;
18527 truncate_it.glyph_row = &scratch_glyph_row;
18528 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18529 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18530 truncate_it.object = make_number (0);
18531 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18532
18533 /* Overwrite glyphs from IT with truncation glyphs. */
18534 if (!it->glyph_row->reversed_p)
18535 {
18536 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18537
18538 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18539 end = from + tused;
18540 to = it->glyph_row->glyphs[TEXT_AREA];
18541 toend = to + it->glyph_row->used[TEXT_AREA];
18542 if (FRAME_WINDOW_P (it->f))
18543 {
18544 /* On GUI frames, when variable-size fonts are displayed,
18545 the truncation glyphs may need more pixels than the row's
18546 glyphs they overwrite. We overwrite more glyphs to free
18547 enough screen real estate, and enlarge the stretch glyph
18548 on the right (see display_line), if there is one, to
18549 preserve the screen position of the truncation glyphs on
18550 the right. */
18551 int w = 0;
18552 struct glyph *g = to;
18553 short used;
18554
18555 /* The first glyph could be partially visible, in which case
18556 it->glyph_row->x will be negative. But we want the left
18557 truncation glyphs to be aligned at the left margin of the
18558 window, so we override the x coordinate at which the row
18559 will begin. */
18560 it->glyph_row->x = 0;
18561 while (g < toend && w < it->truncation_pixel_width)
18562 {
18563 w += g->pixel_width;
18564 ++g;
18565 }
18566 if (g - to - tused > 0)
18567 {
18568 memmove (to + tused, g, (toend - g) * sizeof(*g));
18569 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18570 }
18571 used = it->glyph_row->used[TEXT_AREA];
18572 if (it->glyph_row->truncated_on_right_p
18573 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18574 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18575 == STRETCH_GLYPH)
18576 {
18577 int extra = w - it->truncation_pixel_width;
18578
18579 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18580 }
18581 }
18582
18583 while (from < end)
18584 *to++ = *from++;
18585
18586 /* There may be padding glyphs left over. Overwrite them too. */
18587 if (!FRAME_WINDOW_P (it->f))
18588 {
18589 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18590 {
18591 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18592 while (from < end)
18593 *to++ = *from++;
18594 }
18595 }
18596
18597 if (to > toend)
18598 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18599 }
18600 else
18601 {
18602 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18603
18604 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18605 that back to front. */
18606 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18607 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18608 toend = it->glyph_row->glyphs[TEXT_AREA];
18609 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18610 if (FRAME_WINDOW_P (it->f))
18611 {
18612 int w = 0;
18613 struct glyph *g = to;
18614
18615 while (g >= toend && w < it->truncation_pixel_width)
18616 {
18617 w += g->pixel_width;
18618 --g;
18619 }
18620 if (to - g - tused > 0)
18621 to = g + tused;
18622 if (it->glyph_row->truncated_on_right_p
18623 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18624 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18625 {
18626 int extra = w - it->truncation_pixel_width;
18627
18628 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18629 }
18630 }
18631
18632 while (from >= end && to >= toend)
18633 *to-- = *from--;
18634 if (!FRAME_WINDOW_P (it->f))
18635 {
18636 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18637 {
18638 from =
18639 truncate_it.glyph_row->glyphs[TEXT_AREA]
18640 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18641 while (from >= end && to >= toend)
18642 *to-- = *from--;
18643 }
18644 }
18645 if (from >= end)
18646 {
18647 /* Need to free some room before prepending additional
18648 glyphs. */
18649 int move_by = from - end + 1;
18650 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18651 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18652
18653 for ( ; g >= g0; g--)
18654 g[move_by] = *g;
18655 while (from >= end)
18656 *to-- = *from--;
18657 it->glyph_row->used[TEXT_AREA] += move_by;
18658 }
18659 }
18660 }
18661
18662 /* Compute the hash code for ROW. */
18663 unsigned
18664 row_hash (struct glyph_row *row)
18665 {
18666 int area, k;
18667 unsigned hashval = 0;
18668
18669 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18670 for (k = 0; k < row->used[area]; ++k)
18671 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18672 + row->glyphs[area][k].u.val
18673 + row->glyphs[area][k].face_id
18674 + row->glyphs[area][k].padding_p
18675 + (row->glyphs[area][k].type << 2));
18676
18677 return hashval;
18678 }
18679
18680 /* Compute the pixel height and width of IT->glyph_row.
18681
18682 Most of the time, ascent and height of a display line will be equal
18683 to the max_ascent and max_height values of the display iterator
18684 structure. This is not the case if
18685
18686 1. We hit ZV without displaying anything. In this case, max_ascent
18687 and max_height will be zero.
18688
18689 2. We have some glyphs that don't contribute to the line height.
18690 (The glyph row flag contributes_to_line_height_p is for future
18691 pixmap extensions).
18692
18693 The first case is easily covered by using default values because in
18694 these cases, the line height does not really matter, except that it
18695 must not be zero. */
18696
18697 static void
18698 compute_line_metrics (struct it *it)
18699 {
18700 struct glyph_row *row = it->glyph_row;
18701
18702 if (FRAME_WINDOW_P (it->f))
18703 {
18704 int i, min_y, max_y;
18705
18706 /* The line may consist of one space only, that was added to
18707 place the cursor on it. If so, the row's height hasn't been
18708 computed yet. */
18709 if (row->height == 0)
18710 {
18711 if (it->max_ascent + it->max_descent == 0)
18712 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18713 row->ascent = it->max_ascent;
18714 row->height = it->max_ascent + it->max_descent;
18715 row->phys_ascent = it->max_phys_ascent;
18716 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18717 row->extra_line_spacing = it->max_extra_line_spacing;
18718 }
18719
18720 /* Compute the width of this line. */
18721 row->pixel_width = row->x;
18722 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18723 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18724
18725 eassert (row->pixel_width >= 0);
18726 eassert (row->ascent >= 0 && row->height > 0);
18727
18728 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18729 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18730
18731 /* If first line's physical ascent is larger than its logical
18732 ascent, use the physical ascent, and make the row taller.
18733 This makes accented characters fully visible. */
18734 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18735 && row->phys_ascent > row->ascent)
18736 {
18737 row->height += row->phys_ascent - row->ascent;
18738 row->ascent = row->phys_ascent;
18739 }
18740
18741 /* Compute how much of the line is visible. */
18742 row->visible_height = row->height;
18743
18744 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18745 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18746
18747 if (row->y < min_y)
18748 row->visible_height -= min_y - row->y;
18749 if (row->y + row->height > max_y)
18750 row->visible_height -= row->y + row->height - max_y;
18751 }
18752 else
18753 {
18754 row->pixel_width = row->used[TEXT_AREA];
18755 if (row->continued_p)
18756 row->pixel_width -= it->continuation_pixel_width;
18757 else if (row->truncated_on_right_p)
18758 row->pixel_width -= it->truncation_pixel_width;
18759 row->ascent = row->phys_ascent = 0;
18760 row->height = row->phys_height = row->visible_height = 1;
18761 row->extra_line_spacing = 0;
18762 }
18763
18764 /* Compute a hash code for this row. */
18765 row->hash = row_hash (row);
18766
18767 it->max_ascent = it->max_descent = 0;
18768 it->max_phys_ascent = it->max_phys_descent = 0;
18769 }
18770
18771
18772 /* Append one space to the glyph row of iterator IT if doing a
18773 window-based redisplay. The space has the same face as
18774 IT->face_id. Value is non-zero if a space was added.
18775
18776 This function is called to make sure that there is always one glyph
18777 at the end of a glyph row that the cursor can be set on under
18778 window-systems. (If there weren't such a glyph we would not know
18779 how wide and tall a box cursor should be displayed).
18780
18781 At the same time this space let's a nicely handle clearing to the
18782 end of the line if the row ends in italic text. */
18783
18784 static int
18785 append_space_for_newline (struct it *it, int default_face_p)
18786 {
18787 if (FRAME_WINDOW_P (it->f))
18788 {
18789 int n = it->glyph_row->used[TEXT_AREA];
18790
18791 if (it->glyph_row->glyphs[TEXT_AREA] + n
18792 < it->glyph_row->glyphs[1 + TEXT_AREA])
18793 {
18794 /* Save some values that must not be changed.
18795 Must save IT->c and IT->len because otherwise
18796 ITERATOR_AT_END_P wouldn't work anymore after
18797 append_space_for_newline has been called. */
18798 enum display_element_type saved_what = it->what;
18799 int saved_c = it->c, saved_len = it->len;
18800 int saved_char_to_display = it->char_to_display;
18801 int saved_x = it->current_x;
18802 int saved_face_id = it->face_id;
18803 int saved_box_end = it->end_of_box_run_p;
18804 struct text_pos saved_pos;
18805 Lisp_Object saved_object;
18806 struct face *face;
18807
18808 saved_object = it->object;
18809 saved_pos = it->position;
18810
18811 it->what = IT_CHARACTER;
18812 memset (&it->position, 0, sizeof it->position);
18813 it->object = make_number (0);
18814 it->c = it->char_to_display = ' ';
18815 it->len = 1;
18816
18817 /* If the default face was remapped, be sure to use the
18818 remapped face for the appended newline. */
18819 if (default_face_p)
18820 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18821 else if (it->face_before_selective_p)
18822 it->face_id = it->saved_face_id;
18823 face = FACE_FROM_ID (it->f, it->face_id);
18824 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18825 /* In R2L rows, we will prepend a stretch glyph that will
18826 have the end_of_box_run_p flag set for it, so there's no
18827 need for the appended newline glyph to have that flag
18828 set. */
18829 if (it->glyph_row->reversed_p
18830 /* But if the appended newline glyph goes all the way to
18831 the end of the row, there will be no stretch glyph,
18832 so leave the box flag set. */
18833 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18834 it->end_of_box_run_p = 0;
18835
18836 PRODUCE_GLYPHS (it);
18837
18838 it->override_ascent = -1;
18839 it->constrain_row_ascent_descent_p = 0;
18840 it->current_x = saved_x;
18841 it->object = saved_object;
18842 it->position = saved_pos;
18843 it->what = saved_what;
18844 it->face_id = saved_face_id;
18845 it->len = saved_len;
18846 it->c = saved_c;
18847 it->char_to_display = saved_char_to_display;
18848 it->end_of_box_run_p = saved_box_end;
18849 return 1;
18850 }
18851 }
18852
18853 return 0;
18854 }
18855
18856
18857 /* Extend the face of the last glyph in the text area of IT->glyph_row
18858 to the end of the display line. Called from display_line. If the
18859 glyph row is empty, add a space glyph to it so that we know the
18860 face to draw. Set the glyph row flag fill_line_p. If the glyph
18861 row is R2L, prepend a stretch glyph to cover the empty space to the
18862 left of the leftmost glyph. */
18863
18864 static void
18865 extend_face_to_end_of_line (struct it *it)
18866 {
18867 struct face *face, *default_face;
18868 struct frame *f = it->f;
18869
18870 /* If line is already filled, do nothing. Non window-system frames
18871 get a grace of one more ``pixel'' because their characters are
18872 1-``pixel'' wide, so they hit the equality too early. This grace
18873 is needed only for R2L rows that are not continued, to produce
18874 one extra blank where we could display the cursor. */
18875 if ((it->current_x >= it->last_visible_x
18876 + (!FRAME_WINDOW_P (f)
18877 && it->glyph_row->reversed_p
18878 && !it->glyph_row->continued_p))
18879 /* If the window has display margins, we will need to extend
18880 their face even if the text area is filled. */
18881 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18882 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18883 return;
18884
18885 /* The default face, possibly remapped. */
18886 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18887
18888 /* Face extension extends the background and box of IT->face_id
18889 to the end of the line. If the background equals the background
18890 of the frame, we don't have to do anything. */
18891 if (it->face_before_selective_p)
18892 face = FACE_FROM_ID (f, it->saved_face_id);
18893 else
18894 face = FACE_FROM_ID (f, it->face_id);
18895
18896 if (FRAME_WINDOW_P (f)
18897 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18898 && face->box == FACE_NO_BOX
18899 && face->background == FRAME_BACKGROUND_PIXEL (f)
18900 #ifdef HAVE_WINDOW_SYSTEM
18901 && !face->stipple
18902 #endif
18903 && !it->glyph_row->reversed_p)
18904 return;
18905
18906 /* Set the glyph row flag indicating that the face of the last glyph
18907 in the text area has to be drawn to the end of the text area. */
18908 it->glyph_row->fill_line_p = 1;
18909
18910 /* If current character of IT is not ASCII, make sure we have the
18911 ASCII face. This will be automatically undone the next time
18912 get_next_display_element returns a multibyte character. Note
18913 that the character will always be single byte in unibyte
18914 text. */
18915 if (!ASCII_CHAR_P (it->c))
18916 {
18917 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18918 }
18919
18920 if (FRAME_WINDOW_P (f))
18921 {
18922 /* If the row is empty, add a space with the current face of IT,
18923 so that we know which face to draw. */
18924 if (it->glyph_row->used[TEXT_AREA] == 0)
18925 {
18926 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18927 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18928 it->glyph_row->used[TEXT_AREA] = 1;
18929 }
18930 /* Mode line and the header line don't have margins, and
18931 likewise the frame's tool-bar window, if there is any. */
18932 if (!(it->glyph_row->mode_line_p
18933 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18934 || (WINDOWP (f->tool_bar_window)
18935 && it->w == XWINDOW (f->tool_bar_window))
18936 #endif
18937 ))
18938 {
18939 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18940 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18941 {
18942 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18943 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18944 default_face->id;
18945 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18946 }
18947 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18948 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18949 {
18950 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18951 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18952 default_face->id;
18953 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18954 }
18955 }
18956 #ifdef HAVE_WINDOW_SYSTEM
18957 if (it->glyph_row->reversed_p)
18958 {
18959 /* Prepend a stretch glyph to the row, such that the
18960 rightmost glyph will be drawn flushed all the way to the
18961 right margin of the window. The stretch glyph that will
18962 occupy the empty space, if any, to the left of the
18963 glyphs. */
18964 struct font *font = face->font ? face->font : FRAME_FONT (f);
18965 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18966 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18967 struct glyph *g;
18968 int row_width, stretch_ascent, stretch_width;
18969 struct text_pos saved_pos;
18970 int saved_face_id, saved_avoid_cursor, saved_box_start;
18971
18972 for (row_width = 0, g = row_start; g < row_end; g++)
18973 row_width += g->pixel_width;
18974 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18975 if (stretch_width > 0)
18976 {
18977 stretch_ascent =
18978 (((it->ascent + it->descent)
18979 * FONT_BASE (font)) / FONT_HEIGHT (font));
18980 saved_pos = it->position;
18981 memset (&it->position, 0, sizeof it->position);
18982 saved_avoid_cursor = it->avoid_cursor_p;
18983 it->avoid_cursor_p = 1;
18984 saved_face_id = it->face_id;
18985 saved_box_start = it->start_of_box_run_p;
18986 /* The last row's stretch glyph should get the default
18987 face, to avoid painting the rest of the window with
18988 the region face, if the region ends at ZV. */
18989 if (it->glyph_row->ends_at_zv_p)
18990 it->face_id = default_face->id;
18991 else
18992 it->face_id = face->id;
18993 it->start_of_box_run_p = 0;
18994 append_stretch_glyph (it, make_number (0), stretch_width,
18995 it->ascent + it->descent, stretch_ascent);
18996 it->position = saved_pos;
18997 it->avoid_cursor_p = saved_avoid_cursor;
18998 it->face_id = saved_face_id;
18999 it->start_of_box_run_p = saved_box_start;
19000 }
19001 }
19002 #endif /* HAVE_WINDOW_SYSTEM */
19003 }
19004 else
19005 {
19006 /* Save some values that must not be changed. */
19007 int saved_x = it->current_x;
19008 struct text_pos saved_pos;
19009 Lisp_Object saved_object;
19010 enum display_element_type saved_what = it->what;
19011 int saved_face_id = it->face_id;
19012
19013 saved_object = it->object;
19014 saved_pos = it->position;
19015
19016 it->what = IT_CHARACTER;
19017 memset (&it->position, 0, sizeof it->position);
19018 it->object = make_number (0);
19019 it->c = it->char_to_display = ' ';
19020 it->len = 1;
19021
19022 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19023 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19024 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19025 && !it->glyph_row->mode_line_p
19026 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19027 {
19028 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19029 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19030
19031 for (it->current_x = 0; g < e; g++)
19032 it->current_x += g->pixel_width;
19033
19034 it->area = LEFT_MARGIN_AREA;
19035 it->face_id = default_face->id;
19036 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19037 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19038 {
19039 PRODUCE_GLYPHS (it);
19040 /* term.c:produce_glyphs advances it->current_x only for
19041 TEXT_AREA. */
19042 it->current_x += it->pixel_width;
19043 }
19044
19045 it->current_x = saved_x;
19046 it->area = TEXT_AREA;
19047 }
19048
19049 /* The last row's blank glyphs should get the default face, to
19050 avoid painting the rest of the window with the region face,
19051 if the region ends at ZV. */
19052 if (it->glyph_row->ends_at_zv_p)
19053 it->face_id = default_face->id;
19054 else
19055 it->face_id = face->id;
19056 PRODUCE_GLYPHS (it);
19057
19058 while (it->current_x <= it->last_visible_x)
19059 PRODUCE_GLYPHS (it);
19060
19061 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19062 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19063 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19064 && !it->glyph_row->mode_line_p
19065 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19066 {
19067 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19068 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19069
19070 for ( ; g < e; g++)
19071 it->current_x += g->pixel_width;
19072
19073 it->area = RIGHT_MARGIN_AREA;
19074 it->face_id = default_face->id;
19075 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19076 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19077 {
19078 PRODUCE_GLYPHS (it);
19079 it->current_x += it->pixel_width;
19080 }
19081
19082 it->area = TEXT_AREA;
19083 }
19084
19085 /* Don't count these blanks really. It would let us insert a left
19086 truncation glyph below and make us set the cursor on them, maybe. */
19087 it->current_x = saved_x;
19088 it->object = saved_object;
19089 it->position = saved_pos;
19090 it->what = saved_what;
19091 it->face_id = saved_face_id;
19092 }
19093 }
19094
19095
19096 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19097 trailing whitespace. */
19098
19099 static int
19100 trailing_whitespace_p (ptrdiff_t charpos)
19101 {
19102 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19103 int c = 0;
19104
19105 while (bytepos < ZV_BYTE
19106 && (c = FETCH_CHAR (bytepos),
19107 c == ' ' || c == '\t'))
19108 ++bytepos;
19109
19110 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19111 {
19112 if (bytepos != PT_BYTE)
19113 return 1;
19114 }
19115 return 0;
19116 }
19117
19118
19119 /* Highlight trailing whitespace, if any, in ROW. */
19120
19121 static void
19122 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19123 {
19124 int used = row->used[TEXT_AREA];
19125
19126 if (used)
19127 {
19128 struct glyph *start = row->glyphs[TEXT_AREA];
19129 struct glyph *glyph = start + used - 1;
19130
19131 if (row->reversed_p)
19132 {
19133 /* Right-to-left rows need to be processed in the opposite
19134 direction, so swap the edge pointers. */
19135 glyph = start;
19136 start = row->glyphs[TEXT_AREA] + used - 1;
19137 }
19138
19139 /* Skip over glyphs inserted to display the cursor at the
19140 end of a line, for extending the face of the last glyph
19141 to the end of the line on terminals, and for truncation
19142 and continuation glyphs. */
19143 if (!row->reversed_p)
19144 {
19145 while (glyph >= start
19146 && glyph->type == CHAR_GLYPH
19147 && INTEGERP (glyph->object))
19148 --glyph;
19149 }
19150 else
19151 {
19152 while (glyph <= start
19153 && glyph->type == CHAR_GLYPH
19154 && INTEGERP (glyph->object))
19155 ++glyph;
19156 }
19157
19158 /* If last glyph is a space or stretch, and it's trailing
19159 whitespace, set the face of all trailing whitespace glyphs in
19160 IT->glyph_row to `trailing-whitespace'. */
19161 if ((row->reversed_p ? glyph <= start : glyph >= start)
19162 && BUFFERP (glyph->object)
19163 && (glyph->type == STRETCH_GLYPH
19164 || (glyph->type == CHAR_GLYPH
19165 && glyph->u.ch == ' '))
19166 && trailing_whitespace_p (glyph->charpos))
19167 {
19168 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19169 if (face_id < 0)
19170 return;
19171
19172 if (!row->reversed_p)
19173 {
19174 while (glyph >= start
19175 && BUFFERP (glyph->object)
19176 && (glyph->type == STRETCH_GLYPH
19177 || (glyph->type == CHAR_GLYPH
19178 && glyph->u.ch == ' ')))
19179 (glyph--)->face_id = face_id;
19180 }
19181 else
19182 {
19183 while (glyph <= start
19184 && BUFFERP (glyph->object)
19185 && (glyph->type == STRETCH_GLYPH
19186 || (glyph->type == CHAR_GLYPH
19187 && glyph->u.ch == ' ')))
19188 (glyph++)->face_id = face_id;
19189 }
19190 }
19191 }
19192 }
19193
19194
19195 /* Value is non-zero if glyph row ROW should be
19196 considered to hold the buffer position CHARPOS. */
19197
19198 static int
19199 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19200 {
19201 int result = 1;
19202
19203 if (charpos == CHARPOS (row->end.pos)
19204 || charpos == MATRIX_ROW_END_CHARPOS (row))
19205 {
19206 /* Suppose the row ends on a string.
19207 Unless the row is continued, that means it ends on a newline
19208 in the string. If it's anything other than a display string
19209 (e.g., a before-string from an overlay), we don't want the
19210 cursor there. (This heuristic seems to give the optimal
19211 behavior for the various types of multi-line strings.)
19212 One exception: if the string has `cursor' property on one of
19213 its characters, we _do_ want the cursor there. */
19214 if (CHARPOS (row->end.string_pos) >= 0)
19215 {
19216 if (row->continued_p)
19217 result = 1;
19218 else
19219 {
19220 /* Check for `display' property. */
19221 struct glyph *beg = row->glyphs[TEXT_AREA];
19222 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19223 struct glyph *glyph;
19224
19225 result = 0;
19226 for (glyph = end; glyph >= beg; --glyph)
19227 if (STRINGP (glyph->object))
19228 {
19229 Lisp_Object prop
19230 = Fget_char_property (make_number (charpos),
19231 Qdisplay, Qnil);
19232 result =
19233 (!NILP (prop)
19234 && display_prop_string_p (prop, glyph->object));
19235 /* If there's a `cursor' property on one of the
19236 string's characters, this row is a cursor row,
19237 even though this is not a display string. */
19238 if (!result)
19239 {
19240 Lisp_Object s = glyph->object;
19241
19242 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19243 {
19244 ptrdiff_t gpos = glyph->charpos;
19245
19246 if (!NILP (Fget_char_property (make_number (gpos),
19247 Qcursor, s)))
19248 {
19249 result = 1;
19250 break;
19251 }
19252 }
19253 }
19254 break;
19255 }
19256 }
19257 }
19258 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19259 {
19260 /* If the row ends in middle of a real character,
19261 and the line is continued, we want the cursor here.
19262 That's because CHARPOS (ROW->end.pos) would equal
19263 PT if PT is before the character. */
19264 if (!row->ends_in_ellipsis_p)
19265 result = row->continued_p;
19266 else
19267 /* If the row ends in an ellipsis, then
19268 CHARPOS (ROW->end.pos) will equal point after the
19269 invisible text. We want that position to be displayed
19270 after the ellipsis. */
19271 result = 0;
19272 }
19273 /* If the row ends at ZV, display the cursor at the end of that
19274 row instead of at the start of the row below. */
19275 else if (row->ends_at_zv_p)
19276 result = 1;
19277 else
19278 result = 0;
19279 }
19280
19281 return result;
19282 }
19283
19284 /* Value is non-zero if glyph row ROW should be
19285 used to hold the cursor. */
19286
19287 static int
19288 cursor_row_p (struct glyph_row *row)
19289 {
19290 return row_for_charpos_p (row, PT);
19291 }
19292
19293 \f
19294
19295 /* Push the property PROP so that it will be rendered at the current
19296 position in IT. Return 1 if PROP was successfully pushed, 0
19297 otherwise. Called from handle_line_prefix to handle the
19298 `line-prefix' and `wrap-prefix' properties. */
19299
19300 static int
19301 push_prefix_prop (struct it *it, Lisp_Object prop)
19302 {
19303 struct text_pos pos =
19304 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19305
19306 eassert (it->method == GET_FROM_BUFFER
19307 || it->method == GET_FROM_DISPLAY_VECTOR
19308 || it->method == GET_FROM_STRING);
19309
19310 /* We need to save the current buffer/string position, so it will be
19311 restored by pop_it, because iterate_out_of_display_property
19312 depends on that being set correctly, but some situations leave
19313 it->position not yet set when this function is called. */
19314 push_it (it, &pos);
19315
19316 if (STRINGP (prop))
19317 {
19318 if (SCHARS (prop) == 0)
19319 {
19320 pop_it (it);
19321 return 0;
19322 }
19323
19324 it->string = prop;
19325 it->string_from_prefix_prop_p = 1;
19326 it->multibyte_p = STRING_MULTIBYTE (it->string);
19327 it->current.overlay_string_index = -1;
19328 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19329 it->end_charpos = it->string_nchars = SCHARS (it->string);
19330 it->method = GET_FROM_STRING;
19331 it->stop_charpos = 0;
19332 it->prev_stop = 0;
19333 it->base_level_stop = 0;
19334
19335 /* Force paragraph direction to be that of the parent
19336 buffer/string. */
19337 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19338 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19339 else
19340 it->paragraph_embedding = L2R;
19341
19342 /* Set up the bidi iterator for this display string. */
19343 if (it->bidi_p)
19344 {
19345 it->bidi_it.string.lstring = it->string;
19346 it->bidi_it.string.s = NULL;
19347 it->bidi_it.string.schars = it->end_charpos;
19348 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19349 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19350 it->bidi_it.string.unibyte = !it->multibyte_p;
19351 it->bidi_it.w = it->w;
19352 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19353 }
19354 }
19355 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19356 {
19357 it->method = GET_FROM_STRETCH;
19358 it->object = prop;
19359 }
19360 #ifdef HAVE_WINDOW_SYSTEM
19361 else if (IMAGEP (prop))
19362 {
19363 it->what = IT_IMAGE;
19364 it->image_id = lookup_image (it->f, prop);
19365 it->method = GET_FROM_IMAGE;
19366 }
19367 #endif /* HAVE_WINDOW_SYSTEM */
19368 else
19369 {
19370 pop_it (it); /* bogus display property, give up */
19371 return 0;
19372 }
19373
19374 return 1;
19375 }
19376
19377 /* Return the character-property PROP at the current position in IT. */
19378
19379 static Lisp_Object
19380 get_it_property (struct it *it, Lisp_Object prop)
19381 {
19382 Lisp_Object position, object = it->object;
19383
19384 if (STRINGP (object))
19385 position = make_number (IT_STRING_CHARPOS (*it));
19386 else if (BUFFERP (object))
19387 {
19388 position = make_number (IT_CHARPOS (*it));
19389 object = it->window;
19390 }
19391 else
19392 return Qnil;
19393
19394 return Fget_char_property (position, prop, object);
19395 }
19396
19397 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19398
19399 static void
19400 handle_line_prefix (struct it *it)
19401 {
19402 Lisp_Object prefix;
19403
19404 if (it->continuation_lines_width > 0)
19405 {
19406 prefix = get_it_property (it, Qwrap_prefix);
19407 if (NILP (prefix))
19408 prefix = Vwrap_prefix;
19409 }
19410 else
19411 {
19412 prefix = get_it_property (it, Qline_prefix);
19413 if (NILP (prefix))
19414 prefix = Vline_prefix;
19415 }
19416 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19417 {
19418 /* If the prefix is wider than the window, and we try to wrap
19419 it, it would acquire its own wrap prefix, and so on till the
19420 iterator stack overflows. So, don't wrap the prefix. */
19421 it->line_wrap = TRUNCATE;
19422 it->avoid_cursor_p = 1;
19423 }
19424 }
19425
19426 \f
19427
19428 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19429 only for R2L lines from display_line and display_string, when they
19430 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19431 the line/string needs to be continued on the next glyph row. */
19432 static void
19433 unproduce_glyphs (struct it *it, int n)
19434 {
19435 struct glyph *glyph, *end;
19436
19437 eassert (it->glyph_row);
19438 eassert (it->glyph_row->reversed_p);
19439 eassert (it->area == TEXT_AREA);
19440 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19441
19442 if (n > it->glyph_row->used[TEXT_AREA])
19443 n = it->glyph_row->used[TEXT_AREA];
19444 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19445 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19446 for ( ; glyph < end; glyph++)
19447 glyph[-n] = *glyph;
19448 }
19449
19450 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19451 and ROW->maxpos. */
19452 static void
19453 find_row_edges (struct it *it, struct glyph_row *row,
19454 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19455 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19456 {
19457 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19458 lines' rows is implemented for bidi-reordered rows. */
19459
19460 /* ROW->minpos is the value of min_pos, the minimal buffer position
19461 we have in ROW, or ROW->start.pos if that is smaller. */
19462 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19463 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19464 else
19465 /* We didn't find buffer positions smaller than ROW->start, or
19466 didn't find _any_ valid buffer positions in any of the glyphs,
19467 so we must trust the iterator's computed positions. */
19468 row->minpos = row->start.pos;
19469 if (max_pos <= 0)
19470 {
19471 max_pos = CHARPOS (it->current.pos);
19472 max_bpos = BYTEPOS (it->current.pos);
19473 }
19474
19475 /* Here are the various use-cases for ending the row, and the
19476 corresponding values for ROW->maxpos:
19477
19478 Line ends in a newline from buffer eol_pos + 1
19479 Line is continued from buffer max_pos + 1
19480 Line is truncated on right it->current.pos
19481 Line ends in a newline from string max_pos + 1(*)
19482 (*) + 1 only when line ends in a forward scan
19483 Line is continued from string max_pos
19484 Line is continued from display vector max_pos
19485 Line is entirely from a string min_pos == max_pos
19486 Line is entirely from a display vector min_pos == max_pos
19487 Line that ends at ZV ZV
19488
19489 If you discover other use-cases, please add them here as
19490 appropriate. */
19491 if (row->ends_at_zv_p)
19492 row->maxpos = it->current.pos;
19493 else if (row->used[TEXT_AREA])
19494 {
19495 int seen_this_string = 0;
19496 struct glyph_row *r1 = row - 1;
19497
19498 /* Did we see the same display string on the previous row? */
19499 if (STRINGP (it->object)
19500 /* this is not the first row */
19501 && row > it->w->desired_matrix->rows
19502 /* previous row is not the header line */
19503 && !r1->mode_line_p
19504 /* previous row also ends in a newline from a string */
19505 && r1->ends_in_newline_from_string_p)
19506 {
19507 struct glyph *start, *end;
19508
19509 /* Search for the last glyph of the previous row that came
19510 from buffer or string. Depending on whether the row is
19511 L2R or R2L, we need to process it front to back or the
19512 other way round. */
19513 if (!r1->reversed_p)
19514 {
19515 start = r1->glyphs[TEXT_AREA];
19516 end = start + r1->used[TEXT_AREA];
19517 /* Glyphs inserted by redisplay have an integer (zero)
19518 as their object. */
19519 while (end > start
19520 && INTEGERP ((end - 1)->object)
19521 && (end - 1)->charpos <= 0)
19522 --end;
19523 if (end > start)
19524 {
19525 if (EQ ((end - 1)->object, it->object))
19526 seen_this_string = 1;
19527 }
19528 else
19529 /* If all the glyphs of the previous row were inserted
19530 by redisplay, it means the previous row was
19531 produced from a single newline, which is only
19532 possible if that newline came from the same string
19533 as the one which produced this ROW. */
19534 seen_this_string = 1;
19535 }
19536 else
19537 {
19538 end = r1->glyphs[TEXT_AREA] - 1;
19539 start = end + r1->used[TEXT_AREA];
19540 while (end < start
19541 && INTEGERP ((end + 1)->object)
19542 && (end + 1)->charpos <= 0)
19543 ++end;
19544 if (end < start)
19545 {
19546 if (EQ ((end + 1)->object, it->object))
19547 seen_this_string = 1;
19548 }
19549 else
19550 seen_this_string = 1;
19551 }
19552 }
19553 /* Take note of each display string that covers a newline only
19554 once, the first time we see it. This is for when a display
19555 string includes more than one newline in it. */
19556 if (row->ends_in_newline_from_string_p && !seen_this_string)
19557 {
19558 /* If we were scanning the buffer forward when we displayed
19559 the string, we want to account for at least one buffer
19560 position that belongs to this row (position covered by
19561 the display string), so that cursor positioning will
19562 consider this row as a candidate when point is at the end
19563 of the visual line represented by this row. This is not
19564 required when scanning back, because max_pos will already
19565 have a much larger value. */
19566 if (CHARPOS (row->end.pos) > max_pos)
19567 INC_BOTH (max_pos, max_bpos);
19568 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19569 }
19570 else if (CHARPOS (it->eol_pos) > 0)
19571 SET_TEXT_POS (row->maxpos,
19572 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19573 else if (row->continued_p)
19574 {
19575 /* If max_pos is different from IT's current position, it
19576 means IT->method does not belong to the display element
19577 at max_pos. However, it also means that the display
19578 element at max_pos was displayed in its entirety on this
19579 line, which is equivalent to saying that the next line
19580 starts at the next buffer position. */
19581 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19582 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19583 else
19584 {
19585 INC_BOTH (max_pos, max_bpos);
19586 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19587 }
19588 }
19589 else if (row->truncated_on_right_p)
19590 /* display_line already called reseat_at_next_visible_line_start,
19591 which puts the iterator at the beginning of the next line, in
19592 the logical order. */
19593 row->maxpos = it->current.pos;
19594 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19595 /* A line that is entirely from a string/image/stretch... */
19596 row->maxpos = row->minpos;
19597 else
19598 emacs_abort ();
19599 }
19600 else
19601 row->maxpos = it->current.pos;
19602 }
19603
19604 /* Construct the glyph row IT->glyph_row in the desired matrix of
19605 IT->w from text at the current position of IT. See dispextern.h
19606 for an overview of struct it. Value is non-zero if
19607 IT->glyph_row displays text, as opposed to a line displaying ZV
19608 only. */
19609
19610 static int
19611 display_line (struct it *it)
19612 {
19613 struct glyph_row *row = it->glyph_row;
19614 Lisp_Object overlay_arrow_string;
19615 struct it wrap_it;
19616 void *wrap_data = NULL;
19617 int may_wrap = 0, wrap_x IF_LINT (= 0);
19618 int wrap_row_used = -1;
19619 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19620 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19621 int wrap_row_extra_line_spacing IF_LINT (= 0);
19622 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19623 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19624 int cvpos;
19625 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19626 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19627
19628 /* We always start displaying at hpos zero even if hscrolled. */
19629 eassert (it->hpos == 0 && it->current_x == 0);
19630
19631 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19632 >= it->w->desired_matrix->nrows)
19633 {
19634 it->w->nrows_scale_factor++;
19635 it->f->fonts_changed = 1;
19636 return 0;
19637 }
19638
19639 /* Clear the result glyph row and enable it. */
19640 prepare_desired_row (row);
19641
19642 row->y = it->current_y;
19643 row->start = it->start;
19644 row->continuation_lines_width = it->continuation_lines_width;
19645 row->displays_text_p = 1;
19646 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19647 it->starts_in_middle_of_char_p = 0;
19648
19649 /* Arrange the overlays nicely for our purposes. Usually, we call
19650 display_line on only one line at a time, in which case this
19651 can't really hurt too much, or we call it on lines which appear
19652 one after another in the buffer, in which case all calls to
19653 recenter_overlay_lists but the first will be pretty cheap. */
19654 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19655
19656 /* Move over display elements that are not visible because we are
19657 hscrolled. This may stop at an x-position < IT->first_visible_x
19658 if the first glyph is partially visible or if we hit a line end. */
19659 if (it->current_x < it->first_visible_x)
19660 {
19661 enum move_it_result move_result;
19662
19663 this_line_min_pos = row->start.pos;
19664 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19665 MOVE_TO_POS | MOVE_TO_X);
19666 /* If we are under a large hscroll, move_it_in_display_line_to
19667 could hit the end of the line without reaching
19668 it->first_visible_x. Pretend that we did reach it. This is
19669 especially important on a TTY, where we will call
19670 extend_face_to_end_of_line, which needs to know how many
19671 blank glyphs to produce. */
19672 if (it->current_x < it->first_visible_x
19673 && (move_result == MOVE_NEWLINE_OR_CR
19674 || move_result == MOVE_POS_MATCH_OR_ZV))
19675 it->current_x = it->first_visible_x;
19676
19677 /* Record the smallest positions seen while we moved over
19678 display elements that are not visible. This is needed by
19679 redisplay_internal for optimizing the case where the cursor
19680 stays inside the same line. The rest of this function only
19681 considers positions that are actually displayed, so
19682 RECORD_MAX_MIN_POS will not otherwise record positions that
19683 are hscrolled to the left of the left edge of the window. */
19684 min_pos = CHARPOS (this_line_min_pos);
19685 min_bpos = BYTEPOS (this_line_min_pos);
19686 }
19687 else
19688 {
19689 /* We only do this when not calling `move_it_in_display_line_to'
19690 above, because move_it_in_display_line_to calls
19691 handle_line_prefix itself. */
19692 handle_line_prefix (it);
19693 }
19694
19695 /* Get the initial row height. This is either the height of the
19696 text hscrolled, if there is any, or zero. */
19697 row->ascent = it->max_ascent;
19698 row->height = it->max_ascent + it->max_descent;
19699 row->phys_ascent = it->max_phys_ascent;
19700 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19701 row->extra_line_spacing = it->max_extra_line_spacing;
19702
19703 /* Utility macro to record max and min buffer positions seen until now. */
19704 #define RECORD_MAX_MIN_POS(IT) \
19705 do \
19706 { \
19707 int composition_p = !STRINGP ((IT)->string) \
19708 && ((IT)->what == IT_COMPOSITION); \
19709 ptrdiff_t current_pos = \
19710 composition_p ? (IT)->cmp_it.charpos \
19711 : IT_CHARPOS (*(IT)); \
19712 ptrdiff_t current_bpos = \
19713 composition_p ? CHAR_TO_BYTE (current_pos) \
19714 : IT_BYTEPOS (*(IT)); \
19715 if (current_pos < min_pos) \
19716 { \
19717 min_pos = current_pos; \
19718 min_bpos = current_bpos; \
19719 } \
19720 if (IT_CHARPOS (*it) > max_pos) \
19721 { \
19722 max_pos = IT_CHARPOS (*it); \
19723 max_bpos = IT_BYTEPOS (*it); \
19724 } \
19725 } \
19726 while (0)
19727
19728 /* Loop generating characters. The loop is left with IT on the next
19729 character to display. */
19730 while (1)
19731 {
19732 int n_glyphs_before, hpos_before, x_before;
19733 int x, nglyphs;
19734 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19735
19736 /* Retrieve the next thing to display. Value is zero if end of
19737 buffer reached. */
19738 if (!get_next_display_element (it))
19739 {
19740 /* Maybe add a space at the end of this line that is used to
19741 display the cursor there under X. Set the charpos of the
19742 first glyph of blank lines not corresponding to any text
19743 to -1. */
19744 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19745 row->exact_window_width_line_p = 1;
19746 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19747 || row->used[TEXT_AREA] == 0)
19748 {
19749 row->glyphs[TEXT_AREA]->charpos = -1;
19750 row->displays_text_p = 0;
19751
19752 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19753 && (!MINI_WINDOW_P (it->w)
19754 || (minibuf_level && EQ (it->window, minibuf_window))))
19755 row->indicate_empty_line_p = 1;
19756 }
19757
19758 it->continuation_lines_width = 0;
19759 row->ends_at_zv_p = 1;
19760 /* A row that displays right-to-left text must always have
19761 its last face extended all the way to the end of line,
19762 even if this row ends in ZV, because we still write to
19763 the screen left to right. We also need to extend the
19764 last face if the default face is remapped to some
19765 different face, otherwise the functions that clear
19766 portions of the screen will clear with the default face's
19767 background color. */
19768 if (row->reversed_p
19769 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19770 extend_face_to_end_of_line (it);
19771 break;
19772 }
19773
19774 /* Now, get the metrics of what we want to display. This also
19775 generates glyphs in `row' (which is IT->glyph_row). */
19776 n_glyphs_before = row->used[TEXT_AREA];
19777 x = it->current_x;
19778
19779 /* Remember the line height so far in case the next element doesn't
19780 fit on the line. */
19781 if (it->line_wrap != TRUNCATE)
19782 {
19783 ascent = it->max_ascent;
19784 descent = it->max_descent;
19785 phys_ascent = it->max_phys_ascent;
19786 phys_descent = it->max_phys_descent;
19787
19788 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19789 {
19790 if (IT_DISPLAYING_WHITESPACE (it))
19791 may_wrap = 1;
19792 else if (may_wrap)
19793 {
19794 SAVE_IT (wrap_it, *it, wrap_data);
19795 wrap_x = x;
19796 wrap_row_used = row->used[TEXT_AREA];
19797 wrap_row_ascent = row->ascent;
19798 wrap_row_height = row->height;
19799 wrap_row_phys_ascent = row->phys_ascent;
19800 wrap_row_phys_height = row->phys_height;
19801 wrap_row_extra_line_spacing = row->extra_line_spacing;
19802 wrap_row_min_pos = min_pos;
19803 wrap_row_min_bpos = min_bpos;
19804 wrap_row_max_pos = max_pos;
19805 wrap_row_max_bpos = max_bpos;
19806 may_wrap = 0;
19807 }
19808 }
19809 }
19810
19811 PRODUCE_GLYPHS (it);
19812
19813 /* If this display element was in marginal areas, continue with
19814 the next one. */
19815 if (it->area != TEXT_AREA)
19816 {
19817 row->ascent = max (row->ascent, it->max_ascent);
19818 row->height = max (row->height, it->max_ascent + it->max_descent);
19819 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19820 row->phys_height = max (row->phys_height,
19821 it->max_phys_ascent + it->max_phys_descent);
19822 row->extra_line_spacing = max (row->extra_line_spacing,
19823 it->max_extra_line_spacing);
19824 set_iterator_to_next (it, 1);
19825 continue;
19826 }
19827
19828 /* Does the display element fit on the line? If we truncate
19829 lines, we should draw past the right edge of the window. If
19830 we don't truncate, we want to stop so that we can display the
19831 continuation glyph before the right margin. If lines are
19832 continued, there are two possible strategies for characters
19833 resulting in more than 1 glyph (e.g. tabs): Display as many
19834 glyphs as possible in this line and leave the rest for the
19835 continuation line, or display the whole element in the next
19836 line. Original redisplay did the former, so we do it also. */
19837 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19838 hpos_before = it->hpos;
19839 x_before = x;
19840
19841 if (/* Not a newline. */
19842 nglyphs > 0
19843 /* Glyphs produced fit entirely in the line. */
19844 && it->current_x < it->last_visible_x)
19845 {
19846 it->hpos += nglyphs;
19847 row->ascent = max (row->ascent, it->max_ascent);
19848 row->height = max (row->height, it->max_ascent + it->max_descent);
19849 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19850 row->phys_height = max (row->phys_height,
19851 it->max_phys_ascent + it->max_phys_descent);
19852 row->extra_line_spacing = max (row->extra_line_spacing,
19853 it->max_extra_line_spacing);
19854 if (it->current_x - it->pixel_width < it->first_visible_x)
19855 row->x = x - it->first_visible_x;
19856 /* Record the maximum and minimum buffer positions seen so
19857 far in glyphs that will be displayed by this row. */
19858 if (it->bidi_p)
19859 RECORD_MAX_MIN_POS (it);
19860 }
19861 else
19862 {
19863 int i, new_x;
19864 struct glyph *glyph;
19865
19866 for (i = 0; i < nglyphs; ++i, x = new_x)
19867 {
19868 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19869 new_x = x + glyph->pixel_width;
19870
19871 if (/* Lines are continued. */
19872 it->line_wrap != TRUNCATE
19873 && (/* Glyph doesn't fit on the line. */
19874 new_x > it->last_visible_x
19875 /* Or it fits exactly on a window system frame. */
19876 || (new_x == it->last_visible_x
19877 && FRAME_WINDOW_P (it->f)
19878 && (row->reversed_p
19879 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19880 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19881 {
19882 /* End of a continued line. */
19883
19884 if (it->hpos == 0
19885 || (new_x == it->last_visible_x
19886 && FRAME_WINDOW_P (it->f)
19887 && (row->reversed_p
19888 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19889 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19890 {
19891 /* Current glyph is the only one on the line or
19892 fits exactly on the line. We must continue
19893 the line because we can't draw the cursor
19894 after the glyph. */
19895 row->continued_p = 1;
19896 it->current_x = new_x;
19897 it->continuation_lines_width += new_x;
19898 ++it->hpos;
19899 if (i == nglyphs - 1)
19900 {
19901 /* If line-wrap is on, check if a previous
19902 wrap point was found. */
19903 if (wrap_row_used > 0
19904 /* Even if there is a previous wrap
19905 point, continue the line here as
19906 usual, if (i) the previous character
19907 was a space or tab AND (ii) the
19908 current character is not. */
19909 && (!may_wrap
19910 || IT_DISPLAYING_WHITESPACE (it)))
19911 goto back_to_wrap;
19912
19913 /* Record the maximum and minimum buffer
19914 positions seen so far in glyphs that will be
19915 displayed by this row. */
19916 if (it->bidi_p)
19917 RECORD_MAX_MIN_POS (it);
19918 set_iterator_to_next (it, 1);
19919 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19920 {
19921 if (!get_next_display_element (it))
19922 {
19923 row->exact_window_width_line_p = 1;
19924 it->continuation_lines_width = 0;
19925 row->continued_p = 0;
19926 row->ends_at_zv_p = 1;
19927 }
19928 else if (ITERATOR_AT_END_OF_LINE_P (it))
19929 {
19930 row->continued_p = 0;
19931 row->exact_window_width_line_p = 1;
19932 }
19933 }
19934 }
19935 else if (it->bidi_p)
19936 RECORD_MAX_MIN_POS (it);
19937 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19938 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19939 extend_face_to_end_of_line (it);
19940 }
19941 else if (CHAR_GLYPH_PADDING_P (*glyph)
19942 && !FRAME_WINDOW_P (it->f))
19943 {
19944 /* A padding glyph that doesn't fit on this line.
19945 This means the whole character doesn't fit
19946 on the line. */
19947 if (row->reversed_p)
19948 unproduce_glyphs (it, row->used[TEXT_AREA]
19949 - n_glyphs_before);
19950 row->used[TEXT_AREA] = n_glyphs_before;
19951
19952 /* Fill the rest of the row with continuation
19953 glyphs like in 20.x. */
19954 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19955 < row->glyphs[1 + TEXT_AREA])
19956 produce_special_glyphs (it, IT_CONTINUATION);
19957
19958 row->continued_p = 1;
19959 it->current_x = x_before;
19960 it->continuation_lines_width += x_before;
19961
19962 /* Restore the height to what it was before the
19963 element not fitting on the line. */
19964 it->max_ascent = ascent;
19965 it->max_descent = descent;
19966 it->max_phys_ascent = phys_ascent;
19967 it->max_phys_descent = phys_descent;
19968 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19969 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19970 extend_face_to_end_of_line (it);
19971 }
19972 else if (wrap_row_used > 0)
19973 {
19974 back_to_wrap:
19975 if (row->reversed_p)
19976 unproduce_glyphs (it,
19977 row->used[TEXT_AREA] - wrap_row_used);
19978 RESTORE_IT (it, &wrap_it, wrap_data);
19979 it->continuation_lines_width += wrap_x;
19980 row->used[TEXT_AREA] = wrap_row_used;
19981 row->ascent = wrap_row_ascent;
19982 row->height = wrap_row_height;
19983 row->phys_ascent = wrap_row_phys_ascent;
19984 row->phys_height = wrap_row_phys_height;
19985 row->extra_line_spacing = wrap_row_extra_line_spacing;
19986 min_pos = wrap_row_min_pos;
19987 min_bpos = wrap_row_min_bpos;
19988 max_pos = wrap_row_max_pos;
19989 max_bpos = wrap_row_max_bpos;
19990 row->continued_p = 1;
19991 row->ends_at_zv_p = 0;
19992 row->exact_window_width_line_p = 0;
19993 it->continuation_lines_width += x;
19994
19995 /* Make sure that a non-default face is extended
19996 up to the right margin of the window. */
19997 extend_face_to_end_of_line (it);
19998 }
19999 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20000 {
20001 /* A TAB that extends past the right edge of the
20002 window. This produces a single glyph on
20003 window system frames. We leave the glyph in
20004 this row and let it fill the row, but don't
20005 consume the TAB. */
20006 if ((row->reversed_p
20007 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20008 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20009 produce_special_glyphs (it, IT_CONTINUATION);
20010 it->continuation_lines_width += it->last_visible_x;
20011 row->ends_in_middle_of_char_p = 1;
20012 row->continued_p = 1;
20013 glyph->pixel_width = it->last_visible_x - x;
20014 it->starts_in_middle_of_char_p = 1;
20015 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20016 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20017 extend_face_to_end_of_line (it);
20018 }
20019 else
20020 {
20021 /* Something other than a TAB that draws past
20022 the right edge of the window. Restore
20023 positions to values before the element. */
20024 if (row->reversed_p)
20025 unproduce_glyphs (it, row->used[TEXT_AREA]
20026 - (n_glyphs_before + i));
20027 row->used[TEXT_AREA] = n_glyphs_before + i;
20028
20029 /* Display continuation glyphs. */
20030 it->current_x = x_before;
20031 it->continuation_lines_width += x;
20032 if (!FRAME_WINDOW_P (it->f)
20033 || (row->reversed_p
20034 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20035 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20036 produce_special_glyphs (it, IT_CONTINUATION);
20037 row->continued_p = 1;
20038
20039 extend_face_to_end_of_line (it);
20040
20041 if (nglyphs > 1 && i > 0)
20042 {
20043 row->ends_in_middle_of_char_p = 1;
20044 it->starts_in_middle_of_char_p = 1;
20045 }
20046
20047 /* Restore the height to what it was before the
20048 element not fitting on the line. */
20049 it->max_ascent = ascent;
20050 it->max_descent = descent;
20051 it->max_phys_ascent = phys_ascent;
20052 it->max_phys_descent = phys_descent;
20053 }
20054
20055 break;
20056 }
20057 else if (new_x > it->first_visible_x)
20058 {
20059 /* Increment number of glyphs actually displayed. */
20060 ++it->hpos;
20061
20062 /* Record the maximum and minimum buffer positions
20063 seen so far in glyphs that will be displayed by
20064 this row. */
20065 if (it->bidi_p)
20066 RECORD_MAX_MIN_POS (it);
20067
20068 if (x < it->first_visible_x)
20069 /* Glyph is partially visible, i.e. row starts at
20070 negative X position. */
20071 row->x = x - it->first_visible_x;
20072 }
20073 else
20074 {
20075 /* Glyph is completely off the left margin of the
20076 window. This should not happen because of the
20077 move_it_in_display_line at the start of this
20078 function, unless the text display area of the
20079 window is empty. */
20080 eassert (it->first_visible_x <= it->last_visible_x);
20081 }
20082 }
20083 /* Even if this display element produced no glyphs at all,
20084 we want to record its position. */
20085 if (it->bidi_p && nglyphs == 0)
20086 RECORD_MAX_MIN_POS (it);
20087
20088 row->ascent = max (row->ascent, it->max_ascent);
20089 row->height = max (row->height, it->max_ascent + it->max_descent);
20090 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20091 row->phys_height = max (row->phys_height,
20092 it->max_phys_ascent + it->max_phys_descent);
20093 row->extra_line_spacing = max (row->extra_line_spacing,
20094 it->max_extra_line_spacing);
20095
20096 /* End of this display line if row is continued. */
20097 if (row->continued_p || row->ends_at_zv_p)
20098 break;
20099 }
20100
20101 at_end_of_line:
20102 /* Is this a line end? If yes, we're also done, after making
20103 sure that a non-default face is extended up to the right
20104 margin of the window. */
20105 if (ITERATOR_AT_END_OF_LINE_P (it))
20106 {
20107 int used_before = row->used[TEXT_AREA];
20108
20109 row->ends_in_newline_from_string_p = STRINGP (it->object);
20110
20111 /* Add a space at the end of the line that is used to
20112 display the cursor there. */
20113 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20114 append_space_for_newline (it, 0);
20115
20116 /* Extend the face to the end of the line. */
20117 extend_face_to_end_of_line (it);
20118
20119 /* Make sure we have the position. */
20120 if (used_before == 0)
20121 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20122
20123 /* Record the position of the newline, for use in
20124 find_row_edges. */
20125 it->eol_pos = it->current.pos;
20126
20127 /* Consume the line end. This skips over invisible lines. */
20128 set_iterator_to_next (it, 1);
20129 it->continuation_lines_width = 0;
20130 break;
20131 }
20132
20133 /* Proceed with next display element. Note that this skips
20134 over lines invisible because of selective display. */
20135 set_iterator_to_next (it, 1);
20136
20137 /* If we truncate lines, we are done when the last displayed
20138 glyphs reach past the right margin of the window. */
20139 if (it->line_wrap == TRUNCATE
20140 && ((FRAME_WINDOW_P (it->f)
20141 /* Images are preprocessed in produce_image_glyph such
20142 that they are cropped at the right edge of the
20143 window, so an image glyph will always end exactly at
20144 last_visible_x, even if there's no right fringe. */
20145 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20146 ? (it->current_x >= it->last_visible_x)
20147 : (it->current_x > it->last_visible_x)))
20148 {
20149 /* Maybe add truncation glyphs. */
20150 if (!FRAME_WINDOW_P (it->f)
20151 || (row->reversed_p
20152 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20153 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20154 {
20155 int i, n;
20156
20157 if (!row->reversed_p)
20158 {
20159 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20160 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20161 break;
20162 }
20163 else
20164 {
20165 for (i = 0; i < row->used[TEXT_AREA]; i++)
20166 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20167 break;
20168 /* Remove any padding glyphs at the front of ROW, to
20169 make room for the truncation glyphs we will be
20170 adding below. The loop below always inserts at
20171 least one truncation glyph, so also remove the
20172 last glyph added to ROW. */
20173 unproduce_glyphs (it, i + 1);
20174 /* Adjust i for the loop below. */
20175 i = row->used[TEXT_AREA] - (i + 1);
20176 }
20177
20178 /* produce_special_glyphs overwrites the last glyph, so
20179 we don't want that if we want to keep that last
20180 glyph, which means it's an image. */
20181 if (it->current_x > it->last_visible_x)
20182 {
20183 it->current_x = x_before;
20184 if (!FRAME_WINDOW_P (it->f))
20185 {
20186 for (n = row->used[TEXT_AREA]; i < n; ++i)
20187 {
20188 row->used[TEXT_AREA] = i;
20189 produce_special_glyphs (it, IT_TRUNCATION);
20190 }
20191 }
20192 else
20193 {
20194 row->used[TEXT_AREA] = i;
20195 produce_special_glyphs (it, IT_TRUNCATION);
20196 }
20197 it->hpos = hpos_before;
20198 }
20199 }
20200 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20201 {
20202 /* Don't truncate if we can overflow newline into fringe. */
20203 if (!get_next_display_element (it))
20204 {
20205 it->continuation_lines_width = 0;
20206 row->ends_at_zv_p = 1;
20207 row->exact_window_width_line_p = 1;
20208 break;
20209 }
20210 if (ITERATOR_AT_END_OF_LINE_P (it))
20211 {
20212 row->exact_window_width_line_p = 1;
20213 goto at_end_of_line;
20214 }
20215 it->current_x = x_before;
20216 it->hpos = hpos_before;
20217 }
20218
20219 row->truncated_on_right_p = 1;
20220 it->continuation_lines_width = 0;
20221 reseat_at_next_visible_line_start (it, 0);
20222 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20223 break;
20224 }
20225 }
20226
20227 if (wrap_data)
20228 bidi_unshelve_cache (wrap_data, 1);
20229
20230 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20231 at the left window margin. */
20232 if (it->first_visible_x
20233 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20234 {
20235 if (!FRAME_WINDOW_P (it->f)
20236 || (((row->reversed_p
20237 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20238 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20239 /* Don't let insert_left_trunc_glyphs overwrite the
20240 first glyph of the row if it is an image. */
20241 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20242 insert_left_trunc_glyphs (it);
20243 row->truncated_on_left_p = 1;
20244 }
20245
20246 /* Remember the position at which this line ends.
20247
20248 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20249 cannot be before the call to find_row_edges below, since that is
20250 where these positions are determined. */
20251 row->end = it->current;
20252 if (!it->bidi_p)
20253 {
20254 row->minpos = row->start.pos;
20255 row->maxpos = row->end.pos;
20256 }
20257 else
20258 {
20259 /* ROW->minpos and ROW->maxpos must be the smallest and
20260 `1 + the largest' buffer positions in ROW. But if ROW was
20261 bidi-reordered, these two positions can be anywhere in the
20262 row, so we must determine them now. */
20263 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20264 }
20265
20266 /* If the start of this line is the overlay arrow-position, then
20267 mark this glyph row as the one containing the overlay arrow.
20268 This is clearly a mess with variable size fonts. It would be
20269 better to let it be displayed like cursors under X. */
20270 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20271 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20272 !NILP (overlay_arrow_string)))
20273 {
20274 /* Overlay arrow in window redisplay is a fringe bitmap. */
20275 if (STRINGP (overlay_arrow_string))
20276 {
20277 struct glyph_row *arrow_row
20278 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20279 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20280 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20281 struct glyph *p = row->glyphs[TEXT_AREA];
20282 struct glyph *p2, *end;
20283
20284 /* Copy the arrow glyphs. */
20285 while (glyph < arrow_end)
20286 *p++ = *glyph++;
20287
20288 /* Throw away padding glyphs. */
20289 p2 = p;
20290 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20291 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20292 ++p2;
20293 if (p2 > p)
20294 {
20295 while (p2 < end)
20296 *p++ = *p2++;
20297 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20298 }
20299 }
20300 else
20301 {
20302 eassert (INTEGERP (overlay_arrow_string));
20303 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20304 }
20305 overlay_arrow_seen = 1;
20306 }
20307
20308 /* Highlight trailing whitespace. */
20309 if (!NILP (Vshow_trailing_whitespace))
20310 highlight_trailing_whitespace (it->f, it->glyph_row);
20311
20312 /* Compute pixel dimensions of this line. */
20313 compute_line_metrics (it);
20314
20315 /* Implementation note: No changes in the glyphs of ROW or in their
20316 faces can be done past this point, because compute_line_metrics
20317 computes ROW's hash value and stores it within the glyph_row
20318 structure. */
20319
20320 /* Record whether this row ends inside an ellipsis. */
20321 row->ends_in_ellipsis_p
20322 = (it->method == GET_FROM_DISPLAY_VECTOR
20323 && it->ellipsis_p);
20324
20325 /* Save fringe bitmaps in this row. */
20326 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20327 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20328 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20329 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20330
20331 it->left_user_fringe_bitmap = 0;
20332 it->left_user_fringe_face_id = 0;
20333 it->right_user_fringe_bitmap = 0;
20334 it->right_user_fringe_face_id = 0;
20335
20336 /* Maybe set the cursor. */
20337 cvpos = it->w->cursor.vpos;
20338 if ((cvpos < 0
20339 /* In bidi-reordered rows, keep checking for proper cursor
20340 position even if one has been found already, because buffer
20341 positions in such rows change non-linearly with ROW->VPOS,
20342 when a line is continued. One exception: when we are at ZV,
20343 display cursor on the first suitable glyph row, since all
20344 the empty rows after that also have their position set to ZV. */
20345 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20346 lines' rows is implemented for bidi-reordered rows. */
20347 || (it->bidi_p
20348 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20349 && PT >= MATRIX_ROW_START_CHARPOS (row)
20350 && PT <= MATRIX_ROW_END_CHARPOS (row)
20351 && cursor_row_p (row))
20352 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20353
20354 /* Prepare for the next line. This line starts horizontally at (X
20355 HPOS) = (0 0). Vertical positions are incremented. As a
20356 convenience for the caller, IT->glyph_row is set to the next
20357 row to be used. */
20358 it->current_x = it->hpos = 0;
20359 it->current_y += row->height;
20360 SET_TEXT_POS (it->eol_pos, 0, 0);
20361 ++it->vpos;
20362 ++it->glyph_row;
20363 /* The next row should by default use the same value of the
20364 reversed_p flag as this one. set_iterator_to_next decides when
20365 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20366 the flag accordingly. */
20367 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20368 it->glyph_row->reversed_p = row->reversed_p;
20369 it->start = row->end;
20370 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20371
20372 #undef RECORD_MAX_MIN_POS
20373 }
20374
20375 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20376 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20377 doc: /* Return paragraph direction at point in BUFFER.
20378 Value is either `left-to-right' or `right-to-left'.
20379 If BUFFER is omitted or nil, it defaults to the current buffer.
20380
20381 Paragraph direction determines how the text in the paragraph is displayed.
20382 In left-to-right paragraphs, text begins at the left margin of the window
20383 and the reading direction is generally left to right. In right-to-left
20384 paragraphs, text begins at the right margin and is read from right to left.
20385
20386 See also `bidi-paragraph-direction'. */)
20387 (Lisp_Object buffer)
20388 {
20389 struct buffer *buf = current_buffer;
20390 struct buffer *old = buf;
20391
20392 if (! NILP (buffer))
20393 {
20394 CHECK_BUFFER (buffer);
20395 buf = XBUFFER (buffer);
20396 }
20397
20398 if (NILP (BVAR (buf, bidi_display_reordering))
20399 || NILP (BVAR (buf, enable_multibyte_characters))
20400 /* When we are loading loadup.el, the character property tables
20401 needed for bidi iteration are not yet available. */
20402 || !NILP (Vpurify_flag))
20403 return Qleft_to_right;
20404 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20405 return BVAR (buf, bidi_paragraph_direction);
20406 else
20407 {
20408 /* Determine the direction from buffer text. We could try to
20409 use current_matrix if it is up to date, but this seems fast
20410 enough as it is. */
20411 struct bidi_it itb;
20412 ptrdiff_t pos = BUF_PT (buf);
20413 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20414 int c;
20415 void *itb_data = bidi_shelve_cache ();
20416
20417 set_buffer_temp (buf);
20418 /* bidi_paragraph_init finds the base direction of the paragraph
20419 by searching forward from paragraph start. We need the base
20420 direction of the current or _previous_ paragraph, so we need
20421 to make sure we are within that paragraph. To that end, find
20422 the previous non-empty line. */
20423 if (pos >= ZV && pos > BEGV)
20424 DEC_BOTH (pos, bytepos);
20425 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20426 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20427 {
20428 while ((c = FETCH_BYTE (bytepos)) == '\n'
20429 || c == ' ' || c == '\t' || c == '\f')
20430 {
20431 if (bytepos <= BEGV_BYTE)
20432 break;
20433 bytepos--;
20434 pos--;
20435 }
20436 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20437 bytepos--;
20438 }
20439 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20440 itb.paragraph_dir = NEUTRAL_DIR;
20441 itb.string.s = NULL;
20442 itb.string.lstring = Qnil;
20443 itb.string.bufpos = 0;
20444 itb.string.from_disp_str = 0;
20445 itb.string.unibyte = 0;
20446 /* We have no window to use here for ignoring window-specific
20447 overlays. Using NULL for window pointer will cause
20448 compute_display_string_pos to use the current buffer. */
20449 itb.w = NULL;
20450 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20451 bidi_unshelve_cache (itb_data, 0);
20452 set_buffer_temp (old);
20453 switch (itb.paragraph_dir)
20454 {
20455 case L2R:
20456 return Qleft_to_right;
20457 break;
20458 case R2L:
20459 return Qright_to_left;
20460 break;
20461 default:
20462 emacs_abort ();
20463 }
20464 }
20465 }
20466
20467 DEFUN ("move-point-visually", Fmove_point_visually,
20468 Smove_point_visually, 1, 1, 0,
20469 doc: /* Move point in the visual order in the specified DIRECTION.
20470 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20471 left.
20472
20473 Value is the new character position of point. */)
20474 (Lisp_Object direction)
20475 {
20476 struct window *w = XWINDOW (selected_window);
20477 struct buffer *b = XBUFFER (w->contents);
20478 struct glyph_row *row;
20479 int dir;
20480 Lisp_Object paragraph_dir;
20481
20482 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20483 (!(ROW)->continued_p \
20484 && INTEGERP ((GLYPH)->object) \
20485 && (GLYPH)->type == CHAR_GLYPH \
20486 && (GLYPH)->u.ch == ' ' \
20487 && (GLYPH)->charpos >= 0 \
20488 && !(GLYPH)->avoid_cursor_p)
20489
20490 CHECK_NUMBER (direction);
20491 dir = XINT (direction);
20492 if (dir > 0)
20493 dir = 1;
20494 else
20495 dir = -1;
20496
20497 /* If current matrix is up-to-date, we can use the information
20498 recorded in the glyphs, at least as long as the goal is on the
20499 screen. */
20500 if (w->window_end_valid
20501 && !windows_or_buffers_changed
20502 && b
20503 && !b->clip_changed
20504 && !b->prevent_redisplay_optimizations_p
20505 && !window_outdated (w)
20506 && w->cursor.vpos >= 0
20507 && w->cursor.vpos < w->current_matrix->nrows
20508 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20509 {
20510 struct glyph *g = row->glyphs[TEXT_AREA];
20511 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20512 struct glyph *gpt = g + w->cursor.hpos;
20513
20514 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20515 {
20516 if (BUFFERP (g->object) && g->charpos != PT)
20517 {
20518 SET_PT (g->charpos);
20519 w->cursor.vpos = -1;
20520 return make_number (PT);
20521 }
20522 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20523 {
20524 ptrdiff_t new_pos;
20525
20526 if (BUFFERP (gpt->object))
20527 {
20528 new_pos = PT;
20529 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20530 new_pos += (row->reversed_p ? -dir : dir);
20531 else
20532 new_pos -= (row->reversed_p ? -dir : dir);;
20533 }
20534 else if (BUFFERP (g->object))
20535 new_pos = g->charpos;
20536 else
20537 break;
20538 SET_PT (new_pos);
20539 w->cursor.vpos = -1;
20540 return make_number (PT);
20541 }
20542 else if (ROW_GLYPH_NEWLINE_P (row, g))
20543 {
20544 /* Glyphs inserted at the end of a non-empty line for
20545 positioning the cursor have zero charpos, so we must
20546 deduce the value of point by other means. */
20547 if (g->charpos > 0)
20548 SET_PT (g->charpos);
20549 else if (row->ends_at_zv_p && PT != ZV)
20550 SET_PT (ZV);
20551 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20552 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20553 else
20554 break;
20555 w->cursor.vpos = -1;
20556 return make_number (PT);
20557 }
20558 }
20559 if (g == e || INTEGERP (g->object))
20560 {
20561 if (row->truncated_on_left_p || row->truncated_on_right_p)
20562 goto simulate_display;
20563 if (!row->reversed_p)
20564 row += dir;
20565 else
20566 row -= dir;
20567 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20568 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20569 goto simulate_display;
20570
20571 if (dir > 0)
20572 {
20573 if (row->reversed_p && !row->continued_p)
20574 {
20575 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20576 w->cursor.vpos = -1;
20577 return make_number (PT);
20578 }
20579 g = row->glyphs[TEXT_AREA];
20580 e = g + row->used[TEXT_AREA];
20581 for ( ; g < e; g++)
20582 {
20583 if (BUFFERP (g->object)
20584 /* Empty lines have only one glyph, which stands
20585 for the newline, and whose charpos is the
20586 buffer position of the newline. */
20587 || ROW_GLYPH_NEWLINE_P (row, g)
20588 /* When the buffer ends in a newline, the line at
20589 EOB also has one glyph, but its charpos is -1. */
20590 || (row->ends_at_zv_p
20591 && !row->reversed_p
20592 && INTEGERP (g->object)
20593 && g->type == CHAR_GLYPH
20594 && g->u.ch == ' '))
20595 {
20596 if (g->charpos > 0)
20597 SET_PT (g->charpos);
20598 else if (!row->reversed_p
20599 && row->ends_at_zv_p
20600 && PT != ZV)
20601 SET_PT (ZV);
20602 else
20603 continue;
20604 w->cursor.vpos = -1;
20605 return make_number (PT);
20606 }
20607 }
20608 }
20609 else
20610 {
20611 if (!row->reversed_p && !row->continued_p)
20612 {
20613 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20614 w->cursor.vpos = -1;
20615 return make_number (PT);
20616 }
20617 e = row->glyphs[TEXT_AREA];
20618 g = e + row->used[TEXT_AREA] - 1;
20619 for ( ; g >= e; g--)
20620 {
20621 if (BUFFERP (g->object)
20622 || (ROW_GLYPH_NEWLINE_P (row, g)
20623 && g->charpos > 0)
20624 /* Empty R2L lines on GUI frames have the buffer
20625 position of the newline stored in the stretch
20626 glyph. */
20627 || g->type == STRETCH_GLYPH
20628 || (row->ends_at_zv_p
20629 && row->reversed_p
20630 && INTEGERP (g->object)
20631 && g->type == CHAR_GLYPH
20632 && g->u.ch == ' '))
20633 {
20634 if (g->charpos > 0)
20635 SET_PT (g->charpos);
20636 else if (row->reversed_p
20637 && row->ends_at_zv_p
20638 && PT != ZV)
20639 SET_PT (ZV);
20640 else
20641 continue;
20642 w->cursor.vpos = -1;
20643 return make_number (PT);
20644 }
20645 }
20646 }
20647 }
20648 }
20649
20650 simulate_display:
20651
20652 /* If we wind up here, we failed to move by using the glyphs, so we
20653 need to simulate display instead. */
20654
20655 if (b)
20656 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20657 else
20658 paragraph_dir = Qleft_to_right;
20659 if (EQ (paragraph_dir, Qright_to_left))
20660 dir = -dir;
20661 if (PT <= BEGV && dir < 0)
20662 xsignal0 (Qbeginning_of_buffer);
20663 else if (PT >= ZV && dir > 0)
20664 xsignal0 (Qend_of_buffer);
20665 else
20666 {
20667 struct text_pos pt;
20668 struct it it;
20669 int pt_x, target_x, pixel_width, pt_vpos;
20670 bool at_eol_p;
20671 bool overshoot_expected = false;
20672 bool target_is_eol_p = false;
20673
20674 /* Setup the arena. */
20675 SET_TEXT_POS (pt, PT, PT_BYTE);
20676 start_display (&it, w, pt);
20677
20678 if (it.cmp_it.id < 0
20679 && it.method == GET_FROM_STRING
20680 && it.area == TEXT_AREA
20681 && it.string_from_display_prop_p
20682 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20683 overshoot_expected = true;
20684
20685 /* Find the X coordinate of point. We start from the beginning
20686 of this or previous line to make sure we are before point in
20687 the logical order (since the move_it_* functions can only
20688 move forward). */
20689 reseat:
20690 reseat_at_previous_visible_line_start (&it);
20691 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20692 if (IT_CHARPOS (it) != PT)
20693 {
20694 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20695 -1, -1, -1, MOVE_TO_POS);
20696 /* If we missed point because the character there is
20697 displayed out of a display vector that has more than one
20698 glyph, retry expecting overshoot. */
20699 if (it.method == GET_FROM_DISPLAY_VECTOR
20700 && it.current.dpvec_index > 0
20701 && !overshoot_expected)
20702 {
20703 overshoot_expected = true;
20704 goto reseat;
20705 }
20706 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20707 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20708 }
20709 pt_x = it.current_x;
20710 pt_vpos = it.vpos;
20711 if (dir > 0 || overshoot_expected)
20712 {
20713 struct glyph_row *row = it.glyph_row;
20714
20715 /* When point is at beginning of line, we don't have
20716 information about the glyph there loaded into struct
20717 it. Calling get_next_display_element fixes that. */
20718 if (pt_x == 0)
20719 get_next_display_element (&it);
20720 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20721 it.glyph_row = NULL;
20722 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20723 it.glyph_row = row;
20724 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20725 it, lest it will become out of sync with it's buffer
20726 position. */
20727 it.current_x = pt_x;
20728 }
20729 else
20730 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20731 pixel_width = it.pixel_width;
20732 if (overshoot_expected && at_eol_p)
20733 pixel_width = 0;
20734 else if (pixel_width <= 0)
20735 pixel_width = 1;
20736
20737 /* If there's a display string (or something similar) at point,
20738 we are actually at the glyph to the left of point, so we need
20739 to correct the X coordinate. */
20740 if (overshoot_expected)
20741 {
20742 if (it.bidi_p)
20743 pt_x += pixel_width * it.bidi_it.scan_dir;
20744 else
20745 pt_x += pixel_width;
20746 }
20747
20748 /* Compute target X coordinate, either to the left or to the
20749 right of point. On TTY frames, all characters have the same
20750 pixel width of 1, so we can use that. On GUI frames we don't
20751 have an easy way of getting at the pixel width of the
20752 character to the left of point, so we use a different method
20753 of getting to that place. */
20754 if (dir > 0)
20755 target_x = pt_x + pixel_width;
20756 else
20757 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20758
20759 /* Target X coordinate could be one line above or below the line
20760 of point, in which case we need to adjust the target X
20761 coordinate. Also, if moving to the left, we need to begin at
20762 the left edge of the point's screen line. */
20763 if (dir < 0)
20764 {
20765 if (pt_x > 0)
20766 {
20767 start_display (&it, w, pt);
20768 reseat_at_previous_visible_line_start (&it);
20769 it.current_x = it.current_y = it.hpos = 0;
20770 if (pt_vpos != 0)
20771 move_it_by_lines (&it, pt_vpos);
20772 }
20773 else
20774 {
20775 move_it_by_lines (&it, -1);
20776 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20777 target_is_eol_p = true;
20778 }
20779 }
20780 else
20781 {
20782 if (at_eol_p
20783 || (target_x >= it.last_visible_x
20784 && it.line_wrap != TRUNCATE))
20785 {
20786 if (pt_x > 0)
20787 move_it_by_lines (&it, 0);
20788 move_it_by_lines (&it, 1);
20789 target_x = 0;
20790 }
20791 }
20792
20793 /* Move to the target X coordinate. */
20794 #ifdef HAVE_WINDOW_SYSTEM
20795 /* On GUI frames, as we don't know the X coordinate of the
20796 character to the left of point, moving point to the left
20797 requires walking, one grapheme cluster at a time, until we
20798 find ourself at a place immediately to the left of the
20799 character at point. */
20800 if (FRAME_WINDOW_P (it.f) && dir < 0)
20801 {
20802 struct text_pos new_pos;
20803 enum move_it_result rc = MOVE_X_REACHED;
20804
20805 if (it.current_x == 0)
20806 get_next_display_element (&it);
20807 if (it.what == IT_COMPOSITION)
20808 {
20809 new_pos.charpos = it.cmp_it.charpos;
20810 new_pos.bytepos = -1;
20811 }
20812 else
20813 new_pos = it.current.pos;
20814
20815 while (it.current_x + it.pixel_width <= target_x
20816 && rc == MOVE_X_REACHED)
20817 {
20818 int new_x = it.current_x + it.pixel_width;
20819
20820 /* For composed characters, we want the position of the
20821 first character in the grapheme cluster (usually, the
20822 composition's base character), whereas it.current
20823 might give us the position of the _last_ one, e.g. if
20824 the composition is rendered in reverse due to bidi
20825 reordering. */
20826 if (it.what == IT_COMPOSITION)
20827 {
20828 new_pos.charpos = it.cmp_it.charpos;
20829 new_pos.bytepos = -1;
20830 }
20831 else
20832 new_pos = it.current.pos;
20833 if (new_x == it.current_x)
20834 new_x++;
20835 rc = move_it_in_display_line_to (&it, ZV, new_x,
20836 MOVE_TO_POS | MOVE_TO_X);
20837 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20838 break;
20839 }
20840 /* The previous position we saw in the loop is the one we
20841 want. */
20842 if (new_pos.bytepos == -1)
20843 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20844 it.current.pos = new_pos;
20845 }
20846 else
20847 #endif
20848 if (it.current_x != target_x)
20849 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20850
20851 /* When lines are truncated, the above loop will stop at the
20852 window edge. But we want to get to the end of line, even if
20853 it is beyond the window edge; automatic hscroll will then
20854 scroll the window to show point as appropriate. */
20855 if (target_is_eol_p && it.line_wrap == TRUNCATE
20856 && get_next_display_element (&it))
20857 {
20858 struct text_pos new_pos = it.current.pos;
20859
20860 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20861 {
20862 set_iterator_to_next (&it, 0);
20863 if (it.method == GET_FROM_BUFFER)
20864 new_pos = it.current.pos;
20865 if (!get_next_display_element (&it))
20866 break;
20867 }
20868
20869 it.current.pos = new_pos;
20870 }
20871
20872 /* If we ended up in a display string that covers point, move to
20873 buffer position to the right in the visual order. */
20874 if (dir > 0)
20875 {
20876 while (IT_CHARPOS (it) == PT)
20877 {
20878 set_iterator_to_next (&it, 0);
20879 if (!get_next_display_element (&it))
20880 break;
20881 }
20882 }
20883
20884 /* Move point to that position. */
20885 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20886 }
20887
20888 return make_number (PT);
20889
20890 #undef ROW_GLYPH_NEWLINE_P
20891 }
20892
20893 \f
20894 /***********************************************************************
20895 Menu Bar
20896 ***********************************************************************/
20897
20898 /* Redisplay the menu bar in the frame for window W.
20899
20900 The menu bar of X frames that don't have X toolkit support is
20901 displayed in a special window W->frame->menu_bar_window.
20902
20903 The menu bar of terminal frames is treated specially as far as
20904 glyph matrices are concerned. Menu bar lines are not part of
20905 windows, so the update is done directly on the frame matrix rows
20906 for the menu bar. */
20907
20908 static void
20909 display_menu_bar (struct window *w)
20910 {
20911 struct frame *f = XFRAME (WINDOW_FRAME (w));
20912 struct it it;
20913 Lisp_Object items;
20914 int i;
20915
20916 /* Don't do all this for graphical frames. */
20917 #ifdef HAVE_NTGUI
20918 if (FRAME_W32_P (f))
20919 return;
20920 #endif
20921 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20922 if (FRAME_X_P (f))
20923 return;
20924 #endif
20925
20926 #ifdef HAVE_NS
20927 if (FRAME_NS_P (f))
20928 return;
20929 #endif /* HAVE_NS */
20930
20931 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20932 eassert (!FRAME_WINDOW_P (f));
20933 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20934 it.first_visible_x = 0;
20935 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20936 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20937 if (FRAME_WINDOW_P (f))
20938 {
20939 /* Menu bar lines are displayed in the desired matrix of the
20940 dummy window menu_bar_window. */
20941 struct window *menu_w;
20942 menu_w = XWINDOW (f->menu_bar_window);
20943 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20944 MENU_FACE_ID);
20945 it.first_visible_x = 0;
20946 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20947 }
20948 else
20949 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20950 {
20951 /* This is a TTY frame, i.e. character hpos/vpos are used as
20952 pixel x/y. */
20953 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20954 MENU_FACE_ID);
20955 it.first_visible_x = 0;
20956 it.last_visible_x = FRAME_COLS (f);
20957 }
20958
20959 /* FIXME: This should be controlled by a user option. See the
20960 comments in redisplay_tool_bar and display_mode_line about
20961 this. */
20962 it.paragraph_embedding = L2R;
20963
20964 /* Clear all rows of the menu bar. */
20965 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20966 {
20967 struct glyph_row *row = it.glyph_row + i;
20968 clear_glyph_row (row);
20969 row->enabled_p = true;
20970 row->full_width_p = 1;
20971 }
20972
20973 /* Display all items of the menu bar. */
20974 items = FRAME_MENU_BAR_ITEMS (it.f);
20975 for (i = 0; i < ASIZE (items); i += 4)
20976 {
20977 Lisp_Object string;
20978
20979 /* Stop at nil string. */
20980 string = AREF (items, i + 1);
20981 if (NILP (string))
20982 break;
20983
20984 /* Remember where item was displayed. */
20985 ASET (items, i + 3, make_number (it.hpos));
20986
20987 /* Display the item, pad with one space. */
20988 if (it.current_x < it.last_visible_x)
20989 display_string (NULL, string, Qnil, 0, 0, &it,
20990 SCHARS (string) + 1, 0, 0, -1);
20991 }
20992
20993 /* Fill out the line with spaces. */
20994 if (it.current_x < it.last_visible_x)
20995 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20996
20997 /* Compute the total height of the lines. */
20998 compute_line_metrics (&it);
20999 }
21000
21001 /* Deep copy of a glyph row, including the glyphs. */
21002 static void
21003 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21004 {
21005 struct glyph *pointers[1 + LAST_AREA];
21006 int to_used = to->used[TEXT_AREA];
21007
21008 /* Save glyph pointers of TO. */
21009 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21010
21011 /* Do a structure assignment. */
21012 *to = *from;
21013
21014 /* Restore original glyph pointers of TO. */
21015 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21016
21017 /* Copy the glyphs. */
21018 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21019 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21020
21021 /* If we filled only part of the TO row, fill the rest with
21022 space_glyph (which will display as empty space). */
21023 if (to_used > from->used[TEXT_AREA])
21024 fill_up_frame_row_with_spaces (to, to_used);
21025 }
21026
21027 /* Display one menu item on a TTY, by overwriting the glyphs in the
21028 frame F's desired glyph matrix with glyphs produced from the menu
21029 item text. Called from term.c to display TTY drop-down menus one
21030 item at a time.
21031
21032 ITEM_TEXT is the menu item text as a C string.
21033
21034 FACE_ID is the face ID to be used for this menu item. FACE_ID
21035 could specify one of 3 faces: a face for an enabled item, a face
21036 for a disabled item, or a face for a selected item.
21037
21038 X and Y are coordinates of the first glyph in the frame's desired
21039 matrix to be overwritten by the menu item. Since this is a TTY, Y
21040 is the zero-based number of the glyph row and X is the zero-based
21041 glyph number in the row, starting from left, where to start
21042 displaying the item.
21043
21044 SUBMENU non-zero means this menu item drops down a submenu, which
21045 should be indicated by displaying a proper visual cue after the
21046 item text. */
21047
21048 void
21049 display_tty_menu_item (const char *item_text, int width, int face_id,
21050 int x, int y, int submenu)
21051 {
21052 struct it it;
21053 struct frame *f = SELECTED_FRAME ();
21054 struct window *w = XWINDOW (f->selected_window);
21055 int saved_used, saved_truncated, saved_width, saved_reversed;
21056 struct glyph_row *row;
21057 size_t item_len = strlen (item_text);
21058
21059 eassert (FRAME_TERMCAP_P (f));
21060
21061 /* Don't write beyond the matrix's last row. This can happen for
21062 TTY screens that are not high enough to show the entire menu.
21063 (This is actually a bit of defensive programming, as
21064 tty_menu_display already limits the number of menu items to one
21065 less than the number of screen lines.) */
21066 if (y >= f->desired_matrix->nrows)
21067 return;
21068
21069 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21070 it.first_visible_x = 0;
21071 it.last_visible_x = FRAME_COLS (f) - 1;
21072 row = it.glyph_row;
21073 /* Start with the row contents from the current matrix. */
21074 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21075 saved_width = row->full_width_p;
21076 row->full_width_p = 1;
21077 saved_reversed = row->reversed_p;
21078 row->reversed_p = 0;
21079 row->enabled_p = true;
21080
21081 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21082 desired face. */
21083 eassert (x < f->desired_matrix->matrix_w);
21084 it.current_x = it.hpos = x;
21085 it.current_y = it.vpos = y;
21086 saved_used = row->used[TEXT_AREA];
21087 saved_truncated = row->truncated_on_right_p;
21088 row->used[TEXT_AREA] = x;
21089 it.face_id = face_id;
21090 it.line_wrap = TRUNCATE;
21091
21092 /* FIXME: This should be controlled by a user option. See the
21093 comments in redisplay_tool_bar and display_mode_line about this.
21094 Also, if paragraph_embedding could ever be R2L, changes will be
21095 needed to avoid shifting to the right the row characters in
21096 term.c:append_glyph. */
21097 it.paragraph_embedding = L2R;
21098
21099 /* Pad with a space on the left. */
21100 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21101 width--;
21102 /* Display the menu item, pad with spaces to WIDTH. */
21103 if (submenu)
21104 {
21105 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21106 item_len, 0, FRAME_COLS (f) - 1, -1);
21107 width -= item_len;
21108 /* Indicate with " >" that there's a submenu. */
21109 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21110 FRAME_COLS (f) - 1, -1);
21111 }
21112 else
21113 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21114 width, 0, FRAME_COLS (f) - 1, -1);
21115
21116 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21117 row->truncated_on_right_p = saved_truncated;
21118 row->hash = row_hash (row);
21119 row->full_width_p = saved_width;
21120 row->reversed_p = saved_reversed;
21121 }
21122 \f
21123 /***********************************************************************
21124 Mode Line
21125 ***********************************************************************/
21126
21127 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21128 FORCE is non-zero, redisplay mode lines unconditionally.
21129 Otherwise, redisplay only mode lines that are garbaged. Value is
21130 the number of windows whose mode lines were redisplayed. */
21131
21132 static int
21133 redisplay_mode_lines (Lisp_Object window, bool force)
21134 {
21135 int nwindows = 0;
21136
21137 while (!NILP (window))
21138 {
21139 struct window *w = XWINDOW (window);
21140
21141 if (WINDOWP (w->contents))
21142 nwindows += redisplay_mode_lines (w->contents, force);
21143 else if (force
21144 || FRAME_GARBAGED_P (XFRAME (w->frame))
21145 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21146 {
21147 struct text_pos lpoint;
21148 struct buffer *old = current_buffer;
21149
21150 /* Set the window's buffer for the mode line display. */
21151 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21152 set_buffer_internal_1 (XBUFFER (w->contents));
21153
21154 /* Point refers normally to the selected window. For any
21155 other window, set up appropriate value. */
21156 if (!EQ (window, selected_window))
21157 {
21158 struct text_pos pt;
21159
21160 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21161 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21162 }
21163
21164 /* Display mode lines. */
21165 clear_glyph_matrix (w->desired_matrix);
21166 if (display_mode_lines (w))
21167 ++nwindows;
21168
21169 /* Restore old settings. */
21170 set_buffer_internal_1 (old);
21171 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21172 }
21173
21174 window = w->next;
21175 }
21176
21177 return nwindows;
21178 }
21179
21180
21181 /* Display the mode and/or header line of window W. Value is the
21182 sum number of mode lines and header lines displayed. */
21183
21184 static int
21185 display_mode_lines (struct window *w)
21186 {
21187 Lisp_Object old_selected_window = selected_window;
21188 Lisp_Object old_selected_frame = selected_frame;
21189 Lisp_Object new_frame = w->frame;
21190 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21191 int n = 0;
21192
21193 selected_frame = new_frame;
21194 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21195 or window's point, then we'd need select_window_1 here as well. */
21196 XSETWINDOW (selected_window, w);
21197 XFRAME (new_frame)->selected_window = selected_window;
21198
21199 /* These will be set while the mode line specs are processed. */
21200 line_number_displayed = 0;
21201 w->column_number_displayed = -1;
21202
21203 if (WINDOW_WANTS_MODELINE_P (w))
21204 {
21205 struct window *sel_w = XWINDOW (old_selected_window);
21206
21207 /* Select mode line face based on the real selected window. */
21208 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21209 BVAR (current_buffer, mode_line_format));
21210 ++n;
21211 }
21212
21213 if (WINDOW_WANTS_HEADER_LINE_P (w))
21214 {
21215 display_mode_line (w, HEADER_LINE_FACE_ID,
21216 BVAR (current_buffer, header_line_format));
21217 ++n;
21218 }
21219
21220 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21221 selected_frame = old_selected_frame;
21222 selected_window = old_selected_window;
21223 if (n > 0)
21224 w->must_be_updated_p = true;
21225 return n;
21226 }
21227
21228
21229 /* Display mode or header line of window W. FACE_ID specifies which
21230 line to display; it is either MODE_LINE_FACE_ID or
21231 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21232 display. Value is the pixel height of the mode/header line
21233 displayed. */
21234
21235 static int
21236 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21237 {
21238 struct it it;
21239 struct face *face;
21240 ptrdiff_t count = SPECPDL_INDEX ();
21241
21242 init_iterator (&it, w, -1, -1, NULL, face_id);
21243 /* Don't extend on a previously drawn mode-line.
21244 This may happen if called from pos_visible_p. */
21245 it.glyph_row->enabled_p = false;
21246 prepare_desired_row (it.glyph_row);
21247
21248 it.glyph_row->mode_line_p = 1;
21249
21250 /* FIXME: This should be controlled by a user option. But
21251 supporting such an option is not trivial, since the mode line is
21252 made up of many separate strings. */
21253 it.paragraph_embedding = L2R;
21254
21255 record_unwind_protect (unwind_format_mode_line,
21256 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21257
21258 mode_line_target = MODE_LINE_DISPLAY;
21259
21260 /* Temporarily make frame's keyboard the current kboard so that
21261 kboard-local variables in the mode_line_format will get the right
21262 values. */
21263 push_kboard (FRAME_KBOARD (it.f));
21264 record_unwind_save_match_data ();
21265 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21266 pop_kboard ();
21267
21268 unbind_to (count, Qnil);
21269
21270 /* Fill up with spaces. */
21271 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21272
21273 compute_line_metrics (&it);
21274 it.glyph_row->full_width_p = 1;
21275 it.glyph_row->continued_p = 0;
21276 it.glyph_row->truncated_on_left_p = 0;
21277 it.glyph_row->truncated_on_right_p = 0;
21278
21279 /* Make a 3D mode-line have a shadow at its right end. */
21280 face = FACE_FROM_ID (it.f, face_id);
21281 extend_face_to_end_of_line (&it);
21282 if (face->box != FACE_NO_BOX)
21283 {
21284 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21285 + it.glyph_row->used[TEXT_AREA] - 1);
21286 last->right_box_line_p = 1;
21287 }
21288
21289 return it.glyph_row->height;
21290 }
21291
21292 /* Move element ELT in LIST to the front of LIST.
21293 Return the updated list. */
21294
21295 static Lisp_Object
21296 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21297 {
21298 register Lisp_Object tail, prev;
21299 register Lisp_Object tem;
21300
21301 tail = list;
21302 prev = Qnil;
21303 while (CONSP (tail))
21304 {
21305 tem = XCAR (tail);
21306
21307 if (EQ (elt, tem))
21308 {
21309 /* Splice out the link TAIL. */
21310 if (NILP (prev))
21311 list = XCDR (tail);
21312 else
21313 Fsetcdr (prev, XCDR (tail));
21314
21315 /* Now make it the first. */
21316 Fsetcdr (tail, list);
21317 return tail;
21318 }
21319 else
21320 prev = tail;
21321 tail = XCDR (tail);
21322 QUIT;
21323 }
21324
21325 /* Not found--return unchanged LIST. */
21326 return list;
21327 }
21328
21329 /* Contribute ELT to the mode line for window IT->w. How it
21330 translates into text depends on its data type.
21331
21332 IT describes the display environment in which we display, as usual.
21333
21334 DEPTH is the depth in recursion. It is used to prevent
21335 infinite recursion here.
21336
21337 FIELD_WIDTH is the number of characters the display of ELT should
21338 occupy in the mode line, and PRECISION is the maximum number of
21339 characters to display from ELT's representation. See
21340 display_string for details.
21341
21342 Returns the hpos of the end of the text generated by ELT.
21343
21344 PROPS is a property list to add to any string we encounter.
21345
21346 If RISKY is nonzero, remove (disregard) any properties in any string
21347 we encounter, and ignore :eval and :propertize.
21348
21349 The global variable `mode_line_target' determines whether the
21350 output is passed to `store_mode_line_noprop',
21351 `store_mode_line_string', or `display_string'. */
21352
21353 static int
21354 display_mode_element (struct it *it, int depth, int field_width, int precision,
21355 Lisp_Object elt, Lisp_Object props, int risky)
21356 {
21357 int n = 0, field, prec;
21358 int literal = 0;
21359
21360 tail_recurse:
21361 if (depth > 100)
21362 elt = build_string ("*too-deep*");
21363
21364 depth++;
21365
21366 switch (XTYPE (elt))
21367 {
21368 case Lisp_String:
21369 {
21370 /* A string: output it and check for %-constructs within it. */
21371 unsigned char c;
21372 ptrdiff_t offset = 0;
21373
21374 if (SCHARS (elt) > 0
21375 && (!NILP (props) || risky))
21376 {
21377 Lisp_Object oprops, aelt;
21378 oprops = Ftext_properties_at (make_number (0), elt);
21379
21380 /* If the starting string's properties are not what
21381 we want, translate the string. Also, if the string
21382 is risky, do that anyway. */
21383
21384 if (NILP (Fequal (props, oprops)) || risky)
21385 {
21386 /* If the starting string has properties,
21387 merge the specified ones onto the existing ones. */
21388 if (! NILP (oprops) && !risky)
21389 {
21390 Lisp_Object tem;
21391
21392 oprops = Fcopy_sequence (oprops);
21393 tem = props;
21394 while (CONSP (tem))
21395 {
21396 oprops = Fplist_put (oprops, XCAR (tem),
21397 XCAR (XCDR (tem)));
21398 tem = XCDR (XCDR (tem));
21399 }
21400 props = oprops;
21401 }
21402
21403 aelt = Fassoc (elt, mode_line_proptrans_alist);
21404 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21405 {
21406 /* AELT is what we want. Move it to the front
21407 without consing. */
21408 elt = XCAR (aelt);
21409 mode_line_proptrans_alist
21410 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21411 }
21412 else
21413 {
21414 Lisp_Object tem;
21415
21416 /* If AELT has the wrong props, it is useless.
21417 so get rid of it. */
21418 if (! NILP (aelt))
21419 mode_line_proptrans_alist
21420 = Fdelq (aelt, mode_line_proptrans_alist);
21421
21422 elt = Fcopy_sequence (elt);
21423 Fset_text_properties (make_number (0), Flength (elt),
21424 props, elt);
21425 /* Add this item to mode_line_proptrans_alist. */
21426 mode_line_proptrans_alist
21427 = Fcons (Fcons (elt, props),
21428 mode_line_proptrans_alist);
21429 /* Truncate mode_line_proptrans_alist
21430 to at most 50 elements. */
21431 tem = Fnthcdr (make_number (50),
21432 mode_line_proptrans_alist);
21433 if (! NILP (tem))
21434 XSETCDR (tem, Qnil);
21435 }
21436 }
21437 }
21438
21439 offset = 0;
21440
21441 if (literal)
21442 {
21443 prec = precision - n;
21444 switch (mode_line_target)
21445 {
21446 case MODE_LINE_NOPROP:
21447 case MODE_LINE_TITLE:
21448 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21449 break;
21450 case MODE_LINE_STRING:
21451 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21452 break;
21453 case MODE_LINE_DISPLAY:
21454 n += display_string (NULL, elt, Qnil, 0, 0, it,
21455 0, prec, 0, STRING_MULTIBYTE (elt));
21456 break;
21457 }
21458
21459 break;
21460 }
21461
21462 /* Handle the non-literal case. */
21463
21464 while ((precision <= 0 || n < precision)
21465 && SREF (elt, offset) != 0
21466 && (mode_line_target != MODE_LINE_DISPLAY
21467 || it->current_x < it->last_visible_x))
21468 {
21469 ptrdiff_t last_offset = offset;
21470
21471 /* Advance to end of string or next format specifier. */
21472 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21473 ;
21474
21475 if (offset - 1 != last_offset)
21476 {
21477 ptrdiff_t nchars, nbytes;
21478
21479 /* Output to end of string or up to '%'. Field width
21480 is length of string. Don't output more than
21481 PRECISION allows us. */
21482 offset--;
21483
21484 prec = c_string_width (SDATA (elt) + last_offset,
21485 offset - last_offset, precision - n,
21486 &nchars, &nbytes);
21487
21488 switch (mode_line_target)
21489 {
21490 case MODE_LINE_NOPROP:
21491 case MODE_LINE_TITLE:
21492 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21493 break;
21494 case MODE_LINE_STRING:
21495 {
21496 ptrdiff_t bytepos = last_offset;
21497 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21498 ptrdiff_t endpos = (precision <= 0
21499 ? string_byte_to_char (elt, offset)
21500 : charpos + nchars);
21501
21502 n += store_mode_line_string (NULL,
21503 Fsubstring (elt, make_number (charpos),
21504 make_number (endpos)),
21505 0, 0, 0, Qnil);
21506 }
21507 break;
21508 case MODE_LINE_DISPLAY:
21509 {
21510 ptrdiff_t bytepos = last_offset;
21511 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21512
21513 if (precision <= 0)
21514 nchars = string_byte_to_char (elt, offset) - charpos;
21515 n += display_string (NULL, elt, Qnil, 0, charpos,
21516 it, 0, nchars, 0,
21517 STRING_MULTIBYTE (elt));
21518 }
21519 break;
21520 }
21521 }
21522 else /* c == '%' */
21523 {
21524 ptrdiff_t percent_position = offset;
21525
21526 /* Get the specified minimum width. Zero means
21527 don't pad. */
21528 field = 0;
21529 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21530 field = field * 10 + c - '0';
21531
21532 /* Don't pad beyond the total padding allowed. */
21533 if (field_width - n > 0 && field > field_width - n)
21534 field = field_width - n;
21535
21536 /* Note that either PRECISION <= 0 or N < PRECISION. */
21537 prec = precision - n;
21538
21539 if (c == 'M')
21540 n += display_mode_element (it, depth, field, prec,
21541 Vglobal_mode_string, props,
21542 risky);
21543 else if (c != 0)
21544 {
21545 bool multibyte;
21546 ptrdiff_t bytepos, charpos;
21547 const char *spec;
21548 Lisp_Object string;
21549
21550 bytepos = percent_position;
21551 charpos = (STRING_MULTIBYTE (elt)
21552 ? string_byte_to_char (elt, bytepos)
21553 : bytepos);
21554 spec = decode_mode_spec (it->w, c, field, &string);
21555 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21556
21557 switch (mode_line_target)
21558 {
21559 case MODE_LINE_NOPROP:
21560 case MODE_LINE_TITLE:
21561 n += store_mode_line_noprop (spec, field, prec);
21562 break;
21563 case MODE_LINE_STRING:
21564 {
21565 Lisp_Object tem = build_string (spec);
21566 props = Ftext_properties_at (make_number (charpos), elt);
21567 /* Should only keep face property in props */
21568 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21569 }
21570 break;
21571 case MODE_LINE_DISPLAY:
21572 {
21573 int nglyphs_before, nwritten;
21574
21575 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21576 nwritten = display_string (spec, string, elt,
21577 charpos, 0, it,
21578 field, prec, 0,
21579 multibyte);
21580
21581 /* Assign to the glyphs written above the
21582 string where the `%x' came from, position
21583 of the `%'. */
21584 if (nwritten > 0)
21585 {
21586 struct glyph *glyph
21587 = (it->glyph_row->glyphs[TEXT_AREA]
21588 + nglyphs_before);
21589 int i;
21590
21591 for (i = 0; i < nwritten; ++i)
21592 {
21593 glyph[i].object = elt;
21594 glyph[i].charpos = charpos;
21595 }
21596
21597 n += nwritten;
21598 }
21599 }
21600 break;
21601 }
21602 }
21603 else /* c == 0 */
21604 break;
21605 }
21606 }
21607 }
21608 break;
21609
21610 case Lisp_Symbol:
21611 /* A symbol: process the value of the symbol recursively
21612 as if it appeared here directly. Avoid error if symbol void.
21613 Special case: if value of symbol is a string, output the string
21614 literally. */
21615 {
21616 register Lisp_Object tem;
21617
21618 /* If the variable is not marked as risky to set
21619 then its contents are risky to use. */
21620 if (NILP (Fget (elt, Qrisky_local_variable)))
21621 risky = 1;
21622
21623 tem = Fboundp (elt);
21624 if (!NILP (tem))
21625 {
21626 tem = Fsymbol_value (elt);
21627 /* If value is a string, output that string literally:
21628 don't check for % within it. */
21629 if (STRINGP (tem))
21630 literal = 1;
21631
21632 if (!EQ (tem, elt))
21633 {
21634 /* Give up right away for nil or t. */
21635 elt = tem;
21636 goto tail_recurse;
21637 }
21638 }
21639 }
21640 break;
21641
21642 case Lisp_Cons:
21643 {
21644 register Lisp_Object car, tem;
21645
21646 /* A cons cell: five distinct cases.
21647 If first element is :eval or :propertize, do something special.
21648 If first element is a string or a cons, process all the elements
21649 and effectively concatenate them.
21650 If first element is a negative number, truncate displaying cdr to
21651 at most that many characters. If positive, pad (with spaces)
21652 to at least that many characters.
21653 If first element is a symbol, process the cadr or caddr recursively
21654 according to whether the symbol's value is non-nil or nil. */
21655 car = XCAR (elt);
21656 if (EQ (car, QCeval))
21657 {
21658 /* An element of the form (:eval FORM) means evaluate FORM
21659 and use the result as mode line elements. */
21660
21661 if (risky)
21662 break;
21663
21664 if (CONSP (XCDR (elt)))
21665 {
21666 Lisp_Object spec;
21667 spec = safe_eval (XCAR (XCDR (elt)));
21668 n += display_mode_element (it, depth, field_width - n,
21669 precision - n, spec, props,
21670 risky);
21671 }
21672 }
21673 else if (EQ (car, QCpropertize))
21674 {
21675 /* An element of the form (:propertize ELT PROPS...)
21676 means display ELT but applying properties PROPS. */
21677
21678 if (risky)
21679 break;
21680
21681 if (CONSP (XCDR (elt)))
21682 n += display_mode_element (it, depth, field_width - n,
21683 precision - n, XCAR (XCDR (elt)),
21684 XCDR (XCDR (elt)), risky);
21685 }
21686 else if (SYMBOLP (car))
21687 {
21688 tem = Fboundp (car);
21689 elt = XCDR (elt);
21690 if (!CONSP (elt))
21691 goto invalid;
21692 /* elt is now the cdr, and we know it is a cons cell.
21693 Use its car if CAR has a non-nil value. */
21694 if (!NILP (tem))
21695 {
21696 tem = Fsymbol_value (car);
21697 if (!NILP (tem))
21698 {
21699 elt = XCAR (elt);
21700 goto tail_recurse;
21701 }
21702 }
21703 /* Symbol's value is nil (or symbol is unbound)
21704 Get the cddr of the original list
21705 and if possible find the caddr and use that. */
21706 elt = XCDR (elt);
21707 if (NILP (elt))
21708 break;
21709 else if (!CONSP (elt))
21710 goto invalid;
21711 elt = XCAR (elt);
21712 goto tail_recurse;
21713 }
21714 else if (INTEGERP (car))
21715 {
21716 register int lim = XINT (car);
21717 elt = XCDR (elt);
21718 if (lim < 0)
21719 {
21720 /* Negative int means reduce maximum width. */
21721 if (precision <= 0)
21722 precision = -lim;
21723 else
21724 precision = min (precision, -lim);
21725 }
21726 else if (lim > 0)
21727 {
21728 /* Padding specified. Don't let it be more than
21729 current maximum. */
21730 if (precision > 0)
21731 lim = min (precision, lim);
21732
21733 /* If that's more padding than already wanted, queue it.
21734 But don't reduce padding already specified even if
21735 that is beyond the current truncation point. */
21736 field_width = max (lim, field_width);
21737 }
21738 goto tail_recurse;
21739 }
21740 else if (STRINGP (car) || CONSP (car))
21741 {
21742 Lisp_Object halftail = elt;
21743 int len = 0;
21744
21745 while (CONSP (elt)
21746 && (precision <= 0 || n < precision))
21747 {
21748 n += display_mode_element (it, depth,
21749 /* Do padding only after the last
21750 element in the list. */
21751 (! CONSP (XCDR (elt))
21752 ? field_width - n
21753 : 0),
21754 precision - n, XCAR (elt),
21755 props, risky);
21756 elt = XCDR (elt);
21757 len++;
21758 if ((len & 1) == 0)
21759 halftail = XCDR (halftail);
21760 /* Check for cycle. */
21761 if (EQ (halftail, elt))
21762 break;
21763 }
21764 }
21765 }
21766 break;
21767
21768 default:
21769 invalid:
21770 elt = build_string ("*invalid*");
21771 goto tail_recurse;
21772 }
21773
21774 /* Pad to FIELD_WIDTH. */
21775 if (field_width > 0 && n < field_width)
21776 {
21777 switch (mode_line_target)
21778 {
21779 case MODE_LINE_NOPROP:
21780 case MODE_LINE_TITLE:
21781 n += store_mode_line_noprop ("", field_width - n, 0);
21782 break;
21783 case MODE_LINE_STRING:
21784 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21785 break;
21786 case MODE_LINE_DISPLAY:
21787 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21788 0, 0, 0);
21789 break;
21790 }
21791 }
21792
21793 return n;
21794 }
21795
21796 /* Store a mode-line string element in mode_line_string_list.
21797
21798 If STRING is non-null, display that C string. Otherwise, the Lisp
21799 string LISP_STRING is displayed.
21800
21801 FIELD_WIDTH is the minimum number of output glyphs to produce.
21802 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21803 with spaces. FIELD_WIDTH <= 0 means don't pad.
21804
21805 PRECISION is the maximum number of characters to output from
21806 STRING. PRECISION <= 0 means don't truncate the string.
21807
21808 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21809 properties to the string.
21810
21811 PROPS are the properties to add to the string.
21812 The mode_line_string_face face property is always added to the string.
21813 */
21814
21815 static int
21816 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21817 int field_width, int precision, Lisp_Object props)
21818 {
21819 ptrdiff_t len;
21820 int n = 0;
21821
21822 if (string != NULL)
21823 {
21824 len = strlen (string);
21825 if (precision > 0 && len > precision)
21826 len = precision;
21827 lisp_string = make_string (string, len);
21828 if (NILP (props))
21829 props = mode_line_string_face_prop;
21830 else if (!NILP (mode_line_string_face))
21831 {
21832 Lisp_Object face = Fplist_get (props, Qface);
21833 props = Fcopy_sequence (props);
21834 if (NILP (face))
21835 face = mode_line_string_face;
21836 else
21837 face = list2 (face, mode_line_string_face);
21838 props = Fplist_put (props, Qface, face);
21839 }
21840 Fadd_text_properties (make_number (0), make_number (len),
21841 props, lisp_string);
21842 }
21843 else
21844 {
21845 len = XFASTINT (Flength (lisp_string));
21846 if (precision > 0 && len > precision)
21847 {
21848 len = precision;
21849 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21850 precision = -1;
21851 }
21852 if (!NILP (mode_line_string_face))
21853 {
21854 Lisp_Object face;
21855 if (NILP (props))
21856 props = Ftext_properties_at (make_number (0), lisp_string);
21857 face = Fplist_get (props, Qface);
21858 if (NILP (face))
21859 face = mode_line_string_face;
21860 else
21861 face = list2 (face, mode_line_string_face);
21862 props = list2 (Qface, face);
21863 if (copy_string)
21864 lisp_string = Fcopy_sequence (lisp_string);
21865 }
21866 if (!NILP (props))
21867 Fadd_text_properties (make_number (0), make_number (len),
21868 props, lisp_string);
21869 }
21870
21871 if (len > 0)
21872 {
21873 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21874 n += len;
21875 }
21876
21877 if (field_width > len)
21878 {
21879 field_width -= len;
21880 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21881 if (!NILP (props))
21882 Fadd_text_properties (make_number (0), make_number (field_width),
21883 props, lisp_string);
21884 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21885 n += field_width;
21886 }
21887
21888 return n;
21889 }
21890
21891
21892 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21893 1, 4, 0,
21894 doc: /* Format a string out of a mode line format specification.
21895 First arg FORMAT specifies the mode line format (see `mode-line-format'
21896 for details) to use.
21897
21898 By default, the format is evaluated for the currently selected window.
21899
21900 Optional second arg FACE specifies the face property to put on all
21901 characters for which no face is specified. The value nil means the
21902 default face. The value t means whatever face the window's mode line
21903 currently uses (either `mode-line' or `mode-line-inactive',
21904 depending on whether the window is the selected window or not).
21905 An integer value means the value string has no text
21906 properties.
21907
21908 Optional third and fourth args WINDOW and BUFFER specify the window
21909 and buffer to use as the context for the formatting (defaults
21910 are the selected window and the WINDOW's buffer). */)
21911 (Lisp_Object format, Lisp_Object face,
21912 Lisp_Object window, Lisp_Object buffer)
21913 {
21914 struct it it;
21915 int len;
21916 struct window *w;
21917 struct buffer *old_buffer = NULL;
21918 int face_id;
21919 int no_props = INTEGERP (face);
21920 ptrdiff_t count = SPECPDL_INDEX ();
21921 Lisp_Object str;
21922 int string_start = 0;
21923
21924 w = decode_any_window (window);
21925 XSETWINDOW (window, w);
21926
21927 if (NILP (buffer))
21928 buffer = w->contents;
21929 CHECK_BUFFER (buffer);
21930
21931 /* Make formatting the modeline a non-op when noninteractive, otherwise
21932 there will be problems later caused by a partially initialized frame. */
21933 if (NILP (format) || noninteractive)
21934 return empty_unibyte_string;
21935
21936 if (no_props)
21937 face = Qnil;
21938
21939 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21940 : EQ (face, Qt) ? (EQ (window, selected_window)
21941 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21942 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21943 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21944 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21945 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21946 : DEFAULT_FACE_ID;
21947
21948 old_buffer = current_buffer;
21949
21950 /* Save things including mode_line_proptrans_alist,
21951 and set that to nil so that we don't alter the outer value. */
21952 record_unwind_protect (unwind_format_mode_line,
21953 format_mode_line_unwind_data
21954 (XFRAME (WINDOW_FRAME (w)),
21955 old_buffer, selected_window, 1));
21956 mode_line_proptrans_alist = Qnil;
21957
21958 Fselect_window (window, Qt);
21959 set_buffer_internal_1 (XBUFFER (buffer));
21960
21961 init_iterator (&it, w, -1, -1, NULL, face_id);
21962
21963 if (no_props)
21964 {
21965 mode_line_target = MODE_LINE_NOPROP;
21966 mode_line_string_face_prop = Qnil;
21967 mode_line_string_list = Qnil;
21968 string_start = MODE_LINE_NOPROP_LEN (0);
21969 }
21970 else
21971 {
21972 mode_line_target = MODE_LINE_STRING;
21973 mode_line_string_list = Qnil;
21974 mode_line_string_face = face;
21975 mode_line_string_face_prop
21976 = NILP (face) ? Qnil : list2 (Qface, face);
21977 }
21978
21979 push_kboard (FRAME_KBOARD (it.f));
21980 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21981 pop_kboard ();
21982
21983 if (no_props)
21984 {
21985 len = MODE_LINE_NOPROP_LEN (string_start);
21986 str = make_string (mode_line_noprop_buf + string_start, len);
21987 }
21988 else
21989 {
21990 mode_line_string_list = Fnreverse (mode_line_string_list);
21991 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21992 empty_unibyte_string);
21993 }
21994
21995 unbind_to (count, Qnil);
21996 return str;
21997 }
21998
21999 /* Write a null-terminated, right justified decimal representation of
22000 the positive integer D to BUF using a minimal field width WIDTH. */
22001
22002 static void
22003 pint2str (register char *buf, register int width, register ptrdiff_t d)
22004 {
22005 register char *p = buf;
22006
22007 if (d <= 0)
22008 *p++ = '0';
22009 else
22010 {
22011 while (d > 0)
22012 {
22013 *p++ = d % 10 + '0';
22014 d /= 10;
22015 }
22016 }
22017
22018 for (width -= (int) (p - buf); width > 0; --width)
22019 *p++ = ' ';
22020 *p-- = '\0';
22021 while (p > buf)
22022 {
22023 d = *buf;
22024 *buf++ = *p;
22025 *p-- = d;
22026 }
22027 }
22028
22029 /* Write a null-terminated, right justified decimal and "human
22030 readable" representation of the nonnegative integer D to BUF using
22031 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22032
22033 static const char power_letter[] =
22034 {
22035 0, /* no letter */
22036 'k', /* kilo */
22037 'M', /* mega */
22038 'G', /* giga */
22039 'T', /* tera */
22040 'P', /* peta */
22041 'E', /* exa */
22042 'Z', /* zetta */
22043 'Y' /* yotta */
22044 };
22045
22046 static void
22047 pint2hrstr (char *buf, int width, ptrdiff_t d)
22048 {
22049 /* We aim to represent the nonnegative integer D as
22050 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22051 ptrdiff_t quotient = d;
22052 int remainder = 0;
22053 /* -1 means: do not use TENTHS. */
22054 int tenths = -1;
22055 int exponent = 0;
22056
22057 /* Length of QUOTIENT.TENTHS as a string. */
22058 int length;
22059
22060 char * psuffix;
22061 char * p;
22062
22063 if (quotient >= 1000)
22064 {
22065 /* Scale to the appropriate EXPONENT. */
22066 do
22067 {
22068 remainder = quotient % 1000;
22069 quotient /= 1000;
22070 exponent++;
22071 }
22072 while (quotient >= 1000);
22073
22074 /* Round to nearest and decide whether to use TENTHS or not. */
22075 if (quotient <= 9)
22076 {
22077 tenths = remainder / 100;
22078 if (remainder % 100 >= 50)
22079 {
22080 if (tenths < 9)
22081 tenths++;
22082 else
22083 {
22084 quotient++;
22085 if (quotient == 10)
22086 tenths = -1;
22087 else
22088 tenths = 0;
22089 }
22090 }
22091 }
22092 else
22093 if (remainder >= 500)
22094 {
22095 if (quotient < 999)
22096 quotient++;
22097 else
22098 {
22099 quotient = 1;
22100 exponent++;
22101 tenths = 0;
22102 }
22103 }
22104 }
22105
22106 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22107 if (tenths == -1 && quotient <= 99)
22108 if (quotient <= 9)
22109 length = 1;
22110 else
22111 length = 2;
22112 else
22113 length = 3;
22114 p = psuffix = buf + max (width, length);
22115
22116 /* Print EXPONENT. */
22117 *psuffix++ = power_letter[exponent];
22118 *psuffix = '\0';
22119
22120 /* Print TENTHS. */
22121 if (tenths >= 0)
22122 {
22123 *--p = '0' + tenths;
22124 *--p = '.';
22125 }
22126
22127 /* Print QUOTIENT. */
22128 do
22129 {
22130 int digit = quotient % 10;
22131 *--p = '0' + digit;
22132 }
22133 while ((quotient /= 10) != 0);
22134
22135 /* Print leading spaces. */
22136 while (buf < p)
22137 *--p = ' ';
22138 }
22139
22140 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22141 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22142 type of CODING_SYSTEM. Return updated pointer into BUF. */
22143
22144 static unsigned char invalid_eol_type[] = "(*invalid*)";
22145
22146 static char *
22147 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22148 {
22149 Lisp_Object val;
22150 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22151 const unsigned char *eol_str;
22152 int eol_str_len;
22153 /* The EOL conversion we are using. */
22154 Lisp_Object eoltype;
22155
22156 val = CODING_SYSTEM_SPEC (coding_system);
22157 eoltype = Qnil;
22158
22159 if (!VECTORP (val)) /* Not yet decided. */
22160 {
22161 *buf++ = multibyte ? '-' : ' ';
22162 if (eol_flag)
22163 eoltype = eol_mnemonic_undecided;
22164 /* Don't mention EOL conversion if it isn't decided. */
22165 }
22166 else
22167 {
22168 Lisp_Object attrs;
22169 Lisp_Object eolvalue;
22170
22171 attrs = AREF (val, 0);
22172 eolvalue = AREF (val, 2);
22173
22174 *buf++ = multibyte
22175 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22176 : ' ';
22177
22178 if (eol_flag)
22179 {
22180 /* The EOL conversion that is normal on this system. */
22181
22182 if (NILP (eolvalue)) /* Not yet decided. */
22183 eoltype = eol_mnemonic_undecided;
22184 else if (VECTORP (eolvalue)) /* Not yet decided. */
22185 eoltype = eol_mnemonic_undecided;
22186 else /* eolvalue is Qunix, Qdos, or Qmac. */
22187 eoltype = (EQ (eolvalue, Qunix)
22188 ? eol_mnemonic_unix
22189 : (EQ (eolvalue, Qdos) == 1
22190 ? eol_mnemonic_dos : eol_mnemonic_mac));
22191 }
22192 }
22193
22194 if (eol_flag)
22195 {
22196 /* Mention the EOL conversion if it is not the usual one. */
22197 if (STRINGP (eoltype))
22198 {
22199 eol_str = SDATA (eoltype);
22200 eol_str_len = SBYTES (eoltype);
22201 }
22202 else if (CHARACTERP (eoltype))
22203 {
22204 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22205 int c = XFASTINT (eoltype);
22206 eol_str_len = CHAR_STRING (c, tmp);
22207 eol_str = tmp;
22208 }
22209 else
22210 {
22211 eol_str = invalid_eol_type;
22212 eol_str_len = sizeof (invalid_eol_type) - 1;
22213 }
22214 memcpy (buf, eol_str, eol_str_len);
22215 buf += eol_str_len;
22216 }
22217
22218 return buf;
22219 }
22220
22221 /* Return a string for the output of a mode line %-spec for window W,
22222 generated by character C. FIELD_WIDTH > 0 means pad the string
22223 returned with spaces to that value. Return a Lisp string in
22224 *STRING if the resulting string is taken from that Lisp string.
22225
22226 Note we operate on the current buffer for most purposes. */
22227
22228 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22229
22230 static const char *
22231 decode_mode_spec (struct window *w, register int c, int field_width,
22232 Lisp_Object *string)
22233 {
22234 Lisp_Object obj;
22235 struct frame *f = XFRAME (WINDOW_FRAME (w));
22236 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22237 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22238 produce strings from numerical values, so limit preposterously
22239 large values of FIELD_WIDTH to avoid overrunning the buffer's
22240 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22241 bytes plus the terminating null. */
22242 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22243 struct buffer *b = current_buffer;
22244
22245 obj = Qnil;
22246 *string = Qnil;
22247
22248 switch (c)
22249 {
22250 case '*':
22251 if (!NILP (BVAR (b, read_only)))
22252 return "%";
22253 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22254 return "*";
22255 return "-";
22256
22257 case '+':
22258 /* This differs from %* only for a modified read-only buffer. */
22259 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22260 return "*";
22261 if (!NILP (BVAR (b, read_only)))
22262 return "%";
22263 return "-";
22264
22265 case '&':
22266 /* This differs from %* in ignoring read-only-ness. */
22267 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22268 return "*";
22269 return "-";
22270
22271 case '%':
22272 return "%";
22273
22274 case '[':
22275 {
22276 int i;
22277 char *p;
22278
22279 if (command_loop_level > 5)
22280 return "[[[... ";
22281 p = decode_mode_spec_buf;
22282 for (i = 0; i < command_loop_level; i++)
22283 *p++ = '[';
22284 *p = 0;
22285 return decode_mode_spec_buf;
22286 }
22287
22288 case ']':
22289 {
22290 int i;
22291 char *p;
22292
22293 if (command_loop_level > 5)
22294 return " ...]]]";
22295 p = decode_mode_spec_buf;
22296 for (i = 0; i < command_loop_level; i++)
22297 *p++ = ']';
22298 *p = 0;
22299 return decode_mode_spec_buf;
22300 }
22301
22302 case '-':
22303 {
22304 register int i;
22305
22306 /* Let lots_of_dashes be a string of infinite length. */
22307 if (mode_line_target == MODE_LINE_NOPROP
22308 || mode_line_target == MODE_LINE_STRING)
22309 return "--";
22310 if (field_width <= 0
22311 || field_width > sizeof (lots_of_dashes))
22312 {
22313 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22314 decode_mode_spec_buf[i] = '-';
22315 decode_mode_spec_buf[i] = '\0';
22316 return decode_mode_spec_buf;
22317 }
22318 else
22319 return lots_of_dashes;
22320 }
22321
22322 case 'b':
22323 obj = BVAR (b, name);
22324 break;
22325
22326 case 'c':
22327 /* %c and %l are ignored in `frame-title-format'.
22328 (In redisplay_internal, the frame title is drawn _before_ the
22329 windows are updated, so the stuff which depends on actual
22330 window contents (such as %l) may fail to render properly, or
22331 even crash emacs.) */
22332 if (mode_line_target == MODE_LINE_TITLE)
22333 return "";
22334 else
22335 {
22336 ptrdiff_t col = current_column ();
22337 w->column_number_displayed = col;
22338 pint2str (decode_mode_spec_buf, width, col);
22339 return decode_mode_spec_buf;
22340 }
22341
22342 case 'e':
22343 #ifndef SYSTEM_MALLOC
22344 {
22345 if (NILP (Vmemory_full))
22346 return "";
22347 else
22348 return "!MEM FULL! ";
22349 }
22350 #else
22351 return "";
22352 #endif
22353
22354 case 'F':
22355 /* %F displays the frame name. */
22356 if (!NILP (f->title))
22357 return SSDATA (f->title);
22358 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22359 return SSDATA (f->name);
22360 return "Emacs";
22361
22362 case 'f':
22363 obj = BVAR (b, filename);
22364 break;
22365
22366 case 'i':
22367 {
22368 ptrdiff_t size = ZV - BEGV;
22369 pint2str (decode_mode_spec_buf, width, size);
22370 return decode_mode_spec_buf;
22371 }
22372
22373 case 'I':
22374 {
22375 ptrdiff_t size = ZV - BEGV;
22376 pint2hrstr (decode_mode_spec_buf, width, size);
22377 return decode_mode_spec_buf;
22378 }
22379
22380 case 'l':
22381 {
22382 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22383 ptrdiff_t topline, nlines, height;
22384 ptrdiff_t junk;
22385
22386 /* %c and %l are ignored in `frame-title-format'. */
22387 if (mode_line_target == MODE_LINE_TITLE)
22388 return "";
22389
22390 startpos = marker_position (w->start);
22391 startpos_byte = marker_byte_position (w->start);
22392 height = WINDOW_TOTAL_LINES (w);
22393
22394 /* If we decided that this buffer isn't suitable for line numbers,
22395 don't forget that too fast. */
22396 if (w->base_line_pos == -1)
22397 goto no_value;
22398
22399 /* If the buffer is very big, don't waste time. */
22400 if (INTEGERP (Vline_number_display_limit)
22401 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22402 {
22403 w->base_line_pos = 0;
22404 w->base_line_number = 0;
22405 goto no_value;
22406 }
22407
22408 if (w->base_line_number > 0
22409 && w->base_line_pos > 0
22410 && w->base_line_pos <= startpos)
22411 {
22412 line = w->base_line_number;
22413 linepos = w->base_line_pos;
22414 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22415 }
22416 else
22417 {
22418 line = 1;
22419 linepos = BUF_BEGV (b);
22420 linepos_byte = BUF_BEGV_BYTE (b);
22421 }
22422
22423 /* Count lines from base line to window start position. */
22424 nlines = display_count_lines (linepos_byte,
22425 startpos_byte,
22426 startpos, &junk);
22427
22428 topline = nlines + line;
22429
22430 /* Determine a new base line, if the old one is too close
22431 or too far away, or if we did not have one.
22432 "Too close" means it's plausible a scroll-down would
22433 go back past it. */
22434 if (startpos == BUF_BEGV (b))
22435 {
22436 w->base_line_number = topline;
22437 w->base_line_pos = BUF_BEGV (b);
22438 }
22439 else if (nlines < height + 25 || nlines > height * 3 + 50
22440 || linepos == BUF_BEGV (b))
22441 {
22442 ptrdiff_t limit = BUF_BEGV (b);
22443 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22444 ptrdiff_t position;
22445 ptrdiff_t distance =
22446 (height * 2 + 30) * line_number_display_limit_width;
22447
22448 if (startpos - distance > limit)
22449 {
22450 limit = startpos - distance;
22451 limit_byte = CHAR_TO_BYTE (limit);
22452 }
22453
22454 nlines = display_count_lines (startpos_byte,
22455 limit_byte,
22456 - (height * 2 + 30),
22457 &position);
22458 /* If we couldn't find the lines we wanted within
22459 line_number_display_limit_width chars per line,
22460 give up on line numbers for this window. */
22461 if (position == limit_byte && limit == startpos - distance)
22462 {
22463 w->base_line_pos = -1;
22464 w->base_line_number = 0;
22465 goto no_value;
22466 }
22467
22468 w->base_line_number = topline - nlines;
22469 w->base_line_pos = BYTE_TO_CHAR (position);
22470 }
22471
22472 /* Now count lines from the start pos to point. */
22473 nlines = display_count_lines (startpos_byte,
22474 PT_BYTE, PT, &junk);
22475
22476 /* Record that we did display the line number. */
22477 line_number_displayed = 1;
22478
22479 /* Make the string to show. */
22480 pint2str (decode_mode_spec_buf, width, topline + nlines);
22481 return decode_mode_spec_buf;
22482 no_value:
22483 {
22484 char* p = decode_mode_spec_buf;
22485 int pad = width - 2;
22486 while (pad-- > 0)
22487 *p++ = ' ';
22488 *p++ = '?';
22489 *p++ = '?';
22490 *p = '\0';
22491 return decode_mode_spec_buf;
22492 }
22493 }
22494 break;
22495
22496 case 'm':
22497 obj = BVAR (b, mode_name);
22498 break;
22499
22500 case 'n':
22501 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22502 return " Narrow";
22503 break;
22504
22505 case 'p':
22506 {
22507 ptrdiff_t pos = marker_position (w->start);
22508 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22509
22510 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22511 {
22512 if (pos <= BUF_BEGV (b))
22513 return "All";
22514 else
22515 return "Bottom";
22516 }
22517 else if (pos <= BUF_BEGV (b))
22518 return "Top";
22519 else
22520 {
22521 if (total > 1000000)
22522 /* Do it differently for a large value, to avoid overflow. */
22523 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22524 else
22525 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22526 /* We can't normally display a 3-digit number,
22527 so get us a 2-digit number that is close. */
22528 if (total == 100)
22529 total = 99;
22530 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22531 return decode_mode_spec_buf;
22532 }
22533 }
22534
22535 /* Display percentage of size above the bottom of the screen. */
22536 case 'P':
22537 {
22538 ptrdiff_t toppos = marker_position (w->start);
22539 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22540 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22541
22542 if (botpos >= BUF_ZV (b))
22543 {
22544 if (toppos <= BUF_BEGV (b))
22545 return "All";
22546 else
22547 return "Bottom";
22548 }
22549 else
22550 {
22551 if (total > 1000000)
22552 /* Do it differently for a large value, to avoid overflow. */
22553 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22554 else
22555 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22556 /* We can't normally display a 3-digit number,
22557 so get us a 2-digit number that is close. */
22558 if (total == 100)
22559 total = 99;
22560 if (toppos <= BUF_BEGV (b))
22561 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22562 else
22563 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22564 return decode_mode_spec_buf;
22565 }
22566 }
22567
22568 case 's':
22569 /* status of process */
22570 obj = Fget_buffer_process (Fcurrent_buffer ());
22571 if (NILP (obj))
22572 return "no process";
22573 #ifndef MSDOS
22574 obj = Fsymbol_name (Fprocess_status (obj));
22575 #endif
22576 break;
22577
22578 case '@':
22579 {
22580 ptrdiff_t count = inhibit_garbage_collection ();
22581 Lisp_Object val = call1 (intern ("file-remote-p"),
22582 BVAR (current_buffer, directory));
22583 unbind_to (count, Qnil);
22584
22585 if (NILP (val))
22586 return "-";
22587 else
22588 return "@";
22589 }
22590
22591 case 'z':
22592 /* coding-system (not including end-of-line format) */
22593 case 'Z':
22594 /* coding-system (including end-of-line type) */
22595 {
22596 int eol_flag = (c == 'Z');
22597 char *p = decode_mode_spec_buf;
22598
22599 if (! FRAME_WINDOW_P (f))
22600 {
22601 /* No need to mention EOL here--the terminal never needs
22602 to do EOL conversion. */
22603 p = decode_mode_spec_coding (CODING_ID_NAME
22604 (FRAME_KEYBOARD_CODING (f)->id),
22605 p, 0);
22606 p = decode_mode_spec_coding (CODING_ID_NAME
22607 (FRAME_TERMINAL_CODING (f)->id),
22608 p, 0);
22609 }
22610 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22611 p, eol_flag);
22612
22613 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22614 #ifdef subprocesses
22615 obj = Fget_buffer_process (Fcurrent_buffer ());
22616 if (PROCESSP (obj))
22617 {
22618 p = decode_mode_spec_coding
22619 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22620 p = decode_mode_spec_coding
22621 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22622 }
22623 #endif /* subprocesses */
22624 #endif /* 0 */
22625 *p = 0;
22626 return decode_mode_spec_buf;
22627 }
22628 }
22629
22630 if (STRINGP (obj))
22631 {
22632 *string = obj;
22633 return SSDATA (obj);
22634 }
22635 else
22636 return "";
22637 }
22638
22639
22640 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22641 means count lines back from START_BYTE. But don't go beyond
22642 LIMIT_BYTE. Return the number of lines thus found (always
22643 nonnegative).
22644
22645 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22646 either the position COUNT lines after/before START_BYTE, if we
22647 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22648 COUNT lines. */
22649
22650 static ptrdiff_t
22651 display_count_lines (ptrdiff_t start_byte,
22652 ptrdiff_t limit_byte, ptrdiff_t count,
22653 ptrdiff_t *byte_pos_ptr)
22654 {
22655 register unsigned char *cursor;
22656 unsigned char *base;
22657
22658 register ptrdiff_t ceiling;
22659 register unsigned char *ceiling_addr;
22660 ptrdiff_t orig_count = count;
22661
22662 /* If we are not in selective display mode,
22663 check only for newlines. */
22664 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22665 && !INTEGERP (BVAR (current_buffer, selective_display)));
22666
22667 if (count > 0)
22668 {
22669 while (start_byte < limit_byte)
22670 {
22671 ceiling = BUFFER_CEILING_OF (start_byte);
22672 ceiling = min (limit_byte - 1, ceiling);
22673 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22674 base = (cursor = BYTE_POS_ADDR (start_byte));
22675
22676 do
22677 {
22678 if (selective_display)
22679 {
22680 while (*cursor != '\n' && *cursor != 015
22681 && ++cursor != ceiling_addr)
22682 continue;
22683 if (cursor == ceiling_addr)
22684 break;
22685 }
22686 else
22687 {
22688 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22689 if (! cursor)
22690 break;
22691 }
22692
22693 cursor++;
22694
22695 if (--count == 0)
22696 {
22697 start_byte += cursor - base;
22698 *byte_pos_ptr = start_byte;
22699 return orig_count;
22700 }
22701 }
22702 while (cursor < ceiling_addr);
22703
22704 start_byte += ceiling_addr - base;
22705 }
22706 }
22707 else
22708 {
22709 while (start_byte > limit_byte)
22710 {
22711 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22712 ceiling = max (limit_byte, ceiling);
22713 ceiling_addr = BYTE_POS_ADDR (ceiling);
22714 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22715 while (1)
22716 {
22717 if (selective_display)
22718 {
22719 while (--cursor >= ceiling_addr
22720 && *cursor != '\n' && *cursor != 015)
22721 continue;
22722 if (cursor < ceiling_addr)
22723 break;
22724 }
22725 else
22726 {
22727 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22728 if (! cursor)
22729 break;
22730 }
22731
22732 if (++count == 0)
22733 {
22734 start_byte += cursor - base + 1;
22735 *byte_pos_ptr = start_byte;
22736 /* When scanning backwards, we should
22737 not count the newline posterior to which we stop. */
22738 return - orig_count - 1;
22739 }
22740 }
22741 start_byte += ceiling_addr - base;
22742 }
22743 }
22744
22745 *byte_pos_ptr = limit_byte;
22746
22747 if (count < 0)
22748 return - orig_count + count;
22749 return orig_count - count;
22750
22751 }
22752
22753
22754 \f
22755 /***********************************************************************
22756 Displaying strings
22757 ***********************************************************************/
22758
22759 /* Display a NUL-terminated string, starting with index START.
22760
22761 If STRING is non-null, display that C string. Otherwise, the Lisp
22762 string LISP_STRING is displayed. There's a case that STRING is
22763 non-null and LISP_STRING is not nil. It means STRING is a string
22764 data of LISP_STRING. In that case, we display LISP_STRING while
22765 ignoring its text properties.
22766
22767 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22768 FACE_STRING. Display STRING or LISP_STRING with the face at
22769 FACE_STRING_POS in FACE_STRING:
22770
22771 Display the string in the environment given by IT, but use the
22772 standard display table, temporarily.
22773
22774 FIELD_WIDTH is the minimum number of output glyphs to produce.
22775 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22776 with spaces. If STRING has more characters, more than FIELD_WIDTH
22777 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22778
22779 PRECISION is the maximum number of characters to output from
22780 STRING. PRECISION < 0 means don't truncate the string.
22781
22782 This is roughly equivalent to printf format specifiers:
22783
22784 FIELD_WIDTH PRECISION PRINTF
22785 ----------------------------------------
22786 -1 -1 %s
22787 -1 10 %.10s
22788 10 -1 %10s
22789 20 10 %20.10s
22790
22791 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22792 display them, and < 0 means obey the current buffer's value of
22793 enable_multibyte_characters.
22794
22795 Value is the number of columns displayed. */
22796
22797 static int
22798 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22799 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22800 int field_width, int precision, int max_x, int multibyte)
22801 {
22802 int hpos_at_start = it->hpos;
22803 int saved_face_id = it->face_id;
22804 struct glyph_row *row = it->glyph_row;
22805 ptrdiff_t it_charpos;
22806
22807 /* Initialize the iterator IT for iteration over STRING beginning
22808 with index START. */
22809 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22810 precision, field_width, multibyte);
22811 if (string && STRINGP (lisp_string))
22812 /* LISP_STRING is the one returned by decode_mode_spec. We should
22813 ignore its text properties. */
22814 it->stop_charpos = it->end_charpos;
22815
22816 /* If displaying STRING, set up the face of the iterator from
22817 FACE_STRING, if that's given. */
22818 if (STRINGP (face_string))
22819 {
22820 ptrdiff_t endptr;
22821 struct face *face;
22822
22823 it->face_id
22824 = face_at_string_position (it->w, face_string, face_string_pos,
22825 0, &endptr, it->base_face_id, 0);
22826 face = FACE_FROM_ID (it->f, it->face_id);
22827 it->face_box_p = face->box != FACE_NO_BOX;
22828 }
22829
22830 /* Set max_x to the maximum allowed X position. Don't let it go
22831 beyond the right edge of the window. */
22832 if (max_x <= 0)
22833 max_x = it->last_visible_x;
22834 else
22835 max_x = min (max_x, it->last_visible_x);
22836
22837 /* Skip over display elements that are not visible. because IT->w is
22838 hscrolled. */
22839 if (it->current_x < it->first_visible_x)
22840 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22841 MOVE_TO_POS | MOVE_TO_X);
22842
22843 row->ascent = it->max_ascent;
22844 row->height = it->max_ascent + it->max_descent;
22845 row->phys_ascent = it->max_phys_ascent;
22846 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22847 row->extra_line_spacing = it->max_extra_line_spacing;
22848
22849 if (STRINGP (it->string))
22850 it_charpos = IT_STRING_CHARPOS (*it);
22851 else
22852 it_charpos = IT_CHARPOS (*it);
22853
22854 /* This condition is for the case that we are called with current_x
22855 past last_visible_x. */
22856 while (it->current_x < max_x)
22857 {
22858 int x_before, x, n_glyphs_before, i, nglyphs;
22859
22860 /* Get the next display element. */
22861 if (!get_next_display_element (it))
22862 break;
22863
22864 /* Produce glyphs. */
22865 x_before = it->current_x;
22866 n_glyphs_before = row->used[TEXT_AREA];
22867 PRODUCE_GLYPHS (it);
22868
22869 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22870 i = 0;
22871 x = x_before;
22872 while (i < nglyphs)
22873 {
22874 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22875
22876 if (it->line_wrap != TRUNCATE
22877 && x + glyph->pixel_width > max_x)
22878 {
22879 /* End of continued line or max_x reached. */
22880 if (CHAR_GLYPH_PADDING_P (*glyph))
22881 {
22882 /* A wide character is unbreakable. */
22883 if (row->reversed_p)
22884 unproduce_glyphs (it, row->used[TEXT_AREA]
22885 - n_glyphs_before);
22886 row->used[TEXT_AREA] = n_glyphs_before;
22887 it->current_x = x_before;
22888 }
22889 else
22890 {
22891 if (row->reversed_p)
22892 unproduce_glyphs (it, row->used[TEXT_AREA]
22893 - (n_glyphs_before + i));
22894 row->used[TEXT_AREA] = n_glyphs_before + i;
22895 it->current_x = x;
22896 }
22897 break;
22898 }
22899 else if (x + glyph->pixel_width >= it->first_visible_x)
22900 {
22901 /* Glyph is at least partially visible. */
22902 ++it->hpos;
22903 if (x < it->first_visible_x)
22904 row->x = x - it->first_visible_x;
22905 }
22906 else
22907 {
22908 /* Glyph is off the left margin of the display area.
22909 Should not happen. */
22910 emacs_abort ();
22911 }
22912
22913 row->ascent = max (row->ascent, it->max_ascent);
22914 row->height = max (row->height, it->max_ascent + it->max_descent);
22915 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22916 row->phys_height = max (row->phys_height,
22917 it->max_phys_ascent + it->max_phys_descent);
22918 row->extra_line_spacing = max (row->extra_line_spacing,
22919 it->max_extra_line_spacing);
22920 x += glyph->pixel_width;
22921 ++i;
22922 }
22923
22924 /* Stop if max_x reached. */
22925 if (i < nglyphs)
22926 break;
22927
22928 /* Stop at line ends. */
22929 if (ITERATOR_AT_END_OF_LINE_P (it))
22930 {
22931 it->continuation_lines_width = 0;
22932 break;
22933 }
22934
22935 set_iterator_to_next (it, 1);
22936 if (STRINGP (it->string))
22937 it_charpos = IT_STRING_CHARPOS (*it);
22938 else
22939 it_charpos = IT_CHARPOS (*it);
22940
22941 /* Stop if truncating at the right edge. */
22942 if (it->line_wrap == TRUNCATE
22943 && it->current_x >= it->last_visible_x)
22944 {
22945 /* Add truncation mark, but don't do it if the line is
22946 truncated at a padding space. */
22947 if (it_charpos < it->string_nchars)
22948 {
22949 if (!FRAME_WINDOW_P (it->f))
22950 {
22951 int ii, n;
22952
22953 if (it->current_x > it->last_visible_x)
22954 {
22955 if (!row->reversed_p)
22956 {
22957 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22958 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22959 break;
22960 }
22961 else
22962 {
22963 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22964 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22965 break;
22966 unproduce_glyphs (it, ii + 1);
22967 ii = row->used[TEXT_AREA] - (ii + 1);
22968 }
22969 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22970 {
22971 row->used[TEXT_AREA] = ii;
22972 produce_special_glyphs (it, IT_TRUNCATION);
22973 }
22974 }
22975 produce_special_glyphs (it, IT_TRUNCATION);
22976 }
22977 row->truncated_on_right_p = 1;
22978 }
22979 break;
22980 }
22981 }
22982
22983 /* Maybe insert a truncation at the left. */
22984 if (it->first_visible_x
22985 && it_charpos > 0)
22986 {
22987 if (!FRAME_WINDOW_P (it->f)
22988 || (row->reversed_p
22989 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22990 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22991 insert_left_trunc_glyphs (it);
22992 row->truncated_on_left_p = 1;
22993 }
22994
22995 it->face_id = saved_face_id;
22996
22997 /* Value is number of columns displayed. */
22998 return it->hpos - hpos_at_start;
22999 }
23000
23001
23002 \f
23003 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23004 appears as an element of LIST or as the car of an element of LIST.
23005 If PROPVAL is a list, compare each element against LIST in that
23006 way, and return 1/2 if any element of PROPVAL is found in LIST.
23007 Otherwise return 0. This function cannot quit.
23008 The return value is 2 if the text is invisible but with an ellipsis
23009 and 1 if it's invisible and without an ellipsis. */
23010
23011 int
23012 invisible_p (register Lisp_Object propval, Lisp_Object list)
23013 {
23014 register Lisp_Object tail, proptail;
23015
23016 for (tail = list; CONSP (tail); tail = XCDR (tail))
23017 {
23018 register Lisp_Object tem;
23019 tem = XCAR (tail);
23020 if (EQ (propval, tem))
23021 return 1;
23022 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23023 return NILP (XCDR (tem)) ? 1 : 2;
23024 }
23025
23026 if (CONSP (propval))
23027 {
23028 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23029 {
23030 Lisp_Object propelt;
23031 propelt = XCAR (proptail);
23032 for (tail = list; CONSP (tail); tail = XCDR (tail))
23033 {
23034 register Lisp_Object tem;
23035 tem = XCAR (tail);
23036 if (EQ (propelt, tem))
23037 return 1;
23038 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23039 return NILP (XCDR (tem)) ? 1 : 2;
23040 }
23041 }
23042 }
23043
23044 return 0;
23045 }
23046
23047 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23048 doc: /* Non-nil if the property makes the text invisible.
23049 POS-OR-PROP can be a marker or number, in which case it is taken to be
23050 a position in the current buffer and the value of the `invisible' property
23051 is checked; or it can be some other value, which is then presumed to be the
23052 value of the `invisible' property of the text of interest.
23053 The non-nil value returned can be t for truly invisible text or something
23054 else if the text is replaced by an ellipsis. */)
23055 (Lisp_Object pos_or_prop)
23056 {
23057 Lisp_Object prop
23058 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23059 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23060 : pos_or_prop);
23061 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23062 return (invis == 0 ? Qnil
23063 : invis == 1 ? Qt
23064 : make_number (invis));
23065 }
23066
23067 /* Calculate a width or height in pixels from a specification using
23068 the following elements:
23069
23070 SPEC ::=
23071 NUM - a (fractional) multiple of the default font width/height
23072 (NUM) - specifies exactly NUM pixels
23073 UNIT - a fixed number of pixels, see below.
23074 ELEMENT - size of a display element in pixels, see below.
23075 (NUM . SPEC) - equals NUM * SPEC
23076 (+ SPEC SPEC ...) - add pixel values
23077 (- SPEC SPEC ...) - subtract pixel values
23078 (- SPEC) - negate pixel value
23079
23080 NUM ::=
23081 INT or FLOAT - a number constant
23082 SYMBOL - use symbol's (buffer local) variable binding.
23083
23084 UNIT ::=
23085 in - pixels per inch *)
23086 mm - pixels per 1/1000 meter *)
23087 cm - pixels per 1/100 meter *)
23088 width - width of current font in pixels.
23089 height - height of current font in pixels.
23090
23091 *) using the ratio(s) defined in display-pixels-per-inch.
23092
23093 ELEMENT ::=
23094
23095 left-fringe - left fringe width in pixels
23096 right-fringe - right fringe width in pixels
23097
23098 left-margin - left margin width in pixels
23099 right-margin - right margin width in pixels
23100
23101 scroll-bar - scroll-bar area width in pixels
23102
23103 Examples:
23104
23105 Pixels corresponding to 5 inches:
23106 (5 . in)
23107
23108 Total width of non-text areas on left side of window (if scroll-bar is on left):
23109 '(space :width (+ left-fringe left-margin scroll-bar))
23110
23111 Align to first text column (in header line):
23112 '(space :align-to 0)
23113
23114 Align to middle of text area minus half the width of variable `my-image'
23115 containing a loaded image:
23116 '(space :align-to (0.5 . (- text my-image)))
23117
23118 Width of left margin minus width of 1 character in the default font:
23119 '(space :width (- left-margin 1))
23120
23121 Width of left margin minus width of 2 characters in the current font:
23122 '(space :width (- left-margin (2 . width)))
23123
23124 Center 1 character over left-margin (in header line):
23125 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23126
23127 Different ways to express width of left fringe plus left margin minus one pixel:
23128 '(space :width (- (+ left-fringe left-margin) (1)))
23129 '(space :width (+ left-fringe left-margin (- (1))))
23130 '(space :width (+ left-fringe left-margin (-1)))
23131
23132 */
23133
23134 static int
23135 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23136 struct font *font, int width_p, int *align_to)
23137 {
23138 double pixels;
23139
23140 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23141 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23142
23143 if (NILP (prop))
23144 return OK_PIXELS (0);
23145
23146 eassert (FRAME_LIVE_P (it->f));
23147
23148 if (SYMBOLP (prop))
23149 {
23150 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23151 {
23152 char *unit = SSDATA (SYMBOL_NAME (prop));
23153
23154 if (unit[0] == 'i' && unit[1] == 'n')
23155 pixels = 1.0;
23156 else if (unit[0] == 'm' && unit[1] == 'm')
23157 pixels = 25.4;
23158 else if (unit[0] == 'c' && unit[1] == 'm')
23159 pixels = 2.54;
23160 else
23161 pixels = 0;
23162 if (pixels > 0)
23163 {
23164 double ppi = (width_p ? FRAME_RES_X (it->f)
23165 : FRAME_RES_Y (it->f));
23166
23167 if (ppi > 0)
23168 return OK_PIXELS (ppi / pixels);
23169 return 0;
23170 }
23171 }
23172
23173 #ifdef HAVE_WINDOW_SYSTEM
23174 if (EQ (prop, Qheight))
23175 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23176 if (EQ (prop, Qwidth))
23177 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23178 #else
23179 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23180 return OK_PIXELS (1);
23181 #endif
23182
23183 if (EQ (prop, Qtext))
23184 return OK_PIXELS (width_p
23185 ? window_box_width (it->w, TEXT_AREA)
23186 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23187
23188 if (align_to && *align_to < 0)
23189 {
23190 *res = 0;
23191 if (EQ (prop, Qleft))
23192 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23193 if (EQ (prop, Qright))
23194 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23195 if (EQ (prop, Qcenter))
23196 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23197 + window_box_width (it->w, TEXT_AREA) / 2);
23198 if (EQ (prop, Qleft_fringe))
23199 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23200 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23201 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23202 if (EQ (prop, Qright_fringe))
23203 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23204 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23205 : window_box_right_offset (it->w, TEXT_AREA));
23206 if (EQ (prop, Qleft_margin))
23207 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23208 if (EQ (prop, Qright_margin))
23209 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23210 if (EQ (prop, Qscroll_bar))
23211 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23212 ? 0
23213 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23214 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23215 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23216 : 0)));
23217 }
23218 else
23219 {
23220 if (EQ (prop, Qleft_fringe))
23221 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23222 if (EQ (prop, Qright_fringe))
23223 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23224 if (EQ (prop, Qleft_margin))
23225 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23226 if (EQ (prop, Qright_margin))
23227 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23228 if (EQ (prop, Qscroll_bar))
23229 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23230 }
23231
23232 prop = buffer_local_value_1 (prop, it->w->contents);
23233 if (EQ (prop, Qunbound))
23234 prop = Qnil;
23235 }
23236
23237 if (INTEGERP (prop) || FLOATP (prop))
23238 {
23239 int base_unit = (width_p
23240 ? FRAME_COLUMN_WIDTH (it->f)
23241 : FRAME_LINE_HEIGHT (it->f));
23242 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23243 }
23244
23245 if (CONSP (prop))
23246 {
23247 Lisp_Object car = XCAR (prop);
23248 Lisp_Object cdr = XCDR (prop);
23249
23250 if (SYMBOLP (car))
23251 {
23252 #ifdef HAVE_WINDOW_SYSTEM
23253 if (FRAME_WINDOW_P (it->f)
23254 && valid_image_p (prop))
23255 {
23256 ptrdiff_t id = lookup_image (it->f, prop);
23257 struct image *img = IMAGE_FROM_ID (it->f, id);
23258
23259 return OK_PIXELS (width_p ? img->width : img->height);
23260 }
23261 #endif
23262 if (EQ (car, Qplus) || EQ (car, Qminus))
23263 {
23264 int first = 1;
23265 double px;
23266
23267 pixels = 0;
23268 while (CONSP (cdr))
23269 {
23270 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23271 font, width_p, align_to))
23272 return 0;
23273 if (first)
23274 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23275 else
23276 pixels += px;
23277 cdr = XCDR (cdr);
23278 }
23279 if (EQ (car, Qminus))
23280 pixels = -pixels;
23281 return OK_PIXELS (pixels);
23282 }
23283
23284 car = buffer_local_value_1 (car, it->w->contents);
23285 if (EQ (car, Qunbound))
23286 car = Qnil;
23287 }
23288
23289 if (INTEGERP (car) || FLOATP (car))
23290 {
23291 double fact;
23292 pixels = XFLOATINT (car);
23293 if (NILP (cdr))
23294 return OK_PIXELS (pixels);
23295 if (calc_pixel_width_or_height (&fact, it, cdr,
23296 font, width_p, align_to))
23297 return OK_PIXELS (pixels * fact);
23298 return 0;
23299 }
23300
23301 return 0;
23302 }
23303
23304 return 0;
23305 }
23306
23307 \f
23308 /***********************************************************************
23309 Glyph Display
23310 ***********************************************************************/
23311
23312 #ifdef HAVE_WINDOW_SYSTEM
23313
23314 #ifdef GLYPH_DEBUG
23315
23316 void
23317 dump_glyph_string (struct glyph_string *s)
23318 {
23319 fprintf (stderr, "glyph string\n");
23320 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23321 s->x, s->y, s->width, s->height);
23322 fprintf (stderr, " ybase = %d\n", s->ybase);
23323 fprintf (stderr, " hl = %d\n", s->hl);
23324 fprintf (stderr, " left overhang = %d, right = %d\n",
23325 s->left_overhang, s->right_overhang);
23326 fprintf (stderr, " nchars = %d\n", s->nchars);
23327 fprintf (stderr, " extends to end of line = %d\n",
23328 s->extends_to_end_of_line_p);
23329 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23330 fprintf (stderr, " bg width = %d\n", s->background_width);
23331 }
23332
23333 #endif /* GLYPH_DEBUG */
23334
23335 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23336 of XChar2b structures for S; it can't be allocated in
23337 init_glyph_string because it must be allocated via `alloca'. W
23338 is the window on which S is drawn. ROW and AREA are the glyph row
23339 and area within the row from which S is constructed. START is the
23340 index of the first glyph structure covered by S. HL is a
23341 face-override for drawing S. */
23342
23343 #ifdef HAVE_NTGUI
23344 #define OPTIONAL_HDC(hdc) HDC hdc,
23345 #define DECLARE_HDC(hdc) HDC hdc;
23346 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23347 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23348 #endif
23349
23350 #ifndef OPTIONAL_HDC
23351 #define OPTIONAL_HDC(hdc)
23352 #define DECLARE_HDC(hdc)
23353 #define ALLOCATE_HDC(hdc, f)
23354 #define RELEASE_HDC(hdc, f)
23355 #endif
23356
23357 static void
23358 init_glyph_string (struct glyph_string *s,
23359 OPTIONAL_HDC (hdc)
23360 XChar2b *char2b, struct window *w, struct glyph_row *row,
23361 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23362 {
23363 memset (s, 0, sizeof *s);
23364 s->w = w;
23365 s->f = XFRAME (w->frame);
23366 #ifdef HAVE_NTGUI
23367 s->hdc = hdc;
23368 #endif
23369 s->display = FRAME_X_DISPLAY (s->f);
23370 s->window = FRAME_X_WINDOW (s->f);
23371 s->char2b = char2b;
23372 s->hl = hl;
23373 s->row = row;
23374 s->area = area;
23375 s->first_glyph = row->glyphs[area] + start;
23376 s->height = row->height;
23377 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23378 s->ybase = s->y + row->ascent;
23379 }
23380
23381
23382 /* Append the list of glyph strings with head H and tail T to the list
23383 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23384
23385 static void
23386 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23387 struct glyph_string *h, struct glyph_string *t)
23388 {
23389 if (h)
23390 {
23391 if (*head)
23392 (*tail)->next = h;
23393 else
23394 *head = h;
23395 h->prev = *tail;
23396 *tail = t;
23397 }
23398 }
23399
23400
23401 /* Prepend the list of glyph strings with head H and tail T to the
23402 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23403 result. */
23404
23405 static void
23406 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23407 struct glyph_string *h, struct glyph_string *t)
23408 {
23409 if (h)
23410 {
23411 if (*head)
23412 (*head)->prev = t;
23413 else
23414 *tail = t;
23415 t->next = *head;
23416 *head = h;
23417 }
23418 }
23419
23420
23421 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23422 Set *HEAD and *TAIL to the resulting list. */
23423
23424 static void
23425 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23426 struct glyph_string *s)
23427 {
23428 s->next = s->prev = NULL;
23429 append_glyph_string_lists (head, tail, s, s);
23430 }
23431
23432
23433 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23434 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23435 make sure that X resources for the face returned are allocated.
23436 Value is a pointer to a realized face that is ready for display if
23437 DISPLAY_P is non-zero. */
23438
23439 static struct face *
23440 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23441 XChar2b *char2b, int display_p)
23442 {
23443 struct face *face = FACE_FROM_ID (f, face_id);
23444 unsigned code = 0;
23445
23446 if (face->font)
23447 {
23448 code = face->font->driver->encode_char (face->font, c);
23449
23450 if (code == FONT_INVALID_CODE)
23451 code = 0;
23452 }
23453 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23454
23455 /* Make sure X resources of the face are allocated. */
23456 #ifdef HAVE_X_WINDOWS
23457 if (display_p)
23458 #endif
23459 {
23460 eassert (face != NULL);
23461 PREPARE_FACE_FOR_DISPLAY (f, face);
23462 }
23463
23464 return face;
23465 }
23466
23467
23468 /* Get face and two-byte form of character glyph GLYPH on frame F.
23469 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23470 a pointer to a realized face that is ready for display. */
23471
23472 static struct face *
23473 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23474 XChar2b *char2b, int *two_byte_p)
23475 {
23476 struct face *face;
23477 unsigned code = 0;
23478
23479 eassert (glyph->type == CHAR_GLYPH);
23480 face = FACE_FROM_ID (f, glyph->face_id);
23481
23482 /* Make sure X resources of the face are allocated. */
23483 eassert (face != NULL);
23484 PREPARE_FACE_FOR_DISPLAY (f, face);
23485
23486 if (two_byte_p)
23487 *two_byte_p = 0;
23488
23489 if (face->font)
23490 {
23491 if (CHAR_BYTE8_P (glyph->u.ch))
23492 code = CHAR_TO_BYTE8 (glyph->u.ch);
23493 else
23494 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23495
23496 if (code == FONT_INVALID_CODE)
23497 code = 0;
23498 }
23499
23500 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23501 return face;
23502 }
23503
23504
23505 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23506 Return 1 if FONT has a glyph for C, otherwise return 0. */
23507
23508 static int
23509 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23510 {
23511 unsigned code;
23512
23513 if (CHAR_BYTE8_P (c))
23514 code = CHAR_TO_BYTE8 (c);
23515 else
23516 code = font->driver->encode_char (font, c);
23517
23518 if (code == FONT_INVALID_CODE)
23519 return 0;
23520 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23521 return 1;
23522 }
23523
23524
23525 /* Fill glyph string S with composition components specified by S->cmp.
23526
23527 BASE_FACE is the base face of the composition.
23528 S->cmp_from is the index of the first component for S.
23529
23530 OVERLAPS non-zero means S should draw the foreground only, and use
23531 its physical height for clipping. See also draw_glyphs.
23532
23533 Value is the index of a component not in S. */
23534
23535 static int
23536 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23537 int overlaps)
23538 {
23539 int i;
23540 /* For all glyphs of this composition, starting at the offset
23541 S->cmp_from, until we reach the end of the definition or encounter a
23542 glyph that requires the different face, add it to S. */
23543 struct face *face;
23544
23545 eassert (s);
23546
23547 s->for_overlaps = overlaps;
23548 s->face = NULL;
23549 s->font = NULL;
23550 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23551 {
23552 int c = COMPOSITION_GLYPH (s->cmp, i);
23553
23554 /* TAB in a composition means display glyphs with padding space
23555 on the left or right. */
23556 if (c != '\t')
23557 {
23558 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23559 -1, Qnil);
23560
23561 face = get_char_face_and_encoding (s->f, c, face_id,
23562 s->char2b + i, 1);
23563 if (face)
23564 {
23565 if (! s->face)
23566 {
23567 s->face = face;
23568 s->font = s->face->font;
23569 }
23570 else if (s->face != face)
23571 break;
23572 }
23573 }
23574 ++s->nchars;
23575 }
23576 s->cmp_to = i;
23577
23578 if (s->face == NULL)
23579 {
23580 s->face = base_face->ascii_face;
23581 s->font = s->face->font;
23582 }
23583
23584 /* All glyph strings for the same composition has the same width,
23585 i.e. the width set for the first component of the composition. */
23586 s->width = s->first_glyph->pixel_width;
23587
23588 /* If the specified font could not be loaded, use the frame's
23589 default font, but record the fact that we couldn't load it in
23590 the glyph string so that we can draw rectangles for the
23591 characters of the glyph string. */
23592 if (s->font == NULL)
23593 {
23594 s->font_not_found_p = 1;
23595 s->font = FRAME_FONT (s->f);
23596 }
23597
23598 /* Adjust base line for subscript/superscript text. */
23599 s->ybase += s->first_glyph->voffset;
23600
23601 /* This glyph string must always be drawn with 16-bit functions. */
23602 s->two_byte_p = 1;
23603
23604 return s->cmp_to;
23605 }
23606
23607 static int
23608 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23609 int start, int end, int overlaps)
23610 {
23611 struct glyph *glyph, *last;
23612 Lisp_Object lgstring;
23613 int i;
23614
23615 s->for_overlaps = overlaps;
23616 glyph = s->row->glyphs[s->area] + start;
23617 last = s->row->glyphs[s->area] + end;
23618 s->cmp_id = glyph->u.cmp.id;
23619 s->cmp_from = glyph->slice.cmp.from;
23620 s->cmp_to = glyph->slice.cmp.to + 1;
23621 s->face = FACE_FROM_ID (s->f, face_id);
23622 lgstring = composition_gstring_from_id (s->cmp_id);
23623 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23624 glyph++;
23625 while (glyph < last
23626 && glyph->u.cmp.automatic
23627 && glyph->u.cmp.id == s->cmp_id
23628 && s->cmp_to == glyph->slice.cmp.from)
23629 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23630
23631 for (i = s->cmp_from; i < s->cmp_to; i++)
23632 {
23633 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23634 unsigned code = LGLYPH_CODE (lglyph);
23635
23636 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23637 }
23638 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23639 return glyph - s->row->glyphs[s->area];
23640 }
23641
23642
23643 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23644 See the comment of fill_glyph_string for arguments.
23645 Value is the index of the first glyph not in S. */
23646
23647
23648 static int
23649 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23650 int start, int end, int overlaps)
23651 {
23652 struct glyph *glyph, *last;
23653 int voffset;
23654
23655 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23656 s->for_overlaps = overlaps;
23657 glyph = s->row->glyphs[s->area] + start;
23658 last = s->row->glyphs[s->area] + end;
23659 voffset = glyph->voffset;
23660 s->face = FACE_FROM_ID (s->f, face_id);
23661 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23662 s->nchars = 1;
23663 s->width = glyph->pixel_width;
23664 glyph++;
23665 while (glyph < last
23666 && glyph->type == GLYPHLESS_GLYPH
23667 && glyph->voffset == voffset
23668 && glyph->face_id == face_id)
23669 {
23670 s->nchars++;
23671 s->width += glyph->pixel_width;
23672 glyph++;
23673 }
23674 s->ybase += voffset;
23675 return glyph - s->row->glyphs[s->area];
23676 }
23677
23678
23679 /* Fill glyph string S from a sequence of character glyphs.
23680
23681 FACE_ID is the face id of the string. START is the index of the
23682 first glyph to consider, END is the index of the last + 1.
23683 OVERLAPS non-zero means S should draw the foreground only, and use
23684 its physical height for clipping. See also draw_glyphs.
23685
23686 Value is the index of the first glyph not in S. */
23687
23688 static int
23689 fill_glyph_string (struct glyph_string *s, int face_id,
23690 int start, int end, int overlaps)
23691 {
23692 struct glyph *glyph, *last;
23693 int voffset;
23694 int glyph_not_available_p;
23695
23696 eassert (s->f == XFRAME (s->w->frame));
23697 eassert (s->nchars == 0);
23698 eassert (start >= 0 && end > start);
23699
23700 s->for_overlaps = overlaps;
23701 glyph = s->row->glyphs[s->area] + start;
23702 last = s->row->glyphs[s->area] + end;
23703 voffset = glyph->voffset;
23704 s->padding_p = glyph->padding_p;
23705 glyph_not_available_p = glyph->glyph_not_available_p;
23706
23707 while (glyph < last
23708 && glyph->type == CHAR_GLYPH
23709 && glyph->voffset == voffset
23710 /* Same face id implies same font, nowadays. */
23711 && glyph->face_id == face_id
23712 && glyph->glyph_not_available_p == glyph_not_available_p)
23713 {
23714 int two_byte_p;
23715
23716 s->face = get_glyph_face_and_encoding (s->f, glyph,
23717 s->char2b + s->nchars,
23718 &two_byte_p);
23719 s->two_byte_p = two_byte_p;
23720 ++s->nchars;
23721 eassert (s->nchars <= end - start);
23722 s->width += glyph->pixel_width;
23723 if (glyph++->padding_p != s->padding_p)
23724 break;
23725 }
23726
23727 s->font = s->face->font;
23728
23729 /* If the specified font could not be loaded, use the frame's font,
23730 but record the fact that we couldn't load it in
23731 S->font_not_found_p so that we can draw rectangles for the
23732 characters of the glyph string. */
23733 if (s->font == NULL || glyph_not_available_p)
23734 {
23735 s->font_not_found_p = 1;
23736 s->font = FRAME_FONT (s->f);
23737 }
23738
23739 /* Adjust base line for subscript/superscript text. */
23740 s->ybase += voffset;
23741
23742 eassert (s->face && s->face->gc);
23743 return glyph - s->row->glyphs[s->area];
23744 }
23745
23746
23747 /* Fill glyph string S from image glyph S->first_glyph. */
23748
23749 static void
23750 fill_image_glyph_string (struct glyph_string *s)
23751 {
23752 eassert (s->first_glyph->type == IMAGE_GLYPH);
23753 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23754 eassert (s->img);
23755 s->slice = s->first_glyph->slice.img;
23756 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23757 s->font = s->face->font;
23758 s->width = s->first_glyph->pixel_width;
23759
23760 /* Adjust base line for subscript/superscript text. */
23761 s->ybase += s->first_glyph->voffset;
23762 }
23763
23764
23765 /* Fill glyph string S from a sequence of stretch glyphs.
23766
23767 START is the index of the first glyph to consider,
23768 END is the index of the last + 1.
23769
23770 Value is the index of the first glyph not in S. */
23771
23772 static int
23773 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23774 {
23775 struct glyph *glyph, *last;
23776 int voffset, face_id;
23777
23778 eassert (s->first_glyph->type == STRETCH_GLYPH);
23779
23780 glyph = s->row->glyphs[s->area] + start;
23781 last = s->row->glyphs[s->area] + end;
23782 face_id = glyph->face_id;
23783 s->face = FACE_FROM_ID (s->f, face_id);
23784 s->font = s->face->font;
23785 s->width = glyph->pixel_width;
23786 s->nchars = 1;
23787 voffset = glyph->voffset;
23788
23789 for (++glyph;
23790 (glyph < last
23791 && glyph->type == STRETCH_GLYPH
23792 && glyph->voffset == voffset
23793 && glyph->face_id == face_id);
23794 ++glyph)
23795 s->width += glyph->pixel_width;
23796
23797 /* Adjust base line for subscript/superscript text. */
23798 s->ybase += voffset;
23799
23800 /* The case that face->gc == 0 is handled when drawing the glyph
23801 string by calling PREPARE_FACE_FOR_DISPLAY. */
23802 eassert (s->face);
23803 return glyph - s->row->glyphs[s->area];
23804 }
23805
23806 static struct font_metrics *
23807 get_per_char_metric (struct font *font, XChar2b *char2b)
23808 {
23809 static struct font_metrics metrics;
23810 unsigned code;
23811
23812 if (! font)
23813 return NULL;
23814 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23815 if (code == FONT_INVALID_CODE)
23816 return NULL;
23817 font->driver->text_extents (font, &code, 1, &metrics);
23818 return &metrics;
23819 }
23820
23821 /* EXPORT for RIF:
23822 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23823 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23824 assumed to be zero. */
23825
23826 void
23827 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23828 {
23829 *left = *right = 0;
23830
23831 if (glyph->type == CHAR_GLYPH)
23832 {
23833 struct face *face;
23834 XChar2b char2b;
23835 struct font_metrics *pcm;
23836
23837 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23838 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23839 {
23840 if (pcm->rbearing > pcm->width)
23841 *right = pcm->rbearing - pcm->width;
23842 if (pcm->lbearing < 0)
23843 *left = -pcm->lbearing;
23844 }
23845 }
23846 else if (glyph->type == COMPOSITE_GLYPH)
23847 {
23848 if (! glyph->u.cmp.automatic)
23849 {
23850 struct composition *cmp = composition_table[glyph->u.cmp.id];
23851
23852 if (cmp->rbearing > cmp->pixel_width)
23853 *right = cmp->rbearing - cmp->pixel_width;
23854 if (cmp->lbearing < 0)
23855 *left = - cmp->lbearing;
23856 }
23857 else
23858 {
23859 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23860 struct font_metrics metrics;
23861
23862 composition_gstring_width (gstring, glyph->slice.cmp.from,
23863 glyph->slice.cmp.to + 1, &metrics);
23864 if (metrics.rbearing > metrics.width)
23865 *right = metrics.rbearing - metrics.width;
23866 if (metrics.lbearing < 0)
23867 *left = - metrics.lbearing;
23868 }
23869 }
23870 }
23871
23872
23873 /* Return the index of the first glyph preceding glyph string S that
23874 is overwritten by S because of S's left overhang. Value is -1
23875 if no glyphs are overwritten. */
23876
23877 static int
23878 left_overwritten (struct glyph_string *s)
23879 {
23880 int k;
23881
23882 if (s->left_overhang)
23883 {
23884 int x = 0, i;
23885 struct glyph *glyphs = s->row->glyphs[s->area];
23886 int first = s->first_glyph - glyphs;
23887
23888 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23889 x -= glyphs[i].pixel_width;
23890
23891 k = i + 1;
23892 }
23893 else
23894 k = -1;
23895
23896 return k;
23897 }
23898
23899
23900 /* Return the index of the first glyph preceding glyph string S that
23901 is overwriting S because of its right overhang. Value is -1 if no
23902 glyph in front of S overwrites S. */
23903
23904 static int
23905 left_overwriting (struct glyph_string *s)
23906 {
23907 int i, k, x;
23908 struct glyph *glyphs = s->row->glyphs[s->area];
23909 int first = s->first_glyph - glyphs;
23910
23911 k = -1;
23912 x = 0;
23913 for (i = first - 1; i >= 0; --i)
23914 {
23915 int left, right;
23916 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23917 if (x + right > 0)
23918 k = i;
23919 x -= glyphs[i].pixel_width;
23920 }
23921
23922 return k;
23923 }
23924
23925
23926 /* Return the index of the last glyph following glyph string S that is
23927 overwritten by S because of S's right overhang. Value is -1 if
23928 no such glyph is found. */
23929
23930 static int
23931 right_overwritten (struct glyph_string *s)
23932 {
23933 int k = -1;
23934
23935 if (s->right_overhang)
23936 {
23937 int x = 0, i;
23938 struct glyph *glyphs = s->row->glyphs[s->area];
23939 int first = (s->first_glyph - glyphs
23940 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23941 int end = s->row->used[s->area];
23942
23943 for (i = first; i < end && s->right_overhang > x; ++i)
23944 x += glyphs[i].pixel_width;
23945
23946 k = i;
23947 }
23948
23949 return k;
23950 }
23951
23952
23953 /* Return the index of the last glyph following glyph string S that
23954 overwrites S because of its left overhang. Value is negative
23955 if no such glyph is found. */
23956
23957 static int
23958 right_overwriting (struct glyph_string *s)
23959 {
23960 int i, k, x;
23961 int end = s->row->used[s->area];
23962 struct glyph *glyphs = s->row->glyphs[s->area];
23963 int first = (s->first_glyph - glyphs
23964 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23965
23966 k = -1;
23967 x = 0;
23968 for (i = first; i < end; ++i)
23969 {
23970 int left, right;
23971 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23972 if (x - left < 0)
23973 k = i;
23974 x += glyphs[i].pixel_width;
23975 }
23976
23977 return k;
23978 }
23979
23980
23981 /* Set background width of glyph string S. START is the index of the
23982 first glyph following S. LAST_X is the right-most x-position + 1
23983 in the drawing area. */
23984
23985 static void
23986 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23987 {
23988 /* If the face of this glyph string has to be drawn to the end of
23989 the drawing area, set S->extends_to_end_of_line_p. */
23990
23991 if (start == s->row->used[s->area]
23992 && ((s->row->fill_line_p
23993 && (s->hl == DRAW_NORMAL_TEXT
23994 || s->hl == DRAW_IMAGE_RAISED
23995 || s->hl == DRAW_IMAGE_SUNKEN))
23996 || s->hl == DRAW_MOUSE_FACE))
23997 s->extends_to_end_of_line_p = 1;
23998
23999 /* If S extends its face to the end of the line, set its
24000 background_width to the distance to the right edge of the drawing
24001 area. */
24002 if (s->extends_to_end_of_line_p)
24003 s->background_width = last_x - s->x + 1;
24004 else
24005 s->background_width = s->width;
24006 }
24007
24008
24009 /* Compute overhangs and x-positions for glyph string S and its
24010 predecessors, or successors. X is the starting x-position for S.
24011 BACKWARD_P non-zero means process predecessors. */
24012
24013 static void
24014 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24015 {
24016 if (backward_p)
24017 {
24018 while (s)
24019 {
24020 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24021 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24022 x -= s->width;
24023 s->x = x;
24024 s = s->prev;
24025 }
24026 }
24027 else
24028 {
24029 while (s)
24030 {
24031 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24032 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24033 s->x = x;
24034 x += s->width;
24035 s = s->next;
24036 }
24037 }
24038 }
24039
24040
24041
24042 /* The following macros are only called from draw_glyphs below.
24043 They reference the following parameters of that function directly:
24044 `w', `row', `area', and `overlap_p'
24045 as well as the following local variables:
24046 `s', `f', and `hdc' (in W32) */
24047
24048 #ifdef HAVE_NTGUI
24049 /* On W32, silently add local `hdc' variable to argument list of
24050 init_glyph_string. */
24051 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24052 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24053 #else
24054 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24055 init_glyph_string (s, char2b, w, row, area, start, hl)
24056 #endif
24057
24058 /* Add a glyph string for a stretch glyph to the list of strings
24059 between HEAD and TAIL. START is the index of the stretch glyph in
24060 row area AREA of glyph row ROW. END is the index of the last glyph
24061 in that glyph row area. X is the current output position assigned
24062 to the new glyph string constructed. HL overrides that face of the
24063 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24064 is the right-most x-position of the drawing area. */
24065
24066 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24067 and below -- keep them on one line. */
24068 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24069 do \
24070 { \
24071 s = alloca (sizeof *s); \
24072 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24073 START = fill_stretch_glyph_string (s, START, END); \
24074 append_glyph_string (&HEAD, &TAIL, s); \
24075 s->x = (X); \
24076 } \
24077 while (0)
24078
24079
24080 /* Add a glyph string for an image glyph to the list of strings
24081 between HEAD and TAIL. START is the index of the image glyph in
24082 row area AREA of glyph row ROW. END is the index of the last glyph
24083 in that glyph row area. X is the current output position assigned
24084 to the new glyph string constructed. HL overrides that face of the
24085 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24086 is the right-most x-position of the drawing area. */
24087
24088 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24089 do \
24090 { \
24091 s = alloca (sizeof *s); \
24092 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24093 fill_image_glyph_string (s); \
24094 append_glyph_string (&HEAD, &TAIL, s); \
24095 ++START; \
24096 s->x = (X); \
24097 } \
24098 while (0)
24099
24100
24101 /* Add a glyph string for a sequence of character glyphs to the list
24102 of strings between HEAD and TAIL. START is the index of the first
24103 glyph in row area AREA of glyph row ROW that is part of the new
24104 glyph string. END is the index of the last glyph in that glyph row
24105 area. X is the current output position assigned to the new glyph
24106 string constructed. HL overrides that face of the glyph; e.g. it
24107 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24108 right-most x-position of the drawing area. */
24109
24110 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24111 do \
24112 { \
24113 int face_id; \
24114 XChar2b *char2b; \
24115 \
24116 face_id = (row)->glyphs[area][START].face_id; \
24117 \
24118 s = alloca (sizeof *s); \
24119 char2b = alloca ((END - START) * sizeof *char2b); \
24120 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24121 append_glyph_string (&HEAD, &TAIL, s); \
24122 s->x = (X); \
24123 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24124 } \
24125 while (0)
24126
24127
24128 /* Add a glyph string for a composite sequence to the list of strings
24129 between HEAD and TAIL. START is the index of the first glyph in
24130 row area AREA of glyph row ROW that is part of the new glyph
24131 string. END is the index of the last glyph in that glyph row area.
24132 X is the current output position assigned to the new glyph string
24133 constructed. HL overrides that face of the glyph; e.g. it is
24134 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24135 x-position of the drawing area. */
24136
24137 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24138 do { \
24139 int face_id = (row)->glyphs[area][START].face_id; \
24140 struct face *base_face = FACE_FROM_ID (f, face_id); \
24141 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24142 struct composition *cmp = composition_table[cmp_id]; \
24143 XChar2b *char2b; \
24144 struct glyph_string *first_s = NULL; \
24145 int n; \
24146 \
24147 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24148 \
24149 /* Make glyph_strings for each glyph sequence that is drawable by \
24150 the same face, and append them to HEAD/TAIL. */ \
24151 for (n = 0; n < cmp->glyph_len;) \
24152 { \
24153 s = alloca (sizeof *s); \
24154 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24155 append_glyph_string (&(HEAD), &(TAIL), s); \
24156 s->cmp = cmp; \
24157 s->cmp_from = n; \
24158 s->x = (X); \
24159 if (n == 0) \
24160 first_s = s; \
24161 n = fill_composite_glyph_string (s, base_face, overlaps); \
24162 } \
24163 \
24164 ++START; \
24165 s = first_s; \
24166 } while (0)
24167
24168
24169 /* Add a glyph string for a glyph-string sequence to the list of strings
24170 between HEAD and TAIL. */
24171
24172 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24173 do { \
24174 int face_id; \
24175 XChar2b *char2b; \
24176 Lisp_Object gstring; \
24177 \
24178 face_id = (row)->glyphs[area][START].face_id; \
24179 gstring = (composition_gstring_from_id \
24180 ((row)->glyphs[area][START].u.cmp.id)); \
24181 s = alloca (sizeof *s); \
24182 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24183 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24184 append_glyph_string (&(HEAD), &(TAIL), s); \
24185 s->x = (X); \
24186 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24187 } while (0)
24188
24189
24190 /* Add a glyph string for a sequence of glyphless character's glyphs
24191 to the list of strings between HEAD and TAIL. The meanings of
24192 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24193
24194 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24195 do \
24196 { \
24197 int face_id; \
24198 \
24199 face_id = (row)->glyphs[area][START].face_id; \
24200 \
24201 s = alloca (sizeof *s); \
24202 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24203 append_glyph_string (&HEAD, &TAIL, s); \
24204 s->x = (X); \
24205 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24206 overlaps); \
24207 } \
24208 while (0)
24209
24210
24211 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24212 of AREA of glyph row ROW on window W between indices START and END.
24213 HL overrides the face for drawing glyph strings, e.g. it is
24214 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24215 x-positions of the drawing area.
24216
24217 This is an ugly monster macro construct because we must use alloca
24218 to allocate glyph strings (because draw_glyphs can be called
24219 asynchronously). */
24220
24221 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24222 do \
24223 { \
24224 HEAD = TAIL = NULL; \
24225 while (START < END) \
24226 { \
24227 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24228 switch (first_glyph->type) \
24229 { \
24230 case CHAR_GLYPH: \
24231 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24232 HL, X, LAST_X); \
24233 break; \
24234 \
24235 case COMPOSITE_GLYPH: \
24236 if (first_glyph->u.cmp.automatic) \
24237 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24238 HL, X, LAST_X); \
24239 else \
24240 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24241 HL, X, LAST_X); \
24242 break; \
24243 \
24244 case STRETCH_GLYPH: \
24245 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24246 HL, X, LAST_X); \
24247 break; \
24248 \
24249 case IMAGE_GLYPH: \
24250 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24251 HL, X, LAST_X); \
24252 break; \
24253 \
24254 case GLYPHLESS_GLYPH: \
24255 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24256 HL, X, LAST_X); \
24257 break; \
24258 \
24259 default: \
24260 emacs_abort (); \
24261 } \
24262 \
24263 if (s) \
24264 { \
24265 set_glyph_string_background_width (s, START, LAST_X); \
24266 (X) += s->width; \
24267 } \
24268 } \
24269 } while (0)
24270
24271
24272 /* Draw glyphs between START and END in AREA of ROW on window W,
24273 starting at x-position X. X is relative to AREA in W. HL is a
24274 face-override with the following meaning:
24275
24276 DRAW_NORMAL_TEXT draw normally
24277 DRAW_CURSOR draw in cursor face
24278 DRAW_MOUSE_FACE draw in mouse face.
24279 DRAW_INVERSE_VIDEO draw in mode line face
24280 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24281 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24282
24283 If OVERLAPS is non-zero, draw only the foreground of characters and
24284 clip to the physical height of ROW. Non-zero value also defines
24285 the overlapping part to be drawn:
24286
24287 OVERLAPS_PRED overlap with preceding rows
24288 OVERLAPS_SUCC overlap with succeeding rows
24289 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24290 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24291
24292 Value is the x-position reached, relative to AREA of W. */
24293
24294 static int
24295 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24296 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24297 enum draw_glyphs_face hl, int overlaps)
24298 {
24299 struct glyph_string *head, *tail;
24300 struct glyph_string *s;
24301 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24302 int i, j, x_reached, last_x, area_left = 0;
24303 struct frame *f = XFRAME (WINDOW_FRAME (w));
24304 DECLARE_HDC (hdc);
24305
24306 ALLOCATE_HDC (hdc, f);
24307
24308 /* Let's rather be paranoid than getting a SEGV. */
24309 end = min (end, row->used[area]);
24310 start = clip_to_bounds (0, start, end);
24311
24312 /* Translate X to frame coordinates. Set last_x to the right
24313 end of the drawing area. */
24314 if (row->full_width_p)
24315 {
24316 /* X is relative to the left edge of W, without scroll bars
24317 or fringes. */
24318 area_left = WINDOW_LEFT_EDGE_X (w);
24319 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24320 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24321 }
24322 else
24323 {
24324 area_left = window_box_left (w, area);
24325 last_x = area_left + window_box_width (w, area);
24326 }
24327 x += area_left;
24328
24329 /* Build a doubly-linked list of glyph_string structures between
24330 head and tail from what we have to draw. Note that the macro
24331 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24332 the reason we use a separate variable `i'. */
24333 i = start;
24334 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24335 if (tail)
24336 x_reached = tail->x + tail->background_width;
24337 else
24338 x_reached = x;
24339
24340 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24341 the row, redraw some glyphs in front or following the glyph
24342 strings built above. */
24343 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24344 {
24345 struct glyph_string *h, *t;
24346 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24347 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24348 int check_mouse_face = 0;
24349 int dummy_x = 0;
24350
24351 /* If mouse highlighting is on, we may need to draw adjacent
24352 glyphs using mouse-face highlighting. */
24353 if (area == TEXT_AREA && row->mouse_face_p
24354 && hlinfo->mouse_face_beg_row >= 0
24355 && hlinfo->mouse_face_end_row >= 0)
24356 {
24357 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24358
24359 if (row_vpos >= hlinfo->mouse_face_beg_row
24360 && row_vpos <= hlinfo->mouse_face_end_row)
24361 {
24362 check_mouse_face = 1;
24363 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24364 ? hlinfo->mouse_face_beg_col : 0;
24365 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24366 ? hlinfo->mouse_face_end_col
24367 : row->used[TEXT_AREA];
24368 }
24369 }
24370
24371 /* Compute overhangs for all glyph strings. */
24372 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24373 for (s = head; s; s = s->next)
24374 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24375
24376 /* Prepend glyph strings for glyphs in front of the first glyph
24377 string that are overwritten because of the first glyph
24378 string's left overhang. The background of all strings
24379 prepended must be drawn because the first glyph string
24380 draws over it. */
24381 i = left_overwritten (head);
24382 if (i >= 0)
24383 {
24384 enum draw_glyphs_face overlap_hl;
24385
24386 /* If this row contains mouse highlighting, attempt to draw
24387 the overlapped glyphs with the correct highlight. This
24388 code fails if the overlap encompasses more than one glyph
24389 and mouse-highlight spans only some of these glyphs.
24390 However, making it work perfectly involves a lot more
24391 code, and I don't know if the pathological case occurs in
24392 practice, so we'll stick to this for now. --- cyd */
24393 if (check_mouse_face
24394 && mouse_beg_col < start && mouse_end_col > i)
24395 overlap_hl = DRAW_MOUSE_FACE;
24396 else
24397 overlap_hl = DRAW_NORMAL_TEXT;
24398
24399 j = i;
24400 BUILD_GLYPH_STRINGS (j, start, h, t,
24401 overlap_hl, dummy_x, last_x);
24402 start = i;
24403 compute_overhangs_and_x (t, head->x, 1);
24404 prepend_glyph_string_lists (&head, &tail, h, t);
24405 clip_head = head;
24406 }
24407
24408 /* Prepend glyph strings for glyphs in front of the first glyph
24409 string that overwrite that glyph string because of their
24410 right overhang. For these strings, only the foreground must
24411 be drawn, because it draws over the glyph string at `head'.
24412 The background must not be drawn because this would overwrite
24413 right overhangs of preceding glyphs for which no glyph
24414 strings exist. */
24415 i = left_overwriting (head);
24416 if (i >= 0)
24417 {
24418 enum draw_glyphs_face overlap_hl;
24419
24420 if (check_mouse_face
24421 && mouse_beg_col < start && mouse_end_col > i)
24422 overlap_hl = DRAW_MOUSE_FACE;
24423 else
24424 overlap_hl = DRAW_NORMAL_TEXT;
24425
24426 clip_head = head;
24427 BUILD_GLYPH_STRINGS (i, start, h, t,
24428 overlap_hl, dummy_x, last_x);
24429 for (s = h; s; s = s->next)
24430 s->background_filled_p = 1;
24431 compute_overhangs_and_x (t, head->x, 1);
24432 prepend_glyph_string_lists (&head, &tail, h, t);
24433 }
24434
24435 /* Append glyphs strings for glyphs following the last glyph
24436 string tail that are overwritten by tail. The background of
24437 these strings has to be drawn because tail's foreground draws
24438 over it. */
24439 i = right_overwritten (tail);
24440 if (i >= 0)
24441 {
24442 enum draw_glyphs_face overlap_hl;
24443
24444 if (check_mouse_face
24445 && mouse_beg_col < i && mouse_end_col > end)
24446 overlap_hl = DRAW_MOUSE_FACE;
24447 else
24448 overlap_hl = DRAW_NORMAL_TEXT;
24449
24450 BUILD_GLYPH_STRINGS (end, i, h, t,
24451 overlap_hl, x, last_x);
24452 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24453 we don't have `end = i;' here. */
24454 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24455 append_glyph_string_lists (&head, &tail, h, t);
24456 clip_tail = tail;
24457 }
24458
24459 /* Append glyph strings for glyphs following the last glyph
24460 string tail that overwrite tail. The foreground of such
24461 glyphs has to be drawn because it writes into the background
24462 of tail. The background must not be drawn because it could
24463 paint over the foreground of following glyphs. */
24464 i = right_overwriting (tail);
24465 if (i >= 0)
24466 {
24467 enum draw_glyphs_face overlap_hl;
24468 if (check_mouse_face
24469 && mouse_beg_col < i && mouse_end_col > end)
24470 overlap_hl = DRAW_MOUSE_FACE;
24471 else
24472 overlap_hl = DRAW_NORMAL_TEXT;
24473
24474 clip_tail = tail;
24475 i++; /* We must include the Ith glyph. */
24476 BUILD_GLYPH_STRINGS (end, i, h, t,
24477 overlap_hl, x, last_x);
24478 for (s = h; s; s = s->next)
24479 s->background_filled_p = 1;
24480 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24481 append_glyph_string_lists (&head, &tail, h, t);
24482 }
24483 if (clip_head || clip_tail)
24484 for (s = head; s; s = s->next)
24485 {
24486 s->clip_head = clip_head;
24487 s->clip_tail = clip_tail;
24488 }
24489 }
24490
24491 /* Draw all strings. */
24492 for (s = head; s; s = s->next)
24493 FRAME_RIF (f)->draw_glyph_string (s);
24494
24495 #ifndef HAVE_NS
24496 /* When focus a sole frame and move horizontally, this sets on_p to 0
24497 causing a failure to erase prev cursor position. */
24498 if (area == TEXT_AREA
24499 && !row->full_width_p
24500 /* When drawing overlapping rows, only the glyph strings'
24501 foreground is drawn, which doesn't erase a cursor
24502 completely. */
24503 && !overlaps)
24504 {
24505 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24506 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24507 : (tail ? tail->x + tail->background_width : x));
24508 x0 -= area_left;
24509 x1 -= area_left;
24510
24511 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24512 row->y, MATRIX_ROW_BOTTOM_Y (row));
24513 }
24514 #endif
24515
24516 /* Value is the x-position up to which drawn, relative to AREA of W.
24517 This doesn't include parts drawn because of overhangs. */
24518 if (row->full_width_p)
24519 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24520 else
24521 x_reached -= area_left;
24522
24523 RELEASE_HDC (hdc, f);
24524
24525 return x_reached;
24526 }
24527
24528 /* Expand row matrix if too narrow. Don't expand if area
24529 is not present. */
24530
24531 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24532 { \
24533 if (!it->f->fonts_changed \
24534 && (it->glyph_row->glyphs[area] \
24535 < it->glyph_row->glyphs[area + 1])) \
24536 { \
24537 it->w->ncols_scale_factor++; \
24538 it->f->fonts_changed = 1; \
24539 } \
24540 }
24541
24542 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24543 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24544
24545 static void
24546 append_glyph (struct it *it)
24547 {
24548 struct glyph *glyph;
24549 enum glyph_row_area area = it->area;
24550
24551 eassert (it->glyph_row);
24552 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24553
24554 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24555 if (glyph < it->glyph_row->glyphs[area + 1])
24556 {
24557 /* If the glyph row is reversed, we need to prepend the glyph
24558 rather than append it. */
24559 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24560 {
24561 struct glyph *g;
24562
24563 /* Make room for the additional glyph. */
24564 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24565 g[1] = *g;
24566 glyph = it->glyph_row->glyphs[area];
24567 }
24568 glyph->charpos = CHARPOS (it->position);
24569 glyph->object = it->object;
24570 if (it->pixel_width > 0)
24571 {
24572 glyph->pixel_width = it->pixel_width;
24573 glyph->padding_p = 0;
24574 }
24575 else
24576 {
24577 /* Assure at least 1-pixel width. Otherwise, cursor can't
24578 be displayed correctly. */
24579 glyph->pixel_width = 1;
24580 glyph->padding_p = 1;
24581 }
24582 glyph->ascent = it->ascent;
24583 glyph->descent = it->descent;
24584 glyph->voffset = it->voffset;
24585 glyph->type = CHAR_GLYPH;
24586 glyph->avoid_cursor_p = it->avoid_cursor_p;
24587 glyph->multibyte_p = it->multibyte_p;
24588 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24589 {
24590 /* In R2L rows, the left and the right box edges need to be
24591 drawn in reverse direction. */
24592 glyph->right_box_line_p = it->start_of_box_run_p;
24593 glyph->left_box_line_p = it->end_of_box_run_p;
24594 }
24595 else
24596 {
24597 glyph->left_box_line_p = it->start_of_box_run_p;
24598 glyph->right_box_line_p = it->end_of_box_run_p;
24599 }
24600 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24601 || it->phys_descent > it->descent);
24602 glyph->glyph_not_available_p = it->glyph_not_available_p;
24603 glyph->face_id = it->face_id;
24604 glyph->u.ch = it->char_to_display;
24605 glyph->slice.img = null_glyph_slice;
24606 glyph->font_type = FONT_TYPE_UNKNOWN;
24607 if (it->bidi_p)
24608 {
24609 glyph->resolved_level = it->bidi_it.resolved_level;
24610 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24611 emacs_abort ();
24612 glyph->bidi_type = it->bidi_it.type;
24613 }
24614 else
24615 {
24616 glyph->resolved_level = 0;
24617 glyph->bidi_type = UNKNOWN_BT;
24618 }
24619 ++it->glyph_row->used[area];
24620 }
24621 else
24622 IT_EXPAND_MATRIX_WIDTH (it, area);
24623 }
24624
24625 /* Store one glyph for the composition IT->cmp_it.id in
24626 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24627 non-null. */
24628
24629 static void
24630 append_composite_glyph (struct it *it)
24631 {
24632 struct glyph *glyph;
24633 enum glyph_row_area area = it->area;
24634
24635 eassert (it->glyph_row);
24636
24637 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24638 if (glyph < it->glyph_row->glyphs[area + 1])
24639 {
24640 /* If the glyph row is reversed, we need to prepend the glyph
24641 rather than append it. */
24642 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24643 {
24644 struct glyph *g;
24645
24646 /* Make room for the new glyph. */
24647 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24648 g[1] = *g;
24649 glyph = it->glyph_row->glyphs[it->area];
24650 }
24651 glyph->charpos = it->cmp_it.charpos;
24652 glyph->object = it->object;
24653 glyph->pixel_width = it->pixel_width;
24654 glyph->ascent = it->ascent;
24655 glyph->descent = it->descent;
24656 glyph->voffset = it->voffset;
24657 glyph->type = COMPOSITE_GLYPH;
24658 if (it->cmp_it.ch < 0)
24659 {
24660 glyph->u.cmp.automatic = 0;
24661 glyph->u.cmp.id = it->cmp_it.id;
24662 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24663 }
24664 else
24665 {
24666 glyph->u.cmp.automatic = 1;
24667 glyph->u.cmp.id = it->cmp_it.id;
24668 glyph->slice.cmp.from = it->cmp_it.from;
24669 glyph->slice.cmp.to = it->cmp_it.to - 1;
24670 }
24671 glyph->avoid_cursor_p = it->avoid_cursor_p;
24672 glyph->multibyte_p = it->multibyte_p;
24673 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24674 {
24675 /* In R2L rows, the left and the right box edges need to be
24676 drawn in reverse direction. */
24677 glyph->right_box_line_p = it->start_of_box_run_p;
24678 glyph->left_box_line_p = it->end_of_box_run_p;
24679 }
24680 else
24681 {
24682 glyph->left_box_line_p = it->start_of_box_run_p;
24683 glyph->right_box_line_p = it->end_of_box_run_p;
24684 }
24685 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24686 || it->phys_descent > it->descent);
24687 glyph->padding_p = 0;
24688 glyph->glyph_not_available_p = 0;
24689 glyph->face_id = it->face_id;
24690 glyph->font_type = FONT_TYPE_UNKNOWN;
24691 if (it->bidi_p)
24692 {
24693 glyph->resolved_level = it->bidi_it.resolved_level;
24694 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24695 emacs_abort ();
24696 glyph->bidi_type = it->bidi_it.type;
24697 }
24698 ++it->glyph_row->used[area];
24699 }
24700 else
24701 IT_EXPAND_MATRIX_WIDTH (it, area);
24702 }
24703
24704
24705 /* Change IT->ascent and IT->height according to the setting of
24706 IT->voffset. */
24707
24708 static void
24709 take_vertical_position_into_account (struct it *it)
24710 {
24711 if (it->voffset)
24712 {
24713 if (it->voffset < 0)
24714 /* Increase the ascent so that we can display the text higher
24715 in the line. */
24716 it->ascent -= it->voffset;
24717 else
24718 /* Increase the descent so that we can display the text lower
24719 in the line. */
24720 it->descent += it->voffset;
24721 }
24722 }
24723
24724
24725 /* Produce glyphs/get display metrics for the image IT is loaded with.
24726 See the description of struct display_iterator in dispextern.h for
24727 an overview of struct display_iterator. */
24728
24729 static void
24730 produce_image_glyph (struct it *it)
24731 {
24732 struct image *img;
24733 struct face *face;
24734 int glyph_ascent, crop;
24735 struct glyph_slice slice;
24736
24737 eassert (it->what == IT_IMAGE);
24738
24739 face = FACE_FROM_ID (it->f, it->face_id);
24740 eassert (face);
24741 /* Make sure X resources of the face is loaded. */
24742 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24743
24744 if (it->image_id < 0)
24745 {
24746 /* Fringe bitmap. */
24747 it->ascent = it->phys_ascent = 0;
24748 it->descent = it->phys_descent = 0;
24749 it->pixel_width = 0;
24750 it->nglyphs = 0;
24751 return;
24752 }
24753
24754 img = IMAGE_FROM_ID (it->f, it->image_id);
24755 eassert (img);
24756 /* Make sure X resources of the image is loaded. */
24757 prepare_image_for_display (it->f, img);
24758
24759 slice.x = slice.y = 0;
24760 slice.width = img->width;
24761 slice.height = img->height;
24762
24763 if (INTEGERP (it->slice.x))
24764 slice.x = XINT (it->slice.x);
24765 else if (FLOATP (it->slice.x))
24766 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24767
24768 if (INTEGERP (it->slice.y))
24769 slice.y = XINT (it->slice.y);
24770 else if (FLOATP (it->slice.y))
24771 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24772
24773 if (INTEGERP (it->slice.width))
24774 slice.width = XINT (it->slice.width);
24775 else if (FLOATP (it->slice.width))
24776 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24777
24778 if (INTEGERP (it->slice.height))
24779 slice.height = XINT (it->slice.height);
24780 else if (FLOATP (it->slice.height))
24781 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24782
24783 if (slice.x >= img->width)
24784 slice.x = img->width;
24785 if (slice.y >= img->height)
24786 slice.y = img->height;
24787 if (slice.x + slice.width >= img->width)
24788 slice.width = img->width - slice.x;
24789 if (slice.y + slice.height > img->height)
24790 slice.height = img->height - slice.y;
24791
24792 if (slice.width == 0 || slice.height == 0)
24793 return;
24794
24795 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24796
24797 it->descent = slice.height - glyph_ascent;
24798 if (slice.y == 0)
24799 it->descent += img->vmargin;
24800 if (slice.y + slice.height == img->height)
24801 it->descent += img->vmargin;
24802 it->phys_descent = it->descent;
24803
24804 it->pixel_width = slice.width;
24805 if (slice.x == 0)
24806 it->pixel_width += img->hmargin;
24807 if (slice.x + slice.width == img->width)
24808 it->pixel_width += img->hmargin;
24809
24810 /* It's quite possible for images to have an ascent greater than
24811 their height, so don't get confused in that case. */
24812 if (it->descent < 0)
24813 it->descent = 0;
24814
24815 it->nglyphs = 1;
24816
24817 if (face->box != FACE_NO_BOX)
24818 {
24819 if (face->box_line_width > 0)
24820 {
24821 if (slice.y == 0)
24822 it->ascent += face->box_line_width;
24823 if (slice.y + slice.height == img->height)
24824 it->descent += face->box_line_width;
24825 }
24826
24827 if (it->start_of_box_run_p && slice.x == 0)
24828 it->pixel_width += eabs (face->box_line_width);
24829 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24830 it->pixel_width += eabs (face->box_line_width);
24831 }
24832
24833 take_vertical_position_into_account (it);
24834
24835 /* Automatically crop wide image glyphs at right edge so we can
24836 draw the cursor on same display row. */
24837 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24838 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24839 {
24840 it->pixel_width -= crop;
24841 slice.width -= crop;
24842 }
24843
24844 if (it->glyph_row)
24845 {
24846 struct glyph *glyph;
24847 enum glyph_row_area area = it->area;
24848
24849 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24850 if (glyph < it->glyph_row->glyphs[area + 1])
24851 {
24852 glyph->charpos = CHARPOS (it->position);
24853 glyph->object = it->object;
24854 glyph->pixel_width = it->pixel_width;
24855 glyph->ascent = glyph_ascent;
24856 glyph->descent = it->descent;
24857 glyph->voffset = it->voffset;
24858 glyph->type = IMAGE_GLYPH;
24859 glyph->avoid_cursor_p = it->avoid_cursor_p;
24860 glyph->multibyte_p = it->multibyte_p;
24861 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24862 {
24863 /* In R2L rows, the left and the right box edges need to be
24864 drawn in reverse direction. */
24865 glyph->right_box_line_p = it->start_of_box_run_p;
24866 glyph->left_box_line_p = it->end_of_box_run_p;
24867 }
24868 else
24869 {
24870 glyph->left_box_line_p = it->start_of_box_run_p;
24871 glyph->right_box_line_p = it->end_of_box_run_p;
24872 }
24873 glyph->overlaps_vertically_p = 0;
24874 glyph->padding_p = 0;
24875 glyph->glyph_not_available_p = 0;
24876 glyph->face_id = it->face_id;
24877 glyph->u.img_id = img->id;
24878 glyph->slice.img = slice;
24879 glyph->font_type = FONT_TYPE_UNKNOWN;
24880 if (it->bidi_p)
24881 {
24882 glyph->resolved_level = it->bidi_it.resolved_level;
24883 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24884 emacs_abort ();
24885 glyph->bidi_type = it->bidi_it.type;
24886 }
24887 ++it->glyph_row->used[area];
24888 }
24889 else
24890 IT_EXPAND_MATRIX_WIDTH (it, area);
24891 }
24892 }
24893
24894
24895 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24896 of the glyph, WIDTH and HEIGHT are the width and height of the
24897 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24898
24899 static void
24900 append_stretch_glyph (struct it *it, Lisp_Object object,
24901 int width, int height, int ascent)
24902 {
24903 struct glyph *glyph;
24904 enum glyph_row_area area = it->area;
24905
24906 eassert (ascent >= 0 && ascent <= height);
24907
24908 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24909 if (glyph < it->glyph_row->glyphs[area + 1])
24910 {
24911 /* If the glyph row is reversed, we need to prepend the glyph
24912 rather than append it. */
24913 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24914 {
24915 struct glyph *g;
24916
24917 /* Make room for the additional glyph. */
24918 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24919 g[1] = *g;
24920 glyph = it->glyph_row->glyphs[area];
24921 }
24922 glyph->charpos = CHARPOS (it->position);
24923 glyph->object = object;
24924 glyph->pixel_width = width;
24925 glyph->ascent = ascent;
24926 glyph->descent = height - ascent;
24927 glyph->voffset = it->voffset;
24928 glyph->type = STRETCH_GLYPH;
24929 glyph->avoid_cursor_p = it->avoid_cursor_p;
24930 glyph->multibyte_p = it->multibyte_p;
24931 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24932 {
24933 /* In R2L rows, the left and the right box edges need to be
24934 drawn in reverse direction. */
24935 glyph->right_box_line_p = it->start_of_box_run_p;
24936 glyph->left_box_line_p = it->end_of_box_run_p;
24937 }
24938 else
24939 {
24940 glyph->left_box_line_p = it->start_of_box_run_p;
24941 glyph->right_box_line_p = it->end_of_box_run_p;
24942 }
24943 glyph->overlaps_vertically_p = 0;
24944 glyph->padding_p = 0;
24945 glyph->glyph_not_available_p = 0;
24946 glyph->face_id = it->face_id;
24947 glyph->u.stretch.ascent = ascent;
24948 glyph->u.stretch.height = height;
24949 glyph->slice.img = null_glyph_slice;
24950 glyph->font_type = FONT_TYPE_UNKNOWN;
24951 if (it->bidi_p)
24952 {
24953 glyph->resolved_level = it->bidi_it.resolved_level;
24954 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24955 emacs_abort ();
24956 glyph->bidi_type = it->bidi_it.type;
24957 }
24958 else
24959 {
24960 glyph->resolved_level = 0;
24961 glyph->bidi_type = UNKNOWN_BT;
24962 }
24963 ++it->glyph_row->used[area];
24964 }
24965 else
24966 IT_EXPAND_MATRIX_WIDTH (it, area);
24967 }
24968
24969 #endif /* HAVE_WINDOW_SYSTEM */
24970
24971 /* Produce a stretch glyph for iterator IT. IT->object is the value
24972 of the glyph property displayed. The value must be a list
24973 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24974 being recognized:
24975
24976 1. `:width WIDTH' specifies that the space should be WIDTH *
24977 canonical char width wide. WIDTH may be an integer or floating
24978 point number.
24979
24980 2. `:relative-width FACTOR' specifies that the width of the stretch
24981 should be computed from the width of the first character having the
24982 `glyph' property, and should be FACTOR times that width.
24983
24984 3. `:align-to HPOS' specifies that the space should be wide enough
24985 to reach HPOS, a value in canonical character units.
24986
24987 Exactly one of the above pairs must be present.
24988
24989 4. `:height HEIGHT' specifies that the height of the stretch produced
24990 should be HEIGHT, measured in canonical character units.
24991
24992 5. `:relative-height FACTOR' specifies that the height of the
24993 stretch should be FACTOR times the height of the characters having
24994 the glyph property.
24995
24996 Either none or exactly one of 4 or 5 must be present.
24997
24998 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24999 of the stretch should be used for the ascent of the stretch.
25000 ASCENT must be in the range 0 <= ASCENT <= 100. */
25001
25002 void
25003 produce_stretch_glyph (struct it *it)
25004 {
25005 /* (space :width WIDTH :height HEIGHT ...) */
25006 Lisp_Object prop, plist;
25007 int width = 0, height = 0, align_to = -1;
25008 int zero_width_ok_p = 0;
25009 double tem;
25010 struct font *font = NULL;
25011
25012 #ifdef HAVE_WINDOW_SYSTEM
25013 int ascent = 0;
25014 int zero_height_ok_p = 0;
25015
25016 if (FRAME_WINDOW_P (it->f))
25017 {
25018 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25019 font = face->font ? face->font : FRAME_FONT (it->f);
25020 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25021 }
25022 #endif
25023
25024 /* List should start with `space'. */
25025 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25026 plist = XCDR (it->object);
25027
25028 /* Compute the width of the stretch. */
25029 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25030 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25031 {
25032 /* Absolute width `:width WIDTH' specified and valid. */
25033 zero_width_ok_p = 1;
25034 width = (int)tem;
25035 }
25036 #ifdef HAVE_WINDOW_SYSTEM
25037 else if (FRAME_WINDOW_P (it->f)
25038 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25039 {
25040 /* Relative width `:relative-width FACTOR' specified and valid.
25041 Compute the width of the characters having the `glyph'
25042 property. */
25043 struct it it2;
25044 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25045
25046 it2 = *it;
25047 if (it->multibyte_p)
25048 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25049 else
25050 {
25051 it2.c = it2.char_to_display = *p, it2.len = 1;
25052 if (! ASCII_CHAR_P (it2.c))
25053 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25054 }
25055
25056 it2.glyph_row = NULL;
25057 it2.what = IT_CHARACTER;
25058 x_produce_glyphs (&it2);
25059 width = NUMVAL (prop) * it2.pixel_width;
25060 }
25061 #endif /* HAVE_WINDOW_SYSTEM */
25062 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25063 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25064 {
25065 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25066 align_to = (align_to < 0
25067 ? 0
25068 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25069 else if (align_to < 0)
25070 align_to = window_box_left_offset (it->w, TEXT_AREA);
25071 width = max (0, (int)tem + align_to - it->current_x);
25072 zero_width_ok_p = 1;
25073 }
25074 else
25075 /* Nothing specified -> width defaults to canonical char width. */
25076 width = FRAME_COLUMN_WIDTH (it->f);
25077
25078 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25079 width = 1;
25080
25081 #ifdef HAVE_WINDOW_SYSTEM
25082 /* Compute height. */
25083 if (FRAME_WINDOW_P (it->f))
25084 {
25085 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25086 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25087 {
25088 height = (int)tem;
25089 zero_height_ok_p = 1;
25090 }
25091 else if (prop = Fplist_get (plist, QCrelative_height),
25092 NUMVAL (prop) > 0)
25093 height = FONT_HEIGHT (font) * NUMVAL (prop);
25094 else
25095 height = FONT_HEIGHT (font);
25096
25097 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25098 height = 1;
25099
25100 /* Compute percentage of height used for ascent. If
25101 `:ascent ASCENT' is present and valid, use that. Otherwise,
25102 derive the ascent from the font in use. */
25103 if (prop = Fplist_get (plist, QCascent),
25104 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25105 ascent = height * NUMVAL (prop) / 100.0;
25106 else if (!NILP (prop)
25107 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25108 ascent = min (max (0, (int)tem), height);
25109 else
25110 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25111 }
25112 else
25113 #endif /* HAVE_WINDOW_SYSTEM */
25114 height = 1;
25115
25116 if (width > 0 && it->line_wrap != TRUNCATE
25117 && it->current_x + width > it->last_visible_x)
25118 {
25119 width = it->last_visible_x - it->current_x;
25120 #ifdef HAVE_WINDOW_SYSTEM
25121 /* Subtract one more pixel from the stretch width, but only on
25122 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25123 width -= FRAME_WINDOW_P (it->f);
25124 #endif
25125 }
25126
25127 if (width > 0 && height > 0 && it->glyph_row)
25128 {
25129 Lisp_Object o_object = it->object;
25130 Lisp_Object object = it->stack[it->sp - 1].string;
25131 int n = width;
25132
25133 if (!STRINGP (object))
25134 object = it->w->contents;
25135 #ifdef HAVE_WINDOW_SYSTEM
25136 if (FRAME_WINDOW_P (it->f))
25137 append_stretch_glyph (it, object, width, height, ascent);
25138 else
25139 #endif
25140 {
25141 it->object = object;
25142 it->char_to_display = ' ';
25143 it->pixel_width = it->len = 1;
25144 while (n--)
25145 tty_append_glyph (it);
25146 it->object = o_object;
25147 }
25148 }
25149
25150 it->pixel_width = width;
25151 #ifdef HAVE_WINDOW_SYSTEM
25152 if (FRAME_WINDOW_P (it->f))
25153 {
25154 it->ascent = it->phys_ascent = ascent;
25155 it->descent = it->phys_descent = height - it->ascent;
25156 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25157 take_vertical_position_into_account (it);
25158 }
25159 else
25160 #endif
25161 it->nglyphs = width;
25162 }
25163
25164 /* Get information about special display element WHAT in an
25165 environment described by IT. WHAT is one of IT_TRUNCATION or
25166 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25167 non-null glyph_row member. This function ensures that fields like
25168 face_id, c, len of IT are left untouched. */
25169
25170 static void
25171 produce_special_glyphs (struct it *it, enum display_element_type what)
25172 {
25173 struct it temp_it;
25174 Lisp_Object gc;
25175 GLYPH glyph;
25176
25177 temp_it = *it;
25178 temp_it.object = make_number (0);
25179 memset (&temp_it.current, 0, sizeof temp_it.current);
25180
25181 if (what == IT_CONTINUATION)
25182 {
25183 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25184 if (it->bidi_it.paragraph_dir == R2L)
25185 SET_GLYPH_FROM_CHAR (glyph, '/');
25186 else
25187 SET_GLYPH_FROM_CHAR (glyph, '\\');
25188 if (it->dp
25189 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25190 {
25191 /* FIXME: Should we mirror GC for R2L lines? */
25192 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25193 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25194 }
25195 }
25196 else if (what == IT_TRUNCATION)
25197 {
25198 /* Truncation glyph. */
25199 SET_GLYPH_FROM_CHAR (glyph, '$');
25200 if (it->dp
25201 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25202 {
25203 /* FIXME: Should we mirror GC for R2L lines? */
25204 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25205 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25206 }
25207 }
25208 else
25209 emacs_abort ();
25210
25211 #ifdef HAVE_WINDOW_SYSTEM
25212 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25213 is turned off, we precede the truncation/continuation glyphs by a
25214 stretch glyph whose width is computed such that these special
25215 glyphs are aligned at the window margin, even when very different
25216 fonts are used in different glyph rows. */
25217 if (FRAME_WINDOW_P (temp_it.f)
25218 /* init_iterator calls this with it->glyph_row == NULL, and it
25219 wants only the pixel width of the truncation/continuation
25220 glyphs. */
25221 && temp_it.glyph_row
25222 /* insert_left_trunc_glyphs calls us at the beginning of the
25223 row, and it has its own calculation of the stretch glyph
25224 width. */
25225 && temp_it.glyph_row->used[TEXT_AREA] > 0
25226 && (temp_it.glyph_row->reversed_p
25227 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25228 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25229 {
25230 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25231
25232 if (stretch_width > 0)
25233 {
25234 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25235 struct font *font =
25236 face->font ? face->font : FRAME_FONT (temp_it.f);
25237 int stretch_ascent =
25238 (((temp_it.ascent + temp_it.descent)
25239 * FONT_BASE (font)) / FONT_HEIGHT (font));
25240
25241 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25242 temp_it.ascent + temp_it.descent,
25243 stretch_ascent);
25244 }
25245 }
25246 #endif
25247
25248 temp_it.dp = NULL;
25249 temp_it.what = IT_CHARACTER;
25250 temp_it.len = 1;
25251 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25252 temp_it.face_id = GLYPH_FACE (glyph);
25253 temp_it.len = CHAR_BYTES (temp_it.c);
25254
25255 PRODUCE_GLYPHS (&temp_it);
25256 it->pixel_width = temp_it.pixel_width;
25257 it->nglyphs = temp_it.pixel_width;
25258 }
25259
25260 #ifdef HAVE_WINDOW_SYSTEM
25261
25262 /* Calculate line-height and line-spacing properties.
25263 An integer value specifies explicit pixel value.
25264 A float value specifies relative value to current face height.
25265 A cons (float . face-name) specifies relative value to
25266 height of specified face font.
25267
25268 Returns height in pixels, or nil. */
25269
25270
25271 static Lisp_Object
25272 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25273 int boff, int override)
25274 {
25275 Lisp_Object face_name = Qnil;
25276 int ascent, descent, height;
25277
25278 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25279 return val;
25280
25281 if (CONSP (val))
25282 {
25283 face_name = XCAR (val);
25284 val = XCDR (val);
25285 if (!NUMBERP (val))
25286 val = make_number (1);
25287 if (NILP (face_name))
25288 {
25289 height = it->ascent + it->descent;
25290 goto scale;
25291 }
25292 }
25293
25294 if (NILP (face_name))
25295 {
25296 font = FRAME_FONT (it->f);
25297 boff = FRAME_BASELINE_OFFSET (it->f);
25298 }
25299 else if (EQ (face_name, Qt))
25300 {
25301 override = 0;
25302 }
25303 else
25304 {
25305 int face_id;
25306 struct face *face;
25307
25308 face_id = lookup_named_face (it->f, face_name, 0);
25309 if (face_id < 0)
25310 return make_number (-1);
25311
25312 face = FACE_FROM_ID (it->f, face_id);
25313 font = face->font;
25314 if (font == NULL)
25315 return make_number (-1);
25316 boff = font->baseline_offset;
25317 if (font->vertical_centering)
25318 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25319 }
25320
25321 ascent = FONT_BASE (font) + boff;
25322 descent = FONT_DESCENT (font) - boff;
25323
25324 if (override)
25325 {
25326 it->override_ascent = ascent;
25327 it->override_descent = descent;
25328 it->override_boff = boff;
25329 }
25330
25331 height = ascent + descent;
25332
25333 scale:
25334 if (FLOATP (val))
25335 height = (int)(XFLOAT_DATA (val) * height);
25336 else if (INTEGERP (val))
25337 height *= XINT (val);
25338
25339 return make_number (height);
25340 }
25341
25342
25343 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25344 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25345 and only if this is for a character for which no font was found.
25346
25347 If the display method (it->glyphless_method) is
25348 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25349 length of the acronym or the hexadecimal string, UPPER_XOFF and
25350 UPPER_YOFF are pixel offsets for the upper part of the string,
25351 LOWER_XOFF and LOWER_YOFF are for the lower part.
25352
25353 For the other display methods, LEN through LOWER_YOFF are zero. */
25354
25355 static void
25356 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25357 short upper_xoff, short upper_yoff,
25358 short lower_xoff, short lower_yoff)
25359 {
25360 struct glyph *glyph;
25361 enum glyph_row_area area = it->area;
25362
25363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25364 if (glyph < it->glyph_row->glyphs[area + 1])
25365 {
25366 /* If the glyph row is reversed, we need to prepend the glyph
25367 rather than append it. */
25368 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25369 {
25370 struct glyph *g;
25371
25372 /* Make room for the additional glyph. */
25373 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25374 g[1] = *g;
25375 glyph = it->glyph_row->glyphs[area];
25376 }
25377 glyph->charpos = CHARPOS (it->position);
25378 glyph->object = it->object;
25379 glyph->pixel_width = it->pixel_width;
25380 glyph->ascent = it->ascent;
25381 glyph->descent = it->descent;
25382 glyph->voffset = it->voffset;
25383 glyph->type = GLYPHLESS_GLYPH;
25384 glyph->u.glyphless.method = it->glyphless_method;
25385 glyph->u.glyphless.for_no_font = for_no_font;
25386 glyph->u.glyphless.len = len;
25387 glyph->u.glyphless.ch = it->c;
25388 glyph->slice.glyphless.upper_xoff = upper_xoff;
25389 glyph->slice.glyphless.upper_yoff = upper_yoff;
25390 glyph->slice.glyphless.lower_xoff = lower_xoff;
25391 glyph->slice.glyphless.lower_yoff = lower_yoff;
25392 glyph->avoid_cursor_p = it->avoid_cursor_p;
25393 glyph->multibyte_p = it->multibyte_p;
25394 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25395 {
25396 /* In R2L rows, the left and the right box edges need to be
25397 drawn in reverse direction. */
25398 glyph->right_box_line_p = it->start_of_box_run_p;
25399 glyph->left_box_line_p = it->end_of_box_run_p;
25400 }
25401 else
25402 {
25403 glyph->left_box_line_p = it->start_of_box_run_p;
25404 glyph->right_box_line_p = it->end_of_box_run_p;
25405 }
25406 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25407 || it->phys_descent > it->descent);
25408 glyph->padding_p = 0;
25409 glyph->glyph_not_available_p = 0;
25410 glyph->face_id = face_id;
25411 glyph->font_type = FONT_TYPE_UNKNOWN;
25412 if (it->bidi_p)
25413 {
25414 glyph->resolved_level = it->bidi_it.resolved_level;
25415 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25416 emacs_abort ();
25417 glyph->bidi_type = it->bidi_it.type;
25418 }
25419 ++it->glyph_row->used[area];
25420 }
25421 else
25422 IT_EXPAND_MATRIX_WIDTH (it, area);
25423 }
25424
25425
25426 /* Produce a glyph for a glyphless character for iterator IT.
25427 IT->glyphless_method specifies which method to use for displaying
25428 the character. See the description of enum
25429 glyphless_display_method in dispextern.h for the detail.
25430
25431 FOR_NO_FONT is nonzero if and only if this is for a character for
25432 which no font was found. ACRONYM, if non-nil, is an acronym string
25433 for the character. */
25434
25435 static void
25436 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25437 {
25438 int face_id;
25439 struct face *face;
25440 struct font *font;
25441 int base_width, base_height, width, height;
25442 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25443 int len;
25444
25445 /* Get the metrics of the base font. We always refer to the current
25446 ASCII face. */
25447 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25448 font = face->font ? face->font : FRAME_FONT (it->f);
25449 it->ascent = FONT_BASE (font) + font->baseline_offset;
25450 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25451 base_height = it->ascent + it->descent;
25452 base_width = font->average_width;
25453
25454 face_id = merge_glyphless_glyph_face (it);
25455
25456 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25457 {
25458 it->pixel_width = THIN_SPACE_WIDTH;
25459 len = 0;
25460 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25461 }
25462 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25463 {
25464 width = CHAR_WIDTH (it->c);
25465 if (width == 0)
25466 width = 1;
25467 else if (width > 4)
25468 width = 4;
25469 it->pixel_width = base_width * width;
25470 len = 0;
25471 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25472 }
25473 else
25474 {
25475 char buf[7];
25476 const char *str;
25477 unsigned int code[6];
25478 int upper_len;
25479 int ascent, descent;
25480 struct font_metrics metrics_upper, metrics_lower;
25481
25482 face = FACE_FROM_ID (it->f, face_id);
25483 font = face->font ? face->font : FRAME_FONT (it->f);
25484 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25485
25486 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25487 {
25488 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25489 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25490 if (CONSP (acronym))
25491 acronym = XCAR (acronym);
25492 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25493 }
25494 else
25495 {
25496 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25497 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25498 str = buf;
25499 }
25500 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25501 code[len] = font->driver->encode_char (font, str[len]);
25502 upper_len = (len + 1) / 2;
25503 font->driver->text_extents (font, code, upper_len,
25504 &metrics_upper);
25505 font->driver->text_extents (font, code + upper_len, len - upper_len,
25506 &metrics_lower);
25507
25508
25509
25510 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25511 width = max (metrics_upper.width, metrics_lower.width) + 4;
25512 upper_xoff = upper_yoff = 2; /* the typical case */
25513 if (base_width >= width)
25514 {
25515 /* Align the upper to the left, the lower to the right. */
25516 it->pixel_width = base_width;
25517 lower_xoff = base_width - 2 - metrics_lower.width;
25518 }
25519 else
25520 {
25521 /* Center the shorter one. */
25522 it->pixel_width = width;
25523 if (metrics_upper.width >= metrics_lower.width)
25524 lower_xoff = (width - metrics_lower.width) / 2;
25525 else
25526 {
25527 /* FIXME: This code doesn't look right. It formerly was
25528 missing the "lower_xoff = 0;", which couldn't have
25529 been right since it left lower_xoff uninitialized. */
25530 lower_xoff = 0;
25531 upper_xoff = (width - metrics_upper.width) / 2;
25532 }
25533 }
25534
25535 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25536 top, bottom, and between upper and lower strings. */
25537 height = (metrics_upper.ascent + metrics_upper.descent
25538 + metrics_lower.ascent + metrics_lower.descent) + 5;
25539 /* Center vertically.
25540 H:base_height, D:base_descent
25541 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25542
25543 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25544 descent = D - H/2 + h/2;
25545 lower_yoff = descent - 2 - ld;
25546 upper_yoff = lower_yoff - la - 1 - ud; */
25547 ascent = - (it->descent - (base_height + height + 1) / 2);
25548 descent = it->descent - (base_height - height) / 2;
25549 lower_yoff = descent - 2 - metrics_lower.descent;
25550 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25551 - metrics_upper.descent);
25552 /* Don't make the height shorter than the base height. */
25553 if (height > base_height)
25554 {
25555 it->ascent = ascent;
25556 it->descent = descent;
25557 }
25558 }
25559
25560 it->phys_ascent = it->ascent;
25561 it->phys_descent = it->descent;
25562 if (it->glyph_row)
25563 append_glyphless_glyph (it, face_id, for_no_font, len,
25564 upper_xoff, upper_yoff,
25565 lower_xoff, lower_yoff);
25566 it->nglyphs = 1;
25567 take_vertical_position_into_account (it);
25568 }
25569
25570
25571 /* RIF:
25572 Produce glyphs/get display metrics for the display element IT is
25573 loaded with. See the description of struct it in dispextern.h
25574 for an overview of struct it. */
25575
25576 void
25577 x_produce_glyphs (struct it *it)
25578 {
25579 int extra_line_spacing = it->extra_line_spacing;
25580
25581 it->glyph_not_available_p = 0;
25582
25583 if (it->what == IT_CHARACTER)
25584 {
25585 XChar2b char2b;
25586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25587 struct font *font = face->font;
25588 struct font_metrics *pcm = NULL;
25589 int boff; /* Baseline offset. */
25590
25591 if (font == NULL)
25592 {
25593 /* When no suitable font is found, display this character by
25594 the method specified in the first extra slot of
25595 Vglyphless_char_display. */
25596 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25597
25598 eassert (it->what == IT_GLYPHLESS);
25599 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25600 goto done;
25601 }
25602
25603 boff = font->baseline_offset;
25604 if (font->vertical_centering)
25605 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25606
25607 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25608 {
25609 int stretched_p;
25610
25611 it->nglyphs = 1;
25612
25613 if (it->override_ascent >= 0)
25614 {
25615 it->ascent = it->override_ascent;
25616 it->descent = it->override_descent;
25617 boff = it->override_boff;
25618 }
25619 else
25620 {
25621 it->ascent = FONT_BASE (font) + boff;
25622 it->descent = FONT_DESCENT (font) - boff;
25623 }
25624
25625 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25626 {
25627 pcm = get_per_char_metric (font, &char2b);
25628 if (pcm->width == 0
25629 && pcm->rbearing == 0 && pcm->lbearing == 0)
25630 pcm = NULL;
25631 }
25632
25633 if (pcm)
25634 {
25635 it->phys_ascent = pcm->ascent + boff;
25636 it->phys_descent = pcm->descent - boff;
25637 it->pixel_width = pcm->width;
25638 }
25639 else
25640 {
25641 it->glyph_not_available_p = 1;
25642 it->phys_ascent = it->ascent;
25643 it->phys_descent = it->descent;
25644 it->pixel_width = font->space_width;
25645 }
25646
25647 if (it->constrain_row_ascent_descent_p)
25648 {
25649 if (it->descent > it->max_descent)
25650 {
25651 it->ascent += it->descent - it->max_descent;
25652 it->descent = it->max_descent;
25653 }
25654 if (it->ascent > it->max_ascent)
25655 {
25656 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25657 it->ascent = it->max_ascent;
25658 }
25659 it->phys_ascent = min (it->phys_ascent, it->ascent);
25660 it->phys_descent = min (it->phys_descent, it->descent);
25661 extra_line_spacing = 0;
25662 }
25663
25664 /* If this is a space inside a region of text with
25665 `space-width' property, change its width. */
25666 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25667 if (stretched_p)
25668 it->pixel_width *= XFLOATINT (it->space_width);
25669
25670 /* If face has a box, add the box thickness to the character
25671 height. If character has a box line to the left and/or
25672 right, add the box line width to the character's width. */
25673 if (face->box != FACE_NO_BOX)
25674 {
25675 int thick = face->box_line_width;
25676
25677 if (thick > 0)
25678 {
25679 it->ascent += thick;
25680 it->descent += thick;
25681 }
25682 else
25683 thick = -thick;
25684
25685 if (it->start_of_box_run_p)
25686 it->pixel_width += thick;
25687 if (it->end_of_box_run_p)
25688 it->pixel_width += thick;
25689 }
25690
25691 /* If face has an overline, add the height of the overline
25692 (1 pixel) and a 1 pixel margin to the character height. */
25693 if (face->overline_p)
25694 it->ascent += overline_margin;
25695
25696 if (it->constrain_row_ascent_descent_p)
25697 {
25698 if (it->ascent > it->max_ascent)
25699 it->ascent = it->max_ascent;
25700 if (it->descent > it->max_descent)
25701 it->descent = it->max_descent;
25702 }
25703
25704 take_vertical_position_into_account (it);
25705
25706 /* If we have to actually produce glyphs, do it. */
25707 if (it->glyph_row)
25708 {
25709 if (stretched_p)
25710 {
25711 /* Translate a space with a `space-width' property
25712 into a stretch glyph. */
25713 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25714 / FONT_HEIGHT (font));
25715 append_stretch_glyph (it, it->object, it->pixel_width,
25716 it->ascent + it->descent, ascent);
25717 }
25718 else
25719 append_glyph (it);
25720
25721 /* If characters with lbearing or rbearing are displayed
25722 in this line, record that fact in a flag of the
25723 glyph row. This is used to optimize X output code. */
25724 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25725 it->glyph_row->contains_overlapping_glyphs_p = 1;
25726 }
25727 if (! stretched_p && it->pixel_width == 0)
25728 /* We assure that all visible glyphs have at least 1-pixel
25729 width. */
25730 it->pixel_width = 1;
25731 }
25732 else if (it->char_to_display == '\n')
25733 {
25734 /* A newline has no width, but we need the height of the
25735 line. But if previous part of the line sets a height,
25736 don't increase that height. */
25737
25738 Lisp_Object height;
25739 Lisp_Object total_height = Qnil;
25740
25741 it->override_ascent = -1;
25742 it->pixel_width = 0;
25743 it->nglyphs = 0;
25744
25745 height = get_it_property (it, Qline_height);
25746 /* Split (line-height total-height) list. */
25747 if (CONSP (height)
25748 && CONSP (XCDR (height))
25749 && NILP (XCDR (XCDR (height))))
25750 {
25751 total_height = XCAR (XCDR (height));
25752 height = XCAR (height);
25753 }
25754 height = calc_line_height_property (it, height, font, boff, 1);
25755
25756 if (it->override_ascent >= 0)
25757 {
25758 it->ascent = it->override_ascent;
25759 it->descent = it->override_descent;
25760 boff = it->override_boff;
25761 }
25762 else
25763 {
25764 it->ascent = FONT_BASE (font) + boff;
25765 it->descent = FONT_DESCENT (font) - boff;
25766 }
25767
25768 if (EQ (height, Qt))
25769 {
25770 if (it->descent > it->max_descent)
25771 {
25772 it->ascent += it->descent - it->max_descent;
25773 it->descent = it->max_descent;
25774 }
25775 if (it->ascent > it->max_ascent)
25776 {
25777 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25778 it->ascent = it->max_ascent;
25779 }
25780 it->phys_ascent = min (it->phys_ascent, it->ascent);
25781 it->phys_descent = min (it->phys_descent, it->descent);
25782 it->constrain_row_ascent_descent_p = 1;
25783 extra_line_spacing = 0;
25784 }
25785 else
25786 {
25787 Lisp_Object spacing;
25788
25789 it->phys_ascent = it->ascent;
25790 it->phys_descent = it->descent;
25791
25792 if ((it->max_ascent > 0 || it->max_descent > 0)
25793 && face->box != FACE_NO_BOX
25794 && face->box_line_width > 0)
25795 {
25796 it->ascent += face->box_line_width;
25797 it->descent += face->box_line_width;
25798 }
25799 if (!NILP (height)
25800 && XINT (height) > it->ascent + it->descent)
25801 it->ascent = XINT (height) - it->descent;
25802
25803 if (!NILP (total_height))
25804 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25805 else
25806 {
25807 spacing = get_it_property (it, Qline_spacing);
25808 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25809 }
25810 if (INTEGERP (spacing))
25811 {
25812 extra_line_spacing = XINT (spacing);
25813 if (!NILP (total_height))
25814 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25815 }
25816 }
25817 }
25818 else /* i.e. (it->char_to_display == '\t') */
25819 {
25820 if (font->space_width > 0)
25821 {
25822 int tab_width = it->tab_width * font->space_width;
25823 int x = it->current_x + it->continuation_lines_width;
25824 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25825
25826 /* If the distance from the current position to the next tab
25827 stop is less than a space character width, use the
25828 tab stop after that. */
25829 if (next_tab_x - x < font->space_width)
25830 next_tab_x += tab_width;
25831
25832 it->pixel_width = next_tab_x - x;
25833 it->nglyphs = 1;
25834 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25835 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25836
25837 if (it->glyph_row)
25838 {
25839 append_stretch_glyph (it, it->object, it->pixel_width,
25840 it->ascent + it->descent, it->ascent);
25841 }
25842 }
25843 else
25844 {
25845 it->pixel_width = 0;
25846 it->nglyphs = 1;
25847 }
25848 }
25849 }
25850 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25851 {
25852 /* A static composition.
25853
25854 Note: A composition is represented as one glyph in the
25855 glyph matrix. There are no padding glyphs.
25856
25857 Important note: pixel_width, ascent, and descent are the
25858 values of what is drawn by draw_glyphs (i.e. the values of
25859 the overall glyphs composed). */
25860 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25861 int boff; /* baseline offset */
25862 struct composition *cmp = composition_table[it->cmp_it.id];
25863 int glyph_len = cmp->glyph_len;
25864 struct font *font = face->font;
25865
25866 it->nglyphs = 1;
25867
25868 /* If we have not yet calculated pixel size data of glyphs of
25869 the composition for the current face font, calculate them
25870 now. Theoretically, we have to check all fonts for the
25871 glyphs, but that requires much time and memory space. So,
25872 here we check only the font of the first glyph. This may
25873 lead to incorrect display, but it's very rare, and C-l
25874 (recenter-top-bottom) can correct the display anyway. */
25875 if (! cmp->font || cmp->font != font)
25876 {
25877 /* Ascent and descent of the font of the first character
25878 of this composition (adjusted by baseline offset).
25879 Ascent and descent of overall glyphs should not be less
25880 than these, respectively. */
25881 int font_ascent, font_descent, font_height;
25882 /* Bounding box of the overall glyphs. */
25883 int leftmost, rightmost, lowest, highest;
25884 int lbearing, rbearing;
25885 int i, width, ascent, descent;
25886 int left_padded = 0, right_padded = 0;
25887 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25888 XChar2b char2b;
25889 struct font_metrics *pcm;
25890 int font_not_found_p;
25891 ptrdiff_t pos;
25892
25893 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25894 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25895 break;
25896 if (glyph_len < cmp->glyph_len)
25897 right_padded = 1;
25898 for (i = 0; i < glyph_len; i++)
25899 {
25900 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25901 break;
25902 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25903 }
25904 if (i > 0)
25905 left_padded = 1;
25906
25907 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25908 : IT_CHARPOS (*it));
25909 /* If no suitable font is found, use the default font. */
25910 font_not_found_p = font == NULL;
25911 if (font_not_found_p)
25912 {
25913 face = face->ascii_face;
25914 font = face->font;
25915 }
25916 boff = font->baseline_offset;
25917 if (font->vertical_centering)
25918 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25919 font_ascent = FONT_BASE (font) + boff;
25920 font_descent = FONT_DESCENT (font) - boff;
25921 font_height = FONT_HEIGHT (font);
25922
25923 cmp->font = font;
25924
25925 pcm = NULL;
25926 if (! font_not_found_p)
25927 {
25928 get_char_face_and_encoding (it->f, c, it->face_id,
25929 &char2b, 0);
25930 pcm = get_per_char_metric (font, &char2b);
25931 }
25932
25933 /* Initialize the bounding box. */
25934 if (pcm)
25935 {
25936 width = cmp->glyph_len > 0 ? pcm->width : 0;
25937 ascent = pcm->ascent;
25938 descent = pcm->descent;
25939 lbearing = pcm->lbearing;
25940 rbearing = pcm->rbearing;
25941 }
25942 else
25943 {
25944 width = cmp->glyph_len > 0 ? font->space_width : 0;
25945 ascent = FONT_BASE (font);
25946 descent = FONT_DESCENT (font);
25947 lbearing = 0;
25948 rbearing = width;
25949 }
25950
25951 rightmost = width;
25952 leftmost = 0;
25953 lowest = - descent + boff;
25954 highest = ascent + boff;
25955
25956 if (! font_not_found_p
25957 && font->default_ascent
25958 && CHAR_TABLE_P (Vuse_default_ascent)
25959 && !NILP (Faref (Vuse_default_ascent,
25960 make_number (it->char_to_display))))
25961 highest = font->default_ascent + boff;
25962
25963 /* Draw the first glyph at the normal position. It may be
25964 shifted to right later if some other glyphs are drawn
25965 at the left. */
25966 cmp->offsets[i * 2] = 0;
25967 cmp->offsets[i * 2 + 1] = boff;
25968 cmp->lbearing = lbearing;
25969 cmp->rbearing = rbearing;
25970
25971 /* Set cmp->offsets for the remaining glyphs. */
25972 for (i++; i < glyph_len; i++)
25973 {
25974 int left, right, btm, top;
25975 int ch = COMPOSITION_GLYPH (cmp, i);
25976 int face_id;
25977 struct face *this_face;
25978
25979 if (ch == '\t')
25980 ch = ' ';
25981 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25982 this_face = FACE_FROM_ID (it->f, face_id);
25983 font = this_face->font;
25984
25985 if (font == NULL)
25986 pcm = NULL;
25987 else
25988 {
25989 get_char_face_and_encoding (it->f, ch, face_id,
25990 &char2b, 0);
25991 pcm = get_per_char_metric (font, &char2b);
25992 }
25993 if (! pcm)
25994 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25995 else
25996 {
25997 width = pcm->width;
25998 ascent = pcm->ascent;
25999 descent = pcm->descent;
26000 lbearing = pcm->lbearing;
26001 rbearing = pcm->rbearing;
26002 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26003 {
26004 /* Relative composition with or without
26005 alternate chars. */
26006 left = (leftmost + rightmost - width) / 2;
26007 btm = - descent + boff;
26008 if (font->relative_compose
26009 && (! CHAR_TABLE_P (Vignore_relative_composition)
26010 || NILP (Faref (Vignore_relative_composition,
26011 make_number (ch)))))
26012 {
26013
26014 if (- descent >= font->relative_compose)
26015 /* One extra pixel between two glyphs. */
26016 btm = highest + 1;
26017 else if (ascent <= 0)
26018 /* One extra pixel between two glyphs. */
26019 btm = lowest - 1 - ascent - descent;
26020 }
26021 }
26022 else
26023 {
26024 /* A composition rule is specified by an integer
26025 value that encodes global and new reference
26026 points (GREF and NREF). GREF and NREF are
26027 specified by numbers as below:
26028
26029 0---1---2 -- ascent
26030 | |
26031 | |
26032 | |
26033 9--10--11 -- center
26034 | |
26035 ---3---4---5--- baseline
26036 | |
26037 6---7---8 -- descent
26038 */
26039 int rule = COMPOSITION_RULE (cmp, i);
26040 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26041
26042 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26043 grefx = gref % 3, nrefx = nref % 3;
26044 grefy = gref / 3, nrefy = nref / 3;
26045 if (xoff)
26046 xoff = font_height * (xoff - 128) / 256;
26047 if (yoff)
26048 yoff = font_height * (yoff - 128) / 256;
26049
26050 left = (leftmost
26051 + grefx * (rightmost - leftmost) / 2
26052 - nrefx * width / 2
26053 + xoff);
26054
26055 btm = ((grefy == 0 ? highest
26056 : grefy == 1 ? 0
26057 : grefy == 2 ? lowest
26058 : (highest + lowest) / 2)
26059 - (nrefy == 0 ? ascent + descent
26060 : nrefy == 1 ? descent - boff
26061 : nrefy == 2 ? 0
26062 : (ascent + descent) / 2)
26063 + yoff);
26064 }
26065
26066 cmp->offsets[i * 2] = left;
26067 cmp->offsets[i * 2 + 1] = btm + descent;
26068
26069 /* Update the bounding box of the overall glyphs. */
26070 if (width > 0)
26071 {
26072 right = left + width;
26073 if (left < leftmost)
26074 leftmost = left;
26075 if (right > rightmost)
26076 rightmost = right;
26077 }
26078 top = btm + descent + ascent;
26079 if (top > highest)
26080 highest = top;
26081 if (btm < lowest)
26082 lowest = btm;
26083
26084 if (cmp->lbearing > left + lbearing)
26085 cmp->lbearing = left + lbearing;
26086 if (cmp->rbearing < left + rbearing)
26087 cmp->rbearing = left + rbearing;
26088 }
26089 }
26090
26091 /* If there are glyphs whose x-offsets are negative,
26092 shift all glyphs to the right and make all x-offsets
26093 non-negative. */
26094 if (leftmost < 0)
26095 {
26096 for (i = 0; i < cmp->glyph_len; i++)
26097 cmp->offsets[i * 2] -= leftmost;
26098 rightmost -= leftmost;
26099 cmp->lbearing -= leftmost;
26100 cmp->rbearing -= leftmost;
26101 }
26102
26103 if (left_padded && cmp->lbearing < 0)
26104 {
26105 for (i = 0; i < cmp->glyph_len; i++)
26106 cmp->offsets[i * 2] -= cmp->lbearing;
26107 rightmost -= cmp->lbearing;
26108 cmp->rbearing -= cmp->lbearing;
26109 cmp->lbearing = 0;
26110 }
26111 if (right_padded && rightmost < cmp->rbearing)
26112 {
26113 rightmost = cmp->rbearing;
26114 }
26115
26116 cmp->pixel_width = rightmost;
26117 cmp->ascent = highest;
26118 cmp->descent = - lowest;
26119 if (cmp->ascent < font_ascent)
26120 cmp->ascent = font_ascent;
26121 if (cmp->descent < font_descent)
26122 cmp->descent = font_descent;
26123 }
26124
26125 if (it->glyph_row
26126 && (cmp->lbearing < 0
26127 || cmp->rbearing > cmp->pixel_width))
26128 it->glyph_row->contains_overlapping_glyphs_p = 1;
26129
26130 it->pixel_width = cmp->pixel_width;
26131 it->ascent = it->phys_ascent = cmp->ascent;
26132 it->descent = it->phys_descent = cmp->descent;
26133 if (face->box != FACE_NO_BOX)
26134 {
26135 int thick = face->box_line_width;
26136
26137 if (thick > 0)
26138 {
26139 it->ascent += thick;
26140 it->descent += thick;
26141 }
26142 else
26143 thick = - thick;
26144
26145 if (it->start_of_box_run_p)
26146 it->pixel_width += thick;
26147 if (it->end_of_box_run_p)
26148 it->pixel_width += thick;
26149 }
26150
26151 /* If face has an overline, add the height of the overline
26152 (1 pixel) and a 1 pixel margin to the character height. */
26153 if (face->overline_p)
26154 it->ascent += overline_margin;
26155
26156 take_vertical_position_into_account (it);
26157 if (it->ascent < 0)
26158 it->ascent = 0;
26159 if (it->descent < 0)
26160 it->descent = 0;
26161
26162 if (it->glyph_row && cmp->glyph_len > 0)
26163 append_composite_glyph (it);
26164 }
26165 else if (it->what == IT_COMPOSITION)
26166 {
26167 /* A dynamic (automatic) composition. */
26168 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26169 Lisp_Object gstring;
26170 struct font_metrics metrics;
26171
26172 it->nglyphs = 1;
26173
26174 gstring = composition_gstring_from_id (it->cmp_it.id);
26175 it->pixel_width
26176 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26177 &metrics);
26178 if (it->glyph_row
26179 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26180 it->glyph_row->contains_overlapping_glyphs_p = 1;
26181 it->ascent = it->phys_ascent = metrics.ascent;
26182 it->descent = it->phys_descent = metrics.descent;
26183 if (face->box != FACE_NO_BOX)
26184 {
26185 int thick = face->box_line_width;
26186
26187 if (thick > 0)
26188 {
26189 it->ascent += thick;
26190 it->descent += thick;
26191 }
26192 else
26193 thick = - thick;
26194
26195 if (it->start_of_box_run_p)
26196 it->pixel_width += thick;
26197 if (it->end_of_box_run_p)
26198 it->pixel_width += thick;
26199 }
26200 /* If face has an overline, add the height of the overline
26201 (1 pixel) and a 1 pixel margin to the character height. */
26202 if (face->overline_p)
26203 it->ascent += overline_margin;
26204 take_vertical_position_into_account (it);
26205 if (it->ascent < 0)
26206 it->ascent = 0;
26207 if (it->descent < 0)
26208 it->descent = 0;
26209
26210 if (it->glyph_row)
26211 append_composite_glyph (it);
26212 }
26213 else if (it->what == IT_GLYPHLESS)
26214 produce_glyphless_glyph (it, 0, Qnil);
26215 else if (it->what == IT_IMAGE)
26216 produce_image_glyph (it);
26217 else if (it->what == IT_STRETCH)
26218 produce_stretch_glyph (it);
26219
26220 done:
26221 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26222 because this isn't true for images with `:ascent 100'. */
26223 eassert (it->ascent >= 0 && it->descent >= 0);
26224 if (it->area == TEXT_AREA)
26225 it->current_x += it->pixel_width;
26226
26227 if (extra_line_spacing > 0)
26228 {
26229 it->descent += extra_line_spacing;
26230 if (extra_line_spacing > it->max_extra_line_spacing)
26231 it->max_extra_line_spacing = extra_line_spacing;
26232 }
26233
26234 it->max_ascent = max (it->max_ascent, it->ascent);
26235 it->max_descent = max (it->max_descent, it->descent);
26236 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26237 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26238 }
26239
26240 /* EXPORT for RIF:
26241 Output LEN glyphs starting at START at the nominal cursor position.
26242 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26243 being updated, and UPDATED_AREA is the area of that row being updated. */
26244
26245 void
26246 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26247 struct glyph *start, enum glyph_row_area updated_area, int len)
26248 {
26249 int x, hpos, chpos = w->phys_cursor.hpos;
26250
26251 eassert (updated_row);
26252 /* When the window is hscrolled, cursor hpos can legitimately be out
26253 of bounds, but we draw the cursor at the corresponding window
26254 margin in that case. */
26255 if (!updated_row->reversed_p && chpos < 0)
26256 chpos = 0;
26257 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26258 chpos = updated_row->used[TEXT_AREA] - 1;
26259
26260 block_input ();
26261
26262 /* Write glyphs. */
26263
26264 hpos = start - updated_row->glyphs[updated_area];
26265 x = draw_glyphs (w, w->output_cursor.x,
26266 updated_row, updated_area,
26267 hpos, hpos + len,
26268 DRAW_NORMAL_TEXT, 0);
26269
26270 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26271 if (updated_area == TEXT_AREA
26272 && w->phys_cursor_on_p
26273 && w->phys_cursor.vpos == w->output_cursor.vpos
26274 && chpos >= hpos
26275 && chpos < hpos + len)
26276 w->phys_cursor_on_p = 0;
26277
26278 unblock_input ();
26279
26280 /* Advance the output cursor. */
26281 w->output_cursor.hpos += len;
26282 w->output_cursor.x = x;
26283 }
26284
26285
26286 /* EXPORT for RIF:
26287 Insert LEN glyphs from START at the nominal cursor position. */
26288
26289 void
26290 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26291 struct glyph *start, enum glyph_row_area updated_area, int len)
26292 {
26293 struct frame *f;
26294 int line_height, shift_by_width, shifted_region_width;
26295 struct glyph_row *row;
26296 struct glyph *glyph;
26297 int frame_x, frame_y;
26298 ptrdiff_t hpos;
26299
26300 eassert (updated_row);
26301 block_input ();
26302 f = XFRAME (WINDOW_FRAME (w));
26303
26304 /* Get the height of the line we are in. */
26305 row = updated_row;
26306 line_height = row->height;
26307
26308 /* Get the width of the glyphs to insert. */
26309 shift_by_width = 0;
26310 for (glyph = start; glyph < start + len; ++glyph)
26311 shift_by_width += glyph->pixel_width;
26312
26313 /* Get the width of the region to shift right. */
26314 shifted_region_width = (window_box_width (w, updated_area)
26315 - w->output_cursor.x
26316 - shift_by_width);
26317
26318 /* Shift right. */
26319 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26320 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26321
26322 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26323 line_height, shift_by_width);
26324
26325 /* Write the glyphs. */
26326 hpos = start - row->glyphs[updated_area];
26327 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26328 hpos, hpos + len,
26329 DRAW_NORMAL_TEXT, 0);
26330
26331 /* Advance the output cursor. */
26332 w->output_cursor.hpos += len;
26333 w->output_cursor.x += shift_by_width;
26334 unblock_input ();
26335 }
26336
26337
26338 /* EXPORT for RIF:
26339 Erase the current text line from the nominal cursor position
26340 (inclusive) to pixel column TO_X (exclusive). The idea is that
26341 everything from TO_X onward is already erased.
26342
26343 TO_X is a pixel position relative to UPDATED_AREA of currently
26344 updated window W. TO_X == -1 means clear to the end of this area. */
26345
26346 void
26347 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26348 enum glyph_row_area updated_area, int to_x)
26349 {
26350 struct frame *f;
26351 int max_x, min_y, max_y;
26352 int from_x, from_y, to_y;
26353
26354 eassert (updated_row);
26355 f = XFRAME (w->frame);
26356
26357 if (updated_row->full_width_p)
26358 max_x = (WINDOW_PIXEL_WIDTH (w)
26359 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26360 else
26361 max_x = window_box_width (w, updated_area);
26362 max_y = window_text_bottom_y (w);
26363
26364 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26365 of window. For TO_X > 0, truncate to end of drawing area. */
26366 if (to_x == 0)
26367 return;
26368 else if (to_x < 0)
26369 to_x = max_x;
26370 else
26371 to_x = min (to_x, max_x);
26372
26373 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26374
26375 /* Notice if the cursor will be cleared by this operation. */
26376 if (!updated_row->full_width_p)
26377 notice_overwritten_cursor (w, updated_area,
26378 w->output_cursor.x, -1,
26379 updated_row->y,
26380 MATRIX_ROW_BOTTOM_Y (updated_row));
26381
26382 from_x = w->output_cursor.x;
26383
26384 /* Translate to frame coordinates. */
26385 if (updated_row->full_width_p)
26386 {
26387 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26388 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26389 }
26390 else
26391 {
26392 int area_left = window_box_left (w, updated_area);
26393 from_x += area_left;
26394 to_x += area_left;
26395 }
26396
26397 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26398 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26399 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26400
26401 /* Prevent inadvertently clearing to end of the X window. */
26402 if (to_x > from_x && to_y > from_y)
26403 {
26404 block_input ();
26405 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26406 to_x - from_x, to_y - from_y);
26407 unblock_input ();
26408 }
26409 }
26410
26411 #endif /* HAVE_WINDOW_SYSTEM */
26412
26413
26414 \f
26415 /***********************************************************************
26416 Cursor types
26417 ***********************************************************************/
26418
26419 /* Value is the internal representation of the specified cursor type
26420 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26421 of the bar cursor. */
26422
26423 static enum text_cursor_kinds
26424 get_specified_cursor_type (Lisp_Object arg, int *width)
26425 {
26426 enum text_cursor_kinds type;
26427
26428 if (NILP (arg))
26429 return NO_CURSOR;
26430
26431 if (EQ (arg, Qbox))
26432 return FILLED_BOX_CURSOR;
26433
26434 if (EQ (arg, Qhollow))
26435 return HOLLOW_BOX_CURSOR;
26436
26437 if (EQ (arg, Qbar))
26438 {
26439 *width = 2;
26440 return BAR_CURSOR;
26441 }
26442
26443 if (CONSP (arg)
26444 && EQ (XCAR (arg), Qbar)
26445 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26446 {
26447 *width = XINT (XCDR (arg));
26448 return BAR_CURSOR;
26449 }
26450
26451 if (EQ (arg, Qhbar))
26452 {
26453 *width = 2;
26454 return HBAR_CURSOR;
26455 }
26456
26457 if (CONSP (arg)
26458 && EQ (XCAR (arg), Qhbar)
26459 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26460 {
26461 *width = XINT (XCDR (arg));
26462 return HBAR_CURSOR;
26463 }
26464
26465 /* Treat anything unknown as "hollow box cursor".
26466 It was bad to signal an error; people have trouble fixing
26467 .Xdefaults with Emacs, when it has something bad in it. */
26468 type = HOLLOW_BOX_CURSOR;
26469
26470 return type;
26471 }
26472
26473 /* Set the default cursor types for specified frame. */
26474 void
26475 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26476 {
26477 int width = 1;
26478 Lisp_Object tem;
26479
26480 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26481 FRAME_CURSOR_WIDTH (f) = width;
26482
26483 /* By default, set up the blink-off state depending on the on-state. */
26484
26485 tem = Fassoc (arg, Vblink_cursor_alist);
26486 if (!NILP (tem))
26487 {
26488 FRAME_BLINK_OFF_CURSOR (f)
26489 = get_specified_cursor_type (XCDR (tem), &width);
26490 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26491 }
26492 else
26493 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26494
26495 /* Make sure the cursor gets redrawn. */
26496 f->cursor_type_changed = 1;
26497 }
26498
26499
26500 #ifdef HAVE_WINDOW_SYSTEM
26501
26502 /* Return the cursor we want to be displayed in window W. Return
26503 width of bar/hbar cursor through WIDTH arg. Return with
26504 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26505 (i.e. if the `system caret' should track this cursor).
26506
26507 In a mini-buffer window, we want the cursor only to appear if we
26508 are reading input from this window. For the selected window, we
26509 want the cursor type given by the frame parameter or buffer local
26510 setting of cursor-type. If explicitly marked off, draw no cursor.
26511 In all other cases, we want a hollow box cursor. */
26512
26513 static enum text_cursor_kinds
26514 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26515 int *active_cursor)
26516 {
26517 struct frame *f = XFRAME (w->frame);
26518 struct buffer *b = XBUFFER (w->contents);
26519 int cursor_type = DEFAULT_CURSOR;
26520 Lisp_Object alt_cursor;
26521 int non_selected = 0;
26522
26523 *active_cursor = 1;
26524
26525 /* Echo area */
26526 if (cursor_in_echo_area
26527 && FRAME_HAS_MINIBUF_P (f)
26528 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26529 {
26530 if (w == XWINDOW (echo_area_window))
26531 {
26532 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26533 {
26534 *width = FRAME_CURSOR_WIDTH (f);
26535 return FRAME_DESIRED_CURSOR (f);
26536 }
26537 else
26538 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26539 }
26540
26541 *active_cursor = 0;
26542 non_selected = 1;
26543 }
26544
26545 /* Detect a nonselected window or nonselected frame. */
26546 else if (w != XWINDOW (f->selected_window)
26547 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26548 {
26549 *active_cursor = 0;
26550
26551 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26552 return NO_CURSOR;
26553
26554 non_selected = 1;
26555 }
26556
26557 /* Never display a cursor in a window in which cursor-type is nil. */
26558 if (NILP (BVAR (b, cursor_type)))
26559 return NO_CURSOR;
26560
26561 /* Get the normal cursor type for this window. */
26562 if (EQ (BVAR (b, cursor_type), Qt))
26563 {
26564 cursor_type = FRAME_DESIRED_CURSOR (f);
26565 *width = FRAME_CURSOR_WIDTH (f);
26566 }
26567 else
26568 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26569
26570 /* Use cursor-in-non-selected-windows instead
26571 for non-selected window or frame. */
26572 if (non_selected)
26573 {
26574 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26575 if (!EQ (Qt, alt_cursor))
26576 return get_specified_cursor_type (alt_cursor, width);
26577 /* t means modify the normal cursor type. */
26578 if (cursor_type == FILLED_BOX_CURSOR)
26579 cursor_type = HOLLOW_BOX_CURSOR;
26580 else if (cursor_type == BAR_CURSOR && *width > 1)
26581 --*width;
26582 return cursor_type;
26583 }
26584
26585 /* Use normal cursor if not blinked off. */
26586 if (!w->cursor_off_p)
26587 {
26588 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26589 {
26590 if (cursor_type == FILLED_BOX_CURSOR)
26591 {
26592 /* Using a block cursor on large images can be very annoying.
26593 So use a hollow cursor for "large" images.
26594 If image is not transparent (no mask), also use hollow cursor. */
26595 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26596 if (img != NULL && IMAGEP (img->spec))
26597 {
26598 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26599 where N = size of default frame font size.
26600 This should cover most of the "tiny" icons people may use. */
26601 if (!img->mask
26602 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26603 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26604 cursor_type = HOLLOW_BOX_CURSOR;
26605 }
26606 }
26607 else if (cursor_type != NO_CURSOR)
26608 {
26609 /* Display current only supports BOX and HOLLOW cursors for images.
26610 So for now, unconditionally use a HOLLOW cursor when cursor is
26611 not a solid box cursor. */
26612 cursor_type = HOLLOW_BOX_CURSOR;
26613 }
26614 }
26615 return cursor_type;
26616 }
26617
26618 /* Cursor is blinked off, so determine how to "toggle" it. */
26619
26620 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26621 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26622 return get_specified_cursor_type (XCDR (alt_cursor), width);
26623
26624 /* Then see if frame has specified a specific blink off cursor type. */
26625 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26626 {
26627 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26628 return FRAME_BLINK_OFF_CURSOR (f);
26629 }
26630
26631 #if 0
26632 /* Some people liked having a permanently visible blinking cursor,
26633 while others had very strong opinions against it. So it was
26634 decided to remove it. KFS 2003-09-03 */
26635
26636 /* Finally perform built-in cursor blinking:
26637 filled box <-> hollow box
26638 wide [h]bar <-> narrow [h]bar
26639 narrow [h]bar <-> no cursor
26640 other type <-> no cursor */
26641
26642 if (cursor_type == FILLED_BOX_CURSOR)
26643 return HOLLOW_BOX_CURSOR;
26644
26645 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26646 {
26647 *width = 1;
26648 return cursor_type;
26649 }
26650 #endif
26651
26652 return NO_CURSOR;
26653 }
26654
26655
26656 /* Notice when the text cursor of window W has been completely
26657 overwritten by a drawing operation that outputs glyphs in AREA
26658 starting at X0 and ending at X1 in the line starting at Y0 and
26659 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26660 the rest of the line after X0 has been written. Y coordinates
26661 are window-relative. */
26662
26663 static void
26664 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26665 int x0, int x1, int y0, int y1)
26666 {
26667 int cx0, cx1, cy0, cy1;
26668 struct glyph_row *row;
26669
26670 if (!w->phys_cursor_on_p)
26671 return;
26672 if (area != TEXT_AREA)
26673 return;
26674
26675 if (w->phys_cursor.vpos < 0
26676 || w->phys_cursor.vpos >= w->current_matrix->nrows
26677 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26678 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26679 return;
26680
26681 if (row->cursor_in_fringe_p)
26682 {
26683 row->cursor_in_fringe_p = 0;
26684 draw_fringe_bitmap (w, row, row->reversed_p);
26685 w->phys_cursor_on_p = 0;
26686 return;
26687 }
26688
26689 cx0 = w->phys_cursor.x;
26690 cx1 = cx0 + w->phys_cursor_width;
26691 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26692 return;
26693
26694 /* The cursor image will be completely removed from the
26695 screen if the output area intersects the cursor area in
26696 y-direction. When we draw in [y0 y1[, and some part of
26697 the cursor is at y < y0, that part must have been drawn
26698 before. When scrolling, the cursor is erased before
26699 actually scrolling, so we don't come here. When not
26700 scrolling, the rows above the old cursor row must have
26701 changed, and in this case these rows must have written
26702 over the cursor image.
26703
26704 Likewise if part of the cursor is below y1, with the
26705 exception of the cursor being in the first blank row at
26706 the buffer and window end because update_text_area
26707 doesn't draw that row. (Except when it does, but
26708 that's handled in update_text_area.) */
26709
26710 cy0 = w->phys_cursor.y;
26711 cy1 = cy0 + w->phys_cursor_height;
26712 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26713 return;
26714
26715 w->phys_cursor_on_p = 0;
26716 }
26717
26718 #endif /* HAVE_WINDOW_SYSTEM */
26719
26720 \f
26721 /************************************************************************
26722 Mouse Face
26723 ************************************************************************/
26724
26725 #ifdef HAVE_WINDOW_SYSTEM
26726
26727 /* EXPORT for RIF:
26728 Fix the display of area AREA of overlapping row ROW in window W
26729 with respect to the overlapping part OVERLAPS. */
26730
26731 void
26732 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26733 enum glyph_row_area area, int overlaps)
26734 {
26735 int i, x;
26736
26737 block_input ();
26738
26739 x = 0;
26740 for (i = 0; i < row->used[area];)
26741 {
26742 if (row->glyphs[area][i].overlaps_vertically_p)
26743 {
26744 int start = i, start_x = x;
26745
26746 do
26747 {
26748 x += row->glyphs[area][i].pixel_width;
26749 ++i;
26750 }
26751 while (i < row->used[area]
26752 && row->glyphs[area][i].overlaps_vertically_p);
26753
26754 draw_glyphs (w, start_x, row, area,
26755 start, i,
26756 DRAW_NORMAL_TEXT, overlaps);
26757 }
26758 else
26759 {
26760 x += row->glyphs[area][i].pixel_width;
26761 ++i;
26762 }
26763 }
26764
26765 unblock_input ();
26766 }
26767
26768
26769 /* EXPORT:
26770 Draw the cursor glyph of window W in glyph row ROW. See the
26771 comment of draw_glyphs for the meaning of HL. */
26772
26773 void
26774 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26775 enum draw_glyphs_face hl)
26776 {
26777 /* If cursor hpos is out of bounds, don't draw garbage. This can
26778 happen in mini-buffer windows when switching between echo area
26779 glyphs and mini-buffer. */
26780 if ((row->reversed_p
26781 ? (w->phys_cursor.hpos >= 0)
26782 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26783 {
26784 int on_p = w->phys_cursor_on_p;
26785 int x1;
26786 int hpos = w->phys_cursor.hpos;
26787
26788 /* When the window is hscrolled, cursor hpos can legitimately be
26789 out of bounds, but we draw the cursor at the corresponding
26790 window margin in that case. */
26791 if (!row->reversed_p && hpos < 0)
26792 hpos = 0;
26793 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26794 hpos = row->used[TEXT_AREA] - 1;
26795
26796 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26797 hl, 0);
26798 w->phys_cursor_on_p = on_p;
26799
26800 if (hl == DRAW_CURSOR)
26801 w->phys_cursor_width = x1 - w->phys_cursor.x;
26802 /* When we erase the cursor, and ROW is overlapped by other
26803 rows, make sure that these overlapping parts of other rows
26804 are redrawn. */
26805 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26806 {
26807 w->phys_cursor_width = x1 - w->phys_cursor.x;
26808
26809 if (row > w->current_matrix->rows
26810 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26811 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26812 OVERLAPS_ERASED_CURSOR);
26813
26814 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26815 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26816 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26817 OVERLAPS_ERASED_CURSOR);
26818 }
26819 }
26820 }
26821
26822
26823 /* Erase the image of a cursor of window W from the screen. */
26824
26825 #ifndef HAVE_NTGUI
26826 static
26827 #endif
26828 void
26829 erase_phys_cursor (struct window *w)
26830 {
26831 struct frame *f = XFRAME (w->frame);
26832 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26833 int hpos = w->phys_cursor.hpos;
26834 int vpos = w->phys_cursor.vpos;
26835 int mouse_face_here_p = 0;
26836 struct glyph_matrix *active_glyphs = w->current_matrix;
26837 struct glyph_row *cursor_row;
26838 struct glyph *cursor_glyph;
26839 enum draw_glyphs_face hl;
26840
26841 /* No cursor displayed or row invalidated => nothing to do on the
26842 screen. */
26843 if (w->phys_cursor_type == NO_CURSOR)
26844 goto mark_cursor_off;
26845
26846 /* VPOS >= active_glyphs->nrows means that window has been resized.
26847 Don't bother to erase the cursor. */
26848 if (vpos >= active_glyphs->nrows)
26849 goto mark_cursor_off;
26850
26851 /* If row containing cursor is marked invalid, there is nothing we
26852 can do. */
26853 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26854 if (!cursor_row->enabled_p)
26855 goto mark_cursor_off;
26856
26857 /* If line spacing is > 0, old cursor may only be partially visible in
26858 window after split-window. So adjust visible height. */
26859 cursor_row->visible_height = min (cursor_row->visible_height,
26860 window_text_bottom_y (w) - cursor_row->y);
26861
26862 /* If row is completely invisible, don't attempt to delete a cursor which
26863 isn't there. This can happen if cursor is at top of a window, and
26864 we switch to a buffer with a header line in that window. */
26865 if (cursor_row->visible_height <= 0)
26866 goto mark_cursor_off;
26867
26868 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26869 if (cursor_row->cursor_in_fringe_p)
26870 {
26871 cursor_row->cursor_in_fringe_p = 0;
26872 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26873 goto mark_cursor_off;
26874 }
26875
26876 /* This can happen when the new row is shorter than the old one.
26877 In this case, either draw_glyphs or clear_end_of_line
26878 should have cleared the cursor. Note that we wouldn't be
26879 able to erase the cursor in this case because we don't have a
26880 cursor glyph at hand. */
26881 if ((cursor_row->reversed_p
26882 ? (w->phys_cursor.hpos < 0)
26883 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26884 goto mark_cursor_off;
26885
26886 /* When the window is hscrolled, cursor hpos can legitimately be out
26887 of bounds, but we draw the cursor at the corresponding window
26888 margin in that case. */
26889 if (!cursor_row->reversed_p && hpos < 0)
26890 hpos = 0;
26891 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26892 hpos = cursor_row->used[TEXT_AREA] - 1;
26893
26894 /* If the cursor is in the mouse face area, redisplay that when
26895 we clear the cursor. */
26896 if (! NILP (hlinfo->mouse_face_window)
26897 && coords_in_mouse_face_p (w, hpos, vpos)
26898 /* Don't redraw the cursor's spot in mouse face if it is at the
26899 end of a line (on a newline). The cursor appears there, but
26900 mouse highlighting does not. */
26901 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26902 mouse_face_here_p = 1;
26903
26904 /* Maybe clear the display under the cursor. */
26905 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26906 {
26907 int x, y, left_x;
26908 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26909 int width;
26910
26911 cursor_glyph = get_phys_cursor_glyph (w);
26912 if (cursor_glyph == NULL)
26913 goto mark_cursor_off;
26914
26915 width = cursor_glyph->pixel_width;
26916 left_x = window_box_left_offset (w, TEXT_AREA);
26917 x = w->phys_cursor.x;
26918 if (x < left_x)
26919 width -= left_x - x;
26920 width = min (width, window_box_width (w, TEXT_AREA) - x);
26921 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26922 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26923
26924 if (width > 0)
26925 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26926 }
26927
26928 /* Erase the cursor by redrawing the character underneath it. */
26929 if (mouse_face_here_p)
26930 hl = DRAW_MOUSE_FACE;
26931 else
26932 hl = DRAW_NORMAL_TEXT;
26933 draw_phys_cursor_glyph (w, cursor_row, hl);
26934
26935 mark_cursor_off:
26936 w->phys_cursor_on_p = 0;
26937 w->phys_cursor_type = NO_CURSOR;
26938 }
26939
26940
26941 /* EXPORT:
26942 Display or clear cursor of window W. If ON is zero, clear the
26943 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26944 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26945
26946 void
26947 display_and_set_cursor (struct window *w, bool on,
26948 int hpos, int vpos, int x, int y)
26949 {
26950 struct frame *f = XFRAME (w->frame);
26951 int new_cursor_type;
26952 int new_cursor_width;
26953 int active_cursor;
26954 struct glyph_row *glyph_row;
26955 struct glyph *glyph;
26956
26957 /* This is pointless on invisible frames, and dangerous on garbaged
26958 windows and frames; in the latter case, the frame or window may
26959 be in the midst of changing its size, and x and y may be off the
26960 window. */
26961 if (! FRAME_VISIBLE_P (f)
26962 || FRAME_GARBAGED_P (f)
26963 || vpos >= w->current_matrix->nrows
26964 || hpos >= w->current_matrix->matrix_w)
26965 return;
26966
26967 /* If cursor is off and we want it off, return quickly. */
26968 if (!on && !w->phys_cursor_on_p)
26969 return;
26970
26971 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26972 /* If cursor row is not enabled, we don't really know where to
26973 display the cursor. */
26974 if (!glyph_row->enabled_p)
26975 {
26976 w->phys_cursor_on_p = 0;
26977 return;
26978 }
26979
26980 glyph = NULL;
26981 if (!glyph_row->exact_window_width_line_p
26982 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26983 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26984
26985 eassert (input_blocked_p ());
26986
26987 /* Set new_cursor_type to the cursor we want to be displayed. */
26988 new_cursor_type = get_window_cursor_type (w, glyph,
26989 &new_cursor_width, &active_cursor);
26990
26991 /* If cursor is currently being shown and we don't want it to be or
26992 it is in the wrong place, or the cursor type is not what we want,
26993 erase it. */
26994 if (w->phys_cursor_on_p
26995 && (!on
26996 || w->phys_cursor.x != x
26997 || w->phys_cursor.y != y
26998 || new_cursor_type != w->phys_cursor_type
26999 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27000 && new_cursor_width != w->phys_cursor_width)))
27001 erase_phys_cursor (w);
27002
27003 /* Don't check phys_cursor_on_p here because that flag is only set
27004 to zero in some cases where we know that the cursor has been
27005 completely erased, to avoid the extra work of erasing the cursor
27006 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27007 still not be visible, or it has only been partly erased. */
27008 if (on)
27009 {
27010 w->phys_cursor_ascent = glyph_row->ascent;
27011 w->phys_cursor_height = glyph_row->height;
27012
27013 /* Set phys_cursor_.* before x_draw_.* is called because some
27014 of them may need the information. */
27015 w->phys_cursor.x = x;
27016 w->phys_cursor.y = glyph_row->y;
27017 w->phys_cursor.hpos = hpos;
27018 w->phys_cursor.vpos = vpos;
27019 }
27020
27021 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27022 new_cursor_type, new_cursor_width,
27023 on, active_cursor);
27024 }
27025
27026
27027 /* Switch the display of W's cursor on or off, according to the value
27028 of ON. */
27029
27030 static void
27031 update_window_cursor (struct window *w, bool on)
27032 {
27033 /* Don't update cursor in windows whose frame is in the process
27034 of being deleted. */
27035 if (w->current_matrix)
27036 {
27037 int hpos = w->phys_cursor.hpos;
27038 int vpos = w->phys_cursor.vpos;
27039 struct glyph_row *row;
27040
27041 if (vpos >= w->current_matrix->nrows
27042 || hpos >= w->current_matrix->matrix_w)
27043 return;
27044
27045 row = MATRIX_ROW (w->current_matrix, vpos);
27046
27047 /* When the window is hscrolled, cursor hpos can legitimately be
27048 out of bounds, but we draw the cursor at the corresponding
27049 window margin in that case. */
27050 if (!row->reversed_p && hpos < 0)
27051 hpos = 0;
27052 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27053 hpos = row->used[TEXT_AREA] - 1;
27054
27055 block_input ();
27056 display_and_set_cursor (w, on, hpos, vpos,
27057 w->phys_cursor.x, w->phys_cursor.y);
27058 unblock_input ();
27059 }
27060 }
27061
27062
27063 /* Call update_window_cursor with parameter ON_P on all leaf windows
27064 in the window tree rooted at W. */
27065
27066 static void
27067 update_cursor_in_window_tree (struct window *w, bool on_p)
27068 {
27069 while (w)
27070 {
27071 if (WINDOWP (w->contents))
27072 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27073 else
27074 update_window_cursor (w, on_p);
27075
27076 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27077 }
27078 }
27079
27080
27081 /* EXPORT:
27082 Display the cursor on window W, or clear it, according to ON_P.
27083 Don't change the cursor's position. */
27084
27085 void
27086 x_update_cursor (struct frame *f, bool on_p)
27087 {
27088 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27089 }
27090
27091
27092 /* EXPORT:
27093 Clear the cursor of window W to background color, and mark the
27094 cursor as not shown. This is used when the text where the cursor
27095 is about to be rewritten. */
27096
27097 void
27098 x_clear_cursor (struct window *w)
27099 {
27100 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27101 update_window_cursor (w, 0);
27102 }
27103
27104 #endif /* HAVE_WINDOW_SYSTEM */
27105
27106 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27107 and MSDOS. */
27108 static void
27109 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27110 int start_hpos, int end_hpos,
27111 enum draw_glyphs_face draw)
27112 {
27113 #ifdef HAVE_WINDOW_SYSTEM
27114 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27115 {
27116 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27117 return;
27118 }
27119 #endif
27120 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27121 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27122 #endif
27123 }
27124
27125 /* Display the active region described by mouse_face_* according to DRAW. */
27126
27127 static void
27128 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27129 {
27130 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27131 struct frame *f = XFRAME (WINDOW_FRAME (w));
27132
27133 if (/* If window is in the process of being destroyed, don't bother
27134 to do anything. */
27135 w->current_matrix != NULL
27136 /* Don't update mouse highlight if hidden */
27137 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27138 /* Recognize when we are called to operate on rows that don't exist
27139 anymore. This can happen when a window is split. */
27140 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27141 {
27142 int phys_cursor_on_p = w->phys_cursor_on_p;
27143 struct glyph_row *row, *first, *last;
27144
27145 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27146 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27147
27148 for (row = first; row <= last && row->enabled_p; ++row)
27149 {
27150 int start_hpos, end_hpos, start_x;
27151
27152 /* For all but the first row, the highlight starts at column 0. */
27153 if (row == first)
27154 {
27155 /* R2L rows have BEG and END in reversed order, but the
27156 screen drawing geometry is always left to right. So
27157 we need to mirror the beginning and end of the
27158 highlighted area in R2L rows. */
27159 if (!row->reversed_p)
27160 {
27161 start_hpos = hlinfo->mouse_face_beg_col;
27162 start_x = hlinfo->mouse_face_beg_x;
27163 }
27164 else if (row == last)
27165 {
27166 start_hpos = hlinfo->mouse_face_end_col;
27167 start_x = hlinfo->mouse_face_end_x;
27168 }
27169 else
27170 {
27171 start_hpos = 0;
27172 start_x = 0;
27173 }
27174 }
27175 else if (row->reversed_p && row == last)
27176 {
27177 start_hpos = hlinfo->mouse_face_end_col;
27178 start_x = hlinfo->mouse_face_end_x;
27179 }
27180 else
27181 {
27182 start_hpos = 0;
27183 start_x = 0;
27184 }
27185
27186 if (row == last)
27187 {
27188 if (!row->reversed_p)
27189 end_hpos = hlinfo->mouse_face_end_col;
27190 else if (row == first)
27191 end_hpos = hlinfo->mouse_face_beg_col;
27192 else
27193 {
27194 end_hpos = row->used[TEXT_AREA];
27195 if (draw == DRAW_NORMAL_TEXT)
27196 row->fill_line_p = 1; /* Clear to end of line */
27197 }
27198 }
27199 else if (row->reversed_p && row == first)
27200 end_hpos = hlinfo->mouse_face_beg_col;
27201 else
27202 {
27203 end_hpos = row->used[TEXT_AREA];
27204 if (draw == DRAW_NORMAL_TEXT)
27205 row->fill_line_p = 1; /* Clear to end of line */
27206 }
27207
27208 if (end_hpos > start_hpos)
27209 {
27210 draw_row_with_mouse_face (w, start_x, row,
27211 start_hpos, end_hpos, draw);
27212
27213 row->mouse_face_p
27214 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27215 }
27216 }
27217
27218 #ifdef HAVE_WINDOW_SYSTEM
27219 /* When we've written over the cursor, arrange for it to
27220 be displayed again. */
27221 if (FRAME_WINDOW_P (f)
27222 && phys_cursor_on_p && !w->phys_cursor_on_p)
27223 {
27224 int hpos = w->phys_cursor.hpos;
27225
27226 /* When the window is hscrolled, cursor hpos can legitimately be
27227 out of bounds, but we draw the cursor at the corresponding
27228 window margin in that case. */
27229 if (!row->reversed_p && hpos < 0)
27230 hpos = 0;
27231 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27232 hpos = row->used[TEXT_AREA] - 1;
27233
27234 block_input ();
27235 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27236 w->phys_cursor.x, w->phys_cursor.y);
27237 unblock_input ();
27238 }
27239 #endif /* HAVE_WINDOW_SYSTEM */
27240 }
27241
27242 #ifdef HAVE_WINDOW_SYSTEM
27243 /* Change the mouse cursor. */
27244 if (FRAME_WINDOW_P (f))
27245 {
27246 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27247 if (draw == DRAW_NORMAL_TEXT
27248 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27249 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27250 else
27251 #endif
27252 if (draw == DRAW_MOUSE_FACE)
27253 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27254 else
27255 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27256 }
27257 #endif /* HAVE_WINDOW_SYSTEM */
27258 }
27259
27260 /* EXPORT:
27261 Clear out the mouse-highlighted active region.
27262 Redraw it un-highlighted first. Value is non-zero if mouse
27263 face was actually drawn unhighlighted. */
27264
27265 int
27266 clear_mouse_face (Mouse_HLInfo *hlinfo)
27267 {
27268 int cleared = 0;
27269
27270 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27271 {
27272 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27273 cleared = 1;
27274 }
27275
27276 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27277 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27278 hlinfo->mouse_face_window = Qnil;
27279 hlinfo->mouse_face_overlay = Qnil;
27280 return cleared;
27281 }
27282
27283 /* Return true if the coordinates HPOS and VPOS on windows W are
27284 within the mouse face on that window. */
27285 static bool
27286 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27287 {
27288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27289
27290 /* Quickly resolve the easy cases. */
27291 if (!(WINDOWP (hlinfo->mouse_face_window)
27292 && XWINDOW (hlinfo->mouse_face_window) == w))
27293 return false;
27294 if (vpos < hlinfo->mouse_face_beg_row
27295 || vpos > hlinfo->mouse_face_end_row)
27296 return false;
27297 if (vpos > hlinfo->mouse_face_beg_row
27298 && vpos < hlinfo->mouse_face_end_row)
27299 return true;
27300
27301 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27302 {
27303 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27304 {
27305 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27306 return true;
27307 }
27308 else if ((vpos == hlinfo->mouse_face_beg_row
27309 && hpos >= hlinfo->mouse_face_beg_col)
27310 || (vpos == hlinfo->mouse_face_end_row
27311 && hpos < hlinfo->mouse_face_end_col))
27312 return true;
27313 }
27314 else
27315 {
27316 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27317 {
27318 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27319 return true;
27320 }
27321 else if ((vpos == hlinfo->mouse_face_beg_row
27322 && hpos <= hlinfo->mouse_face_beg_col)
27323 || (vpos == hlinfo->mouse_face_end_row
27324 && hpos > hlinfo->mouse_face_end_col))
27325 return true;
27326 }
27327 return false;
27328 }
27329
27330
27331 /* EXPORT:
27332 True if physical cursor of window W is within mouse face. */
27333
27334 bool
27335 cursor_in_mouse_face_p (struct window *w)
27336 {
27337 int hpos = w->phys_cursor.hpos;
27338 int vpos = w->phys_cursor.vpos;
27339 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27340
27341 /* When the window is hscrolled, cursor hpos can legitimately be out
27342 of bounds, but we draw the cursor at the corresponding window
27343 margin in that case. */
27344 if (!row->reversed_p && hpos < 0)
27345 hpos = 0;
27346 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27347 hpos = row->used[TEXT_AREA] - 1;
27348
27349 return coords_in_mouse_face_p (w, hpos, vpos);
27350 }
27351
27352
27353 \f
27354 /* Find the glyph rows START_ROW and END_ROW of window W that display
27355 characters between buffer positions START_CHARPOS and END_CHARPOS
27356 (excluding END_CHARPOS). DISP_STRING is a display string that
27357 covers these buffer positions. This is similar to
27358 row_containing_pos, but is more accurate when bidi reordering makes
27359 buffer positions change non-linearly with glyph rows. */
27360 static void
27361 rows_from_pos_range (struct window *w,
27362 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27363 Lisp_Object disp_string,
27364 struct glyph_row **start, struct glyph_row **end)
27365 {
27366 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27367 int last_y = window_text_bottom_y (w);
27368 struct glyph_row *row;
27369
27370 *start = NULL;
27371 *end = NULL;
27372
27373 while (!first->enabled_p
27374 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27375 first++;
27376
27377 /* Find the START row. */
27378 for (row = first;
27379 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27380 row++)
27381 {
27382 /* A row can potentially be the START row if the range of the
27383 characters it displays intersects the range
27384 [START_CHARPOS..END_CHARPOS). */
27385 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27386 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27387 /* See the commentary in row_containing_pos, for the
27388 explanation of the complicated way to check whether
27389 some position is beyond the end of the characters
27390 displayed by a row. */
27391 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27392 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27393 && !row->ends_at_zv_p
27394 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27395 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27396 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27397 && !row->ends_at_zv_p
27398 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27399 {
27400 /* Found a candidate row. Now make sure at least one of the
27401 glyphs it displays has a charpos from the range
27402 [START_CHARPOS..END_CHARPOS).
27403
27404 This is not obvious because bidi reordering could make
27405 buffer positions of a row be 1,2,3,102,101,100, and if we
27406 want to highlight characters in [50..60), we don't want
27407 this row, even though [50..60) does intersect [1..103),
27408 the range of character positions given by the row's start
27409 and end positions. */
27410 struct glyph *g = row->glyphs[TEXT_AREA];
27411 struct glyph *e = g + row->used[TEXT_AREA];
27412
27413 while (g < e)
27414 {
27415 if (((BUFFERP (g->object) || INTEGERP (g->object))
27416 && start_charpos <= g->charpos && g->charpos < end_charpos)
27417 /* A glyph that comes from DISP_STRING is by
27418 definition to be highlighted. */
27419 || EQ (g->object, disp_string))
27420 *start = row;
27421 g++;
27422 }
27423 if (*start)
27424 break;
27425 }
27426 }
27427
27428 /* Find the END row. */
27429 if (!*start
27430 /* If the last row is partially visible, start looking for END
27431 from that row, instead of starting from FIRST. */
27432 && !(row->enabled_p
27433 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27434 row = first;
27435 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27436 {
27437 struct glyph_row *next = row + 1;
27438 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27439
27440 if (!next->enabled_p
27441 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27442 /* The first row >= START whose range of displayed characters
27443 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27444 is the row END + 1. */
27445 || (start_charpos < next_start
27446 && end_charpos < next_start)
27447 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27448 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27449 && !next->ends_at_zv_p
27450 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27451 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27452 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27453 && !next->ends_at_zv_p
27454 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27455 {
27456 *end = row;
27457 break;
27458 }
27459 else
27460 {
27461 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27462 but none of the characters it displays are in the range, it is
27463 also END + 1. */
27464 struct glyph *g = next->glyphs[TEXT_AREA];
27465 struct glyph *s = g;
27466 struct glyph *e = g + next->used[TEXT_AREA];
27467
27468 while (g < e)
27469 {
27470 if (((BUFFERP (g->object) || INTEGERP (g->object))
27471 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27472 /* If the buffer position of the first glyph in
27473 the row is equal to END_CHARPOS, it means
27474 the last character to be highlighted is the
27475 newline of ROW, and we must consider NEXT as
27476 END, not END+1. */
27477 || (((!next->reversed_p && g == s)
27478 || (next->reversed_p && g == e - 1))
27479 && (g->charpos == end_charpos
27480 /* Special case for when NEXT is an
27481 empty line at ZV. */
27482 || (g->charpos == -1
27483 && !row->ends_at_zv_p
27484 && next_start == end_charpos)))))
27485 /* A glyph that comes from DISP_STRING is by
27486 definition to be highlighted. */
27487 || EQ (g->object, disp_string))
27488 break;
27489 g++;
27490 }
27491 if (g == e)
27492 {
27493 *end = row;
27494 break;
27495 }
27496 /* The first row that ends at ZV must be the last to be
27497 highlighted. */
27498 else if (next->ends_at_zv_p)
27499 {
27500 *end = next;
27501 break;
27502 }
27503 }
27504 }
27505 }
27506
27507 /* This function sets the mouse_face_* elements of HLINFO, assuming
27508 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27509 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27510 for the overlay or run of text properties specifying the mouse
27511 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27512 before-string and after-string that must also be highlighted.
27513 DISP_STRING, if non-nil, is a display string that may cover some
27514 or all of the highlighted text. */
27515
27516 static void
27517 mouse_face_from_buffer_pos (Lisp_Object window,
27518 Mouse_HLInfo *hlinfo,
27519 ptrdiff_t mouse_charpos,
27520 ptrdiff_t start_charpos,
27521 ptrdiff_t end_charpos,
27522 Lisp_Object before_string,
27523 Lisp_Object after_string,
27524 Lisp_Object disp_string)
27525 {
27526 struct window *w = XWINDOW (window);
27527 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27528 struct glyph_row *r1, *r2;
27529 struct glyph *glyph, *end;
27530 ptrdiff_t ignore, pos;
27531 int x;
27532
27533 eassert (NILP (disp_string) || STRINGP (disp_string));
27534 eassert (NILP (before_string) || STRINGP (before_string));
27535 eassert (NILP (after_string) || STRINGP (after_string));
27536
27537 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27538 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27539 if (r1 == NULL)
27540 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27541 /* If the before-string or display-string contains newlines,
27542 rows_from_pos_range skips to its last row. Move back. */
27543 if (!NILP (before_string) || !NILP (disp_string))
27544 {
27545 struct glyph_row *prev;
27546 while ((prev = r1 - 1, prev >= first)
27547 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27548 && prev->used[TEXT_AREA] > 0)
27549 {
27550 struct glyph *beg = prev->glyphs[TEXT_AREA];
27551 glyph = beg + prev->used[TEXT_AREA];
27552 while (--glyph >= beg && INTEGERP (glyph->object));
27553 if (glyph < beg
27554 || !(EQ (glyph->object, before_string)
27555 || EQ (glyph->object, disp_string)))
27556 break;
27557 r1 = prev;
27558 }
27559 }
27560 if (r2 == NULL)
27561 {
27562 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27563 hlinfo->mouse_face_past_end = 1;
27564 }
27565 else if (!NILP (after_string))
27566 {
27567 /* If the after-string has newlines, advance to its last row. */
27568 struct glyph_row *next;
27569 struct glyph_row *last
27570 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27571
27572 for (next = r2 + 1;
27573 next <= last
27574 && next->used[TEXT_AREA] > 0
27575 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27576 ++next)
27577 r2 = next;
27578 }
27579 /* The rest of the display engine assumes that mouse_face_beg_row is
27580 either above mouse_face_end_row or identical to it. But with
27581 bidi-reordered continued lines, the row for START_CHARPOS could
27582 be below the row for END_CHARPOS. If so, swap the rows and store
27583 them in correct order. */
27584 if (r1->y > r2->y)
27585 {
27586 struct glyph_row *tem = r2;
27587
27588 r2 = r1;
27589 r1 = tem;
27590 }
27591
27592 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27593 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27594
27595 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27596 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27597 could be anywhere in the row and in any order. The strategy
27598 below is to find the leftmost and the rightmost glyph that
27599 belongs to either of these 3 strings, or whose position is
27600 between START_CHARPOS and END_CHARPOS, and highlight all the
27601 glyphs between those two. This may cover more than just the text
27602 between START_CHARPOS and END_CHARPOS if the range of characters
27603 strides the bidi level boundary, e.g. if the beginning is in R2L
27604 text while the end is in L2R text or vice versa. */
27605 if (!r1->reversed_p)
27606 {
27607 /* This row is in a left to right paragraph. Scan it left to
27608 right. */
27609 glyph = r1->glyphs[TEXT_AREA];
27610 end = glyph + r1->used[TEXT_AREA];
27611 x = r1->x;
27612
27613 /* Skip truncation glyphs at the start of the glyph row. */
27614 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27615 for (; glyph < end
27616 && INTEGERP (glyph->object)
27617 && glyph->charpos < 0;
27618 ++glyph)
27619 x += glyph->pixel_width;
27620
27621 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27622 or DISP_STRING, and the first glyph from buffer whose
27623 position is between START_CHARPOS and END_CHARPOS. */
27624 for (; glyph < end
27625 && !INTEGERP (glyph->object)
27626 && !EQ (glyph->object, disp_string)
27627 && !(BUFFERP (glyph->object)
27628 && (glyph->charpos >= start_charpos
27629 && glyph->charpos < end_charpos));
27630 ++glyph)
27631 {
27632 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27633 are present at buffer positions between START_CHARPOS and
27634 END_CHARPOS, or if they come from an overlay. */
27635 if (EQ (glyph->object, before_string))
27636 {
27637 pos = string_buffer_position (before_string,
27638 start_charpos);
27639 /* If pos == 0, it means before_string came from an
27640 overlay, not from a buffer position. */
27641 if (!pos || (pos >= start_charpos && pos < end_charpos))
27642 break;
27643 }
27644 else if (EQ (glyph->object, after_string))
27645 {
27646 pos = string_buffer_position (after_string, end_charpos);
27647 if (!pos || (pos >= start_charpos && pos < end_charpos))
27648 break;
27649 }
27650 x += glyph->pixel_width;
27651 }
27652 hlinfo->mouse_face_beg_x = x;
27653 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27654 }
27655 else
27656 {
27657 /* This row is in a right to left paragraph. Scan it right to
27658 left. */
27659 struct glyph *g;
27660
27661 end = r1->glyphs[TEXT_AREA] - 1;
27662 glyph = end + r1->used[TEXT_AREA];
27663
27664 /* Skip truncation glyphs at the start of the glyph row. */
27665 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27666 for (; glyph > end
27667 && INTEGERP (glyph->object)
27668 && glyph->charpos < 0;
27669 --glyph)
27670 ;
27671
27672 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27673 or DISP_STRING, and the first glyph from buffer whose
27674 position is between START_CHARPOS and END_CHARPOS. */
27675 for (; glyph > end
27676 && !INTEGERP (glyph->object)
27677 && !EQ (glyph->object, disp_string)
27678 && !(BUFFERP (glyph->object)
27679 && (glyph->charpos >= start_charpos
27680 && glyph->charpos < end_charpos));
27681 --glyph)
27682 {
27683 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27684 are present at buffer positions between START_CHARPOS and
27685 END_CHARPOS, or if they come from an overlay. */
27686 if (EQ (glyph->object, before_string))
27687 {
27688 pos = string_buffer_position (before_string, start_charpos);
27689 /* If pos == 0, it means before_string came from an
27690 overlay, not from a buffer position. */
27691 if (!pos || (pos >= start_charpos && pos < end_charpos))
27692 break;
27693 }
27694 else if (EQ (glyph->object, after_string))
27695 {
27696 pos = string_buffer_position (after_string, end_charpos);
27697 if (!pos || (pos >= start_charpos && pos < end_charpos))
27698 break;
27699 }
27700 }
27701
27702 glyph++; /* first glyph to the right of the highlighted area */
27703 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27704 x += g->pixel_width;
27705 hlinfo->mouse_face_beg_x = x;
27706 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27707 }
27708
27709 /* If the highlight ends in a different row, compute GLYPH and END
27710 for the end row. Otherwise, reuse the values computed above for
27711 the row where the highlight begins. */
27712 if (r2 != r1)
27713 {
27714 if (!r2->reversed_p)
27715 {
27716 glyph = r2->glyphs[TEXT_AREA];
27717 end = glyph + r2->used[TEXT_AREA];
27718 x = r2->x;
27719 }
27720 else
27721 {
27722 end = r2->glyphs[TEXT_AREA] - 1;
27723 glyph = end + r2->used[TEXT_AREA];
27724 }
27725 }
27726
27727 if (!r2->reversed_p)
27728 {
27729 /* Skip truncation and continuation glyphs near the end of the
27730 row, and also blanks and stretch glyphs inserted by
27731 extend_face_to_end_of_line. */
27732 while (end > glyph
27733 && INTEGERP ((end - 1)->object))
27734 --end;
27735 /* Scan the rest of the glyph row from the end, looking for the
27736 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27737 DISP_STRING, or whose position is between START_CHARPOS
27738 and END_CHARPOS */
27739 for (--end;
27740 end > glyph
27741 && !INTEGERP (end->object)
27742 && !EQ (end->object, disp_string)
27743 && !(BUFFERP (end->object)
27744 && (end->charpos >= start_charpos
27745 && end->charpos < end_charpos));
27746 --end)
27747 {
27748 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27749 are present at buffer positions between START_CHARPOS and
27750 END_CHARPOS, or if they come from an overlay. */
27751 if (EQ (end->object, before_string))
27752 {
27753 pos = string_buffer_position (before_string, start_charpos);
27754 if (!pos || (pos >= start_charpos && pos < end_charpos))
27755 break;
27756 }
27757 else if (EQ (end->object, after_string))
27758 {
27759 pos = string_buffer_position (after_string, end_charpos);
27760 if (!pos || (pos >= start_charpos && pos < end_charpos))
27761 break;
27762 }
27763 }
27764 /* Find the X coordinate of the last glyph to be highlighted. */
27765 for (; glyph <= end; ++glyph)
27766 x += glyph->pixel_width;
27767
27768 hlinfo->mouse_face_end_x = x;
27769 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27770 }
27771 else
27772 {
27773 /* Skip truncation and continuation glyphs near the end of the
27774 row, and also blanks and stretch glyphs inserted by
27775 extend_face_to_end_of_line. */
27776 x = r2->x;
27777 end++;
27778 while (end < glyph
27779 && INTEGERP (end->object))
27780 {
27781 x += end->pixel_width;
27782 ++end;
27783 }
27784 /* Scan the rest of the glyph row from the end, looking for the
27785 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27786 DISP_STRING, or whose position is between START_CHARPOS
27787 and END_CHARPOS */
27788 for ( ;
27789 end < glyph
27790 && !INTEGERP (end->object)
27791 && !EQ (end->object, disp_string)
27792 && !(BUFFERP (end->object)
27793 && (end->charpos >= start_charpos
27794 && end->charpos < end_charpos));
27795 ++end)
27796 {
27797 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27798 are present at buffer positions between START_CHARPOS and
27799 END_CHARPOS, or if they come from an overlay. */
27800 if (EQ (end->object, before_string))
27801 {
27802 pos = string_buffer_position (before_string, start_charpos);
27803 if (!pos || (pos >= start_charpos && pos < end_charpos))
27804 break;
27805 }
27806 else if (EQ (end->object, after_string))
27807 {
27808 pos = string_buffer_position (after_string, end_charpos);
27809 if (!pos || (pos >= start_charpos && pos < end_charpos))
27810 break;
27811 }
27812 x += end->pixel_width;
27813 }
27814 /* If we exited the above loop because we arrived at the last
27815 glyph of the row, and its buffer position is still not in
27816 range, it means the last character in range is the preceding
27817 newline. Bump the end column and x values to get past the
27818 last glyph. */
27819 if (end == glyph
27820 && BUFFERP (end->object)
27821 && (end->charpos < start_charpos
27822 || end->charpos >= end_charpos))
27823 {
27824 x += end->pixel_width;
27825 ++end;
27826 }
27827 hlinfo->mouse_face_end_x = x;
27828 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27829 }
27830
27831 hlinfo->mouse_face_window = window;
27832 hlinfo->mouse_face_face_id
27833 = face_at_buffer_position (w, mouse_charpos, &ignore,
27834 mouse_charpos + 1,
27835 !hlinfo->mouse_face_hidden, -1);
27836 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27837 }
27838
27839 /* The following function is not used anymore (replaced with
27840 mouse_face_from_string_pos), but I leave it here for the time
27841 being, in case someone would. */
27842
27843 #if 0 /* not used */
27844
27845 /* Find the position of the glyph for position POS in OBJECT in
27846 window W's current matrix, and return in *X, *Y the pixel
27847 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27848
27849 RIGHT_P non-zero means return the position of the right edge of the
27850 glyph, RIGHT_P zero means return the left edge position.
27851
27852 If no glyph for POS exists in the matrix, return the position of
27853 the glyph with the next smaller position that is in the matrix, if
27854 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27855 exists in the matrix, return the position of the glyph with the
27856 next larger position in OBJECT.
27857
27858 Value is non-zero if a glyph was found. */
27859
27860 static int
27861 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27862 int *hpos, int *vpos, int *x, int *y, int right_p)
27863 {
27864 int yb = window_text_bottom_y (w);
27865 struct glyph_row *r;
27866 struct glyph *best_glyph = NULL;
27867 struct glyph_row *best_row = NULL;
27868 int best_x = 0;
27869
27870 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27871 r->enabled_p && r->y < yb;
27872 ++r)
27873 {
27874 struct glyph *g = r->glyphs[TEXT_AREA];
27875 struct glyph *e = g + r->used[TEXT_AREA];
27876 int gx;
27877
27878 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27879 if (EQ (g->object, object))
27880 {
27881 if (g->charpos == pos)
27882 {
27883 best_glyph = g;
27884 best_x = gx;
27885 best_row = r;
27886 goto found;
27887 }
27888 else if (best_glyph == NULL
27889 || ((eabs (g->charpos - pos)
27890 < eabs (best_glyph->charpos - pos))
27891 && (right_p
27892 ? g->charpos < pos
27893 : g->charpos > pos)))
27894 {
27895 best_glyph = g;
27896 best_x = gx;
27897 best_row = r;
27898 }
27899 }
27900 }
27901
27902 found:
27903
27904 if (best_glyph)
27905 {
27906 *x = best_x;
27907 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27908
27909 if (right_p)
27910 {
27911 *x += best_glyph->pixel_width;
27912 ++*hpos;
27913 }
27914
27915 *y = best_row->y;
27916 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27917 }
27918
27919 return best_glyph != NULL;
27920 }
27921 #endif /* not used */
27922
27923 /* Find the positions of the first and the last glyphs in window W's
27924 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27925 (assumed to be a string), and return in HLINFO's mouse_face_*
27926 members the pixel and column/row coordinates of those glyphs. */
27927
27928 static void
27929 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27930 Lisp_Object object,
27931 ptrdiff_t startpos, ptrdiff_t endpos)
27932 {
27933 int yb = window_text_bottom_y (w);
27934 struct glyph_row *r;
27935 struct glyph *g, *e;
27936 int gx;
27937 int found = 0;
27938
27939 /* Find the glyph row with at least one position in the range
27940 [STARTPOS..ENDPOS), and the first glyph in that row whose
27941 position belongs to that range. */
27942 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27943 r->enabled_p && r->y < yb;
27944 ++r)
27945 {
27946 if (!r->reversed_p)
27947 {
27948 g = r->glyphs[TEXT_AREA];
27949 e = g + r->used[TEXT_AREA];
27950 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27951 if (EQ (g->object, object)
27952 && startpos <= g->charpos && g->charpos < endpos)
27953 {
27954 hlinfo->mouse_face_beg_row
27955 = MATRIX_ROW_VPOS (r, w->current_matrix);
27956 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27957 hlinfo->mouse_face_beg_x = gx;
27958 found = 1;
27959 break;
27960 }
27961 }
27962 else
27963 {
27964 struct glyph *g1;
27965
27966 e = r->glyphs[TEXT_AREA];
27967 g = e + r->used[TEXT_AREA];
27968 for ( ; g > e; --g)
27969 if (EQ ((g-1)->object, object)
27970 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27971 {
27972 hlinfo->mouse_face_beg_row
27973 = MATRIX_ROW_VPOS (r, w->current_matrix);
27974 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27975 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27976 gx += g1->pixel_width;
27977 hlinfo->mouse_face_beg_x = gx;
27978 found = 1;
27979 break;
27980 }
27981 }
27982 if (found)
27983 break;
27984 }
27985
27986 if (!found)
27987 return;
27988
27989 /* Starting with the next row, look for the first row which does NOT
27990 include any glyphs whose positions are in the range. */
27991 for (++r; r->enabled_p && r->y < yb; ++r)
27992 {
27993 g = r->glyphs[TEXT_AREA];
27994 e = g + r->used[TEXT_AREA];
27995 found = 0;
27996 for ( ; g < e; ++g)
27997 if (EQ (g->object, object)
27998 && startpos <= g->charpos && g->charpos < endpos)
27999 {
28000 found = 1;
28001 break;
28002 }
28003 if (!found)
28004 break;
28005 }
28006
28007 /* The highlighted region ends on the previous row. */
28008 r--;
28009
28010 /* Set the end row. */
28011 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28012
28013 /* Compute and set the end column and the end column's horizontal
28014 pixel coordinate. */
28015 if (!r->reversed_p)
28016 {
28017 g = r->glyphs[TEXT_AREA];
28018 e = g + r->used[TEXT_AREA];
28019 for ( ; e > g; --e)
28020 if (EQ ((e-1)->object, object)
28021 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28022 break;
28023 hlinfo->mouse_face_end_col = e - g;
28024
28025 for (gx = r->x; g < e; ++g)
28026 gx += g->pixel_width;
28027 hlinfo->mouse_face_end_x = gx;
28028 }
28029 else
28030 {
28031 e = r->glyphs[TEXT_AREA];
28032 g = e + r->used[TEXT_AREA];
28033 for (gx = r->x ; e < g; ++e)
28034 {
28035 if (EQ (e->object, object)
28036 && startpos <= e->charpos && e->charpos < endpos)
28037 break;
28038 gx += e->pixel_width;
28039 }
28040 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28041 hlinfo->mouse_face_end_x = gx;
28042 }
28043 }
28044
28045 #ifdef HAVE_WINDOW_SYSTEM
28046
28047 /* See if position X, Y is within a hot-spot of an image. */
28048
28049 static int
28050 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28051 {
28052 if (!CONSP (hot_spot))
28053 return 0;
28054
28055 if (EQ (XCAR (hot_spot), Qrect))
28056 {
28057 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28058 Lisp_Object rect = XCDR (hot_spot);
28059 Lisp_Object tem;
28060 if (!CONSP (rect))
28061 return 0;
28062 if (!CONSP (XCAR (rect)))
28063 return 0;
28064 if (!CONSP (XCDR (rect)))
28065 return 0;
28066 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28067 return 0;
28068 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28069 return 0;
28070 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28071 return 0;
28072 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28073 return 0;
28074 return 1;
28075 }
28076 else if (EQ (XCAR (hot_spot), Qcircle))
28077 {
28078 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28079 Lisp_Object circ = XCDR (hot_spot);
28080 Lisp_Object lr, lx0, ly0;
28081 if (CONSP (circ)
28082 && CONSP (XCAR (circ))
28083 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28084 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28085 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28086 {
28087 double r = XFLOATINT (lr);
28088 double dx = XINT (lx0) - x;
28089 double dy = XINT (ly0) - y;
28090 return (dx * dx + dy * dy <= r * r);
28091 }
28092 }
28093 else if (EQ (XCAR (hot_spot), Qpoly))
28094 {
28095 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28096 if (VECTORP (XCDR (hot_spot)))
28097 {
28098 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28099 Lisp_Object *poly = v->contents;
28100 ptrdiff_t n = v->header.size;
28101 ptrdiff_t i;
28102 int inside = 0;
28103 Lisp_Object lx, ly;
28104 int x0, y0;
28105
28106 /* Need an even number of coordinates, and at least 3 edges. */
28107 if (n < 6 || n & 1)
28108 return 0;
28109
28110 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28111 If count is odd, we are inside polygon. Pixels on edges
28112 may or may not be included depending on actual geometry of the
28113 polygon. */
28114 if ((lx = poly[n-2], !INTEGERP (lx))
28115 || (ly = poly[n-1], !INTEGERP (lx)))
28116 return 0;
28117 x0 = XINT (lx), y0 = XINT (ly);
28118 for (i = 0; i < n; i += 2)
28119 {
28120 int x1 = x0, y1 = y0;
28121 if ((lx = poly[i], !INTEGERP (lx))
28122 || (ly = poly[i+1], !INTEGERP (ly)))
28123 return 0;
28124 x0 = XINT (lx), y0 = XINT (ly);
28125
28126 /* Does this segment cross the X line? */
28127 if (x0 >= x)
28128 {
28129 if (x1 >= x)
28130 continue;
28131 }
28132 else if (x1 < x)
28133 continue;
28134 if (y > y0 && y > y1)
28135 continue;
28136 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28137 inside = !inside;
28138 }
28139 return inside;
28140 }
28141 }
28142 return 0;
28143 }
28144
28145 Lisp_Object
28146 find_hot_spot (Lisp_Object map, int x, int y)
28147 {
28148 while (CONSP (map))
28149 {
28150 if (CONSP (XCAR (map))
28151 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28152 return XCAR (map);
28153 map = XCDR (map);
28154 }
28155
28156 return Qnil;
28157 }
28158
28159 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28160 3, 3, 0,
28161 doc: /* Lookup in image map MAP coordinates X and Y.
28162 An image map is an alist where each element has the format (AREA ID PLIST).
28163 An AREA is specified as either a rectangle, a circle, or a polygon:
28164 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28165 pixel coordinates of the upper left and bottom right corners.
28166 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28167 and the radius of the circle; r may be a float or integer.
28168 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28169 vector describes one corner in the polygon.
28170 Returns the alist element for the first matching AREA in MAP. */)
28171 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28172 {
28173 if (NILP (map))
28174 return Qnil;
28175
28176 CHECK_NUMBER (x);
28177 CHECK_NUMBER (y);
28178
28179 return find_hot_spot (map,
28180 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28181 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28182 }
28183
28184
28185 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28186 static void
28187 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28188 {
28189 /* Do not change cursor shape while dragging mouse. */
28190 if (!NILP (do_mouse_tracking))
28191 return;
28192
28193 if (!NILP (pointer))
28194 {
28195 if (EQ (pointer, Qarrow))
28196 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28197 else if (EQ (pointer, Qhand))
28198 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28199 else if (EQ (pointer, Qtext))
28200 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28201 else if (EQ (pointer, intern ("hdrag")))
28202 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28203 else if (EQ (pointer, intern ("nhdrag")))
28204 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28205 #ifdef HAVE_X_WINDOWS
28206 else if (EQ (pointer, intern ("vdrag")))
28207 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28208 #endif
28209 else if (EQ (pointer, intern ("hourglass")))
28210 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28211 else if (EQ (pointer, Qmodeline))
28212 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28213 else
28214 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28215 }
28216
28217 if (cursor != No_Cursor)
28218 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28219 }
28220
28221 #endif /* HAVE_WINDOW_SYSTEM */
28222
28223 /* Take proper action when mouse has moved to the mode or header line
28224 or marginal area AREA of window W, x-position X and y-position Y.
28225 X is relative to the start of the text display area of W, so the
28226 width of bitmap areas and scroll bars must be subtracted to get a
28227 position relative to the start of the mode line. */
28228
28229 static void
28230 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28231 enum window_part area)
28232 {
28233 struct window *w = XWINDOW (window);
28234 struct frame *f = XFRAME (w->frame);
28235 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28236 #ifdef HAVE_WINDOW_SYSTEM
28237 Display_Info *dpyinfo;
28238 #endif
28239 Cursor cursor = No_Cursor;
28240 Lisp_Object pointer = Qnil;
28241 int dx, dy, width, height;
28242 ptrdiff_t charpos;
28243 Lisp_Object string, object = Qnil;
28244 Lisp_Object pos IF_LINT (= Qnil), help;
28245
28246 Lisp_Object mouse_face;
28247 int original_x_pixel = x;
28248 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28249 struct glyph_row *row IF_LINT (= 0);
28250
28251 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28252 {
28253 int x0;
28254 struct glyph *end;
28255
28256 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28257 returns them in row/column units! */
28258 string = mode_line_string (w, area, &x, &y, &charpos,
28259 &object, &dx, &dy, &width, &height);
28260
28261 row = (area == ON_MODE_LINE
28262 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28263 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28264
28265 /* Find the glyph under the mouse pointer. */
28266 if (row->mode_line_p && row->enabled_p)
28267 {
28268 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28269 end = glyph + row->used[TEXT_AREA];
28270
28271 for (x0 = original_x_pixel;
28272 glyph < end && x0 >= glyph->pixel_width;
28273 ++glyph)
28274 x0 -= glyph->pixel_width;
28275
28276 if (glyph >= end)
28277 glyph = NULL;
28278 }
28279 }
28280 else
28281 {
28282 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28283 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28284 returns them in row/column units! */
28285 string = marginal_area_string (w, area, &x, &y, &charpos,
28286 &object, &dx, &dy, &width, &height);
28287 }
28288
28289 help = Qnil;
28290
28291 #ifdef HAVE_WINDOW_SYSTEM
28292 if (IMAGEP (object))
28293 {
28294 Lisp_Object image_map, hotspot;
28295 if ((image_map = Fplist_get (XCDR (object), QCmap),
28296 !NILP (image_map))
28297 && (hotspot = find_hot_spot (image_map, dx, dy),
28298 CONSP (hotspot))
28299 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28300 {
28301 Lisp_Object plist;
28302
28303 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28304 If so, we could look for mouse-enter, mouse-leave
28305 properties in PLIST (and do something...). */
28306 hotspot = XCDR (hotspot);
28307 if (CONSP (hotspot)
28308 && (plist = XCAR (hotspot), CONSP (plist)))
28309 {
28310 pointer = Fplist_get (plist, Qpointer);
28311 if (NILP (pointer))
28312 pointer = Qhand;
28313 help = Fplist_get (plist, Qhelp_echo);
28314 if (!NILP (help))
28315 {
28316 help_echo_string = help;
28317 XSETWINDOW (help_echo_window, w);
28318 help_echo_object = w->contents;
28319 help_echo_pos = charpos;
28320 }
28321 }
28322 }
28323 if (NILP (pointer))
28324 pointer = Fplist_get (XCDR (object), QCpointer);
28325 }
28326 #endif /* HAVE_WINDOW_SYSTEM */
28327
28328 if (STRINGP (string))
28329 pos = make_number (charpos);
28330
28331 /* Set the help text and mouse pointer. If the mouse is on a part
28332 of the mode line without any text (e.g. past the right edge of
28333 the mode line text), use the default help text and pointer. */
28334 if (STRINGP (string) || area == ON_MODE_LINE)
28335 {
28336 /* Arrange to display the help by setting the global variables
28337 help_echo_string, help_echo_object, and help_echo_pos. */
28338 if (NILP (help))
28339 {
28340 if (STRINGP (string))
28341 help = Fget_text_property (pos, Qhelp_echo, string);
28342
28343 if (!NILP (help))
28344 {
28345 help_echo_string = help;
28346 XSETWINDOW (help_echo_window, w);
28347 help_echo_object = string;
28348 help_echo_pos = charpos;
28349 }
28350 else if (area == ON_MODE_LINE)
28351 {
28352 Lisp_Object default_help
28353 = buffer_local_value_1 (Qmode_line_default_help_echo,
28354 w->contents);
28355
28356 if (STRINGP (default_help))
28357 {
28358 help_echo_string = default_help;
28359 XSETWINDOW (help_echo_window, w);
28360 help_echo_object = Qnil;
28361 help_echo_pos = -1;
28362 }
28363 }
28364 }
28365
28366 #ifdef HAVE_WINDOW_SYSTEM
28367 /* Change the mouse pointer according to what is under it. */
28368 if (FRAME_WINDOW_P (f))
28369 {
28370 dpyinfo = FRAME_DISPLAY_INFO (f);
28371 if (STRINGP (string))
28372 {
28373 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28374
28375 if (NILP (pointer))
28376 pointer = Fget_text_property (pos, Qpointer, string);
28377
28378 /* Change the mouse pointer according to what is under X/Y. */
28379 if (NILP (pointer)
28380 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28381 {
28382 Lisp_Object map;
28383 map = Fget_text_property (pos, Qlocal_map, string);
28384 if (!KEYMAPP (map))
28385 map = Fget_text_property (pos, Qkeymap, string);
28386 if (!KEYMAPP (map))
28387 cursor = dpyinfo->vertical_scroll_bar_cursor;
28388 }
28389 }
28390 else
28391 /* Default mode-line pointer. */
28392 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28393 }
28394 #endif
28395 }
28396
28397 /* Change the mouse face according to what is under X/Y. */
28398 if (STRINGP (string))
28399 {
28400 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28401 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28402 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28403 && glyph)
28404 {
28405 Lisp_Object b, e;
28406
28407 struct glyph * tmp_glyph;
28408
28409 int gpos;
28410 int gseq_length;
28411 int total_pixel_width;
28412 ptrdiff_t begpos, endpos, ignore;
28413
28414 int vpos, hpos;
28415
28416 b = Fprevious_single_property_change (make_number (charpos + 1),
28417 Qmouse_face, string, Qnil);
28418 if (NILP (b))
28419 begpos = 0;
28420 else
28421 begpos = XINT (b);
28422
28423 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28424 if (NILP (e))
28425 endpos = SCHARS (string);
28426 else
28427 endpos = XINT (e);
28428
28429 /* Calculate the glyph position GPOS of GLYPH in the
28430 displayed string, relative to the beginning of the
28431 highlighted part of the string.
28432
28433 Note: GPOS is different from CHARPOS. CHARPOS is the
28434 position of GLYPH in the internal string object. A mode
28435 line string format has structures which are converted to
28436 a flattened string by the Emacs Lisp interpreter. The
28437 internal string is an element of those structures. The
28438 displayed string is the flattened string. */
28439 tmp_glyph = row_start_glyph;
28440 while (tmp_glyph < glyph
28441 && (!(EQ (tmp_glyph->object, glyph->object)
28442 && begpos <= tmp_glyph->charpos
28443 && tmp_glyph->charpos < endpos)))
28444 tmp_glyph++;
28445 gpos = glyph - tmp_glyph;
28446
28447 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28448 the highlighted part of the displayed string to which
28449 GLYPH belongs. Note: GSEQ_LENGTH is different from
28450 SCHARS (STRING), because the latter returns the length of
28451 the internal string. */
28452 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28453 tmp_glyph > glyph
28454 && (!(EQ (tmp_glyph->object, glyph->object)
28455 && begpos <= tmp_glyph->charpos
28456 && tmp_glyph->charpos < endpos));
28457 tmp_glyph--)
28458 ;
28459 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28460
28461 /* Calculate the total pixel width of all the glyphs between
28462 the beginning of the highlighted area and GLYPH. */
28463 total_pixel_width = 0;
28464 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28465 total_pixel_width += tmp_glyph->pixel_width;
28466
28467 /* Pre calculation of re-rendering position. Note: X is in
28468 column units here, after the call to mode_line_string or
28469 marginal_area_string. */
28470 hpos = x - gpos;
28471 vpos = (area == ON_MODE_LINE
28472 ? (w->current_matrix)->nrows - 1
28473 : 0);
28474
28475 /* If GLYPH's position is included in the region that is
28476 already drawn in mouse face, we have nothing to do. */
28477 if ( EQ (window, hlinfo->mouse_face_window)
28478 && (!row->reversed_p
28479 ? (hlinfo->mouse_face_beg_col <= hpos
28480 && hpos < hlinfo->mouse_face_end_col)
28481 /* In R2L rows we swap BEG and END, see below. */
28482 : (hlinfo->mouse_face_end_col <= hpos
28483 && hpos < hlinfo->mouse_face_beg_col))
28484 && hlinfo->mouse_face_beg_row == vpos )
28485 return;
28486
28487 if (clear_mouse_face (hlinfo))
28488 cursor = No_Cursor;
28489
28490 if (!row->reversed_p)
28491 {
28492 hlinfo->mouse_face_beg_col = hpos;
28493 hlinfo->mouse_face_beg_x = original_x_pixel
28494 - (total_pixel_width + dx);
28495 hlinfo->mouse_face_end_col = hpos + gseq_length;
28496 hlinfo->mouse_face_end_x = 0;
28497 }
28498 else
28499 {
28500 /* In R2L rows, show_mouse_face expects BEG and END
28501 coordinates to be swapped. */
28502 hlinfo->mouse_face_end_col = hpos;
28503 hlinfo->mouse_face_end_x = original_x_pixel
28504 - (total_pixel_width + dx);
28505 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28506 hlinfo->mouse_face_beg_x = 0;
28507 }
28508
28509 hlinfo->mouse_face_beg_row = vpos;
28510 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28511 hlinfo->mouse_face_past_end = 0;
28512 hlinfo->mouse_face_window = window;
28513
28514 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28515 charpos,
28516 0, &ignore,
28517 glyph->face_id,
28518 1);
28519 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28520
28521 if (NILP (pointer))
28522 pointer = Qhand;
28523 }
28524 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28525 clear_mouse_face (hlinfo);
28526 }
28527 #ifdef HAVE_WINDOW_SYSTEM
28528 if (FRAME_WINDOW_P (f))
28529 define_frame_cursor1 (f, cursor, pointer);
28530 #endif
28531 }
28532
28533
28534 /* EXPORT:
28535 Take proper action when the mouse has moved to position X, Y on
28536 frame F with regards to highlighting portions of display that have
28537 mouse-face properties. Also de-highlight portions of display where
28538 the mouse was before, set the mouse pointer shape as appropriate
28539 for the mouse coordinates, and activate help echo (tooltips).
28540 X and Y can be negative or out of range. */
28541
28542 void
28543 note_mouse_highlight (struct frame *f, int x, int y)
28544 {
28545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28546 enum window_part part = ON_NOTHING;
28547 Lisp_Object window;
28548 struct window *w;
28549 Cursor cursor = No_Cursor;
28550 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28551 struct buffer *b;
28552
28553 /* When a menu is active, don't highlight because this looks odd. */
28554 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28555 if (popup_activated ())
28556 return;
28557 #endif
28558
28559 if (!f->glyphs_initialized_p
28560 || f->pointer_invisible)
28561 return;
28562
28563 hlinfo->mouse_face_mouse_x = x;
28564 hlinfo->mouse_face_mouse_y = y;
28565 hlinfo->mouse_face_mouse_frame = f;
28566
28567 if (hlinfo->mouse_face_defer)
28568 return;
28569
28570 /* Which window is that in? */
28571 window = window_from_coordinates (f, x, y, &part, 1);
28572
28573 /* If displaying active text in another window, clear that. */
28574 if (! EQ (window, hlinfo->mouse_face_window)
28575 /* Also clear if we move out of text area in same window. */
28576 || (!NILP (hlinfo->mouse_face_window)
28577 && !NILP (window)
28578 && part != ON_TEXT
28579 && part != ON_MODE_LINE
28580 && part != ON_HEADER_LINE))
28581 clear_mouse_face (hlinfo);
28582
28583 /* Not on a window -> return. */
28584 if (!WINDOWP (window))
28585 return;
28586
28587 /* Reset help_echo_string. It will get recomputed below. */
28588 help_echo_string = Qnil;
28589
28590 /* Convert to window-relative pixel coordinates. */
28591 w = XWINDOW (window);
28592 frame_to_window_pixel_xy (w, &x, &y);
28593
28594 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28595 /* Handle tool-bar window differently since it doesn't display a
28596 buffer. */
28597 if (EQ (window, f->tool_bar_window))
28598 {
28599 note_tool_bar_highlight (f, x, y);
28600 return;
28601 }
28602 #endif
28603
28604 /* Mouse is on the mode, header line or margin? */
28605 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28606 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28607 {
28608 note_mode_line_or_margin_highlight (window, x, y, part);
28609
28610 #ifdef HAVE_WINDOW_SYSTEM
28611 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28612 {
28613 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28614 /* Show non-text cursor (Bug#16647). */
28615 goto set_cursor;
28616 }
28617 else
28618 #endif
28619 return;
28620 }
28621
28622 #ifdef HAVE_WINDOW_SYSTEM
28623 if (part == ON_VERTICAL_BORDER)
28624 {
28625 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28626 help_echo_string = build_string ("drag-mouse-1: resize");
28627 }
28628 else if (part == ON_RIGHT_DIVIDER)
28629 {
28630 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28631 help_echo_string = build_string ("drag-mouse-1: resize");
28632 }
28633 else if (part == ON_BOTTOM_DIVIDER)
28634 {
28635 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28636 help_echo_string = build_string ("drag-mouse-1: resize");
28637 }
28638 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28639 || part == ON_SCROLL_BAR)
28640 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28641 else
28642 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28643 #endif
28644
28645 /* Are we in a window whose display is up to date?
28646 And verify the buffer's text has not changed. */
28647 b = XBUFFER (w->contents);
28648 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28649 {
28650 int hpos, vpos, dx, dy, area = LAST_AREA;
28651 ptrdiff_t pos;
28652 struct glyph *glyph;
28653 Lisp_Object object;
28654 Lisp_Object mouse_face = Qnil, position;
28655 Lisp_Object *overlay_vec = NULL;
28656 ptrdiff_t i, noverlays;
28657 struct buffer *obuf;
28658 ptrdiff_t obegv, ozv;
28659 int same_region;
28660
28661 /* Find the glyph under X/Y. */
28662 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28663
28664 #ifdef HAVE_WINDOW_SYSTEM
28665 /* Look for :pointer property on image. */
28666 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28667 {
28668 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28669 if (img != NULL && IMAGEP (img->spec))
28670 {
28671 Lisp_Object image_map, hotspot;
28672 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28673 !NILP (image_map))
28674 && (hotspot = find_hot_spot (image_map,
28675 glyph->slice.img.x + dx,
28676 glyph->slice.img.y + dy),
28677 CONSP (hotspot))
28678 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28679 {
28680 Lisp_Object plist;
28681
28682 /* Could check XCAR (hotspot) to see if we enter/leave
28683 this hot-spot.
28684 If so, we could look for mouse-enter, mouse-leave
28685 properties in PLIST (and do something...). */
28686 hotspot = XCDR (hotspot);
28687 if (CONSP (hotspot)
28688 && (plist = XCAR (hotspot), CONSP (plist)))
28689 {
28690 pointer = Fplist_get (plist, Qpointer);
28691 if (NILP (pointer))
28692 pointer = Qhand;
28693 help_echo_string = Fplist_get (plist, Qhelp_echo);
28694 if (!NILP (help_echo_string))
28695 {
28696 help_echo_window = window;
28697 help_echo_object = glyph->object;
28698 help_echo_pos = glyph->charpos;
28699 }
28700 }
28701 }
28702 if (NILP (pointer))
28703 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28704 }
28705 }
28706 #endif /* HAVE_WINDOW_SYSTEM */
28707
28708 /* Clear mouse face if X/Y not over text. */
28709 if (glyph == NULL
28710 || area != TEXT_AREA
28711 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28712 /* Glyph's OBJECT is an integer for glyphs inserted by the
28713 display engine for its internal purposes, like truncation
28714 and continuation glyphs and blanks beyond the end of
28715 line's text on text terminals. If we are over such a
28716 glyph, we are not over any text. */
28717 || INTEGERP (glyph->object)
28718 /* R2L rows have a stretch glyph at their front, which
28719 stands for no text, whereas L2R rows have no glyphs at
28720 all beyond the end of text. Treat such stretch glyphs
28721 like we do with NULL glyphs in L2R rows. */
28722 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28723 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28724 && glyph->type == STRETCH_GLYPH
28725 && glyph->avoid_cursor_p))
28726 {
28727 if (clear_mouse_face (hlinfo))
28728 cursor = No_Cursor;
28729 #ifdef HAVE_WINDOW_SYSTEM
28730 if (FRAME_WINDOW_P (f) && NILP (pointer))
28731 {
28732 if (area != TEXT_AREA)
28733 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28734 else
28735 pointer = Vvoid_text_area_pointer;
28736 }
28737 #endif
28738 goto set_cursor;
28739 }
28740
28741 pos = glyph->charpos;
28742 object = glyph->object;
28743 if (!STRINGP (object) && !BUFFERP (object))
28744 goto set_cursor;
28745
28746 /* If we get an out-of-range value, return now; avoid an error. */
28747 if (BUFFERP (object) && pos > BUF_Z (b))
28748 goto set_cursor;
28749
28750 /* Make the window's buffer temporarily current for
28751 overlays_at and compute_char_face. */
28752 obuf = current_buffer;
28753 current_buffer = b;
28754 obegv = BEGV;
28755 ozv = ZV;
28756 BEGV = BEG;
28757 ZV = Z;
28758
28759 /* Is this char mouse-active or does it have help-echo? */
28760 position = make_number (pos);
28761
28762 if (BUFFERP (object))
28763 {
28764 /* Put all the overlays we want in a vector in overlay_vec. */
28765 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28766 /* Sort overlays into increasing priority order. */
28767 noverlays = sort_overlays (overlay_vec, noverlays, w);
28768 }
28769 else
28770 noverlays = 0;
28771
28772 if (NILP (Vmouse_highlight))
28773 {
28774 clear_mouse_face (hlinfo);
28775 goto check_help_echo;
28776 }
28777
28778 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28779
28780 if (same_region)
28781 cursor = No_Cursor;
28782
28783 /* Check mouse-face highlighting. */
28784 if (! same_region
28785 /* If there exists an overlay with mouse-face overlapping
28786 the one we are currently highlighting, we have to
28787 check if we enter the overlapping overlay, and then
28788 highlight only that. */
28789 || (OVERLAYP (hlinfo->mouse_face_overlay)
28790 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28791 {
28792 /* Find the highest priority overlay with a mouse-face. */
28793 Lisp_Object overlay = Qnil;
28794 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28795 {
28796 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28797 if (!NILP (mouse_face))
28798 overlay = overlay_vec[i];
28799 }
28800
28801 /* If we're highlighting the same overlay as before, there's
28802 no need to do that again. */
28803 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28804 goto check_help_echo;
28805 hlinfo->mouse_face_overlay = overlay;
28806
28807 /* Clear the display of the old active region, if any. */
28808 if (clear_mouse_face (hlinfo))
28809 cursor = No_Cursor;
28810
28811 /* If no overlay applies, get a text property. */
28812 if (NILP (overlay))
28813 mouse_face = Fget_text_property (position, Qmouse_face, object);
28814
28815 /* Next, compute the bounds of the mouse highlighting and
28816 display it. */
28817 if (!NILP (mouse_face) && STRINGP (object))
28818 {
28819 /* The mouse-highlighting comes from a display string
28820 with a mouse-face. */
28821 Lisp_Object s, e;
28822 ptrdiff_t ignore;
28823
28824 s = Fprevious_single_property_change
28825 (make_number (pos + 1), Qmouse_face, object, Qnil);
28826 e = Fnext_single_property_change
28827 (position, Qmouse_face, object, Qnil);
28828 if (NILP (s))
28829 s = make_number (0);
28830 if (NILP (e))
28831 e = make_number (SCHARS (object));
28832 mouse_face_from_string_pos (w, hlinfo, object,
28833 XINT (s), XINT (e));
28834 hlinfo->mouse_face_past_end = 0;
28835 hlinfo->mouse_face_window = window;
28836 hlinfo->mouse_face_face_id
28837 = face_at_string_position (w, object, pos, 0, &ignore,
28838 glyph->face_id, 1);
28839 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28840 cursor = No_Cursor;
28841 }
28842 else
28843 {
28844 /* The mouse-highlighting, if any, comes from an overlay
28845 or text property in the buffer. */
28846 Lisp_Object buffer IF_LINT (= Qnil);
28847 Lisp_Object disp_string IF_LINT (= Qnil);
28848
28849 if (STRINGP (object))
28850 {
28851 /* If we are on a display string with no mouse-face,
28852 check if the text under it has one. */
28853 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28854 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28855 pos = string_buffer_position (object, start);
28856 if (pos > 0)
28857 {
28858 mouse_face = get_char_property_and_overlay
28859 (make_number (pos), Qmouse_face, w->contents, &overlay);
28860 buffer = w->contents;
28861 disp_string = object;
28862 }
28863 }
28864 else
28865 {
28866 buffer = object;
28867 disp_string = Qnil;
28868 }
28869
28870 if (!NILP (mouse_face))
28871 {
28872 Lisp_Object before, after;
28873 Lisp_Object before_string, after_string;
28874 /* To correctly find the limits of mouse highlight
28875 in a bidi-reordered buffer, we must not use the
28876 optimization of limiting the search in
28877 previous-single-property-change and
28878 next-single-property-change, because
28879 rows_from_pos_range needs the real start and end
28880 positions to DTRT in this case. That's because
28881 the first row visible in a window does not
28882 necessarily display the character whose position
28883 is the smallest. */
28884 Lisp_Object lim1
28885 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28886 ? Fmarker_position (w->start)
28887 : Qnil;
28888 Lisp_Object lim2
28889 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28890 ? make_number (BUF_Z (XBUFFER (buffer))
28891 - w->window_end_pos)
28892 : Qnil;
28893
28894 if (NILP (overlay))
28895 {
28896 /* Handle the text property case. */
28897 before = Fprevious_single_property_change
28898 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28899 after = Fnext_single_property_change
28900 (make_number (pos), Qmouse_face, buffer, lim2);
28901 before_string = after_string = Qnil;
28902 }
28903 else
28904 {
28905 /* Handle the overlay case. */
28906 before = Foverlay_start (overlay);
28907 after = Foverlay_end (overlay);
28908 before_string = Foverlay_get (overlay, Qbefore_string);
28909 after_string = Foverlay_get (overlay, Qafter_string);
28910
28911 if (!STRINGP (before_string)) before_string = Qnil;
28912 if (!STRINGP (after_string)) after_string = Qnil;
28913 }
28914
28915 mouse_face_from_buffer_pos (window, hlinfo, pos,
28916 NILP (before)
28917 ? 1
28918 : XFASTINT (before),
28919 NILP (after)
28920 ? BUF_Z (XBUFFER (buffer))
28921 : XFASTINT (after),
28922 before_string, after_string,
28923 disp_string);
28924 cursor = No_Cursor;
28925 }
28926 }
28927 }
28928
28929 check_help_echo:
28930
28931 /* Look for a `help-echo' property. */
28932 if (NILP (help_echo_string)) {
28933 Lisp_Object help, overlay;
28934
28935 /* Check overlays first. */
28936 help = overlay = Qnil;
28937 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28938 {
28939 overlay = overlay_vec[i];
28940 help = Foverlay_get (overlay, Qhelp_echo);
28941 }
28942
28943 if (!NILP (help))
28944 {
28945 help_echo_string = help;
28946 help_echo_window = window;
28947 help_echo_object = overlay;
28948 help_echo_pos = pos;
28949 }
28950 else
28951 {
28952 Lisp_Object obj = glyph->object;
28953 ptrdiff_t charpos = glyph->charpos;
28954
28955 /* Try text properties. */
28956 if (STRINGP (obj)
28957 && charpos >= 0
28958 && charpos < SCHARS (obj))
28959 {
28960 help = Fget_text_property (make_number (charpos),
28961 Qhelp_echo, obj);
28962 if (NILP (help))
28963 {
28964 /* If the string itself doesn't specify a help-echo,
28965 see if the buffer text ``under'' it does. */
28966 struct glyph_row *r
28967 = MATRIX_ROW (w->current_matrix, vpos);
28968 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28969 ptrdiff_t p = string_buffer_position (obj, start);
28970 if (p > 0)
28971 {
28972 help = Fget_char_property (make_number (p),
28973 Qhelp_echo, w->contents);
28974 if (!NILP (help))
28975 {
28976 charpos = p;
28977 obj = w->contents;
28978 }
28979 }
28980 }
28981 }
28982 else if (BUFFERP (obj)
28983 && charpos >= BEGV
28984 && charpos < ZV)
28985 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28986 obj);
28987
28988 if (!NILP (help))
28989 {
28990 help_echo_string = help;
28991 help_echo_window = window;
28992 help_echo_object = obj;
28993 help_echo_pos = charpos;
28994 }
28995 }
28996 }
28997
28998 #ifdef HAVE_WINDOW_SYSTEM
28999 /* Look for a `pointer' property. */
29000 if (FRAME_WINDOW_P (f) && NILP (pointer))
29001 {
29002 /* Check overlays first. */
29003 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29004 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29005
29006 if (NILP (pointer))
29007 {
29008 Lisp_Object obj = glyph->object;
29009 ptrdiff_t charpos = glyph->charpos;
29010
29011 /* Try text properties. */
29012 if (STRINGP (obj)
29013 && charpos >= 0
29014 && charpos < SCHARS (obj))
29015 {
29016 pointer = Fget_text_property (make_number (charpos),
29017 Qpointer, obj);
29018 if (NILP (pointer))
29019 {
29020 /* If the string itself doesn't specify a pointer,
29021 see if the buffer text ``under'' it does. */
29022 struct glyph_row *r
29023 = MATRIX_ROW (w->current_matrix, vpos);
29024 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29025 ptrdiff_t p = string_buffer_position (obj, start);
29026 if (p > 0)
29027 pointer = Fget_char_property (make_number (p),
29028 Qpointer, w->contents);
29029 }
29030 }
29031 else if (BUFFERP (obj)
29032 && charpos >= BEGV
29033 && charpos < ZV)
29034 pointer = Fget_text_property (make_number (charpos),
29035 Qpointer, obj);
29036 }
29037 }
29038 #endif /* HAVE_WINDOW_SYSTEM */
29039
29040 BEGV = obegv;
29041 ZV = ozv;
29042 current_buffer = obuf;
29043 }
29044
29045 set_cursor:
29046
29047 #ifdef HAVE_WINDOW_SYSTEM
29048 if (FRAME_WINDOW_P (f))
29049 define_frame_cursor1 (f, cursor, pointer);
29050 #else
29051 /* This is here to prevent a compiler error, about "label at end of
29052 compound statement". */
29053 return;
29054 #endif
29055 }
29056
29057
29058 /* EXPORT for RIF:
29059 Clear any mouse-face on window W. This function is part of the
29060 redisplay interface, and is called from try_window_id and similar
29061 functions to ensure the mouse-highlight is off. */
29062
29063 void
29064 x_clear_window_mouse_face (struct window *w)
29065 {
29066 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29067 Lisp_Object window;
29068
29069 block_input ();
29070 XSETWINDOW (window, w);
29071 if (EQ (window, hlinfo->mouse_face_window))
29072 clear_mouse_face (hlinfo);
29073 unblock_input ();
29074 }
29075
29076
29077 /* EXPORT:
29078 Just discard the mouse face information for frame F, if any.
29079 This is used when the size of F is changed. */
29080
29081 void
29082 cancel_mouse_face (struct frame *f)
29083 {
29084 Lisp_Object window;
29085 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29086
29087 window = hlinfo->mouse_face_window;
29088 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29089 reset_mouse_highlight (hlinfo);
29090 }
29091
29092
29093 \f
29094 /***********************************************************************
29095 Exposure Events
29096 ***********************************************************************/
29097
29098 #ifdef HAVE_WINDOW_SYSTEM
29099
29100 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29101 which intersects rectangle R. R is in window-relative coordinates. */
29102
29103 static void
29104 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29105 enum glyph_row_area area)
29106 {
29107 struct glyph *first = row->glyphs[area];
29108 struct glyph *end = row->glyphs[area] + row->used[area];
29109 struct glyph *last;
29110 int first_x, start_x, x;
29111
29112 if (area == TEXT_AREA && row->fill_line_p)
29113 /* If row extends face to end of line write the whole line. */
29114 draw_glyphs (w, 0, row, area,
29115 0, row->used[area],
29116 DRAW_NORMAL_TEXT, 0);
29117 else
29118 {
29119 /* Set START_X to the window-relative start position for drawing glyphs of
29120 AREA. The first glyph of the text area can be partially visible.
29121 The first glyphs of other areas cannot. */
29122 start_x = window_box_left_offset (w, area);
29123 x = start_x;
29124 if (area == TEXT_AREA)
29125 x += row->x;
29126
29127 /* Find the first glyph that must be redrawn. */
29128 while (first < end
29129 && x + first->pixel_width < r->x)
29130 {
29131 x += first->pixel_width;
29132 ++first;
29133 }
29134
29135 /* Find the last one. */
29136 last = first;
29137 first_x = x;
29138 while (last < end
29139 && x < r->x + r->width)
29140 {
29141 x += last->pixel_width;
29142 ++last;
29143 }
29144
29145 /* Repaint. */
29146 if (last > first)
29147 draw_glyphs (w, first_x - start_x, row, area,
29148 first - row->glyphs[area], last - row->glyphs[area],
29149 DRAW_NORMAL_TEXT, 0);
29150 }
29151 }
29152
29153
29154 /* Redraw the parts of the glyph row ROW on window W intersecting
29155 rectangle R. R is in window-relative coordinates. Value is
29156 non-zero if mouse-face was overwritten. */
29157
29158 static int
29159 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29160 {
29161 eassert (row->enabled_p);
29162
29163 if (row->mode_line_p || w->pseudo_window_p)
29164 draw_glyphs (w, 0, row, TEXT_AREA,
29165 0, row->used[TEXT_AREA],
29166 DRAW_NORMAL_TEXT, 0);
29167 else
29168 {
29169 if (row->used[LEFT_MARGIN_AREA])
29170 expose_area (w, row, r, LEFT_MARGIN_AREA);
29171 if (row->used[TEXT_AREA])
29172 expose_area (w, row, r, TEXT_AREA);
29173 if (row->used[RIGHT_MARGIN_AREA])
29174 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29175 draw_row_fringe_bitmaps (w, row);
29176 }
29177
29178 return row->mouse_face_p;
29179 }
29180
29181
29182 /* Redraw those parts of glyphs rows during expose event handling that
29183 overlap other rows. Redrawing of an exposed line writes over parts
29184 of lines overlapping that exposed line; this function fixes that.
29185
29186 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29187 row in W's current matrix that is exposed and overlaps other rows.
29188 LAST_OVERLAPPING_ROW is the last such row. */
29189
29190 static void
29191 expose_overlaps (struct window *w,
29192 struct glyph_row *first_overlapping_row,
29193 struct glyph_row *last_overlapping_row,
29194 XRectangle *r)
29195 {
29196 struct glyph_row *row;
29197
29198 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29199 if (row->overlapping_p)
29200 {
29201 eassert (row->enabled_p && !row->mode_line_p);
29202
29203 row->clip = r;
29204 if (row->used[LEFT_MARGIN_AREA])
29205 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29206
29207 if (row->used[TEXT_AREA])
29208 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29209
29210 if (row->used[RIGHT_MARGIN_AREA])
29211 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29212 row->clip = NULL;
29213 }
29214 }
29215
29216
29217 /* Return non-zero if W's cursor intersects rectangle R. */
29218
29219 static int
29220 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29221 {
29222 XRectangle cr, result;
29223 struct glyph *cursor_glyph;
29224 struct glyph_row *row;
29225
29226 if (w->phys_cursor.vpos >= 0
29227 && w->phys_cursor.vpos < w->current_matrix->nrows
29228 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29229 row->enabled_p)
29230 && row->cursor_in_fringe_p)
29231 {
29232 /* Cursor is in the fringe. */
29233 cr.x = window_box_right_offset (w,
29234 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29235 ? RIGHT_MARGIN_AREA
29236 : TEXT_AREA));
29237 cr.y = row->y;
29238 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29239 cr.height = row->height;
29240 return x_intersect_rectangles (&cr, r, &result);
29241 }
29242
29243 cursor_glyph = get_phys_cursor_glyph (w);
29244 if (cursor_glyph)
29245 {
29246 /* r is relative to W's box, but w->phys_cursor.x is relative
29247 to left edge of W's TEXT area. Adjust it. */
29248 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29249 cr.y = w->phys_cursor.y;
29250 cr.width = cursor_glyph->pixel_width;
29251 cr.height = w->phys_cursor_height;
29252 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29253 I assume the effect is the same -- and this is portable. */
29254 return x_intersect_rectangles (&cr, r, &result);
29255 }
29256 /* If we don't understand the format, pretend we're not in the hot-spot. */
29257 return 0;
29258 }
29259
29260
29261 /* EXPORT:
29262 Draw a vertical window border to the right of window W if W doesn't
29263 have vertical scroll bars. */
29264
29265 void
29266 x_draw_vertical_border (struct window *w)
29267 {
29268 struct frame *f = XFRAME (WINDOW_FRAME (w));
29269
29270 /* We could do better, if we knew what type of scroll-bar the adjacent
29271 windows (on either side) have... But we don't :-(
29272 However, I think this works ok. ++KFS 2003-04-25 */
29273
29274 /* Redraw borders between horizontally adjacent windows. Don't
29275 do it for frames with vertical scroll bars because either the
29276 right scroll bar of a window, or the left scroll bar of its
29277 neighbor will suffice as a border. */
29278 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29279 return;
29280
29281 /* Note: It is necessary to redraw both the left and the right
29282 borders, for when only this single window W is being
29283 redisplayed. */
29284 if (!WINDOW_RIGHTMOST_P (w)
29285 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29286 {
29287 int x0, x1, y0, y1;
29288
29289 window_box_edges (w, &x0, &y0, &x1, &y1);
29290 y1 -= 1;
29291
29292 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29293 x1 -= 1;
29294
29295 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29296 }
29297
29298 if (!WINDOW_LEFTMOST_P (w)
29299 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29300 {
29301 int x0, x1, y0, y1;
29302
29303 window_box_edges (w, &x0, &y0, &x1, &y1);
29304 y1 -= 1;
29305
29306 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29307 x0 -= 1;
29308
29309 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29310 }
29311 }
29312
29313
29314 /* Draw window dividers for window W. */
29315
29316 void
29317 x_draw_right_divider (struct window *w)
29318 {
29319 struct frame *f = WINDOW_XFRAME (w);
29320
29321 if (w->mini || w->pseudo_window_p)
29322 return;
29323 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29324 {
29325 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29326 int x1 = WINDOW_RIGHT_EDGE_X (w);
29327 int y0 = WINDOW_TOP_EDGE_Y (w);
29328 /* The bottom divider prevails. */
29329 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29330
29331 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29332 }
29333 }
29334
29335 static void
29336 x_draw_bottom_divider (struct window *w)
29337 {
29338 struct frame *f = XFRAME (WINDOW_FRAME (w));
29339
29340 if (w->mini || w->pseudo_window_p)
29341 return;
29342 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29343 {
29344 int x0 = WINDOW_LEFT_EDGE_X (w);
29345 int x1 = WINDOW_RIGHT_EDGE_X (w);
29346 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29347 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29348
29349 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29350 }
29351 }
29352
29353 /* Redraw the part of window W intersection rectangle FR. Pixel
29354 coordinates in FR are frame-relative. Call this function with
29355 input blocked. Value is non-zero if the exposure overwrites
29356 mouse-face. */
29357
29358 static int
29359 expose_window (struct window *w, XRectangle *fr)
29360 {
29361 struct frame *f = XFRAME (w->frame);
29362 XRectangle wr, r;
29363 int mouse_face_overwritten_p = 0;
29364
29365 /* If window is not yet fully initialized, do nothing. This can
29366 happen when toolkit scroll bars are used and a window is split.
29367 Reconfiguring the scroll bar will generate an expose for a newly
29368 created window. */
29369 if (w->current_matrix == NULL)
29370 return 0;
29371
29372 /* When we're currently updating the window, display and current
29373 matrix usually don't agree. Arrange for a thorough display
29374 later. */
29375 if (w->must_be_updated_p)
29376 {
29377 SET_FRAME_GARBAGED (f);
29378 return 0;
29379 }
29380
29381 /* Frame-relative pixel rectangle of W. */
29382 wr.x = WINDOW_LEFT_EDGE_X (w);
29383 wr.y = WINDOW_TOP_EDGE_Y (w);
29384 wr.width = WINDOW_PIXEL_WIDTH (w);
29385 wr.height = WINDOW_PIXEL_HEIGHT (w);
29386
29387 if (x_intersect_rectangles (fr, &wr, &r))
29388 {
29389 int yb = window_text_bottom_y (w);
29390 struct glyph_row *row;
29391 int cursor_cleared_p, phys_cursor_on_p;
29392 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29393
29394 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29395 r.x, r.y, r.width, r.height));
29396
29397 /* Convert to window coordinates. */
29398 r.x -= WINDOW_LEFT_EDGE_X (w);
29399 r.y -= WINDOW_TOP_EDGE_Y (w);
29400
29401 /* Turn off the cursor. */
29402 if (!w->pseudo_window_p
29403 && phys_cursor_in_rect_p (w, &r))
29404 {
29405 x_clear_cursor (w);
29406 cursor_cleared_p = 1;
29407 }
29408 else
29409 cursor_cleared_p = 0;
29410
29411 /* If the row containing the cursor extends face to end of line,
29412 then expose_area might overwrite the cursor outside the
29413 rectangle and thus notice_overwritten_cursor might clear
29414 w->phys_cursor_on_p. We remember the original value and
29415 check later if it is changed. */
29416 phys_cursor_on_p = w->phys_cursor_on_p;
29417
29418 /* Update lines intersecting rectangle R. */
29419 first_overlapping_row = last_overlapping_row = NULL;
29420 for (row = w->current_matrix->rows;
29421 row->enabled_p;
29422 ++row)
29423 {
29424 int y0 = row->y;
29425 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29426
29427 if ((y0 >= r.y && y0 < r.y + r.height)
29428 || (y1 > r.y && y1 < r.y + r.height)
29429 || (r.y >= y0 && r.y < y1)
29430 || (r.y + r.height > y0 && r.y + r.height < y1))
29431 {
29432 /* A header line may be overlapping, but there is no need
29433 to fix overlapping areas for them. KFS 2005-02-12 */
29434 if (row->overlapping_p && !row->mode_line_p)
29435 {
29436 if (first_overlapping_row == NULL)
29437 first_overlapping_row = row;
29438 last_overlapping_row = row;
29439 }
29440
29441 row->clip = fr;
29442 if (expose_line (w, row, &r))
29443 mouse_face_overwritten_p = 1;
29444 row->clip = NULL;
29445 }
29446 else if (row->overlapping_p)
29447 {
29448 /* We must redraw a row overlapping the exposed area. */
29449 if (y0 < r.y
29450 ? y0 + row->phys_height > r.y
29451 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29452 {
29453 if (first_overlapping_row == NULL)
29454 first_overlapping_row = row;
29455 last_overlapping_row = row;
29456 }
29457 }
29458
29459 if (y1 >= yb)
29460 break;
29461 }
29462
29463 /* Display the mode line if there is one. */
29464 if (WINDOW_WANTS_MODELINE_P (w)
29465 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29466 row->enabled_p)
29467 && row->y < r.y + r.height)
29468 {
29469 if (expose_line (w, row, &r))
29470 mouse_face_overwritten_p = 1;
29471 }
29472
29473 if (!w->pseudo_window_p)
29474 {
29475 /* Fix the display of overlapping rows. */
29476 if (first_overlapping_row)
29477 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29478 fr);
29479
29480 /* Draw border between windows. */
29481 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29482 x_draw_right_divider (w);
29483 else
29484 x_draw_vertical_border (w);
29485
29486 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29487 x_draw_bottom_divider (w);
29488
29489 /* Turn the cursor on again. */
29490 if (cursor_cleared_p
29491 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29492 update_window_cursor (w, 1);
29493 }
29494 }
29495
29496 return mouse_face_overwritten_p;
29497 }
29498
29499
29500
29501 /* Redraw (parts) of all windows in the window tree rooted at W that
29502 intersect R. R contains frame pixel coordinates. Value is
29503 non-zero if the exposure overwrites mouse-face. */
29504
29505 static int
29506 expose_window_tree (struct window *w, XRectangle *r)
29507 {
29508 struct frame *f = XFRAME (w->frame);
29509 int mouse_face_overwritten_p = 0;
29510
29511 while (w && !FRAME_GARBAGED_P (f))
29512 {
29513 if (WINDOWP (w->contents))
29514 mouse_face_overwritten_p
29515 |= expose_window_tree (XWINDOW (w->contents), r);
29516 else
29517 mouse_face_overwritten_p |= expose_window (w, r);
29518
29519 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29520 }
29521
29522 return mouse_face_overwritten_p;
29523 }
29524
29525
29526 /* EXPORT:
29527 Redisplay an exposed area of frame F. X and Y are the upper-left
29528 corner of the exposed rectangle. W and H are width and height of
29529 the exposed area. All are pixel values. W or H zero means redraw
29530 the entire frame. */
29531
29532 void
29533 expose_frame (struct frame *f, int x, int y, int w, int h)
29534 {
29535 XRectangle r;
29536 int mouse_face_overwritten_p = 0;
29537
29538 TRACE ((stderr, "expose_frame "));
29539
29540 /* No need to redraw if frame will be redrawn soon. */
29541 if (FRAME_GARBAGED_P (f))
29542 {
29543 TRACE ((stderr, " garbaged\n"));
29544 return;
29545 }
29546
29547 /* If basic faces haven't been realized yet, there is no point in
29548 trying to redraw anything. This can happen when we get an expose
29549 event while Emacs is starting, e.g. by moving another window. */
29550 if (FRAME_FACE_CACHE (f) == NULL
29551 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29552 {
29553 TRACE ((stderr, " no faces\n"));
29554 return;
29555 }
29556
29557 if (w == 0 || h == 0)
29558 {
29559 r.x = r.y = 0;
29560 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29561 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29562 }
29563 else
29564 {
29565 r.x = x;
29566 r.y = y;
29567 r.width = w;
29568 r.height = h;
29569 }
29570
29571 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29572 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29573
29574 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29575 if (WINDOWP (f->tool_bar_window))
29576 mouse_face_overwritten_p
29577 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29578 #endif
29579
29580 #ifdef HAVE_X_WINDOWS
29581 #ifndef MSDOS
29582 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29583 if (WINDOWP (f->menu_bar_window))
29584 mouse_face_overwritten_p
29585 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29586 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29587 #endif
29588 #endif
29589
29590 /* Some window managers support a focus-follows-mouse style with
29591 delayed raising of frames. Imagine a partially obscured frame,
29592 and moving the mouse into partially obscured mouse-face on that
29593 frame. The visible part of the mouse-face will be highlighted,
29594 then the WM raises the obscured frame. With at least one WM, KDE
29595 2.1, Emacs is not getting any event for the raising of the frame
29596 (even tried with SubstructureRedirectMask), only Expose events.
29597 These expose events will draw text normally, i.e. not
29598 highlighted. Which means we must redo the highlight here.
29599 Subsume it under ``we love X''. --gerd 2001-08-15 */
29600 /* Included in Windows version because Windows most likely does not
29601 do the right thing if any third party tool offers
29602 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29603 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29604 {
29605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29606 if (f == hlinfo->mouse_face_mouse_frame)
29607 {
29608 int mouse_x = hlinfo->mouse_face_mouse_x;
29609 int mouse_y = hlinfo->mouse_face_mouse_y;
29610 clear_mouse_face (hlinfo);
29611 note_mouse_highlight (f, mouse_x, mouse_y);
29612 }
29613 }
29614 }
29615
29616
29617 /* EXPORT:
29618 Determine the intersection of two rectangles R1 and R2. Return
29619 the intersection in *RESULT. Value is non-zero if RESULT is not
29620 empty. */
29621
29622 int
29623 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29624 {
29625 XRectangle *left, *right;
29626 XRectangle *upper, *lower;
29627 int intersection_p = 0;
29628
29629 /* Rearrange so that R1 is the left-most rectangle. */
29630 if (r1->x < r2->x)
29631 left = r1, right = r2;
29632 else
29633 left = r2, right = r1;
29634
29635 /* X0 of the intersection is right.x0, if this is inside R1,
29636 otherwise there is no intersection. */
29637 if (right->x <= left->x + left->width)
29638 {
29639 result->x = right->x;
29640
29641 /* The right end of the intersection is the minimum of
29642 the right ends of left and right. */
29643 result->width = (min (left->x + left->width, right->x + right->width)
29644 - result->x);
29645
29646 /* Same game for Y. */
29647 if (r1->y < r2->y)
29648 upper = r1, lower = r2;
29649 else
29650 upper = r2, lower = r1;
29651
29652 /* The upper end of the intersection is lower.y0, if this is inside
29653 of upper. Otherwise, there is no intersection. */
29654 if (lower->y <= upper->y + upper->height)
29655 {
29656 result->y = lower->y;
29657
29658 /* The lower end of the intersection is the minimum of the lower
29659 ends of upper and lower. */
29660 result->height = (min (lower->y + lower->height,
29661 upper->y + upper->height)
29662 - result->y);
29663 intersection_p = 1;
29664 }
29665 }
29666
29667 return intersection_p;
29668 }
29669
29670 #endif /* HAVE_WINDOW_SYSTEM */
29671
29672 \f
29673 /***********************************************************************
29674 Initialization
29675 ***********************************************************************/
29676
29677 void
29678 syms_of_xdisp (void)
29679 {
29680 Vwith_echo_area_save_vector = Qnil;
29681 staticpro (&Vwith_echo_area_save_vector);
29682
29683 Vmessage_stack = Qnil;
29684 staticpro (&Vmessage_stack);
29685
29686 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29687 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29688
29689 message_dolog_marker1 = Fmake_marker ();
29690 staticpro (&message_dolog_marker1);
29691 message_dolog_marker2 = Fmake_marker ();
29692 staticpro (&message_dolog_marker2);
29693 message_dolog_marker3 = Fmake_marker ();
29694 staticpro (&message_dolog_marker3);
29695
29696 #ifdef GLYPH_DEBUG
29697 defsubr (&Sdump_frame_glyph_matrix);
29698 defsubr (&Sdump_glyph_matrix);
29699 defsubr (&Sdump_glyph_row);
29700 defsubr (&Sdump_tool_bar_row);
29701 defsubr (&Strace_redisplay);
29702 defsubr (&Strace_to_stderr);
29703 #endif
29704 #ifdef HAVE_WINDOW_SYSTEM
29705 defsubr (&Stool_bar_height);
29706 defsubr (&Slookup_image_map);
29707 #endif
29708 defsubr (&Sline_pixel_height);
29709 defsubr (&Sformat_mode_line);
29710 defsubr (&Sinvisible_p);
29711 defsubr (&Scurrent_bidi_paragraph_direction);
29712 defsubr (&Swindow_text_pixel_size);
29713 defsubr (&Smove_point_visually);
29714
29715 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29716 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29717 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29718 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29719 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29720 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29721 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29722 DEFSYM (Qeval, "eval");
29723 DEFSYM (QCdata, ":data");
29724 DEFSYM (Qdisplay, "display");
29725 DEFSYM (Qspace_width, "space-width");
29726 DEFSYM (Qraise, "raise");
29727 DEFSYM (Qslice, "slice");
29728 DEFSYM (Qspace, "space");
29729 DEFSYM (Qmargin, "margin");
29730 DEFSYM (Qpointer, "pointer");
29731 DEFSYM (Qleft_margin, "left-margin");
29732 DEFSYM (Qright_margin, "right-margin");
29733 DEFSYM (Qcenter, "center");
29734 DEFSYM (Qline_height, "line-height");
29735 DEFSYM (QCalign_to, ":align-to");
29736 DEFSYM (QCrelative_width, ":relative-width");
29737 DEFSYM (QCrelative_height, ":relative-height");
29738 DEFSYM (QCeval, ":eval");
29739 DEFSYM (QCpropertize, ":propertize");
29740 DEFSYM (QCfile, ":file");
29741 DEFSYM (Qfontified, "fontified");
29742 DEFSYM (Qfontification_functions, "fontification-functions");
29743 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29744 DEFSYM (Qescape_glyph, "escape-glyph");
29745 DEFSYM (Qnobreak_space, "nobreak-space");
29746 DEFSYM (Qimage, "image");
29747 DEFSYM (Qtext, "text");
29748 DEFSYM (Qboth, "both");
29749 DEFSYM (Qboth_horiz, "both-horiz");
29750 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29751 DEFSYM (QCmap, ":map");
29752 DEFSYM (QCpointer, ":pointer");
29753 DEFSYM (Qrect, "rect");
29754 DEFSYM (Qcircle, "circle");
29755 DEFSYM (Qpoly, "poly");
29756 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29757 DEFSYM (Qgrow_only, "grow-only");
29758 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29759 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29760 DEFSYM (Qposition, "position");
29761 DEFSYM (Qbuffer_position, "buffer-position");
29762 DEFSYM (Qobject, "object");
29763 DEFSYM (Qbar, "bar");
29764 DEFSYM (Qhbar, "hbar");
29765 DEFSYM (Qbox, "box");
29766 DEFSYM (Qhollow, "hollow");
29767 DEFSYM (Qhand, "hand");
29768 DEFSYM (Qarrow, "arrow");
29769 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29770
29771 list_of_error = list1 (list2 (intern_c_string ("error"),
29772 intern_c_string ("void-variable")));
29773 staticpro (&list_of_error);
29774
29775 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29776 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29777 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29778 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29779
29780 echo_buffer[0] = echo_buffer[1] = Qnil;
29781 staticpro (&echo_buffer[0]);
29782 staticpro (&echo_buffer[1]);
29783
29784 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29785 staticpro (&echo_area_buffer[0]);
29786 staticpro (&echo_area_buffer[1]);
29787
29788 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29789 staticpro (&Vmessages_buffer_name);
29790
29791 mode_line_proptrans_alist = Qnil;
29792 staticpro (&mode_line_proptrans_alist);
29793 mode_line_string_list = Qnil;
29794 staticpro (&mode_line_string_list);
29795 mode_line_string_face = Qnil;
29796 staticpro (&mode_line_string_face);
29797 mode_line_string_face_prop = Qnil;
29798 staticpro (&mode_line_string_face_prop);
29799 Vmode_line_unwind_vector = Qnil;
29800 staticpro (&Vmode_line_unwind_vector);
29801
29802 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29803
29804 help_echo_string = Qnil;
29805 staticpro (&help_echo_string);
29806 help_echo_object = Qnil;
29807 staticpro (&help_echo_object);
29808 help_echo_window = Qnil;
29809 staticpro (&help_echo_window);
29810 previous_help_echo_string = Qnil;
29811 staticpro (&previous_help_echo_string);
29812 help_echo_pos = -1;
29813
29814 DEFSYM (Qright_to_left, "right-to-left");
29815 DEFSYM (Qleft_to_right, "left-to-right");
29816
29817 #ifdef HAVE_WINDOW_SYSTEM
29818 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29819 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29820 For example, if a block cursor is over a tab, it will be drawn as
29821 wide as that tab on the display. */);
29822 x_stretch_cursor_p = 0;
29823 #endif
29824
29825 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29826 doc: /* Non-nil means highlight trailing whitespace.
29827 The face used for trailing whitespace is `trailing-whitespace'. */);
29828 Vshow_trailing_whitespace = Qnil;
29829
29830 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29831 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29832 If the value is t, Emacs highlights non-ASCII chars which have the
29833 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29834 or `escape-glyph' face respectively.
29835
29836 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29837 U+2011 (non-breaking hyphen) are affected.
29838
29839 Any other non-nil value means to display these characters as a escape
29840 glyph followed by an ordinary space or hyphen.
29841
29842 A value of nil means no special handling of these characters. */);
29843 Vnobreak_char_display = Qt;
29844
29845 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29846 doc: /* The pointer shape to show in void text areas.
29847 A value of nil means to show the text pointer. Other options are `arrow',
29848 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29849 Vvoid_text_area_pointer = Qarrow;
29850
29851 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29852 doc: /* Non-nil means don't actually do any redisplay.
29853 This is used for internal purposes. */);
29854 Vinhibit_redisplay = Qnil;
29855
29856 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29857 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29858 Vglobal_mode_string = Qnil;
29859
29860 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29861 doc: /* Marker for where to display an arrow on top of the buffer text.
29862 This must be the beginning of a line in order to work.
29863 See also `overlay-arrow-string'. */);
29864 Voverlay_arrow_position = Qnil;
29865
29866 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29867 doc: /* String to display as an arrow in non-window frames.
29868 See also `overlay-arrow-position'. */);
29869 Voverlay_arrow_string = build_pure_c_string ("=>");
29870
29871 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29872 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29873 The symbols on this list are examined during redisplay to determine
29874 where to display overlay arrows. */);
29875 Voverlay_arrow_variable_list
29876 = list1 (intern_c_string ("overlay-arrow-position"));
29877
29878 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29879 doc: /* The number of lines to try scrolling a window by when point moves out.
29880 If that fails to bring point back on frame, point is centered instead.
29881 If this is zero, point is always centered after it moves off frame.
29882 If you want scrolling to always be a line at a time, you should set
29883 `scroll-conservatively' to a large value rather than set this to 1. */);
29884
29885 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29886 doc: /* Scroll up to this many lines, to bring point back on screen.
29887 If point moves off-screen, redisplay will scroll by up to
29888 `scroll-conservatively' lines in order to bring point just barely
29889 onto the screen again. If that cannot be done, then redisplay
29890 recenters point as usual.
29891
29892 If the value is greater than 100, redisplay will never recenter point,
29893 but will always scroll just enough text to bring point into view, even
29894 if you move far away.
29895
29896 A value of zero means always recenter point if it moves off screen. */);
29897 scroll_conservatively = 0;
29898
29899 DEFVAR_INT ("scroll-margin", scroll_margin,
29900 doc: /* Number of lines of margin at the top and bottom of a window.
29901 Recenter the window whenever point gets within this many lines
29902 of the top or bottom of the window. */);
29903 scroll_margin = 0;
29904
29905 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29906 doc: /* Pixels per inch value for non-window system displays.
29907 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29908 Vdisplay_pixels_per_inch = make_float (72.0);
29909
29910 #ifdef GLYPH_DEBUG
29911 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29912 #endif
29913
29914 DEFVAR_LISP ("truncate-partial-width-windows",
29915 Vtruncate_partial_width_windows,
29916 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29917 For an integer value, truncate lines in each window narrower than the
29918 full frame width, provided the window width is less than that integer;
29919 otherwise, respect the value of `truncate-lines'.
29920
29921 For any other non-nil value, truncate lines in all windows that do
29922 not span the full frame width.
29923
29924 A value of nil means to respect the value of `truncate-lines'.
29925
29926 If `word-wrap' is enabled, you might want to reduce this. */);
29927 Vtruncate_partial_width_windows = make_number (50);
29928
29929 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29930 doc: /* Maximum buffer size for which line number should be displayed.
29931 If the buffer is bigger than this, the line number does not appear
29932 in the mode line. A value of nil means no limit. */);
29933 Vline_number_display_limit = Qnil;
29934
29935 DEFVAR_INT ("line-number-display-limit-width",
29936 line_number_display_limit_width,
29937 doc: /* Maximum line width (in characters) for line number display.
29938 If the average length of the lines near point is bigger than this, then the
29939 line number may be omitted from the mode line. */);
29940 line_number_display_limit_width = 200;
29941
29942 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29943 doc: /* Non-nil means highlight region even in nonselected windows. */);
29944 highlight_nonselected_windows = 0;
29945
29946 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29947 doc: /* Non-nil if more than one frame is visible on this display.
29948 Minibuffer-only frames don't count, but iconified frames do.
29949 This variable is not guaranteed to be accurate except while processing
29950 `frame-title-format' and `icon-title-format'. */);
29951
29952 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29953 doc: /* Template for displaying the title bar of visible frames.
29954 \(Assuming the window manager supports this feature.)
29955
29956 This variable has the same structure as `mode-line-format', except that
29957 the %c and %l constructs are ignored. It is used only on frames for
29958 which no explicit name has been set \(see `modify-frame-parameters'). */);
29959
29960 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29961 doc: /* Template for displaying the title bar of an iconified frame.
29962 \(Assuming the window manager supports this feature.)
29963 This variable has the same structure as `mode-line-format' (which see),
29964 and is used only on frames for which no explicit name has been set
29965 \(see `modify-frame-parameters'). */);
29966 Vicon_title_format
29967 = Vframe_title_format
29968 = listn (CONSTYPE_PURE, 3,
29969 intern_c_string ("multiple-frames"),
29970 build_pure_c_string ("%b"),
29971 listn (CONSTYPE_PURE, 4,
29972 empty_unibyte_string,
29973 intern_c_string ("invocation-name"),
29974 build_pure_c_string ("@"),
29975 intern_c_string ("system-name")));
29976
29977 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29978 doc: /* Maximum number of lines to keep in the message log buffer.
29979 If nil, disable message logging. If t, log messages but don't truncate
29980 the buffer when it becomes large. */);
29981 Vmessage_log_max = make_number (1000);
29982
29983 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29984 doc: /* Functions called before redisplay, if window sizes have changed.
29985 The value should be a list of functions that take one argument.
29986 Just before redisplay, for each frame, if any of its windows have changed
29987 size since the last redisplay, or have been split or deleted,
29988 all the functions in the list are called, with the frame as argument. */);
29989 Vwindow_size_change_functions = Qnil;
29990
29991 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29992 doc: /* List of functions to call before redisplaying a window with scrolling.
29993 Each function is called with two arguments, the window and its new
29994 display-start position. Note that these functions are also called by
29995 `set-window-buffer'. Also note that the value of `window-end' is not
29996 valid when these functions are called.
29997
29998 Warning: Do not use this feature to alter the way the window
29999 is scrolled. It is not designed for that, and such use probably won't
30000 work. */);
30001 Vwindow_scroll_functions = Qnil;
30002
30003 DEFVAR_LISP ("window-text-change-functions",
30004 Vwindow_text_change_functions,
30005 doc: /* Functions to call in redisplay when text in the window might change. */);
30006 Vwindow_text_change_functions = Qnil;
30007
30008 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30009 doc: /* Functions called when redisplay of a window reaches the end trigger.
30010 Each function is called with two arguments, the window and the end trigger value.
30011 See `set-window-redisplay-end-trigger'. */);
30012 Vredisplay_end_trigger_functions = Qnil;
30013
30014 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30015 doc: /* Non-nil means autoselect window with mouse pointer.
30016 If nil, do not autoselect windows.
30017 A positive number means delay autoselection by that many seconds: a
30018 window is autoselected only after the mouse has remained in that
30019 window for the duration of the delay.
30020 A negative number has a similar effect, but causes windows to be
30021 autoselected only after the mouse has stopped moving. \(Because of
30022 the way Emacs compares mouse events, you will occasionally wait twice
30023 that time before the window gets selected.\)
30024 Any other value means to autoselect window instantaneously when the
30025 mouse pointer enters it.
30026
30027 Autoselection selects the minibuffer only if it is active, and never
30028 unselects the minibuffer if it is active.
30029
30030 When customizing this variable make sure that the actual value of
30031 `focus-follows-mouse' matches the behavior of your window manager. */);
30032 Vmouse_autoselect_window = Qnil;
30033
30034 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30035 doc: /* Non-nil means automatically resize tool-bars.
30036 This dynamically changes the tool-bar's height to the minimum height
30037 that is needed to make all tool-bar items visible.
30038 If value is `grow-only', the tool-bar's height is only increased
30039 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30040 Vauto_resize_tool_bars = Qt;
30041
30042 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30043 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30044 auto_raise_tool_bar_buttons_p = 1;
30045
30046 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30047 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30048 make_cursor_line_fully_visible_p = 1;
30049
30050 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30051 doc: /* Border below tool-bar in pixels.
30052 If an integer, use it as the height of the border.
30053 If it is one of `internal-border-width' or `border-width', use the
30054 value of the corresponding frame parameter.
30055 Otherwise, no border is added below the tool-bar. */);
30056 Vtool_bar_border = Qinternal_border_width;
30057
30058 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30059 doc: /* Margin around tool-bar buttons in pixels.
30060 If an integer, use that for both horizontal and vertical margins.
30061 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30062 HORZ specifying the horizontal margin, and VERT specifying the
30063 vertical margin. */);
30064 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30065
30066 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30067 doc: /* Relief thickness of tool-bar buttons. */);
30068 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30069
30070 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30071 doc: /* Tool bar style to use.
30072 It can be one of
30073 image - show images only
30074 text - show text only
30075 both - show both, text below image
30076 both-horiz - show text to the right of the image
30077 text-image-horiz - show text to the left of the image
30078 any other - use system default or image if no system default.
30079
30080 This variable only affects the GTK+ toolkit version of Emacs. */);
30081 Vtool_bar_style = Qnil;
30082
30083 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30084 doc: /* Maximum number of characters a label can have to be shown.
30085 The tool bar style must also show labels for this to have any effect, see
30086 `tool-bar-style'. */);
30087 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30088
30089 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30090 doc: /* List of functions to call to fontify regions of text.
30091 Each function is called with one argument POS. Functions must
30092 fontify a region starting at POS in the current buffer, and give
30093 fontified regions the property `fontified'. */);
30094 Vfontification_functions = Qnil;
30095 Fmake_variable_buffer_local (Qfontification_functions);
30096
30097 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30098 unibyte_display_via_language_environment,
30099 doc: /* Non-nil means display unibyte text according to language environment.
30100 Specifically, this means that raw bytes in the range 160-255 decimal
30101 are displayed by converting them to the equivalent multibyte characters
30102 according to the current language environment. As a result, they are
30103 displayed according to the current fontset.
30104
30105 Note that this variable affects only how these bytes are displayed,
30106 but does not change the fact they are interpreted as raw bytes. */);
30107 unibyte_display_via_language_environment = 0;
30108
30109 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30110 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30111 If a float, it specifies a fraction of the mini-window frame's height.
30112 If an integer, it specifies a number of lines. */);
30113 Vmax_mini_window_height = make_float (0.25);
30114
30115 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30116 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30117 A value of nil means don't automatically resize mini-windows.
30118 A value of t means resize them to fit the text displayed in them.
30119 A value of `grow-only', the default, means let mini-windows grow only;
30120 they return to their normal size when the minibuffer is closed, or the
30121 echo area becomes empty. */);
30122 Vresize_mini_windows = Qgrow_only;
30123
30124 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30125 doc: /* Alist specifying how to blink the cursor off.
30126 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30127 `cursor-type' frame-parameter or variable equals ON-STATE,
30128 comparing using `equal', Emacs uses OFF-STATE to specify
30129 how to blink it off. ON-STATE and OFF-STATE are values for
30130 the `cursor-type' frame parameter.
30131
30132 If a frame's ON-STATE has no entry in this list,
30133 the frame's other specifications determine how to blink the cursor off. */);
30134 Vblink_cursor_alist = Qnil;
30135
30136 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30137 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30138 If non-nil, windows are automatically scrolled horizontally to make
30139 point visible. */);
30140 automatic_hscrolling_p = 1;
30141 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30142
30143 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30144 doc: /* How many columns away from the window edge point is allowed to get
30145 before automatic hscrolling will horizontally scroll the window. */);
30146 hscroll_margin = 5;
30147
30148 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30149 doc: /* How many columns to scroll the window when point gets too close to the edge.
30150 When point is less than `hscroll-margin' columns from the window
30151 edge, automatic hscrolling will scroll the window by the amount of columns
30152 determined by this variable. If its value is a positive integer, scroll that
30153 many columns. If it's a positive floating-point number, it specifies the
30154 fraction of the window's width to scroll. If it's nil or zero, point will be
30155 centered horizontally after the scroll. Any other value, including negative
30156 numbers, are treated as if the value were zero.
30157
30158 Automatic hscrolling always moves point outside the scroll margin, so if
30159 point was more than scroll step columns inside the margin, the window will
30160 scroll more than the value given by the scroll step.
30161
30162 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30163 and `scroll-right' overrides this variable's effect. */);
30164 Vhscroll_step = make_number (0);
30165
30166 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30167 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30168 Bind this around calls to `message' to let it take effect. */);
30169 message_truncate_lines = 0;
30170
30171 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30172 doc: /* Normal hook run to update the menu bar definitions.
30173 Redisplay runs this hook before it redisplays the menu bar.
30174 This is used to update menus such as Buffers, whose contents depend on
30175 various data. */);
30176 Vmenu_bar_update_hook = Qnil;
30177
30178 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30179 doc: /* Frame for which we are updating a menu.
30180 The enable predicate for a menu binding should check this variable. */);
30181 Vmenu_updating_frame = Qnil;
30182
30183 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30184 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30185 inhibit_menubar_update = 0;
30186
30187 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30188 doc: /* Prefix prepended to all continuation lines at display time.
30189 The value may be a string, an image, or a stretch-glyph; it is
30190 interpreted in the same way as the value of a `display' text property.
30191
30192 This variable is overridden by any `wrap-prefix' text or overlay
30193 property.
30194
30195 To add a prefix to non-continuation lines, use `line-prefix'. */);
30196 Vwrap_prefix = Qnil;
30197 DEFSYM (Qwrap_prefix, "wrap-prefix");
30198 Fmake_variable_buffer_local (Qwrap_prefix);
30199
30200 DEFVAR_LISP ("line-prefix", Vline_prefix,
30201 doc: /* Prefix prepended to all non-continuation lines at display time.
30202 The value may be a string, an image, or a stretch-glyph; it is
30203 interpreted in the same way as the value of a `display' text property.
30204
30205 This variable is overridden by any `line-prefix' text or overlay
30206 property.
30207
30208 To add a prefix to continuation lines, use `wrap-prefix'. */);
30209 Vline_prefix = Qnil;
30210 DEFSYM (Qline_prefix, "line-prefix");
30211 Fmake_variable_buffer_local (Qline_prefix);
30212
30213 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30214 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30215 inhibit_eval_during_redisplay = 0;
30216
30217 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30218 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30219 inhibit_free_realized_faces = 0;
30220
30221 #ifdef GLYPH_DEBUG
30222 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30223 doc: /* Inhibit try_window_id display optimization. */);
30224 inhibit_try_window_id = 0;
30225
30226 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30227 doc: /* Inhibit try_window_reusing display optimization. */);
30228 inhibit_try_window_reusing = 0;
30229
30230 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30231 doc: /* Inhibit try_cursor_movement display optimization. */);
30232 inhibit_try_cursor_movement = 0;
30233 #endif /* GLYPH_DEBUG */
30234
30235 DEFVAR_INT ("overline-margin", overline_margin,
30236 doc: /* Space between overline and text, in pixels.
30237 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30238 margin to the character height. */);
30239 overline_margin = 2;
30240
30241 DEFVAR_INT ("underline-minimum-offset",
30242 underline_minimum_offset,
30243 doc: /* Minimum distance between baseline and underline.
30244 This can improve legibility of underlined text at small font sizes,
30245 particularly when using variable `x-use-underline-position-properties'
30246 with fonts that specify an UNDERLINE_POSITION relatively close to the
30247 baseline. The default value is 1. */);
30248 underline_minimum_offset = 1;
30249
30250 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30251 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30252 This feature only works when on a window system that can change
30253 cursor shapes. */);
30254 display_hourglass_p = 1;
30255
30256 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30257 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30258 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30259
30260 #ifdef HAVE_WINDOW_SYSTEM
30261 hourglass_atimer = NULL;
30262 hourglass_shown_p = 0;
30263 #endif /* HAVE_WINDOW_SYSTEM */
30264
30265 DEFSYM (Qglyphless_char, "glyphless-char");
30266 DEFSYM (Qhex_code, "hex-code");
30267 DEFSYM (Qempty_box, "empty-box");
30268 DEFSYM (Qthin_space, "thin-space");
30269 DEFSYM (Qzero_width, "zero-width");
30270
30271 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30272 doc: /* Function run just before redisplay.
30273 It is called with one argument, which is the set of windows that are to
30274 be redisplayed. This set can be nil (meaning, only the selected window),
30275 or t (meaning all windows). */);
30276 Vpre_redisplay_function = intern ("ignore");
30277
30278 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30279 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30280
30281 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30282 doc: /* Char-table defining glyphless characters.
30283 Each element, if non-nil, should be one of the following:
30284 an ASCII acronym string: display this string in a box
30285 `hex-code': display the hexadecimal code of a character in a box
30286 `empty-box': display as an empty box
30287 `thin-space': display as 1-pixel width space
30288 `zero-width': don't display
30289 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30290 display method for graphical terminals and text terminals respectively.
30291 GRAPHICAL and TEXT should each have one of the values listed above.
30292
30293 The char-table has one extra slot to control the display of a character for
30294 which no font is found. This slot only takes effect on graphical terminals.
30295 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30296 `thin-space'. The default is `empty-box'.
30297
30298 If a character has a non-nil entry in an active display table, the
30299 display table takes effect; in this case, Emacs does not consult
30300 `glyphless-char-display' at all. */);
30301 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30302 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30303 Qempty_box);
30304
30305 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30306 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30307 Vdebug_on_message = Qnil;
30308
30309 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30310 doc: /* */);
30311 Vredisplay__all_windows_cause
30312 = Fmake_vector (make_number (100), make_number (0));
30313
30314 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30315 doc: /* */);
30316 Vredisplay__mode_lines_cause
30317 = Fmake_vector (make_number (100), make_number (0));
30318 }
30319
30320
30321 /* Initialize this module when Emacs starts. */
30322
30323 void
30324 init_xdisp (void)
30325 {
30326 CHARPOS (this_line_start_pos) = 0;
30327
30328 if (!noninteractive)
30329 {
30330 struct window *m = XWINDOW (minibuf_window);
30331 Lisp_Object frame = m->frame;
30332 struct frame *f = XFRAME (frame);
30333 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30334 struct window *r = XWINDOW (root);
30335 int i;
30336
30337 echo_area_window = minibuf_window;
30338
30339 r->top_line = FRAME_TOP_MARGIN (f);
30340 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30341 r->total_cols = FRAME_COLS (f);
30342 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30343 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30344 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30345
30346 m->top_line = FRAME_LINES (f) - 1;
30347 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30348 m->total_cols = FRAME_COLS (f);
30349 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30350 m->total_lines = 1;
30351 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30352
30353 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30354 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30355 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30356
30357 /* The default ellipsis glyphs `...'. */
30358 for (i = 0; i < 3; ++i)
30359 default_invis_vector[i] = make_number ('.');
30360 }
30361
30362 {
30363 /* Allocate the buffer for frame titles.
30364 Also used for `format-mode-line'. */
30365 int size = 100;
30366 mode_line_noprop_buf = xmalloc (size);
30367 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30368 mode_line_noprop_ptr = mode_line_noprop_buf;
30369 mode_line_target = MODE_LINE_DISPLAY;
30370 }
30371
30372 help_echo_showing_p = 0;
30373 }
30374
30375 #ifdef HAVE_WINDOW_SYSTEM
30376
30377 /* Platform-independent portion of hourglass implementation. */
30378
30379 /* Cancel a currently active hourglass timer, and start a new one. */
30380 void
30381 start_hourglass (void)
30382 {
30383 struct timespec delay;
30384
30385 cancel_hourglass ();
30386
30387 if (INTEGERP (Vhourglass_delay)
30388 && XINT (Vhourglass_delay) > 0)
30389 delay = make_timespec (min (XINT (Vhourglass_delay),
30390 TYPE_MAXIMUM (time_t)),
30391 0);
30392 else if (FLOATP (Vhourglass_delay)
30393 && XFLOAT_DATA (Vhourglass_delay) > 0)
30394 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30395 else
30396 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30397
30398 #ifdef HAVE_NTGUI
30399 {
30400 extern void w32_note_current_window (void);
30401 w32_note_current_window ();
30402 }
30403 #endif /* HAVE_NTGUI */
30404
30405 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30406 show_hourglass, NULL);
30407 }
30408
30409
30410 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30411 shown. */
30412 void
30413 cancel_hourglass (void)
30414 {
30415 if (hourglass_atimer)
30416 {
30417 cancel_atimer (hourglass_atimer);
30418 hourglass_atimer = NULL;
30419 }
30420
30421 if (hourglass_shown_p)
30422 hide_hourglass ();
30423 }
30424
30425 #endif /* HAVE_WINDOW_SYSTEM */