* src/insdel.c (prepare_to_modify_buffer_1): Cancel lock-file checks and
[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 {
2423 gx += window_box_left_offset (w, area);
2424 /* Don't expand over the modeline to make sure the vertical
2425 drag cursor is shown early enough. */
2426 height = min (height,
2427 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2428 }
2429 }
2430 else
2431 {
2432 /* Use nominal line height at end of window. */
2433 gx = (x / width) * width;
2434 y -= gy;
2435 gy += (y / height) * height;
2436 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2437 /* See comment above. */
2438 height = min (height,
2439 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2440 }
2441 break;
2442
2443 case ON_LEFT_FRINGE:
2444 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2445 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2446 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2447 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2448 goto row_glyph;
2449
2450 case ON_RIGHT_FRINGE:
2451 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2452 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2453 : window_box_right_offset (w, TEXT_AREA));
2454 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2455 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2456 && !WINDOW_RIGHTMOST_P (w))
2457 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2458 /* Make sure the vertical border can get her own glyph to the
2459 right of the one we build here. */
2460 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2461 else
2462 width = WINDOW_PIXEL_WIDTH (w) - gx;
2463 else
2464 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2465
2466 goto row_glyph;
2467
2468 case ON_VERTICAL_BORDER:
2469 gx = WINDOW_PIXEL_WIDTH (w) - width;
2470 goto row_glyph;
2471
2472 case ON_SCROLL_BAR:
2473 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2474 ? 0
2475 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2476 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2477 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2478 : 0)));
2479 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2480
2481 row_glyph:
2482 gr = 0, gy = 0;
2483 for (; r <= end_row && r->enabled_p; ++r)
2484 if (r->y + r->height > y)
2485 {
2486 gr = r; gy = r->y;
2487 break;
2488 }
2489
2490 if (gr && gy <= y)
2491 height = gr->height;
2492 else
2493 {
2494 /* Use nominal line height at end of window. */
2495 y -= gy;
2496 gy += (y / height) * height;
2497 }
2498 break;
2499
2500 case ON_RIGHT_DIVIDER:
2501 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2502 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2503 gy = 0;
2504 /* The bottom divider prevails. */
2505 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2506 goto add_edge;;
2507
2508 case ON_BOTTOM_DIVIDER:
2509 gx = 0;
2510 width = WINDOW_PIXEL_WIDTH (w);
2511 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2512 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2513 goto add_edge;
2514
2515 default:
2516 ;
2517 virtual_glyph:
2518 /* If there is no glyph under the mouse, then we divide the screen
2519 into a grid of the smallest glyph in the frame, and use that
2520 as our "glyph". */
2521
2522 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2523 round down even for negative values. */
2524 if (gx < 0)
2525 gx -= width - 1;
2526 if (gy < 0)
2527 gy -= height - 1;
2528
2529 gx = (gx / width) * width;
2530 gy = (gy / height) * height;
2531
2532 goto store_rect;
2533 }
2534
2535 add_edge:
2536 gx += WINDOW_LEFT_EDGE_X (w);
2537 gy += WINDOW_TOP_EDGE_Y (w);
2538
2539 store_rect:
2540 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2541
2542 /* Visible feedback for debugging. */
2543 #if 0
2544 #if HAVE_X_WINDOWS
2545 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2546 f->output_data.x->normal_gc,
2547 gx, gy, width, height);
2548 #endif
2549 #endif
2550 }
2551
2552
2553 #endif /* HAVE_WINDOW_SYSTEM */
2554
2555 static void
2556 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2557 {
2558 eassert (w);
2559 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2560 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2561 w->window_end_vpos
2562 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2563 }
2564
2565 /***********************************************************************
2566 Lisp form evaluation
2567 ***********************************************************************/
2568
2569 /* Error handler for safe_eval and safe_call. */
2570
2571 static Lisp_Object
2572 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2573 {
2574 add_to_log ("Error during redisplay: %S signaled %S",
2575 Flist (nargs, args), arg);
2576 return Qnil;
2577 }
2578
2579 /* Call function FUNC with the rest of NARGS - 1 arguments
2580 following. Return the result, or nil if something went
2581 wrong. Prevent redisplay during the evaluation. */
2582
2583 Lisp_Object
2584 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2585 {
2586 Lisp_Object val;
2587
2588 if (inhibit_eval_during_redisplay)
2589 val = Qnil;
2590 else
2591 {
2592 va_list ap;
2593 ptrdiff_t i;
2594 ptrdiff_t count = SPECPDL_INDEX ();
2595 struct gcpro gcpro1;
2596 Lisp_Object *args = alloca (nargs * word_size);
2597
2598 args[0] = func;
2599 va_start (ap, func);
2600 for (i = 1; i < nargs; i++)
2601 args[i] = va_arg (ap, Lisp_Object);
2602 va_end (ap);
2603
2604 GCPRO1 (args[0]);
2605 gcpro1.nvars = nargs;
2606 specbind (Qinhibit_redisplay, Qt);
2607 /* Use Qt to ensure debugger does not run,
2608 so there is no possibility of wanting to redisplay. */
2609 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2610 safe_eval_handler);
2611 UNGCPRO;
2612 val = unbind_to (count, val);
2613 }
2614
2615 return val;
2616 }
2617
2618
2619 /* Call function FN with one argument ARG.
2620 Return the result, or nil if something went wrong. */
2621
2622 Lisp_Object
2623 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2624 {
2625 return safe_call (2, fn, arg);
2626 }
2627
2628 static Lisp_Object Qeval;
2629
2630 Lisp_Object
2631 safe_eval (Lisp_Object sexpr)
2632 {
2633 return safe_call1 (Qeval, sexpr);
2634 }
2635
2636 /* Call function FN with two arguments ARG1 and ARG2.
2637 Return the result, or nil if something went wrong. */
2638
2639 Lisp_Object
2640 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2641 {
2642 return safe_call (3, fn, arg1, arg2);
2643 }
2644
2645
2646 \f
2647 /***********************************************************************
2648 Debugging
2649 ***********************************************************************/
2650
2651 #if 0
2652
2653 /* Define CHECK_IT to perform sanity checks on iterators.
2654 This is for debugging. It is too slow to do unconditionally. */
2655
2656 static void
2657 check_it (struct it *it)
2658 {
2659 if (it->method == GET_FROM_STRING)
2660 {
2661 eassert (STRINGP (it->string));
2662 eassert (IT_STRING_CHARPOS (*it) >= 0);
2663 }
2664 else
2665 {
2666 eassert (IT_STRING_CHARPOS (*it) < 0);
2667 if (it->method == GET_FROM_BUFFER)
2668 {
2669 /* Check that character and byte positions agree. */
2670 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2671 }
2672 }
2673
2674 if (it->dpvec)
2675 eassert (it->current.dpvec_index >= 0);
2676 else
2677 eassert (it->current.dpvec_index < 0);
2678 }
2679
2680 #define CHECK_IT(IT) check_it ((IT))
2681
2682 #else /* not 0 */
2683
2684 #define CHECK_IT(IT) (void) 0
2685
2686 #endif /* not 0 */
2687
2688
2689 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2690
2691 /* Check that the window end of window W is what we expect it
2692 to be---the last row in the current matrix displaying text. */
2693
2694 static void
2695 check_window_end (struct window *w)
2696 {
2697 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2698 {
2699 struct glyph_row *row;
2700 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2701 !row->enabled_p
2702 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2703 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2704 }
2705 }
2706
2707 #define CHECK_WINDOW_END(W) check_window_end ((W))
2708
2709 #else
2710
2711 #define CHECK_WINDOW_END(W) (void) 0
2712
2713 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2714
2715 /***********************************************************************
2716 Iterator initialization
2717 ***********************************************************************/
2718
2719 /* Initialize IT for displaying current_buffer in window W, starting
2720 at character position CHARPOS. CHARPOS < 0 means that no buffer
2721 position is specified which is useful when the iterator is assigned
2722 a position later. BYTEPOS is the byte position corresponding to
2723 CHARPOS.
2724
2725 If ROW is not null, calls to produce_glyphs with IT as parameter
2726 will produce glyphs in that row.
2727
2728 BASE_FACE_ID is the id of a base face to use. It must be one of
2729 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2730 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2731 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2732
2733 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2735 will be initialized to use the corresponding mode line glyph row of
2736 the desired matrix of W. */
2737
2738 void
2739 init_iterator (struct it *it, struct window *w,
2740 ptrdiff_t charpos, ptrdiff_t bytepos,
2741 struct glyph_row *row, enum face_id base_face_id)
2742 {
2743 enum face_id remapped_base_face_id = base_face_id;
2744
2745 /* Some precondition checks. */
2746 eassert (w != NULL && it != NULL);
2747 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2748 && charpos <= ZV));
2749
2750 /* If face attributes have been changed since the last redisplay,
2751 free realized faces now because they depend on face definitions
2752 that might have changed. Don't free faces while there might be
2753 desired matrices pending which reference these faces. */
2754 if (face_change_count && !inhibit_free_realized_faces)
2755 {
2756 face_change_count = 0;
2757 free_all_realized_faces (Qnil);
2758 }
2759
2760 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2761 if (! NILP (Vface_remapping_alist))
2762 remapped_base_face_id
2763 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2764
2765 /* Use one of the mode line rows of W's desired matrix if
2766 appropriate. */
2767 if (row == NULL)
2768 {
2769 if (base_face_id == MODE_LINE_FACE_ID
2770 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2771 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2772 else if (base_face_id == HEADER_LINE_FACE_ID)
2773 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2774 }
2775
2776 /* Clear IT. */
2777 memset (it, 0, sizeof *it);
2778 it->current.overlay_string_index = -1;
2779 it->current.dpvec_index = -1;
2780 it->base_face_id = remapped_base_face_id;
2781 it->string = Qnil;
2782 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2783 it->paragraph_embedding = L2R;
2784 it->bidi_it.string.lstring = Qnil;
2785 it->bidi_it.string.s = NULL;
2786 it->bidi_it.string.bufpos = 0;
2787 it->bidi_it.w = w;
2788
2789 /* The window in which we iterate over current_buffer: */
2790 XSETWINDOW (it->window, w);
2791 it->w = w;
2792 it->f = XFRAME (w->frame);
2793
2794 it->cmp_it.id = -1;
2795
2796 /* Extra space between lines (on window systems only). */
2797 if (base_face_id == DEFAULT_FACE_ID
2798 && FRAME_WINDOW_P (it->f))
2799 {
2800 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2801 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2802 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2803 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2804 * FRAME_LINE_HEIGHT (it->f));
2805 else if (it->f->extra_line_spacing > 0)
2806 it->extra_line_spacing = it->f->extra_line_spacing;
2807 it->max_extra_line_spacing = 0;
2808 }
2809
2810 /* If realized faces have been removed, e.g. because of face
2811 attribute changes of named faces, recompute them. When running
2812 in batch mode, the face cache of the initial frame is null. If
2813 we happen to get called, make a dummy face cache. */
2814 if (FRAME_FACE_CACHE (it->f) == NULL)
2815 init_frame_faces (it->f);
2816 if (FRAME_FACE_CACHE (it->f)->used == 0)
2817 recompute_basic_faces (it->f);
2818
2819 /* Current value of the `slice', `space-width', and 'height' properties. */
2820 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2821 it->space_width = Qnil;
2822 it->font_height = Qnil;
2823 it->override_ascent = -1;
2824
2825 /* Are control characters displayed as `^C'? */
2826 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2827
2828 /* -1 means everything between a CR and the following line end
2829 is invisible. >0 means lines indented more than this value are
2830 invisible. */
2831 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2832 ? (clip_to_bounds
2833 (-1, XINT (BVAR (current_buffer, selective_display)),
2834 PTRDIFF_MAX))
2835 : (!NILP (BVAR (current_buffer, selective_display))
2836 ? -1 : 0));
2837 it->selective_display_ellipsis_p
2838 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2839
2840 /* Display table to use. */
2841 it->dp = window_display_table (w);
2842
2843 /* Are multibyte characters enabled in current_buffer? */
2844 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2845
2846 /* Get the position at which the redisplay_end_trigger hook should
2847 be run, if it is to be run at all. */
2848 if (MARKERP (w->redisplay_end_trigger)
2849 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2850 it->redisplay_end_trigger_charpos
2851 = marker_position (w->redisplay_end_trigger);
2852 else if (INTEGERP (w->redisplay_end_trigger))
2853 it->redisplay_end_trigger_charpos
2854 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2855 PTRDIFF_MAX);
2856
2857 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2858
2859 /* Are lines in the display truncated? */
2860 if (base_face_id != DEFAULT_FACE_ID
2861 || it->w->hscroll
2862 || (! WINDOW_FULL_WIDTH_P (it->w)
2863 && ((!NILP (Vtruncate_partial_width_windows)
2864 && !INTEGERP (Vtruncate_partial_width_windows))
2865 || (INTEGERP (Vtruncate_partial_width_windows)
2866 /* PXW: Shall we do something about this? */
2867 && (WINDOW_TOTAL_COLS (it->w)
2868 < XINT (Vtruncate_partial_width_windows))))))
2869 it->line_wrap = TRUNCATE;
2870 else if (NILP (BVAR (current_buffer, truncate_lines)))
2871 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2872 ? WINDOW_WRAP : WORD_WRAP;
2873 else
2874 it->line_wrap = TRUNCATE;
2875
2876 /* Get dimensions of truncation and continuation glyphs. These are
2877 displayed as fringe bitmaps under X, but we need them for such
2878 frames when the fringes are turned off. But leave the dimensions
2879 zero for tooltip frames, as these glyphs look ugly there and also
2880 sabotage calculations of tooltip dimensions in x-show-tip. */
2881 #ifdef HAVE_WINDOW_SYSTEM
2882 if (!(FRAME_WINDOW_P (it->f)
2883 && FRAMEP (tip_frame)
2884 && it->f == XFRAME (tip_frame)))
2885 #endif
2886 {
2887 if (it->line_wrap == TRUNCATE)
2888 {
2889 /* We will need the truncation glyph. */
2890 eassert (it->glyph_row == NULL);
2891 produce_special_glyphs (it, IT_TRUNCATION);
2892 it->truncation_pixel_width = it->pixel_width;
2893 }
2894 else
2895 {
2896 /* We will need the continuation glyph. */
2897 eassert (it->glyph_row == NULL);
2898 produce_special_glyphs (it, IT_CONTINUATION);
2899 it->continuation_pixel_width = it->pixel_width;
2900 }
2901 }
2902
2903 /* Reset these values to zero because the produce_special_glyphs
2904 above has changed them. */
2905 it->pixel_width = it->ascent = it->descent = 0;
2906 it->phys_ascent = it->phys_descent = 0;
2907
2908 /* Set this after getting the dimensions of truncation and
2909 continuation glyphs, so that we don't produce glyphs when calling
2910 produce_special_glyphs, above. */
2911 it->glyph_row = row;
2912 it->area = TEXT_AREA;
2913
2914 /* Forget any previous info about this row being reversed. */
2915 if (it->glyph_row)
2916 it->glyph_row->reversed_p = 0;
2917
2918 /* Get the dimensions of the display area. The display area
2919 consists of the visible window area plus a horizontally scrolled
2920 part to the left of the window. All x-values are relative to the
2921 start of this total display area. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2923 {
2924 /* Mode lines, menu bar in terminal frames. */
2925 it->first_visible_x = 0;
2926 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2927 }
2928 else
2929 {
2930 it->first_visible_x
2931 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2932 it->last_visible_x = (it->first_visible_x
2933 + window_box_width (w, TEXT_AREA));
2934
2935 /* If we truncate lines, leave room for the truncation glyph(s) at
2936 the right margin. Otherwise, leave room for the continuation
2937 glyph(s). Done only if the window has no fringes. Since we
2938 don't know at this point whether there will be any R2L lines in
2939 the window, we reserve space for truncation/continuation glyphs
2940 even if only one of the fringes is absent. */
2941 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2942 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2943 {
2944 if (it->line_wrap == TRUNCATE)
2945 it->last_visible_x -= it->truncation_pixel_width;
2946 else
2947 it->last_visible_x -= it->continuation_pixel_width;
2948 }
2949
2950 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2951 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2952 }
2953
2954 /* Leave room for a border glyph. */
2955 if (!FRAME_WINDOW_P (it->f)
2956 && !WINDOW_RIGHTMOST_P (it->w))
2957 it->last_visible_x -= 1;
2958
2959 it->last_visible_y = window_text_bottom_y (w);
2960
2961 /* For mode lines and alike, arrange for the first glyph having a
2962 left box line if the face specifies a box. */
2963 if (base_face_id != DEFAULT_FACE_ID)
2964 {
2965 struct face *face;
2966
2967 it->face_id = remapped_base_face_id;
2968
2969 /* If we have a boxed mode line, make the first character appear
2970 with a left box line. */
2971 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2972 if (face && face->box != FACE_NO_BOX)
2973 it->start_of_box_run_p = true;
2974 }
2975
2976 /* If a buffer position was specified, set the iterator there,
2977 getting overlays and face properties from that position. */
2978 if (charpos >= BUF_BEG (current_buffer))
2979 {
2980 it->end_charpos = ZV;
2981 eassert (charpos == BYTE_TO_CHAR (bytepos));
2982 IT_CHARPOS (*it) = charpos;
2983 IT_BYTEPOS (*it) = bytepos;
2984
2985 /* We will rely on `reseat' to set this up properly, via
2986 handle_face_prop. */
2987 it->face_id = it->base_face_id;
2988
2989 it->start = it->current;
2990 /* Do we need to reorder bidirectional text? Not if this is a
2991 unibyte buffer: by definition, none of the single-byte
2992 characters are strong R2L, so no reordering is needed. And
2993 bidi.c doesn't support unibyte buffers anyway. Also, don't
2994 reorder while we are loading loadup.el, since the tables of
2995 character properties needed for reordering are not yet
2996 available. */
2997 it->bidi_p =
2998 NILP (Vpurify_flag)
2999 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3000 && it->multibyte_p;
3001
3002 /* If we are to reorder bidirectional text, init the bidi
3003 iterator. */
3004 if (it->bidi_p)
3005 {
3006 /* Note the paragraph direction that this buffer wants to
3007 use. */
3008 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3009 Qleft_to_right))
3010 it->paragraph_embedding = L2R;
3011 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3012 Qright_to_left))
3013 it->paragraph_embedding = R2L;
3014 else
3015 it->paragraph_embedding = NEUTRAL_DIR;
3016 bidi_unshelve_cache (NULL, 0);
3017 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3018 &it->bidi_it);
3019 }
3020
3021 /* Compute faces etc. */
3022 reseat (it, it->current.pos, 1);
3023 }
3024
3025 CHECK_IT (it);
3026 }
3027
3028
3029 /* Initialize IT for the display of window W with window start POS. */
3030
3031 void
3032 start_display (struct it *it, struct window *w, struct text_pos pos)
3033 {
3034 struct glyph_row *row;
3035 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3036
3037 row = w->desired_matrix->rows + first_vpos;
3038 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3039 it->first_vpos = first_vpos;
3040
3041 /* Don't reseat to previous visible line start if current start
3042 position is in a string or image. */
3043 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3044 {
3045 int start_at_line_beg_p;
3046 int first_y = it->current_y;
3047
3048 /* If window start is not at a line start, skip forward to POS to
3049 get the correct continuation lines width. */
3050 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3051 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3052 if (!start_at_line_beg_p)
3053 {
3054 int new_x;
3055
3056 reseat_at_previous_visible_line_start (it);
3057 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3058
3059 new_x = it->current_x + it->pixel_width;
3060
3061 /* If lines are continued, this line may end in the middle
3062 of a multi-glyph character (e.g. a control character
3063 displayed as \003, or in the middle of an overlay
3064 string). In this case move_it_to above will not have
3065 taken us to the start of the continuation line but to the
3066 end of the continued line. */
3067 if (it->current_x > 0
3068 && it->line_wrap != TRUNCATE /* Lines are continued. */
3069 && (/* And glyph doesn't fit on the line. */
3070 new_x > it->last_visible_x
3071 /* Or it fits exactly and we're on a window
3072 system frame. */
3073 || (new_x == it->last_visible_x
3074 && FRAME_WINDOW_P (it->f)
3075 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3076 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3077 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3078 {
3079 if ((it->current.dpvec_index >= 0
3080 || it->current.overlay_string_index >= 0)
3081 /* If we are on a newline from a display vector or
3082 overlay string, then we are already at the end of
3083 a screen line; no need to go to the next line in
3084 that case, as this line is not really continued.
3085 (If we do go to the next line, C-e will not DTRT.) */
3086 && it->c != '\n')
3087 {
3088 set_iterator_to_next (it, 1);
3089 move_it_in_display_line_to (it, -1, -1, 0);
3090 }
3091
3092 it->continuation_lines_width += it->current_x;
3093 }
3094 /* If the character at POS is displayed via a display
3095 vector, move_it_to above stops at the final glyph of
3096 IT->dpvec. To make the caller redisplay that character
3097 again (a.k.a. start at POS), we need to reset the
3098 dpvec_index to the beginning of IT->dpvec. */
3099 else if (it->current.dpvec_index >= 0)
3100 it->current.dpvec_index = 0;
3101
3102 /* We're starting a new display line, not affected by the
3103 height of the continued line, so clear the appropriate
3104 fields in the iterator structure. */
3105 it->max_ascent = it->max_descent = 0;
3106 it->max_phys_ascent = it->max_phys_descent = 0;
3107
3108 it->current_y = first_y;
3109 it->vpos = 0;
3110 it->current_x = it->hpos = 0;
3111 }
3112 }
3113 }
3114
3115
3116 /* Return 1 if POS is a position in ellipses displayed for invisible
3117 text. W is the window we display, for text property lookup. */
3118
3119 static int
3120 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3121 {
3122 Lisp_Object prop, window;
3123 int ellipses_p = 0;
3124 ptrdiff_t charpos = CHARPOS (pos->pos);
3125
3126 /* If POS specifies a position in a display vector, this might
3127 be for an ellipsis displayed for invisible text. We won't
3128 get the iterator set up for delivering that ellipsis unless
3129 we make sure that it gets aware of the invisible text. */
3130 if (pos->dpvec_index >= 0
3131 && pos->overlay_string_index < 0
3132 && CHARPOS (pos->string_pos) < 0
3133 && charpos > BEGV
3134 && (XSETWINDOW (window, w),
3135 prop = Fget_char_property (make_number (charpos),
3136 Qinvisible, window),
3137 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3138 {
3139 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3140 window);
3141 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3142 }
3143
3144 return ellipses_p;
3145 }
3146
3147
3148 /* Initialize IT for stepping through current_buffer in window W,
3149 starting at position POS that includes overlay string and display
3150 vector/ control character translation position information. Value
3151 is zero if there are overlay strings with newlines at POS. */
3152
3153 static int
3154 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3155 {
3156 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3157 int i, overlay_strings_with_newlines = 0;
3158
3159 /* If POS specifies a position in a display vector, this might
3160 be for an ellipsis displayed for invisible text. We won't
3161 get the iterator set up for delivering that ellipsis unless
3162 we make sure that it gets aware of the invisible text. */
3163 if (in_ellipses_for_invisible_text_p (pos, w))
3164 {
3165 --charpos;
3166 bytepos = 0;
3167 }
3168
3169 /* Keep in mind: the call to reseat in init_iterator skips invisible
3170 text, so we might end up at a position different from POS. This
3171 is only a problem when POS is a row start after a newline and an
3172 overlay starts there with an after-string, and the overlay has an
3173 invisible property. Since we don't skip invisible text in
3174 display_line and elsewhere immediately after consuming the
3175 newline before the row start, such a POS will not be in a string,
3176 but the call to init_iterator below will move us to the
3177 after-string. */
3178 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3179
3180 /* This only scans the current chunk -- it should scan all chunks.
3181 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3182 to 16 in 22.1 to make this a lesser problem. */
3183 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3184 {
3185 const char *s = SSDATA (it->overlay_strings[i]);
3186 const char *e = s + SBYTES (it->overlay_strings[i]);
3187
3188 while (s < e && *s != '\n')
3189 ++s;
3190
3191 if (s < e)
3192 {
3193 overlay_strings_with_newlines = 1;
3194 break;
3195 }
3196 }
3197
3198 /* If position is within an overlay string, set up IT to the right
3199 overlay string. */
3200 if (pos->overlay_string_index >= 0)
3201 {
3202 int relative_index;
3203
3204 /* If the first overlay string happens to have a `display'
3205 property for an image, the iterator will be set up for that
3206 image, and we have to undo that setup first before we can
3207 correct the overlay string index. */
3208 if (it->method == GET_FROM_IMAGE)
3209 pop_it (it);
3210
3211 /* We already have the first chunk of overlay strings in
3212 IT->overlay_strings. Load more until the one for
3213 pos->overlay_string_index is in IT->overlay_strings. */
3214 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3215 {
3216 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3217 it->current.overlay_string_index = 0;
3218 while (n--)
3219 {
3220 load_overlay_strings (it, 0);
3221 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3222 }
3223 }
3224
3225 it->current.overlay_string_index = pos->overlay_string_index;
3226 relative_index = (it->current.overlay_string_index
3227 % OVERLAY_STRING_CHUNK_SIZE);
3228 it->string = it->overlay_strings[relative_index];
3229 eassert (STRINGP (it->string));
3230 it->current.string_pos = pos->string_pos;
3231 it->method = GET_FROM_STRING;
3232 it->end_charpos = SCHARS (it->string);
3233 /* Set up the bidi iterator for this overlay string. */
3234 if (it->bidi_p)
3235 {
3236 it->bidi_it.string.lstring = it->string;
3237 it->bidi_it.string.s = NULL;
3238 it->bidi_it.string.schars = SCHARS (it->string);
3239 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3240 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3241 it->bidi_it.string.unibyte = !it->multibyte_p;
3242 it->bidi_it.w = it->w;
3243 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3244 FRAME_WINDOW_P (it->f), &it->bidi_it);
3245
3246 /* Synchronize the state of the bidi iterator with
3247 pos->string_pos. For any string position other than
3248 zero, this will be done automagically when we resume
3249 iteration over the string and get_visually_first_element
3250 is called. But if string_pos is zero, and the string is
3251 to be reordered for display, we need to resync manually,
3252 since it could be that the iteration state recorded in
3253 pos ended at string_pos of 0 moving backwards in string. */
3254 if (CHARPOS (pos->string_pos) == 0)
3255 {
3256 get_visually_first_element (it);
3257 if (IT_STRING_CHARPOS (*it) != 0)
3258 do {
3259 /* Paranoia. */
3260 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3261 bidi_move_to_visually_next (&it->bidi_it);
3262 } while (it->bidi_it.charpos != 0);
3263 }
3264 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3265 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3266 }
3267 }
3268
3269 if (CHARPOS (pos->string_pos) >= 0)
3270 {
3271 /* Recorded position is not in an overlay string, but in another
3272 string. This can only be a string from a `display' property.
3273 IT should already be filled with that string. */
3274 it->current.string_pos = pos->string_pos;
3275 eassert (STRINGP (it->string));
3276 if (it->bidi_p)
3277 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3278 FRAME_WINDOW_P (it->f), &it->bidi_it);
3279 }
3280
3281 /* Restore position in display vector translations, control
3282 character translations or ellipses. */
3283 if (pos->dpvec_index >= 0)
3284 {
3285 if (it->dpvec == NULL)
3286 get_next_display_element (it);
3287 eassert (it->dpvec && it->current.dpvec_index == 0);
3288 it->current.dpvec_index = pos->dpvec_index;
3289 }
3290
3291 CHECK_IT (it);
3292 return !overlay_strings_with_newlines;
3293 }
3294
3295
3296 /* Initialize IT for stepping through current_buffer in window W
3297 starting at ROW->start. */
3298
3299 static void
3300 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3301 {
3302 init_from_display_pos (it, w, &row->start);
3303 it->start = row->start;
3304 it->continuation_lines_width = row->continuation_lines_width;
3305 CHECK_IT (it);
3306 }
3307
3308
3309 /* Initialize IT for stepping through current_buffer in window W
3310 starting in the line following ROW, i.e. starting at ROW->end.
3311 Value is zero if there are overlay strings with newlines at ROW's
3312 end position. */
3313
3314 static int
3315 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3316 {
3317 int success = 0;
3318
3319 if (init_from_display_pos (it, w, &row->end))
3320 {
3321 if (row->continued_p)
3322 it->continuation_lines_width
3323 = row->continuation_lines_width + row->pixel_width;
3324 CHECK_IT (it);
3325 success = 1;
3326 }
3327
3328 return success;
3329 }
3330
3331
3332
3333 \f
3334 /***********************************************************************
3335 Text properties
3336 ***********************************************************************/
3337
3338 /* Called when IT reaches IT->stop_charpos. Handle text property and
3339 overlay changes. Set IT->stop_charpos to the next position where
3340 to stop. */
3341
3342 static void
3343 handle_stop (struct it *it)
3344 {
3345 enum prop_handled handled;
3346 int handle_overlay_change_p;
3347 struct props *p;
3348
3349 it->dpvec = NULL;
3350 it->current.dpvec_index = -1;
3351 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3352 it->ignore_overlay_strings_at_pos_p = 0;
3353 it->ellipsis_p = 0;
3354
3355 /* Use face of preceding text for ellipsis (if invisible) */
3356 if (it->selective_display_ellipsis_p)
3357 it->saved_face_id = it->face_id;
3358
3359 do
3360 {
3361 handled = HANDLED_NORMALLY;
3362
3363 /* Call text property handlers. */
3364 for (p = it_props; p->handler; ++p)
3365 {
3366 handled = p->handler (it);
3367
3368 if (handled == HANDLED_RECOMPUTE_PROPS)
3369 break;
3370 else if (handled == HANDLED_RETURN)
3371 {
3372 /* We still want to show before and after strings from
3373 overlays even if the actual buffer text is replaced. */
3374 if (!handle_overlay_change_p
3375 || it->sp > 1
3376 /* Don't call get_overlay_strings_1 if we already
3377 have overlay strings loaded, because doing so
3378 will load them again and push the iterator state
3379 onto the stack one more time, which is not
3380 expected by the rest of the code that processes
3381 overlay strings. */
3382 || (it->current.overlay_string_index < 0
3383 ? !get_overlay_strings_1 (it, 0, 0)
3384 : 0))
3385 {
3386 if (it->ellipsis_p)
3387 setup_for_ellipsis (it, 0);
3388 /* When handling a display spec, we might load an
3389 empty string. In that case, discard it here. We
3390 used to discard it in handle_single_display_spec,
3391 but that causes get_overlay_strings_1, above, to
3392 ignore overlay strings that we must check. */
3393 if (STRINGP (it->string) && !SCHARS (it->string))
3394 pop_it (it);
3395 return;
3396 }
3397 else if (STRINGP (it->string) && !SCHARS (it->string))
3398 pop_it (it);
3399 else
3400 {
3401 it->ignore_overlay_strings_at_pos_p = true;
3402 it->string_from_display_prop_p = 0;
3403 it->from_disp_prop_p = 0;
3404 handle_overlay_change_p = 0;
3405 }
3406 handled = HANDLED_RECOMPUTE_PROPS;
3407 break;
3408 }
3409 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3410 handle_overlay_change_p = 0;
3411 }
3412
3413 if (handled != HANDLED_RECOMPUTE_PROPS)
3414 {
3415 /* Don't check for overlay strings below when set to deliver
3416 characters from a display vector. */
3417 if (it->method == GET_FROM_DISPLAY_VECTOR)
3418 handle_overlay_change_p = 0;
3419
3420 /* Handle overlay changes.
3421 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3422 if it finds overlays. */
3423 if (handle_overlay_change_p)
3424 handled = handle_overlay_change (it);
3425 }
3426
3427 if (it->ellipsis_p)
3428 {
3429 setup_for_ellipsis (it, 0);
3430 break;
3431 }
3432 }
3433 while (handled == HANDLED_RECOMPUTE_PROPS);
3434
3435 /* Determine where to stop next. */
3436 if (handled == HANDLED_NORMALLY)
3437 compute_stop_pos (it);
3438 }
3439
3440
3441 /* Compute IT->stop_charpos from text property and overlay change
3442 information for IT's current position. */
3443
3444 static void
3445 compute_stop_pos (struct it *it)
3446 {
3447 register INTERVAL iv, next_iv;
3448 Lisp_Object object, limit, position;
3449 ptrdiff_t charpos, bytepos;
3450
3451 if (STRINGP (it->string))
3452 {
3453 /* Strings are usually short, so don't limit the search for
3454 properties. */
3455 it->stop_charpos = it->end_charpos;
3456 object = it->string;
3457 limit = Qnil;
3458 charpos = IT_STRING_CHARPOS (*it);
3459 bytepos = IT_STRING_BYTEPOS (*it);
3460 }
3461 else
3462 {
3463 ptrdiff_t pos;
3464
3465 /* If end_charpos is out of range for some reason, such as a
3466 misbehaving display function, rationalize it (Bug#5984). */
3467 if (it->end_charpos > ZV)
3468 it->end_charpos = ZV;
3469 it->stop_charpos = it->end_charpos;
3470
3471 /* If next overlay change is in front of the current stop pos
3472 (which is IT->end_charpos), stop there. Note: value of
3473 next_overlay_change is point-max if no overlay change
3474 follows. */
3475 charpos = IT_CHARPOS (*it);
3476 bytepos = IT_BYTEPOS (*it);
3477 pos = next_overlay_change (charpos);
3478 if (pos < it->stop_charpos)
3479 it->stop_charpos = pos;
3480
3481 /* Set up variables for computing the stop position from text
3482 property changes. */
3483 XSETBUFFER (object, current_buffer);
3484 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3485 }
3486
3487 /* Get the interval containing IT's position. Value is a null
3488 interval if there isn't such an interval. */
3489 position = make_number (charpos);
3490 iv = validate_interval_range (object, &position, &position, 0);
3491 if (iv)
3492 {
3493 Lisp_Object values_here[LAST_PROP_IDX];
3494 struct props *p;
3495
3496 /* Get properties here. */
3497 for (p = it_props; p->handler; ++p)
3498 values_here[p->idx] = textget (iv->plist, *p->name);
3499
3500 /* Look for an interval following iv that has different
3501 properties. */
3502 for (next_iv = next_interval (iv);
3503 (next_iv
3504 && (NILP (limit)
3505 || XFASTINT (limit) > next_iv->position));
3506 next_iv = next_interval (next_iv))
3507 {
3508 for (p = it_props; p->handler; ++p)
3509 {
3510 Lisp_Object new_value;
3511
3512 new_value = textget (next_iv->plist, *p->name);
3513 if (!EQ (values_here[p->idx], new_value))
3514 break;
3515 }
3516
3517 if (p->handler)
3518 break;
3519 }
3520
3521 if (next_iv)
3522 {
3523 if (INTEGERP (limit)
3524 && next_iv->position >= XFASTINT (limit))
3525 /* No text property change up to limit. */
3526 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3527 else
3528 /* Text properties change in next_iv. */
3529 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3530 }
3531 }
3532
3533 if (it->cmp_it.id < 0)
3534 {
3535 ptrdiff_t stoppos = it->end_charpos;
3536
3537 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3538 stoppos = -1;
3539 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3540 stoppos, it->string);
3541 }
3542
3543 eassert (STRINGP (it->string)
3544 || (it->stop_charpos >= BEGV
3545 && it->stop_charpos >= IT_CHARPOS (*it)));
3546 }
3547
3548
3549 /* Return the position of the next overlay change after POS in
3550 current_buffer. Value is point-max if no overlay change
3551 follows. This is like `next-overlay-change' but doesn't use
3552 xmalloc. */
3553
3554 static ptrdiff_t
3555 next_overlay_change (ptrdiff_t pos)
3556 {
3557 ptrdiff_t i, noverlays;
3558 ptrdiff_t endpos;
3559 Lisp_Object *overlays;
3560
3561 /* Get all overlays at the given position. */
3562 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3563
3564 /* If any of these overlays ends before endpos,
3565 use its ending point instead. */
3566 for (i = 0; i < noverlays; ++i)
3567 {
3568 Lisp_Object oend;
3569 ptrdiff_t oendpos;
3570
3571 oend = OVERLAY_END (overlays[i]);
3572 oendpos = OVERLAY_POSITION (oend);
3573 endpos = min (endpos, oendpos);
3574 }
3575
3576 return endpos;
3577 }
3578
3579 /* How many characters forward to search for a display property or
3580 display string. Searching too far forward makes the bidi display
3581 sluggish, especially in small windows. */
3582 #define MAX_DISP_SCAN 250
3583
3584 /* Return the character position of a display string at or after
3585 position specified by POSITION. If no display string exists at or
3586 after POSITION, return ZV. A display string is either an overlay
3587 with `display' property whose value is a string, or a `display'
3588 text property whose value is a string. STRING is data about the
3589 string to iterate; if STRING->lstring is nil, we are iterating a
3590 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3591 on a GUI frame. DISP_PROP is set to zero if we searched
3592 MAX_DISP_SCAN characters forward without finding any display
3593 strings, non-zero otherwise. It is set to 2 if the display string
3594 uses any kind of `(space ...)' spec that will produce a stretch of
3595 white space in the text area. */
3596 ptrdiff_t
3597 compute_display_string_pos (struct text_pos *position,
3598 struct bidi_string_data *string,
3599 struct window *w,
3600 int frame_window_p, int *disp_prop)
3601 {
3602 /* OBJECT = nil means current buffer. */
3603 Lisp_Object object, object1;
3604 Lisp_Object pos, spec, limpos;
3605 int string_p = (string && (STRINGP (string->lstring) || string->s));
3606 ptrdiff_t eob = string_p ? string->schars : ZV;
3607 ptrdiff_t begb = string_p ? 0 : BEGV;
3608 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3609 ptrdiff_t lim =
3610 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3611 struct text_pos tpos;
3612 int rv = 0;
3613
3614 if (string && STRINGP (string->lstring))
3615 object1 = object = string->lstring;
3616 else if (w && !string_p)
3617 {
3618 XSETWINDOW (object, w);
3619 object1 = Qnil;
3620 }
3621 else
3622 object1 = object = Qnil;
3623
3624 *disp_prop = 1;
3625
3626 if (charpos >= eob
3627 /* We don't support display properties whose values are strings
3628 that have display string properties. */
3629 || string->from_disp_str
3630 /* C strings cannot have display properties. */
3631 || (string->s && !STRINGP (object)))
3632 {
3633 *disp_prop = 0;
3634 return eob;
3635 }
3636
3637 /* If the character at CHARPOS is where the display string begins,
3638 return CHARPOS. */
3639 pos = make_number (charpos);
3640 if (STRINGP (object))
3641 bufpos = string->bufpos;
3642 else
3643 bufpos = charpos;
3644 tpos = *position;
3645 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3646 && (charpos <= begb
3647 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3648 object),
3649 spec))
3650 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3651 frame_window_p)))
3652 {
3653 if (rv == 2)
3654 *disp_prop = 2;
3655 return charpos;
3656 }
3657
3658 /* Look forward for the first character with a `display' property
3659 that will replace the underlying text when displayed. */
3660 limpos = make_number (lim);
3661 do {
3662 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3663 CHARPOS (tpos) = XFASTINT (pos);
3664 if (CHARPOS (tpos) >= lim)
3665 {
3666 *disp_prop = 0;
3667 break;
3668 }
3669 if (STRINGP (object))
3670 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3671 else
3672 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3673 spec = Fget_char_property (pos, Qdisplay, object);
3674 if (!STRINGP (object))
3675 bufpos = CHARPOS (tpos);
3676 } while (NILP (spec)
3677 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3678 bufpos, frame_window_p)));
3679 if (rv == 2)
3680 *disp_prop = 2;
3681
3682 return CHARPOS (tpos);
3683 }
3684
3685 /* Return the character position of the end of the display string that
3686 started at CHARPOS. If there's no display string at CHARPOS,
3687 return -1. A display string is either an overlay with `display'
3688 property whose value is a string or a `display' text property whose
3689 value is a string. */
3690 ptrdiff_t
3691 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3692 {
3693 /* OBJECT = nil means current buffer. */
3694 Lisp_Object object =
3695 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3696 Lisp_Object pos = make_number (charpos);
3697 ptrdiff_t eob =
3698 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3699
3700 if (charpos >= eob || (string->s && !STRINGP (object)))
3701 return eob;
3702
3703 /* It could happen that the display property or overlay was removed
3704 since we found it in compute_display_string_pos above. One way
3705 this can happen is if JIT font-lock was called (through
3706 handle_fontified_prop), and jit-lock-functions remove text
3707 properties or overlays from the portion of buffer that includes
3708 CHARPOS. Muse mode is known to do that, for example. In this
3709 case, we return -1 to the caller, to signal that no display
3710 string is actually present at CHARPOS. See bidi_fetch_char for
3711 how this is handled.
3712
3713 An alternative would be to never look for display properties past
3714 it->stop_charpos. But neither compute_display_string_pos nor
3715 bidi_fetch_char that calls it know or care where the next
3716 stop_charpos is. */
3717 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3718 return -1;
3719
3720 /* Look forward for the first character where the `display' property
3721 changes. */
3722 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3723
3724 return XFASTINT (pos);
3725 }
3726
3727
3728 \f
3729 /***********************************************************************
3730 Fontification
3731 ***********************************************************************/
3732
3733 /* Handle changes in the `fontified' property of the current buffer by
3734 calling hook functions from Qfontification_functions to fontify
3735 regions of text. */
3736
3737 static enum prop_handled
3738 handle_fontified_prop (struct it *it)
3739 {
3740 Lisp_Object prop, pos;
3741 enum prop_handled handled = HANDLED_NORMALLY;
3742
3743 if (!NILP (Vmemory_full))
3744 return handled;
3745
3746 /* Get the value of the `fontified' property at IT's current buffer
3747 position. (The `fontified' property doesn't have a special
3748 meaning in strings.) If the value is nil, call functions from
3749 Qfontification_functions. */
3750 if (!STRINGP (it->string)
3751 && it->s == NULL
3752 && !NILP (Vfontification_functions)
3753 && !NILP (Vrun_hooks)
3754 && (pos = make_number (IT_CHARPOS (*it)),
3755 prop = Fget_char_property (pos, Qfontified, Qnil),
3756 /* Ignore the special cased nil value always present at EOB since
3757 no amount of fontifying will be able to change it. */
3758 NILP (prop) && IT_CHARPOS (*it) < Z))
3759 {
3760 ptrdiff_t count = SPECPDL_INDEX ();
3761 Lisp_Object val;
3762 struct buffer *obuf = current_buffer;
3763 ptrdiff_t begv = BEGV, zv = ZV;
3764 bool old_clip_changed = current_buffer->clip_changed;
3765
3766 val = Vfontification_functions;
3767 specbind (Qfontification_functions, Qnil);
3768
3769 eassert (it->end_charpos == ZV);
3770
3771 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3772 safe_call1 (val, pos);
3773 else
3774 {
3775 Lisp_Object fns, fn;
3776 struct gcpro gcpro1, gcpro2;
3777
3778 fns = Qnil;
3779 GCPRO2 (val, fns);
3780
3781 for (; CONSP (val); val = XCDR (val))
3782 {
3783 fn = XCAR (val);
3784
3785 if (EQ (fn, Qt))
3786 {
3787 /* A value of t indicates this hook has a local
3788 binding; it means to run the global binding too.
3789 In a global value, t should not occur. If it
3790 does, we must ignore it to avoid an endless
3791 loop. */
3792 for (fns = Fdefault_value (Qfontification_functions);
3793 CONSP (fns);
3794 fns = XCDR (fns))
3795 {
3796 fn = XCAR (fns);
3797 if (!EQ (fn, Qt))
3798 safe_call1 (fn, pos);
3799 }
3800 }
3801 else
3802 safe_call1 (fn, pos);
3803 }
3804
3805 UNGCPRO;
3806 }
3807
3808 unbind_to (count, Qnil);
3809
3810 /* Fontification functions routinely call `save-restriction'.
3811 Normally, this tags clip_changed, which can confuse redisplay
3812 (see discussion in Bug#6671). Since we don't perform any
3813 special handling of fontification changes in the case where
3814 `save-restriction' isn't called, there's no point doing so in
3815 this case either. So, if the buffer's restrictions are
3816 actually left unchanged, reset clip_changed. */
3817 if (obuf == current_buffer)
3818 {
3819 if (begv == BEGV && zv == ZV)
3820 current_buffer->clip_changed = old_clip_changed;
3821 }
3822 /* There isn't much we can reasonably do to protect against
3823 misbehaving fontification, but here's a fig leaf. */
3824 else if (BUFFER_LIVE_P (obuf))
3825 set_buffer_internal_1 (obuf);
3826
3827 /* The fontification code may have added/removed text.
3828 It could do even a lot worse, but let's at least protect against
3829 the most obvious case where only the text past `pos' gets changed',
3830 as is/was done in grep.el where some escapes sequences are turned
3831 into face properties (bug#7876). */
3832 it->end_charpos = ZV;
3833
3834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3835 something. This avoids an endless loop if they failed to
3836 fontify the text for which reason ever. */
3837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3838 handled = HANDLED_RECOMPUTE_PROPS;
3839 }
3840
3841 return handled;
3842 }
3843
3844
3845 \f
3846 /***********************************************************************
3847 Faces
3848 ***********************************************************************/
3849
3850 /* Set up iterator IT from face properties at its current position.
3851 Called from handle_stop. */
3852
3853 static enum prop_handled
3854 handle_face_prop (struct it *it)
3855 {
3856 int new_face_id;
3857 ptrdiff_t next_stop;
3858
3859 if (!STRINGP (it->string))
3860 {
3861 new_face_id
3862 = face_at_buffer_position (it->w,
3863 IT_CHARPOS (*it),
3864 &next_stop,
3865 (IT_CHARPOS (*it)
3866 + TEXT_PROP_DISTANCE_LIMIT),
3867 0, it->base_face_id);
3868
3869 /* Is this a start of a run of characters with box face?
3870 Caveat: this can be called for a freshly initialized
3871 iterator; face_id is -1 in this case. We know that the new
3872 face will not change until limit, i.e. if the new face has a
3873 box, all characters up to limit will have one. But, as
3874 usual, we don't know whether limit is really the end. */
3875 if (new_face_id != it->face_id)
3876 {
3877 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3878 /* If it->face_id is -1, old_face below will be NULL, see
3879 the definition of FACE_FROM_ID. This will happen if this
3880 is the initial call that gets the face. */
3881 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3882
3883 /* If the value of face_id of the iterator is -1, we have to
3884 look in front of IT's position and see whether there is a
3885 face there that's different from new_face_id. */
3886 if (!old_face && IT_CHARPOS (*it) > BEG)
3887 {
3888 int prev_face_id = face_before_it_pos (it);
3889
3890 old_face = FACE_FROM_ID (it->f, prev_face_id);
3891 }
3892
3893 /* If the new face has a box, but the old face does not,
3894 this is the start of a run of characters with box face,
3895 i.e. this character has a shadow on the left side. */
3896 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3897 && (old_face == NULL || !old_face->box));
3898 it->face_box_p = new_face->box != FACE_NO_BOX;
3899 }
3900 }
3901 else
3902 {
3903 int base_face_id;
3904 ptrdiff_t bufpos;
3905 int i;
3906 Lisp_Object from_overlay
3907 = (it->current.overlay_string_index >= 0
3908 ? it->string_overlays[it->current.overlay_string_index
3909 % OVERLAY_STRING_CHUNK_SIZE]
3910 : Qnil);
3911
3912 /* See if we got to this string directly or indirectly from
3913 an overlay property. That includes the before-string or
3914 after-string of an overlay, strings in display properties
3915 provided by an overlay, their text properties, etc.
3916
3917 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3918 if (! NILP (from_overlay))
3919 for (i = it->sp - 1; i >= 0; i--)
3920 {
3921 if (it->stack[i].current.overlay_string_index >= 0)
3922 from_overlay
3923 = it->string_overlays[it->stack[i].current.overlay_string_index
3924 % OVERLAY_STRING_CHUNK_SIZE];
3925 else if (! NILP (it->stack[i].from_overlay))
3926 from_overlay = it->stack[i].from_overlay;
3927
3928 if (!NILP (from_overlay))
3929 break;
3930 }
3931
3932 if (! NILP (from_overlay))
3933 {
3934 bufpos = IT_CHARPOS (*it);
3935 /* For a string from an overlay, the base face depends
3936 only on text properties and ignores overlays. */
3937 base_face_id
3938 = face_for_overlay_string (it->w,
3939 IT_CHARPOS (*it),
3940 &next_stop,
3941 (IT_CHARPOS (*it)
3942 + TEXT_PROP_DISTANCE_LIMIT),
3943 0,
3944 from_overlay);
3945 }
3946 else
3947 {
3948 bufpos = 0;
3949
3950 /* For strings from a `display' property, use the face at
3951 IT's current buffer position as the base face to merge
3952 with, so that overlay strings appear in the same face as
3953 surrounding text, unless they specify their own faces.
3954 For strings from wrap-prefix and line-prefix properties,
3955 use the default face, possibly remapped via
3956 Vface_remapping_alist. */
3957 /* Note that the fact that we use the face at _buffer_
3958 position means that a 'display' property on an overlay
3959 string will not inherit the face of that overlay string,
3960 but will instead revert to the face of buffer text
3961 covered by the overlay. This is visible, e.g., when the
3962 overlay specifies a box face, but neither the buffer nor
3963 the display string do. This sounds like a design bug,
3964 but Emacs always did that since v21.1, so changing that
3965 might be a big deal. */
3966 base_face_id = it->string_from_prefix_prop_p
3967 ? (!NILP (Vface_remapping_alist)
3968 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3969 : DEFAULT_FACE_ID)
3970 : underlying_face_id (it);
3971 }
3972
3973 new_face_id = face_at_string_position (it->w,
3974 it->string,
3975 IT_STRING_CHARPOS (*it),
3976 bufpos,
3977 &next_stop,
3978 base_face_id, 0);
3979
3980 /* Is this a start of a run of characters with box? Caveat:
3981 this can be called for a freshly allocated iterator; face_id
3982 is -1 is this case. We know that the new face will not
3983 change until the next check pos, i.e. if the new face has a
3984 box, all characters up to that position will have a
3985 box. But, as usual, we don't know whether that position
3986 is really the end. */
3987 if (new_face_id != it->face_id)
3988 {
3989 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3990 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3991
3992 /* If new face has a box but old face hasn't, this is the
3993 start of a run of characters with box, i.e. it has a
3994 shadow on the left side. */
3995 it->start_of_box_run_p
3996 = new_face->box && (old_face == NULL || !old_face->box);
3997 it->face_box_p = new_face->box != FACE_NO_BOX;
3998 }
3999 }
4000
4001 it->face_id = new_face_id;
4002 return HANDLED_NORMALLY;
4003 }
4004
4005
4006 /* Return the ID of the face ``underlying'' IT's current position,
4007 which is in a string. If the iterator is associated with a
4008 buffer, return the face at IT's current buffer position.
4009 Otherwise, use the iterator's base_face_id. */
4010
4011 static int
4012 underlying_face_id (struct it *it)
4013 {
4014 int face_id = it->base_face_id, i;
4015
4016 eassert (STRINGP (it->string));
4017
4018 for (i = it->sp - 1; i >= 0; --i)
4019 if (NILP (it->stack[i].string))
4020 face_id = it->stack[i].face_id;
4021
4022 return face_id;
4023 }
4024
4025
4026 /* Compute the face one character before or after the current position
4027 of IT, in the visual order. BEFORE_P non-zero means get the face
4028 in front (to the left in L2R paragraphs, to the right in R2L
4029 paragraphs) of IT's screen position. Value is the ID of the face. */
4030
4031 static int
4032 face_before_or_after_it_pos (struct it *it, int before_p)
4033 {
4034 int face_id, limit;
4035 ptrdiff_t next_check_charpos;
4036 struct it it_copy;
4037 void *it_copy_data = NULL;
4038
4039 eassert (it->s == NULL);
4040
4041 if (STRINGP (it->string))
4042 {
4043 ptrdiff_t bufpos, charpos;
4044 int base_face_id;
4045
4046 /* No face change past the end of the string (for the case
4047 we are padding with spaces). No face change before the
4048 string start. */
4049 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4050 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4051 return it->face_id;
4052
4053 if (!it->bidi_p)
4054 {
4055 /* Set charpos to the position before or after IT's current
4056 position, in the logical order, which in the non-bidi
4057 case is the same as the visual order. */
4058 if (before_p)
4059 charpos = IT_STRING_CHARPOS (*it) - 1;
4060 else if (it->what == IT_COMPOSITION)
4061 /* For composition, we must check the character after the
4062 composition. */
4063 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4064 else
4065 charpos = IT_STRING_CHARPOS (*it) + 1;
4066 }
4067 else
4068 {
4069 if (before_p)
4070 {
4071 /* With bidi iteration, the character before the current
4072 in the visual order cannot be found by simple
4073 iteration, because "reverse" reordering is not
4074 supported. Instead, we need to use the move_it_*
4075 family of functions. */
4076 /* Ignore face changes before the first visible
4077 character on this display line. */
4078 if (it->current_x <= it->first_visible_x)
4079 return it->face_id;
4080 SAVE_IT (it_copy, *it, it_copy_data);
4081 /* Implementation note: Since move_it_in_display_line
4082 works in the iterator geometry, and thinks the first
4083 character is always the leftmost, even in R2L lines,
4084 we don't need to distinguish between the R2L and L2R
4085 cases here. */
4086 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4087 it_copy.current_x - 1, MOVE_TO_X);
4088 charpos = IT_STRING_CHARPOS (it_copy);
4089 RESTORE_IT (it, it, it_copy_data);
4090 }
4091 else
4092 {
4093 /* Set charpos to the string position of the character
4094 that comes after IT's current position in the visual
4095 order. */
4096 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4097
4098 it_copy = *it;
4099 while (n--)
4100 bidi_move_to_visually_next (&it_copy.bidi_it);
4101
4102 charpos = it_copy.bidi_it.charpos;
4103 }
4104 }
4105 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4106
4107 if (it->current.overlay_string_index >= 0)
4108 bufpos = IT_CHARPOS (*it);
4109 else
4110 bufpos = 0;
4111
4112 base_face_id = underlying_face_id (it);
4113
4114 /* Get the face for ASCII, or unibyte. */
4115 face_id = face_at_string_position (it->w,
4116 it->string,
4117 charpos,
4118 bufpos,
4119 &next_check_charpos,
4120 base_face_id, 0);
4121
4122 /* Correct the face for charsets different from ASCII. Do it
4123 for the multibyte case only. The face returned above is
4124 suitable for unibyte text if IT->string is unibyte. */
4125 if (STRING_MULTIBYTE (it->string))
4126 {
4127 struct text_pos pos1 = string_pos (charpos, it->string);
4128 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4129 int c, len;
4130 struct face *face = FACE_FROM_ID (it->f, face_id);
4131
4132 c = string_char_and_length (p, &len);
4133 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4134 }
4135 }
4136 else
4137 {
4138 struct text_pos pos;
4139
4140 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4141 || (IT_CHARPOS (*it) <= BEGV && before_p))
4142 return it->face_id;
4143
4144 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4145 pos = it->current.pos;
4146
4147 if (!it->bidi_p)
4148 {
4149 if (before_p)
4150 DEC_TEXT_POS (pos, it->multibyte_p);
4151 else
4152 {
4153 if (it->what == IT_COMPOSITION)
4154 {
4155 /* For composition, we must check the position after
4156 the composition. */
4157 pos.charpos += it->cmp_it.nchars;
4158 pos.bytepos += it->len;
4159 }
4160 else
4161 INC_TEXT_POS (pos, it->multibyte_p);
4162 }
4163 }
4164 else
4165 {
4166 if (before_p)
4167 {
4168 /* With bidi iteration, the character before the current
4169 in the visual order cannot be found by simple
4170 iteration, because "reverse" reordering is not
4171 supported. Instead, we need to use the move_it_*
4172 family of functions. */
4173 /* Ignore face changes before the first visible
4174 character on this display line. */
4175 if (it->current_x <= it->first_visible_x)
4176 return it->face_id;
4177 SAVE_IT (it_copy, *it, it_copy_data);
4178 /* Implementation note: Since move_it_in_display_line
4179 works in the iterator geometry, and thinks the first
4180 character is always the leftmost, even in R2L lines,
4181 we don't need to distinguish between the R2L and L2R
4182 cases here. */
4183 move_it_in_display_line (&it_copy, ZV,
4184 it_copy.current_x - 1, MOVE_TO_X);
4185 pos = it_copy.current.pos;
4186 RESTORE_IT (it, it, it_copy_data);
4187 }
4188 else
4189 {
4190 /* Set charpos to the buffer position of the character
4191 that comes after IT's current position in the visual
4192 order. */
4193 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4194
4195 it_copy = *it;
4196 while (n--)
4197 bidi_move_to_visually_next (&it_copy.bidi_it);
4198
4199 SET_TEXT_POS (pos,
4200 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4201 }
4202 }
4203 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4204
4205 /* Determine face for CHARSET_ASCII, or unibyte. */
4206 face_id = face_at_buffer_position (it->w,
4207 CHARPOS (pos),
4208 &next_check_charpos,
4209 limit, 0, -1);
4210
4211 /* Correct the face for charsets different from ASCII. Do it
4212 for the multibyte case only. The face returned above is
4213 suitable for unibyte text if current_buffer is unibyte. */
4214 if (it->multibyte_p)
4215 {
4216 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4217 struct face *face = FACE_FROM_ID (it->f, face_id);
4218 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4219 }
4220 }
4221
4222 return face_id;
4223 }
4224
4225
4226 \f
4227 /***********************************************************************
4228 Invisible text
4229 ***********************************************************************/
4230
4231 /* Set up iterator IT from invisible properties at its current
4232 position. Called from handle_stop. */
4233
4234 static enum prop_handled
4235 handle_invisible_prop (struct it *it)
4236 {
4237 enum prop_handled handled = HANDLED_NORMALLY;
4238 int invis_p;
4239 Lisp_Object prop;
4240
4241 if (STRINGP (it->string))
4242 {
4243 Lisp_Object end_charpos, limit, charpos;
4244
4245 /* Get the value of the invisible text property at the
4246 current position. Value will be nil if there is no such
4247 property. */
4248 charpos = make_number (IT_STRING_CHARPOS (*it));
4249 prop = Fget_text_property (charpos, Qinvisible, it->string);
4250 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4251
4252 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4253 {
4254 /* Record whether we have to display an ellipsis for the
4255 invisible text. */
4256 int display_ellipsis_p = (invis_p == 2);
4257 ptrdiff_t len, endpos;
4258
4259 handled = HANDLED_RECOMPUTE_PROPS;
4260
4261 /* Get the position at which the next visible text can be
4262 found in IT->string, if any. */
4263 endpos = len = SCHARS (it->string);
4264 XSETINT (limit, len);
4265 do
4266 {
4267 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4268 it->string, limit);
4269 if (INTEGERP (end_charpos))
4270 {
4271 endpos = XFASTINT (end_charpos);
4272 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4273 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4274 if (invis_p == 2)
4275 display_ellipsis_p = true;
4276 }
4277 }
4278 while (invis_p && endpos < len);
4279
4280 if (display_ellipsis_p)
4281 it->ellipsis_p = true;
4282
4283 if (endpos < len)
4284 {
4285 /* Text at END_CHARPOS is visible. Move IT there. */
4286 struct text_pos old;
4287 ptrdiff_t oldpos;
4288
4289 old = it->current.string_pos;
4290 oldpos = CHARPOS (old);
4291 if (it->bidi_p)
4292 {
4293 if (it->bidi_it.first_elt
4294 && it->bidi_it.charpos < SCHARS (it->string))
4295 bidi_paragraph_init (it->paragraph_embedding,
4296 &it->bidi_it, 1);
4297 /* Bidi-iterate out of the invisible text. */
4298 do
4299 {
4300 bidi_move_to_visually_next (&it->bidi_it);
4301 }
4302 while (oldpos <= it->bidi_it.charpos
4303 && it->bidi_it.charpos < endpos);
4304
4305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4306 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4307 if (IT_CHARPOS (*it) >= endpos)
4308 it->prev_stop = endpos;
4309 }
4310 else
4311 {
4312 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4313 compute_string_pos (&it->current.string_pos, old, it->string);
4314 }
4315 }
4316 else
4317 {
4318 /* The rest of the string is invisible. If this is an
4319 overlay string, proceed with the next overlay string
4320 or whatever comes and return a character from there. */
4321 if (it->current.overlay_string_index >= 0
4322 && !display_ellipsis_p)
4323 {
4324 next_overlay_string (it);
4325 /* Don't check for overlay strings when we just
4326 finished processing them. */
4327 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4328 }
4329 else
4330 {
4331 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4332 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4333 }
4334 }
4335 }
4336 }
4337 else
4338 {
4339 ptrdiff_t newpos, next_stop, start_charpos, tem;
4340 Lisp_Object pos, overlay;
4341
4342 /* First of all, is there invisible text at this position? */
4343 tem = start_charpos = IT_CHARPOS (*it);
4344 pos = make_number (tem);
4345 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4346 &overlay);
4347 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4348
4349 /* If we are on invisible text, skip over it. */
4350 if (invis_p && start_charpos < it->end_charpos)
4351 {
4352 /* Record whether we have to display an ellipsis for the
4353 invisible text. */
4354 int display_ellipsis_p = invis_p == 2;
4355
4356 handled = HANDLED_RECOMPUTE_PROPS;
4357
4358 /* Loop skipping over invisible text. The loop is left at
4359 ZV or with IT on the first char being visible again. */
4360 do
4361 {
4362 /* Try to skip some invisible text. Return value is the
4363 position reached which can be equal to where we start
4364 if there is nothing invisible there. This skips both
4365 over invisible text properties and overlays with
4366 invisible property. */
4367 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4368
4369 /* If we skipped nothing at all we weren't at invisible
4370 text in the first place. If everything to the end of
4371 the buffer was skipped, end the loop. */
4372 if (newpos == tem || newpos >= ZV)
4373 invis_p = 0;
4374 else
4375 {
4376 /* We skipped some characters but not necessarily
4377 all there are. Check if we ended up on visible
4378 text. Fget_char_property returns the property of
4379 the char before the given position, i.e. if we
4380 get invis_p = 0, this means that the char at
4381 newpos is visible. */
4382 pos = make_number (newpos);
4383 prop = Fget_char_property (pos, Qinvisible, it->window);
4384 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4385 }
4386
4387 /* If we ended up on invisible text, proceed to
4388 skip starting with next_stop. */
4389 if (invis_p)
4390 tem = next_stop;
4391
4392 /* If there are adjacent invisible texts, don't lose the
4393 second one's ellipsis. */
4394 if (invis_p == 2)
4395 display_ellipsis_p = true;
4396 }
4397 while (invis_p);
4398
4399 /* The position newpos is now either ZV or on visible text. */
4400 if (it->bidi_p)
4401 {
4402 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4403 int on_newline
4404 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4405 int after_newline
4406 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4407
4408 /* If the invisible text ends on a newline or on a
4409 character after a newline, we can avoid the costly,
4410 character by character, bidi iteration to NEWPOS, and
4411 instead simply reseat the iterator there. That's
4412 because all bidi reordering information is tossed at
4413 the newline. This is a big win for modes that hide
4414 complete lines, like Outline, Org, etc. */
4415 if (on_newline || after_newline)
4416 {
4417 struct text_pos tpos;
4418 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4419
4420 SET_TEXT_POS (tpos, newpos, bpos);
4421 reseat_1 (it, tpos, 0);
4422 /* If we reseat on a newline/ZV, we need to prep the
4423 bidi iterator for advancing to the next character
4424 after the newline/EOB, keeping the current paragraph
4425 direction (so that PRODUCE_GLYPHS does TRT wrt
4426 prepending/appending glyphs to a glyph row). */
4427 if (on_newline)
4428 {
4429 it->bidi_it.first_elt = 0;
4430 it->bidi_it.paragraph_dir = pdir;
4431 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4432 it->bidi_it.nchars = 1;
4433 it->bidi_it.ch_len = 1;
4434 }
4435 }
4436 else /* Must use the slow method. */
4437 {
4438 /* With bidi iteration, the region of invisible text
4439 could start and/or end in the middle of a
4440 non-base embedding level. Therefore, we need to
4441 skip invisible text using the bidi iterator,
4442 starting at IT's current position, until we find
4443 ourselves outside of the invisible text.
4444 Skipping invisible text _after_ bidi iteration
4445 avoids affecting the visual order of the
4446 displayed text when invisible properties are
4447 added or removed. */
4448 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4449 {
4450 /* If we were `reseat'ed to a new paragraph,
4451 determine the paragraph base direction. We
4452 need to do it now because
4453 next_element_from_buffer may not have a
4454 chance to do it, if we are going to skip any
4455 text at the beginning, which resets the
4456 FIRST_ELT flag. */
4457 bidi_paragraph_init (it->paragraph_embedding,
4458 &it->bidi_it, 1);
4459 }
4460 do
4461 {
4462 bidi_move_to_visually_next (&it->bidi_it);
4463 }
4464 while (it->stop_charpos <= it->bidi_it.charpos
4465 && it->bidi_it.charpos < newpos);
4466 IT_CHARPOS (*it) = it->bidi_it.charpos;
4467 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4468 /* If we overstepped NEWPOS, record its position in
4469 the iterator, so that we skip invisible text if
4470 later the bidi iteration lands us in the
4471 invisible region again. */
4472 if (IT_CHARPOS (*it) >= newpos)
4473 it->prev_stop = newpos;
4474 }
4475 }
4476 else
4477 {
4478 IT_CHARPOS (*it) = newpos;
4479 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4480 }
4481
4482 /* If there are before-strings at the start of invisible
4483 text, and the text is invisible because of a text
4484 property, arrange to show before-strings because 20.x did
4485 it that way. (If the text is invisible because of an
4486 overlay property instead of a text property, this is
4487 already handled in the overlay code.) */
4488 if (NILP (overlay)
4489 && get_overlay_strings (it, it->stop_charpos))
4490 {
4491 handled = HANDLED_RECOMPUTE_PROPS;
4492 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4493 }
4494 else if (display_ellipsis_p)
4495 {
4496 /* Make sure that the glyphs of the ellipsis will get
4497 correct `charpos' values. If we would not update
4498 it->position here, the glyphs would belong to the
4499 last visible character _before_ the invisible
4500 text, which confuses `set_cursor_from_row'.
4501
4502 We use the last invisible position instead of the
4503 first because this way the cursor is always drawn on
4504 the first "." of the ellipsis, whenever PT is inside
4505 the invisible text. Otherwise the cursor would be
4506 placed _after_ the ellipsis when the point is after the
4507 first invisible character. */
4508 if (!STRINGP (it->object))
4509 {
4510 it->position.charpos = newpos - 1;
4511 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4512 }
4513 it->ellipsis_p = true;
4514 /* Let the ellipsis display before
4515 considering any properties of the following char.
4516 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4517 handled = HANDLED_RETURN;
4518 }
4519 }
4520 }
4521
4522 return handled;
4523 }
4524
4525
4526 /* Make iterator IT return `...' next.
4527 Replaces LEN characters from buffer. */
4528
4529 static void
4530 setup_for_ellipsis (struct it *it, int len)
4531 {
4532 /* Use the display table definition for `...'. Invalid glyphs
4533 will be handled by the method returning elements from dpvec. */
4534 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4535 {
4536 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4537 it->dpvec = v->contents;
4538 it->dpend = v->contents + v->header.size;
4539 }
4540 else
4541 {
4542 /* Default `...'. */
4543 it->dpvec = default_invis_vector;
4544 it->dpend = default_invis_vector + 3;
4545 }
4546
4547 it->dpvec_char_len = len;
4548 it->current.dpvec_index = 0;
4549 it->dpvec_face_id = -1;
4550
4551 /* Remember the current face id in case glyphs specify faces.
4552 IT's face is restored in set_iterator_to_next.
4553 saved_face_id was set to preceding char's face in handle_stop. */
4554 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4555 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4556
4557 it->method = GET_FROM_DISPLAY_VECTOR;
4558 it->ellipsis_p = true;
4559 }
4560
4561
4562 \f
4563 /***********************************************************************
4564 'display' property
4565 ***********************************************************************/
4566
4567 /* Set up iterator IT from `display' property at its current position.
4568 Called from handle_stop.
4569 We return HANDLED_RETURN if some part of the display property
4570 overrides the display of the buffer text itself.
4571 Otherwise we return HANDLED_NORMALLY. */
4572
4573 static enum prop_handled
4574 handle_display_prop (struct it *it)
4575 {
4576 Lisp_Object propval, object, overlay;
4577 struct text_pos *position;
4578 ptrdiff_t bufpos;
4579 /* Nonzero if some property replaces the display of the text itself. */
4580 int display_replaced_p = 0;
4581
4582 if (STRINGP (it->string))
4583 {
4584 object = it->string;
4585 position = &it->current.string_pos;
4586 bufpos = CHARPOS (it->current.pos);
4587 }
4588 else
4589 {
4590 XSETWINDOW (object, it->w);
4591 position = &it->current.pos;
4592 bufpos = CHARPOS (*position);
4593 }
4594
4595 /* Reset those iterator values set from display property values. */
4596 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4597 it->space_width = Qnil;
4598 it->font_height = Qnil;
4599 it->voffset = 0;
4600
4601 /* We don't support recursive `display' properties, i.e. string
4602 values that have a string `display' property, that have a string
4603 `display' property etc. */
4604 if (!it->string_from_display_prop_p)
4605 it->area = TEXT_AREA;
4606
4607 propval = get_char_property_and_overlay (make_number (position->charpos),
4608 Qdisplay, object, &overlay);
4609 if (NILP (propval))
4610 return HANDLED_NORMALLY;
4611 /* Now OVERLAY is the overlay that gave us this property, or nil
4612 if it was a text property. */
4613
4614 if (!STRINGP (it->string))
4615 object = it->w->contents;
4616
4617 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4618 position, bufpos,
4619 FRAME_WINDOW_P (it->f));
4620
4621 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4622 }
4623
4624 /* Subroutine of handle_display_prop. Returns non-zero if the display
4625 specification in SPEC is a replacing specification, i.e. it would
4626 replace the text covered by `display' property with something else,
4627 such as an image or a display string. If SPEC includes any kind or
4628 `(space ...) specification, the value is 2; this is used by
4629 compute_display_string_pos, which see.
4630
4631 See handle_single_display_spec for documentation of arguments.
4632 frame_window_p is non-zero if the window being redisplayed is on a
4633 GUI frame; this argument is used only if IT is NULL, see below.
4634
4635 IT can be NULL, if this is called by the bidi reordering code
4636 through compute_display_string_pos, which see. In that case, this
4637 function only examines SPEC, but does not otherwise "handle" it, in
4638 the sense that it doesn't set up members of IT from the display
4639 spec. */
4640 static int
4641 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4642 Lisp_Object overlay, struct text_pos *position,
4643 ptrdiff_t bufpos, int frame_window_p)
4644 {
4645 int replacing_p = 0;
4646 int rv;
4647
4648 if (CONSP (spec)
4649 /* Simple specifications. */
4650 && !EQ (XCAR (spec), Qimage)
4651 && !EQ (XCAR (spec), Qspace)
4652 && !EQ (XCAR (spec), Qwhen)
4653 && !EQ (XCAR (spec), Qslice)
4654 && !EQ (XCAR (spec), Qspace_width)
4655 && !EQ (XCAR (spec), Qheight)
4656 && !EQ (XCAR (spec), Qraise)
4657 /* Marginal area specifications. */
4658 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4659 && !EQ (XCAR (spec), Qleft_fringe)
4660 && !EQ (XCAR (spec), Qright_fringe)
4661 && !NILP (XCAR (spec)))
4662 {
4663 for (; CONSP (spec); spec = XCDR (spec))
4664 {
4665 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4666 overlay, position, bufpos,
4667 replacing_p, frame_window_p)))
4668 {
4669 replacing_p = rv;
4670 /* If some text in a string is replaced, `position' no
4671 longer points to the position of `object'. */
4672 if (!it || STRINGP (object))
4673 break;
4674 }
4675 }
4676 }
4677 else if (VECTORP (spec))
4678 {
4679 ptrdiff_t i;
4680 for (i = 0; i < ASIZE (spec); ++i)
4681 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4682 overlay, position, bufpos,
4683 replacing_p, frame_window_p)))
4684 {
4685 replacing_p = rv;
4686 /* If some text in a string is replaced, `position' no
4687 longer points to the position of `object'. */
4688 if (!it || STRINGP (object))
4689 break;
4690 }
4691 }
4692 else
4693 {
4694 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4695 position, bufpos, 0,
4696 frame_window_p)))
4697 replacing_p = rv;
4698 }
4699
4700 return replacing_p;
4701 }
4702
4703 /* Value is the position of the end of the `display' property starting
4704 at START_POS in OBJECT. */
4705
4706 static struct text_pos
4707 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4708 {
4709 Lisp_Object end;
4710 struct text_pos end_pos;
4711
4712 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4713 Qdisplay, object, Qnil);
4714 CHARPOS (end_pos) = XFASTINT (end);
4715 if (STRINGP (object))
4716 compute_string_pos (&end_pos, start_pos, it->string);
4717 else
4718 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4719
4720 return end_pos;
4721 }
4722
4723
4724 /* Set up IT from a single `display' property specification SPEC. OBJECT
4725 is the object in which the `display' property was found. *POSITION
4726 is the position in OBJECT at which the `display' property was found.
4727 BUFPOS is the buffer position of OBJECT (different from POSITION if
4728 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4729 previously saw a display specification which already replaced text
4730 display with something else, for example an image; we ignore such
4731 properties after the first one has been processed.
4732
4733 OVERLAY is the overlay this `display' property came from,
4734 or nil if it was a text property.
4735
4736 If SPEC is a `space' or `image' specification, and in some other
4737 cases too, set *POSITION to the position where the `display'
4738 property ends.
4739
4740 If IT is NULL, only examine the property specification in SPEC, but
4741 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4742 is intended to be displayed in a window on a GUI frame.
4743
4744 Value is non-zero if something was found which replaces the display
4745 of buffer or string text. */
4746
4747 static int
4748 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4749 Lisp_Object overlay, struct text_pos *position,
4750 ptrdiff_t bufpos, int display_replaced_p,
4751 int frame_window_p)
4752 {
4753 Lisp_Object form;
4754 Lisp_Object location, value;
4755 struct text_pos start_pos = *position;
4756 int valid_p;
4757
4758 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4759 If the result is non-nil, use VALUE instead of SPEC. */
4760 form = Qt;
4761 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4762 {
4763 spec = XCDR (spec);
4764 if (!CONSP (spec))
4765 return 0;
4766 form = XCAR (spec);
4767 spec = XCDR (spec);
4768 }
4769
4770 if (!NILP (form) && !EQ (form, Qt))
4771 {
4772 ptrdiff_t count = SPECPDL_INDEX ();
4773 struct gcpro gcpro1;
4774
4775 /* Bind `object' to the object having the `display' property, a
4776 buffer or string. Bind `position' to the position in the
4777 object where the property was found, and `buffer-position'
4778 to the current position in the buffer. */
4779
4780 if (NILP (object))
4781 XSETBUFFER (object, current_buffer);
4782 specbind (Qobject, object);
4783 specbind (Qposition, make_number (CHARPOS (*position)));
4784 specbind (Qbuffer_position, make_number (bufpos));
4785 GCPRO1 (form);
4786 form = safe_eval (form);
4787 UNGCPRO;
4788 unbind_to (count, Qnil);
4789 }
4790
4791 if (NILP (form))
4792 return 0;
4793
4794 /* Handle `(height HEIGHT)' specifications. */
4795 if (CONSP (spec)
4796 && EQ (XCAR (spec), Qheight)
4797 && CONSP (XCDR (spec)))
4798 {
4799 if (it)
4800 {
4801 if (!FRAME_WINDOW_P (it->f))
4802 return 0;
4803
4804 it->font_height = XCAR (XCDR (spec));
4805 if (!NILP (it->font_height))
4806 {
4807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4808 int new_height = -1;
4809
4810 if (CONSP (it->font_height)
4811 && (EQ (XCAR (it->font_height), Qplus)
4812 || EQ (XCAR (it->font_height), Qminus))
4813 && CONSP (XCDR (it->font_height))
4814 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4815 {
4816 /* `(+ N)' or `(- N)' where N is an integer. */
4817 int steps = XINT (XCAR (XCDR (it->font_height)));
4818 if (EQ (XCAR (it->font_height), Qplus))
4819 steps = - steps;
4820 it->face_id = smaller_face (it->f, it->face_id, steps);
4821 }
4822 else if (FUNCTIONP (it->font_height))
4823 {
4824 /* Call function with current height as argument.
4825 Value is the new height. */
4826 Lisp_Object height;
4827 height = safe_call1 (it->font_height,
4828 face->lface[LFACE_HEIGHT_INDEX]);
4829 if (NUMBERP (height))
4830 new_height = XFLOATINT (height);
4831 }
4832 else if (NUMBERP (it->font_height))
4833 {
4834 /* Value is a multiple of the canonical char height. */
4835 struct face *f;
4836
4837 f = FACE_FROM_ID (it->f,
4838 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4839 new_height = (XFLOATINT (it->font_height)
4840 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4841 }
4842 else
4843 {
4844 /* Evaluate IT->font_height with `height' bound to the
4845 current specified height to get the new height. */
4846 ptrdiff_t count = SPECPDL_INDEX ();
4847
4848 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4849 value = safe_eval (it->font_height);
4850 unbind_to (count, Qnil);
4851
4852 if (NUMBERP (value))
4853 new_height = XFLOATINT (value);
4854 }
4855
4856 if (new_height > 0)
4857 it->face_id = face_with_height (it->f, it->face_id, new_height);
4858 }
4859 }
4860
4861 return 0;
4862 }
4863
4864 /* Handle `(space-width WIDTH)'. */
4865 if (CONSP (spec)
4866 && EQ (XCAR (spec), Qspace_width)
4867 && CONSP (XCDR (spec)))
4868 {
4869 if (it)
4870 {
4871 if (!FRAME_WINDOW_P (it->f))
4872 return 0;
4873
4874 value = XCAR (XCDR (spec));
4875 if (NUMBERP (value) && XFLOATINT (value) > 0)
4876 it->space_width = value;
4877 }
4878
4879 return 0;
4880 }
4881
4882 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4883 if (CONSP (spec)
4884 && EQ (XCAR (spec), Qslice))
4885 {
4886 Lisp_Object tem;
4887
4888 if (it)
4889 {
4890 if (!FRAME_WINDOW_P (it->f))
4891 return 0;
4892
4893 if (tem = XCDR (spec), CONSP (tem))
4894 {
4895 it->slice.x = XCAR (tem);
4896 if (tem = XCDR (tem), CONSP (tem))
4897 {
4898 it->slice.y = XCAR (tem);
4899 if (tem = XCDR (tem), CONSP (tem))
4900 {
4901 it->slice.width = XCAR (tem);
4902 if (tem = XCDR (tem), CONSP (tem))
4903 it->slice.height = XCAR (tem);
4904 }
4905 }
4906 }
4907 }
4908
4909 return 0;
4910 }
4911
4912 /* Handle `(raise FACTOR)'. */
4913 if (CONSP (spec)
4914 && EQ (XCAR (spec), Qraise)
4915 && CONSP (XCDR (spec)))
4916 {
4917 if (it)
4918 {
4919 if (!FRAME_WINDOW_P (it->f))
4920 return 0;
4921
4922 #ifdef HAVE_WINDOW_SYSTEM
4923 value = XCAR (XCDR (spec));
4924 if (NUMBERP (value))
4925 {
4926 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4927 it->voffset = - (XFLOATINT (value)
4928 * (FONT_HEIGHT (face->font)));
4929 }
4930 #endif /* HAVE_WINDOW_SYSTEM */
4931 }
4932
4933 return 0;
4934 }
4935
4936 /* Don't handle the other kinds of display specifications
4937 inside a string that we got from a `display' property. */
4938 if (it && it->string_from_display_prop_p)
4939 return 0;
4940
4941 /* Characters having this form of property are not displayed, so
4942 we have to find the end of the property. */
4943 if (it)
4944 {
4945 start_pos = *position;
4946 *position = display_prop_end (it, object, start_pos);
4947 }
4948 value = Qnil;
4949
4950 /* Stop the scan at that end position--we assume that all
4951 text properties change there. */
4952 if (it)
4953 it->stop_charpos = position->charpos;
4954
4955 /* Handle `(left-fringe BITMAP [FACE])'
4956 and `(right-fringe BITMAP [FACE])'. */
4957 if (CONSP (spec)
4958 && (EQ (XCAR (spec), Qleft_fringe)
4959 || EQ (XCAR (spec), Qright_fringe))
4960 && CONSP (XCDR (spec)))
4961 {
4962 int fringe_bitmap;
4963
4964 if (it)
4965 {
4966 if (!FRAME_WINDOW_P (it->f))
4967 /* If we return here, POSITION has been advanced
4968 across the text with this property. */
4969 {
4970 /* Synchronize the bidi iterator with POSITION. This is
4971 needed because we are not going to push the iterator
4972 on behalf of this display property, so there will be
4973 no pop_it call to do this synchronization for us. */
4974 if (it->bidi_p)
4975 {
4976 it->position = *position;
4977 iterate_out_of_display_property (it);
4978 *position = it->position;
4979 }
4980 return 1;
4981 }
4982 }
4983 else if (!frame_window_p)
4984 return 1;
4985
4986 #ifdef HAVE_WINDOW_SYSTEM
4987 value = XCAR (XCDR (spec));
4988 if (!SYMBOLP (value)
4989 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4990 /* If we return here, POSITION has been advanced
4991 across the text with this property. */
4992 {
4993 if (it && it->bidi_p)
4994 {
4995 it->position = *position;
4996 iterate_out_of_display_property (it);
4997 *position = it->position;
4998 }
4999 return 1;
5000 }
5001
5002 if (it)
5003 {
5004 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
5005
5006 if (CONSP (XCDR (XCDR (spec))))
5007 {
5008 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5009 int face_id2 = lookup_derived_face (it->f, face_name,
5010 FRINGE_FACE_ID, 0);
5011 if (face_id2 >= 0)
5012 face_id = face_id2;
5013 }
5014
5015 /* Save current settings of IT so that we can restore them
5016 when we are finished with the glyph property value. */
5017 push_it (it, position);
5018
5019 it->area = TEXT_AREA;
5020 it->what = IT_IMAGE;
5021 it->image_id = -1; /* no image */
5022 it->position = start_pos;
5023 it->object = NILP (object) ? it->w->contents : object;
5024 it->method = GET_FROM_IMAGE;
5025 it->from_overlay = Qnil;
5026 it->face_id = face_id;
5027 it->from_disp_prop_p = true;
5028
5029 /* Say that we haven't consumed the characters with
5030 `display' property yet. The call to pop_it in
5031 set_iterator_to_next will clean this up. */
5032 *position = start_pos;
5033
5034 if (EQ (XCAR (spec), Qleft_fringe))
5035 {
5036 it->left_user_fringe_bitmap = fringe_bitmap;
5037 it->left_user_fringe_face_id = face_id;
5038 }
5039 else
5040 {
5041 it->right_user_fringe_bitmap = fringe_bitmap;
5042 it->right_user_fringe_face_id = face_id;
5043 }
5044 }
5045 #endif /* HAVE_WINDOW_SYSTEM */
5046 return 1;
5047 }
5048
5049 /* Prepare to handle `((margin left-margin) ...)',
5050 `((margin right-margin) ...)' and `((margin nil) ...)'
5051 prefixes for display specifications. */
5052 location = Qunbound;
5053 if (CONSP (spec) && CONSP (XCAR (spec)))
5054 {
5055 Lisp_Object tem;
5056
5057 value = XCDR (spec);
5058 if (CONSP (value))
5059 value = XCAR (value);
5060
5061 tem = XCAR (spec);
5062 if (EQ (XCAR (tem), Qmargin)
5063 && (tem = XCDR (tem),
5064 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5065 (NILP (tem)
5066 || EQ (tem, Qleft_margin)
5067 || EQ (tem, Qright_margin))))
5068 location = tem;
5069 }
5070
5071 if (EQ (location, Qunbound))
5072 {
5073 location = Qnil;
5074 value = spec;
5075 }
5076
5077 /* After this point, VALUE is the property after any
5078 margin prefix has been stripped. It must be a string,
5079 an image specification, or `(space ...)'.
5080
5081 LOCATION specifies where to display: `left-margin',
5082 `right-margin' or nil. */
5083
5084 valid_p = (STRINGP (value)
5085 #ifdef HAVE_WINDOW_SYSTEM
5086 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5087 && valid_image_p (value))
5088 #endif /* not HAVE_WINDOW_SYSTEM */
5089 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5090
5091 if (valid_p && !display_replaced_p)
5092 {
5093 int retval = 1;
5094
5095 if (!it)
5096 {
5097 /* Callers need to know whether the display spec is any kind
5098 of `(space ...)' spec that is about to affect text-area
5099 display. */
5100 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5101 retval = 2;
5102 return retval;
5103 }
5104
5105 /* Save current settings of IT so that we can restore them
5106 when we are finished with the glyph property value. */
5107 push_it (it, position);
5108 it->from_overlay = overlay;
5109 it->from_disp_prop_p = true;
5110
5111 if (NILP (location))
5112 it->area = TEXT_AREA;
5113 else if (EQ (location, Qleft_margin))
5114 it->area = LEFT_MARGIN_AREA;
5115 else
5116 it->area = RIGHT_MARGIN_AREA;
5117
5118 if (STRINGP (value))
5119 {
5120 it->string = value;
5121 it->multibyte_p = STRING_MULTIBYTE (it->string);
5122 it->current.overlay_string_index = -1;
5123 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5124 it->end_charpos = it->string_nchars = SCHARS (it->string);
5125 it->method = GET_FROM_STRING;
5126 it->stop_charpos = 0;
5127 it->prev_stop = 0;
5128 it->base_level_stop = 0;
5129 it->string_from_display_prop_p = true;
5130 /* Say that we haven't consumed the characters with
5131 `display' property yet. The call to pop_it in
5132 set_iterator_to_next will clean this up. */
5133 if (BUFFERP (object))
5134 *position = start_pos;
5135
5136 /* Force paragraph direction to be that of the parent
5137 object. If the parent object's paragraph direction is
5138 not yet determined, default to L2R. */
5139 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5140 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5141 else
5142 it->paragraph_embedding = L2R;
5143
5144 /* Set up the bidi iterator for this display string. */
5145 if (it->bidi_p)
5146 {
5147 it->bidi_it.string.lstring = it->string;
5148 it->bidi_it.string.s = NULL;
5149 it->bidi_it.string.schars = it->end_charpos;
5150 it->bidi_it.string.bufpos = bufpos;
5151 it->bidi_it.string.from_disp_str = 1;
5152 it->bidi_it.string.unibyte = !it->multibyte_p;
5153 it->bidi_it.w = it->w;
5154 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5155 }
5156 }
5157 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5158 {
5159 it->method = GET_FROM_STRETCH;
5160 it->object = value;
5161 *position = it->position = start_pos;
5162 retval = 1 + (it->area == TEXT_AREA);
5163 }
5164 #ifdef HAVE_WINDOW_SYSTEM
5165 else
5166 {
5167 it->what = IT_IMAGE;
5168 it->image_id = lookup_image (it->f, value);
5169 it->position = start_pos;
5170 it->object = NILP (object) ? it->w->contents : object;
5171 it->method = GET_FROM_IMAGE;
5172
5173 /* Say that we haven't consumed the characters with
5174 `display' property yet. The call to pop_it in
5175 set_iterator_to_next will clean this up. */
5176 *position = start_pos;
5177 }
5178 #endif /* HAVE_WINDOW_SYSTEM */
5179
5180 return retval;
5181 }
5182
5183 /* Invalid property or property not supported. Restore
5184 POSITION to what it was before. */
5185 *position = start_pos;
5186 return 0;
5187 }
5188
5189 /* Check if PROP is a display property value whose text should be
5190 treated as intangible. OVERLAY is the overlay from which PROP
5191 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5192 specify the buffer position covered by PROP. */
5193
5194 int
5195 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5196 ptrdiff_t charpos, ptrdiff_t bytepos)
5197 {
5198 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5199 struct text_pos position;
5200
5201 SET_TEXT_POS (position, charpos, bytepos);
5202 return handle_display_spec (NULL, prop, Qnil, overlay,
5203 &position, charpos, frame_window_p);
5204 }
5205
5206
5207 /* Return 1 if PROP is a display sub-property value containing STRING.
5208
5209 Implementation note: this and the following function are really
5210 special cases of handle_display_spec and
5211 handle_single_display_spec, and should ideally use the same code.
5212 Until they do, these two pairs must be consistent and must be
5213 modified in sync. */
5214
5215 static int
5216 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5217 {
5218 if (EQ (string, prop))
5219 return 1;
5220
5221 /* Skip over `when FORM'. */
5222 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5223 {
5224 prop = XCDR (prop);
5225 if (!CONSP (prop))
5226 return 0;
5227 /* Actually, the condition following `when' should be eval'ed,
5228 like handle_single_display_spec does, and we should return
5229 zero if it evaluates to nil. However, this function is
5230 called only when the buffer was already displayed and some
5231 glyph in the glyph matrix was found to come from a display
5232 string. Therefore, the condition was already evaluated, and
5233 the result was non-nil, otherwise the display string wouldn't
5234 have been displayed and we would have never been called for
5235 this property. Thus, we can skip the evaluation and assume
5236 its result is non-nil. */
5237 prop = XCDR (prop);
5238 }
5239
5240 if (CONSP (prop))
5241 /* Skip over `margin LOCATION'. */
5242 if (EQ (XCAR (prop), Qmargin))
5243 {
5244 prop = XCDR (prop);
5245 if (!CONSP (prop))
5246 return 0;
5247
5248 prop = XCDR (prop);
5249 if (!CONSP (prop))
5250 return 0;
5251 }
5252
5253 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5254 }
5255
5256
5257 /* Return 1 if STRING appears in the `display' property PROP. */
5258
5259 static int
5260 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5261 {
5262 if (CONSP (prop)
5263 && !EQ (XCAR (prop), Qwhen)
5264 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5265 {
5266 /* A list of sub-properties. */
5267 while (CONSP (prop))
5268 {
5269 if (single_display_spec_string_p (XCAR (prop), string))
5270 return 1;
5271 prop = XCDR (prop);
5272 }
5273 }
5274 else if (VECTORP (prop))
5275 {
5276 /* A vector of sub-properties. */
5277 ptrdiff_t i;
5278 for (i = 0; i < ASIZE (prop); ++i)
5279 if (single_display_spec_string_p (AREF (prop, i), string))
5280 return 1;
5281 }
5282 else
5283 return single_display_spec_string_p (prop, string);
5284
5285 return 0;
5286 }
5287
5288 /* Look for STRING in overlays and text properties in the current
5289 buffer, between character positions FROM and TO (excluding TO).
5290 BACK_P non-zero means look back (in this case, TO is supposed to be
5291 less than FROM).
5292 Value is the first character position where STRING was found, or
5293 zero if it wasn't found before hitting TO.
5294
5295 This function may only use code that doesn't eval because it is
5296 called asynchronously from note_mouse_highlight. */
5297
5298 static ptrdiff_t
5299 string_buffer_position_lim (Lisp_Object string,
5300 ptrdiff_t from, ptrdiff_t to, int back_p)
5301 {
5302 Lisp_Object limit, prop, pos;
5303 int found = 0;
5304
5305 pos = make_number (max (from, BEGV));
5306
5307 if (!back_p) /* looking forward */
5308 {
5309 limit = make_number (min (to, ZV));
5310 while (!found && !EQ (pos, limit))
5311 {
5312 prop = Fget_char_property (pos, Qdisplay, Qnil);
5313 if (!NILP (prop) && display_prop_string_p (prop, string))
5314 found = 1;
5315 else
5316 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5317 limit);
5318 }
5319 }
5320 else /* looking back */
5321 {
5322 limit = make_number (max (to, BEGV));
5323 while (!found && !EQ (pos, limit))
5324 {
5325 prop = Fget_char_property (pos, Qdisplay, Qnil);
5326 if (!NILP (prop) && display_prop_string_p (prop, string))
5327 found = 1;
5328 else
5329 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5330 limit);
5331 }
5332 }
5333
5334 return found ? XINT (pos) : 0;
5335 }
5336
5337 /* Determine which buffer position in current buffer STRING comes from.
5338 AROUND_CHARPOS is an approximate position where it could come from.
5339 Value is the buffer position or 0 if it couldn't be determined.
5340
5341 This function is necessary because we don't record buffer positions
5342 in glyphs generated from strings (to keep struct glyph small).
5343 This function may only use code that doesn't eval because it is
5344 called asynchronously from note_mouse_highlight. */
5345
5346 static ptrdiff_t
5347 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5348 {
5349 const int MAX_DISTANCE = 1000;
5350 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5351 around_charpos + MAX_DISTANCE,
5352 0);
5353
5354 if (!found)
5355 found = string_buffer_position_lim (string, around_charpos,
5356 around_charpos - MAX_DISTANCE, 1);
5357 return found;
5358 }
5359
5360
5361 \f
5362 /***********************************************************************
5363 `composition' property
5364 ***********************************************************************/
5365
5366 /* Set up iterator IT from `composition' property at its current
5367 position. Called from handle_stop. */
5368
5369 static enum prop_handled
5370 handle_composition_prop (struct it *it)
5371 {
5372 Lisp_Object prop, string;
5373 ptrdiff_t pos, pos_byte, start, end;
5374
5375 if (STRINGP (it->string))
5376 {
5377 unsigned char *s;
5378
5379 pos = IT_STRING_CHARPOS (*it);
5380 pos_byte = IT_STRING_BYTEPOS (*it);
5381 string = it->string;
5382 s = SDATA (string) + pos_byte;
5383 it->c = STRING_CHAR (s);
5384 }
5385 else
5386 {
5387 pos = IT_CHARPOS (*it);
5388 pos_byte = IT_BYTEPOS (*it);
5389 string = Qnil;
5390 it->c = FETCH_CHAR (pos_byte);
5391 }
5392
5393 /* If there's a valid composition and point is not inside of the
5394 composition (in the case that the composition is from the current
5395 buffer), draw a glyph composed from the composition components. */
5396 if (find_composition (pos, -1, &start, &end, &prop, string)
5397 && composition_valid_p (start, end, prop)
5398 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5399 {
5400 if (start < pos)
5401 /* As we can't handle this situation (perhaps font-lock added
5402 a new composition), we just return here hoping that next
5403 redisplay will detect this composition much earlier. */
5404 return HANDLED_NORMALLY;
5405 if (start != pos)
5406 {
5407 if (STRINGP (it->string))
5408 pos_byte = string_char_to_byte (it->string, start);
5409 else
5410 pos_byte = CHAR_TO_BYTE (start);
5411 }
5412 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5413 prop, string);
5414
5415 if (it->cmp_it.id >= 0)
5416 {
5417 it->cmp_it.ch = -1;
5418 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5419 it->cmp_it.nglyphs = -1;
5420 }
5421 }
5422
5423 return HANDLED_NORMALLY;
5424 }
5425
5426
5427 \f
5428 /***********************************************************************
5429 Overlay strings
5430 ***********************************************************************/
5431
5432 /* The following structure is used to record overlay strings for
5433 later sorting in load_overlay_strings. */
5434
5435 struct overlay_entry
5436 {
5437 Lisp_Object overlay;
5438 Lisp_Object string;
5439 EMACS_INT priority;
5440 int after_string_p;
5441 };
5442
5443
5444 /* Set up iterator IT from overlay strings at its current position.
5445 Called from handle_stop. */
5446
5447 static enum prop_handled
5448 handle_overlay_change (struct it *it)
5449 {
5450 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5451 return HANDLED_RECOMPUTE_PROPS;
5452 else
5453 return HANDLED_NORMALLY;
5454 }
5455
5456
5457 /* Set up the next overlay string for delivery by IT, if there is an
5458 overlay string to deliver. Called by set_iterator_to_next when the
5459 end of the current overlay string is reached. If there are more
5460 overlay strings to display, IT->string and
5461 IT->current.overlay_string_index are set appropriately here.
5462 Otherwise IT->string is set to nil. */
5463
5464 static void
5465 next_overlay_string (struct it *it)
5466 {
5467 ++it->current.overlay_string_index;
5468 if (it->current.overlay_string_index == it->n_overlay_strings)
5469 {
5470 /* No more overlay strings. Restore IT's settings to what
5471 they were before overlay strings were processed, and
5472 continue to deliver from current_buffer. */
5473
5474 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5475 pop_it (it);
5476 eassert (it->sp > 0
5477 || (NILP (it->string)
5478 && it->method == GET_FROM_BUFFER
5479 && it->stop_charpos >= BEGV
5480 && it->stop_charpos <= it->end_charpos));
5481 it->current.overlay_string_index = -1;
5482 it->n_overlay_strings = 0;
5483 it->overlay_strings_charpos = -1;
5484 /* If there's an empty display string on the stack, pop the
5485 stack, to resync the bidi iterator with IT's position. Such
5486 empty strings are pushed onto the stack in
5487 get_overlay_strings_1. */
5488 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5489 pop_it (it);
5490
5491 /* If we're at the end of the buffer, record that we have
5492 processed the overlay strings there already, so that
5493 next_element_from_buffer doesn't try it again. */
5494 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5495 it->overlay_strings_at_end_processed_p = true;
5496 }
5497 else
5498 {
5499 /* There are more overlay strings to process. If
5500 IT->current.overlay_string_index has advanced to a position
5501 where we must load IT->overlay_strings with more strings, do
5502 it. We must load at the IT->overlay_strings_charpos where
5503 IT->n_overlay_strings was originally computed; when invisible
5504 text is present, this might not be IT_CHARPOS (Bug#7016). */
5505 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5506
5507 if (it->current.overlay_string_index && i == 0)
5508 load_overlay_strings (it, it->overlay_strings_charpos);
5509
5510 /* Initialize IT to deliver display elements from the overlay
5511 string. */
5512 it->string = it->overlay_strings[i];
5513 it->multibyte_p = STRING_MULTIBYTE (it->string);
5514 SET_TEXT_POS (it->current.string_pos, 0, 0);
5515 it->method = GET_FROM_STRING;
5516 it->stop_charpos = 0;
5517 it->end_charpos = SCHARS (it->string);
5518 if (it->cmp_it.stop_pos >= 0)
5519 it->cmp_it.stop_pos = 0;
5520 it->prev_stop = 0;
5521 it->base_level_stop = 0;
5522
5523 /* Set up the bidi iterator for this overlay string. */
5524 if (it->bidi_p)
5525 {
5526 it->bidi_it.string.lstring = it->string;
5527 it->bidi_it.string.s = NULL;
5528 it->bidi_it.string.schars = SCHARS (it->string);
5529 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5530 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5531 it->bidi_it.string.unibyte = !it->multibyte_p;
5532 it->bidi_it.w = it->w;
5533 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5534 }
5535 }
5536
5537 CHECK_IT (it);
5538 }
5539
5540
5541 /* Compare two overlay_entry structures E1 and E2. Used as a
5542 comparison function for qsort in load_overlay_strings. Overlay
5543 strings for the same position are sorted so that
5544
5545 1. All after-strings come in front of before-strings, except
5546 when they come from the same overlay.
5547
5548 2. Within after-strings, strings are sorted so that overlay strings
5549 from overlays with higher priorities come first.
5550
5551 2. Within before-strings, strings are sorted so that overlay
5552 strings from overlays with higher priorities come last.
5553
5554 Value is analogous to strcmp. */
5555
5556
5557 static int
5558 compare_overlay_entries (const void *e1, const void *e2)
5559 {
5560 struct overlay_entry const *entry1 = e1;
5561 struct overlay_entry const *entry2 = e2;
5562 int result;
5563
5564 if (entry1->after_string_p != entry2->after_string_p)
5565 {
5566 /* Let after-strings appear in front of before-strings if
5567 they come from different overlays. */
5568 if (EQ (entry1->overlay, entry2->overlay))
5569 result = entry1->after_string_p ? 1 : -1;
5570 else
5571 result = entry1->after_string_p ? -1 : 1;
5572 }
5573 else if (entry1->priority != entry2->priority)
5574 {
5575 if (entry1->after_string_p)
5576 /* After-strings sorted in order of decreasing priority. */
5577 result = entry2->priority < entry1->priority ? -1 : 1;
5578 else
5579 /* Before-strings sorted in order of increasing priority. */
5580 result = entry1->priority < entry2->priority ? -1 : 1;
5581 }
5582 else
5583 result = 0;
5584
5585 return result;
5586 }
5587
5588
5589 /* Load the vector IT->overlay_strings with overlay strings from IT's
5590 current buffer position, or from CHARPOS if that is > 0. Set
5591 IT->n_overlays to the total number of overlay strings found.
5592
5593 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5594 a time. On entry into load_overlay_strings,
5595 IT->current.overlay_string_index gives the number of overlay
5596 strings that have already been loaded by previous calls to this
5597 function.
5598
5599 IT->add_overlay_start contains an additional overlay start
5600 position to consider for taking overlay strings from, if non-zero.
5601 This position comes into play when the overlay has an `invisible'
5602 property, and both before and after-strings. When we've skipped to
5603 the end of the overlay, because of its `invisible' property, we
5604 nevertheless want its before-string to appear.
5605 IT->add_overlay_start will contain the overlay start position
5606 in this case.
5607
5608 Overlay strings are sorted so that after-string strings come in
5609 front of before-string strings. Within before and after-strings,
5610 strings are sorted by overlay priority. See also function
5611 compare_overlay_entries. */
5612
5613 static void
5614 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5615 {
5616 Lisp_Object overlay, window, str, invisible;
5617 struct Lisp_Overlay *ov;
5618 ptrdiff_t start, end;
5619 ptrdiff_t size = 20;
5620 ptrdiff_t n = 0, i, j;
5621 int invis_p;
5622 struct overlay_entry *entries = alloca (size * sizeof *entries);
5623 USE_SAFE_ALLOCA;
5624
5625 if (charpos <= 0)
5626 charpos = IT_CHARPOS (*it);
5627
5628 /* Append the overlay string STRING of overlay OVERLAY to vector
5629 `entries' which has size `size' and currently contains `n'
5630 elements. AFTER_P non-zero means STRING is an after-string of
5631 OVERLAY. */
5632 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5633 do \
5634 { \
5635 Lisp_Object priority; \
5636 \
5637 if (n == size) \
5638 { \
5639 struct overlay_entry *old = entries; \
5640 SAFE_NALLOCA (entries, 2, size); \
5641 memcpy (entries, old, size * sizeof *entries); \
5642 size *= 2; \
5643 } \
5644 \
5645 entries[n].string = (STRING); \
5646 entries[n].overlay = (OVERLAY); \
5647 priority = Foverlay_get ((OVERLAY), Qpriority); \
5648 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5649 entries[n].after_string_p = (AFTER_P); \
5650 ++n; \
5651 } \
5652 while (0)
5653
5654 /* Process overlay before the overlay center. */
5655 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5656 {
5657 XSETMISC (overlay, ov);
5658 eassert (OVERLAYP (overlay));
5659 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5660 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5661
5662 if (end < charpos)
5663 break;
5664
5665 /* Skip this overlay if it doesn't start or end at IT's current
5666 position. */
5667 if (end != charpos && start != charpos)
5668 continue;
5669
5670 /* Skip this overlay if it doesn't apply to IT->w. */
5671 window = Foverlay_get (overlay, Qwindow);
5672 if (WINDOWP (window) && XWINDOW (window) != it->w)
5673 continue;
5674
5675 /* If the text ``under'' the overlay is invisible, both before-
5676 and after-strings from this overlay are visible; start and
5677 end position are indistinguishable. */
5678 invisible = Foverlay_get (overlay, Qinvisible);
5679 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5680
5681 /* If overlay has a non-empty before-string, record it. */
5682 if ((start == charpos || (end == charpos && invis_p))
5683 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, 0);
5686
5687 /* If overlay has a non-empty after-string, record it. */
5688 if ((end == charpos || (start == charpos && invis_p))
5689 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5690 && SCHARS (str))
5691 RECORD_OVERLAY_STRING (overlay, str, 1);
5692 }
5693
5694 /* Process overlays after the overlay center. */
5695 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5696 {
5697 XSETMISC (overlay, ov);
5698 eassert (OVERLAYP (overlay));
5699 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5700 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5701
5702 if (start > charpos)
5703 break;
5704
5705 /* Skip this overlay if it doesn't start or end at IT's current
5706 position. */
5707 if (end != charpos && start != charpos)
5708 continue;
5709
5710 /* Skip this overlay if it doesn't apply to IT->w. */
5711 window = Foverlay_get (overlay, Qwindow);
5712 if (WINDOWP (window) && XWINDOW (window) != it->w)
5713 continue;
5714
5715 /* If the text ``under'' the overlay is invisible, it has a zero
5716 dimension, and both before- and after-strings apply. */
5717 invisible = Foverlay_get (overlay, Qinvisible);
5718 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5719
5720 /* If overlay has a non-empty before-string, record it. */
5721 if ((start == charpos || (end == charpos && invis_p))
5722 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5723 && SCHARS (str))
5724 RECORD_OVERLAY_STRING (overlay, str, 0);
5725
5726 /* If overlay has a non-empty after-string, record it. */
5727 if ((end == charpos || (start == charpos && invis_p))
5728 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5729 && SCHARS (str))
5730 RECORD_OVERLAY_STRING (overlay, str, 1);
5731 }
5732
5733 #undef RECORD_OVERLAY_STRING
5734
5735 /* Sort entries. */
5736 if (n > 1)
5737 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5738
5739 /* Record number of overlay strings, and where we computed it. */
5740 it->n_overlay_strings = n;
5741 it->overlay_strings_charpos = charpos;
5742
5743 /* IT->current.overlay_string_index is the number of overlay strings
5744 that have already been consumed by IT. Copy some of the
5745 remaining overlay strings to IT->overlay_strings. */
5746 i = 0;
5747 j = it->current.overlay_string_index;
5748 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5749 {
5750 it->overlay_strings[i] = entries[j].string;
5751 it->string_overlays[i++] = entries[j++].overlay;
5752 }
5753
5754 CHECK_IT (it);
5755 SAFE_FREE ();
5756 }
5757
5758
5759 /* Get the first chunk of overlay strings at IT's current buffer
5760 position, or at CHARPOS if that is > 0. Value is non-zero if at
5761 least one overlay string was found. */
5762
5763 static int
5764 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5765 {
5766 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5767 process. This fills IT->overlay_strings with strings, and sets
5768 IT->n_overlay_strings to the total number of strings to process.
5769 IT->pos.overlay_string_index has to be set temporarily to zero
5770 because load_overlay_strings needs this; it must be set to -1
5771 when no overlay strings are found because a zero value would
5772 indicate a position in the first overlay string. */
5773 it->current.overlay_string_index = 0;
5774 load_overlay_strings (it, charpos);
5775
5776 /* If we found overlay strings, set up IT to deliver display
5777 elements from the first one. Otherwise set up IT to deliver
5778 from current_buffer. */
5779 if (it->n_overlay_strings)
5780 {
5781 /* Make sure we know settings in current_buffer, so that we can
5782 restore meaningful values when we're done with the overlay
5783 strings. */
5784 if (compute_stop_p)
5785 compute_stop_pos (it);
5786 eassert (it->face_id >= 0);
5787
5788 /* Save IT's settings. They are restored after all overlay
5789 strings have been processed. */
5790 eassert (!compute_stop_p || it->sp == 0);
5791
5792 /* When called from handle_stop, there might be an empty display
5793 string loaded. In that case, don't bother saving it. But
5794 don't use this optimization with the bidi iterator, since we
5795 need the corresponding pop_it call to resync the bidi
5796 iterator's position with IT's position, after we are done
5797 with the overlay strings. (The corresponding call to pop_it
5798 in case of an empty display string is in
5799 next_overlay_string.) */
5800 if (!(!it->bidi_p
5801 && STRINGP (it->string) && !SCHARS (it->string)))
5802 push_it (it, NULL);
5803
5804 /* Set up IT to deliver display elements from the first overlay
5805 string. */
5806 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5807 it->string = it->overlay_strings[0];
5808 it->from_overlay = Qnil;
5809 it->stop_charpos = 0;
5810 eassert (STRINGP (it->string));
5811 it->end_charpos = SCHARS (it->string);
5812 it->prev_stop = 0;
5813 it->base_level_stop = 0;
5814 it->multibyte_p = STRING_MULTIBYTE (it->string);
5815 it->method = GET_FROM_STRING;
5816 it->from_disp_prop_p = 0;
5817
5818 /* Force paragraph direction to be that of the parent
5819 buffer. */
5820 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5821 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5822 else
5823 it->paragraph_embedding = L2R;
5824
5825 /* Set up the bidi iterator for this overlay string. */
5826 if (it->bidi_p)
5827 {
5828 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5829
5830 it->bidi_it.string.lstring = it->string;
5831 it->bidi_it.string.s = NULL;
5832 it->bidi_it.string.schars = SCHARS (it->string);
5833 it->bidi_it.string.bufpos = pos;
5834 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5835 it->bidi_it.string.unibyte = !it->multibyte_p;
5836 it->bidi_it.w = it->w;
5837 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5838 }
5839 return 1;
5840 }
5841
5842 it->current.overlay_string_index = -1;
5843 return 0;
5844 }
5845
5846 static int
5847 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5848 {
5849 it->string = Qnil;
5850 it->method = GET_FROM_BUFFER;
5851
5852 (void) get_overlay_strings_1 (it, charpos, 1);
5853
5854 CHECK_IT (it);
5855
5856 /* Value is non-zero if we found at least one overlay string. */
5857 return STRINGP (it->string);
5858 }
5859
5860
5861 \f
5862 /***********************************************************************
5863 Saving and restoring state
5864 ***********************************************************************/
5865
5866 /* Save current settings of IT on IT->stack. Called, for example,
5867 before setting up IT for an overlay string, to be able to restore
5868 IT's settings to what they were after the overlay string has been
5869 processed. If POSITION is non-NULL, it is the position to save on
5870 the stack instead of IT->position. */
5871
5872 static void
5873 push_it (struct it *it, struct text_pos *position)
5874 {
5875 struct iterator_stack_entry *p;
5876
5877 eassert (it->sp < IT_STACK_SIZE);
5878 p = it->stack + it->sp;
5879
5880 p->stop_charpos = it->stop_charpos;
5881 p->prev_stop = it->prev_stop;
5882 p->base_level_stop = it->base_level_stop;
5883 p->cmp_it = it->cmp_it;
5884 eassert (it->face_id >= 0);
5885 p->face_id = it->face_id;
5886 p->string = it->string;
5887 p->method = it->method;
5888 p->from_overlay = it->from_overlay;
5889 switch (p->method)
5890 {
5891 case GET_FROM_IMAGE:
5892 p->u.image.object = it->object;
5893 p->u.image.image_id = it->image_id;
5894 p->u.image.slice = it->slice;
5895 break;
5896 case GET_FROM_STRETCH:
5897 p->u.stretch.object = it->object;
5898 break;
5899 }
5900 p->position = position ? *position : it->position;
5901 p->current = it->current;
5902 p->end_charpos = it->end_charpos;
5903 p->string_nchars = it->string_nchars;
5904 p->area = it->area;
5905 p->multibyte_p = it->multibyte_p;
5906 p->avoid_cursor_p = it->avoid_cursor_p;
5907 p->space_width = it->space_width;
5908 p->font_height = it->font_height;
5909 p->voffset = it->voffset;
5910 p->string_from_display_prop_p = it->string_from_display_prop_p;
5911 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5912 p->display_ellipsis_p = 0;
5913 p->line_wrap = it->line_wrap;
5914 p->bidi_p = it->bidi_p;
5915 p->paragraph_embedding = it->paragraph_embedding;
5916 p->from_disp_prop_p = it->from_disp_prop_p;
5917 ++it->sp;
5918
5919 /* Save the state of the bidi iterator as well. */
5920 if (it->bidi_p)
5921 bidi_push_it (&it->bidi_it);
5922 }
5923
5924 static void
5925 iterate_out_of_display_property (struct it *it)
5926 {
5927 int buffer_p = !STRINGP (it->string);
5928 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5929 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5930
5931 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5932
5933 /* Maybe initialize paragraph direction. If we are at the beginning
5934 of a new paragraph, next_element_from_buffer may not have a
5935 chance to do that. */
5936 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5937 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5938 /* prev_stop can be zero, so check against BEGV as well. */
5939 while (it->bidi_it.charpos >= bob
5940 && it->prev_stop <= it->bidi_it.charpos
5941 && it->bidi_it.charpos < CHARPOS (it->position)
5942 && it->bidi_it.charpos < eob)
5943 bidi_move_to_visually_next (&it->bidi_it);
5944 /* Record the stop_pos we just crossed, for when we cross it
5945 back, maybe. */
5946 if (it->bidi_it.charpos > CHARPOS (it->position))
5947 it->prev_stop = CHARPOS (it->position);
5948 /* If we ended up not where pop_it put us, resync IT's
5949 positional members with the bidi iterator. */
5950 if (it->bidi_it.charpos != CHARPOS (it->position))
5951 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5952 if (buffer_p)
5953 it->current.pos = it->position;
5954 else
5955 it->current.string_pos = it->position;
5956 }
5957
5958 /* Restore IT's settings from IT->stack. Called, for example, when no
5959 more overlay strings must be processed, and we return to delivering
5960 display elements from a buffer, or when the end of a string from a
5961 `display' property is reached and we return to delivering display
5962 elements from an overlay string, or from a buffer. */
5963
5964 static void
5965 pop_it (struct it *it)
5966 {
5967 struct iterator_stack_entry *p;
5968 int from_display_prop = it->from_disp_prop_p;
5969
5970 eassert (it->sp > 0);
5971 --it->sp;
5972 p = it->stack + it->sp;
5973 it->stop_charpos = p->stop_charpos;
5974 it->prev_stop = p->prev_stop;
5975 it->base_level_stop = p->base_level_stop;
5976 it->cmp_it = p->cmp_it;
5977 it->face_id = p->face_id;
5978 it->current = p->current;
5979 it->position = p->position;
5980 it->string = p->string;
5981 it->from_overlay = p->from_overlay;
5982 if (NILP (it->string))
5983 SET_TEXT_POS (it->current.string_pos, -1, -1);
5984 it->method = p->method;
5985 switch (it->method)
5986 {
5987 case GET_FROM_IMAGE:
5988 it->image_id = p->u.image.image_id;
5989 it->object = p->u.image.object;
5990 it->slice = p->u.image.slice;
5991 break;
5992 case GET_FROM_STRETCH:
5993 it->object = p->u.stretch.object;
5994 break;
5995 case GET_FROM_BUFFER:
5996 it->object = it->w->contents;
5997 break;
5998 case GET_FROM_STRING:
5999 {
6000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6001
6002 /* Restore the face_box_p flag, since it could have been
6003 overwritten by the face of the object that we just finished
6004 displaying. */
6005 if (face)
6006 it->face_box_p = face->box != FACE_NO_BOX;
6007 it->object = it->string;
6008 }
6009 break;
6010 case GET_FROM_DISPLAY_VECTOR:
6011 if (it->s)
6012 it->method = GET_FROM_C_STRING;
6013 else if (STRINGP (it->string))
6014 it->method = GET_FROM_STRING;
6015 else
6016 {
6017 it->method = GET_FROM_BUFFER;
6018 it->object = it->w->contents;
6019 }
6020 }
6021 it->end_charpos = p->end_charpos;
6022 it->string_nchars = p->string_nchars;
6023 it->area = p->area;
6024 it->multibyte_p = p->multibyte_p;
6025 it->avoid_cursor_p = p->avoid_cursor_p;
6026 it->space_width = p->space_width;
6027 it->font_height = p->font_height;
6028 it->voffset = p->voffset;
6029 it->string_from_display_prop_p = p->string_from_display_prop_p;
6030 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6031 it->line_wrap = p->line_wrap;
6032 it->bidi_p = p->bidi_p;
6033 it->paragraph_embedding = p->paragraph_embedding;
6034 it->from_disp_prop_p = p->from_disp_prop_p;
6035 if (it->bidi_p)
6036 {
6037 bidi_pop_it (&it->bidi_it);
6038 /* Bidi-iterate until we get out of the portion of text, if any,
6039 covered by a `display' text property or by an overlay with
6040 `display' property. (We cannot just jump there, because the
6041 internal coherency of the bidi iterator state can not be
6042 preserved across such jumps.) We also must determine the
6043 paragraph base direction if the overlay we just processed is
6044 at the beginning of a new paragraph. */
6045 if (from_display_prop
6046 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6047 iterate_out_of_display_property (it);
6048
6049 eassert ((BUFFERP (it->object)
6050 && IT_CHARPOS (*it) == it->bidi_it.charpos
6051 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6052 || (STRINGP (it->object)
6053 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6054 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6055 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6056 }
6057 }
6058
6059
6060 \f
6061 /***********************************************************************
6062 Moving over lines
6063 ***********************************************************************/
6064
6065 /* Set IT's current position to the previous line start. */
6066
6067 static void
6068 back_to_previous_line_start (struct it *it)
6069 {
6070 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6071
6072 DEC_BOTH (cp, bp);
6073 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6074 }
6075
6076
6077 /* Move IT to the next line start.
6078
6079 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6080 we skipped over part of the text (as opposed to moving the iterator
6081 continuously over the text). Otherwise, don't change the value
6082 of *SKIPPED_P.
6083
6084 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6085 iterator on the newline, if it was found.
6086
6087 Newlines may come from buffer text, overlay strings, or strings
6088 displayed via the `display' property. That's the reason we can't
6089 simply use find_newline_no_quit.
6090
6091 Note that this function may not skip over invisible text that is so
6092 because of text properties and immediately follows a newline. If
6093 it would, function reseat_at_next_visible_line_start, when called
6094 from set_iterator_to_next, would effectively make invisible
6095 characters following a newline part of the wrong glyph row, which
6096 leads to wrong cursor motion. */
6097
6098 static int
6099 forward_to_next_line_start (struct it *it, int *skipped_p,
6100 struct bidi_it *bidi_it_prev)
6101 {
6102 ptrdiff_t old_selective;
6103 int newline_found_p, n;
6104 const int MAX_NEWLINE_DISTANCE = 500;
6105
6106 /* If already on a newline, just consume it to avoid unintended
6107 skipping over invisible text below. */
6108 if (it->what == IT_CHARACTER
6109 && it->c == '\n'
6110 && CHARPOS (it->position) == IT_CHARPOS (*it))
6111 {
6112 if (it->bidi_p && bidi_it_prev)
6113 *bidi_it_prev = it->bidi_it;
6114 set_iterator_to_next (it, 0);
6115 it->c = 0;
6116 return 1;
6117 }
6118
6119 /* Don't handle selective display in the following. It's (a)
6120 unnecessary because it's done by the caller, and (b) leads to an
6121 infinite recursion because next_element_from_ellipsis indirectly
6122 calls this function. */
6123 old_selective = it->selective;
6124 it->selective = 0;
6125
6126 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6127 from buffer text. */
6128 for (n = newline_found_p = 0;
6129 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6130 n += STRINGP (it->string) ? 0 : 1)
6131 {
6132 if (!get_next_display_element (it))
6133 return 0;
6134 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6135 if (newline_found_p && it->bidi_p && bidi_it_prev)
6136 *bidi_it_prev = it->bidi_it;
6137 set_iterator_to_next (it, 0);
6138 }
6139
6140 /* If we didn't find a newline near enough, see if we can use a
6141 short-cut. */
6142 if (!newline_found_p)
6143 {
6144 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6145 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6146 1, &bytepos);
6147 Lisp_Object pos;
6148
6149 eassert (!STRINGP (it->string));
6150
6151 /* If there isn't any `display' property in sight, and no
6152 overlays, we can just use the position of the newline in
6153 buffer text. */
6154 if (it->stop_charpos >= limit
6155 || ((pos = Fnext_single_property_change (make_number (start),
6156 Qdisplay, Qnil,
6157 make_number (limit)),
6158 NILP (pos))
6159 && next_overlay_change (start) == ZV))
6160 {
6161 if (!it->bidi_p)
6162 {
6163 IT_CHARPOS (*it) = limit;
6164 IT_BYTEPOS (*it) = bytepos;
6165 }
6166 else
6167 {
6168 struct bidi_it bprev;
6169
6170 /* Help bidi.c avoid expensive searches for display
6171 properties and overlays, by telling it that there are
6172 none up to `limit'. */
6173 if (it->bidi_it.disp_pos < limit)
6174 {
6175 it->bidi_it.disp_pos = limit;
6176 it->bidi_it.disp_prop = 0;
6177 }
6178 do {
6179 bprev = it->bidi_it;
6180 bidi_move_to_visually_next (&it->bidi_it);
6181 } while (it->bidi_it.charpos != limit);
6182 IT_CHARPOS (*it) = limit;
6183 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6184 if (bidi_it_prev)
6185 *bidi_it_prev = bprev;
6186 }
6187 *skipped_p = newline_found_p = true;
6188 }
6189 else
6190 {
6191 while (get_next_display_element (it)
6192 && !newline_found_p)
6193 {
6194 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6195 if (newline_found_p && it->bidi_p && bidi_it_prev)
6196 *bidi_it_prev = it->bidi_it;
6197 set_iterator_to_next (it, 0);
6198 }
6199 }
6200 }
6201
6202 it->selective = old_selective;
6203 return newline_found_p;
6204 }
6205
6206
6207 /* Set IT's current position to the previous visible line start. Skip
6208 invisible text that is so either due to text properties or due to
6209 selective display. Caution: this does not change IT->current_x and
6210 IT->hpos. */
6211
6212 static void
6213 back_to_previous_visible_line_start (struct it *it)
6214 {
6215 while (IT_CHARPOS (*it) > BEGV)
6216 {
6217 back_to_previous_line_start (it);
6218
6219 if (IT_CHARPOS (*it) <= BEGV)
6220 break;
6221
6222 /* If selective > 0, then lines indented more than its value are
6223 invisible. */
6224 if (it->selective > 0
6225 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6226 it->selective))
6227 continue;
6228
6229 /* Check the newline before point for invisibility. */
6230 {
6231 Lisp_Object prop;
6232 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6233 Qinvisible, it->window);
6234 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6235 continue;
6236 }
6237
6238 if (IT_CHARPOS (*it) <= BEGV)
6239 break;
6240
6241 {
6242 struct it it2;
6243 void *it2data = NULL;
6244 ptrdiff_t pos;
6245 ptrdiff_t beg, end;
6246 Lisp_Object val, overlay;
6247
6248 SAVE_IT (it2, *it, it2data);
6249
6250 /* If newline is part of a composition, continue from start of composition */
6251 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6252 && beg < IT_CHARPOS (*it))
6253 goto replaced;
6254
6255 /* If newline is replaced by a display property, find start of overlay
6256 or interval and continue search from that point. */
6257 pos = --IT_CHARPOS (it2);
6258 --IT_BYTEPOS (it2);
6259 it2.sp = 0;
6260 bidi_unshelve_cache (NULL, 0);
6261 it2.string_from_display_prop_p = 0;
6262 it2.from_disp_prop_p = 0;
6263 if (handle_display_prop (&it2) == HANDLED_RETURN
6264 && !NILP (val = get_char_property_and_overlay
6265 (make_number (pos), Qdisplay, Qnil, &overlay))
6266 && (OVERLAYP (overlay)
6267 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6268 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6269 {
6270 RESTORE_IT (it, it, it2data);
6271 goto replaced;
6272 }
6273
6274 /* Newline is not replaced by anything -- so we are done. */
6275 RESTORE_IT (it, it, it2data);
6276 break;
6277
6278 replaced:
6279 if (beg < BEGV)
6280 beg = BEGV;
6281 IT_CHARPOS (*it) = beg;
6282 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6283 }
6284 }
6285
6286 it->continuation_lines_width = 0;
6287
6288 eassert (IT_CHARPOS (*it) >= BEGV);
6289 eassert (IT_CHARPOS (*it) == BEGV
6290 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6291 CHECK_IT (it);
6292 }
6293
6294
6295 /* Reseat iterator IT at the previous visible line start. Skip
6296 invisible text that is so either due to text properties or due to
6297 selective display. At the end, update IT's overlay information,
6298 face information etc. */
6299
6300 void
6301 reseat_at_previous_visible_line_start (struct it *it)
6302 {
6303 back_to_previous_visible_line_start (it);
6304 reseat (it, it->current.pos, 1);
6305 CHECK_IT (it);
6306 }
6307
6308
6309 /* Reseat iterator IT on the next visible line start in the current
6310 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6311 preceding the line start. Skip over invisible text that is so
6312 because of selective display. Compute faces, overlays etc at the
6313 new position. Note that this function does not skip over text that
6314 is invisible because of text properties. */
6315
6316 static void
6317 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6318 {
6319 int newline_found_p, skipped_p = 0;
6320 struct bidi_it bidi_it_prev;
6321
6322 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6323
6324 /* Skip over lines that are invisible because they are indented
6325 more than the value of IT->selective. */
6326 if (it->selective > 0)
6327 while (IT_CHARPOS (*it) < ZV
6328 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6329 it->selective))
6330 {
6331 eassert (IT_BYTEPOS (*it) == BEGV
6332 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6333 newline_found_p =
6334 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6335 }
6336
6337 /* Position on the newline if that's what's requested. */
6338 if (on_newline_p && newline_found_p)
6339 {
6340 if (STRINGP (it->string))
6341 {
6342 if (IT_STRING_CHARPOS (*it) > 0)
6343 {
6344 if (!it->bidi_p)
6345 {
6346 --IT_STRING_CHARPOS (*it);
6347 --IT_STRING_BYTEPOS (*it);
6348 }
6349 else
6350 {
6351 /* We need to restore the bidi iterator to the state
6352 it had on the newline, and resync the IT's
6353 position with that. */
6354 it->bidi_it = bidi_it_prev;
6355 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6356 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6357 }
6358 }
6359 }
6360 else if (IT_CHARPOS (*it) > BEGV)
6361 {
6362 if (!it->bidi_p)
6363 {
6364 --IT_CHARPOS (*it);
6365 --IT_BYTEPOS (*it);
6366 }
6367 else
6368 {
6369 /* We need to restore the bidi iterator to the state it
6370 had on the newline and resync IT with that. */
6371 it->bidi_it = bidi_it_prev;
6372 IT_CHARPOS (*it) = it->bidi_it.charpos;
6373 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6374 }
6375 reseat (it, it->current.pos, 0);
6376 }
6377 }
6378 else if (skipped_p)
6379 reseat (it, it->current.pos, 0);
6380
6381 CHECK_IT (it);
6382 }
6383
6384
6385 \f
6386 /***********************************************************************
6387 Changing an iterator's position
6388 ***********************************************************************/
6389
6390 /* Change IT's current position to POS in current_buffer. If FORCE_P
6391 is non-zero, always check for text properties at the new position.
6392 Otherwise, text properties are only looked up if POS >=
6393 IT->check_charpos of a property. */
6394
6395 static void
6396 reseat (struct it *it, struct text_pos pos, int force_p)
6397 {
6398 ptrdiff_t original_pos = IT_CHARPOS (*it);
6399
6400 reseat_1 (it, pos, 0);
6401
6402 /* Determine where to check text properties. Avoid doing it
6403 where possible because text property lookup is very expensive. */
6404 if (force_p
6405 || CHARPOS (pos) > it->stop_charpos
6406 || CHARPOS (pos) < original_pos)
6407 {
6408 if (it->bidi_p)
6409 {
6410 /* For bidi iteration, we need to prime prev_stop and
6411 base_level_stop with our best estimations. */
6412 /* Implementation note: Of course, POS is not necessarily a
6413 stop position, so assigning prev_pos to it is a lie; we
6414 should have called compute_stop_backwards. However, if
6415 the current buffer does not include any R2L characters,
6416 that call would be a waste of cycles, because the
6417 iterator will never move back, and thus never cross this
6418 "fake" stop position. So we delay that backward search
6419 until the time we really need it, in next_element_from_buffer. */
6420 if (CHARPOS (pos) != it->prev_stop)
6421 it->prev_stop = CHARPOS (pos);
6422 if (CHARPOS (pos) < it->base_level_stop)
6423 it->base_level_stop = 0; /* meaning it's unknown */
6424 handle_stop (it);
6425 }
6426 else
6427 {
6428 handle_stop (it);
6429 it->prev_stop = it->base_level_stop = 0;
6430 }
6431
6432 }
6433
6434 CHECK_IT (it);
6435 }
6436
6437
6438 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6439 IT->stop_pos to POS, also. */
6440
6441 static void
6442 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6443 {
6444 /* Don't call this function when scanning a C string. */
6445 eassert (it->s == NULL);
6446
6447 /* POS must be a reasonable value. */
6448 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6449
6450 it->current.pos = it->position = pos;
6451 it->end_charpos = ZV;
6452 it->dpvec = NULL;
6453 it->current.dpvec_index = -1;
6454 it->current.overlay_string_index = -1;
6455 IT_STRING_CHARPOS (*it) = -1;
6456 IT_STRING_BYTEPOS (*it) = -1;
6457 it->string = Qnil;
6458 it->method = GET_FROM_BUFFER;
6459 it->object = it->w->contents;
6460 it->area = TEXT_AREA;
6461 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6462 it->sp = 0;
6463 it->string_from_display_prop_p = 0;
6464 it->string_from_prefix_prop_p = 0;
6465
6466 it->from_disp_prop_p = 0;
6467 it->face_before_selective_p = 0;
6468 if (it->bidi_p)
6469 {
6470 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6471 &it->bidi_it);
6472 bidi_unshelve_cache (NULL, 0);
6473 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6474 it->bidi_it.string.s = NULL;
6475 it->bidi_it.string.lstring = Qnil;
6476 it->bidi_it.string.bufpos = 0;
6477 it->bidi_it.string.from_disp_str = 0;
6478 it->bidi_it.string.unibyte = 0;
6479 it->bidi_it.w = it->w;
6480 }
6481
6482 if (set_stop_p)
6483 {
6484 it->stop_charpos = CHARPOS (pos);
6485 it->base_level_stop = CHARPOS (pos);
6486 }
6487 /* This make the information stored in it->cmp_it invalidate. */
6488 it->cmp_it.id = -1;
6489 }
6490
6491
6492 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6493 If S is non-null, it is a C string to iterate over. Otherwise,
6494 STRING gives a Lisp string to iterate over.
6495
6496 If PRECISION > 0, don't return more then PRECISION number of
6497 characters from the string.
6498
6499 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6500 characters have been returned. FIELD_WIDTH < 0 means an infinite
6501 field width.
6502
6503 MULTIBYTE = 0 means disable processing of multibyte characters,
6504 MULTIBYTE > 0 means enable it,
6505 MULTIBYTE < 0 means use IT->multibyte_p.
6506
6507 IT must be initialized via a prior call to init_iterator before
6508 calling this function. */
6509
6510 static void
6511 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6512 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6513 int multibyte)
6514 {
6515 /* No text property checks performed by default, but see below. */
6516 it->stop_charpos = -1;
6517
6518 /* Set iterator position and end position. */
6519 memset (&it->current, 0, sizeof it->current);
6520 it->current.overlay_string_index = -1;
6521 it->current.dpvec_index = -1;
6522 eassert (charpos >= 0);
6523
6524 /* If STRING is specified, use its multibyteness, otherwise use the
6525 setting of MULTIBYTE, if specified. */
6526 if (multibyte >= 0)
6527 it->multibyte_p = multibyte > 0;
6528
6529 /* Bidirectional reordering of strings is controlled by the default
6530 value of bidi-display-reordering. Don't try to reorder while
6531 loading loadup.el, as the necessary character property tables are
6532 not yet available. */
6533 it->bidi_p =
6534 NILP (Vpurify_flag)
6535 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6536
6537 if (s == NULL)
6538 {
6539 eassert (STRINGP (string));
6540 it->string = string;
6541 it->s = NULL;
6542 it->end_charpos = it->string_nchars = SCHARS (string);
6543 it->method = GET_FROM_STRING;
6544 it->current.string_pos = string_pos (charpos, string);
6545
6546 if (it->bidi_p)
6547 {
6548 it->bidi_it.string.lstring = string;
6549 it->bidi_it.string.s = NULL;
6550 it->bidi_it.string.schars = it->end_charpos;
6551 it->bidi_it.string.bufpos = 0;
6552 it->bidi_it.string.from_disp_str = 0;
6553 it->bidi_it.string.unibyte = !it->multibyte_p;
6554 it->bidi_it.w = it->w;
6555 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6556 FRAME_WINDOW_P (it->f), &it->bidi_it);
6557 }
6558 }
6559 else
6560 {
6561 it->s = (const unsigned char *) s;
6562 it->string = Qnil;
6563
6564 /* Note that we use IT->current.pos, not it->current.string_pos,
6565 for displaying C strings. */
6566 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6567 if (it->multibyte_p)
6568 {
6569 it->current.pos = c_string_pos (charpos, s, 1);
6570 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6571 }
6572 else
6573 {
6574 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6575 it->end_charpos = it->string_nchars = strlen (s);
6576 }
6577
6578 if (it->bidi_p)
6579 {
6580 it->bidi_it.string.lstring = Qnil;
6581 it->bidi_it.string.s = (const unsigned char *) s;
6582 it->bidi_it.string.schars = it->end_charpos;
6583 it->bidi_it.string.bufpos = 0;
6584 it->bidi_it.string.from_disp_str = 0;
6585 it->bidi_it.string.unibyte = !it->multibyte_p;
6586 it->bidi_it.w = it->w;
6587 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6588 &it->bidi_it);
6589 }
6590 it->method = GET_FROM_C_STRING;
6591 }
6592
6593 /* PRECISION > 0 means don't return more than PRECISION characters
6594 from the string. */
6595 if (precision > 0 && it->end_charpos - charpos > precision)
6596 {
6597 it->end_charpos = it->string_nchars = charpos + precision;
6598 if (it->bidi_p)
6599 it->bidi_it.string.schars = it->end_charpos;
6600 }
6601
6602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6604 FIELD_WIDTH < 0 means infinite field width. This is useful for
6605 padding with `-' at the end of a mode line. */
6606 if (field_width < 0)
6607 field_width = INFINITY;
6608 /* Implementation note: We deliberately don't enlarge
6609 it->bidi_it.string.schars here to fit it->end_charpos, because
6610 the bidi iterator cannot produce characters out of thin air. */
6611 if (field_width > it->end_charpos - charpos)
6612 it->end_charpos = charpos + field_width;
6613
6614 /* Use the standard display table for displaying strings. */
6615 if (DISP_TABLE_P (Vstandard_display_table))
6616 it->dp = XCHAR_TABLE (Vstandard_display_table);
6617
6618 it->stop_charpos = charpos;
6619 it->prev_stop = charpos;
6620 it->base_level_stop = 0;
6621 if (it->bidi_p)
6622 {
6623 it->bidi_it.first_elt = 1;
6624 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6625 it->bidi_it.disp_pos = -1;
6626 }
6627 if (s == NULL && it->multibyte_p)
6628 {
6629 ptrdiff_t endpos = SCHARS (it->string);
6630 if (endpos > it->end_charpos)
6631 endpos = it->end_charpos;
6632 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6633 it->string);
6634 }
6635 CHECK_IT (it);
6636 }
6637
6638
6639 \f
6640 /***********************************************************************
6641 Iteration
6642 ***********************************************************************/
6643
6644 /* Map enum it_method value to corresponding next_element_from_* function. */
6645
6646 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6647 {
6648 next_element_from_buffer,
6649 next_element_from_display_vector,
6650 next_element_from_string,
6651 next_element_from_c_string,
6652 next_element_from_image,
6653 next_element_from_stretch
6654 };
6655
6656 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6657
6658
6659 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6660 (possibly with the following characters). */
6661
6662 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6663 ((IT)->cmp_it.id >= 0 \
6664 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6665 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6666 END_CHARPOS, (IT)->w, \
6667 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6668 (IT)->string)))
6669
6670
6671 /* Lookup the char-table Vglyphless_char_display for character C (-1
6672 if we want information for no-font case), and return the display
6673 method symbol. By side-effect, update it->what and
6674 it->glyphless_method. This function is called from
6675 get_next_display_element for each character element, and from
6676 x_produce_glyphs when no suitable font was found. */
6677
6678 Lisp_Object
6679 lookup_glyphless_char_display (int c, struct it *it)
6680 {
6681 Lisp_Object glyphless_method = Qnil;
6682
6683 if (CHAR_TABLE_P (Vglyphless_char_display)
6684 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6685 {
6686 if (c >= 0)
6687 {
6688 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6689 if (CONSP (glyphless_method))
6690 glyphless_method = FRAME_WINDOW_P (it->f)
6691 ? XCAR (glyphless_method)
6692 : XCDR (glyphless_method);
6693 }
6694 else
6695 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6696 }
6697
6698 retry:
6699 if (NILP (glyphless_method))
6700 {
6701 if (c >= 0)
6702 /* The default is to display the character by a proper font. */
6703 return Qnil;
6704 /* The default for the no-font case is to display an empty box. */
6705 glyphless_method = Qempty_box;
6706 }
6707 if (EQ (glyphless_method, Qzero_width))
6708 {
6709 if (c >= 0)
6710 return glyphless_method;
6711 /* This method can't be used for the no-font case. */
6712 glyphless_method = Qempty_box;
6713 }
6714 if (EQ (glyphless_method, Qthin_space))
6715 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6716 else if (EQ (glyphless_method, Qempty_box))
6717 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6718 else if (EQ (glyphless_method, Qhex_code))
6719 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6720 else if (STRINGP (glyphless_method))
6721 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6722 else
6723 {
6724 /* Invalid value. We use the default method. */
6725 glyphless_method = Qnil;
6726 goto retry;
6727 }
6728 it->what = IT_GLYPHLESS;
6729 return glyphless_method;
6730 }
6731
6732 /* Merge escape glyph face and cache the result. */
6733
6734 static struct frame *last_escape_glyph_frame = NULL;
6735 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6736 static int last_escape_glyph_merged_face_id = 0;
6737
6738 static int
6739 merge_escape_glyph_face (struct it *it)
6740 {
6741 int face_id;
6742
6743 if (it->f == last_escape_glyph_frame
6744 && it->face_id == last_escape_glyph_face_id)
6745 face_id = last_escape_glyph_merged_face_id;
6746 else
6747 {
6748 /* Merge the `escape-glyph' face into the current face. */
6749 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6753 }
6754 return face_id;
6755 }
6756
6757 /* Likewise for glyphless glyph face. */
6758
6759 static struct frame *last_glyphless_glyph_frame = NULL;
6760 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6761 static int last_glyphless_glyph_merged_face_id = 0;
6762
6763 int
6764 merge_glyphless_glyph_face (struct it *it)
6765 {
6766 int face_id;
6767
6768 if (it->f == last_glyphless_glyph_frame
6769 && it->face_id == last_glyphless_glyph_face_id)
6770 face_id = last_glyphless_glyph_merged_face_id;
6771 else
6772 {
6773 /* Merge the `glyphless-char' face into the current face. */
6774 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6775 last_glyphless_glyph_frame = it->f;
6776 last_glyphless_glyph_face_id = it->face_id;
6777 last_glyphless_glyph_merged_face_id = face_id;
6778 }
6779 return face_id;
6780 }
6781
6782 /* Load IT's display element fields with information about the next
6783 display element from the current position of IT. Value is zero if
6784 end of buffer (or C string) is reached. */
6785
6786 static int
6787 get_next_display_element (struct it *it)
6788 {
6789 /* Non-zero means that we found a display element. Zero means that
6790 we hit the end of what we iterate over. Performance note: the
6791 function pointer `method' used here turns out to be faster than
6792 using a sequence of if-statements. */
6793 int success_p;
6794
6795 get_next:
6796 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6797
6798 if (it->what == IT_CHARACTER)
6799 {
6800 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6801 and only if (a) the resolved directionality of that character
6802 is R..." */
6803 /* FIXME: Do we need an exception for characters from display
6804 tables? */
6805 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6806 it->c = bidi_mirror_char (it->c);
6807 /* Map via display table or translate control characters.
6808 IT->c, IT->len etc. have been set to the next character by
6809 the function call above. If we have a display table, and it
6810 contains an entry for IT->c, translate it. Don't do this if
6811 IT->c itself comes from a display table, otherwise we could
6812 end up in an infinite recursion. (An alternative could be to
6813 count the recursion depth of this function and signal an
6814 error when a certain maximum depth is reached.) Is it worth
6815 it? */
6816 if (success_p && it->dpvec == NULL)
6817 {
6818 Lisp_Object dv;
6819 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6820 int nonascii_space_p = 0;
6821 int nonascii_hyphen_p = 0;
6822 int c = it->c; /* This is the character to display. */
6823
6824 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6825 {
6826 eassert (SINGLE_BYTE_CHAR_P (c));
6827 if (unibyte_display_via_language_environment)
6828 {
6829 c = DECODE_CHAR (unibyte, c);
6830 if (c < 0)
6831 c = BYTE8_TO_CHAR (it->c);
6832 }
6833 else
6834 c = BYTE8_TO_CHAR (it->c);
6835 }
6836
6837 if (it->dp
6838 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6839 VECTORP (dv)))
6840 {
6841 struct Lisp_Vector *v = XVECTOR (dv);
6842
6843 /* Return the first character from the display table
6844 entry, if not empty. If empty, don't display the
6845 current character. */
6846 if (v->header.size)
6847 {
6848 it->dpvec_char_len = it->len;
6849 it->dpvec = v->contents;
6850 it->dpend = v->contents + v->header.size;
6851 it->current.dpvec_index = 0;
6852 it->dpvec_face_id = -1;
6853 it->saved_face_id = it->face_id;
6854 it->method = GET_FROM_DISPLAY_VECTOR;
6855 it->ellipsis_p = 0;
6856 }
6857 else
6858 {
6859 set_iterator_to_next (it, 0);
6860 }
6861 goto get_next;
6862 }
6863
6864 if (! NILP (lookup_glyphless_char_display (c, it)))
6865 {
6866 if (it->what == IT_GLYPHLESS)
6867 goto done;
6868 /* Don't display this character. */
6869 set_iterator_to_next (it, 0);
6870 goto get_next;
6871 }
6872
6873 /* If `nobreak-char-display' is non-nil, we display
6874 non-ASCII spaces and hyphens specially. */
6875 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6876 {
6877 if (c == 0xA0)
6878 nonascii_space_p = true;
6879 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6880 nonascii_hyphen_p = true;
6881 }
6882
6883 /* Translate control characters into `\003' or `^C' form.
6884 Control characters coming from a display table entry are
6885 currently not translated because we use IT->dpvec to hold
6886 the translation. This could easily be changed but I
6887 don't believe that it is worth doing.
6888
6889 The characters handled by `nobreak-char-display' must be
6890 translated too.
6891
6892 Non-printable characters and raw-byte characters are also
6893 translated to octal form. */
6894 if (((c < ' ' || c == 127) /* ASCII control chars. */
6895 ? (it->area != TEXT_AREA
6896 /* In mode line, treat \n, \t like other crl chars. */
6897 || (c != '\t'
6898 && it->glyph_row
6899 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6900 || (c != '\n' && c != '\t'))
6901 : (nonascii_space_p
6902 || nonascii_hyphen_p
6903 || CHAR_BYTE8_P (c)
6904 || ! CHAR_PRINTABLE_P (c))))
6905 {
6906 /* C is a control character, non-ASCII space/hyphen,
6907 raw-byte, or a non-printable character which must be
6908 displayed either as '\003' or as `^C' where the '\\'
6909 and '^' can be defined in the display table. Fill
6910 IT->ctl_chars with glyphs for what we have to
6911 display. Then, set IT->dpvec to these glyphs. */
6912 Lisp_Object gc;
6913 int ctl_len;
6914 int face_id;
6915 int lface_id = 0;
6916 int escape_glyph;
6917
6918 /* Handle control characters with ^. */
6919
6920 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6921 {
6922 int g;
6923
6924 g = '^'; /* default glyph for Control */
6925 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6926 if (it->dp
6927 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6928 {
6929 g = GLYPH_CODE_CHAR (gc);
6930 lface_id = GLYPH_CODE_FACE (gc);
6931 }
6932
6933 face_id = (lface_id
6934 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6935 : merge_escape_glyph_face (it));
6936
6937 XSETINT (it->ctl_chars[0], g);
6938 XSETINT (it->ctl_chars[1], c ^ 0100);
6939 ctl_len = 2;
6940 goto display_control;
6941 }
6942
6943 /* Handle non-ascii space in the mode where it only gets
6944 highlighting. */
6945
6946 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6947 {
6948 /* Merge `nobreak-space' into the current face. */
6949 face_id = merge_faces (it->f, Qnobreak_space, 0,
6950 it->face_id);
6951 XSETINT (it->ctl_chars[0], ' ');
6952 ctl_len = 1;
6953 goto display_control;
6954 }
6955
6956 /* Handle sequences that start with the "escape glyph". */
6957
6958 /* the default escape glyph is \. */
6959 escape_glyph = '\\';
6960
6961 if (it->dp
6962 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6963 {
6964 escape_glyph = GLYPH_CODE_CHAR (gc);
6965 lface_id = GLYPH_CODE_FACE (gc);
6966 }
6967
6968 face_id = (lface_id
6969 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6970 : merge_escape_glyph_face (it));
6971
6972 /* Draw non-ASCII hyphen with just highlighting: */
6973
6974 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6975 {
6976 XSETINT (it->ctl_chars[0], '-');
6977 ctl_len = 1;
6978 goto display_control;
6979 }
6980
6981 /* Draw non-ASCII space/hyphen with escape glyph: */
6982
6983 if (nonascii_space_p || nonascii_hyphen_p)
6984 {
6985 XSETINT (it->ctl_chars[0], escape_glyph);
6986 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6987 ctl_len = 2;
6988 goto display_control;
6989 }
6990
6991 {
6992 char str[10];
6993 int len, i;
6994
6995 if (CHAR_BYTE8_P (c))
6996 /* Display \200 instead of \17777600. */
6997 c = CHAR_TO_BYTE8 (c);
6998 len = sprintf (str, "%03o", c);
6999
7000 XSETINT (it->ctl_chars[0], escape_glyph);
7001 for (i = 0; i < len; i++)
7002 XSETINT (it->ctl_chars[i + 1], str[i]);
7003 ctl_len = len + 1;
7004 }
7005
7006 display_control:
7007 /* Set up IT->dpvec and return first character from it. */
7008 it->dpvec_char_len = it->len;
7009 it->dpvec = it->ctl_chars;
7010 it->dpend = it->dpvec + ctl_len;
7011 it->current.dpvec_index = 0;
7012 it->dpvec_face_id = face_id;
7013 it->saved_face_id = it->face_id;
7014 it->method = GET_FROM_DISPLAY_VECTOR;
7015 it->ellipsis_p = 0;
7016 goto get_next;
7017 }
7018 it->char_to_display = c;
7019 }
7020 else if (success_p)
7021 {
7022 it->char_to_display = it->c;
7023 }
7024 }
7025
7026 #ifdef HAVE_WINDOW_SYSTEM
7027 /* Adjust face id for a multibyte character. There are no multibyte
7028 character in unibyte text. */
7029 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7030 && it->multibyte_p
7031 && success_p
7032 && FRAME_WINDOW_P (it->f))
7033 {
7034 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7035
7036 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7037 {
7038 /* Automatic composition with glyph-string. */
7039 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7040
7041 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7042 }
7043 else
7044 {
7045 ptrdiff_t pos = (it->s ? -1
7046 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7047 : IT_CHARPOS (*it));
7048 int c;
7049
7050 if (it->what == IT_CHARACTER)
7051 c = it->char_to_display;
7052 else
7053 {
7054 struct composition *cmp = composition_table[it->cmp_it.id];
7055 int i;
7056
7057 c = ' ';
7058 for (i = 0; i < cmp->glyph_len; i++)
7059 /* TAB in a composition means display glyphs with
7060 padding space on the left or right. */
7061 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7062 break;
7063 }
7064 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7065 }
7066 }
7067 #endif /* HAVE_WINDOW_SYSTEM */
7068
7069 done:
7070 /* Is this character the last one of a run of characters with
7071 box? If yes, set IT->end_of_box_run_p to 1. */
7072 if (it->face_box_p
7073 && it->s == NULL)
7074 {
7075 if (it->method == GET_FROM_STRING && it->sp)
7076 {
7077 int face_id = underlying_face_id (it);
7078 struct face *face = FACE_FROM_ID (it->f, face_id);
7079
7080 if (face)
7081 {
7082 if (face->box == FACE_NO_BOX)
7083 {
7084 /* If the box comes from face properties in a
7085 display string, check faces in that string. */
7086 int string_face_id = face_after_it_pos (it);
7087 it->end_of_box_run_p
7088 = (FACE_FROM_ID (it->f, string_face_id)->box
7089 == FACE_NO_BOX);
7090 }
7091 /* Otherwise, the box comes from the underlying face.
7092 If this is the last string character displayed, check
7093 the next buffer location. */
7094 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7095 /* n_overlay_strings is unreliable unless
7096 overlay_string_index is non-negative. */
7097 && ((it->current.overlay_string_index >= 0
7098 && (it->current.overlay_string_index
7099 == it->n_overlay_strings - 1))
7100 /* A string from display property. */
7101 || it->from_disp_prop_p))
7102 {
7103 ptrdiff_t ignore;
7104 int next_face_id;
7105 struct text_pos pos = it->current.pos;
7106
7107 /* For a string from a display property, the next
7108 buffer position is stored in the 'position'
7109 member of the iteration stack slot below the
7110 current one, see handle_single_display_spec. By
7111 contrast, it->current.pos was is not yet updated
7112 to point to that buffer position; that will
7113 happen in pop_it, after we finish displaying the
7114 current string. Note that we already checked
7115 above that it->sp is positive, so subtracting one
7116 from it is safe. */
7117 if (it->from_disp_prop_p)
7118 pos = (it->stack + it->sp - 1)->position;
7119 else
7120 INC_TEXT_POS (pos, it->multibyte_p);
7121
7122 if (CHARPOS (pos) >= ZV)
7123 it->end_of_box_run_p = true;
7124 else
7125 {
7126 next_face_id = face_at_buffer_position
7127 (it->w, CHARPOS (pos), &ignore,
7128 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7129 it->end_of_box_run_p
7130 = (FACE_FROM_ID (it->f, next_face_id)->box
7131 == FACE_NO_BOX);
7132 }
7133 }
7134 }
7135 }
7136 /* next_element_from_display_vector sets this flag according to
7137 faces of the display vector glyphs, see there. */
7138 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7139 {
7140 int face_id = face_after_it_pos (it);
7141 it->end_of_box_run_p
7142 = (face_id != it->face_id
7143 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7144 }
7145 }
7146 /* If we reached the end of the object we've been iterating (e.g., a
7147 display string or an overlay string), and there's something on
7148 IT->stack, proceed with what's on the stack. It doesn't make
7149 sense to return zero if there's unprocessed stuff on the stack,
7150 because otherwise that stuff will never be displayed. */
7151 if (!success_p && it->sp > 0)
7152 {
7153 set_iterator_to_next (it, 0);
7154 success_p = get_next_display_element (it);
7155 }
7156
7157 /* Value is 0 if end of buffer or string reached. */
7158 return success_p;
7159 }
7160
7161
7162 /* Move IT to the next display element.
7163
7164 RESEAT_P non-zero means if called on a newline in buffer text,
7165 skip to the next visible line start.
7166
7167 Functions get_next_display_element and set_iterator_to_next are
7168 separate because I find this arrangement easier to handle than a
7169 get_next_display_element function that also increments IT's
7170 position. The way it is we can first look at an iterator's current
7171 display element, decide whether it fits on a line, and if it does,
7172 increment the iterator position. The other way around we probably
7173 would either need a flag indicating whether the iterator has to be
7174 incremented the next time, or we would have to implement a
7175 decrement position function which would not be easy to write. */
7176
7177 void
7178 set_iterator_to_next (struct it *it, int reseat_p)
7179 {
7180 /* Reset flags indicating start and end of a sequence of characters
7181 with box. Reset them at the start of this function because
7182 moving the iterator to a new position might set them. */
7183 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7184
7185 switch (it->method)
7186 {
7187 case GET_FROM_BUFFER:
7188 /* The current display element of IT is a character from
7189 current_buffer. Advance in the buffer, and maybe skip over
7190 invisible lines that are so because of selective display. */
7191 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7192 reseat_at_next_visible_line_start (it, 0);
7193 else if (it->cmp_it.id >= 0)
7194 {
7195 /* We are currently getting glyphs from a composition. */
7196 int i;
7197
7198 if (! it->bidi_p)
7199 {
7200 IT_CHARPOS (*it) += it->cmp_it.nchars;
7201 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7202 if (it->cmp_it.to < it->cmp_it.nglyphs)
7203 {
7204 it->cmp_it.from = it->cmp_it.to;
7205 }
7206 else
7207 {
7208 it->cmp_it.id = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it),
7211 it->end_charpos, Qnil);
7212 }
7213 }
7214 else if (! it->cmp_it.reversed_p)
7215 {
7216 /* Composition created while scanning forward. */
7217 /* Update IT's char/byte positions to point to the first
7218 character of the next grapheme cluster, or to the
7219 character visually after the current composition. */
7220 for (i = 0; i < it->cmp_it.nchars; i++)
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7223 IT_CHARPOS (*it) = it->bidi_it.charpos;
7224
7225 if (it->cmp_it.to < it->cmp_it.nglyphs)
7226 {
7227 /* Proceed to the next grapheme cluster. */
7228 it->cmp_it.from = it->cmp_it.to;
7229 }
7230 else
7231 {
7232 /* No more grapheme clusters in this composition.
7233 Find the next stop position. */
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 /* Now we are scanning backward and don't know
7237 where to stop. */
7238 stop = -1;
7239 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7240 IT_BYTEPOS (*it), stop, Qnil);
7241 }
7242 }
7243 else
7244 {
7245 /* Composition created while scanning backward. */
7246 /* Update IT's char/byte positions to point to the last
7247 character of the previous grapheme cluster, or the
7248 character visually after the current composition. */
7249 for (i = 0; i < it->cmp_it.nchars; i++)
7250 bidi_move_to_visually_next (&it->bidi_it);
7251 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7252 IT_CHARPOS (*it) = it->bidi_it.charpos;
7253 if (it->cmp_it.from > 0)
7254 {
7255 /* Proceed to the previous grapheme cluster. */
7256 it->cmp_it.to = it->cmp_it.from;
7257 }
7258 else
7259 {
7260 /* No more grapheme clusters in this composition.
7261 Find the next stop position. */
7262 ptrdiff_t stop = it->end_charpos;
7263 if (it->bidi_it.scan_dir < 0)
7264 /* Now we are scanning backward and don't know
7265 where to stop. */
7266 stop = -1;
7267 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7268 IT_BYTEPOS (*it), stop, Qnil);
7269 }
7270 }
7271 }
7272 else
7273 {
7274 eassert (it->len != 0);
7275
7276 if (!it->bidi_p)
7277 {
7278 IT_BYTEPOS (*it) += it->len;
7279 IT_CHARPOS (*it) += 1;
7280 }
7281 else
7282 {
7283 int prev_scan_dir = it->bidi_it.scan_dir;
7284 /* If this is a new paragraph, determine its base
7285 direction (a.k.a. its base embedding level). */
7286 if (it->bidi_it.new_paragraph)
7287 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7288 bidi_move_to_visually_next (&it->bidi_it);
7289 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7290 IT_CHARPOS (*it) = it->bidi_it.charpos;
7291 if (prev_scan_dir != it->bidi_it.scan_dir)
7292 {
7293 /* As the scan direction was changed, we must
7294 re-compute the stop position for composition. */
7295 ptrdiff_t stop = it->end_charpos;
7296 if (it->bidi_it.scan_dir < 0)
7297 stop = -1;
7298 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7299 IT_BYTEPOS (*it), stop, Qnil);
7300 }
7301 }
7302 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7303 }
7304 break;
7305
7306 case GET_FROM_C_STRING:
7307 /* Current display element of IT is from a C string. */
7308 if (!it->bidi_p
7309 /* If the string position is beyond string's end, it means
7310 next_element_from_c_string is padding the string with
7311 blanks, in which case we bypass the bidi iterator,
7312 because it cannot deal with such virtual characters. */
7313 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7314 {
7315 IT_BYTEPOS (*it) += it->len;
7316 IT_CHARPOS (*it) += 1;
7317 }
7318 else
7319 {
7320 bidi_move_to_visually_next (&it->bidi_it);
7321 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7322 IT_CHARPOS (*it) = it->bidi_it.charpos;
7323 }
7324 break;
7325
7326 case GET_FROM_DISPLAY_VECTOR:
7327 /* Current display element of IT is from a display table entry.
7328 Advance in the display table definition. Reset it to null if
7329 end reached, and continue with characters from buffers/
7330 strings. */
7331 ++it->current.dpvec_index;
7332
7333 /* Restore face of the iterator to what they were before the
7334 display vector entry (these entries may contain faces). */
7335 it->face_id = it->saved_face_id;
7336
7337 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7338 {
7339 int recheck_faces = it->ellipsis_p;
7340
7341 if (it->s)
7342 it->method = GET_FROM_C_STRING;
7343 else if (STRINGP (it->string))
7344 it->method = GET_FROM_STRING;
7345 else
7346 {
7347 it->method = GET_FROM_BUFFER;
7348 it->object = it->w->contents;
7349 }
7350
7351 it->dpvec = NULL;
7352 it->current.dpvec_index = -1;
7353
7354 /* Skip over characters which were displayed via IT->dpvec. */
7355 if (it->dpvec_char_len < 0)
7356 reseat_at_next_visible_line_start (it, 1);
7357 else if (it->dpvec_char_len > 0)
7358 {
7359 if (it->method == GET_FROM_STRING
7360 && it->current.overlay_string_index >= 0
7361 && it->n_overlay_strings > 0)
7362 it->ignore_overlay_strings_at_pos_p = true;
7363 it->len = it->dpvec_char_len;
7364 set_iterator_to_next (it, reseat_p);
7365 }
7366
7367 /* Maybe recheck faces after display vector. */
7368 if (recheck_faces)
7369 it->stop_charpos = IT_CHARPOS (*it);
7370 }
7371 break;
7372
7373 case GET_FROM_STRING:
7374 /* Current display element is a character from a Lisp string. */
7375 eassert (it->s == NULL && STRINGP (it->string));
7376 /* Don't advance past string end. These conditions are true
7377 when set_iterator_to_next is called at the end of
7378 get_next_display_element, in which case the Lisp string is
7379 already exhausted, and all we want is pop the iterator
7380 stack. */
7381 if (it->current.overlay_string_index >= 0)
7382 {
7383 /* This is an overlay string, so there's no padding with
7384 spaces, and the number of characters in the string is
7385 where the string ends. */
7386 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7387 goto consider_string_end;
7388 }
7389 else
7390 {
7391 /* Not an overlay string. There could be padding, so test
7392 against it->end_charpos. */
7393 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7394 goto consider_string_end;
7395 }
7396 if (it->cmp_it.id >= 0)
7397 {
7398 int i;
7399
7400 if (! it->bidi_p)
7401 {
7402 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7403 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7404 if (it->cmp_it.to < it->cmp_it.nglyphs)
7405 it->cmp_it.from = it->cmp_it.to;
7406 else
7407 {
7408 it->cmp_it.id = -1;
7409 composition_compute_stop_pos (&it->cmp_it,
7410 IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it),
7412 it->end_charpos, it->string);
7413 }
7414 }
7415 else if (! it->cmp_it.reversed_p)
7416 {
7417 for (i = 0; i < it->cmp_it.nchars; i++)
7418 bidi_move_to_visually_next (&it->bidi_it);
7419 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7420 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7421
7422 if (it->cmp_it.to < it->cmp_it.nglyphs)
7423 it->cmp_it.from = it->cmp_it.to;
7424 else
7425 {
7426 ptrdiff_t stop = it->end_charpos;
7427 if (it->bidi_it.scan_dir < 0)
7428 stop = -1;
7429 composition_compute_stop_pos (&it->cmp_it,
7430 IT_STRING_CHARPOS (*it),
7431 IT_STRING_BYTEPOS (*it), stop,
7432 it->string);
7433 }
7434 }
7435 else
7436 {
7437 for (i = 0; i < it->cmp_it.nchars; i++)
7438 bidi_move_to_visually_next (&it->bidi_it);
7439 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7440 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7441 if (it->cmp_it.from > 0)
7442 it->cmp_it.to = it->cmp_it.from;
7443 else
7444 {
7445 ptrdiff_t stop = it->end_charpos;
7446 if (it->bidi_it.scan_dir < 0)
7447 stop = -1;
7448 composition_compute_stop_pos (&it->cmp_it,
7449 IT_STRING_CHARPOS (*it),
7450 IT_STRING_BYTEPOS (*it), stop,
7451 it->string);
7452 }
7453 }
7454 }
7455 else
7456 {
7457 if (!it->bidi_p
7458 /* If the string position is beyond string's end, it
7459 means next_element_from_string is padding the string
7460 with blanks, in which case we bypass the bidi
7461 iterator, because it cannot deal with such virtual
7462 characters. */
7463 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7464 {
7465 IT_STRING_BYTEPOS (*it) += it->len;
7466 IT_STRING_CHARPOS (*it) += 1;
7467 }
7468 else
7469 {
7470 int prev_scan_dir = it->bidi_it.scan_dir;
7471
7472 bidi_move_to_visually_next (&it->bidi_it);
7473 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7474 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7475 if (prev_scan_dir != it->bidi_it.scan_dir)
7476 {
7477 ptrdiff_t stop = it->end_charpos;
7478
7479 if (it->bidi_it.scan_dir < 0)
7480 stop = -1;
7481 composition_compute_stop_pos (&it->cmp_it,
7482 IT_STRING_CHARPOS (*it),
7483 IT_STRING_BYTEPOS (*it), stop,
7484 it->string);
7485 }
7486 }
7487 }
7488
7489 consider_string_end:
7490
7491 if (it->current.overlay_string_index >= 0)
7492 {
7493 /* IT->string is an overlay string. Advance to the
7494 next, if there is one. */
7495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7496 {
7497 it->ellipsis_p = 0;
7498 next_overlay_string (it);
7499 if (it->ellipsis_p)
7500 setup_for_ellipsis (it, 0);
7501 }
7502 }
7503 else
7504 {
7505 /* IT->string is not an overlay string. If we reached
7506 its end, and there is something on IT->stack, proceed
7507 with what is on the stack. This can be either another
7508 string, this time an overlay string, or a buffer. */
7509 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7510 && it->sp > 0)
7511 {
7512 pop_it (it);
7513 if (it->method == GET_FROM_STRING)
7514 goto consider_string_end;
7515 }
7516 }
7517 break;
7518
7519 case GET_FROM_IMAGE:
7520 case GET_FROM_STRETCH:
7521 /* The position etc with which we have to proceed are on
7522 the stack. The position may be at the end of a string,
7523 if the `display' property takes up the whole string. */
7524 eassert (it->sp > 0);
7525 pop_it (it);
7526 if (it->method == GET_FROM_STRING)
7527 goto consider_string_end;
7528 break;
7529
7530 default:
7531 /* There are no other methods defined, so this should be a bug. */
7532 emacs_abort ();
7533 }
7534
7535 eassert (it->method != GET_FROM_STRING
7536 || (STRINGP (it->string)
7537 && IT_STRING_CHARPOS (*it) >= 0));
7538 }
7539
7540 /* Load IT's display element fields with information about the next
7541 display element which comes from a display table entry or from the
7542 result of translating a control character to one of the forms `^C'
7543 or `\003'.
7544
7545 IT->dpvec holds the glyphs to return as characters.
7546 IT->saved_face_id holds the face id before the display vector--it
7547 is restored into IT->face_id in set_iterator_to_next. */
7548
7549 static int
7550 next_element_from_display_vector (struct it *it)
7551 {
7552 Lisp_Object gc;
7553 int prev_face_id = it->face_id;
7554 int next_face_id;
7555
7556 /* Precondition. */
7557 eassert (it->dpvec && it->current.dpvec_index >= 0);
7558
7559 it->face_id = it->saved_face_id;
7560
7561 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7562 That seemed totally bogus - so I changed it... */
7563 gc = it->dpvec[it->current.dpvec_index];
7564
7565 if (GLYPH_CODE_P (gc))
7566 {
7567 struct face *this_face, *prev_face, *next_face;
7568
7569 it->c = GLYPH_CODE_CHAR (gc);
7570 it->len = CHAR_BYTES (it->c);
7571
7572 /* The entry may contain a face id to use. Such a face id is
7573 the id of a Lisp face, not a realized face. A face id of
7574 zero means no face is specified. */
7575 if (it->dpvec_face_id >= 0)
7576 it->face_id = it->dpvec_face_id;
7577 else
7578 {
7579 int lface_id = GLYPH_CODE_FACE (gc);
7580 if (lface_id > 0)
7581 it->face_id = merge_faces (it->f, Qt, lface_id,
7582 it->saved_face_id);
7583 }
7584
7585 /* Glyphs in the display vector could have the box face, so we
7586 need to set the related flags in the iterator, as
7587 appropriate. */
7588 this_face = FACE_FROM_ID (it->f, it->face_id);
7589 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7590
7591 /* Is this character the first character of a box-face run? */
7592 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7593 && (!prev_face
7594 || prev_face->box == FACE_NO_BOX));
7595
7596 /* For the last character of the box-face run, we need to look
7597 either at the next glyph from the display vector, or at the
7598 face we saw before the display vector. */
7599 next_face_id = it->saved_face_id;
7600 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7601 {
7602 if (it->dpvec_face_id >= 0)
7603 next_face_id = it->dpvec_face_id;
7604 else
7605 {
7606 int lface_id =
7607 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7608
7609 if (lface_id > 0)
7610 next_face_id = merge_faces (it->f, Qt, lface_id,
7611 it->saved_face_id);
7612 }
7613 }
7614 next_face = FACE_FROM_ID (it->f, next_face_id);
7615 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7616 && (!next_face
7617 || next_face->box == FACE_NO_BOX));
7618 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7619 }
7620 else
7621 /* Display table entry is invalid. Return a space. */
7622 it->c = ' ', it->len = 1;
7623
7624 /* Don't change position and object of the iterator here. They are
7625 still the values of the character that had this display table
7626 entry or was translated, and that's what we want. */
7627 it->what = IT_CHARACTER;
7628 return 1;
7629 }
7630
7631 /* Get the first element of string/buffer in the visual order, after
7632 being reseated to a new position in a string or a buffer. */
7633 static void
7634 get_visually_first_element (struct it *it)
7635 {
7636 int string_p = STRINGP (it->string) || it->s;
7637 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7638 ptrdiff_t bob = (string_p ? 0 : BEGV);
7639
7640 if (STRINGP (it->string))
7641 {
7642 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7643 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7644 }
7645 else
7646 {
7647 it->bidi_it.charpos = IT_CHARPOS (*it);
7648 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7649 }
7650
7651 if (it->bidi_it.charpos == eob)
7652 {
7653 /* Nothing to do, but reset the FIRST_ELT flag, like
7654 bidi_paragraph_init does, because we are not going to
7655 call it. */
7656 it->bidi_it.first_elt = 0;
7657 }
7658 else if (it->bidi_it.charpos == bob
7659 || (!string_p
7660 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7661 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7662 {
7663 /* If we are at the beginning of a line/string, we can produce
7664 the next element right away. */
7665 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7666 bidi_move_to_visually_next (&it->bidi_it);
7667 }
7668 else
7669 {
7670 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7671
7672 /* We need to prime the bidi iterator starting at the line's or
7673 string's beginning, before we will be able to produce the
7674 next element. */
7675 if (string_p)
7676 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7677 else
7678 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7679 IT_BYTEPOS (*it), -1,
7680 &it->bidi_it.bytepos);
7681 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7682 do
7683 {
7684 /* Now return to buffer/string position where we were asked
7685 to get the next display element, and produce that. */
7686 bidi_move_to_visually_next (&it->bidi_it);
7687 }
7688 while (it->bidi_it.bytepos != orig_bytepos
7689 && it->bidi_it.charpos < eob);
7690 }
7691
7692 /* Adjust IT's position information to where we ended up. */
7693 if (STRINGP (it->string))
7694 {
7695 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7696 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7697 }
7698 else
7699 {
7700 IT_CHARPOS (*it) = it->bidi_it.charpos;
7701 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7702 }
7703
7704 if (STRINGP (it->string) || !it->s)
7705 {
7706 ptrdiff_t stop, charpos, bytepos;
7707
7708 if (STRINGP (it->string))
7709 {
7710 eassert (!it->s);
7711 stop = SCHARS (it->string);
7712 if (stop > it->end_charpos)
7713 stop = it->end_charpos;
7714 charpos = IT_STRING_CHARPOS (*it);
7715 bytepos = IT_STRING_BYTEPOS (*it);
7716 }
7717 else
7718 {
7719 stop = it->end_charpos;
7720 charpos = IT_CHARPOS (*it);
7721 bytepos = IT_BYTEPOS (*it);
7722 }
7723 if (it->bidi_it.scan_dir < 0)
7724 stop = -1;
7725 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7726 it->string);
7727 }
7728 }
7729
7730 /* Load IT with the next display element from Lisp string IT->string.
7731 IT->current.string_pos is the current position within the string.
7732 If IT->current.overlay_string_index >= 0, the Lisp string is an
7733 overlay string. */
7734
7735 static int
7736 next_element_from_string (struct it *it)
7737 {
7738 struct text_pos position;
7739
7740 eassert (STRINGP (it->string));
7741 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7742 eassert (IT_STRING_CHARPOS (*it) >= 0);
7743 position = it->current.string_pos;
7744
7745 /* With bidi reordering, the character to display might not be the
7746 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7747 that we were reseat()ed to a new string, whose paragraph
7748 direction is not known. */
7749 if (it->bidi_p && it->bidi_it.first_elt)
7750 {
7751 get_visually_first_element (it);
7752 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7753 }
7754
7755 /* Time to check for invisible text? */
7756 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7757 {
7758 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7759 {
7760 if (!(!it->bidi_p
7761 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7762 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7763 {
7764 /* With bidi non-linear iteration, we could find
7765 ourselves far beyond the last computed stop_charpos,
7766 with several other stop positions in between that we
7767 missed. Scan them all now, in buffer's logical
7768 order, until we find and handle the last stop_charpos
7769 that precedes our current position. */
7770 handle_stop_backwards (it, it->stop_charpos);
7771 return GET_NEXT_DISPLAY_ELEMENT (it);
7772 }
7773 else
7774 {
7775 if (it->bidi_p)
7776 {
7777 /* Take note of the stop position we just moved
7778 across, for when we will move back across it. */
7779 it->prev_stop = it->stop_charpos;
7780 /* If we are at base paragraph embedding level, take
7781 note of the last stop position seen at this
7782 level. */
7783 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7784 it->base_level_stop = it->stop_charpos;
7785 }
7786 handle_stop (it);
7787
7788 /* Since a handler may have changed IT->method, we must
7789 recurse here. */
7790 return GET_NEXT_DISPLAY_ELEMENT (it);
7791 }
7792 }
7793 else if (it->bidi_p
7794 /* If we are before prev_stop, we may have overstepped
7795 on our way backwards a stop_pos, and if so, we need
7796 to handle that stop_pos. */
7797 && IT_STRING_CHARPOS (*it) < it->prev_stop
7798 /* We can sometimes back up for reasons that have nothing
7799 to do with bidi reordering. E.g., compositions. The
7800 code below is only needed when we are above the base
7801 embedding level, so test for that explicitly. */
7802 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7803 {
7804 /* If we lost track of base_level_stop, we have no better
7805 place for handle_stop_backwards to start from than string
7806 beginning. This happens, e.g., when we were reseated to
7807 the previous screenful of text by vertical-motion. */
7808 if (it->base_level_stop <= 0
7809 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7810 it->base_level_stop = 0;
7811 handle_stop_backwards (it, it->base_level_stop);
7812 return GET_NEXT_DISPLAY_ELEMENT (it);
7813 }
7814 }
7815
7816 if (it->current.overlay_string_index >= 0)
7817 {
7818 /* Get the next character from an overlay string. In overlay
7819 strings, there is no field width or padding with spaces to
7820 do. */
7821 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7822 {
7823 it->what = IT_EOB;
7824 return 0;
7825 }
7826 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7827 IT_STRING_BYTEPOS (*it),
7828 it->bidi_it.scan_dir < 0
7829 ? -1
7830 : SCHARS (it->string))
7831 && next_element_from_composition (it))
7832 {
7833 return 1;
7834 }
7835 else if (STRING_MULTIBYTE (it->string))
7836 {
7837 const unsigned char *s = (SDATA (it->string)
7838 + IT_STRING_BYTEPOS (*it));
7839 it->c = string_char_and_length (s, &it->len);
7840 }
7841 else
7842 {
7843 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7844 it->len = 1;
7845 }
7846 }
7847 else
7848 {
7849 /* Get the next character from a Lisp string that is not an
7850 overlay string. Such strings come from the mode line, for
7851 example. We may have to pad with spaces, or truncate the
7852 string. See also next_element_from_c_string. */
7853 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7854 {
7855 it->what = IT_EOB;
7856 return 0;
7857 }
7858 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7859 {
7860 /* Pad with spaces. */
7861 it->c = ' ', it->len = 1;
7862 CHARPOS (position) = BYTEPOS (position) = -1;
7863 }
7864 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7865 IT_STRING_BYTEPOS (*it),
7866 it->bidi_it.scan_dir < 0
7867 ? -1
7868 : it->string_nchars)
7869 && next_element_from_composition (it))
7870 {
7871 return 1;
7872 }
7873 else if (STRING_MULTIBYTE (it->string))
7874 {
7875 const unsigned char *s = (SDATA (it->string)
7876 + IT_STRING_BYTEPOS (*it));
7877 it->c = string_char_and_length (s, &it->len);
7878 }
7879 else
7880 {
7881 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7882 it->len = 1;
7883 }
7884 }
7885
7886 /* Record what we have and where it came from. */
7887 it->what = IT_CHARACTER;
7888 it->object = it->string;
7889 it->position = position;
7890 return 1;
7891 }
7892
7893
7894 /* Load IT with next display element from C string IT->s.
7895 IT->string_nchars is the maximum number of characters to return
7896 from the string. IT->end_charpos may be greater than
7897 IT->string_nchars when this function is called, in which case we
7898 may have to return padding spaces. Value is zero if end of string
7899 reached, including padding spaces. */
7900
7901 static int
7902 next_element_from_c_string (struct it *it)
7903 {
7904 bool success_p = true;
7905
7906 eassert (it->s);
7907 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7908 it->what = IT_CHARACTER;
7909 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7910 it->object = Qnil;
7911
7912 /* With bidi reordering, the character to display might not be the
7913 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7914 we were reseated to a new string, whose paragraph direction is
7915 not known. */
7916 if (it->bidi_p && it->bidi_it.first_elt)
7917 get_visually_first_element (it);
7918
7919 /* IT's position can be greater than IT->string_nchars in case a
7920 field width or precision has been specified when the iterator was
7921 initialized. */
7922 if (IT_CHARPOS (*it) >= it->end_charpos)
7923 {
7924 /* End of the game. */
7925 it->what = IT_EOB;
7926 success_p = 0;
7927 }
7928 else if (IT_CHARPOS (*it) >= it->string_nchars)
7929 {
7930 /* Pad with spaces. */
7931 it->c = ' ', it->len = 1;
7932 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7933 }
7934 else if (it->multibyte_p)
7935 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7936 else
7937 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7938
7939 return success_p;
7940 }
7941
7942
7943 /* Set up IT to return characters from an ellipsis, if appropriate.
7944 The definition of the ellipsis glyphs may come from a display table
7945 entry. This function fills IT with the first glyph from the
7946 ellipsis if an ellipsis is to be displayed. */
7947
7948 static int
7949 next_element_from_ellipsis (struct it *it)
7950 {
7951 if (it->selective_display_ellipsis_p)
7952 setup_for_ellipsis (it, it->len);
7953 else
7954 {
7955 /* The face at the current position may be different from the
7956 face we find after the invisible text. Remember what it
7957 was in IT->saved_face_id, and signal that it's there by
7958 setting face_before_selective_p. */
7959 it->saved_face_id = it->face_id;
7960 it->method = GET_FROM_BUFFER;
7961 it->object = it->w->contents;
7962 reseat_at_next_visible_line_start (it, 1);
7963 it->face_before_selective_p = true;
7964 }
7965
7966 return GET_NEXT_DISPLAY_ELEMENT (it);
7967 }
7968
7969
7970 /* Deliver an image display element. The iterator IT is already
7971 filled with image information (done in handle_display_prop). Value
7972 is always 1. */
7973
7974
7975 static int
7976 next_element_from_image (struct it *it)
7977 {
7978 it->what = IT_IMAGE;
7979 it->ignore_overlay_strings_at_pos_p = 0;
7980 return 1;
7981 }
7982
7983
7984 /* Fill iterator IT with next display element from a stretch glyph
7985 property. IT->object is the value of the text property. Value is
7986 always 1. */
7987
7988 static int
7989 next_element_from_stretch (struct it *it)
7990 {
7991 it->what = IT_STRETCH;
7992 return 1;
7993 }
7994
7995 /* Scan backwards from IT's current position until we find a stop
7996 position, or until BEGV. This is called when we find ourself
7997 before both the last known prev_stop and base_level_stop while
7998 reordering bidirectional text. */
7999
8000 static void
8001 compute_stop_pos_backwards (struct it *it)
8002 {
8003 const int SCAN_BACK_LIMIT = 1000;
8004 struct text_pos pos;
8005 struct display_pos save_current = it->current;
8006 struct text_pos save_position = it->position;
8007 ptrdiff_t charpos = IT_CHARPOS (*it);
8008 ptrdiff_t where_we_are = charpos;
8009 ptrdiff_t save_stop_pos = it->stop_charpos;
8010 ptrdiff_t save_end_pos = it->end_charpos;
8011
8012 eassert (NILP (it->string) && !it->s);
8013 eassert (it->bidi_p);
8014 it->bidi_p = 0;
8015 do
8016 {
8017 it->end_charpos = min (charpos + 1, ZV);
8018 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8019 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8020 reseat_1 (it, pos, 0);
8021 compute_stop_pos (it);
8022 /* We must advance forward, right? */
8023 if (it->stop_charpos <= charpos)
8024 emacs_abort ();
8025 }
8026 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8027
8028 if (it->stop_charpos <= where_we_are)
8029 it->prev_stop = it->stop_charpos;
8030 else
8031 it->prev_stop = BEGV;
8032 it->bidi_p = true;
8033 it->current = save_current;
8034 it->position = save_position;
8035 it->stop_charpos = save_stop_pos;
8036 it->end_charpos = save_end_pos;
8037 }
8038
8039 /* Scan forward from CHARPOS in the current buffer/string, until we
8040 find a stop position > current IT's position. Then handle the stop
8041 position before that. This is called when we bump into a stop
8042 position while reordering bidirectional text. CHARPOS should be
8043 the last previously processed stop_pos (or BEGV/0, if none were
8044 processed yet) whose position is less that IT's current
8045 position. */
8046
8047 static void
8048 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8049 {
8050 int bufp = !STRINGP (it->string);
8051 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8052 struct display_pos save_current = it->current;
8053 struct text_pos save_position = it->position;
8054 struct text_pos pos1;
8055 ptrdiff_t next_stop;
8056
8057 /* Scan in strict logical order. */
8058 eassert (it->bidi_p);
8059 it->bidi_p = 0;
8060 do
8061 {
8062 it->prev_stop = charpos;
8063 if (bufp)
8064 {
8065 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8066 reseat_1 (it, pos1, 0);
8067 }
8068 else
8069 it->current.string_pos = string_pos (charpos, it->string);
8070 compute_stop_pos (it);
8071 /* We must advance forward, right? */
8072 if (it->stop_charpos <= it->prev_stop)
8073 emacs_abort ();
8074 charpos = it->stop_charpos;
8075 }
8076 while (charpos <= where_we_are);
8077
8078 it->bidi_p = true;
8079 it->current = save_current;
8080 it->position = save_position;
8081 next_stop = it->stop_charpos;
8082 it->stop_charpos = it->prev_stop;
8083 handle_stop (it);
8084 it->stop_charpos = next_stop;
8085 }
8086
8087 /* Load IT with the next display element from current_buffer. Value
8088 is zero if end of buffer reached. IT->stop_charpos is the next
8089 position at which to stop and check for text properties or buffer
8090 end. */
8091
8092 static int
8093 next_element_from_buffer (struct it *it)
8094 {
8095 bool success_p = true;
8096
8097 eassert (IT_CHARPOS (*it) >= BEGV);
8098 eassert (NILP (it->string) && !it->s);
8099 eassert (!it->bidi_p
8100 || (EQ (it->bidi_it.string.lstring, Qnil)
8101 && it->bidi_it.string.s == NULL));
8102
8103 /* With bidi reordering, the character to display might not be the
8104 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8105 we were reseat()ed to a new buffer position, which is potentially
8106 a different paragraph. */
8107 if (it->bidi_p && it->bidi_it.first_elt)
8108 {
8109 get_visually_first_element (it);
8110 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8111 }
8112
8113 if (IT_CHARPOS (*it) >= it->stop_charpos)
8114 {
8115 if (IT_CHARPOS (*it) >= it->end_charpos)
8116 {
8117 int overlay_strings_follow_p;
8118
8119 /* End of the game, except when overlay strings follow that
8120 haven't been returned yet. */
8121 if (it->overlay_strings_at_end_processed_p)
8122 overlay_strings_follow_p = 0;
8123 else
8124 {
8125 it->overlay_strings_at_end_processed_p = true;
8126 overlay_strings_follow_p = get_overlay_strings (it, 0);
8127 }
8128
8129 if (overlay_strings_follow_p)
8130 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8131 else
8132 {
8133 it->what = IT_EOB;
8134 it->position = it->current.pos;
8135 success_p = 0;
8136 }
8137 }
8138 else if (!(!it->bidi_p
8139 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8140 || IT_CHARPOS (*it) == it->stop_charpos))
8141 {
8142 /* With bidi non-linear iteration, we could find ourselves
8143 far beyond the last computed stop_charpos, with several
8144 other stop positions in between that we missed. Scan
8145 them all now, in buffer's logical order, until we find
8146 and handle the last stop_charpos that precedes our
8147 current position. */
8148 handle_stop_backwards (it, it->stop_charpos);
8149 return GET_NEXT_DISPLAY_ELEMENT (it);
8150 }
8151 else
8152 {
8153 if (it->bidi_p)
8154 {
8155 /* Take note of the stop position we just moved across,
8156 for when we will move back across it. */
8157 it->prev_stop = it->stop_charpos;
8158 /* If we are at base paragraph embedding level, take
8159 note of the last stop position seen at this
8160 level. */
8161 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8162 it->base_level_stop = it->stop_charpos;
8163 }
8164 handle_stop (it);
8165 return GET_NEXT_DISPLAY_ELEMENT (it);
8166 }
8167 }
8168 else if (it->bidi_p
8169 /* If we are before prev_stop, we may have overstepped on
8170 our way backwards a stop_pos, and if so, we need to
8171 handle that stop_pos. */
8172 && IT_CHARPOS (*it) < it->prev_stop
8173 /* We can sometimes back up for reasons that have nothing
8174 to do with bidi reordering. E.g., compositions. The
8175 code below is only needed when we are above the base
8176 embedding level, so test for that explicitly. */
8177 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8178 {
8179 if (it->base_level_stop <= 0
8180 || IT_CHARPOS (*it) < it->base_level_stop)
8181 {
8182 /* If we lost track of base_level_stop, we need to find
8183 prev_stop by looking backwards. This happens, e.g., when
8184 we were reseated to the previous screenful of text by
8185 vertical-motion. */
8186 it->base_level_stop = BEGV;
8187 compute_stop_pos_backwards (it);
8188 handle_stop_backwards (it, it->prev_stop);
8189 }
8190 else
8191 handle_stop_backwards (it, it->base_level_stop);
8192 return GET_NEXT_DISPLAY_ELEMENT (it);
8193 }
8194 else
8195 {
8196 /* No face changes, overlays etc. in sight, so just return a
8197 character from current_buffer. */
8198 unsigned char *p;
8199 ptrdiff_t stop;
8200
8201 /* Maybe run the redisplay end trigger hook. Performance note:
8202 This doesn't seem to cost measurable time. */
8203 if (it->redisplay_end_trigger_charpos
8204 && it->glyph_row
8205 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8206 run_redisplay_end_trigger_hook (it);
8207
8208 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8209 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8210 stop)
8211 && next_element_from_composition (it))
8212 {
8213 return 1;
8214 }
8215
8216 /* Get the next character, maybe multibyte. */
8217 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8218 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8219 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8220 else
8221 it->c = *p, it->len = 1;
8222
8223 /* Record what we have and where it came from. */
8224 it->what = IT_CHARACTER;
8225 it->object = it->w->contents;
8226 it->position = it->current.pos;
8227
8228 /* Normally we return the character found above, except when we
8229 really want to return an ellipsis for selective display. */
8230 if (it->selective)
8231 {
8232 if (it->c == '\n')
8233 {
8234 /* A value of selective > 0 means hide lines indented more
8235 than that number of columns. */
8236 if (it->selective > 0
8237 && IT_CHARPOS (*it) + 1 < ZV
8238 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8239 IT_BYTEPOS (*it) + 1,
8240 it->selective))
8241 {
8242 success_p = next_element_from_ellipsis (it);
8243 it->dpvec_char_len = -1;
8244 }
8245 }
8246 else if (it->c == '\r' && it->selective == -1)
8247 {
8248 /* A value of selective == -1 means that everything from the
8249 CR to the end of the line is invisible, with maybe an
8250 ellipsis displayed for it. */
8251 success_p = next_element_from_ellipsis (it);
8252 it->dpvec_char_len = -1;
8253 }
8254 }
8255 }
8256
8257 /* Value is zero if end of buffer reached. */
8258 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8259 return success_p;
8260 }
8261
8262
8263 /* Run the redisplay end trigger hook for IT. */
8264
8265 static void
8266 run_redisplay_end_trigger_hook (struct it *it)
8267 {
8268 Lisp_Object args[3];
8269
8270 /* IT->glyph_row should be non-null, i.e. we should be actually
8271 displaying something, or otherwise we should not run the hook. */
8272 eassert (it->glyph_row);
8273
8274 /* Set up hook arguments. */
8275 args[0] = Qredisplay_end_trigger_functions;
8276 args[1] = it->window;
8277 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8278 it->redisplay_end_trigger_charpos = 0;
8279
8280 /* Since we are *trying* to run these functions, don't try to run
8281 them again, even if they get an error. */
8282 wset_redisplay_end_trigger (it->w, Qnil);
8283 Frun_hook_with_args (3, args);
8284
8285 /* Notice if it changed the face of the character we are on. */
8286 handle_face_prop (it);
8287 }
8288
8289
8290 /* Deliver a composition display element. Unlike the other
8291 next_element_from_XXX, this function is not registered in the array
8292 get_next_element[]. It is called from next_element_from_buffer and
8293 next_element_from_string when necessary. */
8294
8295 static int
8296 next_element_from_composition (struct it *it)
8297 {
8298 it->what = IT_COMPOSITION;
8299 it->len = it->cmp_it.nbytes;
8300 if (STRINGP (it->string))
8301 {
8302 if (it->c < 0)
8303 {
8304 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8305 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8306 return 0;
8307 }
8308 it->position = it->current.string_pos;
8309 it->object = it->string;
8310 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8311 IT_STRING_BYTEPOS (*it), it->string);
8312 }
8313 else
8314 {
8315 if (it->c < 0)
8316 {
8317 IT_CHARPOS (*it) += it->cmp_it.nchars;
8318 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8319 if (it->bidi_p)
8320 {
8321 if (it->bidi_it.new_paragraph)
8322 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8323 /* Resync the bidi iterator with IT's new position.
8324 FIXME: this doesn't support bidirectional text. */
8325 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8326 bidi_move_to_visually_next (&it->bidi_it);
8327 }
8328 return 0;
8329 }
8330 it->position = it->current.pos;
8331 it->object = it->w->contents;
8332 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8333 IT_BYTEPOS (*it), Qnil);
8334 }
8335 return 1;
8336 }
8337
8338
8339 \f
8340 /***********************************************************************
8341 Moving an iterator without producing glyphs
8342 ***********************************************************************/
8343
8344 /* Check if iterator is at a position corresponding to a valid buffer
8345 position after some move_it_ call. */
8346
8347 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8348 ((it)->method == GET_FROM_STRING \
8349 ? IT_STRING_CHARPOS (*it) == 0 \
8350 : 1)
8351
8352
8353 /* Move iterator IT to a specified buffer or X position within one
8354 line on the display without producing glyphs.
8355
8356 OP should be a bit mask including some or all of these bits:
8357 MOVE_TO_X: Stop upon reaching x-position TO_X.
8358 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8359 Regardless of OP's value, stop upon reaching the end of the display line.
8360
8361 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8362 This means, in particular, that TO_X includes window's horizontal
8363 scroll amount.
8364
8365 The return value has several possible values that
8366 say what condition caused the scan to stop:
8367
8368 MOVE_POS_MATCH_OR_ZV
8369 - when TO_POS or ZV was reached.
8370
8371 MOVE_X_REACHED
8372 -when TO_X was reached before TO_POS or ZV were reached.
8373
8374 MOVE_LINE_CONTINUED
8375 - when we reached the end of the display area and the line must
8376 be continued.
8377
8378 MOVE_LINE_TRUNCATED
8379 - when we reached the end of the display area and the line is
8380 truncated.
8381
8382 MOVE_NEWLINE_OR_CR
8383 - when we stopped at a line end, i.e. a newline or a CR and selective
8384 display is on. */
8385
8386 static enum move_it_result
8387 move_it_in_display_line_to (struct it *it,
8388 ptrdiff_t to_charpos, int to_x,
8389 enum move_operation_enum op)
8390 {
8391 enum move_it_result result = MOVE_UNDEFINED;
8392 struct glyph_row *saved_glyph_row;
8393 struct it wrap_it, atpos_it, atx_it, ppos_it;
8394 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8395 void *ppos_data = NULL;
8396 int may_wrap = 0;
8397 enum it_method prev_method = it->method;
8398 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8399 int saw_smaller_pos = prev_pos < to_charpos;
8400
8401 /* Don't produce glyphs in produce_glyphs. */
8402 saved_glyph_row = it->glyph_row;
8403 it->glyph_row = NULL;
8404
8405 /* Use wrap_it to save a copy of IT wherever a word wrap could
8406 occur. Use atpos_it to save a copy of IT at the desired buffer
8407 position, if found, so that we can scan ahead and check if the
8408 word later overshoots the window edge. Use atx_it similarly, for
8409 pixel positions. */
8410 wrap_it.sp = -1;
8411 atpos_it.sp = -1;
8412 atx_it.sp = -1;
8413
8414 /* Use ppos_it under bidi reordering to save a copy of IT for the
8415 initial position. We restore that position in IT when we have
8416 scanned the entire display line without finding a match for
8417 TO_CHARPOS and all the character positions are greater than
8418 TO_CHARPOS. We then restart the scan from the initial position,
8419 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8420 the closest to TO_CHARPOS. */
8421 if (it->bidi_p)
8422 {
8423 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8424 {
8425 SAVE_IT (ppos_it, *it, ppos_data);
8426 closest_pos = IT_CHARPOS (*it);
8427 }
8428 else
8429 closest_pos = ZV;
8430 }
8431
8432 #define BUFFER_POS_REACHED_P() \
8433 ((op & MOVE_TO_POS) != 0 \
8434 && BUFFERP (it->object) \
8435 && (IT_CHARPOS (*it) == to_charpos \
8436 || ((!it->bidi_p \
8437 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8438 && IT_CHARPOS (*it) > to_charpos) \
8439 || (it->what == IT_COMPOSITION \
8440 && ((IT_CHARPOS (*it) > to_charpos \
8441 && to_charpos >= it->cmp_it.charpos) \
8442 || (IT_CHARPOS (*it) < to_charpos \
8443 && to_charpos <= it->cmp_it.charpos)))) \
8444 && (it->method == GET_FROM_BUFFER \
8445 || (it->method == GET_FROM_DISPLAY_VECTOR \
8446 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8447
8448 /* If there's a line-/wrap-prefix, handle it. */
8449 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8450 && it->current_y < it->last_visible_y)
8451 handle_line_prefix (it);
8452
8453 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8454 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8455
8456 while (1)
8457 {
8458 int x, i, ascent = 0, descent = 0;
8459
8460 /* Utility macro to reset an iterator with x, ascent, and descent. */
8461 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8462 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8463 (IT)->max_descent = descent)
8464
8465 /* Stop if we move beyond TO_CHARPOS (after an image or a
8466 display string or stretch glyph). */
8467 if ((op & MOVE_TO_POS) != 0
8468 && BUFFERP (it->object)
8469 && it->method == GET_FROM_BUFFER
8470 && (((!it->bidi_p
8471 /* When the iterator is at base embedding level, we
8472 are guaranteed that characters are delivered for
8473 display in strictly increasing order of their
8474 buffer positions. */
8475 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8476 && IT_CHARPOS (*it) > to_charpos)
8477 || (it->bidi_p
8478 && (prev_method == GET_FROM_IMAGE
8479 || prev_method == GET_FROM_STRETCH
8480 || prev_method == GET_FROM_STRING)
8481 /* Passed TO_CHARPOS from left to right. */
8482 && ((prev_pos < to_charpos
8483 && IT_CHARPOS (*it) > to_charpos)
8484 /* Passed TO_CHARPOS from right to left. */
8485 || (prev_pos > to_charpos
8486 && IT_CHARPOS (*it) < to_charpos)))))
8487 {
8488 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8489 {
8490 result = MOVE_POS_MATCH_OR_ZV;
8491 break;
8492 }
8493 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8494 /* If wrap_it is valid, the current position might be in a
8495 word that is wrapped. So, save the iterator in
8496 atpos_it and continue to see if wrapping happens. */
8497 SAVE_IT (atpos_it, *it, atpos_data);
8498 }
8499
8500 /* Stop when ZV reached.
8501 We used to stop here when TO_CHARPOS reached as well, but that is
8502 too soon if this glyph does not fit on this line. So we handle it
8503 explicitly below. */
8504 if (!get_next_display_element (it))
8505 {
8506 result = MOVE_POS_MATCH_OR_ZV;
8507 break;
8508 }
8509
8510 if (it->line_wrap == TRUNCATE)
8511 {
8512 if (BUFFER_POS_REACHED_P ())
8513 {
8514 result = MOVE_POS_MATCH_OR_ZV;
8515 break;
8516 }
8517 }
8518 else
8519 {
8520 if (it->line_wrap == WORD_WRAP)
8521 {
8522 if (IT_DISPLAYING_WHITESPACE (it))
8523 may_wrap = 1;
8524 else if (may_wrap)
8525 {
8526 /* We have reached a glyph that follows one or more
8527 whitespace characters. If the position is
8528 already found, we are done. */
8529 if (atpos_it.sp >= 0)
8530 {
8531 RESTORE_IT (it, &atpos_it, atpos_data);
8532 result = MOVE_POS_MATCH_OR_ZV;
8533 goto done;
8534 }
8535 if (atx_it.sp >= 0)
8536 {
8537 RESTORE_IT (it, &atx_it, atx_data);
8538 result = MOVE_X_REACHED;
8539 goto done;
8540 }
8541 /* Otherwise, we can wrap here. */
8542 SAVE_IT (wrap_it, *it, wrap_data);
8543 may_wrap = 0;
8544 }
8545 }
8546 }
8547
8548 /* Remember the line height for the current line, in case
8549 the next element doesn't fit on the line. */
8550 ascent = it->max_ascent;
8551 descent = it->max_descent;
8552
8553 /* The call to produce_glyphs will get the metrics of the
8554 display element IT is loaded with. Record the x-position
8555 before this display element, in case it doesn't fit on the
8556 line. */
8557 x = it->current_x;
8558
8559 PRODUCE_GLYPHS (it);
8560
8561 if (it->area != TEXT_AREA)
8562 {
8563 prev_method = it->method;
8564 if (it->method == GET_FROM_BUFFER)
8565 prev_pos = IT_CHARPOS (*it);
8566 set_iterator_to_next (it, 1);
8567 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8568 SET_TEXT_POS (this_line_min_pos,
8569 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8570 if (it->bidi_p
8571 && (op & MOVE_TO_POS)
8572 && IT_CHARPOS (*it) > to_charpos
8573 && IT_CHARPOS (*it) < closest_pos)
8574 closest_pos = IT_CHARPOS (*it);
8575 continue;
8576 }
8577
8578 /* The number of glyphs we get back in IT->nglyphs will normally
8579 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8580 character on a terminal frame, or (iii) a line end. For the
8581 second case, IT->nglyphs - 1 padding glyphs will be present.
8582 (On X frames, there is only one glyph produced for a
8583 composite character.)
8584
8585 The behavior implemented below means, for continuation lines,
8586 that as many spaces of a TAB as fit on the current line are
8587 displayed there. For terminal frames, as many glyphs of a
8588 multi-glyph character are displayed in the current line, too.
8589 This is what the old redisplay code did, and we keep it that
8590 way. Under X, the whole shape of a complex character must
8591 fit on the line or it will be completely displayed in the
8592 next line.
8593
8594 Note that both for tabs and padding glyphs, all glyphs have
8595 the same width. */
8596 if (it->nglyphs)
8597 {
8598 /* More than one glyph or glyph doesn't fit on line. All
8599 glyphs have the same width. */
8600 int single_glyph_width = it->pixel_width / it->nglyphs;
8601 int new_x;
8602 int x_before_this_char = x;
8603 int hpos_before_this_char = it->hpos;
8604
8605 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8606 {
8607 new_x = x + single_glyph_width;
8608
8609 /* We want to leave anything reaching TO_X to the caller. */
8610 if ((op & MOVE_TO_X) && new_x > to_x)
8611 {
8612 if (BUFFER_POS_REACHED_P ())
8613 {
8614 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8615 goto buffer_pos_reached;
8616 if (atpos_it.sp < 0)
8617 {
8618 SAVE_IT (atpos_it, *it, atpos_data);
8619 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8620 }
8621 }
8622 else
8623 {
8624 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8625 {
8626 it->current_x = x;
8627 result = MOVE_X_REACHED;
8628 break;
8629 }
8630 if (atx_it.sp < 0)
8631 {
8632 SAVE_IT (atx_it, *it, atx_data);
8633 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8634 }
8635 }
8636 }
8637
8638 if (/* Lines are continued. */
8639 it->line_wrap != TRUNCATE
8640 && (/* And glyph doesn't fit on the line. */
8641 new_x > it->last_visible_x
8642 /* Or it fits exactly and we're on a window
8643 system frame. */
8644 || (new_x == it->last_visible_x
8645 && FRAME_WINDOW_P (it->f)
8646 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8647 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8648 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8649 {
8650 if (/* IT->hpos == 0 means the very first glyph
8651 doesn't fit on the line, e.g. a wide image. */
8652 it->hpos == 0
8653 || (new_x == it->last_visible_x
8654 && FRAME_WINDOW_P (it->f)
8655 /* When word-wrap is ON and we have a valid
8656 wrap point, we don't allow the last glyph
8657 to "just barely fit" on the line. */
8658 && (it->line_wrap != WORD_WRAP
8659 || wrap_it.sp < 0)))
8660 {
8661 ++it->hpos;
8662 it->current_x = new_x;
8663
8664 /* The character's last glyph just barely fits
8665 in this row. */
8666 if (i == it->nglyphs - 1)
8667 {
8668 /* If this is the destination position,
8669 return a position *before* it in this row,
8670 now that we know it fits in this row. */
8671 if (BUFFER_POS_REACHED_P ())
8672 {
8673 if (it->line_wrap != WORD_WRAP
8674 || wrap_it.sp < 0)
8675 {
8676 it->hpos = hpos_before_this_char;
8677 it->current_x = x_before_this_char;
8678 result = MOVE_POS_MATCH_OR_ZV;
8679 break;
8680 }
8681 if (it->line_wrap == WORD_WRAP
8682 && atpos_it.sp < 0)
8683 {
8684 SAVE_IT (atpos_it, *it, atpos_data);
8685 atpos_it.current_x = x_before_this_char;
8686 atpos_it.hpos = hpos_before_this_char;
8687 }
8688 }
8689
8690 prev_method = it->method;
8691 if (it->method == GET_FROM_BUFFER)
8692 prev_pos = IT_CHARPOS (*it);
8693 set_iterator_to_next (it, 1);
8694 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8695 SET_TEXT_POS (this_line_min_pos,
8696 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8697 /* On graphical terminals, newlines may
8698 "overflow" into the fringe if
8699 overflow-newline-into-fringe is non-nil.
8700 On text terminals, and on graphical
8701 terminals with no right margin, newlines
8702 may overflow into the last glyph on the
8703 display line.*/
8704 if (!FRAME_WINDOW_P (it->f)
8705 || ((it->bidi_p
8706 && it->bidi_it.paragraph_dir == R2L)
8707 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8708 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8709 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8710 {
8711 if (!get_next_display_element (it))
8712 {
8713 result = MOVE_POS_MATCH_OR_ZV;
8714 break;
8715 }
8716 if (BUFFER_POS_REACHED_P ())
8717 {
8718 if (ITERATOR_AT_END_OF_LINE_P (it))
8719 result = MOVE_POS_MATCH_OR_ZV;
8720 else
8721 result = MOVE_LINE_CONTINUED;
8722 break;
8723 }
8724 if (ITERATOR_AT_END_OF_LINE_P (it)
8725 && (it->line_wrap != WORD_WRAP
8726 || wrap_it.sp < 0))
8727 {
8728 result = MOVE_NEWLINE_OR_CR;
8729 break;
8730 }
8731 }
8732 }
8733 }
8734 else
8735 IT_RESET_X_ASCENT_DESCENT (it);
8736
8737 if (wrap_it.sp >= 0)
8738 {
8739 RESTORE_IT (it, &wrap_it, wrap_data);
8740 atpos_it.sp = -1;
8741 atx_it.sp = -1;
8742 }
8743
8744 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8745 IT_CHARPOS (*it)));
8746 result = MOVE_LINE_CONTINUED;
8747 break;
8748 }
8749
8750 if (BUFFER_POS_REACHED_P ())
8751 {
8752 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8753 goto buffer_pos_reached;
8754 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8755 {
8756 SAVE_IT (atpos_it, *it, atpos_data);
8757 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8758 }
8759 }
8760
8761 if (new_x > it->first_visible_x)
8762 {
8763 /* Glyph is visible. Increment number of glyphs that
8764 would be displayed. */
8765 ++it->hpos;
8766 }
8767 }
8768
8769 if (result != MOVE_UNDEFINED)
8770 break;
8771 }
8772 else if (BUFFER_POS_REACHED_P ())
8773 {
8774 buffer_pos_reached:
8775 IT_RESET_X_ASCENT_DESCENT (it);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8778 }
8779 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8780 {
8781 /* Stop when TO_X specified and reached. This check is
8782 necessary here because of lines consisting of a line end,
8783 only. The line end will not produce any glyphs and we
8784 would never get MOVE_X_REACHED. */
8785 eassert (it->nglyphs == 0);
8786 result = MOVE_X_REACHED;
8787 break;
8788 }
8789
8790 /* Is this a line end? If yes, we're done. */
8791 if (ITERATOR_AT_END_OF_LINE_P (it))
8792 {
8793 /* If we are past TO_CHARPOS, but never saw any character
8794 positions smaller than TO_CHARPOS, return
8795 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8796 did. */
8797 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8798 {
8799 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8800 {
8801 if (closest_pos < ZV)
8802 {
8803 RESTORE_IT (it, &ppos_it, ppos_data);
8804 move_it_in_display_line_to (it, closest_pos, -1,
8805 MOVE_TO_POS);
8806 result = MOVE_POS_MATCH_OR_ZV;
8807 }
8808 else
8809 goto buffer_pos_reached;
8810 }
8811 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8812 && IT_CHARPOS (*it) > to_charpos)
8813 goto buffer_pos_reached;
8814 else
8815 result = MOVE_NEWLINE_OR_CR;
8816 }
8817 else
8818 result = MOVE_NEWLINE_OR_CR;
8819 break;
8820 }
8821
8822 prev_method = it->method;
8823 if (it->method == GET_FROM_BUFFER)
8824 prev_pos = IT_CHARPOS (*it);
8825 /* The current display element has been consumed. Advance
8826 to the next. */
8827 set_iterator_to_next (it, 1);
8828 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8829 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8830 if (IT_CHARPOS (*it) < to_charpos)
8831 saw_smaller_pos = 1;
8832 if (it->bidi_p
8833 && (op & MOVE_TO_POS)
8834 && IT_CHARPOS (*it) >= to_charpos
8835 && IT_CHARPOS (*it) < closest_pos)
8836 closest_pos = IT_CHARPOS (*it);
8837
8838 /* Stop if lines are truncated and IT's current x-position is
8839 past the right edge of the window now. */
8840 if (it->line_wrap == TRUNCATE
8841 && it->current_x >= it->last_visible_x)
8842 {
8843 if (!FRAME_WINDOW_P (it->f)
8844 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8845 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8846 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8847 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8848 {
8849 int at_eob_p = 0;
8850
8851 if ((at_eob_p = !get_next_display_element (it))
8852 || BUFFER_POS_REACHED_P ()
8853 /* If we are past TO_CHARPOS, but never saw any
8854 character positions smaller than TO_CHARPOS,
8855 return MOVE_POS_MATCH_OR_ZV, like the
8856 unidirectional display did. */
8857 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8858 && !saw_smaller_pos
8859 && IT_CHARPOS (*it) > to_charpos))
8860 {
8861 if (it->bidi_p
8862 && !BUFFER_POS_REACHED_P ()
8863 && !at_eob_p && closest_pos < ZV)
8864 {
8865 RESTORE_IT (it, &ppos_it, ppos_data);
8866 move_it_in_display_line_to (it, closest_pos, -1,
8867 MOVE_TO_POS);
8868 }
8869 result = MOVE_POS_MATCH_OR_ZV;
8870 break;
8871 }
8872 if (ITERATOR_AT_END_OF_LINE_P (it))
8873 {
8874 result = MOVE_NEWLINE_OR_CR;
8875 break;
8876 }
8877 }
8878 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8879 && !saw_smaller_pos
8880 && IT_CHARPOS (*it) > to_charpos)
8881 {
8882 if (closest_pos < ZV)
8883 {
8884 RESTORE_IT (it, &ppos_it, ppos_data);
8885 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8886 }
8887 result = MOVE_POS_MATCH_OR_ZV;
8888 break;
8889 }
8890 result = MOVE_LINE_TRUNCATED;
8891 break;
8892 }
8893 #undef IT_RESET_X_ASCENT_DESCENT
8894 }
8895
8896 #undef BUFFER_POS_REACHED_P
8897
8898 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8899 restore the saved iterator. */
8900 if (atpos_it.sp >= 0)
8901 RESTORE_IT (it, &atpos_it, atpos_data);
8902 else if (atx_it.sp >= 0)
8903 RESTORE_IT (it, &atx_it, atx_data);
8904
8905 done:
8906
8907 if (atpos_data)
8908 bidi_unshelve_cache (atpos_data, 1);
8909 if (atx_data)
8910 bidi_unshelve_cache (atx_data, 1);
8911 if (wrap_data)
8912 bidi_unshelve_cache (wrap_data, 1);
8913 if (ppos_data)
8914 bidi_unshelve_cache (ppos_data, 1);
8915
8916 /* Restore the iterator settings altered at the beginning of this
8917 function. */
8918 it->glyph_row = saved_glyph_row;
8919 return result;
8920 }
8921
8922 /* For external use. */
8923 void
8924 move_it_in_display_line (struct it *it,
8925 ptrdiff_t to_charpos, int to_x,
8926 enum move_operation_enum op)
8927 {
8928 if (it->line_wrap == WORD_WRAP
8929 && (op & MOVE_TO_X))
8930 {
8931 struct it save_it;
8932 void *save_data = NULL;
8933 int skip;
8934
8935 SAVE_IT (save_it, *it, save_data);
8936 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8937 /* When word-wrap is on, TO_X may lie past the end
8938 of a wrapped line. Then it->current is the
8939 character on the next line, so backtrack to the
8940 space before the wrap point. */
8941 if (skip == MOVE_LINE_CONTINUED)
8942 {
8943 int prev_x = max (it->current_x - 1, 0);
8944 RESTORE_IT (it, &save_it, save_data);
8945 move_it_in_display_line_to
8946 (it, -1, prev_x, MOVE_TO_X);
8947 }
8948 else
8949 bidi_unshelve_cache (save_data, 1);
8950 }
8951 else
8952 move_it_in_display_line_to (it, to_charpos, to_x, op);
8953 }
8954
8955
8956 /* Move IT forward until it satisfies one or more of the criteria in
8957 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8958
8959 OP is a bit-mask that specifies where to stop, and in particular,
8960 which of those four position arguments makes a difference. See the
8961 description of enum move_operation_enum.
8962
8963 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8964 screen line, this function will set IT to the next position that is
8965 displayed to the right of TO_CHARPOS on the screen.
8966
8967 Return the maximum pixel length of any line scanned but never more
8968 than it.last_visible_x. */
8969
8970 int
8971 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8972 {
8973 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8974 int line_height, line_start_x = 0, reached = 0;
8975 int max_current_x = 0;
8976 void *backup_data = NULL;
8977
8978 for (;;)
8979 {
8980 if (op & MOVE_TO_VPOS)
8981 {
8982 /* If no TO_CHARPOS and no TO_X specified, stop at the
8983 start of the line TO_VPOS. */
8984 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8985 {
8986 if (it->vpos == to_vpos)
8987 {
8988 reached = 1;
8989 break;
8990 }
8991 else
8992 skip = move_it_in_display_line_to (it, -1, -1, 0);
8993 }
8994 else
8995 {
8996 /* TO_VPOS >= 0 means stop at TO_X in the line at
8997 TO_VPOS, or at TO_POS, whichever comes first. */
8998 if (it->vpos == to_vpos)
8999 {
9000 reached = 2;
9001 break;
9002 }
9003
9004 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9005
9006 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9007 {
9008 reached = 3;
9009 break;
9010 }
9011 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9012 {
9013 /* We have reached TO_X but not in the line we want. */
9014 skip = move_it_in_display_line_to (it, to_charpos,
9015 -1, MOVE_TO_POS);
9016 if (skip == MOVE_POS_MATCH_OR_ZV)
9017 {
9018 reached = 4;
9019 break;
9020 }
9021 }
9022 }
9023 }
9024 else if (op & MOVE_TO_Y)
9025 {
9026 struct it it_backup;
9027
9028 if (it->line_wrap == WORD_WRAP)
9029 SAVE_IT (it_backup, *it, backup_data);
9030
9031 /* TO_Y specified means stop at TO_X in the line containing
9032 TO_Y---or at TO_CHARPOS if this is reached first. The
9033 problem is that we can't really tell whether the line
9034 contains TO_Y before we have completely scanned it, and
9035 this may skip past TO_X. What we do is to first scan to
9036 TO_X.
9037
9038 If TO_X is not specified, use a TO_X of zero. The reason
9039 is to make the outcome of this function more predictable.
9040 If we didn't use TO_X == 0, we would stop at the end of
9041 the line which is probably not what a caller would expect
9042 to happen. */
9043 skip = move_it_in_display_line_to
9044 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9045 (MOVE_TO_X | (op & MOVE_TO_POS)));
9046
9047 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9048 if (skip == MOVE_POS_MATCH_OR_ZV)
9049 reached = 5;
9050 else if (skip == MOVE_X_REACHED)
9051 {
9052 /* If TO_X was reached, we want to know whether TO_Y is
9053 in the line. We know this is the case if the already
9054 scanned glyphs make the line tall enough. Otherwise,
9055 we must check by scanning the rest of the line. */
9056 line_height = it->max_ascent + it->max_descent;
9057 if (to_y >= it->current_y
9058 && to_y < it->current_y + line_height)
9059 {
9060 reached = 6;
9061 break;
9062 }
9063 SAVE_IT (it_backup, *it, backup_data);
9064 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9065 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9066 op & MOVE_TO_POS);
9067 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9068 line_height = it->max_ascent + it->max_descent;
9069 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9070
9071 if (to_y >= it->current_y
9072 && to_y < it->current_y + line_height)
9073 {
9074 /* If TO_Y is in this line and TO_X was reached
9075 above, we scanned too far. We have to restore
9076 IT's settings to the ones before skipping. But
9077 keep the more accurate values of max_ascent and
9078 max_descent we've found while skipping the rest
9079 of the line, for the sake of callers, such as
9080 pos_visible_p, that need to know the line
9081 height. */
9082 int max_ascent = it->max_ascent;
9083 int max_descent = it->max_descent;
9084
9085 RESTORE_IT (it, &it_backup, backup_data);
9086 it->max_ascent = max_ascent;
9087 it->max_descent = max_descent;
9088 reached = 6;
9089 }
9090 else
9091 {
9092 skip = skip2;
9093 if (skip == MOVE_POS_MATCH_OR_ZV)
9094 reached = 7;
9095 }
9096 }
9097 else
9098 {
9099 /* Check whether TO_Y is in this line. */
9100 line_height = it->max_ascent + it->max_descent;
9101 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9102
9103 if (to_y >= it->current_y
9104 && to_y < it->current_y + line_height)
9105 {
9106 if (to_y > it->current_y)
9107 max_current_x = max (it->current_x, max_current_x);
9108
9109 /* When word-wrap is on, TO_X may lie past the end
9110 of a wrapped line. Then it->current is the
9111 character on the next line, so backtrack to the
9112 space before the wrap point. */
9113 if (skip == MOVE_LINE_CONTINUED
9114 && it->line_wrap == WORD_WRAP)
9115 {
9116 int prev_x = max (it->current_x - 1, 0);
9117 RESTORE_IT (it, &it_backup, backup_data);
9118 skip = move_it_in_display_line_to
9119 (it, -1, prev_x, MOVE_TO_X);
9120 }
9121
9122 reached = 6;
9123 }
9124 }
9125
9126 if (reached)
9127 {
9128 max_current_x = max (it->current_x, max_current_x);
9129 break;
9130 }
9131 }
9132 else if (BUFFERP (it->object)
9133 && (it->method == GET_FROM_BUFFER
9134 || it->method == GET_FROM_STRETCH)
9135 && IT_CHARPOS (*it) >= to_charpos
9136 /* Under bidi iteration, a call to set_iterator_to_next
9137 can scan far beyond to_charpos if the initial
9138 portion of the next line needs to be reordered. In
9139 that case, give move_it_in_display_line_to another
9140 chance below. */
9141 && !(it->bidi_p
9142 && it->bidi_it.scan_dir == -1))
9143 skip = MOVE_POS_MATCH_OR_ZV;
9144 else
9145 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9146
9147 switch (skip)
9148 {
9149 case MOVE_POS_MATCH_OR_ZV:
9150 max_current_x = max (it->current_x, max_current_x);
9151 reached = 8;
9152 goto out;
9153
9154 case MOVE_NEWLINE_OR_CR:
9155 max_current_x = max (it->current_x, max_current_x);
9156 set_iterator_to_next (it, 1);
9157 it->continuation_lines_width = 0;
9158 break;
9159
9160 case MOVE_LINE_TRUNCATED:
9161 max_current_x = it->last_visible_x;
9162 it->continuation_lines_width = 0;
9163 reseat_at_next_visible_line_start (it, 0);
9164 if ((op & MOVE_TO_POS) != 0
9165 && IT_CHARPOS (*it) > to_charpos)
9166 {
9167 reached = 9;
9168 goto out;
9169 }
9170 break;
9171
9172 case MOVE_LINE_CONTINUED:
9173 max_current_x = it->last_visible_x;
9174 /* For continued lines ending in a tab, some of the glyphs
9175 associated with the tab are displayed on the current
9176 line. Since it->current_x does not include these glyphs,
9177 we use it->last_visible_x instead. */
9178 if (it->c == '\t')
9179 {
9180 it->continuation_lines_width += it->last_visible_x;
9181 /* When moving by vpos, ensure that the iterator really
9182 advances to the next line (bug#847, bug#969). Fixme:
9183 do we need to do this in other circumstances? */
9184 if (it->current_x != it->last_visible_x
9185 && (op & MOVE_TO_VPOS)
9186 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9187 {
9188 line_start_x = it->current_x + it->pixel_width
9189 - it->last_visible_x;
9190 set_iterator_to_next (it, 0);
9191 }
9192 }
9193 else
9194 it->continuation_lines_width += it->current_x;
9195 break;
9196
9197 default:
9198 emacs_abort ();
9199 }
9200
9201 /* Reset/increment for the next run. */
9202 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9203 it->current_x = line_start_x;
9204 line_start_x = 0;
9205 it->hpos = 0;
9206 it->current_y += it->max_ascent + it->max_descent;
9207 ++it->vpos;
9208 last_height = it->max_ascent + it->max_descent;
9209 it->max_ascent = it->max_descent = 0;
9210 }
9211
9212 out:
9213
9214 /* On text terminals, we may stop at the end of a line in the middle
9215 of a multi-character glyph. If the glyph itself is continued,
9216 i.e. it is actually displayed on the next line, don't treat this
9217 stopping point as valid; move to the next line instead (unless
9218 that brings us offscreen). */
9219 if (!FRAME_WINDOW_P (it->f)
9220 && op & MOVE_TO_POS
9221 && IT_CHARPOS (*it) == to_charpos
9222 && it->what == IT_CHARACTER
9223 && it->nglyphs > 1
9224 && it->line_wrap == WINDOW_WRAP
9225 && it->current_x == it->last_visible_x - 1
9226 && it->c != '\n'
9227 && it->c != '\t'
9228 && it->vpos < it->w->window_end_vpos)
9229 {
9230 it->continuation_lines_width += it->current_x;
9231 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9232 it->current_y += it->max_ascent + it->max_descent;
9233 ++it->vpos;
9234 last_height = it->max_ascent + it->max_descent;
9235 }
9236
9237 if (backup_data)
9238 bidi_unshelve_cache (backup_data, 1);
9239
9240 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9241
9242 return max_current_x;
9243 }
9244
9245
9246 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9247
9248 If DY > 0, move IT backward at least that many pixels. DY = 0
9249 means move IT backward to the preceding line start or BEGV. This
9250 function may move over more than DY pixels if IT->current_y - DY
9251 ends up in the middle of a line; in this case IT->current_y will be
9252 set to the top of the line moved to. */
9253
9254 void
9255 move_it_vertically_backward (struct it *it, int dy)
9256 {
9257 int nlines, h;
9258 struct it it2, it3;
9259 void *it2data = NULL, *it3data = NULL;
9260 ptrdiff_t start_pos;
9261 int nchars_per_row
9262 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9263 ptrdiff_t pos_limit;
9264
9265 move_further_back:
9266 eassert (dy >= 0);
9267
9268 start_pos = IT_CHARPOS (*it);
9269
9270 /* Estimate how many newlines we must move back. */
9271 nlines = max (1, dy / default_line_pixel_height (it->w));
9272 if (it->line_wrap == TRUNCATE)
9273 pos_limit = BEGV;
9274 else
9275 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9276
9277 /* Set the iterator's position that many lines back. But don't go
9278 back more than NLINES full screen lines -- this wins a day with
9279 buffers which have very long lines. */
9280 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9281 back_to_previous_visible_line_start (it);
9282
9283 /* Reseat the iterator here. When moving backward, we don't want
9284 reseat to skip forward over invisible text, set up the iterator
9285 to deliver from overlay strings at the new position etc. So,
9286 use reseat_1 here. */
9287 reseat_1 (it, it->current.pos, 1);
9288
9289 /* We are now surely at a line start. */
9290 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9291 reordering is in effect. */
9292 it->continuation_lines_width = 0;
9293
9294 /* Move forward and see what y-distance we moved. First move to the
9295 start of the next line so that we get its height. We need this
9296 height to be able to tell whether we reached the specified
9297 y-distance. */
9298 SAVE_IT (it2, *it, it2data);
9299 it2.max_ascent = it2.max_descent = 0;
9300 do
9301 {
9302 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9303 MOVE_TO_POS | MOVE_TO_VPOS);
9304 }
9305 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9306 /* If we are in a display string which starts at START_POS,
9307 and that display string includes a newline, and we are
9308 right after that newline (i.e. at the beginning of a
9309 display line), exit the loop, because otherwise we will
9310 infloop, since move_it_to will see that it is already at
9311 START_POS and will not move. */
9312 || (it2.method == GET_FROM_STRING
9313 && IT_CHARPOS (it2) == start_pos
9314 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9315 eassert (IT_CHARPOS (*it) >= BEGV);
9316 SAVE_IT (it3, it2, it3data);
9317
9318 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9319 eassert (IT_CHARPOS (*it) >= BEGV);
9320 /* H is the actual vertical distance from the position in *IT
9321 and the starting position. */
9322 h = it2.current_y - it->current_y;
9323 /* NLINES is the distance in number of lines. */
9324 nlines = it2.vpos - it->vpos;
9325
9326 /* Correct IT's y and vpos position
9327 so that they are relative to the starting point. */
9328 it->vpos -= nlines;
9329 it->current_y -= h;
9330
9331 if (dy == 0)
9332 {
9333 /* DY == 0 means move to the start of the screen line. The
9334 value of nlines is > 0 if continuation lines were involved,
9335 or if the original IT position was at start of a line. */
9336 RESTORE_IT (it, it, it2data);
9337 if (nlines > 0)
9338 move_it_by_lines (it, nlines);
9339 /* The above code moves us to some position NLINES down,
9340 usually to its first glyph (leftmost in an L2R line), but
9341 that's not necessarily the start of the line, under bidi
9342 reordering. We want to get to the character position
9343 that is immediately after the newline of the previous
9344 line. */
9345 if (it->bidi_p
9346 && !it->continuation_lines_width
9347 && !STRINGP (it->string)
9348 && IT_CHARPOS (*it) > BEGV
9349 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9350 {
9351 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9352
9353 DEC_BOTH (cp, bp);
9354 cp = find_newline_no_quit (cp, bp, -1, NULL);
9355 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9356 }
9357 bidi_unshelve_cache (it3data, 1);
9358 }
9359 else
9360 {
9361 /* The y-position we try to reach, relative to *IT.
9362 Note that H has been subtracted in front of the if-statement. */
9363 int target_y = it->current_y + h - dy;
9364 int y0 = it3.current_y;
9365 int y1;
9366 int line_height;
9367
9368 RESTORE_IT (&it3, &it3, it3data);
9369 y1 = line_bottom_y (&it3);
9370 line_height = y1 - y0;
9371 RESTORE_IT (it, it, it2data);
9372 /* If we did not reach target_y, try to move further backward if
9373 we can. If we moved too far backward, try to move forward. */
9374 if (target_y < it->current_y
9375 /* This is heuristic. In a window that's 3 lines high, with
9376 a line height of 13 pixels each, recentering with point
9377 on the bottom line will try to move -39/2 = 19 pixels
9378 backward. Try to avoid moving into the first line. */
9379 && (it->current_y - target_y
9380 > min (window_box_height (it->w), line_height * 2 / 3))
9381 && IT_CHARPOS (*it) > BEGV)
9382 {
9383 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9384 target_y - it->current_y));
9385 dy = it->current_y - target_y;
9386 goto move_further_back;
9387 }
9388 else if (target_y >= it->current_y + line_height
9389 && IT_CHARPOS (*it) < ZV)
9390 {
9391 /* Should move forward by at least one line, maybe more.
9392
9393 Note: Calling move_it_by_lines can be expensive on
9394 terminal frames, where compute_motion is used (via
9395 vmotion) to do the job, when there are very long lines
9396 and truncate-lines is nil. That's the reason for
9397 treating terminal frames specially here. */
9398
9399 if (!FRAME_WINDOW_P (it->f))
9400 move_it_vertically (it, target_y - (it->current_y + line_height));
9401 else
9402 {
9403 do
9404 {
9405 move_it_by_lines (it, 1);
9406 }
9407 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9408 }
9409 }
9410 }
9411 }
9412
9413
9414 /* Move IT by a specified amount of pixel lines DY. DY negative means
9415 move backwards. DY = 0 means move to start of screen line. At the
9416 end, IT will be on the start of a screen line. */
9417
9418 void
9419 move_it_vertically (struct it *it, int dy)
9420 {
9421 if (dy <= 0)
9422 move_it_vertically_backward (it, -dy);
9423 else
9424 {
9425 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9426 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9427 MOVE_TO_POS | MOVE_TO_Y);
9428 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9429
9430 /* If buffer ends in ZV without a newline, move to the start of
9431 the line to satisfy the post-condition. */
9432 if (IT_CHARPOS (*it) == ZV
9433 && ZV > BEGV
9434 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9435 move_it_by_lines (it, 0);
9436 }
9437 }
9438
9439
9440 /* Move iterator IT past the end of the text line it is in. */
9441
9442 void
9443 move_it_past_eol (struct it *it)
9444 {
9445 enum move_it_result rc;
9446
9447 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9448 if (rc == MOVE_NEWLINE_OR_CR)
9449 set_iterator_to_next (it, 0);
9450 }
9451
9452
9453 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9454 negative means move up. DVPOS == 0 means move to the start of the
9455 screen line.
9456
9457 Optimization idea: If we would know that IT->f doesn't use
9458 a face with proportional font, we could be faster for
9459 truncate-lines nil. */
9460
9461 void
9462 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9463 {
9464
9465 /* The commented-out optimization uses vmotion on terminals. This
9466 gives bad results, because elements like it->what, on which
9467 callers such as pos_visible_p rely, aren't updated. */
9468 /* struct position pos;
9469 if (!FRAME_WINDOW_P (it->f))
9470 {
9471 struct text_pos textpos;
9472
9473 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9474 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9475 reseat (it, textpos, 1);
9476 it->vpos += pos.vpos;
9477 it->current_y += pos.vpos;
9478 }
9479 else */
9480
9481 if (dvpos == 0)
9482 {
9483 /* DVPOS == 0 means move to the start of the screen line. */
9484 move_it_vertically_backward (it, 0);
9485 /* Let next call to line_bottom_y calculate real line height. */
9486 last_height = 0;
9487 }
9488 else if (dvpos > 0)
9489 {
9490 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9491 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9492 {
9493 /* Only move to the next buffer position if we ended up in a
9494 string from display property, not in an overlay string
9495 (before-string or after-string). That is because the
9496 latter don't conceal the underlying buffer position, so
9497 we can ask to move the iterator to the exact position we
9498 are interested in. Note that, even if we are already at
9499 IT_CHARPOS (*it), the call below is not a no-op, as it
9500 will detect that we are at the end of the string, pop the
9501 iterator, and compute it->current_x and it->hpos
9502 correctly. */
9503 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9504 -1, -1, -1, MOVE_TO_POS);
9505 }
9506 }
9507 else
9508 {
9509 struct it it2;
9510 void *it2data = NULL;
9511 ptrdiff_t start_charpos, i;
9512 int nchars_per_row
9513 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9514 ptrdiff_t pos_limit;
9515
9516 /* Start at the beginning of the screen line containing IT's
9517 position. This may actually move vertically backwards,
9518 in case of overlays, so adjust dvpos accordingly. */
9519 dvpos += it->vpos;
9520 move_it_vertically_backward (it, 0);
9521 dvpos -= it->vpos;
9522
9523 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9524 screen lines, and reseat the iterator there. */
9525 start_charpos = IT_CHARPOS (*it);
9526 if (it->line_wrap == TRUNCATE)
9527 pos_limit = BEGV;
9528 else
9529 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9530 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9531 back_to_previous_visible_line_start (it);
9532 reseat (it, it->current.pos, 1);
9533
9534 /* Move further back if we end up in a string or an image. */
9535 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9536 {
9537 /* First try to move to start of display line. */
9538 dvpos += it->vpos;
9539 move_it_vertically_backward (it, 0);
9540 dvpos -= it->vpos;
9541 if (IT_POS_VALID_AFTER_MOVE_P (it))
9542 break;
9543 /* If start of line is still in string or image,
9544 move further back. */
9545 back_to_previous_visible_line_start (it);
9546 reseat (it, it->current.pos, 1);
9547 dvpos--;
9548 }
9549
9550 it->current_x = it->hpos = 0;
9551
9552 /* Above call may have moved too far if continuation lines
9553 are involved. Scan forward and see if it did. */
9554 SAVE_IT (it2, *it, it2data);
9555 it2.vpos = it2.current_y = 0;
9556 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9557 it->vpos -= it2.vpos;
9558 it->current_y -= it2.current_y;
9559 it->current_x = it->hpos = 0;
9560
9561 /* If we moved too far back, move IT some lines forward. */
9562 if (it2.vpos > -dvpos)
9563 {
9564 int delta = it2.vpos + dvpos;
9565
9566 RESTORE_IT (&it2, &it2, it2data);
9567 SAVE_IT (it2, *it, it2data);
9568 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9569 /* Move back again if we got too far ahead. */
9570 if (IT_CHARPOS (*it) >= start_charpos)
9571 RESTORE_IT (it, &it2, it2data);
9572 else
9573 bidi_unshelve_cache (it2data, 1);
9574 }
9575 else
9576 RESTORE_IT (it, it, it2data);
9577 }
9578 }
9579
9580 /* Return true if IT points into the middle of a display vector. */
9581
9582 bool
9583 in_display_vector_p (struct it *it)
9584 {
9585 return (it->method == GET_FROM_DISPLAY_VECTOR
9586 && it->current.dpvec_index > 0
9587 && it->dpvec + it->current.dpvec_index != it->dpend);
9588 }
9589
9590 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9591 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9592 WINDOW must be a live window and defaults to the selected one. The
9593 return value is a cons of the maximum pixel-width of any text line and
9594 the maximum pixel-height of all text lines.
9595
9596 The optional argument FROM, if non-nil, specifies the first text
9597 position and defaults to the minimum accessible position of the buffer.
9598 If FROM is t, use the minimum accessible position that is not a newline
9599 character. TO, if non-nil, specifies the last text position and
9600 defaults to the maximum accessible position of the buffer. If TO is t,
9601 use the maximum accessible position that is not a newline character.
9602
9603 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9604 width that can be returned. X-LIMIT nil or omitted, means to use the
9605 pixel-width of WINDOW's body; use this if you do not intend to change
9606 the width of WINDOW. Use the maximum width WINDOW may assume if you
9607 intend to change WINDOW's width. In any case, text whose x-coordinate
9608 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9609 can take some time, it's always a good idea to make this argument as
9610 small as possible; in particular, if the buffer contains long lines that
9611 shall be truncated anyway.
9612
9613 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9614 height that can be returned. Text lines whose y-coordinate is beyond
9615 Y-LIMIT are ignored. Since calculating the text height of a large
9616 buffer can take some time, it makes sense to specify this argument if
9617 the size of the buffer is unknown.
9618
9619 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9620 include the height of the mode- or header-line of WINDOW in the return
9621 value. If it is either the symbol `mode-line' or `header-line', include
9622 only the height of that line, if present, in the return value. If t,
9623 include the height of both, if present, in the return value. */)
9624 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9625 Lisp_Object mode_and_header_line)
9626 {
9627 struct window *w = decode_live_window (window);
9628 Lisp_Object buf;
9629 struct buffer *b;
9630 struct it it;
9631 struct buffer *old_buffer = NULL;
9632 ptrdiff_t start, end, pos;
9633 struct text_pos startp;
9634 void *itdata = NULL;
9635 int c, max_y = -1, x = 0, y = 0;
9636
9637 buf = w->contents;
9638 CHECK_BUFFER (buf);
9639 b = XBUFFER (buf);
9640
9641 if (b != current_buffer)
9642 {
9643 old_buffer = current_buffer;
9644 set_buffer_internal (b);
9645 }
9646
9647 if (NILP (from))
9648 start = BEGV;
9649 else if (EQ (from, Qt))
9650 {
9651 start = pos = BEGV;
9652 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9653 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9654 start = pos;
9655 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9656 start = pos;
9657 }
9658 else
9659 {
9660 CHECK_NUMBER_COERCE_MARKER (from);
9661 start = min (max (XINT (from), BEGV), ZV);
9662 }
9663
9664 if (NILP (to))
9665 end = ZV;
9666 else if (EQ (to, Qt))
9667 {
9668 end = pos = ZV;
9669 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9670 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9671 end = pos;
9672 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9673 end = pos;
9674 }
9675 else
9676 {
9677 CHECK_NUMBER_COERCE_MARKER (to);
9678 end = max (start, min (XINT (to), ZV));
9679 }
9680
9681 if (!NILP (y_limit))
9682 {
9683 CHECK_NUMBER (y_limit);
9684 max_y = min (XINT (y_limit), INT_MAX);
9685 }
9686
9687 itdata = bidi_shelve_cache ();
9688 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9689 start_display (&it, w, startp);
9690
9691 if (NILP (x_limit))
9692 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9693 else
9694 {
9695 CHECK_NUMBER (x_limit);
9696 it.last_visible_x = min (XINT (x_limit), INFINITY);
9697 /* Actually, we never want move_it_to stop at to_x. But to make
9698 sure that move_it_in_display_line_to always moves far enough,
9699 we set it to INT_MAX and specify MOVE_TO_X. */
9700 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9701 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9702 }
9703
9704 y = it.current_y + it.max_ascent + it.max_descent;
9705
9706 if (!EQ (mode_and_header_line, Qheader_line)
9707 && !EQ (mode_and_header_line, Qt))
9708 /* Do not count the header-line which was counted automatically by
9709 start_display. */
9710 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9711
9712 if (EQ (mode_and_header_line, Qmode_line)
9713 || EQ (mode_and_header_line, Qt))
9714 /* Do count the mode-line which is not included automatically by
9715 start_display. */
9716 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9717
9718 bidi_unshelve_cache (itdata, 0);
9719
9720 if (old_buffer)
9721 set_buffer_internal (old_buffer);
9722
9723 return Fcons (make_number (x), make_number (y));
9724 }
9725 \f
9726 /***********************************************************************
9727 Messages
9728 ***********************************************************************/
9729
9730
9731 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9732 to *Messages*. */
9733
9734 void
9735 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9736 {
9737 Lisp_Object args[3];
9738 Lisp_Object msg, fmt;
9739 char *buffer;
9740 ptrdiff_t len;
9741 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9742 USE_SAFE_ALLOCA;
9743
9744 fmt = msg = Qnil;
9745 GCPRO4 (fmt, msg, arg1, arg2);
9746
9747 args[0] = fmt = build_string (format);
9748 args[1] = arg1;
9749 args[2] = arg2;
9750 msg = Fformat (3, args);
9751
9752 len = SBYTES (msg) + 1;
9753 buffer = SAFE_ALLOCA (len);
9754 memcpy (buffer, SDATA (msg), len);
9755
9756 message_dolog (buffer, len - 1, 1, 0);
9757 SAFE_FREE ();
9758
9759 UNGCPRO;
9760 }
9761
9762
9763 /* Output a newline in the *Messages* buffer if "needs" one. */
9764
9765 void
9766 message_log_maybe_newline (void)
9767 {
9768 if (message_log_need_newline)
9769 message_dolog ("", 0, 1, 0);
9770 }
9771
9772
9773 /* Add a string M of length NBYTES to the message log, optionally
9774 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9775 true, means interpret the contents of M as multibyte. This
9776 function calls low-level routines in order to bypass text property
9777 hooks, etc. which might not be safe to run.
9778
9779 This may GC (insert may run before/after change hooks),
9780 so the buffer M must NOT point to a Lisp string. */
9781
9782 void
9783 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9784 {
9785 const unsigned char *msg = (const unsigned char *) m;
9786
9787 if (!NILP (Vmemory_full))
9788 return;
9789
9790 if (!NILP (Vmessage_log_max))
9791 {
9792 struct buffer *oldbuf;
9793 Lisp_Object oldpoint, oldbegv, oldzv;
9794 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9795 ptrdiff_t point_at_end = 0;
9796 ptrdiff_t zv_at_end = 0;
9797 Lisp_Object old_deactivate_mark;
9798 struct gcpro gcpro1;
9799
9800 old_deactivate_mark = Vdeactivate_mark;
9801 oldbuf = current_buffer;
9802
9803 /* Ensure the Messages buffer exists, and switch to it.
9804 If we created it, set the major-mode. */
9805 {
9806 int newbuffer = 0;
9807 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9808
9809 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9810
9811 if (newbuffer
9812 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9813 call0 (intern ("messages-buffer-mode"));
9814 }
9815
9816 bset_undo_list (current_buffer, Qt);
9817 bset_cache_long_scans (current_buffer, Qnil);
9818
9819 oldpoint = message_dolog_marker1;
9820 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9821 oldbegv = message_dolog_marker2;
9822 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9823 oldzv = message_dolog_marker3;
9824 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9825 GCPRO1 (old_deactivate_mark);
9826
9827 if (PT == Z)
9828 point_at_end = 1;
9829 if (ZV == Z)
9830 zv_at_end = 1;
9831
9832 BEGV = BEG;
9833 BEGV_BYTE = BEG_BYTE;
9834 ZV = Z;
9835 ZV_BYTE = Z_BYTE;
9836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9837
9838 /* Insert the string--maybe converting multibyte to single byte
9839 or vice versa, so that all the text fits the buffer. */
9840 if (multibyte
9841 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9842 {
9843 ptrdiff_t i;
9844 int c, char_bytes;
9845 char work[1];
9846
9847 /* Convert a multibyte string to single-byte
9848 for the *Message* buffer. */
9849 for (i = 0; i < nbytes; i += char_bytes)
9850 {
9851 c = string_char_and_length (msg + i, &char_bytes);
9852 work[0] = (ASCII_CHAR_P (c)
9853 ? c
9854 : multibyte_char_to_unibyte (c));
9855 insert_1_both (work, 1, 1, 1, 0, 0);
9856 }
9857 }
9858 else if (! multibyte
9859 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9860 {
9861 ptrdiff_t i;
9862 int c, char_bytes;
9863 unsigned char str[MAX_MULTIBYTE_LENGTH];
9864 /* Convert a single-byte string to multibyte
9865 for the *Message* buffer. */
9866 for (i = 0; i < nbytes; i++)
9867 {
9868 c = msg[i];
9869 MAKE_CHAR_MULTIBYTE (c);
9870 char_bytes = CHAR_STRING (c, str);
9871 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9872 }
9873 }
9874 else if (nbytes)
9875 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9876
9877 if (nlflag)
9878 {
9879 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9880 printmax_t dups;
9881
9882 insert_1_both ("\n", 1, 1, 1, 0, 0);
9883
9884 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9885 this_bol = PT;
9886 this_bol_byte = PT_BYTE;
9887
9888 /* See if this line duplicates the previous one.
9889 If so, combine duplicates. */
9890 if (this_bol > BEG)
9891 {
9892 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9893 prev_bol = PT;
9894 prev_bol_byte = PT_BYTE;
9895
9896 dups = message_log_check_duplicate (prev_bol_byte,
9897 this_bol_byte);
9898 if (dups)
9899 {
9900 del_range_both (prev_bol, prev_bol_byte,
9901 this_bol, this_bol_byte, 0);
9902 if (dups > 1)
9903 {
9904 char dupstr[sizeof " [ times]"
9905 + INT_STRLEN_BOUND (printmax_t)];
9906
9907 /* If you change this format, don't forget to also
9908 change message_log_check_duplicate. */
9909 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9910 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9911 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9912 }
9913 }
9914 }
9915
9916 /* If we have more than the desired maximum number of lines
9917 in the *Messages* buffer now, delete the oldest ones.
9918 This is safe because we don't have undo in this buffer. */
9919
9920 if (NATNUMP (Vmessage_log_max))
9921 {
9922 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9923 -XFASTINT (Vmessage_log_max) - 1, 0);
9924 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9925 }
9926 }
9927 BEGV = marker_position (oldbegv);
9928 BEGV_BYTE = marker_byte_position (oldbegv);
9929
9930 if (zv_at_end)
9931 {
9932 ZV = Z;
9933 ZV_BYTE = Z_BYTE;
9934 }
9935 else
9936 {
9937 ZV = marker_position (oldzv);
9938 ZV_BYTE = marker_byte_position (oldzv);
9939 }
9940
9941 if (point_at_end)
9942 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9943 else
9944 /* We can't do Fgoto_char (oldpoint) because it will run some
9945 Lisp code. */
9946 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9947 marker_byte_position (oldpoint));
9948
9949 UNGCPRO;
9950 unchain_marker (XMARKER (oldpoint));
9951 unchain_marker (XMARKER (oldbegv));
9952 unchain_marker (XMARKER (oldzv));
9953
9954 /* We called insert_1_both above with its 5th argument (PREPARE)
9955 zero, which prevents insert_1_both from calling
9956 prepare_to_modify_buffer, which in turns prevents us from
9957 incrementing windows_or_buffers_changed even if *Messages* is
9958 shown in some window. So we must manually set
9959 windows_or_buffers_changed here to make up for that. */
9960 windows_or_buffers_changed = old_windows_or_buffers_changed;
9961 bset_redisplay (current_buffer);
9962
9963 set_buffer_internal (oldbuf);
9964
9965 message_log_need_newline = !nlflag;
9966 Vdeactivate_mark = old_deactivate_mark;
9967 }
9968 }
9969
9970
9971 /* We are at the end of the buffer after just having inserted a newline.
9972 (Note: We depend on the fact we won't be crossing the gap.)
9973 Check to see if the most recent message looks a lot like the previous one.
9974 Return 0 if different, 1 if the new one should just replace it, or a
9975 value N > 1 if we should also append " [N times]". */
9976
9977 static intmax_t
9978 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9979 {
9980 ptrdiff_t i;
9981 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9982 int seen_dots = 0;
9983 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9984 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9985
9986 for (i = 0; i < len; i++)
9987 {
9988 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9989 seen_dots = 1;
9990 if (p1[i] != p2[i])
9991 return seen_dots;
9992 }
9993 p1 += len;
9994 if (*p1 == '\n')
9995 return 2;
9996 if (*p1++ == ' ' && *p1++ == '[')
9997 {
9998 char *pend;
9999 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10000 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10001 return n + 1;
10002 }
10003 return 0;
10004 }
10005 \f
10006
10007 /* Display an echo area message M with a specified length of NBYTES
10008 bytes. The string may include null characters. If M is not a
10009 string, clear out any existing message, and let the mini-buffer
10010 text show through.
10011
10012 This function cancels echoing. */
10013
10014 void
10015 message3 (Lisp_Object m)
10016 {
10017 struct gcpro gcpro1;
10018
10019 GCPRO1 (m);
10020 clear_message (true, true);
10021 cancel_echoing ();
10022
10023 /* First flush out any partial line written with print. */
10024 message_log_maybe_newline ();
10025 if (STRINGP (m))
10026 {
10027 ptrdiff_t nbytes = SBYTES (m);
10028 bool multibyte = STRING_MULTIBYTE (m);
10029 USE_SAFE_ALLOCA;
10030 char *buffer = SAFE_ALLOCA (nbytes);
10031 memcpy (buffer, SDATA (m), nbytes);
10032 message_dolog (buffer, nbytes, 1, multibyte);
10033 SAFE_FREE ();
10034 }
10035 message3_nolog (m);
10036
10037 UNGCPRO;
10038 }
10039
10040
10041 /* The non-logging version of message3.
10042 This does not cancel echoing, because it is used for echoing.
10043 Perhaps we need to make a separate function for echoing
10044 and make this cancel echoing. */
10045
10046 void
10047 message3_nolog (Lisp_Object m)
10048 {
10049 struct frame *sf = SELECTED_FRAME ();
10050
10051 if (FRAME_INITIAL_P (sf))
10052 {
10053 if (noninteractive_need_newline)
10054 putc ('\n', stderr);
10055 noninteractive_need_newline = 0;
10056 if (STRINGP (m))
10057 {
10058 Lisp_Object s = ENCODE_SYSTEM (m);
10059
10060 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10061 }
10062 if (cursor_in_echo_area == 0)
10063 fprintf (stderr, "\n");
10064 fflush (stderr);
10065 }
10066 /* Error messages get reported properly by cmd_error, so this must be just an
10067 informative message; if the frame hasn't really been initialized yet, just
10068 toss it. */
10069 else if (INTERACTIVE && sf->glyphs_initialized_p)
10070 {
10071 /* Get the frame containing the mini-buffer
10072 that the selected frame is using. */
10073 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10074 Lisp_Object frame = XWINDOW (mini_window)->frame;
10075 struct frame *f = XFRAME (frame);
10076
10077 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10078 Fmake_frame_visible (frame);
10079
10080 if (STRINGP (m) && SCHARS (m) > 0)
10081 {
10082 set_message (m);
10083 if (minibuffer_auto_raise)
10084 Fraise_frame (frame);
10085 /* Assume we are not echoing.
10086 (If we are, echo_now will override this.) */
10087 echo_message_buffer = Qnil;
10088 }
10089 else
10090 clear_message (true, true);
10091
10092 do_pending_window_change (0);
10093 echo_area_display (1);
10094 do_pending_window_change (0);
10095 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10096 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10097 }
10098 }
10099
10100
10101 /* Display a null-terminated echo area message M. If M is 0, clear
10102 out any existing message, and let the mini-buffer text show through.
10103
10104 The buffer M must continue to exist until after the echo area gets
10105 cleared or some other message gets displayed there. Do not pass
10106 text that is stored in a Lisp string. Do not pass text in a buffer
10107 that was alloca'd. */
10108
10109 void
10110 message1 (const char *m)
10111 {
10112 message3 (m ? build_unibyte_string (m) : Qnil);
10113 }
10114
10115
10116 /* The non-logging counterpart of message1. */
10117
10118 void
10119 message1_nolog (const char *m)
10120 {
10121 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10122 }
10123
10124 /* Display a message M which contains a single %s
10125 which gets replaced with STRING. */
10126
10127 void
10128 message_with_string (const char *m, Lisp_Object string, int log)
10129 {
10130 CHECK_STRING (string);
10131
10132 if (noninteractive)
10133 {
10134 if (m)
10135 {
10136 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10137 String whose data pointer might be passed to us in M. So
10138 we use a local copy. */
10139 char *fmt = xstrdup (m);
10140
10141 if (noninteractive_need_newline)
10142 putc ('\n', stderr);
10143 noninteractive_need_newline = 0;
10144 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10145 if (!cursor_in_echo_area)
10146 fprintf (stderr, "\n");
10147 fflush (stderr);
10148 xfree (fmt);
10149 }
10150 }
10151 else if (INTERACTIVE)
10152 {
10153 /* The frame whose minibuffer we're going to display the message on.
10154 It may be larger than the selected frame, so we need
10155 to use its buffer, not the selected frame's buffer. */
10156 Lisp_Object mini_window;
10157 struct frame *f, *sf = SELECTED_FRAME ();
10158
10159 /* Get the frame containing the minibuffer
10160 that the selected frame is using. */
10161 mini_window = FRAME_MINIBUF_WINDOW (sf);
10162 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10163
10164 /* Error messages get reported properly by cmd_error, so this must be
10165 just an informative message; if the frame hasn't really been
10166 initialized yet, just toss it. */
10167 if (f->glyphs_initialized_p)
10168 {
10169 Lisp_Object args[2], msg;
10170 struct gcpro gcpro1, gcpro2;
10171
10172 args[0] = build_string (m);
10173 args[1] = msg = string;
10174 GCPRO2 (args[0], msg);
10175 gcpro1.nvars = 2;
10176
10177 msg = Fformat (2, args);
10178
10179 if (log)
10180 message3 (msg);
10181 else
10182 message3_nolog (msg);
10183
10184 UNGCPRO;
10185
10186 /* Print should start at the beginning of the message
10187 buffer next time. */
10188 message_buf_print = 0;
10189 }
10190 }
10191 }
10192
10193
10194 /* Dump an informative message to the minibuf. If M is 0, clear out
10195 any existing message, and let the mini-buffer text show through. */
10196
10197 static void
10198 vmessage (const char *m, va_list ap)
10199 {
10200 if (noninteractive)
10201 {
10202 if (m)
10203 {
10204 if (noninteractive_need_newline)
10205 putc ('\n', stderr);
10206 noninteractive_need_newline = 0;
10207 vfprintf (stderr, m, ap);
10208 if (cursor_in_echo_area == 0)
10209 fprintf (stderr, "\n");
10210 fflush (stderr);
10211 }
10212 }
10213 else if (INTERACTIVE)
10214 {
10215 /* The frame whose mini-buffer we're going to display the message
10216 on. It may be larger than the selected frame, so we need to
10217 use its buffer, not the selected frame's buffer. */
10218 Lisp_Object mini_window;
10219 struct frame *f, *sf = SELECTED_FRAME ();
10220
10221 /* Get the frame containing the mini-buffer
10222 that the selected frame is using. */
10223 mini_window = FRAME_MINIBUF_WINDOW (sf);
10224 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10225
10226 /* Error messages get reported properly by cmd_error, so this must be
10227 just an informative message; if the frame hasn't really been
10228 initialized yet, just toss it. */
10229 if (f->glyphs_initialized_p)
10230 {
10231 if (m)
10232 {
10233 ptrdiff_t len;
10234 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10235 char *message_buf = alloca (maxsize + 1);
10236
10237 len = doprnt (message_buf, maxsize, m, 0, ap);
10238
10239 message3 (make_string (message_buf, len));
10240 }
10241 else
10242 message1 (0);
10243
10244 /* Print should start at the beginning of the message
10245 buffer next time. */
10246 message_buf_print = 0;
10247 }
10248 }
10249 }
10250
10251 void
10252 message (const char *m, ...)
10253 {
10254 va_list ap;
10255 va_start (ap, m);
10256 vmessage (m, ap);
10257 va_end (ap);
10258 }
10259
10260
10261 #if 0
10262 /* The non-logging version of message. */
10263
10264 void
10265 message_nolog (const char *m, ...)
10266 {
10267 Lisp_Object old_log_max;
10268 va_list ap;
10269 va_start (ap, m);
10270 old_log_max = Vmessage_log_max;
10271 Vmessage_log_max = Qnil;
10272 vmessage (m, ap);
10273 Vmessage_log_max = old_log_max;
10274 va_end (ap);
10275 }
10276 #endif
10277
10278
10279 /* Display the current message in the current mini-buffer. This is
10280 only called from error handlers in process.c, and is not time
10281 critical. */
10282
10283 void
10284 update_echo_area (void)
10285 {
10286 if (!NILP (echo_area_buffer[0]))
10287 {
10288 Lisp_Object string;
10289 string = Fcurrent_message ();
10290 message3 (string);
10291 }
10292 }
10293
10294
10295 /* Make sure echo area buffers in `echo_buffers' are live.
10296 If they aren't, make new ones. */
10297
10298 static void
10299 ensure_echo_area_buffers (void)
10300 {
10301 int i;
10302
10303 for (i = 0; i < 2; ++i)
10304 if (!BUFFERP (echo_buffer[i])
10305 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10306 {
10307 char name[30];
10308 Lisp_Object old_buffer;
10309 int j;
10310
10311 old_buffer = echo_buffer[i];
10312 echo_buffer[i] = Fget_buffer_create
10313 (make_formatted_string (name, " *Echo Area %d*", i));
10314 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10315 /* to force word wrap in echo area -
10316 it was decided to postpone this*/
10317 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10318
10319 for (j = 0; j < 2; ++j)
10320 if (EQ (old_buffer, echo_area_buffer[j]))
10321 echo_area_buffer[j] = echo_buffer[i];
10322 }
10323 }
10324
10325
10326 /* Call FN with args A1..A2 with either the current or last displayed
10327 echo_area_buffer as current buffer.
10328
10329 WHICH zero means use the current message buffer
10330 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10331 from echo_buffer[] and clear it.
10332
10333 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10334 suitable buffer from echo_buffer[] and clear it.
10335
10336 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10337 that the current message becomes the last displayed one, make
10338 choose a suitable buffer for echo_area_buffer[0], and clear it.
10339
10340 Value is what FN returns. */
10341
10342 static int
10343 with_echo_area_buffer (struct window *w, int which,
10344 int (*fn) (ptrdiff_t, Lisp_Object),
10345 ptrdiff_t a1, Lisp_Object a2)
10346 {
10347 Lisp_Object buffer;
10348 int this_one, the_other, clear_buffer_p, rc;
10349 ptrdiff_t count = SPECPDL_INDEX ();
10350
10351 /* If buffers aren't live, make new ones. */
10352 ensure_echo_area_buffers ();
10353
10354 clear_buffer_p = 0;
10355
10356 if (which == 0)
10357 this_one = 0, the_other = 1;
10358 else if (which > 0)
10359 this_one = 1, the_other = 0;
10360 else
10361 {
10362 this_one = 0, the_other = 1;
10363 clear_buffer_p = true;
10364
10365 /* We need a fresh one in case the current echo buffer equals
10366 the one containing the last displayed echo area message. */
10367 if (!NILP (echo_area_buffer[this_one])
10368 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10369 echo_area_buffer[this_one] = Qnil;
10370 }
10371
10372 /* Choose a suitable buffer from echo_buffer[] is we don't
10373 have one. */
10374 if (NILP (echo_area_buffer[this_one]))
10375 {
10376 echo_area_buffer[this_one]
10377 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10378 ? echo_buffer[the_other]
10379 : echo_buffer[this_one]);
10380 clear_buffer_p = true;
10381 }
10382
10383 buffer = echo_area_buffer[this_one];
10384
10385 /* Don't get confused by reusing the buffer used for echoing
10386 for a different purpose. */
10387 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10388 cancel_echoing ();
10389
10390 record_unwind_protect (unwind_with_echo_area_buffer,
10391 with_echo_area_buffer_unwind_data (w));
10392
10393 /* Make the echo area buffer current. Note that for display
10394 purposes, it is not necessary that the displayed window's buffer
10395 == current_buffer, except for text property lookup. So, let's
10396 only set that buffer temporarily here without doing a full
10397 Fset_window_buffer. We must also change w->pointm, though,
10398 because otherwise an assertions in unshow_buffer fails, and Emacs
10399 aborts. */
10400 set_buffer_internal_1 (XBUFFER (buffer));
10401 if (w)
10402 {
10403 wset_buffer (w, buffer);
10404 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10405 }
10406
10407 bset_undo_list (current_buffer, Qt);
10408 bset_read_only (current_buffer, Qnil);
10409 specbind (Qinhibit_read_only, Qt);
10410 specbind (Qinhibit_modification_hooks, Qt);
10411
10412 if (clear_buffer_p && Z > BEG)
10413 del_range (BEG, Z);
10414
10415 eassert (BEGV >= BEG);
10416 eassert (ZV <= Z && ZV >= BEGV);
10417
10418 rc = fn (a1, a2);
10419
10420 eassert (BEGV >= BEG);
10421 eassert (ZV <= Z && ZV >= BEGV);
10422
10423 unbind_to (count, Qnil);
10424 return rc;
10425 }
10426
10427
10428 /* Save state that should be preserved around the call to the function
10429 FN called in with_echo_area_buffer. */
10430
10431 static Lisp_Object
10432 with_echo_area_buffer_unwind_data (struct window *w)
10433 {
10434 int i = 0;
10435 Lisp_Object vector, tmp;
10436
10437 /* Reduce consing by keeping one vector in
10438 Vwith_echo_area_save_vector. */
10439 vector = Vwith_echo_area_save_vector;
10440 Vwith_echo_area_save_vector = Qnil;
10441
10442 if (NILP (vector))
10443 vector = Fmake_vector (make_number (9), Qnil);
10444
10445 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10446 ASET (vector, i, Vdeactivate_mark); ++i;
10447 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10448
10449 if (w)
10450 {
10451 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10452 ASET (vector, i, w->contents); ++i;
10453 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10454 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10455 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10456 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10457 }
10458 else
10459 {
10460 int end = i + 6;
10461 for (; i < end; ++i)
10462 ASET (vector, i, Qnil);
10463 }
10464
10465 eassert (i == ASIZE (vector));
10466 return vector;
10467 }
10468
10469
10470 /* Restore global state from VECTOR which was created by
10471 with_echo_area_buffer_unwind_data. */
10472
10473 static void
10474 unwind_with_echo_area_buffer (Lisp_Object vector)
10475 {
10476 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10477 Vdeactivate_mark = AREF (vector, 1);
10478 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10479
10480 if (WINDOWP (AREF (vector, 3)))
10481 {
10482 struct window *w;
10483 Lisp_Object buffer;
10484
10485 w = XWINDOW (AREF (vector, 3));
10486 buffer = AREF (vector, 4);
10487
10488 wset_buffer (w, buffer);
10489 set_marker_both (w->pointm, buffer,
10490 XFASTINT (AREF (vector, 5)),
10491 XFASTINT (AREF (vector, 6)));
10492 set_marker_both (w->start, buffer,
10493 XFASTINT (AREF (vector, 7)),
10494 XFASTINT (AREF (vector, 8)));
10495 }
10496
10497 Vwith_echo_area_save_vector = vector;
10498 }
10499
10500
10501 /* Set up the echo area for use by print functions. MULTIBYTE_P
10502 non-zero means we will print multibyte. */
10503
10504 void
10505 setup_echo_area_for_printing (int multibyte_p)
10506 {
10507 /* If we can't find an echo area any more, exit. */
10508 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10509 Fkill_emacs (Qnil);
10510
10511 ensure_echo_area_buffers ();
10512
10513 if (!message_buf_print)
10514 {
10515 /* A message has been output since the last time we printed.
10516 Choose a fresh echo area buffer. */
10517 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10518 echo_area_buffer[0] = echo_buffer[1];
10519 else
10520 echo_area_buffer[0] = echo_buffer[0];
10521
10522 /* Switch to that buffer and clear it. */
10523 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10524 bset_truncate_lines (current_buffer, Qnil);
10525
10526 if (Z > BEG)
10527 {
10528 ptrdiff_t count = SPECPDL_INDEX ();
10529 specbind (Qinhibit_read_only, Qt);
10530 /* Note that undo recording is always disabled. */
10531 del_range (BEG, Z);
10532 unbind_to (count, Qnil);
10533 }
10534 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10535
10536 /* Set up the buffer for the multibyteness we need. */
10537 if (multibyte_p
10538 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10539 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10540
10541 /* Raise the frame containing the echo area. */
10542 if (minibuffer_auto_raise)
10543 {
10544 struct frame *sf = SELECTED_FRAME ();
10545 Lisp_Object mini_window;
10546 mini_window = FRAME_MINIBUF_WINDOW (sf);
10547 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10548 }
10549
10550 message_log_maybe_newline ();
10551 message_buf_print = 1;
10552 }
10553 else
10554 {
10555 if (NILP (echo_area_buffer[0]))
10556 {
10557 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10558 echo_area_buffer[0] = echo_buffer[1];
10559 else
10560 echo_area_buffer[0] = echo_buffer[0];
10561 }
10562
10563 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10564 {
10565 /* Someone switched buffers between print requests. */
10566 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10567 bset_truncate_lines (current_buffer, Qnil);
10568 }
10569 }
10570 }
10571
10572
10573 /* Display an echo area message in window W. Value is non-zero if W's
10574 height is changed. If display_last_displayed_message_p is
10575 non-zero, display the message that was last displayed, otherwise
10576 display the current message. */
10577
10578 static int
10579 display_echo_area (struct window *w)
10580 {
10581 int i, no_message_p, window_height_changed_p;
10582
10583 /* Temporarily disable garbage collections while displaying the echo
10584 area. This is done because a GC can print a message itself.
10585 That message would modify the echo area buffer's contents while a
10586 redisplay of the buffer is going on, and seriously confuse
10587 redisplay. */
10588 ptrdiff_t count = inhibit_garbage_collection ();
10589
10590 /* If there is no message, we must call display_echo_area_1
10591 nevertheless because it resizes the window. But we will have to
10592 reset the echo_area_buffer in question to nil at the end because
10593 with_echo_area_buffer will sets it to an empty buffer. */
10594 i = display_last_displayed_message_p ? 1 : 0;
10595 no_message_p = NILP (echo_area_buffer[i]);
10596
10597 window_height_changed_p
10598 = with_echo_area_buffer (w, display_last_displayed_message_p,
10599 display_echo_area_1,
10600 (intptr_t) w, Qnil);
10601
10602 if (no_message_p)
10603 echo_area_buffer[i] = Qnil;
10604
10605 unbind_to (count, Qnil);
10606 return window_height_changed_p;
10607 }
10608
10609
10610 /* Helper for display_echo_area. Display the current buffer which
10611 contains the current echo area message in window W, a mini-window,
10612 a pointer to which is passed in A1. A2..A4 are currently not used.
10613 Change the height of W so that all of the message is displayed.
10614 Value is non-zero if height of W was changed. */
10615
10616 static int
10617 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10618 {
10619 intptr_t i1 = a1;
10620 struct window *w = (struct window *) i1;
10621 Lisp_Object window;
10622 struct text_pos start;
10623 int window_height_changed_p = 0;
10624
10625 /* Do this before displaying, so that we have a large enough glyph
10626 matrix for the display. If we can't get enough space for the
10627 whole text, display the last N lines. That works by setting w->start. */
10628 window_height_changed_p = resize_mini_window (w, 0);
10629
10630 /* Use the starting position chosen by resize_mini_window. */
10631 SET_TEXT_POS_FROM_MARKER (start, w->start);
10632
10633 /* Display. */
10634 clear_glyph_matrix (w->desired_matrix);
10635 XSETWINDOW (window, w);
10636 try_window (window, start, 0);
10637
10638 return window_height_changed_p;
10639 }
10640
10641
10642 /* Resize the echo area window to exactly the size needed for the
10643 currently displayed message, if there is one. If a mini-buffer
10644 is active, don't shrink it. */
10645
10646 void
10647 resize_echo_area_exactly (void)
10648 {
10649 if (BUFFERP (echo_area_buffer[0])
10650 && WINDOWP (echo_area_window))
10651 {
10652 struct window *w = XWINDOW (echo_area_window);
10653 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10654 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10655 (intptr_t) w, resize_exactly);
10656 if (resized_p)
10657 {
10658 windows_or_buffers_changed = 42;
10659 update_mode_lines = 30;
10660 redisplay_internal ();
10661 }
10662 }
10663 }
10664
10665
10666 /* Callback function for with_echo_area_buffer, when used from
10667 resize_echo_area_exactly. A1 contains a pointer to the window to
10668 resize, EXACTLY non-nil means resize the mini-window exactly to the
10669 size of the text displayed. A3 and A4 are not used. Value is what
10670 resize_mini_window returns. */
10671
10672 static int
10673 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10674 {
10675 intptr_t i1 = a1;
10676 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10677 }
10678
10679
10680 /* Resize mini-window W to fit the size of its contents. EXACT_P
10681 means size the window exactly to the size needed. Otherwise, it's
10682 only enlarged until W's buffer is empty.
10683
10684 Set W->start to the right place to begin display. If the whole
10685 contents fit, start at the beginning. Otherwise, start so as
10686 to make the end of the contents appear. This is particularly
10687 important for y-or-n-p, but seems desirable generally.
10688
10689 Value is non-zero if the window height has been changed. */
10690
10691 int
10692 resize_mini_window (struct window *w, int exact_p)
10693 {
10694 struct frame *f = XFRAME (w->frame);
10695 int window_height_changed_p = 0;
10696
10697 eassert (MINI_WINDOW_P (w));
10698
10699 /* By default, start display at the beginning. */
10700 set_marker_both (w->start, w->contents,
10701 BUF_BEGV (XBUFFER (w->contents)),
10702 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10703
10704 /* Don't resize windows while redisplaying a window; it would
10705 confuse redisplay functions when the size of the window they are
10706 displaying changes from under them. Such a resizing can happen,
10707 for instance, when which-func prints a long message while
10708 we are running fontification-functions. We're running these
10709 functions with safe_call which binds inhibit-redisplay to t. */
10710 if (!NILP (Vinhibit_redisplay))
10711 return 0;
10712
10713 /* Nil means don't try to resize. */
10714 if (NILP (Vresize_mini_windows)
10715 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10716 return 0;
10717
10718 if (!FRAME_MINIBUF_ONLY_P (f))
10719 {
10720 struct it it;
10721 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10722 + WINDOW_PIXEL_HEIGHT (w));
10723 int unit = FRAME_LINE_HEIGHT (f);
10724 int height, max_height;
10725 struct text_pos start;
10726 struct buffer *old_current_buffer = NULL;
10727
10728 if (current_buffer != XBUFFER (w->contents))
10729 {
10730 old_current_buffer = current_buffer;
10731 set_buffer_internal (XBUFFER (w->contents));
10732 }
10733
10734 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10735
10736 /* Compute the max. number of lines specified by the user. */
10737 if (FLOATP (Vmax_mini_window_height))
10738 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10739 else if (INTEGERP (Vmax_mini_window_height))
10740 max_height = XINT (Vmax_mini_window_height) * unit;
10741 else
10742 max_height = total_height / 4;
10743
10744 /* Correct that max. height if it's bogus. */
10745 max_height = clip_to_bounds (unit, max_height, total_height);
10746
10747 /* Find out the height of the text in the window. */
10748 if (it.line_wrap == TRUNCATE)
10749 height = unit;
10750 else
10751 {
10752 last_height = 0;
10753 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10754 if (it.max_ascent == 0 && it.max_descent == 0)
10755 height = it.current_y + last_height;
10756 else
10757 height = it.current_y + it.max_ascent + it.max_descent;
10758 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10759 }
10760
10761 /* Compute a suitable window start. */
10762 if (height > max_height)
10763 {
10764 height = (max_height / unit) * unit;
10765 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10766 move_it_vertically_backward (&it, height - unit);
10767 start = it.current.pos;
10768 }
10769 else
10770 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10771 SET_MARKER_FROM_TEXT_POS (w->start, start);
10772
10773 if (EQ (Vresize_mini_windows, Qgrow_only))
10774 {
10775 /* Let it grow only, until we display an empty message, in which
10776 case the window shrinks again. */
10777 if (height > WINDOW_PIXEL_HEIGHT (w))
10778 {
10779 int old_height = WINDOW_PIXEL_HEIGHT (w);
10780
10781 FRAME_WINDOWS_FROZEN (f) = 1;
10782 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10783 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10784 }
10785 else if (height < WINDOW_PIXEL_HEIGHT (w)
10786 && (exact_p || BEGV == ZV))
10787 {
10788 int old_height = WINDOW_PIXEL_HEIGHT (w);
10789
10790 FRAME_WINDOWS_FROZEN (f) = 0;
10791 shrink_mini_window (w, 1);
10792 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10793 }
10794 }
10795 else
10796 {
10797 /* Always resize to exact size needed. */
10798 if (height > WINDOW_PIXEL_HEIGHT (w))
10799 {
10800 int old_height = WINDOW_PIXEL_HEIGHT (w);
10801
10802 FRAME_WINDOWS_FROZEN (f) = 1;
10803 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10804 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10805 }
10806 else if (height < WINDOW_PIXEL_HEIGHT (w))
10807 {
10808 int old_height = WINDOW_PIXEL_HEIGHT (w);
10809
10810 FRAME_WINDOWS_FROZEN (f) = 0;
10811 shrink_mini_window (w, 1);
10812
10813 if (height)
10814 {
10815 FRAME_WINDOWS_FROZEN (f) = 1;
10816 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10817 }
10818
10819 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10820 }
10821 }
10822
10823 if (old_current_buffer)
10824 set_buffer_internal (old_current_buffer);
10825 }
10826
10827 return window_height_changed_p;
10828 }
10829
10830
10831 /* Value is the current message, a string, or nil if there is no
10832 current message. */
10833
10834 Lisp_Object
10835 current_message (void)
10836 {
10837 Lisp_Object msg;
10838
10839 if (!BUFFERP (echo_area_buffer[0]))
10840 msg = Qnil;
10841 else
10842 {
10843 with_echo_area_buffer (0, 0, current_message_1,
10844 (intptr_t) &msg, Qnil);
10845 if (NILP (msg))
10846 echo_area_buffer[0] = Qnil;
10847 }
10848
10849 return msg;
10850 }
10851
10852
10853 static int
10854 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10855 {
10856 intptr_t i1 = a1;
10857 Lisp_Object *msg = (Lisp_Object *) i1;
10858
10859 if (Z > BEG)
10860 *msg = make_buffer_string (BEG, Z, 1);
10861 else
10862 *msg = Qnil;
10863 return 0;
10864 }
10865
10866
10867 /* Push the current message on Vmessage_stack for later restoration
10868 by restore_message. Value is non-zero if the current message isn't
10869 empty. This is a relatively infrequent operation, so it's not
10870 worth optimizing. */
10871
10872 bool
10873 push_message (void)
10874 {
10875 Lisp_Object msg = current_message ();
10876 Vmessage_stack = Fcons (msg, Vmessage_stack);
10877 return STRINGP (msg);
10878 }
10879
10880
10881 /* Restore message display from the top of Vmessage_stack. */
10882
10883 void
10884 restore_message (void)
10885 {
10886 eassert (CONSP (Vmessage_stack));
10887 message3_nolog (XCAR (Vmessage_stack));
10888 }
10889
10890
10891 /* Handler for unwind-protect calling pop_message. */
10892
10893 void
10894 pop_message_unwind (void)
10895 {
10896 /* Pop the top-most entry off Vmessage_stack. */
10897 eassert (CONSP (Vmessage_stack));
10898 Vmessage_stack = XCDR (Vmessage_stack);
10899 }
10900
10901
10902 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10903 exits. If the stack is not empty, we have a missing pop_message
10904 somewhere. */
10905
10906 void
10907 check_message_stack (void)
10908 {
10909 if (!NILP (Vmessage_stack))
10910 emacs_abort ();
10911 }
10912
10913
10914 /* Truncate to NCHARS what will be displayed in the echo area the next
10915 time we display it---but don't redisplay it now. */
10916
10917 void
10918 truncate_echo_area (ptrdiff_t nchars)
10919 {
10920 if (nchars == 0)
10921 echo_area_buffer[0] = Qnil;
10922 else if (!noninteractive
10923 && INTERACTIVE
10924 && !NILP (echo_area_buffer[0]))
10925 {
10926 struct frame *sf = SELECTED_FRAME ();
10927 /* Error messages get reported properly by cmd_error, so this must be
10928 just an informative message; if the frame hasn't really been
10929 initialized yet, just toss it. */
10930 if (sf->glyphs_initialized_p)
10931 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10932 }
10933 }
10934
10935
10936 /* Helper function for truncate_echo_area. Truncate the current
10937 message to at most NCHARS characters. */
10938
10939 static int
10940 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10941 {
10942 if (BEG + nchars < Z)
10943 del_range (BEG + nchars, Z);
10944 if (Z == BEG)
10945 echo_area_buffer[0] = Qnil;
10946 return 0;
10947 }
10948
10949 /* Set the current message to STRING. */
10950
10951 static void
10952 set_message (Lisp_Object string)
10953 {
10954 eassert (STRINGP (string));
10955
10956 message_enable_multibyte = STRING_MULTIBYTE (string);
10957
10958 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10959 message_buf_print = 0;
10960 help_echo_showing_p = 0;
10961
10962 if (STRINGP (Vdebug_on_message)
10963 && STRINGP (string)
10964 && fast_string_match (Vdebug_on_message, string) >= 0)
10965 call_debugger (list2 (Qerror, string));
10966 }
10967
10968
10969 /* Helper function for set_message. First argument is ignored and second
10970 argument has the same meaning as for set_message.
10971 This function is called with the echo area buffer being current. */
10972
10973 static int
10974 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10975 {
10976 eassert (STRINGP (string));
10977
10978 /* Change multibyteness of the echo buffer appropriately. */
10979 if (message_enable_multibyte
10980 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10981 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10982
10983 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10984 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10985 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10986
10987 /* Insert new message at BEG. */
10988 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10989
10990 /* This function takes care of single/multibyte conversion.
10991 We just have to ensure that the echo area buffer has the right
10992 setting of enable_multibyte_characters. */
10993 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10994
10995 return 0;
10996 }
10997
10998
10999 /* Clear messages. CURRENT_P non-zero means clear the current
11000 message. LAST_DISPLAYED_P non-zero means clear the message
11001 last displayed. */
11002
11003 void
11004 clear_message (bool current_p, bool last_displayed_p)
11005 {
11006 if (current_p)
11007 {
11008 echo_area_buffer[0] = Qnil;
11009 message_cleared_p = true;
11010 }
11011
11012 if (last_displayed_p)
11013 echo_area_buffer[1] = Qnil;
11014
11015 message_buf_print = 0;
11016 }
11017
11018 /* Clear garbaged frames.
11019
11020 This function is used where the old redisplay called
11021 redraw_garbaged_frames which in turn called redraw_frame which in
11022 turn called clear_frame. The call to clear_frame was a source of
11023 flickering. I believe a clear_frame is not necessary. It should
11024 suffice in the new redisplay to invalidate all current matrices,
11025 and ensure a complete redisplay of all windows. */
11026
11027 static void
11028 clear_garbaged_frames (void)
11029 {
11030 if (frame_garbaged)
11031 {
11032 Lisp_Object tail, frame;
11033
11034 FOR_EACH_FRAME (tail, frame)
11035 {
11036 struct frame *f = XFRAME (frame);
11037
11038 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11039 {
11040 if (f->resized_p)
11041 redraw_frame (f);
11042 else
11043 clear_current_matrices (f);
11044 fset_redisplay (f);
11045 f->garbaged = false;
11046 f->resized_p = false;
11047 }
11048 }
11049
11050 frame_garbaged = false;
11051 }
11052 }
11053
11054
11055 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11056 is non-zero update selected_frame. Value is non-zero if the
11057 mini-windows height has been changed. */
11058
11059 static int
11060 echo_area_display (int update_frame_p)
11061 {
11062 Lisp_Object mini_window;
11063 struct window *w;
11064 struct frame *f;
11065 int window_height_changed_p = 0;
11066 struct frame *sf = SELECTED_FRAME ();
11067
11068 mini_window = FRAME_MINIBUF_WINDOW (sf);
11069 w = XWINDOW (mini_window);
11070 f = XFRAME (WINDOW_FRAME (w));
11071
11072 /* Don't display if frame is invisible or not yet initialized. */
11073 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11074 return 0;
11075
11076 #ifdef HAVE_WINDOW_SYSTEM
11077 /* When Emacs starts, selected_frame may be the initial terminal
11078 frame. If we let this through, a message would be displayed on
11079 the terminal. */
11080 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11081 return 0;
11082 #endif /* HAVE_WINDOW_SYSTEM */
11083
11084 /* Redraw garbaged frames. */
11085 clear_garbaged_frames ();
11086
11087 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11088 {
11089 echo_area_window = mini_window;
11090 window_height_changed_p = display_echo_area (w);
11091 w->must_be_updated_p = true;
11092
11093 /* Update the display, unless called from redisplay_internal.
11094 Also don't update the screen during redisplay itself. The
11095 update will happen at the end of redisplay, and an update
11096 here could cause confusion. */
11097 if (update_frame_p && !redisplaying_p)
11098 {
11099 int n = 0;
11100
11101 /* If the display update has been interrupted by pending
11102 input, update mode lines in the frame. Due to the
11103 pending input, it might have been that redisplay hasn't
11104 been called, so that mode lines above the echo area are
11105 garbaged. This looks odd, so we prevent it here. */
11106 if (!display_completed)
11107 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11108
11109 if (window_height_changed_p
11110 /* Don't do this if Emacs is shutting down. Redisplay
11111 needs to run hooks. */
11112 && !NILP (Vrun_hooks))
11113 {
11114 /* Must update other windows. Likewise as in other
11115 cases, don't let this update be interrupted by
11116 pending input. */
11117 ptrdiff_t count = SPECPDL_INDEX ();
11118 specbind (Qredisplay_dont_pause, Qt);
11119 windows_or_buffers_changed = 44;
11120 redisplay_internal ();
11121 unbind_to (count, Qnil);
11122 }
11123 else if (FRAME_WINDOW_P (f) && n == 0)
11124 {
11125 /* Window configuration is the same as before.
11126 Can do with a display update of the echo area,
11127 unless we displayed some mode lines. */
11128 update_single_window (w, 1);
11129 flush_frame (f);
11130 }
11131 else
11132 update_frame (f, 1, 1);
11133
11134 /* If cursor is in the echo area, make sure that the next
11135 redisplay displays the minibuffer, so that the cursor will
11136 be replaced with what the minibuffer wants. */
11137 if (cursor_in_echo_area)
11138 wset_redisplay (XWINDOW (mini_window));
11139 }
11140 }
11141 else if (!EQ (mini_window, selected_window))
11142 wset_redisplay (XWINDOW (mini_window));
11143
11144 /* Last displayed message is now the current message. */
11145 echo_area_buffer[1] = echo_area_buffer[0];
11146 /* Inform read_char that we're not echoing. */
11147 echo_message_buffer = Qnil;
11148
11149 /* Prevent redisplay optimization in redisplay_internal by resetting
11150 this_line_start_pos. This is done because the mini-buffer now
11151 displays the message instead of its buffer text. */
11152 if (EQ (mini_window, selected_window))
11153 CHARPOS (this_line_start_pos) = 0;
11154
11155 return window_height_changed_p;
11156 }
11157
11158 /* Nonzero if W's buffer was changed but not saved. */
11159
11160 static int
11161 window_buffer_changed (struct window *w)
11162 {
11163 struct buffer *b = XBUFFER (w->contents);
11164
11165 eassert (BUFFER_LIVE_P (b));
11166
11167 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11168 }
11169
11170 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11171
11172 static int
11173 mode_line_update_needed (struct window *w)
11174 {
11175 return (w->column_number_displayed != -1
11176 && !(PT == w->last_point && !window_outdated (w))
11177 && (w->column_number_displayed != current_column ()));
11178 }
11179
11180 /* Nonzero if window start of W is frozen and may not be changed during
11181 redisplay. */
11182
11183 static bool
11184 window_frozen_p (struct window *w)
11185 {
11186 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11187 {
11188 Lisp_Object window;
11189
11190 XSETWINDOW (window, w);
11191 if (MINI_WINDOW_P (w))
11192 return 0;
11193 else if (EQ (window, selected_window))
11194 return 0;
11195 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11196 && EQ (window, Vminibuf_scroll_window))
11197 /* This special window can't be frozen too. */
11198 return 0;
11199 else
11200 return 1;
11201 }
11202 return 0;
11203 }
11204
11205 /***********************************************************************
11206 Mode Lines and Frame Titles
11207 ***********************************************************************/
11208
11209 /* A buffer for constructing non-propertized mode-line strings and
11210 frame titles in it; allocated from the heap in init_xdisp and
11211 resized as needed in store_mode_line_noprop_char. */
11212
11213 static char *mode_line_noprop_buf;
11214
11215 /* The buffer's end, and a current output position in it. */
11216
11217 static char *mode_line_noprop_buf_end;
11218 static char *mode_line_noprop_ptr;
11219
11220 #define MODE_LINE_NOPROP_LEN(start) \
11221 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11222
11223 static enum {
11224 MODE_LINE_DISPLAY = 0,
11225 MODE_LINE_TITLE,
11226 MODE_LINE_NOPROP,
11227 MODE_LINE_STRING
11228 } mode_line_target;
11229
11230 /* Alist that caches the results of :propertize.
11231 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11232 static Lisp_Object mode_line_proptrans_alist;
11233
11234 /* List of strings making up the mode-line. */
11235 static Lisp_Object mode_line_string_list;
11236
11237 /* Base face property when building propertized mode line string. */
11238 static Lisp_Object mode_line_string_face;
11239 static Lisp_Object mode_line_string_face_prop;
11240
11241
11242 /* Unwind data for mode line strings */
11243
11244 static Lisp_Object Vmode_line_unwind_vector;
11245
11246 static Lisp_Object
11247 format_mode_line_unwind_data (struct frame *target_frame,
11248 struct buffer *obuf,
11249 Lisp_Object owin,
11250 int save_proptrans)
11251 {
11252 Lisp_Object vector, tmp;
11253
11254 /* Reduce consing by keeping one vector in
11255 Vwith_echo_area_save_vector. */
11256 vector = Vmode_line_unwind_vector;
11257 Vmode_line_unwind_vector = Qnil;
11258
11259 if (NILP (vector))
11260 vector = Fmake_vector (make_number (10), Qnil);
11261
11262 ASET (vector, 0, make_number (mode_line_target));
11263 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11264 ASET (vector, 2, mode_line_string_list);
11265 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11266 ASET (vector, 4, mode_line_string_face);
11267 ASET (vector, 5, mode_line_string_face_prop);
11268
11269 if (obuf)
11270 XSETBUFFER (tmp, obuf);
11271 else
11272 tmp = Qnil;
11273 ASET (vector, 6, tmp);
11274 ASET (vector, 7, owin);
11275 if (target_frame)
11276 {
11277 /* Similarly to `with-selected-window', if the operation selects
11278 a window on another frame, we must restore that frame's
11279 selected window, and (for a tty) the top-frame. */
11280 ASET (vector, 8, target_frame->selected_window);
11281 if (FRAME_TERMCAP_P (target_frame))
11282 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11283 }
11284
11285 return vector;
11286 }
11287
11288 static void
11289 unwind_format_mode_line (Lisp_Object vector)
11290 {
11291 Lisp_Object old_window = AREF (vector, 7);
11292 Lisp_Object target_frame_window = AREF (vector, 8);
11293 Lisp_Object old_top_frame = AREF (vector, 9);
11294
11295 mode_line_target = XINT (AREF (vector, 0));
11296 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11297 mode_line_string_list = AREF (vector, 2);
11298 if (! EQ (AREF (vector, 3), Qt))
11299 mode_line_proptrans_alist = AREF (vector, 3);
11300 mode_line_string_face = AREF (vector, 4);
11301 mode_line_string_face_prop = AREF (vector, 5);
11302
11303 /* Select window before buffer, since it may change the buffer. */
11304 if (!NILP (old_window))
11305 {
11306 /* If the operation that we are unwinding had selected a window
11307 on a different frame, reset its frame-selected-window. For a
11308 text terminal, reset its top-frame if necessary. */
11309 if (!NILP (target_frame_window))
11310 {
11311 Lisp_Object frame
11312 = WINDOW_FRAME (XWINDOW (target_frame_window));
11313
11314 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11315 Fselect_window (target_frame_window, Qt);
11316
11317 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11318 Fselect_frame (old_top_frame, Qt);
11319 }
11320
11321 Fselect_window (old_window, Qt);
11322 }
11323
11324 if (!NILP (AREF (vector, 6)))
11325 {
11326 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11327 ASET (vector, 6, Qnil);
11328 }
11329
11330 Vmode_line_unwind_vector = vector;
11331 }
11332
11333
11334 /* Store a single character C for the frame title in mode_line_noprop_buf.
11335 Re-allocate mode_line_noprop_buf if necessary. */
11336
11337 static void
11338 store_mode_line_noprop_char (char c)
11339 {
11340 /* If output position has reached the end of the allocated buffer,
11341 increase the buffer's size. */
11342 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11343 {
11344 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11345 ptrdiff_t size = len;
11346 mode_line_noprop_buf =
11347 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11348 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11349 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11350 }
11351
11352 *mode_line_noprop_ptr++ = c;
11353 }
11354
11355
11356 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11357 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11358 characters that yield more columns than PRECISION; PRECISION <= 0
11359 means copy the whole string. Pad with spaces until FIELD_WIDTH
11360 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11361 pad. Called from display_mode_element when it is used to build a
11362 frame title. */
11363
11364 static int
11365 store_mode_line_noprop (const char *string, int field_width, int precision)
11366 {
11367 const unsigned char *str = (const unsigned char *) string;
11368 int n = 0;
11369 ptrdiff_t dummy, nbytes;
11370
11371 /* Copy at most PRECISION chars from STR. */
11372 nbytes = strlen (string);
11373 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11374 while (nbytes--)
11375 store_mode_line_noprop_char (*str++);
11376
11377 /* Fill up with spaces until FIELD_WIDTH reached. */
11378 while (field_width > 0
11379 && n < field_width)
11380 {
11381 store_mode_line_noprop_char (' ');
11382 ++n;
11383 }
11384
11385 return n;
11386 }
11387
11388 /***********************************************************************
11389 Frame Titles
11390 ***********************************************************************/
11391
11392 #ifdef HAVE_WINDOW_SYSTEM
11393
11394 /* Set the title of FRAME, if it has changed. The title format is
11395 Vicon_title_format if FRAME is iconified, otherwise it is
11396 frame_title_format. */
11397
11398 static void
11399 x_consider_frame_title (Lisp_Object frame)
11400 {
11401 struct frame *f = XFRAME (frame);
11402
11403 if (FRAME_WINDOW_P (f)
11404 || FRAME_MINIBUF_ONLY_P (f)
11405 || f->explicit_name)
11406 {
11407 /* Do we have more than one visible frame on this X display? */
11408 Lisp_Object tail, other_frame, fmt;
11409 ptrdiff_t title_start;
11410 char *title;
11411 ptrdiff_t len;
11412 struct it it;
11413 ptrdiff_t count = SPECPDL_INDEX ();
11414
11415 FOR_EACH_FRAME (tail, other_frame)
11416 {
11417 struct frame *tf = XFRAME (other_frame);
11418
11419 if (tf != f
11420 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11421 && !FRAME_MINIBUF_ONLY_P (tf)
11422 && !EQ (other_frame, tip_frame)
11423 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11424 break;
11425 }
11426
11427 /* Set global variable indicating that multiple frames exist. */
11428 multiple_frames = CONSP (tail);
11429
11430 /* Switch to the buffer of selected window of the frame. Set up
11431 mode_line_target so that display_mode_element will output into
11432 mode_line_noprop_buf; then display the title. */
11433 record_unwind_protect (unwind_format_mode_line,
11434 format_mode_line_unwind_data
11435 (f, current_buffer, selected_window, 0));
11436
11437 Fselect_window (f->selected_window, Qt);
11438 set_buffer_internal_1
11439 (XBUFFER (XWINDOW (f->selected_window)->contents));
11440 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11441
11442 mode_line_target = MODE_LINE_TITLE;
11443 title_start = MODE_LINE_NOPROP_LEN (0);
11444 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11445 NULL, DEFAULT_FACE_ID);
11446 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11447 len = MODE_LINE_NOPROP_LEN (title_start);
11448 title = mode_line_noprop_buf + title_start;
11449 unbind_to (count, Qnil);
11450
11451 /* Set the title only if it's changed. This avoids consing in
11452 the common case where it hasn't. (If it turns out that we've
11453 already wasted too much time by walking through the list with
11454 display_mode_element, then we might need to optimize at a
11455 higher level than this.) */
11456 if (! STRINGP (f->name)
11457 || SBYTES (f->name) != len
11458 || memcmp (title, SDATA (f->name), len) != 0)
11459 x_implicitly_set_name (f, make_string (title, len), Qnil);
11460 }
11461 }
11462
11463 #endif /* not HAVE_WINDOW_SYSTEM */
11464
11465 \f
11466 /***********************************************************************
11467 Menu Bars
11468 ***********************************************************************/
11469
11470 /* Non-zero if we will not redisplay all visible windows. */
11471 #define REDISPLAY_SOME_P() \
11472 ((windows_or_buffers_changed == 0 \
11473 || windows_or_buffers_changed == REDISPLAY_SOME) \
11474 && (update_mode_lines == 0 \
11475 || update_mode_lines == REDISPLAY_SOME))
11476
11477 /* Prepare for redisplay by updating menu-bar item lists when
11478 appropriate. This can call eval. */
11479
11480 static void
11481 prepare_menu_bars (void)
11482 {
11483 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11484 bool some_windows = REDISPLAY_SOME_P ();
11485 struct gcpro gcpro1, gcpro2;
11486 Lisp_Object tooltip_frame;
11487
11488 #ifdef HAVE_WINDOW_SYSTEM
11489 tooltip_frame = tip_frame;
11490 #else
11491 tooltip_frame = Qnil;
11492 #endif
11493
11494 if (FUNCTIONP (Vpre_redisplay_function))
11495 {
11496 Lisp_Object windows = all_windows ? Qt : Qnil;
11497 if (all_windows && some_windows)
11498 {
11499 Lisp_Object ws = window_list ();
11500 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11501 {
11502 Lisp_Object this = XCAR (ws);
11503 struct window *w = XWINDOW (this);
11504 if (w->redisplay
11505 || XFRAME (w->frame)->redisplay
11506 || XBUFFER (w->contents)->text->redisplay)
11507 {
11508 windows = Fcons (this, windows);
11509 }
11510 }
11511 }
11512 safe_call1 (Vpre_redisplay_function, windows);
11513 }
11514
11515 /* Update all frame titles based on their buffer names, etc. We do
11516 this before the menu bars so that the buffer-menu will show the
11517 up-to-date frame titles. */
11518 #ifdef HAVE_WINDOW_SYSTEM
11519 if (all_windows)
11520 {
11521 Lisp_Object tail, frame;
11522
11523 FOR_EACH_FRAME (tail, frame)
11524 {
11525 struct frame *f = XFRAME (frame);
11526 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11527 if (some_windows
11528 && !f->redisplay
11529 && !w->redisplay
11530 && !XBUFFER (w->contents)->text->redisplay)
11531 continue;
11532
11533 if (!EQ (frame, tooltip_frame)
11534 && (FRAME_ICONIFIED_P (f)
11535 || FRAME_VISIBLE_P (f) == 1
11536 /* Exclude TTY frames that are obscured because they
11537 are not the top frame on their console. This is
11538 because x_consider_frame_title actually switches
11539 to the frame, which for TTY frames means it is
11540 marked as garbaged, and will be completely
11541 redrawn on the next redisplay cycle. This causes
11542 TTY frames to be completely redrawn, when there
11543 are more than one of them, even though nothing
11544 should be changed on display. */
11545 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11546 x_consider_frame_title (frame);
11547 }
11548 }
11549 #endif /* HAVE_WINDOW_SYSTEM */
11550
11551 /* Update the menu bar item lists, if appropriate. This has to be
11552 done before any actual redisplay or generation of display lines. */
11553
11554 if (all_windows)
11555 {
11556 Lisp_Object tail, frame;
11557 ptrdiff_t count = SPECPDL_INDEX ();
11558 /* 1 means that update_menu_bar has run its hooks
11559 so any further calls to update_menu_bar shouldn't do so again. */
11560 int menu_bar_hooks_run = 0;
11561
11562 record_unwind_save_match_data ();
11563
11564 FOR_EACH_FRAME (tail, frame)
11565 {
11566 struct frame *f = XFRAME (frame);
11567 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11568
11569 /* Ignore tooltip frame. */
11570 if (EQ (frame, tooltip_frame))
11571 continue;
11572
11573 if (some_windows
11574 && !f->redisplay
11575 && !w->redisplay
11576 && !XBUFFER (w->contents)->text->redisplay)
11577 continue;
11578
11579 /* If a window on this frame changed size, report that to
11580 the user and clear the size-change flag. */
11581 if (FRAME_WINDOW_SIZES_CHANGED (f))
11582 {
11583 Lisp_Object functions;
11584
11585 /* Clear flag first in case we get an error below. */
11586 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11587 functions = Vwindow_size_change_functions;
11588 GCPRO2 (tail, functions);
11589
11590 while (CONSP (functions))
11591 {
11592 if (!EQ (XCAR (functions), Qt))
11593 call1 (XCAR (functions), frame);
11594 functions = XCDR (functions);
11595 }
11596 UNGCPRO;
11597 }
11598
11599 GCPRO1 (tail);
11600 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11601 #ifdef HAVE_WINDOW_SYSTEM
11602 update_tool_bar (f, 0);
11603 #endif
11604 #ifdef HAVE_NS
11605 if (windows_or_buffers_changed
11606 && FRAME_NS_P (f))
11607 ns_set_doc_edited
11608 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11609 #endif
11610 UNGCPRO;
11611 }
11612
11613 unbind_to (count, Qnil);
11614 }
11615 else
11616 {
11617 struct frame *sf = SELECTED_FRAME ();
11618 update_menu_bar (sf, 1, 0);
11619 #ifdef HAVE_WINDOW_SYSTEM
11620 update_tool_bar (sf, 1);
11621 #endif
11622 }
11623 }
11624
11625
11626 /* Update the menu bar item list for frame F. This has to be done
11627 before we start to fill in any display lines, because it can call
11628 eval.
11629
11630 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11631
11632 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11633 already ran the menu bar hooks for this redisplay, so there
11634 is no need to run them again. The return value is the
11635 updated value of this flag, to pass to the next call. */
11636
11637 static int
11638 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11639 {
11640 Lisp_Object window;
11641 register struct window *w;
11642
11643 /* If called recursively during a menu update, do nothing. This can
11644 happen when, for instance, an activate-menubar-hook causes a
11645 redisplay. */
11646 if (inhibit_menubar_update)
11647 return hooks_run;
11648
11649 window = FRAME_SELECTED_WINDOW (f);
11650 w = XWINDOW (window);
11651
11652 if (FRAME_WINDOW_P (f)
11653 ?
11654 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11655 || defined (HAVE_NS) || defined (USE_GTK)
11656 FRAME_EXTERNAL_MENU_BAR (f)
11657 #else
11658 FRAME_MENU_BAR_LINES (f) > 0
11659 #endif
11660 : FRAME_MENU_BAR_LINES (f) > 0)
11661 {
11662 /* If the user has switched buffers or windows, we need to
11663 recompute to reflect the new bindings. But we'll
11664 recompute when update_mode_lines is set too; that means
11665 that people can use force-mode-line-update to request
11666 that the menu bar be recomputed. The adverse effect on
11667 the rest of the redisplay algorithm is about the same as
11668 windows_or_buffers_changed anyway. */
11669 if (windows_or_buffers_changed
11670 /* This used to test w->update_mode_line, but we believe
11671 there is no need to recompute the menu in that case. */
11672 || update_mode_lines
11673 || window_buffer_changed (w))
11674 {
11675 struct buffer *prev = current_buffer;
11676 ptrdiff_t count = SPECPDL_INDEX ();
11677
11678 specbind (Qinhibit_menubar_update, Qt);
11679
11680 set_buffer_internal_1 (XBUFFER (w->contents));
11681 if (save_match_data)
11682 record_unwind_save_match_data ();
11683 if (NILP (Voverriding_local_map_menu_flag))
11684 {
11685 specbind (Qoverriding_terminal_local_map, Qnil);
11686 specbind (Qoverriding_local_map, Qnil);
11687 }
11688
11689 if (!hooks_run)
11690 {
11691 /* Run the Lucid hook. */
11692 safe_run_hooks (Qactivate_menubar_hook);
11693
11694 /* If it has changed current-menubar from previous value,
11695 really recompute the menu-bar from the value. */
11696 if (! NILP (Vlucid_menu_bar_dirty_flag))
11697 call0 (Qrecompute_lucid_menubar);
11698
11699 safe_run_hooks (Qmenu_bar_update_hook);
11700
11701 hooks_run = 1;
11702 }
11703
11704 XSETFRAME (Vmenu_updating_frame, f);
11705 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11706
11707 /* Redisplay the menu bar in case we changed it. */
11708 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11709 || defined (HAVE_NS) || defined (USE_GTK)
11710 if (FRAME_WINDOW_P (f))
11711 {
11712 #if defined (HAVE_NS)
11713 /* All frames on Mac OS share the same menubar. So only
11714 the selected frame should be allowed to set it. */
11715 if (f == SELECTED_FRAME ())
11716 #endif
11717 set_frame_menubar (f, 0, 0);
11718 }
11719 else
11720 /* On a terminal screen, the menu bar is an ordinary screen
11721 line, and this makes it get updated. */
11722 w->update_mode_line = 1;
11723 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11724 /* In the non-toolkit version, the menu bar is an ordinary screen
11725 line, and this makes it get updated. */
11726 w->update_mode_line = 1;
11727 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11728
11729 unbind_to (count, Qnil);
11730 set_buffer_internal_1 (prev);
11731 }
11732 }
11733
11734 return hooks_run;
11735 }
11736
11737 /***********************************************************************
11738 Tool-bars
11739 ***********************************************************************/
11740
11741 #ifdef HAVE_WINDOW_SYSTEM
11742
11743 /* Tool-bar item index of the item on which a mouse button was pressed
11744 or -1. */
11745
11746 int last_tool_bar_item;
11747
11748 /* Select `frame' temporarily without running all the code in
11749 do_switch_frame.
11750 FIXME: Maybe do_switch_frame should be trimmed down similarly
11751 when `norecord' is set. */
11752 static void
11753 fast_set_selected_frame (Lisp_Object frame)
11754 {
11755 if (!EQ (selected_frame, frame))
11756 {
11757 selected_frame = frame;
11758 selected_window = XFRAME (frame)->selected_window;
11759 }
11760 }
11761
11762 /* Update the tool-bar item list for frame F. This has to be done
11763 before we start to fill in any display lines. Called from
11764 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11765 and restore it here. */
11766
11767 static void
11768 update_tool_bar (struct frame *f, int save_match_data)
11769 {
11770 #if defined (USE_GTK) || defined (HAVE_NS)
11771 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11772 #else
11773 int do_update = (WINDOWP (f->tool_bar_window)
11774 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11775 #endif
11776
11777 if (do_update)
11778 {
11779 Lisp_Object window;
11780 struct window *w;
11781
11782 window = FRAME_SELECTED_WINDOW (f);
11783 w = XWINDOW (window);
11784
11785 /* If the user has switched buffers or windows, we need to
11786 recompute to reflect the new bindings. But we'll
11787 recompute when update_mode_lines is set too; that means
11788 that people can use force-mode-line-update to request
11789 that the menu bar be recomputed. The adverse effect on
11790 the rest of the redisplay algorithm is about the same as
11791 windows_or_buffers_changed anyway. */
11792 if (windows_or_buffers_changed
11793 || w->update_mode_line
11794 || update_mode_lines
11795 || window_buffer_changed (w))
11796 {
11797 struct buffer *prev = current_buffer;
11798 ptrdiff_t count = SPECPDL_INDEX ();
11799 Lisp_Object frame, new_tool_bar;
11800 int new_n_tool_bar;
11801 struct gcpro gcpro1;
11802
11803 /* Set current_buffer to the buffer of the selected
11804 window of the frame, so that we get the right local
11805 keymaps. */
11806 set_buffer_internal_1 (XBUFFER (w->contents));
11807
11808 /* Save match data, if we must. */
11809 if (save_match_data)
11810 record_unwind_save_match_data ();
11811
11812 /* Make sure that we don't accidentally use bogus keymaps. */
11813 if (NILP (Voverriding_local_map_menu_flag))
11814 {
11815 specbind (Qoverriding_terminal_local_map, Qnil);
11816 specbind (Qoverriding_local_map, Qnil);
11817 }
11818
11819 GCPRO1 (new_tool_bar);
11820
11821 /* We must temporarily set the selected frame to this frame
11822 before calling tool_bar_items, because the calculation of
11823 the tool-bar keymap uses the selected frame (see
11824 `tool-bar-make-keymap' in tool-bar.el). */
11825 eassert (EQ (selected_window,
11826 /* Since we only explicitly preserve selected_frame,
11827 check that selected_window would be redundant. */
11828 XFRAME (selected_frame)->selected_window));
11829 record_unwind_protect (fast_set_selected_frame, selected_frame);
11830 XSETFRAME (frame, f);
11831 fast_set_selected_frame (frame);
11832
11833 /* Build desired tool-bar items from keymaps. */
11834 new_tool_bar
11835 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11836 &new_n_tool_bar);
11837
11838 /* Redisplay the tool-bar if we changed it. */
11839 if (new_n_tool_bar != f->n_tool_bar_items
11840 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11841 {
11842 /* Redisplay that happens asynchronously due to an expose event
11843 may access f->tool_bar_items. Make sure we update both
11844 variables within BLOCK_INPUT so no such event interrupts. */
11845 block_input ();
11846 fset_tool_bar_items (f, new_tool_bar);
11847 f->n_tool_bar_items = new_n_tool_bar;
11848 w->update_mode_line = 1;
11849 unblock_input ();
11850 }
11851
11852 UNGCPRO;
11853
11854 unbind_to (count, Qnil);
11855 set_buffer_internal_1 (prev);
11856 }
11857 }
11858 }
11859
11860 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11861
11862 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11863 F's desired tool-bar contents. F->tool_bar_items must have
11864 been set up previously by calling prepare_menu_bars. */
11865
11866 static void
11867 build_desired_tool_bar_string (struct frame *f)
11868 {
11869 int i, size, size_needed;
11870 struct gcpro gcpro1, gcpro2, gcpro3;
11871 Lisp_Object image, plist, props;
11872
11873 image = plist = props = Qnil;
11874 GCPRO3 (image, plist, props);
11875
11876 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11877 Otherwise, make a new string. */
11878
11879 /* The size of the string we might be able to reuse. */
11880 size = (STRINGP (f->desired_tool_bar_string)
11881 ? SCHARS (f->desired_tool_bar_string)
11882 : 0);
11883
11884 /* We need one space in the string for each image. */
11885 size_needed = f->n_tool_bar_items;
11886
11887 /* Reuse f->desired_tool_bar_string, if possible. */
11888 if (size < size_needed || NILP (f->desired_tool_bar_string))
11889 fset_desired_tool_bar_string
11890 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11891 else
11892 {
11893 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11894 Fremove_text_properties (make_number (0), make_number (size),
11895 props, f->desired_tool_bar_string);
11896 }
11897
11898 /* Put a `display' property on the string for the images to display,
11899 put a `menu_item' property on tool-bar items with a value that
11900 is the index of the item in F's tool-bar item vector. */
11901 for (i = 0; i < f->n_tool_bar_items; ++i)
11902 {
11903 #define PROP(IDX) \
11904 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11905
11906 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11907 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11908 int hmargin, vmargin, relief, idx, end;
11909
11910 /* If image is a vector, choose the image according to the
11911 button state. */
11912 image = PROP (TOOL_BAR_ITEM_IMAGES);
11913 if (VECTORP (image))
11914 {
11915 if (enabled_p)
11916 idx = (selected_p
11917 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11918 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11919 else
11920 idx = (selected_p
11921 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11922 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11923
11924 eassert (ASIZE (image) >= idx);
11925 image = AREF (image, idx);
11926 }
11927 else
11928 idx = -1;
11929
11930 /* Ignore invalid image specifications. */
11931 if (!valid_image_p (image))
11932 continue;
11933
11934 /* Display the tool-bar button pressed, or depressed. */
11935 plist = Fcopy_sequence (XCDR (image));
11936
11937 /* Compute margin and relief to draw. */
11938 relief = (tool_bar_button_relief >= 0
11939 ? tool_bar_button_relief
11940 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11941 hmargin = vmargin = relief;
11942
11943 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11944 INT_MAX - max (hmargin, vmargin)))
11945 {
11946 hmargin += XFASTINT (Vtool_bar_button_margin);
11947 vmargin += XFASTINT (Vtool_bar_button_margin);
11948 }
11949 else if (CONSP (Vtool_bar_button_margin))
11950 {
11951 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11952 INT_MAX - hmargin))
11953 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11954
11955 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11956 INT_MAX - vmargin))
11957 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11958 }
11959
11960 if (auto_raise_tool_bar_buttons_p)
11961 {
11962 /* Add a `:relief' property to the image spec if the item is
11963 selected. */
11964 if (selected_p)
11965 {
11966 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11967 hmargin -= relief;
11968 vmargin -= relief;
11969 }
11970 }
11971 else
11972 {
11973 /* If image is selected, display it pressed, i.e. with a
11974 negative relief. If it's not selected, display it with a
11975 raised relief. */
11976 plist = Fplist_put (plist, QCrelief,
11977 (selected_p
11978 ? make_number (-relief)
11979 : make_number (relief)));
11980 hmargin -= relief;
11981 vmargin -= relief;
11982 }
11983
11984 /* Put a margin around the image. */
11985 if (hmargin || vmargin)
11986 {
11987 if (hmargin == vmargin)
11988 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11989 else
11990 plist = Fplist_put (plist, QCmargin,
11991 Fcons (make_number (hmargin),
11992 make_number (vmargin)));
11993 }
11994
11995 /* If button is not enabled, and we don't have special images
11996 for the disabled state, make the image appear disabled by
11997 applying an appropriate algorithm to it. */
11998 if (!enabled_p && idx < 0)
11999 plist = Fplist_put (plist, QCconversion, Qdisabled);
12000
12001 /* Put a `display' text property on the string for the image to
12002 display. Put a `menu-item' property on the string that gives
12003 the start of this item's properties in the tool-bar items
12004 vector. */
12005 image = Fcons (Qimage, plist);
12006 props = list4 (Qdisplay, image,
12007 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
12008
12009 /* Let the last image hide all remaining spaces in the tool bar
12010 string. The string can be longer than needed when we reuse a
12011 previous string. */
12012 if (i + 1 == f->n_tool_bar_items)
12013 end = SCHARS (f->desired_tool_bar_string);
12014 else
12015 end = i + 1;
12016 Fadd_text_properties (make_number (i), make_number (end),
12017 props, f->desired_tool_bar_string);
12018 #undef PROP
12019 }
12020
12021 UNGCPRO;
12022 }
12023
12024
12025 /* Display one line of the tool-bar of frame IT->f.
12026
12027 HEIGHT specifies the desired height of the tool-bar line.
12028 If the actual height of the glyph row is less than HEIGHT, the
12029 row's height is increased to HEIGHT, and the icons are centered
12030 vertically in the new height.
12031
12032 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12033 count a final empty row in case the tool-bar width exactly matches
12034 the window width.
12035 */
12036
12037 static void
12038 display_tool_bar_line (struct it *it, int height)
12039 {
12040 struct glyph_row *row = it->glyph_row;
12041 int max_x = it->last_visible_x;
12042 struct glyph *last;
12043
12044 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12045 clear_glyph_row (row);
12046 row->enabled_p = true;
12047 row->y = it->current_y;
12048
12049 /* Note that this isn't made use of if the face hasn't a box,
12050 so there's no need to check the face here. */
12051 it->start_of_box_run_p = 1;
12052
12053 while (it->current_x < max_x)
12054 {
12055 int x, n_glyphs_before, i, nglyphs;
12056 struct it it_before;
12057
12058 /* Get the next display element. */
12059 if (!get_next_display_element (it))
12060 {
12061 /* Don't count empty row if we are counting needed tool-bar lines. */
12062 if (height < 0 && !it->hpos)
12063 return;
12064 break;
12065 }
12066
12067 /* Produce glyphs. */
12068 n_glyphs_before = row->used[TEXT_AREA];
12069 it_before = *it;
12070
12071 PRODUCE_GLYPHS (it);
12072
12073 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12074 i = 0;
12075 x = it_before.current_x;
12076 while (i < nglyphs)
12077 {
12078 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12079
12080 if (x + glyph->pixel_width > max_x)
12081 {
12082 /* Glyph doesn't fit on line. Backtrack. */
12083 row->used[TEXT_AREA] = n_glyphs_before;
12084 *it = it_before;
12085 /* If this is the only glyph on this line, it will never fit on the
12086 tool-bar, so skip it. But ensure there is at least one glyph,
12087 so we don't accidentally disable the tool-bar. */
12088 if (n_glyphs_before == 0
12089 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12090 break;
12091 goto out;
12092 }
12093
12094 ++it->hpos;
12095 x += glyph->pixel_width;
12096 ++i;
12097 }
12098
12099 /* Stop at line end. */
12100 if (ITERATOR_AT_END_OF_LINE_P (it))
12101 break;
12102
12103 set_iterator_to_next (it, 1);
12104 }
12105
12106 out:;
12107
12108 row->displays_text_p = row->used[TEXT_AREA] != 0;
12109
12110 /* Use default face for the border below the tool bar.
12111
12112 FIXME: When auto-resize-tool-bars is grow-only, there is
12113 no additional border below the possibly empty tool-bar lines.
12114 So to make the extra empty lines look "normal", we have to
12115 use the tool-bar face for the border too. */
12116 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12117 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12118 it->face_id = DEFAULT_FACE_ID;
12119
12120 extend_face_to_end_of_line (it);
12121 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12122 last->right_box_line_p = 1;
12123 if (last == row->glyphs[TEXT_AREA])
12124 last->left_box_line_p = 1;
12125
12126 /* Make line the desired height and center it vertically. */
12127 if ((height -= it->max_ascent + it->max_descent) > 0)
12128 {
12129 /* Don't add more than one line height. */
12130 height %= FRAME_LINE_HEIGHT (it->f);
12131 it->max_ascent += height / 2;
12132 it->max_descent += (height + 1) / 2;
12133 }
12134
12135 compute_line_metrics (it);
12136
12137 /* If line is empty, make it occupy the rest of the tool-bar. */
12138 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12139 {
12140 row->height = row->phys_height = it->last_visible_y - row->y;
12141 row->visible_height = row->height;
12142 row->ascent = row->phys_ascent = 0;
12143 row->extra_line_spacing = 0;
12144 }
12145
12146 row->full_width_p = 1;
12147 row->continued_p = 0;
12148 row->truncated_on_left_p = 0;
12149 row->truncated_on_right_p = 0;
12150
12151 it->current_x = it->hpos = 0;
12152 it->current_y += row->height;
12153 ++it->vpos;
12154 ++it->glyph_row;
12155 }
12156
12157
12158 /* Max tool-bar height. Basically, this is what makes all other windows
12159 disappear when the frame gets too small. Rethink this! */
12160
12161 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12162 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12163
12164 /* Value is the number of pixels needed to make all tool-bar items of
12165 frame F visible. The actual number of glyph rows needed is
12166 returned in *N_ROWS if non-NULL. */
12167
12168 static int
12169 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12170 {
12171 struct window *w = XWINDOW (f->tool_bar_window);
12172 struct it it;
12173 /* tool_bar_height is called from redisplay_tool_bar after building
12174 the desired matrix, so use (unused) mode-line row as temporary row to
12175 avoid destroying the first tool-bar row. */
12176 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12177
12178 /* Initialize an iterator for iteration over
12179 F->desired_tool_bar_string in the tool-bar window of frame F. */
12180 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12181 it.first_visible_x = 0;
12182 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12183 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12184 it.paragraph_embedding = L2R;
12185
12186 while (!ITERATOR_AT_END_P (&it))
12187 {
12188 clear_glyph_row (temp_row);
12189 it.glyph_row = temp_row;
12190 display_tool_bar_line (&it, -1);
12191 }
12192 clear_glyph_row (temp_row);
12193
12194 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12195 if (n_rows)
12196 *n_rows = it.vpos > 0 ? it.vpos : -1;
12197
12198 if (pixelwise)
12199 return it.current_y;
12200 else
12201 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12202 }
12203
12204 #endif /* !USE_GTK && !HAVE_NS */
12205
12206 #if defined USE_GTK || defined HAVE_NS
12207 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12208 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12209 #endif
12210
12211 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12212 0, 2, 0,
12213 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12214 If FRAME is nil or omitted, use the selected frame. Optional argument
12215 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12216 (Lisp_Object frame, Lisp_Object pixelwise)
12217 {
12218 int height = 0;
12219
12220 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12221 struct frame *f = decode_any_frame (frame);
12222
12223 if (WINDOWP (f->tool_bar_window)
12224 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12225 {
12226 update_tool_bar (f, 1);
12227 if (f->n_tool_bar_items)
12228 {
12229 build_desired_tool_bar_string (f);
12230 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12231 }
12232 }
12233 #endif
12234
12235 return make_number (height);
12236 }
12237
12238
12239 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12240 height should be changed. */
12241
12242 static int
12243 redisplay_tool_bar (struct frame *f)
12244 {
12245 #if defined (USE_GTK) || defined (HAVE_NS)
12246
12247 if (FRAME_EXTERNAL_TOOL_BAR (f))
12248 update_frame_tool_bar (f);
12249 return 0;
12250
12251 #else /* !USE_GTK && !HAVE_NS */
12252
12253 struct window *w;
12254 struct it it;
12255 struct glyph_row *row;
12256
12257 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12258 do anything. This means you must start with tool-bar-lines
12259 non-zero to get the auto-sizing effect. Or in other words, you
12260 can turn off tool-bars by specifying tool-bar-lines zero. */
12261 if (!WINDOWP (f->tool_bar_window)
12262 || (w = XWINDOW (f->tool_bar_window),
12263 WINDOW_PIXEL_HEIGHT (w) == 0))
12264 return 0;
12265
12266 /* Set up an iterator for the tool-bar window. */
12267 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12268 it.first_visible_x = 0;
12269 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12270 row = it.glyph_row;
12271
12272 /* Build a string that represents the contents of the tool-bar. */
12273 build_desired_tool_bar_string (f);
12274 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12275 /* FIXME: This should be controlled by a user option. But it
12276 doesn't make sense to have an R2L tool bar if the menu bar cannot
12277 be drawn also R2L, and making the menu bar R2L is tricky due
12278 toolkit-specific code that implements it. If an R2L tool bar is
12279 ever supported, display_tool_bar_line should also be augmented to
12280 call unproduce_glyphs like display_line and display_string
12281 do. */
12282 it.paragraph_embedding = L2R;
12283
12284 if (f->n_tool_bar_rows == 0)
12285 {
12286 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12287
12288 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12289 {
12290 Lisp_Object frame;
12291 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12292 / FRAME_LINE_HEIGHT (f));
12293
12294 XSETFRAME (frame, f);
12295 Fmodify_frame_parameters (frame,
12296 list1 (Fcons (Qtool_bar_lines,
12297 make_number (new_lines))));
12298 /* Always do that now. */
12299 clear_glyph_matrix (w->desired_matrix);
12300 f->fonts_changed = 1;
12301 return 1;
12302 }
12303 }
12304
12305 /* Display as many lines as needed to display all tool-bar items. */
12306
12307 if (f->n_tool_bar_rows > 0)
12308 {
12309 int border, rows, height, extra;
12310
12311 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12312 border = XINT (Vtool_bar_border);
12313 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12314 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12315 else if (EQ (Vtool_bar_border, Qborder_width))
12316 border = f->border_width;
12317 else
12318 border = 0;
12319 if (border < 0)
12320 border = 0;
12321
12322 rows = f->n_tool_bar_rows;
12323 height = max (1, (it.last_visible_y - border) / rows);
12324 extra = it.last_visible_y - border - height * rows;
12325
12326 while (it.current_y < it.last_visible_y)
12327 {
12328 int h = 0;
12329 if (extra > 0 && rows-- > 0)
12330 {
12331 h = (extra + rows - 1) / rows;
12332 extra -= h;
12333 }
12334 display_tool_bar_line (&it, height + h);
12335 }
12336 }
12337 else
12338 {
12339 while (it.current_y < it.last_visible_y)
12340 display_tool_bar_line (&it, 0);
12341 }
12342
12343 /* It doesn't make much sense to try scrolling in the tool-bar
12344 window, so don't do it. */
12345 w->desired_matrix->no_scrolling_p = 1;
12346 w->must_be_updated_p = 1;
12347
12348 if (!NILP (Vauto_resize_tool_bars))
12349 {
12350 /* Do we really allow the toolbar to occupy the whole frame? */
12351 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12352 int change_height_p = 0;
12353
12354 /* If we couldn't display everything, change the tool-bar's
12355 height if there is room for more. */
12356 if (IT_STRING_CHARPOS (it) < it.end_charpos
12357 && it.current_y < max_tool_bar_height)
12358 change_height_p = 1;
12359
12360 /* We subtract 1 because display_tool_bar_line advances the
12361 glyph_row pointer before returning to its caller. We want to
12362 examine the last glyph row produced by
12363 display_tool_bar_line. */
12364 row = it.glyph_row - 1;
12365
12366 /* If there are blank lines at the end, except for a partially
12367 visible blank line at the end that is smaller than
12368 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12369 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12370 && row->height >= FRAME_LINE_HEIGHT (f))
12371 change_height_p = 1;
12372
12373 /* If row displays tool-bar items, but is partially visible,
12374 change the tool-bar's height. */
12375 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12376 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12377 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12378 change_height_p = 1;
12379
12380 /* Resize windows as needed by changing the `tool-bar-lines'
12381 frame parameter. */
12382 if (change_height_p)
12383 {
12384 Lisp_Object frame;
12385 int nrows;
12386 int new_height = tool_bar_height (f, &nrows, 1);
12387
12388 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12389 && !f->minimize_tool_bar_window_p)
12390 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12391 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12392 f->minimize_tool_bar_window_p = 0;
12393
12394 if (change_height_p)
12395 {
12396 /* Current size of the tool-bar window in canonical line
12397 units. */
12398 int old_lines = WINDOW_TOTAL_LINES (w);
12399 /* Required size of the tool-bar window in canonical
12400 line units. */
12401 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12402 / FRAME_LINE_HEIGHT (f));
12403 /* Maximum size of the tool-bar window in canonical line
12404 units that this frame can allow. */
12405 int max_lines =
12406 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12407
12408 /* Don't try to change the tool-bar window size and set
12409 the fonts_changed flag unless really necessary. That
12410 flag causes redisplay to give up and retry
12411 redisplaying the frame from scratch, so setting it
12412 unnecessarily can lead to nasty redisplay loops. */
12413 if (new_lines <= max_lines
12414 && eabs (new_lines - old_lines) >= 1)
12415 {
12416 XSETFRAME (frame, f);
12417 Fmodify_frame_parameters (frame,
12418 list1 (Fcons (Qtool_bar_lines,
12419 make_number (new_lines))));
12420 clear_glyph_matrix (w->desired_matrix);
12421 f->n_tool_bar_rows = nrows;
12422 f->fonts_changed = 1;
12423 return 1;
12424 }
12425 }
12426 }
12427 }
12428
12429 f->minimize_tool_bar_window_p = 0;
12430 return 0;
12431
12432 #endif /* USE_GTK || HAVE_NS */
12433 }
12434
12435 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12436
12437 /* Get information about the tool-bar item which is displayed in GLYPH
12438 on frame F. Return in *PROP_IDX the index where tool-bar item
12439 properties start in F->tool_bar_items. Value is zero if
12440 GLYPH doesn't display a tool-bar item. */
12441
12442 static int
12443 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12444 {
12445 Lisp_Object prop;
12446 int success_p;
12447 int charpos;
12448
12449 /* This function can be called asynchronously, which means we must
12450 exclude any possibility that Fget_text_property signals an
12451 error. */
12452 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12453 charpos = max (0, charpos);
12454
12455 /* Get the text property `menu-item' at pos. The value of that
12456 property is the start index of this item's properties in
12457 F->tool_bar_items. */
12458 prop = Fget_text_property (make_number (charpos),
12459 Qmenu_item, f->current_tool_bar_string);
12460 if (INTEGERP (prop))
12461 {
12462 *prop_idx = XINT (prop);
12463 success_p = 1;
12464 }
12465 else
12466 success_p = 0;
12467
12468 return success_p;
12469 }
12470
12471 \f
12472 /* Get information about the tool-bar item at position X/Y on frame F.
12473 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12474 the current matrix of the tool-bar window of F, or NULL if not
12475 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12476 item in F->tool_bar_items. Value is
12477
12478 -1 if X/Y is not on a tool-bar item
12479 0 if X/Y is on the same item that was highlighted before.
12480 1 otherwise. */
12481
12482 static int
12483 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12484 int *hpos, int *vpos, int *prop_idx)
12485 {
12486 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12487 struct window *w = XWINDOW (f->tool_bar_window);
12488 int area;
12489
12490 /* Find the glyph under X/Y. */
12491 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12492 if (*glyph == NULL)
12493 return -1;
12494
12495 /* Get the start of this tool-bar item's properties in
12496 f->tool_bar_items. */
12497 if (!tool_bar_item_info (f, *glyph, prop_idx))
12498 return -1;
12499
12500 /* Is mouse on the highlighted item? */
12501 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12502 && *vpos >= hlinfo->mouse_face_beg_row
12503 && *vpos <= hlinfo->mouse_face_end_row
12504 && (*vpos > hlinfo->mouse_face_beg_row
12505 || *hpos >= hlinfo->mouse_face_beg_col)
12506 && (*vpos < hlinfo->mouse_face_end_row
12507 || *hpos < hlinfo->mouse_face_end_col
12508 || hlinfo->mouse_face_past_end))
12509 return 0;
12510
12511 return 1;
12512 }
12513
12514
12515 /* EXPORT:
12516 Handle mouse button event on the tool-bar of frame F, at
12517 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12518 0 for button release. MODIFIERS is event modifiers for button
12519 release. */
12520
12521 void
12522 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12523 int modifiers)
12524 {
12525 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12526 struct window *w = XWINDOW (f->tool_bar_window);
12527 int hpos, vpos, prop_idx;
12528 struct glyph *glyph;
12529 Lisp_Object enabled_p;
12530 int ts;
12531
12532 /* If not on the highlighted tool-bar item, and mouse-highlight is
12533 non-nil, return. This is so we generate the tool-bar button
12534 click only when the mouse button is released on the same item as
12535 where it was pressed. However, when mouse-highlight is disabled,
12536 generate the click when the button is released regardless of the
12537 highlight, since tool-bar items are not highlighted in that
12538 case. */
12539 frame_to_window_pixel_xy (w, &x, &y);
12540 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12541 if (ts == -1
12542 || (ts != 0 && !NILP (Vmouse_highlight)))
12543 return;
12544
12545 /* When mouse-highlight is off, generate the click for the item
12546 where the button was pressed, disregarding where it was
12547 released. */
12548 if (NILP (Vmouse_highlight) && !down_p)
12549 prop_idx = last_tool_bar_item;
12550
12551 /* If item is disabled, do nothing. */
12552 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12553 if (NILP (enabled_p))
12554 return;
12555
12556 if (down_p)
12557 {
12558 /* Show item in pressed state. */
12559 if (!NILP (Vmouse_highlight))
12560 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12561 last_tool_bar_item = prop_idx;
12562 }
12563 else
12564 {
12565 Lisp_Object key, frame;
12566 struct input_event event;
12567 EVENT_INIT (event);
12568
12569 /* Show item in released state. */
12570 if (!NILP (Vmouse_highlight))
12571 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12572
12573 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12574
12575 XSETFRAME (frame, f);
12576 event.kind = TOOL_BAR_EVENT;
12577 event.frame_or_window = frame;
12578 event.arg = frame;
12579 kbd_buffer_store_event (&event);
12580
12581 event.kind = TOOL_BAR_EVENT;
12582 event.frame_or_window = frame;
12583 event.arg = key;
12584 event.modifiers = modifiers;
12585 kbd_buffer_store_event (&event);
12586 last_tool_bar_item = -1;
12587 }
12588 }
12589
12590
12591 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12592 tool-bar window-relative coordinates X/Y. Called from
12593 note_mouse_highlight. */
12594
12595 static void
12596 note_tool_bar_highlight (struct frame *f, int x, int y)
12597 {
12598 Lisp_Object window = f->tool_bar_window;
12599 struct window *w = XWINDOW (window);
12600 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12601 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12602 int hpos, vpos;
12603 struct glyph *glyph;
12604 struct glyph_row *row;
12605 int i;
12606 Lisp_Object enabled_p;
12607 int prop_idx;
12608 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12609 int mouse_down_p, rc;
12610
12611 /* Function note_mouse_highlight is called with negative X/Y
12612 values when mouse moves outside of the frame. */
12613 if (x <= 0 || y <= 0)
12614 {
12615 clear_mouse_face (hlinfo);
12616 return;
12617 }
12618
12619 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12620 if (rc < 0)
12621 {
12622 /* Not on tool-bar item. */
12623 clear_mouse_face (hlinfo);
12624 return;
12625 }
12626 else if (rc == 0)
12627 /* On same tool-bar item as before. */
12628 goto set_help_echo;
12629
12630 clear_mouse_face (hlinfo);
12631
12632 /* Mouse is down, but on different tool-bar item? */
12633 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12634 && f == dpyinfo->last_mouse_frame);
12635
12636 if (mouse_down_p
12637 && last_tool_bar_item != prop_idx)
12638 return;
12639
12640 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12641
12642 /* If tool-bar item is not enabled, don't highlight it. */
12643 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12644 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12645 {
12646 /* Compute the x-position of the glyph. In front and past the
12647 image is a space. We include this in the highlighted area. */
12648 row = MATRIX_ROW (w->current_matrix, vpos);
12649 for (i = x = 0; i < hpos; ++i)
12650 x += row->glyphs[TEXT_AREA][i].pixel_width;
12651
12652 /* Record this as the current active region. */
12653 hlinfo->mouse_face_beg_col = hpos;
12654 hlinfo->mouse_face_beg_row = vpos;
12655 hlinfo->mouse_face_beg_x = x;
12656 hlinfo->mouse_face_past_end = 0;
12657
12658 hlinfo->mouse_face_end_col = hpos + 1;
12659 hlinfo->mouse_face_end_row = vpos;
12660 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12661 hlinfo->mouse_face_window = window;
12662 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12663
12664 /* Display it as active. */
12665 show_mouse_face (hlinfo, draw);
12666 }
12667
12668 set_help_echo:
12669
12670 /* Set help_echo_string to a help string to display for this tool-bar item.
12671 XTread_socket does the rest. */
12672 help_echo_object = help_echo_window = Qnil;
12673 help_echo_pos = -1;
12674 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12675 if (NILP (help_echo_string))
12676 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12677 }
12678
12679 #endif /* !USE_GTK && !HAVE_NS */
12680
12681 #endif /* HAVE_WINDOW_SYSTEM */
12682
12683
12684 \f
12685 /************************************************************************
12686 Horizontal scrolling
12687 ************************************************************************/
12688
12689 static int hscroll_window_tree (Lisp_Object);
12690 static int hscroll_windows (Lisp_Object);
12691
12692 /* For all leaf windows in the window tree rooted at WINDOW, set their
12693 hscroll value so that PT is (i) visible in the window, and (ii) so
12694 that it is not within a certain margin at the window's left and
12695 right border. Value is non-zero if any window's hscroll has been
12696 changed. */
12697
12698 static int
12699 hscroll_window_tree (Lisp_Object window)
12700 {
12701 int hscrolled_p = 0;
12702 int hscroll_relative_p = FLOATP (Vhscroll_step);
12703 int hscroll_step_abs = 0;
12704 double hscroll_step_rel = 0;
12705
12706 if (hscroll_relative_p)
12707 {
12708 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12709 if (hscroll_step_rel < 0)
12710 {
12711 hscroll_relative_p = 0;
12712 hscroll_step_abs = 0;
12713 }
12714 }
12715 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12716 {
12717 hscroll_step_abs = XINT (Vhscroll_step);
12718 if (hscroll_step_abs < 0)
12719 hscroll_step_abs = 0;
12720 }
12721 else
12722 hscroll_step_abs = 0;
12723
12724 while (WINDOWP (window))
12725 {
12726 struct window *w = XWINDOW (window);
12727
12728 if (WINDOWP (w->contents))
12729 hscrolled_p |= hscroll_window_tree (w->contents);
12730 else if (w->cursor.vpos >= 0)
12731 {
12732 int h_margin;
12733 int text_area_width;
12734 struct glyph_row *cursor_row;
12735 struct glyph_row *bottom_row;
12736 int row_r2l_p;
12737
12738 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12739 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12740 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12741 else
12742 cursor_row = bottom_row - 1;
12743
12744 if (!cursor_row->enabled_p)
12745 {
12746 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12747 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12748 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12749 else
12750 cursor_row = bottom_row - 1;
12751 }
12752 row_r2l_p = cursor_row->reversed_p;
12753
12754 text_area_width = window_box_width (w, TEXT_AREA);
12755
12756 /* Scroll when cursor is inside this scroll margin. */
12757 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12758
12759 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12760 /* For left-to-right rows, hscroll when cursor is either
12761 (i) inside the right hscroll margin, or (ii) if it is
12762 inside the left margin and the window is already
12763 hscrolled. */
12764 && ((!row_r2l_p
12765 && ((w->hscroll
12766 && w->cursor.x <= h_margin)
12767 || (cursor_row->enabled_p
12768 && cursor_row->truncated_on_right_p
12769 && (w->cursor.x >= text_area_width - h_margin))))
12770 /* For right-to-left rows, the logic is similar,
12771 except that rules for scrolling to left and right
12772 are reversed. E.g., if cursor.x <= h_margin, we
12773 need to hscroll "to the right" unconditionally,
12774 and that will scroll the screen to the left so as
12775 to reveal the next portion of the row. */
12776 || (row_r2l_p
12777 && ((cursor_row->enabled_p
12778 /* FIXME: It is confusing to set the
12779 truncated_on_right_p flag when R2L rows
12780 are actually truncated on the left. */
12781 && cursor_row->truncated_on_right_p
12782 && w->cursor.x <= h_margin)
12783 || (w->hscroll
12784 && (w->cursor.x >= text_area_width - h_margin))))))
12785 {
12786 struct it it;
12787 ptrdiff_t hscroll;
12788 struct buffer *saved_current_buffer;
12789 ptrdiff_t pt;
12790 int wanted_x;
12791
12792 /* Find point in a display of infinite width. */
12793 saved_current_buffer = current_buffer;
12794 current_buffer = XBUFFER (w->contents);
12795
12796 if (w == XWINDOW (selected_window))
12797 pt = PT;
12798 else
12799 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12800
12801 /* Move iterator to pt starting at cursor_row->start in
12802 a line with infinite width. */
12803 init_to_row_start (&it, w, cursor_row);
12804 it.last_visible_x = INFINITY;
12805 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12806 current_buffer = saved_current_buffer;
12807
12808 /* Position cursor in window. */
12809 if (!hscroll_relative_p && hscroll_step_abs == 0)
12810 hscroll = max (0, (it.current_x
12811 - (ITERATOR_AT_END_OF_LINE_P (&it)
12812 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12813 : (text_area_width / 2))))
12814 / FRAME_COLUMN_WIDTH (it.f);
12815 else if ((!row_r2l_p
12816 && w->cursor.x >= text_area_width - h_margin)
12817 || (row_r2l_p && w->cursor.x <= h_margin))
12818 {
12819 if (hscroll_relative_p)
12820 wanted_x = text_area_width * (1 - hscroll_step_rel)
12821 - h_margin;
12822 else
12823 wanted_x = text_area_width
12824 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12825 - h_margin;
12826 hscroll
12827 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12828 }
12829 else
12830 {
12831 if (hscroll_relative_p)
12832 wanted_x = text_area_width * hscroll_step_rel
12833 + h_margin;
12834 else
12835 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12836 + h_margin;
12837 hscroll
12838 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12839 }
12840 hscroll = max (hscroll, w->min_hscroll);
12841
12842 /* Don't prevent redisplay optimizations if hscroll
12843 hasn't changed, as it will unnecessarily slow down
12844 redisplay. */
12845 if (w->hscroll != hscroll)
12846 {
12847 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12848 w->hscroll = hscroll;
12849 hscrolled_p = 1;
12850 }
12851 }
12852 }
12853
12854 window = w->next;
12855 }
12856
12857 /* Value is non-zero if hscroll of any leaf window has been changed. */
12858 return hscrolled_p;
12859 }
12860
12861
12862 /* Set hscroll so that cursor is visible and not inside horizontal
12863 scroll margins for all windows in the tree rooted at WINDOW. See
12864 also hscroll_window_tree above. Value is non-zero if any window's
12865 hscroll has been changed. If it has, desired matrices on the frame
12866 of WINDOW are cleared. */
12867
12868 static int
12869 hscroll_windows (Lisp_Object window)
12870 {
12871 int hscrolled_p = hscroll_window_tree (window);
12872 if (hscrolled_p)
12873 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12874 return hscrolled_p;
12875 }
12876
12877
12878 \f
12879 /************************************************************************
12880 Redisplay
12881 ************************************************************************/
12882
12883 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12884 to a non-zero value. This is sometimes handy to have in a debugger
12885 session. */
12886
12887 #ifdef GLYPH_DEBUG
12888
12889 /* First and last unchanged row for try_window_id. */
12890
12891 static int debug_first_unchanged_at_end_vpos;
12892 static int debug_last_unchanged_at_beg_vpos;
12893
12894 /* Delta vpos and y. */
12895
12896 static int debug_dvpos, debug_dy;
12897
12898 /* Delta in characters and bytes for try_window_id. */
12899
12900 static ptrdiff_t debug_delta, debug_delta_bytes;
12901
12902 /* Values of window_end_pos and window_end_vpos at the end of
12903 try_window_id. */
12904
12905 static ptrdiff_t debug_end_vpos;
12906
12907 /* Append a string to W->desired_matrix->method. FMT is a printf
12908 format string. If trace_redisplay_p is true also printf the
12909 resulting string to stderr. */
12910
12911 static void debug_method_add (struct window *, char const *, ...)
12912 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12913
12914 static void
12915 debug_method_add (struct window *w, char const *fmt, ...)
12916 {
12917 void *ptr = w;
12918 char *method = w->desired_matrix->method;
12919 int len = strlen (method);
12920 int size = sizeof w->desired_matrix->method;
12921 int remaining = size - len - 1;
12922 va_list ap;
12923
12924 if (len && remaining)
12925 {
12926 method[len] = '|';
12927 --remaining, ++len;
12928 }
12929
12930 va_start (ap, fmt);
12931 vsnprintf (method + len, remaining + 1, fmt, ap);
12932 va_end (ap);
12933
12934 if (trace_redisplay_p)
12935 fprintf (stderr, "%p (%s): %s\n",
12936 ptr,
12937 ((BUFFERP (w->contents)
12938 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12939 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12940 : "no buffer"),
12941 method + len);
12942 }
12943
12944 #endif /* GLYPH_DEBUG */
12945
12946
12947 /* Value is non-zero if all changes in window W, which displays
12948 current_buffer, are in the text between START and END. START is a
12949 buffer position, END is given as a distance from Z. Used in
12950 redisplay_internal for display optimization. */
12951
12952 static int
12953 text_outside_line_unchanged_p (struct window *w,
12954 ptrdiff_t start, ptrdiff_t end)
12955 {
12956 int unchanged_p = 1;
12957
12958 /* If text or overlays have changed, see where. */
12959 if (window_outdated (w))
12960 {
12961 /* Gap in the line? */
12962 if (GPT < start || Z - GPT < end)
12963 unchanged_p = 0;
12964
12965 /* Changes start in front of the line, or end after it? */
12966 if (unchanged_p
12967 && (BEG_UNCHANGED < start - 1
12968 || END_UNCHANGED < end))
12969 unchanged_p = 0;
12970
12971 /* If selective display, can't optimize if changes start at the
12972 beginning of the line. */
12973 if (unchanged_p
12974 && INTEGERP (BVAR (current_buffer, selective_display))
12975 && XINT (BVAR (current_buffer, selective_display)) > 0
12976 && (BEG_UNCHANGED < start || GPT <= start))
12977 unchanged_p = 0;
12978
12979 /* If there are overlays at the start or end of the line, these
12980 may have overlay strings with newlines in them. A change at
12981 START, for instance, may actually concern the display of such
12982 overlay strings as well, and they are displayed on different
12983 lines. So, quickly rule out this case. (For the future, it
12984 might be desirable to implement something more telling than
12985 just BEG/END_UNCHANGED.) */
12986 if (unchanged_p)
12987 {
12988 if (BEG + BEG_UNCHANGED == start
12989 && overlay_touches_p (start))
12990 unchanged_p = 0;
12991 if (END_UNCHANGED == end
12992 && overlay_touches_p (Z - end))
12993 unchanged_p = 0;
12994 }
12995
12996 /* Under bidi reordering, adding or deleting a character in the
12997 beginning of a paragraph, before the first strong directional
12998 character, can change the base direction of the paragraph (unless
12999 the buffer specifies a fixed paragraph direction), which will
13000 require to redisplay the whole paragraph. It might be worthwhile
13001 to find the paragraph limits and widen the range of redisplayed
13002 lines to that, but for now just give up this optimization. */
13003 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13004 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13005 unchanged_p = 0;
13006 }
13007
13008 return unchanged_p;
13009 }
13010
13011
13012 /* Do a frame update, taking possible shortcuts into account. This is
13013 the main external entry point for redisplay.
13014
13015 If the last redisplay displayed an echo area message and that message
13016 is no longer requested, we clear the echo area or bring back the
13017 mini-buffer if that is in use. */
13018
13019 void
13020 redisplay (void)
13021 {
13022 redisplay_internal ();
13023 }
13024
13025
13026 static Lisp_Object
13027 overlay_arrow_string_or_property (Lisp_Object var)
13028 {
13029 Lisp_Object val;
13030
13031 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13032 return val;
13033
13034 return Voverlay_arrow_string;
13035 }
13036
13037 /* Return 1 if there are any overlay-arrows in current_buffer. */
13038 static int
13039 overlay_arrow_in_current_buffer_p (void)
13040 {
13041 Lisp_Object vlist;
13042
13043 for (vlist = Voverlay_arrow_variable_list;
13044 CONSP (vlist);
13045 vlist = XCDR (vlist))
13046 {
13047 Lisp_Object var = XCAR (vlist);
13048 Lisp_Object val;
13049
13050 if (!SYMBOLP (var))
13051 continue;
13052 val = find_symbol_value (var);
13053 if (MARKERP (val)
13054 && current_buffer == XMARKER (val)->buffer)
13055 return 1;
13056 }
13057 return 0;
13058 }
13059
13060
13061 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13062 has changed. */
13063
13064 static int
13065 overlay_arrows_changed_p (void)
13066 {
13067 Lisp_Object vlist;
13068
13069 for (vlist = Voverlay_arrow_variable_list;
13070 CONSP (vlist);
13071 vlist = XCDR (vlist))
13072 {
13073 Lisp_Object var = XCAR (vlist);
13074 Lisp_Object val, pstr;
13075
13076 if (!SYMBOLP (var))
13077 continue;
13078 val = find_symbol_value (var);
13079 if (!MARKERP (val))
13080 continue;
13081 if (! EQ (COERCE_MARKER (val),
13082 Fget (var, Qlast_arrow_position))
13083 || ! (pstr = overlay_arrow_string_or_property (var),
13084 EQ (pstr, Fget (var, Qlast_arrow_string))))
13085 return 1;
13086 }
13087 return 0;
13088 }
13089
13090 /* Mark overlay arrows to be updated on next redisplay. */
13091
13092 static void
13093 update_overlay_arrows (int up_to_date)
13094 {
13095 Lisp_Object vlist;
13096
13097 for (vlist = Voverlay_arrow_variable_list;
13098 CONSP (vlist);
13099 vlist = XCDR (vlist))
13100 {
13101 Lisp_Object var = XCAR (vlist);
13102
13103 if (!SYMBOLP (var))
13104 continue;
13105
13106 if (up_to_date > 0)
13107 {
13108 Lisp_Object val = find_symbol_value (var);
13109 Fput (var, Qlast_arrow_position,
13110 COERCE_MARKER (val));
13111 Fput (var, Qlast_arrow_string,
13112 overlay_arrow_string_or_property (var));
13113 }
13114 else if (up_to_date < 0
13115 || !NILP (Fget (var, Qlast_arrow_position)))
13116 {
13117 Fput (var, Qlast_arrow_position, Qt);
13118 Fput (var, Qlast_arrow_string, Qt);
13119 }
13120 }
13121 }
13122
13123
13124 /* Return overlay arrow string to display at row.
13125 Return integer (bitmap number) for arrow bitmap in left fringe.
13126 Return nil if no overlay arrow. */
13127
13128 static Lisp_Object
13129 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13130 {
13131 Lisp_Object vlist;
13132
13133 for (vlist = Voverlay_arrow_variable_list;
13134 CONSP (vlist);
13135 vlist = XCDR (vlist))
13136 {
13137 Lisp_Object var = XCAR (vlist);
13138 Lisp_Object val;
13139
13140 if (!SYMBOLP (var))
13141 continue;
13142
13143 val = find_symbol_value (var);
13144
13145 if (MARKERP (val)
13146 && current_buffer == XMARKER (val)->buffer
13147 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13148 {
13149 if (FRAME_WINDOW_P (it->f)
13150 /* FIXME: if ROW->reversed_p is set, this should test
13151 the right fringe, not the left one. */
13152 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13153 {
13154 #ifdef HAVE_WINDOW_SYSTEM
13155 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13156 {
13157 int fringe_bitmap;
13158 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13159 return make_number (fringe_bitmap);
13160 }
13161 #endif
13162 return make_number (-1); /* Use default arrow bitmap. */
13163 }
13164 return overlay_arrow_string_or_property (var);
13165 }
13166 }
13167
13168 return Qnil;
13169 }
13170
13171 /* Return 1 if point moved out of or into a composition. Otherwise
13172 return 0. PREV_BUF and PREV_PT are the last point buffer and
13173 position. BUF and PT are the current point buffer and position. */
13174
13175 static int
13176 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13177 struct buffer *buf, ptrdiff_t pt)
13178 {
13179 ptrdiff_t start, end;
13180 Lisp_Object prop;
13181 Lisp_Object buffer;
13182
13183 XSETBUFFER (buffer, buf);
13184 /* Check a composition at the last point if point moved within the
13185 same buffer. */
13186 if (prev_buf == buf)
13187 {
13188 if (prev_pt == pt)
13189 /* Point didn't move. */
13190 return 0;
13191
13192 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13193 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13194 && composition_valid_p (start, end, prop)
13195 && start < prev_pt && end > prev_pt)
13196 /* The last point was within the composition. Return 1 iff
13197 point moved out of the composition. */
13198 return (pt <= start || pt >= end);
13199 }
13200
13201 /* Check a composition at the current point. */
13202 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13203 && find_composition (pt, -1, &start, &end, &prop, buffer)
13204 && composition_valid_p (start, end, prop)
13205 && start < pt && end > pt);
13206 }
13207
13208 /* Reconsider the clip changes of buffer which is displayed in W. */
13209
13210 static void
13211 reconsider_clip_changes (struct window *w)
13212 {
13213 struct buffer *b = XBUFFER (w->contents);
13214
13215 if (b->clip_changed
13216 && w->window_end_valid
13217 && w->current_matrix->buffer == b
13218 && w->current_matrix->zv == BUF_ZV (b)
13219 && w->current_matrix->begv == BUF_BEGV (b))
13220 b->clip_changed = 0;
13221
13222 /* If display wasn't paused, and W is not a tool bar window, see if
13223 point has been moved into or out of a composition. In that case,
13224 we set b->clip_changed to 1 to force updating the screen. If
13225 b->clip_changed has already been set to 1, we can skip this
13226 check. */
13227 if (!b->clip_changed && w->window_end_valid)
13228 {
13229 ptrdiff_t pt = (w == XWINDOW (selected_window)
13230 ? PT : marker_position (w->pointm));
13231
13232 if ((w->current_matrix->buffer != b || pt != w->last_point)
13233 && check_point_in_composition (w->current_matrix->buffer,
13234 w->last_point, b, pt))
13235 b->clip_changed = 1;
13236 }
13237 }
13238
13239 static void
13240 propagate_buffer_redisplay (void)
13241 { /* Resetting b->text->redisplay is problematic!
13242 We can't just reset it in the case that some window that displays
13243 it has not been redisplayed; and such a window can stay
13244 unredisplayed for a long time if it's currently invisible.
13245 But we do want to reset it at the end of redisplay otherwise
13246 its displayed windows will keep being redisplayed over and over
13247 again.
13248 So we copy all b->text->redisplay flags up to their windows here,
13249 such that mark_window_display_accurate can safely reset
13250 b->text->redisplay. */
13251 Lisp_Object ws = window_list ();
13252 for (; CONSP (ws); ws = XCDR (ws))
13253 {
13254 struct window *thisw = XWINDOW (XCAR (ws));
13255 struct buffer *thisb = XBUFFER (thisw->contents);
13256 if (thisb->text->redisplay)
13257 thisw->redisplay = true;
13258 }
13259 }
13260
13261 #define STOP_POLLING \
13262 do { if (! polling_stopped_here) stop_polling (); \
13263 polling_stopped_here = 1; } while (0)
13264
13265 #define RESUME_POLLING \
13266 do { if (polling_stopped_here) start_polling (); \
13267 polling_stopped_here = 0; } while (0)
13268
13269
13270 /* Perhaps in the future avoid recentering windows if it
13271 is not necessary; currently that causes some problems. */
13272
13273 static void
13274 redisplay_internal (void)
13275 {
13276 struct window *w = XWINDOW (selected_window);
13277 struct window *sw;
13278 struct frame *fr;
13279 int pending;
13280 bool must_finish = 0, match_p;
13281 struct text_pos tlbufpos, tlendpos;
13282 int number_of_visible_frames;
13283 ptrdiff_t count;
13284 struct frame *sf;
13285 int polling_stopped_here = 0;
13286 Lisp_Object tail, frame;
13287
13288 /* True means redisplay has to consider all windows on all
13289 frames. False, only selected_window is considered. */
13290 bool consider_all_windows_p;
13291
13292 /* True means redisplay has to redisplay the miniwindow. */
13293 bool update_miniwindow_p = false;
13294
13295 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13296
13297 /* No redisplay if running in batch mode or frame is not yet fully
13298 initialized, or redisplay is explicitly turned off by setting
13299 Vinhibit_redisplay. */
13300 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13301 || !NILP (Vinhibit_redisplay))
13302 return;
13303
13304 /* Don't examine these until after testing Vinhibit_redisplay.
13305 When Emacs is shutting down, perhaps because its connection to
13306 X has dropped, we should not look at them at all. */
13307 fr = XFRAME (w->frame);
13308 sf = SELECTED_FRAME ();
13309
13310 if (!fr->glyphs_initialized_p)
13311 return;
13312
13313 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13314 if (popup_activated ())
13315 return;
13316 #endif
13317
13318 /* I don't think this happens but let's be paranoid. */
13319 if (redisplaying_p)
13320 return;
13321
13322 /* Record a function that clears redisplaying_p
13323 when we leave this function. */
13324 count = SPECPDL_INDEX ();
13325 record_unwind_protect_void (unwind_redisplay);
13326 redisplaying_p = 1;
13327 specbind (Qinhibit_free_realized_faces, Qnil);
13328
13329 /* Record this function, so it appears on the profiler's backtraces. */
13330 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13331
13332 FOR_EACH_FRAME (tail, frame)
13333 XFRAME (frame)->already_hscrolled_p = 0;
13334
13335 retry:
13336 /* Remember the currently selected window. */
13337 sw = w;
13338
13339 pending = 0;
13340 last_escape_glyph_frame = NULL;
13341 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13342 last_glyphless_glyph_frame = NULL;
13343 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13344
13345 /* If face_change_count is non-zero, init_iterator will free all
13346 realized faces, which includes the faces referenced from current
13347 matrices. So, we can't reuse current matrices in this case. */
13348 if (face_change_count)
13349 windows_or_buffers_changed = 47;
13350
13351 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13352 && FRAME_TTY (sf)->previous_frame != sf)
13353 {
13354 /* Since frames on a single ASCII terminal share the same
13355 display area, displaying a different frame means redisplay
13356 the whole thing. */
13357 SET_FRAME_GARBAGED (sf);
13358 #ifndef DOS_NT
13359 set_tty_color_mode (FRAME_TTY (sf), sf);
13360 #endif
13361 FRAME_TTY (sf)->previous_frame = sf;
13362 }
13363
13364 /* Set the visible flags for all frames. Do this before checking for
13365 resized or garbaged frames; they want to know if their frames are
13366 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13367 number_of_visible_frames = 0;
13368
13369 FOR_EACH_FRAME (tail, frame)
13370 {
13371 struct frame *f = XFRAME (frame);
13372
13373 if (FRAME_VISIBLE_P (f))
13374 {
13375 ++number_of_visible_frames;
13376 /* Adjust matrices for visible frames only. */
13377 if (f->fonts_changed)
13378 {
13379 adjust_frame_glyphs (f);
13380 f->fonts_changed = 0;
13381 }
13382 /* If cursor type has been changed on the frame
13383 other than selected, consider all frames. */
13384 if (f != sf && f->cursor_type_changed)
13385 update_mode_lines = 31;
13386 }
13387 clear_desired_matrices (f);
13388 }
13389
13390 /* Notice any pending interrupt request to change frame size. */
13391 do_pending_window_change (1);
13392
13393 /* do_pending_window_change could change the selected_window due to
13394 frame resizing which makes the selected window too small. */
13395 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13396 sw = w;
13397
13398 /* Clear frames marked as garbaged. */
13399 clear_garbaged_frames ();
13400
13401 /* Build menubar and tool-bar items. */
13402 if (NILP (Vmemory_full))
13403 prepare_menu_bars ();
13404
13405 reconsider_clip_changes (w);
13406
13407 /* In most cases selected window displays current buffer. */
13408 match_p = XBUFFER (w->contents) == current_buffer;
13409 if (match_p)
13410 {
13411 /* Detect case that we need to write or remove a star in the mode line. */
13412 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13413 w->update_mode_line = 1;
13414
13415 if (mode_line_update_needed (w))
13416 w->update_mode_line = 1;
13417 }
13418
13419 /* Normally the message* functions will have already displayed and
13420 updated the echo area, but the frame may have been trashed, or
13421 the update may have been preempted, so display the echo area
13422 again here. Checking message_cleared_p captures the case that
13423 the echo area should be cleared. */
13424 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13425 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13426 || (message_cleared_p
13427 && minibuf_level == 0
13428 /* If the mini-window is currently selected, this means the
13429 echo-area doesn't show through. */
13430 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13431 {
13432 int window_height_changed_p = echo_area_display (0);
13433
13434 if (message_cleared_p)
13435 update_miniwindow_p = true;
13436
13437 must_finish = 1;
13438
13439 /* If we don't display the current message, don't clear the
13440 message_cleared_p flag, because, if we did, we wouldn't clear
13441 the echo area in the next redisplay which doesn't preserve
13442 the echo area. */
13443 if (!display_last_displayed_message_p)
13444 message_cleared_p = 0;
13445
13446 if (window_height_changed_p)
13447 {
13448 windows_or_buffers_changed = 50;
13449
13450 /* If window configuration was changed, frames may have been
13451 marked garbaged. Clear them or we will experience
13452 surprises wrt scrolling. */
13453 clear_garbaged_frames ();
13454 }
13455 }
13456 else if (EQ (selected_window, minibuf_window)
13457 && (current_buffer->clip_changed || window_outdated (w))
13458 && resize_mini_window (w, 0))
13459 {
13460 /* Resized active mini-window to fit the size of what it is
13461 showing if its contents might have changed. */
13462 must_finish = 1;
13463
13464 /* If window configuration was changed, frames may have been
13465 marked garbaged. Clear them or we will experience
13466 surprises wrt scrolling. */
13467 clear_garbaged_frames ();
13468 }
13469
13470 if (windows_or_buffers_changed && !update_mode_lines)
13471 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13472 only the windows's contents needs to be refreshed, or whether the
13473 mode-lines also need a refresh. */
13474 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13475 ? REDISPLAY_SOME : 32);
13476
13477 /* If specs for an arrow have changed, do thorough redisplay
13478 to ensure we remove any arrow that should no longer exist. */
13479 if (overlay_arrows_changed_p ())
13480 /* Apparently, this is the only case where we update other windows,
13481 without updating other mode-lines. */
13482 windows_or_buffers_changed = 49;
13483
13484 consider_all_windows_p = (update_mode_lines
13485 || windows_or_buffers_changed);
13486
13487 #define AINC(a,i) \
13488 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13489 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13490
13491 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13492 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13493
13494 /* Optimize the case that only the line containing the cursor in the
13495 selected window has changed. Variables starting with this_ are
13496 set in display_line and record information about the line
13497 containing the cursor. */
13498 tlbufpos = this_line_start_pos;
13499 tlendpos = this_line_end_pos;
13500 if (!consider_all_windows_p
13501 && CHARPOS (tlbufpos) > 0
13502 && !w->update_mode_line
13503 && !current_buffer->clip_changed
13504 && !current_buffer->prevent_redisplay_optimizations_p
13505 && FRAME_VISIBLE_P (XFRAME (w->frame))
13506 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13507 && !XFRAME (w->frame)->cursor_type_changed
13508 /* Make sure recorded data applies to current buffer, etc. */
13509 && this_line_buffer == current_buffer
13510 && match_p
13511 && !w->force_start
13512 && !w->optional_new_start
13513 /* Point must be on the line that we have info recorded about. */
13514 && PT >= CHARPOS (tlbufpos)
13515 && PT <= Z - CHARPOS (tlendpos)
13516 /* All text outside that line, including its final newline,
13517 must be unchanged. */
13518 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13519 CHARPOS (tlendpos)))
13520 {
13521 if (CHARPOS (tlbufpos) > BEGV
13522 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13523 && (CHARPOS (tlbufpos) == ZV
13524 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13525 /* Former continuation line has disappeared by becoming empty. */
13526 goto cancel;
13527 else if (window_outdated (w) || MINI_WINDOW_P (w))
13528 {
13529 /* We have to handle the case of continuation around a
13530 wide-column character (see the comment in indent.c around
13531 line 1340).
13532
13533 For instance, in the following case:
13534
13535 -------- Insert --------
13536 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13537 J_I_ ==> J_I_ `^^' are cursors.
13538 ^^ ^^
13539 -------- --------
13540
13541 As we have to redraw the line above, we cannot use this
13542 optimization. */
13543
13544 struct it it;
13545 int line_height_before = this_line_pixel_height;
13546
13547 /* Note that start_display will handle the case that the
13548 line starting at tlbufpos is a continuation line. */
13549 start_display (&it, w, tlbufpos);
13550
13551 /* Implementation note: It this still necessary? */
13552 if (it.current_x != this_line_start_x)
13553 goto cancel;
13554
13555 TRACE ((stderr, "trying display optimization 1\n"));
13556 w->cursor.vpos = -1;
13557 overlay_arrow_seen = 0;
13558 it.vpos = this_line_vpos;
13559 it.current_y = this_line_y;
13560 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13561 display_line (&it);
13562
13563 /* If line contains point, is not continued,
13564 and ends at same distance from eob as before, we win. */
13565 if (w->cursor.vpos >= 0
13566 /* Line is not continued, otherwise this_line_start_pos
13567 would have been set to 0 in display_line. */
13568 && CHARPOS (this_line_start_pos)
13569 /* Line ends as before. */
13570 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13571 /* Line has same height as before. Otherwise other lines
13572 would have to be shifted up or down. */
13573 && this_line_pixel_height == line_height_before)
13574 {
13575 /* If this is not the window's last line, we must adjust
13576 the charstarts of the lines below. */
13577 if (it.current_y < it.last_visible_y)
13578 {
13579 struct glyph_row *row
13580 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13581 ptrdiff_t delta, delta_bytes;
13582
13583 /* We used to distinguish between two cases here,
13584 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13585 when the line ends in a newline or the end of the
13586 buffer's accessible portion. But both cases did
13587 the same, so they were collapsed. */
13588 delta = (Z
13589 - CHARPOS (tlendpos)
13590 - MATRIX_ROW_START_CHARPOS (row));
13591 delta_bytes = (Z_BYTE
13592 - BYTEPOS (tlendpos)
13593 - MATRIX_ROW_START_BYTEPOS (row));
13594
13595 increment_matrix_positions (w->current_matrix,
13596 this_line_vpos + 1,
13597 w->current_matrix->nrows,
13598 delta, delta_bytes);
13599 }
13600
13601 /* If this row displays text now but previously didn't,
13602 or vice versa, w->window_end_vpos may have to be
13603 adjusted. */
13604 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13605 {
13606 if (w->window_end_vpos < this_line_vpos)
13607 w->window_end_vpos = this_line_vpos;
13608 }
13609 else if (w->window_end_vpos == this_line_vpos
13610 && this_line_vpos > 0)
13611 w->window_end_vpos = this_line_vpos - 1;
13612 w->window_end_valid = 0;
13613
13614 /* Update hint: No need to try to scroll in update_window. */
13615 w->desired_matrix->no_scrolling_p = 1;
13616
13617 #ifdef GLYPH_DEBUG
13618 *w->desired_matrix->method = 0;
13619 debug_method_add (w, "optimization 1");
13620 #endif
13621 #ifdef HAVE_WINDOW_SYSTEM
13622 update_window_fringes (w, 0);
13623 #endif
13624 goto update;
13625 }
13626 else
13627 goto cancel;
13628 }
13629 else if (/* Cursor position hasn't changed. */
13630 PT == w->last_point
13631 /* Make sure the cursor was last displayed
13632 in this window. Otherwise we have to reposition it. */
13633
13634 /* PXW: Must be converted to pixels, probably. */
13635 && 0 <= w->cursor.vpos
13636 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13637 {
13638 if (!must_finish)
13639 {
13640 do_pending_window_change (1);
13641 /* If selected_window changed, redisplay again. */
13642 if (WINDOWP (selected_window)
13643 && (w = XWINDOW (selected_window)) != sw)
13644 goto retry;
13645
13646 /* We used to always goto end_of_redisplay here, but this
13647 isn't enough if we have a blinking cursor. */
13648 if (w->cursor_off_p == w->last_cursor_off_p)
13649 goto end_of_redisplay;
13650 }
13651 goto update;
13652 }
13653 /* If highlighting the region, or if the cursor is in the echo area,
13654 then we can't just move the cursor. */
13655 else if (NILP (Vshow_trailing_whitespace)
13656 && !cursor_in_echo_area)
13657 {
13658 struct it it;
13659 struct glyph_row *row;
13660
13661 /* Skip from tlbufpos to PT and see where it is. Note that
13662 PT may be in invisible text. If so, we will end at the
13663 next visible position. */
13664 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13665 NULL, DEFAULT_FACE_ID);
13666 it.current_x = this_line_start_x;
13667 it.current_y = this_line_y;
13668 it.vpos = this_line_vpos;
13669
13670 /* The call to move_it_to stops in front of PT, but
13671 moves over before-strings. */
13672 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13673
13674 if (it.vpos == this_line_vpos
13675 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13676 row->enabled_p))
13677 {
13678 eassert (this_line_vpos == it.vpos);
13679 eassert (this_line_y == it.current_y);
13680 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13681 #ifdef GLYPH_DEBUG
13682 *w->desired_matrix->method = 0;
13683 debug_method_add (w, "optimization 3");
13684 #endif
13685 goto update;
13686 }
13687 else
13688 goto cancel;
13689 }
13690
13691 cancel:
13692 /* Text changed drastically or point moved off of line. */
13693 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13694 }
13695
13696 CHARPOS (this_line_start_pos) = 0;
13697 ++clear_face_cache_count;
13698 #ifdef HAVE_WINDOW_SYSTEM
13699 ++clear_image_cache_count;
13700 #endif
13701
13702 /* Build desired matrices, and update the display. If
13703 consider_all_windows_p is non-zero, do it for all windows on all
13704 frames. Otherwise do it for selected_window, only. */
13705
13706 if (consider_all_windows_p)
13707 {
13708 FOR_EACH_FRAME (tail, frame)
13709 XFRAME (frame)->updated_p = 0;
13710
13711 propagate_buffer_redisplay ();
13712
13713 FOR_EACH_FRAME (tail, frame)
13714 {
13715 struct frame *f = XFRAME (frame);
13716
13717 /* We don't have to do anything for unselected terminal
13718 frames. */
13719 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13720 && !EQ (FRAME_TTY (f)->top_frame, frame))
13721 continue;
13722
13723 retry_frame:
13724
13725 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13726 {
13727 bool gcscrollbars
13728 /* Only GC scrollbars when we redisplay the whole frame. */
13729 = f->redisplay || !REDISPLAY_SOME_P ();
13730 /* Mark all the scroll bars to be removed; we'll redeem
13731 the ones we want when we redisplay their windows. */
13732 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13733 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13734
13735 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13736 redisplay_windows (FRAME_ROOT_WINDOW (f));
13737 /* Remember that the invisible frames need to be redisplayed next
13738 time they're visible. */
13739 else if (!REDISPLAY_SOME_P ())
13740 f->redisplay = true;
13741
13742 /* The X error handler may have deleted that frame. */
13743 if (!FRAME_LIVE_P (f))
13744 continue;
13745
13746 /* Any scroll bars which redisplay_windows should have
13747 nuked should now go away. */
13748 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13749 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13750
13751 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13752 {
13753 /* If fonts changed on visible frame, display again. */
13754 if (f->fonts_changed)
13755 {
13756 adjust_frame_glyphs (f);
13757 f->fonts_changed = 0;
13758 goto retry_frame;
13759 }
13760
13761 /* See if we have to hscroll. */
13762 if (!f->already_hscrolled_p)
13763 {
13764 f->already_hscrolled_p = 1;
13765 if (hscroll_windows (f->root_window))
13766 goto retry_frame;
13767 }
13768
13769 /* Prevent various kinds of signals during display
13770 update. stdio is not robust about handling
13771 signals, which can cause an apparent I/O error. */
13772 if (interrupt_input)
13773 unrequest_sigio ();
13774 STOP_POLLING;
13775
13776 pending |= update_frame (f, 0, 0);
13777 f->cursor_type_changed = 0;
13778 f->updated_p = 1;
13779 }
13780 }
13781 }
13782
13783 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13784
13785 if (!pending)
13786 {
13787 /* Do the mark_window_display_accurate after all windows have
13788 been redisplayed because this call resets flags in buffers
13789 which are needed for proper redisplay. */
13790 FOR_EACH_FRAME (tail, frame)
13791 {
13792 struct frame *f = XFRAME (frame);
13793 if (f->updated_p)
13794 {
13795 f->redisplay = false;
13796 mark_window_display_accurate (f->root_window, 1);
13797 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13798 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13799 }
13800 }
13801 }
13802 }
13803 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13804 {
13805 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13806 struct frame *mini_frame;
13807
13808 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13809 /* Use list_of_error, not Qerror, so that
13810 we catch only errors and don't run the debugger. */
13811 internal_condition_case_1 (redisplay_window_1, selected_window,
13812 list_of_error,
13813 redisplay_window_error);
13814 if (update_miniwindow_p)
13815 internal_condition_case_1 (redisplay_window_1, mini_window,
13816 list_of_error,
13817 redisplay_window_error);
13818
13819 /* Compare desired and current matrices, perform output. */
13820
13821 update:
13822 /* If fonts changed, display again. */
13823 if (sf->fonts_changed)
13824 goto retry;
13825
13826 /* Prevent various kinds of signals during display update.
13827 stdio is not robust about handling signals,
13828 which can cause an apparent I/O error. */
13829 if (interrupt_input)
13830 unrequest_sigio ();
13831 STOP_POLLING;
13832
13833 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13834 {
13835 if (hscroll_windows (selected_window))
13836 goto retry;
13837
13838 XWINDOW (selected_window)->must_be_updated_p = true;
13839 pending = update_frame (sf, 0, 0);
13840 sf->cursor_type_changed = 0;
13841 }
13842
13843 /* We may have called echo_area_display at the top of this
13844 function. If the echo area is on another frame, that may
13845 have put text on a frame other than the selected one, so the
13846 above call to update_frame would not have caught it. Catch
13847 it here. */
13848 mini_window = FRAME_MINIBUF_WINDOW (sf);
13849 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13850
13851 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13852 {
13853 XWINDOW (mini_window)->must_be_updated_p = true;
13854 pending |= update_frame (mini_frame, 0, 0);
13855 mini_frame->cursor_type_changed = 0;
13856 if (!pending && hscroll_windows (mini_window))
13857 goto retry;
13858 }
13859 }
13860
13861 /* If display was paused because of pending input, make sure we do a
13862 thorough update the next time. */
13863 if (pending)
13864 {
13865 /* Prevent the optimization at the beginning of
13866 redisplay_internal that tries a single-line update of the
13867 line containing the cursor in the selected window. */
13868 CHARPOS (this_line_start_pos) = 0;
13869
13870 /* Let the overlay arrow be updated the next time. */
13871 update_overlay_arrows (0);
13872
13873 /* If we pause after scrolling, some rows in the current
13874 matrices of some windows are not valid. */
13875 if (!WINDOW_FULL_WIDTH_P (w)
13876 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13877 update_mode_lines = 36;
13878 }
13879 else
13880 {
13881 if (!consider_all_windows_p)
13882 {
13883 /* This has already been done above if
13884 consider_all_windows_p is set. */
13885 if (XBUFFER (w->contents)->text->redisplay
13886 && buffer_window_count (XBUFFER (w->contents)) > 1)
13887 /* This can happen if b->text->redisplay was set during
13888 jit-lock. */
13889 propagate_buffer_redisplay ();
13890 mark_window_display_accurate_1 (w, 1);
13891
13892 /* Say overlay arrows are up to date. */
13893 update_overlay_arrows (1);
13894
13895 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13896 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13897 }
13898
13899 update_mode_lines = 0;
13900 windows_or_buffers_changed = 0;
13901 }
13902
13903 /* Start SIGIO interrupts coming again. Having them off during the
13904 code above makes it less likely one will discard output, but not
13905 impossible, since there might be stuff in the system buffer here.
13906 But it is much hairier to try to do anything about that. */
13907 if (interrupt_input)
13908 request_sigio ();
13909 RESUME_POLLING;
13910
13911 /* If a frame has become visible which was not before, redisplay
13912 again, so that we display it. Expose events for such a frame
13913 (which it gets when becoming visible) don't call the parts of
13914 redisplay constructing glyphs, so simply exposing a frame won't
13915 display anything in this case. So, we have to display these
13916 frames here explicitly. */
13917 if (!pending)
13918 {
13919 int new_count = 0;
13920
13921 FOR_EACH_FRAME (tail, frame)
13922 {
13923 if (XFRAME (frame)->visible)
13924 new_count++;
13925 }
13926
13927 if (new_count != number_of_visible_frames)
13928 windows_or_buffers_changed = 52;
13929 }
13930
13931 /* Change frame size now if a change is pending. */
13932 do_pending_window_change (1);
13933
13934 /* If we just did a pending size change, or have additional
13935 visible frames, or selected_window changed, redisplay again. */
13936 if ((windows_or_buffers_changed && !pending)
13937 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13938 goto retry;
13939
13940 /* Clear the face and image caches.
13941
13942 We used to do this only if consider_all_windows_p. But the cache
13943 needs to be cleared if a timer creates images in the current
13944 buffer (e.g. the test case in Bug#6230). */
13945
13946 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13947 {
13948 clear_face_cache (0);
13949 clear_face_cache_count = 0;
13950 }
13951
13952 #ifdef HAVE_WINDOW_SYSTEM
13953 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13954 {
13955 clear_image_caches (Qnil);
13956 clear_image_cache_count = 0;
13957 }
13958 #endif /* HAVE_WINDOW_SYSTEM */
13959
13960 end_of_redisplay:
13961 if (interrupt_input && interrupts_deferred)
13962 request_sigio ();
13963
13964 unbind_to (count, Qnil);
13965 RESUME_POLLING;
13966 }
13967
13968
13969 /* Redisplay, but leave alone any recent echo area message unless
13970 another message has been requested in its place.
13971
13972 This is useful in situations where you need to redisplay but no
13973 user action has occurred, making it inappropriate for the message
13974 area to be cleared. See tracking_off and
13975 wait_reading_process_output for examples of these situations.
13976
13977 FROM_WHERE is an integer saying from where this function was
13978 called. This is useful for debugging. */
13979
13980 void
13981 redisplay_preserve_echo_area (int from_where)
13982 {
13983 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13984
13985 if (!NILP (echo_area_buffer[1]))
13986 {
13987 /* We have a previously displayed message, but no current
13988 message. Redisplay the previous message. */
13989 display_last_displayed_message_p = 1;
13990 redisplay_internal ();
13991 display_last_displayed_message_p = 0;
13992 }
13993 else
13994 redisplay_internal ();
13995
13996 flush_frame (SELECTED_FRAME ());
13997 }
13998
13999
14000 /* Function registered with record_unwind_protect in redisplay_internal. */
14001
14002 static void
14003 unwind_redisplay (void)
14004 {
14005 redisplaying_p = 0;
14006 }
14007
14008
14009 /* Mark the display of leaf window W as accurate or inaccurate.
14010 If ACCURATE_P is non-zero mark display of W as accurate. If
14011 ACCURATE_P is zero, arrange for W to be redisplayed the next
14012 time redisplay_internal is called. */
14013
14014 static void
14015 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14016 {
14017 struct buffer *b = XBUFFER (w->contents);
14018
14019 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14020 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14021 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14022
14023 if (accurate_p)
14024 {
14025 b->clip_changed = false;
14026 b->prevent_redisplay_optimizations_p = false;
14027 eassert (buffer_window_count (b) > 0);
14028 /* Resetting b->text->redisplay is problematic!
14029 In order to make it safer to do it here, redisplay_internal must
14030 have copied all b->text->redisplay to their respective windows. */
14031 b->text->redisplay = false;
14032
14033 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14034 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14035 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14036 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14037
14038 w->current_matrix->buffer = b;
14039 w->current_matrix->begv = BUF_BEGV (b);
14040 w->current_matrix->zv = BUF_ZV (b);
14041
14042 w->last_cursor_vpos = w->cursor.vpos;
14043 w->last_cursor_off_p = w->cursor_off_p;
14044
14045 if (w == XWINDOW (selected_window))
14046 w->last_point = BUF_PT (b);
14047 else
14048 w->last_point = marker_position (w->pointm);
14049
14050 w->window_end_valid = true;
14051 w->update_mode_line = false;
14052 }
14053
14054 w->redisplay = !accurate_p;
14055 }
14056
14057
14058 /* Mark the display of windows in the window tree rooted at WINDOW as
14059 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14060 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14061 be redisplayed the next time redisplay_internal is called. */
14062
14063 void
14064 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14065 {
14066 struct window *w;
14067
14068 for (; !NILP (window); window = w->next)
14069 {
14070 w = XWINDOW (window);
14071 if (WINDOWP (w->contents))
14072 mark_window_display_accurate (w->contents, accurate_p);
14073 else
14074 mark_window_display_accurate_1 (w, accurate_p);
14075 }
14076
14077 if (accurate_p)
14078 update_overlay_arrows (1);
14079 else
14080 /* Force a thorough redisplay the next time by setting
14081 last_arrow_position and last_arrow_string to t, which is
14082 unequal to any useful value of Voverlay_arrow_... */
14083 update_overlay_arrows (-1);
14084 }
14085
14086
14087 /* Return value in display table DP (Lisp_Char_Table *) for character
14088 C. Since a display table doesn't have any parent, we don't have to
14089 follow parent. Do not call this function directly but use the
14090 macro DISP_CHAR_VECTOR. */
14091
14092 Lisp_Object
14093 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14094 {
14095 Lisp_Object val;
14096
14097 if (ASCII_CHAR_P (c))
14098 {
14099 val = dp->ascii;
14100 if (SUB_CHAR_TABLE_P (val))
14101 val = XSUB_CHAR_TABLE (val)->contents[c];
14102 }
14103 else
14104 {
14105 Lisp_Object table;
14106
14107 XSETCHAR_TABLE (table, dp);
14108 val = char_table_ref (table, c);
14109 }
14110 if (NILP (val))
14111 val = dp->defalt;
14112 return val;
14113 }
14114
14115
14116 \f
14117 /***********************************************************************
14118 Window Redisplay
14119 ***********************************************************************/
14120
14121 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14122
14123 static void
14124 redisplay_windows (Lisp_Object window)
14125 {
14126 while (!NILP (window))
14127 {
14128 struct window *w = XWINDOW (window);
14129
14130 if (WINDOWP (w->contents))
14131 redisplay_windows (w->contents);
14132 else if (BUFFERP (w->contents))
14133 {
14134 displayed_buffer = XBUFFER (w->contents);
14135 /* Use list_of_error, not Qerror, so that
14136 we catch only errors and don't run the debugger. */
14137 internal_condition_case_1 (redisplay_window_0, window,
14138 list_of_error,
14139 redisplay_window_error);
14140 }
14141
14142 window = w->next;
14143 }
14144 }
14145
14146 static Lisp_Object
14147 redisplay_window_error (Lisp_Object ignore)
14148 {
14149 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14150 return Qnil;
14151 }
14152
14153 static Lisp_Object
14154 redisplay_window_0 (Lisp_Object window)
14155 {
14156 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14157 redisplay_window (window, false);
14158 return Qnil;
14159 }
14160
14161 static Lisp_Object
14162 redisplay_window_1 (Lisp_Object window)
14163 {
14164 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14165 redisplay_window (window, true);
14166 return Qnil;
14167 }
14168 \f
14169
14170 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14171 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14172 which positions recorded in ROW differ from current buffer
14173 positions.
14174
14175 Return 0 if cursor is not on this row, 1 otherwise. */
14176
14177 static int
14178 set_cursor_from_row (struct window *w, struct glyph_row *row,
14179 struct glyph_matrix *matrix,
14180 ptrdiff_t delta, ptrdiff_t delta_bytes,
14181 int dy, int dvpos)
14182 {
14183 struct glyph *glyph = row->glyphs[TEXT_AREA];
14184 struct glyph *end = glyph + row->used[TEXT_AREA];
14185 struct glyph *cursor = NULL;
14186 /* The last known character position in row. */
14187 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14188 int x = row->x;
14189 ptrdiff_t pt_old = PT - delta;
14190 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14191 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14192 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14193 /* A glyph beyond the edge of TEXT_AREA which we should never
14194 touch. */
14195 struct glyph *glyphs_end = end;
14196 /* Non-zero means we've found a match for cursor position, but that
14197 glyph has the avoid_cursor_p flag set. */
14198 int match_with_avoid_cursor = 0;
14199 /* Non-zero means we've seen at least one glyph that came from a
14200 display string. */
14201 int string_seen = 0;
14202 /* Largest and smallest buffer positions seen so far during scan of
14203 glyph row. */
14204 ptrdiff_t bpos_max = pos_before;
14205 ptrdiff_t bpos_min = pos_after;
14206 /* Last buffer position covered by an overlay string with an integer
14207 `cursor' property. */
14208 ptrdiff_t bpos_covered = 0;
14209 /* Non-zero means the display string on which to display the cursor
14210 comes from a text property, not from an overlay. */
14211 int string_from_text_prop = 0;
14212
14213 /* Don't even try doing anything if called for a mode-line or
14214 header-line row, since the rest of the code isn't prepared to
14215 deal with such calamities. */
14216 eassert (!row->mode_line_p);
14217 if (row->mode_line_p)
14218 return 0;
14219
14220 /* Skip over glyphs not having an object at the start and the end of
14221 the row. These are special glyphs like truncation marks on
14222 terminal frames. */
14223 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14224 {
14225 if (!row->reversed_p)
14226 {
14227 while (glyph < end
14228 && INTEGERP (glyph->object)
14229 && glyph->charpos < 0)
14230 {
14231 x += glyph->pixel_width;
14232 ++glyph;
14233 }
14234 while (end > glyph
14235 && INTEGERP ((end - 1)->object)
14236 /* CHARPOS is zero for blanks and stretch glyphs
14237 inserted by extend_face_to_end_of_line. */
14238 && (end - 1)->charpos <= 0)
14239 --end;
14240 glyph_before = glyph - 1;
14241 glyph_after = end;
14242 }
14243 else
14244 {
14245 struct glyph *g;
14246
14247 /* If the glyph row is reversed, we need to process it from back
14248 to front, so swap the edge pointers. */
14249 glyphs_end = end = glyph - 1;
14250 glyph += row->used[TEXT_AREA] - 1;
14251
14252 while (glyph > end + 1
14253 && INTEGERP (glyph->object)
14254 && glyph->charpos < 0)
14255 {
14256 --glyph;
14257 x -= glyph->pixel_width;
14258 }
14259 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14260 --glyph;
14261 /* By default, in reversed rows we put the cursor on the
14262 rightmost (first in the reading order) glyph. */
14263 for (g = end + 1; g < glyph; g++)
14264 x += g->pixel_width;
14265 while (end < glyph
14266 && INTEGERP ((end + 1)->object)
14267 && (end + 1)->charpos <= 0)
14268 ++end;
14269 glyph_before = glyph + 1;
14270 glyph_after = end;
14271 }
14272 }
14273 else if (row->reversed_p)
14274 {
14275 /* In R2L rows that don't display text, put the cursor on the
14276 rightmost glyph. Case in point: an empty last line that is
14277 part of an R2L paragraph. */
14278 cursor = end - 1;
14279 /* Avoid placing the cursor on the last glyph of the row, where
14280 on terminal frames we hold the vertical border between
14281 adjacent windows. */
14282 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14283 && !WINDOW_RIGHTMOST_P (w)
14284 && cursor == row->glyphs[LAST_AREA] - 1)
14285 cursor--;
14286 x = -1; /* will be computed below, at label compute_x */
14287 }
14288
14289 /* Step 1: Try to find the glyph whose character position
14290 corresponds to point. If that's not possible, find 2 glyphs
14291 whose character positions are the closest to point, one before
14292 point, the other after it. */
14293 if (!row->reversed_p)
14294 while (/* not marched to end of glyph row */
14295 glyph < end
14296 /* glyph was not inserted by redisplay for internal purposes */
14297 && !INTEGERP (glyph->object))
14298 {
14299 if (BUFFERP (glyph->object))
14300 {
14301 ptrdiff_t dpos = glyph->charpos - pt_old;
14302
14303 if (glyph->charpos > bpos_max)
14304 bpos_max = glyph->charpos;
14305 if (glyph->charpos < bpos_min)
14306 bpos_min = glyph->charpos;
14307 if (!glyph->avoid_cursor_p)
14308 {
14309 /* If we hit point, we've found the glyph on which to
14310 display the cursor. */
14311 if (dpos == 0)
14312 {
14313 match_with_avoid_cursor = 0;
14314 break;
14315 }
14316 /* See if we've found a better approximation to
14317 POS_BEFORE or to POS_AFTER. */
14318 if (0 > dpos && dpos > pos_before - pt_old)
14319 {
14320 pos_before = glyph->charpos;
14321 glyph_before = glyph;
14322 }
14323 else if (0 < dpos && dpos < pos_after - pt_old)
14324 {
14325 pos_after = glyph->charpos;
14326 glyph_after = glyph;
14327 }
14328 }
14329 else if (dpos == 0)
14330 match_with_avoid_cursor = 1;
14331 }
14332 else if (STRINGP (glyph->object))
14333 {
14334 Lisp_Object chprop;
14335 ptrdiff_t glyph_pos = glyph->charpos;
14336
14337 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14338 glyph->object);
14339 if (!NILP (chprop))
14340 {
14341 /* If the string came from a `display' text property,
14342 look up the buffer position of that property and
14343 use that position to update bpos_max, as if we
14344 actually saw such a position in one of the row's
14345 glyphs. This helps with supporting integer values
14346 of `cursor' property on the display string in
14347 situations where most or all of the row's buffer
14348 text is completely covered by display properties,
14349 so that no glyph with valid buffer positions is
14350 ever seen in the row. */
14351 ptrdiff_t prop_pos =
14352 string_buffer_position_lim (glyph->object, pos_before,
14353 pos_after, 0);
14354
14355 if (prop_pos >= pos_before)
14356 bpos_max = prop_pos - 1;
14357 }
14358 if (INTEGERP (chprop))
14359 {
14360 bpos_covered = bpos_max + XINT (chprop);
14361 /* If the `cursor' property covers buffer positions up
14362 to and including point, we should display cursor on
14363 this glyph. Note that, if a `cursor' property on one
14364 of the string's characters has an integer value, we
14365 will break out of the loop below _before_ we get to
14366 the position match above. IOW, integer values of
14367 the `cursor' property override the "exact match for
14368 point" strategy of positioning the cursor. */
14369 /* Implementation note: bpos_max == pt_old when, e.g.,
14370 we are in an empty line, where bpos_max is set to
14371 MATRIX_ROW_START_CHARPOS, see above. */
14372 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14373 {
14374 cursor = glyph;
14375 break;
14376 }
14377 }
14378
14379 string_seen = 1;
14380 }
14381 x += glyph->pixel_width;
14382 ++glyph;
14383 }
14384 else if (glyph > end) /* row is reversed */
14385 while (!INTEGERP (glyph->object))
14386 {
14387 if (BUFFERP (glyph->object))
14388 {
14389 ptrdiff_t dpos = glyph->charpos - pt_old;
14390
14391 if (glyph->charpos > bpos_max)
14392 bpos_max = glyph->charpos;
14393 if (glyph->charpos < bpos_min)
14394 bpos_min = glyph->charpos;
14395 if (!glyph->avoid_cursor_p)
14396 {
14397 if (dpos == 0)
14398 {
14399 match_with_avoid_cursor = 0;
14400 break;
14401 }
14402 if (0 > dpos && dpos > pos_before - pt_old)
14403 {
14404 pos_before = glyph->charpos;
14405 glyph_before = glyph;
14406 }
14407 else if (0 < dpos && dpos < pos_after - pt_old)
14408 {
14409 pos_after = glyph->charpos;
14410 glyph_after = glyph;
14411 }
14412 }
14413 else if (dpos == 0)
14414 match_with_avoid_cursor = 1;
14415 }
14416 else if (STRINGP (glyph->object))
14417 {
14418 Lisp_Object chprop;
14419 ptrdiff_t glyph_pos = glyph->charpos;
14420
14421 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14422 glyph->object);
14423 if (!NILP (chprop))
14424 {
14425 ptrdiff_t prop_pos =
14426 string_buffer_position_lim (glyph->object, pos_before,
14427 pos_after, 0);
14428
14429 if (prop_pos >= pos_before)
14430 bpos_max = prop_pos - 1;
14431 }
14432 if (INTEGERP (chprop))
14433 {
14434 bpos_covered = bpos_max + XINT (chprop);
14435 /* If the `cursor' property covers buffer positions up
14436 to and including point, we should display cursor on
14437 this glyph. */
14438 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14439 {
14440 cursor = glyph;
14441 break;
14442 }
14443 }
14444 string_seen = 1;
14445 }
14446 --glyph;
14447 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14448 {
14449 x--; /* can't use any pixel_width */
14450 break;
14451 }
14452 x -= glyph->pixel_width;
14453 }
14454
14455 /* Step 2: If we didn't find an exact match for point, we need to
14456 look for a proper place to put the cursor among glyphs between
14457 GLYPH_BEFORE and GLYPH_AFTER. */
14458 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14459 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14460 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14461 {
14462 /* An empty line has a single glyph whose OBJECT is zero and
14463 whose CHARPOS is the position of a newline on that line.
14464 Note that on a TTY, there are more glyphs after that, which
14465 were produced by extend_face_to_end_of_line, but their
14466 CHARPOS is zero or negative. */
14467 int empty_line_p =
14468 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14469 && INTEGERP (glyph->object) && glyph->charpos > 0
14470 /* On a TTY, continued and truncated rows also have a glyph at
14471 their end whose OBJECT is zero and whose CHARPOS is
14472 positive (the continuation and truncation glyphs), but such
14473 rows are obviously not "empty". */
14474 && !(row->continued_p || row->truncated_on_right_p);
14475
14476 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14477 {
14478 ptrdiff_t ellipsis_pos;
14479
14480 /* Scan back over the ellipsis glyphs. */
14481 if (!row->reversed_p)
14482 {
14483 ellipsis_pos = (glyph - 1)->charpos;
14484 while (glyph > row->glyphs[TEXT_AREA]
14485 && (glyph - 1)->charpos == ellipsis_pos)
14486 glyph--, x -= glyph->pixel_width;
14487 /* That loop always goes one position too far, including
14488 the glyph before the ellipsis. So scan forward over
14489 that one. */
14490 x += glyph->pixel_width;
14491 glyph++;
14492 }
14493 else /* row is reversed */
14494 {
14495 ellipsis_pos = (glyph + 1)->charpos;
14496 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14497 && (glyph + 1)->charpos == ellipsis_pos)
14498 glyph++, x += glyph->pixel_width;
14499 x -= glyph->pixel_width;
14500 glyph--;
14501 }
14502 }
14503 else if (match_with_avoid_cursor)
14504 {
14505 cursor = glyph_after;
14506 x = -1;
14507 }
14508 else if (string_seen)
14509 {
14510 int incr = row->reversed_p ? -1 : +1;
14511
14512 /* Need to find the glyph that came out of a string which is
14513 present at point. That glyph is somewhere between
14514 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14515 positioned between POS_BEFORE and POS_AFTER in the
14516 buffer. */
14517 struct glyph *start, *stop;
14518 ptrdiff_t pos = pos_before;
14519
14520 x = -1;
14521
14522 /* If the row ends in a newline from a display string,
14523 reordering could have moved the glyphs belonging to the
14524 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14525 in this case we extend the search to the last glyph in
14526 the row that was not inserted by redisplay. */
14527 if (row->ends_in_newline_from_string_p)
14528 {
14529 glyph_after = end;
14530 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14531 }
14532
14533 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14534 correspond to POS_BEFORE and POS_AFTER, respectively. We
14535 need START and STOP in the order that corresponds to the
14536 row's direction as given by its reversed_p flag. If the
14537 directionality of characters between POS_BEFORE and
14538 POS_AFTER is the opposite of the row's base direction,
14539 these characters will have been reordered for display,
14540 and we need to reverse START and STOP. */
14541 if (!row->reversed_p)
14542 {
14543 start = min (glyph_before, glyph_after);
14544 stop = max (glyph_before, glyph_after);
14545 }
14546 else
14547 {
14548 start = max (glyph_before, glyph_after);
14549 stop = min (glyph_before, glyph_after);
14550 }
14551 for (glyph = start + incr;
14552 row->reversed_p ? glyph > stop : glyph < stop; )
14553 {
14554
14555 /* Any glyphs that come from the buffer are here because
14556 of bidi reordering. Skip them, and only pay
14557 attention to glyphs that came from some string. */
14558 if (STRINGP (glyph->object))
14559 {
14560 Lisp_Object str;
14561 ptrdiff_t tem;
14562 /* If the display property covers the newline, we
14563 need to search for it one position farther. */
14564 ptrdiff_t lim = pos_after
14565 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14566
14567 string_from_text_prop = 0;
14568 str = glyph->object;
14569 tem = string_buffer_position_lim (str, pos, lim, 0);
14570 if (tem == 0 /* from overlay */
14571 || pos <= tem)
14572 {
14573 /* If the string from which this glyph came is
14574 found in the buffer at point, or at position
14575 that is closer to point than pos_after, then
14576 we've found the glyph we've been looking for.
14577 If it comes from an overlay (tem == 0), and
14578 it has the `cursor' property on one of its
14579 glyphs, record that glyph as a candidate for
14580 displaying the cursor. (As in the
14581 unidirectional version, we will display the
14582 cursor on the last candidate we find.) */
14583 if (tem == 0
14584 || tem == pt_old
14585 || (tem - pt_old > 0 && tem < pos_after))
14586 {
14587 /* The glyphs from this string could have
14588 been reordered. Find the one with the
14589 smallest string position. Or there could
14590 be a character in the string with the
14591 `cursor' property, which means display
14592 cursor on that character's glyph. */
14593 ptrdiff_t strpos = glyph->charpos;
14594
14595 if (tem)
14596 {
14597 cursor = glyph;
14598 string_from_text_prop = 1;
14599 }
14600 for ( ;
14601 (row->reversed_p ? glyph > stop : glyph < stop)
14602 && EQ (glyph->object, str);
14603 glyph += incr)
14604 {
14605 Lisp_Object cprop;
14606 ptrdiff_t gpos = glyph->charpos;
14607
14608 cprop = Fget_char_property (make_number (gpos),
14609 Qcursor,
14610 glyph->object);
14611 if (!NILP (cprop))
14612 {
14613 cursor = glyph;
14614 break;
14615 }
14616 if (tem && glyph->charpos < strpos)
14617 {
14618 strpos = glyph->charpos;
14619 cursor = glyph;
14620 }
14621 }
14622
14623 if (tem == pt_old
14624 || (tem - pt_old > 0 && tem < pos_after))
14625 goto compute_x;
14626 }
14627 if (tem)
14628 pos = tem + 1; /* don't find previous instances */
14629 }
14630 /* This string is not what we want; skip all of the
14631 glyphs that came from it. */
14632 while ((row->reversed_p ? glyph > stop : glyph < stop)
14633 && EQ (glyph->object, str))
14634 glyph += incr;
14635 }
14636 else
14637 glyph += incr;
14638 }
14639
14640 /* If we reached the end of the line, and END was from a string,
14641 the cursor is not on this line. */
14642 if (cursor == NULL
14643 && (row->reversed_p ? glyph <= end : glyph >= end)
14644 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14645 && STRINGP (end->object)
14646 && row->continued_p)
14647 return 0;
14648 }
14649 /* A truncated row may not include PT among its character positions.
14650 Setting the cursor inside the scroll margin will trigger
14651 recalculation of hscroll in hscroll_window_tree. But if a
14652 display string covers point, defer to the string-handling
14653 code below to figure this out. */
14654 else if (row->truncated_on_left_p && pt_old < bpos_min)
14655 {
14656 cursor = glyph_before;
14657 x = -1;
14658 }
14659 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14660 /* Zero-width characters produce no glyphs. */
14661 || (!empty_line_p
14662 && (row->reversed_p
14663 ? glyph_after > glyphs_end
14664 : glyph_after < glyphs_end)))
14665 {
14666 cursor = glyph_after;
14667 x = -1;
14668 }
14669 }
14670
14671 compute_x:
14672 if (cursor != NULL)
14673 glyph = cursor;
14674 else if (glyph == glyphs_end
14675 && pos_before == pos_after
14676 && STRINGP ((row->reversed_p
14677 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14678 : row->glyphs[TEXT_AREA])->object))
14679 {
14680 /* If all the glyphs of this row came from strings, put the
14681 cursor on the first glyph of the row. This avoids having the
14682 cursor outside of the text area in this very rare and hard
14683 use case. */
14684 glyph =
14685 row->reversed_p
14686 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14687 : row->glyphs[TEXT_AREA];
14688 }
14689 if (x < 0)
14690 {
14691 struct glyph *g;
14692
14693 /* Need to compute x that corresponds to GLYPH. */
14694 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14695 {
14696 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14697 emacs_abort ();
14698 x += g->pixel_width;
14699 }
14700 }
14701
14702 /* ROW could be part of a continued line, which, under bidi
14703 reordering, might have other rows whose start and end charpos
14704 occlude point. Only set w->cursor if we found a better
14705 approximation to the cursor position than we have from previously
14706 examined candidate rows belonging to the same continued line. */
14707 if (/* We already have a candidate row. */
14708 w->cursor.vpos >= 0
14709 /* That candidate is not the row we are processing. */
14710 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14711 /* Make sure cursor.vpos specifies a row whose start and end
14712 charpos occlude point, and it is valid candidate for being a
14713 cursor-row. This is because some callers of this function
14714 leave cursor.vpos at the row where the cursor was displayed
14715 during the last redisplay cycle. */
14716 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14717 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14718 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14719 {
14720 struct glyph *g1
14721 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14722
14723 /* Don't consider glyphs that are outside TEXT_AREA. */
14724 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14725 return 0;
14726 /* Keep the candidate whose buffer position is the closest to
14727 point or has the `cursor' property. */
14728 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14729 w->cursor.hpos >= 0
14730 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14731 && ((BUFFERP (g1->object)
14732 && (g1->charpos == pt_old /* An exact match always wins. */
14733 || (BUFFERP (glyph->object)
14734 && eabs (g1->charpos - pt_old)
14735 < eabs (glyph->charpos - pt_old))))
14736 /* Previous candidate is a glyph from a string that has
14737 a non-nil `cursor' property. */
14738 || (STRINGP (g1->object)
14739 && (!NILP (Fget_char_property (make_number (g1->charpos),
14740 Qcursor, g1->object))
14741 /* Previous candidate is from the same display
14742 string as this one, and the display string
14743 came from a text property. */
14744 || (EQ (g1->object, glyph->object)
14745 && string_from_text_prop)
14746 /* this candidate is from newline and its
14747 position is not an exact match */
14748 || (INTEGERP (glyph->object)
14749 && glyph->charpos != pt_old)))))
14750 return 0;
14751 /* If this candidate gives an exact match, use that. */
14752 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14753 /* If this candidate is a glyph created for the
14754 terminating newline of a line, and point is on that
14755 newline, it wins because it's an exact match. */
14756 || (!row->continued_p
14757 && INTEGERP (glyph->object)
14758 && glyph->charpos == 0
14759 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14760 /* Otherwise, keep the candidate that comes from a row
14761 spanning less buffer positions. This may win when one or
14762 both candidate positions are on glyphs that came from
14763 display strings, for which we cannot compare buffer
14764 positions. */
14765 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14766 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14767 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14768 return 0;
14769 }
14770 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14771 w->cursor.x = x;
14772 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14773 w->cursor.y = row->y + dy;
14774
14775 if (w == XWINDOW (selected_window))
14776 {
14777 if (!row->continued_p
14778 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14779 && row->x == 0)
14780 {
14781 this_line_buffer = XBUFFER (w->contents);
14782
14783 CHARPOS (this_line_start_pos)
14784 = MATRIX_ROW_START_CHARPOS (row) + delta;
14785 BYTEPOS (this_line_start_pos)
14786 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14787
14788 CHARPOS (this_line_end_pos)
14789 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14790 BYTEPOS (this_line_end_pos)
14791 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14792
14793 this_line_y = w->cursor.y;
14794 this_line_pixel_height = row->height;
14795 this_line_vpos = w->cursor.vpos;
14796 this_line_start_x = row->x;
14797 }
14798 else
14799 CHARPOS (this_line_start_pos) = 0;
14800 }
14801
14802 return 1;
14803 }
14804
14805
14806 /* Run window scroll functions, if any, for WINDOW with new window
14807 start STARTP. Sets the window start of WINDOW to that position.
14808
14809 We assume that the window's buffer is really current. */
14810
14811 static struct text_pos
14812 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14813 {
14814 struct window *w = XWINDOW (window);
14815 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14816
14817 eassert (current_buffer == XBUFFER (w->contents));
14818
14819 if (!NILP (Vwindow_scroll_functions))
14820 {
14821 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14822 make_number (CHARPOS (startp)));
14823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14824 /* In case the hook functions switch buffers. */
14825 set_buffer_internal (XBUFFER (w->contents));
14826 }
14827
14828 return startp;
14829 }
14830
14831
14832 /* Make sure the line containing the cursor is fully visible.
14833 A value of 1 means there is nothing to be done.
14834 (Either the line is fully visible, or it cannot be made so,
14835 or we cannot tell.)
14836
14837 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14838 is higher than window.
14839
14840 A value of 0 means the caller should do scrolling
14841 as if point had gone off the screen. */
14842
14843 static int
14844 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14845 {
14846 struct glyph_matrix *matrix;
14847 struct glyph_row *row;
14848 int window_height;
14849
14850 if (!make_cursor_line_fully_visible_p)
14851 return 1;
14852
14853 /* It's not always possible to find the cursor, e.g, when a window
14854 is full of overlay strings. Don't do anything in that case. */
14855 if (w->cursor.vpos < 0)
14856 return 1;
14857
14858 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14859 row = MATRIX_ROW (matrix, w->cursor.vpos);
14860
14861 /* If the cursor row is not partially visible, there's nothing to do. */
14862 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14863 return 1;
14864
14865 /* If the row the cursor is in is taller than the window's height,
14866 it's not clear what to do, so do nothing. */
14867 window_height = window_box_height (w);
14868 if (row->height >= window_height)
14869 {
14870 if (!force_p || MINI_WINDOW_P (w)
14871 || w->vscroll || w->cursor.vpos == 0)
14872 return 1;
14873 }
14874 return 0;
14875 }
14876
14877
14878 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14879 non-zero means only WINDOW is redisplayed in redisplay_internal.
14880 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14881 in redisplay_window to bring a partially visible line into view in
14882 the case that only the cursor has moved.
14883
14884 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14885 last screen line's vertical height extends past the end of the screen.
14886
14887 Value is
14888
14889 1 if scrolling succeeded
14890
14891 0 if scrolling didn't find point.
14892
14893 -1 if new fonts have been loaded so that we must interrupt
14894 redisplay, adjust glyph matrices, and try again. */
14895
14896 enum
14897 {
14898 SCROLLING_SUCCESS,
14899 SCROLLING_FAILED,
14900 SCROLLING_NEED_LARGER_MATRICES
14901 };
14902
14903 /* If scroll-conservatively is more than this, never recenter.
14904
14905 If you change this, don't forget to update the doc string of
14906 `scroll-conservatively' and the Emacs manual. */
14907 #define SCROLL_LIMIT 100
14908
14909 static int
14910 try_scrolling (Lisp_Object window, int just_this_one_p,
14911 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14912 int temp_scroll_step, int last_line_misfit)
14913 {
14914 struct window *w = XWINDOW (window);
14915 struct frame *f = XFRAME (w->frame);
14916 struct text_pos pos, startp;
14917 struct it it;
14918 int this_scroll_margin, scroll_max, rc, height;
14919 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14920 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14921 Lisp_Object aggressive;
14922 /* We will never try scrolling more than this number of lines. */
14923 int scroll_limit = SCROLL_LIMIT;
14924 int frame_line_height = default_line_pixel_height (w);
14925 int window_total_lines
14926 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14927
14928 #ifdef GLYPH_DEBUG
14929 debug_method_add (w, "try_scrolling");
14930 #endif
14931
14932 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14933
14934 /* Compute scroll margin height in pixels. We scroll when point is
14935 within this distance from the top or bottom of the window. */
14936 if (scroll_margin > 0)
14937 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14938 * frame_line_height;
14939 else
14940 this_scroll_margin = 0;
14941
14942 /* Force arg_scroll_conservatively to have a reasonable value, to
14943 avoid scrolling too far away with slow move_it_* functions. Note
14944 that the user can supply scroll-conservatively equal to
14945 `most-positive-fixnum', which can be larger than INT_MAX. */
14946 if (arg_scroll_conservatively > scroll_limit)
14947 {
14948 arg_scroll_conservatively = scroll_limit + 1;
14949 scroll_max = scroll_limit * frame_line_height;
14950 }
14951 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14952 /* Compute how much we should try to scroll maximally to bring
14953 point into view. */
14954 scroll_max = (max (scroll_step,
14955 max (arg_scroll_conservatively, temp_scroll_step))
14956 * frame_line_height);
14957 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14958 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14959 /* We're trying to scroll because of aggressive scrolling but no
14960 scroll_step is set. Choose an arbitrary one. */
14961 scroll_max = 10 * frame_line_height;
14962 else
14963 scroll_max = 0;
14964
14965 too_near_end:
14966
14967 /* Decide whether to scroll down. */
14968 if (PT > CHARPOS (startp))
14969 {
14970 int scroll_margin_y;
14971
14972 /* Compute the pixel ypos of the scroll margin, then move IT to
14973 either that ypos or PT, whichever comes first. */
14974 start_display (&it, w, startp);
14975 scroll_margin_y = it.last_visible_y - this_scroll_margin
14976 - frame_line_height * extra_scroll_margin_lines;
14977 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14978 (MOVE_TO_POS | MOVE_TO_Y));
14979
14980 if (PT > CHARPOS (it.current.pos))
14981 {
14982 int y0 = line_bottom_y (&it);
14983 /* Compute how many pixels below window bottom to stop searching
14984 for PT. This avoids costly search for PT that is far away if
14985 the user limited scrolling by a small number of lines, but
14986 always finds PT if scroll_conservatively is set to a large
14987 number, such as most-positive-fixnum. */
14988 int slack = max (scroll_max, 10 * frame_line_height);
14989 int y_to_move = it.last_visible_y + slack;
14990
14991 /* Compute the distance from the scroll margin to PT or to
14992 the scroll limit, whichever comes first. This should
14993 include the height of the cursor line, to make that line
14994 fully visible. */
14995 move_it_to (&it, PT, -1, y_to_move,
14996 -1, MOVE_TO_POS | MOVE_TO_Y);
14997 dy = line_bottom_y (&it) - y0;
14998
14999 if (dy > scroll_max)
15000 return SCROLLING_FAILED;
15001
15002 if (dy > 0)
15003 scroll_down_p = 1;
15004 }
15005 }
15006
15007 if (scroll_down_p)
15008 {
15009 /* Point is in or below the bottom scroll margin, so move the
15010 window start down. If scrolling conservatively, move it just
15011 enough down to make point visible. If scroll_step is set,
15012 move it down by scroll_step. */
15013 if (arg_scroll_conservatively)
15014 amount_to_scroll
15015 = min (max (dy, frame_line_height),
15016 frame_line_height * arg_scroll_conservatively);
15017 else if (scroll_step || temp_scroll_step)
15018 amount_to_scroll = scroll_max;
15019 else
15020 {
15021 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15022 height = WINDOW_BOX_TEXT_HEIGHT (w);
15023 if (NUMBERP (aggressive))
15024 {
15025 double float_amount = XFLOATINT (aggressive) * height;
15026 int aggressive_scroll = float_amount;
15027 if (aggressive_scroll == 0 && float_amount > 0)
15028 aggressive_scroll = 1;
15029 /* Don't let point enter the scroll margin near top of
15030 the window. This could happen if the value of
15031 scroll_up_aggressively is too large and there are
15032 non-zero margins, because scroll_up_aggressively
15033 means put point that fraction of window height
15034 _from_the_bottom_margin_. */
15035 if (aggressive_scroll + 2*this_scroll_margin > height)
15036 aggressive_scroll = height - 2*this_scroll_margin;
15037 amount_to_scroll = dy + aggressive_scroll;
15038 }
15039 }
15040
15041 if (amount_to_scroll <= 0)
15042 return SCROLLING_FAILED;
15043
15044 start_display (&it, w, startp);
15045 if (arg_scroll_conservatively <= scroll_limit)
15046 move_it_vertically (&it, amount_to_scroll);
15047 else
15048 {
15049 /* Extra precision for users who set scroll-conservatively
15050 to a large number: make sure the amount we scroll
15051 the window start is never less than amount_to_scroll,
15052 which was computed as distance from window bottom to
15053 point. This matters when lines at window top and lines
15054 below window bottom have different height. */
15055 struct it it1;
15056 void *it1data = NULL;
15057 /* We use a temporary it1 because line_bottom_y can modify
15058 its argument, if it moves one line down; see there. */
15059 int start_y;
15060
15061 SAVE_IT (it1, it, it1data);
15062 start_y = line_bottom_y (&it1);
15063 do {
15064 RESTORE_IT (&it, &it, it1data);
15065 move_it_by_lines (&it, 1);
15066 SAVE_IT (it1, it, it1data);
15067 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15068 }
15069
15070 /* If STARTP is unchanged, move it down another screen line. */
15071 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15072 move_it_by_lines (&it, 1);
15073 startp = it.current.pos;
15074 }
15075 else
15076 {
15077 struct text_pos scroll_margin_pos = startp;
15078 int y_offset = 0;
15079
15080 /* See if point is inside the scroll margin at the top of the
15081 window. */
15082 if (this_scroll_margin)
15083 {
15084 int y_start;
15085
15086 start_display (&it, w, startp);
15087 y_start = it.current_y;
15088 move_it_vertically (&it, this_scroll_margin);
15089 scroll_margin_pos = it.current.pos;
15090 /* If we didn't move enough before hitting ZV, request
15091 additional amount of scroll, to move point out of the
15092 scroll margin. */
15093 if (IT_CHARPOS (it) == ZV
15094 && it.current_y - y_start < this_scroll_margin)
15095 y_offset = this_scroll_margin - (it.current_y - y_start);
15096 }
15097
15098 if (PT < CHARPOS (scroll_margin_pos))
15099 {
15100 /* Point is in the scroll margin at the top of the window or
15101 above what is displayed in the window. */
15102 int y0, y_to_move;
15103
15104 /* Compute the vertical distance from PT to the scroll
15105 margin position. Move as far as scroll_max allows, or
15106 one screenful, or 10 screen lines, whichever is largest.
15107 Give up if distance is greater than scroll_max or if we
15108 didn't reach the scroll margin position. */
15109 SET_TEXT_POS (pos, PT, PT_BYTE);
15110 start_display (&it, w, pos);
15111 y0 = it.current_y;
15112 y_to_move = max (it.last_visible_y,
15113 max (scroll_max, 10 * frame_line_height));
15114 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15115 y_to_move, -1,
15116 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15117 dy = it.current_y - y0;
15118 if (dy > scroll_max
15119 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15120 return SCROLLING_FAILED;
15121
15122 /* Additional scroll for when ZV was too close to point. */
15123 dy += y_offset;
15124
15125 /* Compute new window start. */
15126 start_display (&it, w, startp);
15127
15128 if (arg_scroll_conservatively)
15129 amount_to_scroll = max (dy, frame_line_height *
15130 max (scroll_step, temp_scroll_step));
15131 else if (scroll_step || temp_scroll_step)
15132 amount_to_scroll = scroll_max;
15133 else
15134 {
15135 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15136 height = WINDOW_BOX_TEXT_HEIGHT (w);
15137 if (NUMBERP (aggressive))
15138 {
15139 double float_amount = XFLOATINT (aggressive) * height;
15140 int aggressive_scroll = float_amount;
15141 if (aggressive_scroll == 0 && float_amount > 0)
15142 aggressive_scroll = 1;
15143 /* Don't let point enter the scroll margin near
15144 bottom of the window, if the value of
15145 scroll_down_aggressively happens to be too
15146 large. */
15147 if (aggressive_scroll + 2*this_scroll_margin > height)
15148 aggressive_scroll = height - 2*this_scroll_margin;
15149 amount_to_scroll = dy + aggressive_scroll;
15150 }
15151 }
15152
15153 if (amount_to_scroll <= 0)
15154 return SCROLLING_FAILED;
15155
15156 move_it_vertically_backward (&it, amount_to_scroll);
15157 startp = it.current.pos;
15158 }
15159 }
15160
15161 /* Run window scroll functions. */
15162 startp = run_window_scroll_functions (window, startp);
15163
15164 /* Display the window. Give up if new fonts are loaded, or if point
15165 doesn't appear. */
15166 if (!try_window (window, startp, 0))
15167 rc = SCROLLING_NEED_LARGER_MATRICES;
15168 else if (w->cursor.vpos < 0)
15169 {
15170 clear_glyph_matrix (w->desired_matrix);
15171 rc = SCROLLING_FAILED;
15172 }
15173 else
15174 {
15175 /* Maybe forget recorded base line for line number display. */
15176 if (!just_this_one_p
15177 || current_buffer->clip_changed
15178 || BEG_UNCHANGED < CHARPOS (startp))
15179 w->base_line_number = 0;
15180
15181 /* If cursor ends up on a partially visible line,
15182 treat that as being off the bottom of the screen. */
15183 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15184 /* It's possible that the cursor is on the first line of the
15185 buffer, which is partially obscured due to a vscroll
15186 (Bug#7537). In that case, avoid looping forever. */
15187 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15188 {
15189 clear_glyph_matrix (w->desired_matrix);
15190 ++extra_scroll_margin_lines;
15191 goto too_near_end;
15192 }
15193 rc = SCROLLING_SUCCESS;
15194 }
15195
15196 return rc;
15197 }
15198
15199
15200 /* Compute a suitable window start for window W if display of W starts
15201 on a continuation line. Value is non-zero if a new window start
15202 was computed.
15203
15204 The new window start will be computed, based on W's width, starting
15205 from the start of the continued line. It is the start of the
15206 screen line with the minimum distance from the old start W->start. */
15207
15208 static int
15209 compute_window_start_on_continuation_line (struct window *w)
15210 {
15211 struct text_pos pos, start_pos;
15212 int window_start_changed_p = 0;
15213
15214 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15215
15216 /* If window start is on a continuation line... Window start may be
15217 < BEGV in case there's invisible text at the start of the
15218 buffer (M-x rmail, for example). */
15219 if (CHARPOS (start_pos) > BEGV
15220 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15221 {
15222 struct it it;
15223 struct glyph_row *row;
15224
15225 /* Handle the case that the window start is out of range. */
15226 if (CHARPOS (start_pos) < BEGV)
15227 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15228 else if (CHARPOS (start_pos) > ZV)
15229 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15230
15231 /* Find the start of the continued line. This should be fast
15232 because find_newline is fast (newline cache). */
15233 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15234 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15235 row, DEFAULT_FACE_ID);
15236 reseat_at_previous_visible_line_start (&it);
15237
15238 /* If the line start is "too far" away from the window start,
15239 say it takes too much time to compute a new window start. */
15240 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15241 /* PXW: Do we need upper bounds here? */
15242 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15243 {
15244 int min_distance, distance;
15245
15246 /* Move forward by display lines to find the new window
15247 start. If window width was enlarged, the new start can
15248 be expected to be > the old start. If window width was
15249 decreased, the new window start will be < the old start.
15250 So, we're looking for the display line start with the
15251 minimum distance from the old window start. */
15252 pos = it.current.pos;
15253 min_distance = INFINITY;
15254 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15255 distance < min_distance)
15256 {
15257 min_distance = distance;
15258 pos = it.current.pos;
15259 if (it.line_wrap == WORD_WRAP)
15260 {
15261 /* Under WORD_WRAP, move_it_by_lines is likely to
15262 overshoot and stop not at the first, but the
15263 second character from the left margin. So in
15264 that case, we need a more tight control on the X
15265 coordinate of the iterator than move_it_by_lines
15266 promises in its contract. The method is to first
15267 go to the last (rightmost) visible character of a
15268 line, then move to the leftmost character on the
15269 next line in a separate call. */
15270 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15272 move_it_to (&it, ZV, 0,
15273 it.current_y + it.max_ascent + it.max_descent, -1,
15274 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15275 }
15276 else
15277 move_it_by_lines (&it, 1);
15278 }
15279
15280 /* Set the window start there. */
15281 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15282 window_start_changed_p = 1;
15283 }
15284 }
15285
15286 return window_start_changed_p;
15287 }
15288
15289
15290 /* Try cursor movement in case text has not changed in window WINDOW,
15291 with window start STARTP. Value is
15292
15293 CURSOR_MOVEMENT_SUCCESS if successful
15294
15295 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15296
15297 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15298 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15299 we want to scroll as if scroll-step were set to 1. See the code.
15300
15301 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15302 which case we have to abort this redisplay, and adjust matrices
15303 first. */
15304
15305 enum
15306 {
15307 CURSOR_MOVEMENT_SUCCESS,
15308 CURSOR_MOVEMENT_CANNOT_BE_USED,
15309 CURSOR_MOVEMENT_MUST_SCROLL,
15310 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15311 };
15312
15313 static int
15314 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15315 {
15316 struct window *w = XWINDOW (window);
15317 struct frame *f = XFRAME (w->frame);
15318 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15319
15320 #ifdef GLYPH_DEBUG
15321 if (inhibit_try_cursor_movement)
15322 return rc;
15323 #endif
15324
15325 /* Previously, there was a check for Lisp integer in the
15326 if-statement below. Now, this field is converted to
15327 ptrdiff_t, thus zero means invalid position in a buffer. */
15328 eassert (w->last_point > 0);
15329 /* Likewise there was a check whether window_end_vpos is nil or larger
15330 than the window. Now window_end_vpos is int and so never nil, but
15331 let's leave eassert to check whether it fits in the window. */
15332 eassert (w->window_end_vpos < w->current_matrix->nrows);
15333
15334 /* Handle case where text has not changed, only point, and it has
15335 not moved off the frame. */
15336 if (/* Point may be in this window. */
15337 PT >= CHARPOS (startp)
15338 /* Selective display hasn't changed. */
15339 && !current_buffer->clip_changed
15340 /* Function force-mode-line-update is used to force a thorough
15341 redisplay. It sets either windows_or_buffers_changed or
15342 update_mode_lines. So don't take a shortcut here for these
15343 cases. */
15344 && !update_mode_lines
15345 && !windows_or_buffers_changed
15346 && !f->cursor_type_changed
15347 && NILP (Vshow_trailing_whitespace)
15348 /* This code is not used for mini-buffer for the sake of the case
15349 of redisplaying to replace an echo area message; since in
15350 that case the mini-buffer contents per se are usually
15351 unchanged. This code is of no real use in the mini-buffer
15352 since the handling of this_line_start_pos, etc., in redisplay
15353 handles the same cases. */
15354 && !EQ (window, minibuf_window)
15355 && (FRAME_WINDOW_P (f)
15356 || !overlay_arrow_in_current_buffer_p ()))
15357 {
15358 int this_scroll_margin, top_scroll_margin;
15359 struct glyph_row *row = NULL;
15360 int frame_line_height = default_line_pixel_height (w);
15361 int window_total_lines
15362 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15363
15364 #ifdef GLYPH_DEBUG
15365 debug_method_add (w, "cursor movement");
15366 #endif
15367
15368 /* Scroll if point within this distance from the top or bottom
15369 of the window. This is a pixel value. */
15370 if (scroll_margin > 0)
15371 {
15372 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15373 this_scroll_margin *= frame_line_height;
15374 }
15375 else
15376 this_scroll_margin = 0;
15377
15378 top_scroll_margin = this_scroll_margin;
15379 if (WINDOW_WANTS_HEADER_LINE_P (w))
15380 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15381
15382 /* Start with the row the cursor was displayed during the last
15383 not paused redisplay. Give up if that row is not valid. */
15384 if (w->last_cursor_vpos < 0
15385 || w->last_cursor_vpos >= w->current_matrix->nrows)
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 else
15388 {
15389 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15390 if (row->mode_line_p)
15391 ++row;
15392 if (!row->enabled_p)
15393 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15394 }
15395
15396 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15397 {
15398 int scroll_p = 0, must_scroll = 0;
15399 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15400
15401 if (PT > w->last_point)
15402 {
15403 /* Point has moved forward. */
15404 while (MATRIX_ROW_END_CHARPOS (row) < PT
15405 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15406 {
15407 eassert (row->enabled_p);
15408 ++row;
15409 }
15410
15411 /* If the end position of a row equals the start
15412 position of the next row, and PT is at that position,
15413 we would rather display cursor in the next line. */
15414 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15415 && MATRIX_ROW_END_CHARPOS (row) == PT
15416 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15417 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15418 && !cursor_row_p (row))
15419 ++row;
15420
15421 /* If within the scroll margin, scroll. Note that
15422 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15423 the next line would be drawn, and that
15424 this_scroll_margin can be zero. */
15425 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15426 || PT > MATRIX_ROW_END_CHARPOS (row)
15427 /* Line is completely visible last line in window
15428 and PT is to be set in the next line. */
15429 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15430 && PT == MATRIX_ROW_END_CHARPOS (row)
15431 && !row->ends_at_zv_p
15432 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15433 scroll_p = 1;
15434 }
15435 else if (PT < w->last_point)
15436 {
15437 /* Cursor has to be moved backward. Note that PT >=
15438 CHARPOS (startp) because of the outer if-statement. */
15439 while (!row->mode_line_p
15440 && (MATRIX_ROW_START_CHARPOS (row) > PT
15441 || (MATRIX_ROW_START_CHARPOS (row) == PT
15442 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15443 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15444 row > w->current_matrix->rows
15445 && (row-1)->ends_in_newline_from_string_p))))
15446 && (row->y > top_scroll_margin
15447 || CHARPOS (startp) == BEGV))
15448 {
15449 eassert (row->enabled_p);
15450 --row;
15451 }
15452
15453 /* Consider the following case: Window starts at BEGV,
15454 there is invisible, intangible text at BEGV, so that
15455 display starts at some point START > BEGV. It can
15456 happen that we are called with PT somewhere between
15457 BEGV and START. Try to handle that case. */
15458 if (row < w->current_matrix->rows
15459 || row->mode_line_p)
15460 {
15461 row = w->current_matrix->rows;
15462 if (row->mode_line_p)
15463 ++row;
15464 }
15465
15466 /* Due to newlines in overlay strings, we may have to
15467 skip forward over overlay strings. */
15468 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15469 && MATRIX_ROW_END_CHARPOS (row) == PT
15470 && !cursor_row_p (row))
15471 ++row;
15472
15473 /* If within the scroll margin, scroll. */
15474 if (row->y < top_scroll_margin
15475 && CHARPOS (startp) != BEGV)
15476 scroll_p = 1;
15477 }
15478 else
15479 {
15480 /* Cursor did not move. So don't scroll even if cursor line
15481 is partially visible, as it was so before. */
15482 rc = CURSOR_MOVEMENT_SUCCESS;
15483 }
15484
15485 if (PT < MATRIX_ROW_START_CHARPOS (row)
15486 || PT > MATRIX_ROW_END_CHARPOS (row))
15487 {
15488 /* if PT is not in the glyph row, give up. */
15489 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15490 must_scroll = 1;
15491 }
15492 else if (rc != CURSOR_MOVEMENT_SUCCESS
15493 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15494 {
15495 struct glyph_row *row1;
15496
15497 /* If rows are bidi-reordered and point moved, back up
15498 until we find a row that does not belong to a
15499 continuation line. This is because we must consider
15500 all rows of a continued line as candidates for the
15501 new cursor positioning, since row start and end
15502 positions change non-linearly with vertical position
15503 in such rows. */
15504 /* FIXME: Revisit this when glyph ``spilling'' in
15505 continuation lines' rows is implemented for
15506 bidi-reordered rows. */
15507 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15508 MATRIX_ROW_CONTINUATION_LINE_P (row);
15509 --row)
15510 {
15511 /* If we hit the beginning of the displayed portion
15512 without finding the first row of a continued
15513 line, give up. */
15514 if (row <= row1)
15515 {
15516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15517 break;
15518 }
15519 eassert (row->enabled_p);
15520 }
15521 }
15522 if (must_scroll)
15523 ;
15524 else if (rc != CURSOR_MOVEMENT_SUCCESS
15525 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15526 /* Make sure this isn't a header line by any chance, since
15527 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15528 && !row->mode_line_p
15529 && make_cursor_line_fully_visible_p)
15530 {
15531 if (PT == MATRIX_ROW_END_CHARPOS (row)
15532 && !row->ends_at_zv_p
15533 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15534 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15535 else if (row->height > window_box_height (w))
15536 {
15537 /* If we end up in a partially visible line, let's
15538 make it fully visible, except when it's taller
15539 than the window, in which case we can't do much
15540 about it. */
15541 *scroll_step = 1;
15542 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15543 }
15544 else
15545 {
15546 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15547 if (!cursor_row_fully_visible_p (w, 0, 1))
15548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15549 else
15550 rc = CURSOR_MOVEMENT_SUCCESS;
15551 }
15552 }
15553 else if (scroll_p)
15554 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15555 else if (rc != CURSOR_MOVEMENT_SUCCESS
15556 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15557 {
15558 /* With bidi-reordered rows, there could be more than
15559 one candidate row whose start and end positions
15560 occlude point. We need to let set_cursor_from_row
15561 find the best candidate. */
15562 /* FIXME: Revisit this when glyph ``spilling'' in
15563 continuation lines' rows is implemented for
15564 bidi-reordered rows. */
15565 int rv = 0;
15566
15567 do
15568 {
15569 int at_zv_p = 0, exact_match_p = 0;
15570
15571 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15572 && PT <= MATRIX_ROW_END_CHARPOS (row)
15573 && cursor_row_p (row))
15574 rv |= set_cursor_from_row (w, row, w->current_matrix,
15575 0, 0, 0, 0);
15576 /* As soon as we've found the exact match for point,
15577 or the first suitable row whose ends_at_zv_p flag
15578 is set, we are done. */
15579 if (rv)
15580 {
15581 at_zv_p = MATRIX_ROW (w->current_matrix,
15582 w->cursor.vpos)->ends_at_zv_p;
15583 if (!at_zv_p
15584 && w->cursor.hpos >= 0
15585 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15586 w->cursor.vpos))
15587 {
15588 struct glyph_row *candidate =
15589 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15590 struct glyph *g =
15591 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15592 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15593
15594 exact_match_p =
15595 (BUFFERP (g->object) && g->charpos == PT)
15596 || (INTEGERP (g->object)
15597 && (g->charpos == PT
15598 || (g->charpos == 0 && endpos - 1 == PT)));
15599 }
15600 if (at_zv_p || exact_match_p)
15601 {
15602 rc = CURSOR_MOVEMENT_SUCCESS;
15603 break;
15604 }
15605 }
15606 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15607 break;
15608 ++row;
15609 }
15610 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15611 || row->continued_p)
15612 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15613 || (MATRIX_ROW_START_CHARPOS (row) == PT
15614 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15615 /* If we didn't find any candidate rows, or exited the
15616 loop before all the candidates were examined, signal
15617 to the caller that this method failed. */
15618 if (rc != CURSOR_MOVEMENT_SUCCESS
15619 && !(rv
15620 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15621 && !row->continued_p))
15622 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15623 else if (rv)
15624 rc = CURSOR_MOVEMENT_SUCCESS;
15625 }
15626 else
15627 {
15628 do
15629 {
15630 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15631 {
15632 rc = CURSOR_MOVEMENT_SUCCESS;
15633 break;
15634 }
15635 ++row;
15636 }
15637 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15638 && MATRIX_ROW_START_CHARPOS (row) == PT
15639 && cursor_row_p (row));
15640 }
15641 }
15642 }
15643
15644 return rc;
15645 }
15646
15647 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15648 static
15649 #endif
15650 void
15651 set_vertical_scroll_bar (struct window *w)
15652 {
15653 ptrdiff_t start, end, whole;
15654
15655 /* Calculate the start and end positions for the current window.
15656 At some point, it would be nice to choose between scrollbars
15657 which reflect the whole buffer size, with special markers
15658 indicating narrowing, and scrollbars which reflect only the
15659 visible region.
15660
15661 Note that mini-buffers sometimes aren't displaying any text. */
15662 if (!MINI_WINDOW_P (w)
15663 || (w == XWINDOW (minibuf_window)
15664 && NILP (echo_area_buffer[0])))
15665 {
15666 struct buffer *buf = XBUFFER (w->contents);
15667 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15668 start = marker_position (w->start) - BUF_BEGV (buf);
15669 /* I don't think this is guaranteed to be right. For the
15670 moment, we'll pretend it is. */
15671 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15672
15673 if (end < start)
15674 end = start;
15675 if (whole < (end - start))
15676 whole = end - start;
15677 }
15678 else
15679 start = end = whole = 0;
15680
15681 /* Indicate what this scroll bar ought to be displaying now. */
15682 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15683 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15684 (w, end - start, whole, start);
15685 }
15686
15687
15688 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15689 selected_window is redisplayed.
15690
15691 We can return without actually redisplaying the window if fonts has been
15692 changed on window's frame. In that case, redisplay_internal will retry. */
15693
15694 static void
15695 redisplay_window (Lisp_Object window, bool just_this_one_p)
15696 {
15697 struct window *w = XWINDOW (window);
15698 struct frame *f = XFRAME (w->frame);
15699 struct buffer *buffer = XBUFFER (w->contents);
15700 struct buffer *old = current_buffer;
15701 struct text_pos lpoint, opoint, startp;
15702 int update_mode_line;
15703 int tem;
15704 struct it it;
15705 /* Record it now because it's overwritten. */
15706 bool current_matrix_up_to_date_p = false;
15707 bool used_current_matrix_p = false;
15708 /* This is less strict than current_matrix_up_to_date_p.
15709 It indicates that the buffer contents and narrowing are unchanged. */
15710 bool buffer_unchanged_p = false;
15711 int temp_scroll_step = 0;
15712 ptrdiff_t count = SPECPDL_INDEX ();
15713 int rc;
15714 int centering_position = -1;
15715 int last_line_misfit = 0;
15716 ptrdiff_t beg_unchanged, end_unchanged;
15717 int frame_line_height;
15718
15719 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15720 opoint = lpoint;
15721
15722 #ifdef GLYPH_DEBUG
15723 *w->desired_matrix->method = 0;
15724 #endif
15725
15726 if (!just_this_one_p
15727 && REDISPLAY_SOME_P ()
15728 && !w->redisplay
15729 && !f->redisplay
15730 && !buffer->text->redisplay
15731 && BUF_PT (buffer) == w->last_point)
15732 return;
15733
15734 /* Make sure that both W's markers are valid. */
15735 eassert (XMARKER (w->start)->buffer == buffer);
15736 eassert (XMARKER (w->pointm)->buffer == buffer);
15737
15738 restart:
15739 reconsider_clip_changes (w);
15740 frame_line_height = default_line_pixel_height (w);
15741
15742 /* Has the mode line to be updated? */
15743 update_mode_line = (w->update_mode_line
15744 || update_mode_lines
15745 || buffer->clip_changed
15746 || buffer->prevent_redisplay_optimizations_p);
15747
15748 if (!just_this_one_p)
15749 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15750 cleverly elsewhere. */
15751 w->must_be_updated_p = true;
15752
15753 if (MINI_WINDOW_P (w))
15754 {
15755 if (w == XWINDOW (echo_area_window)
15756 && !NILP (echo_area_buffer[0]))
15757 {
15758 if (update_mode_line)
15759 /* We may have to update a tty frame's menu bar or a
15760 tool-bar. Example `M-x C-h C-h C-g'. */
15761 goto finish_menu_bars;
15762 else
15763 /* We've already displayed the echo area glyphs in this window. */
15764 goto finish_scroll_bars;
15765 }
15766 else if ((w != XWINDOW (minibuf_window)
15767 || minibuf_level == 0)
15768 /* When buffer is nonempty, redisplay window normally. */
15769 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15770 /* Quail displays non-mini buffers in minibuffer window.
15771 In that case, redisplay the window normally. */
15772 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15773 {
15774 /* W is a mini-buffer window, but it's not active, so clear
15775 it. */
15776 int yb = window_text_bottom_y (w);
15777 struct glyph_row *row;
15778 int y;
15779
15780 for (y = 0, row = w->desired_matrix->rows;
15781 y < yb;
15782 y += row->height, ++row)
15783 blank_row (w, row, y);
15784 goto finish_scroll_bars;
15785 }
15786
15787 clear_glyph_matrix (w->desired_matrix);
15788 }
15789
15790 /* Otherwise set up data on this window; select its buffer and point
15791 value. */
15792 /* Really select the buffer, for the sake of buffer-local
15793 variables. */
15794 set_buffer_internal_1 (XBUFFER (w->contents));
15795
15796 current_matrix_up_to_date_p
15797 = (w->window_end_valid
15798 && !current_buffer->clip_changed
15799 && !current_buffer->prevent_redisplay_optimizations_p
15800 && !window_outdated (w));
15801
15802 /* Run the window-bottom-change-functions
15803 if it is possible that the text on the screen has changed
15804 (either due to modification of the text, or any other reason). */
15805 if (!current_matrix_up_to_date_p
15806 && !NILP (Vwindow_text_change_functions))
15807 {
15808 safe_run_hooks (Qwindow_text_change_functions);
15809 goto restart;
15810 }
15811
15812 beg_unchanged = BEG_UNCHANGED;
15813 end_unchanged = END_UNCHANGED;
15814
15815 SET_TEXT_POS (opoint, PT, PT_BYTE);
15816
15817 specbind (Qinhibit_point_motion_hooks, Qt);
15818
15819 buffer_unchanged_p
15820 = (w->window_end_valid
15821 && !current_buffer->clip_changed
15822 && !window_outdated (w));
15823
15824 /* When windows_or_buffers_changed is non-zero, we can't rely
15825 on the window end being valid, so set it to zero there. */
15826 if (windows_or_buffers_changed)
15827 {
15828 /* If window starts on a continuation line, maybe adjust the
15829 window start in case the window's width changed. */
15830 if (XMARKER (w->start)->buffer == current_buffer)
15831 compute_window_start_on_continuation_line (w);
15832
15833 w->window_end_valid = false;
15834 /* If so, we also can't rely on current matrix
15835 and should not fool try_cursor_movement below. */
15836 current_matrix_up_to_date_p = false;
15837 }
15838
15839 /* Some sanity checks. */
15840 CHECK_WINDOW_END (w);
15841 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15842 emacs_abort ();
15843 if (BYTEPOS (opoint) < CHARPOS (opoint))
15844 emacs_abort ();
15845
15846 if (mode_line_update_needed (w))
15847 update_mode_line = 1;
15848
15849 /* Point refers normally to the selected window. For any other
15850 window, set up appropriate value. */
15851 if (!EQ (window, selected_window))
15852 {
15853 ptrdiff_t new_pt = marker_position (w->pointm);
15854 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15855 if (new_pt < BEGV)
15856 {
15857 new_pt = BEGV;
15858 new_pt_byte = BEGV_BYTE;
15859 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15860 }
15861 else if (new_pt > (ZV - 1))
15862 {
15863 new_pt = ZV;
15864 new_pt_byte = ZV_BYTE;
15865 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15866 }
15867
15868 /* We don't use SET_PT so that the point-motion hooks don't run. */
15869 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15870 }
15871
15872 /* If any of the character widths specified in the display table
15873 have changed, invalidate the width run cache. It's true that
15874 this may be a bit late to catch such changes, but the rest of
15875 redisplay goes (non-fatally) haywire when the display table is
15876 changed, so why should we worry about doing any better? */
15877 if (current_buffer->width_run_cache
15878 || (current_buffer->base_buffer
15879 && current_buffer->base_buffer->width_run_cache))
15880 {
15881 struct Lisp_Char_Table *disptab = buffer_display_table ();
15882
15883 if (! disptab_matches_widthtab
15884 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15885 {
15886 struct buffer *buf = current_buffer;
15887
15888 if (buf->base_buffer)
15889 buf = buf->base_buffer;
15890 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15891 recompute_width_table (current_buffer, disptab);
15892 }
15893 }
15894
15895 /* If window-start is screwed up, choose a new one. */
15896 if (XMARKER (w->start)->buffer != current_buffer)
15897 goto recenter;
15898
15899 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15900
15901 /* If someone specified a new starting point but did not insist,
15902 check whether it can be used. */
15903 if (w->optional_new_start
15904 && CHARPOS (startp) >= BEGV
15905 && CHARPOS (startp) <= ZV)
15906 {
15907 w->optional_new_start = 0;
15908 start_display (&it, w, startp);
15909 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15910 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15911 if (IT_CHARPOS (it) == PT)
15912 w->force_start = 1;
15913 /* IT may overshoot PT if text at PT is invisible. */
15914 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15915 w->force_start = 1;
15916 }
15917
15918 force_start:
15919
15920 /* Handle case where place to start displaying has been specified,
15921 unless the specified location is outside the accessible range. */
15922 if (w->force_start || window_frozen_p (w))
15923 {
15924 /* We set this later on if we have to adjust point. */
15925 int new_vpos = -1;
15926
15927 w->force_start = 0;
15928 w->vscroll = 0;
15929 w->window_end_valid = 0;
15930
15931 /* Forget any recorded base line for line number display. */
15932 if (!buffer_unchanged_p)
15933 w->base_line_number = 0;
15934
15935 /* Redisplay the mode line. Select the buffer properly for that.
15936 Also, run the hook window-scroll-functions
15937 because we have scrolled. */
15938 /* Note, we do this after clearing force_start because
15939 if there's an error, it is better to forget about force_start
15940 than to get into an infinite loop calling the hook functions
15941 and having them get more errors. */
15942 if (!update_mode_line
15943 || ! NILP (Vwindow_scroll_functions))
15944 {
15945 update_mode_line = 1;
15946 w->update_mode_line = 1;
15947 startp = run_window_scroll_functions (window, startp);
15948 }
15949
15950 if (CHARPOS (startp) < BEGV)
15951 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15952 else if (CHARPOS (startp) > ZV)
15953 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15954
15955 /* Redisplay, then check if cursor has been set during the
15956 redisplay. Give up if new fonts were loaded. */
15957 /* We used to issue a CHECK_MARGINS argument to try_window here,
15958 but this causes scrolling to fail when point begins inside
15959 the scroll margin (bug#148) -- cyd */
15960 if (!try_window (window, startp, 0))
15961 {
15962 w->force_start = 1;
15963 clear_glyph_matrix (w->desired_matrix);
15964 goto need_larger_matrices;
15965 }
15966
15967 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15968 {
15969 /* If point does not appear, try to move point so it does
15970 appear. The desired matrix has been built above, so we
15971 can use it here. */
15972 new_vpos = window_box_height (w) / 2;
15973 }
15974
15975 if (!cursor_row_fully_visible_p (w, 0, 0))
15976 {
15977 /* Point does appear, but on a line partly visible at end of window.
15978 Move it back to a fully-visible line. */
15979 new_vpos = window_box_height (w);
15980 }
15981 else if (w->cursor.vpos >= 0)
15982 {
15983 /* Some people insist on not letting point enter the scroll
15984 margin, even though this part handles windows that didn't
15985 scroll at all. */
15986 int window_total_lines
15987 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15988 int margin = min (scroll_margin, window_total_lines / 4);
15989 int pixel_margin = margin * frame_line_height;
15990 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15991
15992 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15993 below, which finds the row to move point to, advances by
15994 the Y coordinate of the _next_ row, see the definition of
15995 MATRIX_ROW_BOTTOM_Y. */
15996 if (w->cursor.vpos < margin + header_line)
15997 {
15998 w->cursor.vpos = -1;
15999 clear_glyph_matrix (w->desired_matrix);
16000 goto try_to_scroll;
16001 }
16002 else
16003 {
16004 int window_height = window_box_height (w);
16005
16006 if (header_line)
16007 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16008 if (w->cursor.y >= window_height - pixel_margin)
16009 {
16010 w->cursor.vpos = -1;
16011 clear_glyph_matrix (w->desired_matrix);
16012 goto try_to_scroll;
16013 }
16014 }
16015 }
16016
16017 /* If we need to move point for either of the above reasons,
16018 now actually do it. */
16019 if (new_vpos >= 0)
16020 {
16021 struct glyph_row *row;
16022
16023 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16024 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16025 ++row;
16026
16027 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16028 MATRIX_ROW_START_BYTEPOS (row));
16029
16030 if (w != XWINDOW (selected_window))
16031 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16032 else if (current_buffer == old)
16033 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16034
16035 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16036
16037 /* If we are highlighting the region, then we just changed
16038 the region, so redisplay to show it. */
16039 /* FIXME: We need to (re)run pre-redisplay-function! */
16040 /* if (markpos_of_region () >= 0)
16041 {
16042 clear_glyph_matrix (w->desired_matrix);
16043 if (!try_window (window, startp, 0))
16044 goto need_larger_matrices;
16045 }
16046 */
16047 }
16048
16049 #ifdef GLYPH_DEBUG
16050 debug_method_add (w, "forced window start");
16051 #endif
16052 goto done;
16053 }
16054
16055 /* Handle case where text has not changed, only point, and it has
16056 not moved off the frame, and we are not retrying after hscroll.
16057 (current_matrix_up_to_date_p is nonzero when retrying.) */
16058 if (current_matrix_up_to_date_p
16059 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16060 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16061 {
16062 switch (rc)
16063 {
16064 case CURSOR_MOVEMENT_SUCCESS:
16065 used_current_matrix_p = 1;
16066 goto done;
16067
16068 case CURSOR_MOVEMENT_MUST_SCROLL:
16069 goto try_to_scroll;
16070
16071 default:
16072 emacs_abort ();
16073 }
16074 }
16075 /* If current starting point was originally the beginning of a line
16076 but no longer is, find a new starting point. */
16077 else if (w->start_at_line_beg
16078 && !(CHARPOS (startp) <= BEGV
16079 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16080 {
16081 #ifdef GLYPH_DEBUG
16082 debug_method_add (w, "recenter 1");
16083 #endif
16084 goto recenter;
16085 }
16086
16087 /* Try scrolling with try_window_id. Value is > 0 if update has
16088 been done, it is -1 if we know that the same window start will
16089 not work. It is 0 if unsuccessful for some other reason. */
16090 else if ((tem = try_window_id (w)) != 0)
16091 {
16092 #ifdef GLYPH_DEBUG
16093 debug_method_add (w, "try_window_id %d", tem);
16094 #endif
16095
16096 if (f->fonts_changed)
16097 goto need_larger_matrices;
16098 if (tem > 0)
16099 goto done;
16100
16101 /* Otherwise try_window_id has returned -1 which means that we
16102 don't want the alternative below this comment to execute. */
16103 }
16104 else if (CHARPOS (startp) >= BEGV
16105 && CHARPOS (startp) <= ZV
16106 && PT >= CHARPOS (startp)
16107 && (CHARPOS (startp) < ZV
16108 /* Avoid starting at end of buffer. */
16109 || CHARPOS (startp) == BEGV
16110 || !window_outdated (w)))
16111 {
16112 int d1, d2, d3, d4, d5, d6;
16113
16114 /* If first window line is a continuation line, and window start
16115 is inside the modified region, but the first change is before
16116 current window start, we must select a new window start.
16117
16118 However, if this is the result of a down-mouse event (e.g. by
16119 extending the mouse-drag-overlay), we don't want to select a
16120 new window start, since that would change the position under
16121 the mouse, resulting in an unwanted mouse-movement rather
16122 than a simple mouse-click. */
16123 if (!w->start_at_line_beg
16124 && NILP (do_mouse_tracking)
16125 && CHARPOS (startp) > BEGV
16126 && CHARPOS (startp) > BEG + beg_unchanged
16127 && CHARPOS (startp) <= Z - end_unchanged
16128 /* Even if w->start_at_line_beg is nil, a new window may
16129 start at a line_beg, since that's how set_buffer_window
16130 sets it. So, we need to check the return value of
16131 compute_window_start_on_continuation_line. (See also
16132 bug#197). */
16133 && XMARKER (w->start)->buffer == current_buffer
16134 && compute_window_start_on_continuation_line (w)
16135 /* It doesn't make sense to force the window start like we
16136 do at label force_start if it is already known that point
16137 will not be visible in the resulting window, because
16138 doing so will move point from its correct position
16139 instead of scrolling the window to bring point into view.
16140 See bug#9324. */
16141 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16142 {
16143 w->force_start = 1;
16144 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16145 goto force_start;
16146 }
16147
16148 #ifdef GLYPH_DEBUG
16149 debug_method_add (w, "same window start");
16150 #endif
16151
16152 /* Try to redisplay starting at same place as before.
16153 If point has not moved off frame, accept the results. */
16154 if (!current_matrix_up_to_date_p
16155 /* Don't use try_window_reusing_current_matrix in this case
16156 because a window scroll function can have changed the
16157 buffer. */
16158 || !NILP (Vwindow_scroll_functions)
16159 || MINI_WINDOW_P (w)
16160 || !(used_current_matrix_p
16161 = try_window_reusing_current_matrix (w)))
16162 {
16163 IF_DEBUG (debug_method_add (w, "1"));
16164 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16165 /* -1 means we need to scroll.
16166 0 means we need new matrices, but fonts_changed
16167 is set in that case, so we will detect it below. */
16168 goto try_to_scroll;
16169 }
16170
16171 if (f->fonts_changed)
16172 goto need_larger_matrices;
16173
16174 if (w->cursor.vpos >= 0)
16175 {
16176 if (!just_this_one_p
16177 || current_buffer->clip_changed
16178 || BEG_UNCHANGED < CHARPOS (startp))
16179 /* Forget any recorded base line for line number display. */
16180 w->base_line_number = 0;
16181
16182 if (!cursor_row_fully_visible_p (w, 1, 0))
16183 {
16184 clear_glyph_matrix (w->desired_matrix);
16185 last_line_misfit = 1;
16186 }
16187 /* Drop through and scroll. */
16188 else
16189 goto done;
16190 }
16191 else
16192 clear_glyph_matrix (w->desired_matrix);
16193 }
16194
16195 try_to_scroll:
16196
16197 /* Redisplay the mode line. Select the buffer properly for that. */
16198 if (!update_mode_line)
16199 {
16200 update_mode_line = 1;
16201 w->update_mode_line = 1;
16202 }
16203
16204 /* Try to scroll by specified few lines. */
16205 if ((scroll_conservatively
16206 || emacs_scroll_step
16207 || temp_scroll_step
16208 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16209 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16210 && CHARPOS (startp) >= BEGV
16211 && CHARPOS (startp) <= ZV)
16212 {
16213 /* The function returns -1 if new fonts were loaded, 1 if
16214 successful, 0 if not successful. */
16215 int ss = try_scrolling (window, just_this_one_p,
16216 scroll_conservatively,
16217 emacs_scroll_step,
16218 temp_scroll_step, last_line_misfit);
16219 switch (ss)
16220 {
16221 case SCROLLING_SUCCESS:
16222 goto done;
16223
16224 case SCROLLING_NEED_LARGER_MATRICES:
16225 goto need_larger_matrices;
16226
16227 case SCROLLING_FAILED:
16228 break;
16229
16230 default:
16231 emacs_abort ();
16232 }
16233 }
16234
16235 /* Finally, just choose a place to start which positions point
16236 according to user preferences. */
16237
16238 recenter:
16239
16240 #ifdef GLYPH_DEBUG
16241 debug_method_add (w, "recenter");
16242 #endif
16243
16244 /* Forget any previously recorded base line for line number display. */
16245 if (!buffer_unchanged_p)
16246 w->base_line_number = 0;
16247
16248 /* Determine the window start relative to point. */
16249 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16250 it.current_y = it.last_visible_y;
16251 if (centering_position < 0)
16252 {
16253 int window_total_lines
16254 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16255 int margin =
16256 scroll_margin > 0
16257 ? min (scroll_margin, window_total_lines / 4)
16258 : 0;
16259 ptrdiff_t margin_pos = CHARPOS (startp);
16260 Lisp_Object aggressive;
16261 int scrolling_up;
16262
16263 /* If there is a scroll margin at the top of the window, find
16264 its character position. */
16265 if (margin
16266 /* Cannot call start_display if startp is not in the
16267 accessible region of the buffer. This can happen when we
16268 have just switched to a different buffer and/or changed
16269 its restriction. In that case, startp is initialized to
16270 the character position 1 (BEGV) because we did not yet
16271 have chance to display the buffer even once. */
16272 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16273 {
16274 struct it it1;
16275 void *it1data = NULL;
16276
16277 SAVE_IT (it1, it, it1data);
16278 start_display (&it1, w, startp);
16279 move_it_vertically (&it1, margin * frame_line_height);
16280 margin_pos = IT_CHARPOS (it1);
16281 RESTORE_IT (&it, &it, it1data);
16282 }
16283 scrolling_up = PT > margin_pos;
16284 aggressive =
16285 scrolling_up
16286 ? BVAR (current_buffer, scroll_up_aggressively)
16287 : BVAR (current_buffer, scroll_down_aggressively);
16288
16289 if (!MINI_WINDOW_P (w)
16290 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16291 {
16292 int pt_offset = 0;
16293
16294 /* Setting scroll-conservatively overrides
16295 scroll-*-aggressively. */
16296 if (!scroll_conservatively && NUMBERP (aggressive))
16297 {
16298 double float_amount = XFLOATINT (aggressive);
16299
16300 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16301 if (pt_offset == 0 && float_amount > 0)
16302 pt_offset = 1;
16303 if (pt_offset && margin > 0)
16304 margin -= 1;
16305 }
16306 /* Compute how much to move the window start backward from
16307 point so that point will be displayed where the user
16308 wants it. */
16309 if (scrolling_up)
16310 {
16311 centering_position = it.last_visible_y;
16312 if (pt_offset)
16313 centering_position -= pt_offset;
16314 centering_position -=
16315 frame_line_height * (1 + margin + (last_line_misfit != 0))
16316 + WINDOW_HEADER_LINE_HEIGHT (w);
16317 /* Don't let point enter the scroll margin near top of
16318 the window. */
16319 if (centering_position < margin * frame_line_height)
16320 centering_position = margin * frame_line_height;
16321 }
16322 else
16323 centering_position = margin * frame_line_height + pt_offset;
16324 }
16325 else
16326 /* Set the window start half the height of the window backward
16327 from point. */
16328 centering_position = window_box_height (w) / 2;
16329 }
16330 move_it_vertically_backward (&it, centering_position);
16331
16332 eassert (IT_CHARPOS (it) >= BEGV);
16333
16334 /* The function move_it_vertically_backward may move over more
16335 than the specified y-distance. If it->w is small, e.g. a
16336 mini-buffer window, we may end up in front of the window's
16337 display area. Start displaying at the start of the line
16338 containing PT in this case. */
16339 if (it.current_y <= 0)
16340 {
16341 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16342 move_it_vertically_backward (&it, 0);
16343 it.current_y = 0;
16344 }
16345
16346 it.current_x = it.hpos = 0;
16347
16348 /* Set the window start position here explicitly, to avoid an
16349 infinite loop in case the functions in window-scroll-functions
16350 get errors. */
16351 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16352
16353 /* Run scroll hooks. */
16354 startp = run_window_scroll_functions (window, it.current.pos);
16355
16356 /* Redisplay the window. */
16357 if (!current_matrix_up_to_date_p
16358 || windows_or_buffers_changed
16359 || f->cursor_type_changed
16360 /* Don't use try_window_reusing_current_matrix in this case
16361 because it can have changed the buffer. */
16362 || !NILP (Vwindow_scroll_functions)
16363 || !just_this_one_p
16364 || MINI_WINDOW_P (w)
16365 || !(used_current_matrix_p
16366 = try_window_reusing_current_matrix (w)))
16367 try_window (window, startp, 0);
16368
16369 /* If new fonts have been loaded (due to fontsets), give up. We
16370 have to start a new redisplay since we need to re-adjust glyph
16371 matrices. */
16372 if (f->fonts_changed)
16373 goto need_larger_matrices;
16374
16375 /* If cursor did not appear assume that the middle of the window is
16376 in the first line of the window. Do it again with the next line.
16377 (Imagine a window of height 100, displaying two lines of height
16378 60. Moving back 50 from it->last_visible_y will end in the first
16379 line.) */
16380 if (w->cursor.vpos < 0)
16381 {
16382 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16383 {
16384 clear_glyph_matrix (w->desired_matrix);
16385 move_it_by_lines (&it, 1);
16386 try_window (window, it.current.pos, 0);
16387 }
16388 else if (PT < IT_CHARPOS (it))
16389 {
16390 clear_glyph_matrix (w->desired_matrix);
16391 move_it_by_lines (&it, -1);
16392 try_window (window, it.current.pos, 0);
16393 }
16394 else
16395 {
16396 /* Not much we can do about it. */
16397 }
16398 }
16399
16400 /* Consider the following case: Window starts at BEGV, there is
16401 invisible, intangible text at BEGV, so that display starts at
16402 some point START > BEGV. It can happen that we are called with
16403 PT somewhere between BEGV and START. Try to handle that case,
16404 and similar ones. */
16405 if (w->cursor.vpos < 0)
16406 {
16407 /* First, try locating the proper glyph row for PT. */
16408 struct glyph_row *row =
16409 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16410
16411 /* Sometimes point is at the beginning of invisible text that is
16412 before the 1st character displayed in the row. In that case,
16413 row_containing_pos fails to find the row, because no glyphs
16414 with appropriate buffer positions are present in the row.
16415 Therefore, we next try to find the row which shows the 1st
16416 position after the invisible text. */
16417 if (!row)
16418 {
16419 Lisp_Object val =
16420 get_char_property_and_overlay (make_number (PT), Qinvisible,
16421 Qnil, NULL);
16422
16423 if (TEXT_PROP_MEANS_INVISIBLE (val))
16424 {
16425 ptrdiff_t alt_pos;
16426 Lisp_Object invis_end =
16427 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16428 Qnil, Qnil);
16429
16430 if (NATNUMP (invis_end))
16431 alt_pos = XFASTINT (invis_end);
16432 else
16433 alt_pos = ZV;
16434 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16435 NULL, 0);
16436 }
16437 }
16438 /* Finally, fall back on the first row of the window after the
16439 header line (if any). This is slightly better than not
16440 displaying the cursor at all. */
16441 if (!row)
16442 {
16443 row = w->current_matrix->rows;
16444 if (row->mode_line_p)
16445 ++row;
16446 }
16447 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16448 }
16449
16450 if (!cursor_row_fully_visible_p (w, 0, 0))
16451 {
16452 /* If vscroll is enabled, disable it and try again. */
16453 if (w->vscroll)
16454 {
16455 w->vscroll = 0;
16456 clear_glyph_matrix (w->desired_matrix);
16457 goto recenter;
16458 }
16459
16460 /* Users who set scroll-conservatively to a large number want
16461 point just above/below the scroll margin. If we ended up
16462 with point's row partially visible, move the window start to
16463 make that row fully visible and out of the margin. */
16464 if (scroll_conservatively > SCROLL_LIMIT)
16465 {
16466 int window_total_lines
16467 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16468 int margin =
16469 scroll_margin > 0
16470 ? min (scroll_margin, window_total_lines / 4)
16471 : 0;
16472 int move_down = w->cursor.vpos >= window_total_lines / 2;
16473
16474 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16475 clear_glyph_matrix (w->desired_matrix);
16476 if (1 == try_window (window, it.current.pos,
16477 TRY_WINDOW_CHECK_MARGINS))
16478 goto done;
16479 }
16480
16481 /* If centering point failed to make the whole line visible,
16482 put point at the top instead. That has to make the whole line
16483 visible, if it can be done. */
16484 if (centering_position == 0)
16485 goto done;
16486
16487 clear_glyph_matrix (w->desired_matrix);
16488 centering_position = 0;
16489 goto recenter;
16490 }
16491
16492 done:
16493
16494 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16495 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16496 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16497
16498 /* Display the mode line, if we must. */
16499 if ((update_mode_line
16500 /* If window not full width, must redo its mode line
16501 if (a) the window to its side is being redone and
16502 (b) we do a frame-based redisplay. This is a consequence
16503 of how inverted lines are drawn in frame-based redisplay. */
16504 || (!just_this_one_p
16505 && !FRAME_WINDOW_P (f)
16506 && !WINDOW_FULL_WIDTH_P (w))
16507 /* Line number to display. */
16508 || w->base_line_pos > 0
16509 /* Column number is displayed and different from the one displayed. */
16510 || (w->column_number_displayed != -1
16511 && (w->column_number_displayed != current_column ())))
16512 /* This means that the window has a mode line. */
16513 && (WINDOW_WANTS_MODELINE_P (w)
16514 || WINDOW_WANTS_HEADER_LINE_P (w)))
16515 {
16516
16517 display_mode_lines (w);
16518
16519 /* If mode line height has changed, arrange for a thorough
16520 immediate redisplay using the correct mode line height. */
16521 if (WINDOW_WANTS_MODELINE_P (w)
16522 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16523 {
16524 f->fonts_changed = 1;
16525 w->mode_line_height = -1;
16526 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16527 = DESIRED_MODE_LINE_HEIGHT (w);
16528 }
16529
16530 /* If header line height has changed, arrange for a thorough
16531 immediate redisplay using the correct header line height. */
16532 if (WINDOW_WANTS_HEADER_LINE_P (w)
16533 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16534 {
16535 f->fonts_changed = 1;
16536 w->header_line_height = -1;
16537 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16538 = DESIRED_HEADER_LINE_HEIGHT (w);
16539 }
16540
16541 if (f->fonts_changed)
16542 goto need_larger_matrices;
16543 }
16544
16545 if (!line_number_displayed && w->base_line_pos != -1)
16546 {
16547 w->base_line_pos = 0;
16548 w->base_line_number = 0;
16549 }
16550
16551 finish_menu_bars:
16552
16553 /* When we reach a frame's selected window, redo the frame's menu bar. */
16554 if (update_mode_line
16555 && EQ (FRAME_SELECTED_WINDOW (f), window))
16556 {
16557 int redisplay_menu_p = 0;
16558
16559 if (FRAME_WINDOW_P (f))
16560 {
16561 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16562 || defined (HAVE_NS) || defined (USE_GTK)
16563 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16564 #else
16565 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16566 #endif
16567 }
16568 else
16569 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16570
16571 if (redisplay_menu_p)
16572 display_menu_bar (w);
16573
16574 #ifdef HAVE_WINDOW_SYSTEM
16575 if (FRAME_WINDOW_P (f))
16576 {
16577 #if defined (USE_GTK) || defined (HAVE_NS)
16578 if (FRAME_EXTERNAL_TOOL_BAR (f))
16579 redisplay_tool_bar (f);
16580 #else
16581 if (WINDOWP (f->tool_bar_window)
16582 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16583 || !NILP (Vauto_resize_tool_bars))
16584 && redisplay_tool_bar (f))
16585 ignore_mouse_drag_p = 1;
16586 #endif
16587 }
16588 #endif
16589 }
16590
16591 #ifdef HAVE_WINDOW_SYSTEM
16592 if (FRAME_WINDOW_P (f)
16593 && update_window_fringes (w, (just_this_one_p
16594 || (!used_current_matrix_p && !overlay_arrow_seen)
16595 || w->pseudo_window_p)))
16596 {
16597 update_begin (f);
16598 block_input ();
16599 if (draw_window_fringes (w, 1))
16600 {
16601 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16602 x_draw_right_divider (w);
16603 else
16604 x_draw_vertical_border (w);
16605 }
16606 unblock_input ();
16607 update_end (f);
16608 }
16609
16610 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16611 x_draw_bottom_divider (w);
16612 #endif /* HAVE_WINDOW_SYSTEM */
16613
16614 /* We go to this label, with fonts_changed set, if it is
16615 necessary to try again using larger glyph matrices.
16616 We have to redeem the scroll bar even in this case,
16617 because the loop in redisplay_internal expects that. */
16618 need_larger_matrices:
16619 ;
16620 finish_scroll_bars:
16621
16622 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16623 {
16624 /* Set the thumb's position and size. */
16625 set_vertical_scroll_bar (w);
16626
16627 /* Note that we actually used the scroll bar attached to this
16628 window, so it shouldn't be deleted at the end of redisplay. */
16629 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16630 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16631 }
16632
16633 /* Restore current_buffer and value of point in it. The window
16634 update may have changed the buffer, so first make sure `opoint'
16635 is still valid (Bug#6177). */
16636 if (CHARPOS (opoint) < BEGV)
16637 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16638 else if (CHARPOS (opoint) > ZV)
16639 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16640 else
16641 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16642
16643 set_buffer_internal_1 (old);
16644 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16645 shorter. This can be caused by log truncation in *Messages*. */
16646 if (CHARPOS (lpoint) <= ZV)
16647 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16648
16649 unbind_to (count, Qnil);
16650 }
16651
16652
16653 /* Build the complete desired matrix of WINDOW with a window start
16654 buffer position POS.
16655
16656 Value is 1 if successful. It is zero if fonts were loaded during
16657 redisplay which makes re-adjusting glyph matrices necessary, and -1
16658 if point would appear in the scroll margins.
16659 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16660 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16661 set in FLAGS.) */
16662
16663 int
16664 try_window (Lisp_Object window, struct text_pos pos, int flags)
16665 {
16666 struct window *w = XWINDOW (window);
16667 struct it it;
16668 struct glyph_row *last_text_row = NULL;
16669 struct frame *f = XFRAME (w->frame);
16670 int frame_line_height = default_line_pixel_height (w);
16671
16672 /* Make POS the new window start. */
16673 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16674
16675 /* Mark cursor position as unknown. No overlay arrow seen. */
16676 w->cursor.vpos = -1;
16677 overlay_arrow_seen = 0;
16678
16679 /* Initialize iterator and info to start at POS. */
16680 start_display (&it, w, pos);
16681
16682 /* Display all lines of W. */
16683 while (it.current_y < it.last_visible_y)
16684 {
16685 if (display_line (&it))
16686 last_text_row = it.glyph_row - 1;
16687 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16688 return 0;
16689 }
16690
16691 /* Don't let the cursor end in the scroll margins. */
16692 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16693 && !MINI_WINDOW_P (w))
16694 {
16695 int this_scroll_margin;
16696 int window_total_lines
16697 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16698
16699 if (scroll_margin > 0)
16700 {
16701 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16702 this_scroll_margin *= frame_line_height;
16703 }
16704 else
16705 this_scroll_margin = 0;
16706
16707 if ((w->cursor.y >= 0 /* not vscrolled */
16708 && w->cursor.y < this_scroll_margin
16709 && CHARPOS (pos) > BEGV
16710 && IT_CHARPOS (it) < ZV)
16711 /* rms: considering make_cursor_line_fully_visible_p here
16712 seems to give wrong results. We don't want to recenter
16713 when the last line is partly visible, we want to allow
16714 that case to be handled in the usual way. */
16715 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16716 {
16717 w->cursor.vpos = -1;
16718 clear_glyph_matrix (w->desired_matrix);
16719 return -1;
16720 }
16721 }
16722
16723 /* If bottom moved off end of frame, change mode line percentage. */
16724 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16725 w->update_mode_line = 1;
16726
16727 /* Set window_end_pos to the offset of the last character displayed
16728 on the window from the end of current_buffer. Set
16729 window_end_vpos to its row number. */
16730 if (last_text_row)
16731 {
16732 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16733 adjust_window_ends (w, last_text_row, 0);
16734 eassert
16735 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16736 w->window_end_vpos)));
16737 }
16738 else
16739 {
16740 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16741 w->window_end_pos = Z - ZV;
16742 w->window_end_vpos = 0;
16743 }
16744
16745 /* But that is not valid info until redisplay finishes. */
16746 w->window_end_valid = 0;
16747 return 1;
16748 }
16749
16750
16751 \f
16752 /************************************************************************
16753 Window redisplay reusing current matrix when buffer has not changed
16754 ************************************************************************/
16755
16756 /* Try redisplay of window W showing an unchanged buffer with a
16757 different window start than the last time it was displayed by
16758 reusing its current matrix. Value is non-zero if successful.
16759 W->start is the new window start. */
16760
16761 static int
16762 try_window_reusing_current_matrix (struct window *w)
16763 {
16764 struct frame *f = XFRAME (w->frame);
16765 struct glyph_row *bottom_row;
16766 struct it it;
16767 struct run run;
16768 struct text_pos start, new_start;
16769 int nrows_scrolled, i;
16770 struct glyph_row *last_text_row;
16771 struct glyph_row *last_reused_text_row;
16772 struct glyph_row *start_row;
16773 int start_vpos, min_y, max_y;
16774
16775 #ifdef GLYPH_DEBUG
16776 if (inhibit_try_window_reusing)
16777 return 0;
16778 #endif
16779
16780 if (/* This function doesn't handle terminal frames. */
16781 !FRAME_WINDOW_P (f)
16782 /* Don't try to reuse the display if windows have been split
16783 or such. */
16784 || windows_or_buffers_changed
16785 || f->cursor_type_changed)
16786 return 0;
16787
16788 /* Can't do this if showing trailing whitespace. */
16789 if (!NILP (Vshow_trailing_whitespace))
16790 return 0;
16791
16792 /* If top-line visibility has changed, give up. */
16793 if (WINDOW_WANTS_HEADER_LINE_P (w)
16794 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16795 return 0;
16796
16797 /* Give up if old or new display is scrolled vertically. We could
16798 make this function handle this, but right now it doesn't. */
16799 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16800 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16801 return 0;
16802
16803 /* The variable new_start now holds the new window start. The old
16804 start `start' can be determined from the current matrix. */
16805 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16806 start = start_row->minpos;
16807 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16808
16809 /* Clear the desired matrix for the display below. */
16810 clear_glyph_matrix (w->desired_matrix);
16811
16812 if (CHARPOS (new_start) <= CHARPOS (start))
16813 {
16814 /* Don't use this method if the display starts with an ellipsis
16815 displayed for invisible text. It's not easy to handle that case
16816 below, and it's certainly not worth the effort since this is
16817 not a frequent case. */
16818 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16819 return 0;
16820
16821 IF_DEBUG (debug_method_add (w, "twu1"));
16822
16823 /* Display up to a row that can be reused. The variable
16824 last_text_row is set to the last row displayed that displays
16825 text. Note that it.vpos == 0 if or if not there is a
16826 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16827 start_display (&it, w, new_start);
16828 w->cursor.vpos = -1;
16829 last_text_row = last_reused_text_row = NULL;
16830
16831 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16832 {
16833 /* If we have reached into the characters in the START row,
16834 that means the line boundaries have changed. So we
16835 can't start copying with the row START. Maybe it will
16836 work to start copying with the following row. */
16837 while (IT_CHARPOS (it) > CHARPOS (start))
16838 {
16839 /* Advance to the next row as the "start". */
16840 start_row++;
16841 start = start_row->minpos;
16842 /* If there are no more rows to try, or just one, give up. */
16843 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16844 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16845 || CHARPOS (start) == ZV)
16846 {
16847 clear_glyph_matrix (w->desired_matrix);
16848 return 0;
16849 }
16850
16851 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16852 }
16853 /* If we have reached alignment, we can copy the rest of the
16854 rows. */
16855 if (IT_CHARPOS (it) == CHARPOS (start)
16856 /* Don't accept "alignment" inside a display vector,
16857 since start_row could have started in the middle of
16858 that same display vector (thus their character
16859 positions match), and we have no way of telling if
16860 that is the case. */
16861 && it.current.dpvec_index < 0)
16862 break;
16863
16864 if (display_line (&it))
16865 last_text_row = it.glyph_row - 1;
16866
16867 }
16868
16869 /* A value of current_y < last_visible_y means that we stopped
16870 at the previous window start, which in turn means that we
16871 have at least one reusable row. */
16872 if (it.current_y < it.last_visible_y)
16873 {
16874 struct glyph_row *row;
16875
16876 /* IT.vpos always starts from 0; it counts text lines. */
16877 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16878
16879 /* Find PT if not already found in the lines displayed. */
16880 if (w->cursor.vpos < 0)
16881 {
16882 int dy = it.current_y - start_row->y;
16883
16884 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16885 row = row_containing_pos (w, PT, row, NULL, dy);
16886 if (row)
16887 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16888 dy, nrows_scrolled);
16889 else
16890 {
16891 clear_glyph_matrix (w->desired_matrix);
16892 return 0;
16893 }
16894 }
16895
16896 /* Scroll the display. Do it before the current matrix is
16897 changed. The problem here is that update has not yet
16898 run, i.e. part of the current matrix is not up to date.
16899 scroll_run_hook will clear the cursor, and use the
16900 current matrix to get the height of the row the cursor is
16901 in. */
16902 run.current_y = start_row->y;
16903 run.desired_y = it.current_y;
16904 run.height = it.last_visible_y - it.current_y;
16905
16906 if (run.height > 0 && run.current_y != run.desired_y)
16907 {
16908 update_begin (f);
16909 FRAME_RIF (f)->update_window_begin_hook (w);
16910 FRAME_RIF (f)->clear_window_mouse_face (w);
16911 FRAME_RIF (f)->scroll_run_hook (w, &run);
16912 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16913 update_end (f);
16914 }
16915
16916 /* Shift current matrix down by nrows_scrolled lines. */
16917 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16918 rotate_matrix (w->current_matrix,
16919 start_vpos,
16920 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16921 nrows_scrolled);
16922
16923 /* Disable lines that must be updated. */
16924 for (i = 0; i < nrows_scrolled; ++i)
16925 (start_row + i)->enabled_p = false;
16926
16927 /* Re-compute Y positions. */
16928 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16929 max_y = it.last_visible_y;
16930 for (row = start_row + nrows_scrolled;
16931 row < bottom_row;
16932 ++row)
16933 {
16934 row->y = it.current_y;
16935 row->visible_height = row->height;
16936
16937 if (row->y < min_y)
16938 row->visible_height -= min_y - row->y;
16939 if (row->y + row->height > max_y)
16940 row->visible_height -= row->y + row->height - max_y;
16941 if (row->fringe_bitmap_periodic_p)
16942 row->redraw_fringe_bitmaps_p = 1;
16943
16944 it.current_y += row->height;
16945
16946 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16947 last_reused_text_row = row;
16948 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16949 break;
16950 }
16951
16952 /* Disable lines in the current matrix which are now
16953 below the window. */
16954 for (++row; row < bottom_row; ++row)
16955 row->enabled_p = row->mode_line_p = 0;
16956 }
16957
16958 /* Update window_end_pos etc.; last_reused_text_row is the last
16959 reused row from the current matrix containing text, if any.
16960 The value of last_text_row is the last displayed line
16961 containing text. */
16962 if (last_reused_text_row)
16963 adjust_window_ends (w, last_reused_text_row, 1);
16964 else if (last_text_row)
16965 adjust_window_ends (w, last_text_row, 0);
16966 else
16967 {
16968 /* This window must be completely empty. */
16969 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16970 w->window_end_pos = Z - ZV;
16971 w->window_end_vpos = 0;
16972 }
16973 w->window_end_valid = 0;
16974
16975 /* Update hint: don't try scrolling again in update_window. */
16976 w->desired_matrix->no_scrolling_p = 1;
16977
16978 #ifdef GLYPH_DEBUG
16979 debug_method_add (w, "try_window_reusing_current_matrix 1");
16980 #endif
16981 return 1;
16982 }
16983 else if (CHARPOS (new_start) > CHARPOS (start))
16984 {
16985 struct glyph_row *pt_row, *row;
16986 struct glyph_row *first_reusable_row;
16987 struct glyph_row *first_row_to_display;
16988 int dy;
16989 int yb = window_text_bottom_y (w);
16990
16991 /* Find the row starting at new_start, if there is one. Don't
16992 reuse a partially visible line at the end. */
16993 first_reusable_row = start_row;
16994 while (first_reusable_row->enabled_p
16995 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16996 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16997 < CHARPOS (new_start)))
16998 ++first_reusable_row;
16999
17000 /* Give up if there is no row to reuse. */
17001 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17002 || !first_reusable_row->enabled_p
17003 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17004 != CHARPOS (new_start)))
17005 return 0;
17006
17007 /* We can reuse fully visible rows beginning with
17008 first_reusable_row to the end of the window. Set
17009 first_row_to_display to the first row that cannot be reused.
17010 Set pt_row to the row containing point, if there is any. */
17011 pt_row = NULL;
17012 for (first_row_to_display = first_reusable_row;
17013 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17014 ++first_row_to_display)
17015 {
17016 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17017 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17018 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17019 && first_row_to_display->ends_at_zv_p
17020 && pt_row == NULL)))
17021 pt_row = first_row_to_display;
17022 }
17023
17024 /* Start displaying at the start of first_row_to_display. */
17025 eassert (first_row_to_display->y < yb);
17026 init_to_row_start (&it, w, first_row_to_display);
17027
17028 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17029 - start_vpos);
17030 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17031 - nrows_scrolled);
17032 it.current_y = (first_row_to_display->y - first_reusable_row->y
17033 + WINDOW_HEADER_LINE_HEIGHT (w));
17034
17035 /* Display lines beginning with first_row_to_display in the
17036 desired matrix. Set last_text_row to the last row displayed
17037 that displays text. */
17038 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17039 if (pt_row == NULL)
17040 w->cursor.vpos = -1;
17041 last_text_row = NULL;
17042 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17043 if (display_line (&it))
17044 last_text_row = it.glyph_row - 1;
17045
17046 /* If point is in a reused row, adjust y and vpos of the cursor
17047 position. */
17048 if (pt_row)
17049 {
17050 w->cursor.vpos -= nrows_scrolled;
17051 w->cursor.y -= first_reusable_row->y - start_row->y;
17052 }
17053
17054 /* Give up if point isn't in a row displayed or reused. (This
17055 also handles the case where w->cursor.vpos < nrows_scrolled
17056 after the calls to display_line, which can happen with scroll
17057 margins. See bug#1295.) */
17058 if (w->cursor.vpos < 0)
17059 {
17060 clear_glyph_matrix (w->desired_matrix);
17061 return 0;
17062 }
17063
17064 /* Scroll the display. */
17065 run.current_y = first_reusable_row->y;
17066 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17067 run.height = it.last_visible_y - run.current_y;
17068 dy = run.current_y - run.desired_y;
17069
17070 if (run.height)
17071 {
17072 update_begin (f);
17073 FRAME_RIF (f)->update_window_begin_hook (w);
17074 FRAME_RIF (f)->clear_window_mouse_face (w);
17075 FRAME_RIF (f)->scroll_run_hook (w, &run);
17076 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17077 update_end (f);
17078 }
17079
17080 /* Adjust Y positions of reused rows. */
17081 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17082 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17083 max_y = it.last_visible_y;
17084 for (row = first_reusable_row; row < first_row_to_display; ++row)
17085 {
17086 row->y -= dy;
17087 row->visible_height = row->height;
17088 if (row->y < min_y)
17089 row->visible_height -= min_y - row->y;
17090 if (row->y + row->height > max_y)
17091 row->visible_height -= row->y + row->height - max_y;
17092 if (row->fringe_bitmap_periodic_p)
17093 row->redraw_fringe_bitmaps_p = 1;
17094 }
17095
17096 /* Scroll the current matrix. */
17097 eassert (nrows_scrolled > 0);
17098 rotate_matrix (w->current_matrix,
17099 start_vpos,
17100 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17101 -nrows_scrolled);
17102
17103 /* Disable rows not reused. */
17104 for (row -= nrows_scrolled; row < bottom_row; ++row)
17105 row->enabled_p = false;
17106
17107 /* Point may have moved to a different line, so we cannot assume that
17108 the previous cursor position is valid; locate the correct row. */
17109 if (pt_row)
17110 {
17111 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17112 row < bottom_row
17113 && PT >= MATRIX_ROW_END_CHARPOS (row)
17114 && !row->ends_at_zv_p;
17115 row++)
17116 {
17117 w->cursor.vpos++;
17118 w->cursor.y = row->y;
17119 }
17120 if (row < bottom_row)
17121 {
17122 /* Can't simply scan the row for point with
17123 bidi-reordered glyph rows. Let set_cursor_from_row
17124 figure out where to put the cursor, and if it fails,
17125 give up. */
17126 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17127 {
17128 if (!set_cursor_from_row (w, row, w->current_matrix,
17129 0, 0, 0, 0))
17130 {
17131 clear_glyph_matrix (w->desired_matrix);
17132 return 0;
17133 }
17134 }
17135 else
17136 {
17137 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17138 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17139
17140 for (; glyph < end
17141 && (!BUFFERP (glyph->object)
17142 || glyph->charpos < PT);
17143 glyph++)
17144 {
17145 w->cursor.hpos++;
17146 w->cursor.x += glyph->pixel_width;
17147 }
17148 }
17149 }
17150 }
17151
17152 /* Adjust window end. A null value of last_text_row means that
17153 the window end is in reused rows which in turn means that
17154 only its vpos can have changed. */
17155 if (last_text_row)
17156 adjust_window_ends (w, last_text_row, 0);
17157 else
17158 w->window_end_vpos -= nrows_scrolled;
17159
17160 w->window_end_valid = 0;
17161 w->desired_matrix->no_scrolling_p = 1;
17162
17163 #ifdef GLYPH_DEBUG
17164 debug_method_add (w, "try_window_reusing_current_matrix 2");
17165 #endif
17166 return 1;
17167 }
17168
17169 return 0;
17170 }
17171
17172
17173 \f
17174 /************************************************************************
17175 Window redisplay reusing current matrix when buffer has changed
17176 ************************************************************************/
17177
17178 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17179 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17180 ptrdiff_t *, ptrdiff_t *);
17181 static struct glyph_row *
17182 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17183 struct glyph_row *);
17184
17185
17186 /* Return the last row in MATRIX displaying text. If row START is
17187 non-null, start searching with that row. IT gives the dimensions
17188 of the display. Value is null if matrix is empty; otherwise it is
17189 a pointer to the row found. */
17190
17191 static struct glyph_row *
17192 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17193 struct glyph_row *start)
17194 {
17195 struct glyph_row *row, *row_found;
17196
17197 /* Set row_found to the last row in IT->w's current matrix
17198 displaying text. The loop looks funny but think of partially
17199 visible lines. */
17200 row_found = NULL;
17201 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17202 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17203 {
17204 eassert (row->enabled_p);
17205 row_found = row;
17206 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17207 break;
17208 ++row;
17209 }
17210
17211 return row_found;
17212 }
17213
17214
17215 /* Return the last row in the current matrix of W that is not affected
17216 by changes at the start of current_buffer that occurred since W's
17217 current matrix was built. Value is null if no such row exists.
17218
17219 BEG_UNCHANGED us the number of characters unchanged at the start of
17220 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17221 first changed character in current_buffer. Characters at positions <
17222 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17223 when the current matrix was built. */
17224
17225 static struct glyph_row *
17226 find_last_unchanged_at_beg_row (struct window *w)
17227 {
17228 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17229 struct glyph_row *row;
17230 struct glyph_row *row_found = NULL;
17231 int yb = window_text_bottom_y (w);
17232
17233 /* Find the last row displaying unchanged text. */
17234 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17235 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17236 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17237 ++row)
17238 {
17239 if (/* If row ends before first_changed_pos, it is unchanged,
17240 except in some case. */
17241 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17242 /* When row ends in ZV and we write at ZV it is not
17243 unchanged. */
17244 && !row->ends_at_zv_p
17245 /* When first_changed_pos is the end of a continued line,
17246 row is not unchanged because it may be no longer
17247 continued. */
17248 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17249 && (row->continued_p
17250 || row->exact_window_width_line_p))
17251 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17252 needs to be recomputed, so don't consider this row as
17253 unchanged. This happens when the last line was
17254 bidi-reordered and was killed immediately before this
17255 redisplay cycle. In that case, ROW->end stores the
17256 buffer position of the first visual-order character of
17257 the killed text, which is now beyond ZV. */
17258 && CHARPOS (row->end.pos) <= ZV)
17259 row_found = row;
17260
17261 /* Stop if last visible row. */
17262 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17263 break;
17264 }
17265
17266 return row_found;
17267 }
17268
17269
17270 /* Find the first glyph row in the current matrix of W that is not
17271 affected by changes at the end of current_buffer since the
17272 time W's current matrix was built.
17273
17274 Return in *DELTA the number of chars by which buffer positions in
17275 unchanged text at the end of current_buffer must be adjusted.
17276
17277 Return in *DELTA_BYTES the corresponding number of bytes.
17278
17279 Value is null if no such row exists, i.e. all rows are affected by
17280 changes. */
17281
17282 static struct glyph_row *
17283 find_first_unchanged_at_end_row (struct window *w,
17284 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17285 {
17286 struct glyph_row *row;
17287 struct glyph_row *row_found = NULL;
17288
17289 *delta = *delta_bytes = 0;
17290
17291 /* Display must not have been paused, otherwise the current matrix
17292 is not up to date. */
17293 eassert (w->window_end_valid);
17294
17295 /* A value of window_end_pos >= END_UNCHANGED means that the window
17296 end is in the range of changed text. If so, there is no
17297 unchanged row at the end of W's current matrix. */
17298 if (w->window_end_pos >= END_UNCHANGED)
17299 return NULL;
17300
17301 /* Set row to the last row in W's current matrix displaying text. */
17302 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17303
17304 /* If matrix is entirely empty, no unchanged row exists. */
17305 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17306 {
17307 /* The value of row is the last glyph row in the matrix having a
17308 meaningful buffer position in it. The end position of row
17309 corresponds to window_end_pos. This allows us to translate
17310 buffer positions in the current matrix to current buffer
17311 positions for characters not in changed text. */
17312 ptrdiff_t Z_old =
17313 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17314 ptrdiff_t Z_BYTE_old =
17315 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17316 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17317 struct glyph_row *first_text_row
17318 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17319
17320 *delta = Z - Z_old;
17321 *delta_bytes = Z_BYTE - Z_BYTE_old;
17322
17323 /* Set last_unchanged_pos to the buffer position of the last
17324 character in the buffer that has not been changed. Z is the
17325 index + 1 of the last character in current_buffer, i.e. by
17326 subtracting END_UNCHANGED we get the index of the last
17327 unchanged character, and we have to add BEG to get its buffer
17328 position. */
17329 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17330 last_unchanged_pos_old = last_unchanged_pos - *delta;
17331
17332 /* Search backward from ROW for a row displaying a line that
17333 starts at a minimum position >= last_unchanged_pos_old. */
17334 for (; row > first_text_row; --row)
17335 {
17336 /* This used to abort, but it can happen.
17337 It is ok to just stop the search instead here. KFS. */
17338 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17339 break;
17340
17341 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17342 row_found = row;
17343 }
17344 }
17345
17346 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17347
17348 return row_found;
17349 }
17350
17351
17352 /* Make sure that glyph rows in the current matrix of window W
17353 reference the same glyph memory as corresponding rows in the
17354 frame's frame matrix. This function is called after scrolling W's
17355 current matrix on a terminal frame in try_window_id and
17356 try_window_reusing_current_matrix. */
17357
17358 static void
17359 sync_frame_with_window_matrix_rows (struct window *w)
17360 {
17361 struct frame *f = XFRAME (w->frame);
17362 struct glyph_row *window_row, *window_row_end, *frame_row;
17363
17364 /* Preconditions: W must be a leaf window and full-width. Its frame
17365 must have a frame matrix. */
17366 eassert (BUFFERP (w->contents));
17367 eassert (WINDOW_FULL_WIDTH_P (w));
17368 eassert (!FRAME_WINDOW_P (f));
17369
17370 /* If W is a full-width window, glyph pointers in W's current matrix
17371 have, by definition, to be the same as glyph pointers in the
17372 corresponding frame matrix. Note that frame matrices have no
17373 marginal areas (see build_frame_matrix). */
17374 window_row = w->current_matrix->rows;
17375 window_row_end = window_row + w->current_matrix->nrows;
17376 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17377 while (window_row < window_row_end)
17378 {
17379 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17380 struct glyph *end = window_row->glyphs[LAST_AREA];
17381
17382 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17383 frame_row->glyphs[TEXT_AREA] = start;
17384 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17385 frame_row->glyphs[LAST_AREA] = end;
17386
17387 /* Disable frame rows whose corresponding window rows have
17388 been disabled in try_window_id. */
17389 if (!window_row->enabled_p)
17390 frame_row->enabled_p = false;
17391
17392 ++window_row, ++frame_row;
17393 }
17394 }
17395
17396
17397 /* Find the glyph row in window W containing CHARPOS. Consider all
17398 rows between START and END (not inclusive). END null means search
17399 all rows to the end of the display area of W. Value is the row
17400 containing CHARPOS or null. */
17401
17402 struct glyph_row *
17403 row_containing_pos (struct window *w, ptrdiff_t charpos,
17404 struct glyph_row *start, struct glyph_row *end, int dy)
17405 {
17406 struct glyph_row *row = start;
17407 struct glyph_row *best_row = NULL;
17408 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17409 int last_y;
17410
17411 /* If we happen to start on a header-line, skip that. */
17412 if (row->mode_line_p)
17413 ++row;
17414
17415 if ((end && row >= end) || !row->enabled_p)
17416 return NULL;
17417
17418 last_y = window_text_bottom_y (w) - dy;
17419
17420 while (1)
17421 {
17422 /* Give up if we have gone too far. */
17423 if (end && row >= end)
17424 return NULL;
17425 /* This formerly returned if they were equal.
17426 I think that both quantities are of a "last plus one" type;
17427 if so, when they are equal, the row is within the screen. -- rms. */
17428 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17429 return NULL;
17430
17431 /* If it is in this row, return this row. */
17432 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17433 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17434 /* The end position of a row equals the start
17435 position of the next row. If CHARPOS is there, we
17436 would rather consider it displayed in the next
17437 line, except when this line ends in ZV. */
17438 && !row_for_charpos_p (row, charpos)))
17439 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17440 {
17441 struct glyph *g;
17442
17443 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17444 || (!best_row && !row->continued_p))
17445 return row;
17446 /* In bidi-reordered rows, there could be several rows whose
17447 edges surround CHARPOS, all of these rows belonging to
17448 the same continued line. We need to find the row which
17449 fits CHARPOS the best. */
17450 for (g = row->glyphs[TEXT_AREA];
17451 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17452 g++)
17453 {
17454 if (!STRINGP (g->object))
17455 {
17456 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17457 {
17458 mindif = eabs (g->charpos - charpos);
17459 best_row = row;
17460 /* Exact match always wins. */
17461 if (mindif == 0)
17462 return best_row;
17463 }
17464 }
17465 }
17466 }
17467 else if (best_row && !row->continued_p)
17468 return best_row;
17469 ++row;
17470 }
17471 }
17472
17473
17474 /* Try to redisplay window W by reusing its existing display. W's
17475 current matrix must be up to date when this function is called,
17476 i.e. window_end_valid must be nonzero.
17477
17478 Value is
17479
17480 >= 1 if successful, i.e. display has been updated
17481 specifically:
17482 1 means the changes were in front of a newline that precedes
17483 the window start, and the whole current matrix was reused
17484 2 means the changes were after the last position displayed
17485 in the window, and the whole current matrix was reused
17486 3 means portions of the current matrix were reused, while
17487 some of the screen lines were redrawn
17488 -1 if redisplay with same window start is known not to succeed
17489 0 if otherwise unsuccessful
17490
17491 The following steps are performed:
17492
17493 1. Find the last row in the current matrix of W that is not
17494 affected by changes at the start of current_buffer. If no such row
17495 is found, give up.
17496
17497 2. Find the first row in W's current matrix that is not affected by
17498 changes at the end of current_buffer. Maybe there is no such row.
17499
17500 3. Display lines beginning with the row + 1 found in step 1 to the
17501 row found in step 2 or, if step 2 didn't find a row, to the end of
17502 the window.
17503
17504 4. If cursor is not known to appear on the window, give up.
17505
17506 5. If display stopped at the row found in step 2, scroll the
17507 display and current matrix as needed.
17508
17509 6. Maybe display some lines at the end of W, if we must. This can
17510 happen under various circumstances, like a partially visible line
17511 becoming fully visible, or because newly displayed lines are displayed
17512 in smaller font sizes.
17513
17514 7. Update W's window end information. */
17515
17516 static int
17517 try_window_id (struct window *w)
17518 {
17519 struct frame *f = XFRAME (w->frame);
17520 struct glyph_matrix *current_matrix = w->current_matrix;
17521 struct glyph_matrix *desired_matrix = w->desired_matrix;
17522 struct glyph_row *last_unchanged_at_beg_row;
17523 struct glyph_row *first_unchanged_at_end_row;
17524 struct glyph_row *row;
17525 struct glyph_row *bottom_row;
17526 int bottom_vpos;
17527 struct it it;
17528 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17529 int dvpos, dy;
17530 struct text_pos start_pos;
17531 struct run run;
17532 int first_unchanged_at_end_vpos = 0;
17533 struct glyph_row *last_text_row, *last_text_row_at_end;
17534 struct text_pos start;
17535 ptrdiff_t first_changed_charpos, last_changed_charpos;
17536
17537 #ifdef GLYPH_DEBUG
17538 if (inhibit_try_window_id)
17539 return 0;
17540 #endif
17541
17542 /* This is handy for debugging. */
17543 #if 0
17544 #define GIVE_UP(X) \
17545 do { \
17546 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17547 return 0; \
17548 } while (0)
17549 #else
17550 #define GIVE_UP(X) return 0
17551 #endif
17552
17553 SET_TEXT_POS_FROM_MARKER (start, w->start);
17554
17555 /* Don't use this for mini-windows because these can show
17556 messages and mini-buffers, and we don't handle that here. */
17557 if (MINI_WINDOW_P (w))
17558 GIVE_UP (1);
17559
17560 /* This flag is used to prevent redisplay optimizations. */
17561 if (windows_or_buffers_changed || f->cursor_type_changed)
17562 GIVE_UP (2);
17563
17564 /* This function's optimizations cannot be used if overlays have
17565 changed in the buffer displayed by the window, so give up if they
17566 have. */
17567 if (w->last_overlay_modified != OVERLAY_MODIFF)
17568 GIVE_UP (21);
17569
17570 /* Verify that narrowing has not changed.
17571 Also verify that we were not told to prevent redisplay optimizations.
17572 It would be nice to further
17573 reduce the number of cases where this prevents try_window_id. */
17574 if (current_buffer->clip_changed
17575 || current_buffer->prevent_redisplay_optimizations_p)
17576 GIVE_UP (3);
17577
17578 /* Window must either use window-based redisplay or be full width. */
17579 if (!FRAME_WINDOW_P (f)
17580 && (!FRAME_LINE_INS_DEL_OK (f)
17581 || !WINDOW_FULL_WIDTH_P (w)))
17582 GIVE_UP (4);
17583
17584 /* Give up if point is known NOT to appear in W. */
17585 if (PT < CHARPOS (start))
17586 GIVE_UP (5);
17587
17588 /* Another way to prevent redisplay optimizations. */
17589 if (w->last_modified == 0)
17590 GIVE_UP (6);
17591
17592 /* Verify that window is not hscrolled. */
17593 if (w->hscroll != 0)
17594 GIVE_UP (7);
17595
17596 /* Verify that display wasn't paused. */
17597 if (!w->window_end_valid)
17598 GIVE_UP (8);
17599
17600 /* Likewise if highlighting trailing whitespace. */
17601 if (!NILP (Vshow_trailing_whitespace))
17602 GIVE_UP (11);
17603
17604 /* Can't use this if overlay arrow position and/or string have
17605 changed. */
17606 if (overlay_arrows_changed_p ())
17607 GIVE_UP (12);
17608
17609 /* When word-wrap is on, adding a space to the first word of a
17610 wrapped line can change the wrap position, altering the line
17611 above it. It might be worthwhile to handle this more
17612 intelligently, but for now just redisplay from scratch. */
17613 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17614 GIVE_UP (21);
17615
17616 /* Under bidi reordering, adding or deleting a character in the
17617 beginning of a paragraph, before the first strong directional
17618 character, can change the base direction of the paragraph (unless
17619 the buffer specifies a fixed paragraph direction), which will
17620 require to redisplay the whole paragraph. It might be worthwhile
17621 to find the paragraph limits and widen the range of redisplayed
17622 lines to that, but for now just give up this optimization and
17623 redisplay from scratch. */
17624 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17625 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17626 GIVE_UP (22);
17627
17628 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17629 only if buffer has really changed. The reason is that the gap is
17630 initially at Z for freshly visited files. The code below would
17631 set end_unchanged to 0 in that case. */
17632 if (MODIFF > SAVE_MODIFF
17633 /* This seems to happen sometimes after saving a buffer. */
17634 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17635 {
17636 if (GPT - BEG < BEG_UNCHANGED)
17637 BEG_UNCHANGED = GPT - BEG;
17638 if (Z - GPT < END_UNCHANGED)
17639 END_UNCHANGED = Z - GPT;
17640 }
17641
17642 /* The position of the first and last character that has been changed. */
17643 first_changed_charpos = BEG + BEG_UNCHANGED;
17644 last_changed_charpos = Z - END_UNCHANGED;
17645
17646 /* If window starts after a line end, and the last change is in
17647 front of that newline, then changes don't affect the display.
17648 This case happens with stealth-fontification. Note that although
17649 the display is unchanged, glyph positions in the matrix have to
17650 be adjusted, of course. */
17651 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17652 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17653 && ((last_changed_charpos < CHARPOS (start)
17654 && CHARPOS (start) == BEGV)
17655 || (last_changed_charpos < CHARPOS (start) - 1
17656 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17657 {
17658 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17659 struct glyph_row *r0;
17660
17661 /* Compute how many chars/bytes have been added to or removed
17662 from the buffer. */
17663 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17664 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17665 Z_delta = Z - Z_old;
17666 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17667
17668 /* Give up if PT is not in the window. Note that it already has
17669 been checked at the start of try_window_id that PT is not in
17670 front of the window start. */
17671 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17672 GIVE_UP (13);
17673
17674 /* If window start is unchanged, we can reuse the whole matrix
17675 as is, after adjusting glyph positions. No need to compute
17676 the window end again, since its offset from Z hasn't changed. */
17677 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17678 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17679 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17680 /* PT must not be in a partially visible line. */
17681 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17682 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17683 {
17684 /* Adjust positions in the glyph matrix. */
17685 if (Z_delta || Z_delta_bytes)
17686 {
17687 struct glyph_row *r1
17688 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17689 increment_matrix_positions (w->current_matrix,
17690 MATRIX_ROW_VPOS (r0, current_matrix),
17691 MATRIX_ROW_VPOS (r1, current_matrix),
17692 Z_delta, Z_delta_bytes);
17693 }
17694
17695 /* Set the cursor. */
17696 row = row_containing_pos (w, PT, r0, NULL, 0);
17697 if (row)
17698 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17699 return 1;
17700 }
17701 }
17702
17703 /* Handle the case that changes are all below what is displayed in
17704 the window, and that PT is in the window. This shortcut cannot
17705 be taken if ZV is visible in the window, and text has been added
17706 there that is visible in the window. */
17707 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17708 /* ZV is not visible in the window, or there are no
17709 changes at ZV, actually. */
17710 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17711 || first_changed_charpos == last_changed_charpos))
17712 {
17713 struct glyph_row *r0;
17714
17715 /* Give up if PT is not in the window. Note that it already has
17716 been checked at the start of try_window_id that PT is not in
17717 front of the window start. */
17718 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17719 GIVE_UP (14);
17720
17721 /* If window start is unchanged, we can reuse the whole matrix
17722 as is, without changing glyph positions since no text has
17723 been added/removed in front of the window end. */
17724 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17725 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17726 /* PT must not be in a partially visible line. */
17727 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17728 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17729 {
17730 /* We have to compute the window end anew since text
17731 could have been added/removed after it. */
17732 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17733 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17734
17735 /* Set the cursor. */
17736 row = row_containing_pos (w, PT, r0, NULL, 0);
17737 if (row)
17738 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17739 return 2;
17740 }
17741 }
17742
17743 /* Give up if window start is in the changed area.
17744
17745 The condition used to read
17746
17747 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17748
17749 but why that was tested escapes me at the moment. */
17750 if (CHARPOS (start) >= first_changed_charpos
17751 && CHARPOS (start) <= last_changed_charpos)
17752 GIVE_UP (15);
17753
17754 /* Check that window start agrees with the start of the first glyph
17755 row in its current matrix. Check this after we know the window
17756 start is not in changed text, otherwise positions would not be
17757 comparable. */
17758 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17759 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17760 GIVE_UP (16);
17761
17762 /* Give up if the window ends in strings. Overlay strings
17763 at the end are difficult to handle, so don't try. */
17764 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17765 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17766 GIVE_UP (20);
17767
17768 /* Compute the position at which we have to start displaying new
17769 lines. Some of the lines at the top of the window might be
17770 reusable because they are not displaying changed text. Find the
17771 last row in W's current matrix not affected by changes at the
17772 start of current_buffer. Value is null if changes start in the
17773 first line of window. */
17774 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17775 if (last_unchanged_at_beg_row)
17776 {
17777 /* Avoid starting to display in the middle of a character, a TAB
17778 for instance. This is easier than to set up the iterator
17779 exactly, and it's not a frequent case, so the additional
17780 effort wouldn't really pay off. */
17781 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17782 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17783 && last_unchanged_at_beg_row > w->current_matrix->rows)
17784 --last_unchanged_at_beg_row;
17785
17786 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17787 GIVE_UP (17);
17788
17789 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17790 GIVE_UP (18);
17791 start_pos = it.current.pos;
17792
17793 /* Start displaying new lines in the desired matrix at the same
17794 vpos we would use in the current matrix, i.e. below
17795 last_unchanged_at_beg_row. */
17796 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17797 current_matrix);
17798 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17799 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17800
17801 eassert (it.hpos == 0 && it.current_x == 0);
17802 }
17803 else
17804 {
17805 /* There are no reusable lines at the start of the window.
17806 Start displaying in the first text line. */
17807 start_display (&it, w, start);
17808 it.vpos = it.first_vpos;
17809 start_pos = it.current.pos;
17810 }
17811
17812 /* Find the first row that is not affected by changes at the end of
17813 the buffer. Value will be null if there is no unchanged row, in
17814 which case we must redisplay to the end of the window. delta
17815 will be set to the value by which buffer positions beginning with
17816 first_unchanged_at_end_row have to be adjusted due to text
17817 changes. */
17818 first_unchanged_at_end_row
17819 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17820 IF_DEBUG (debug_delta = delta);
17821 IF_DEBUG (debug_delta_bytes = delta_bytes);
17822
17823 /* Set stop_pos to the buffer position up to which we will have to
17824 display new lines. If first_unchanged_at_end_row != NULL, this
17825 is the buffer position of the start of the line displayed in that
17826 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17827 that we don't stop at a buffer position. */
17828 stop_pos = 0;
17829 if (first_unchanged_at_end_row)
17830 {
17831 eassert (last_unchanged_at_beg_row == NULL
17832 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17833
17834 /* If this is a continuation line, move forward to the next one
17835 that isn't. Changes in lines above affect this line.
17836 Caution: this may move first_unchanged_at_end_row to a row
17837 not displaying text. */
17838 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17839 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17840 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17841 < it.last_visible_y))
17842 ++first_unchanged_at_end_row;
17843
17844 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17845 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17846 >= it.last_visible_y))
17847 first_unchanged_at_end_row = NULL;
17848 else
17849 {
17850 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17851 + delta);
17852 first_unchanged_at_end_vpos
17853 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17854 eassert (stop_pos >= Z - END_UNCHANGED);
17855 }
17856 }
17857 else if (last_unchanged_at_beg_row == NULL)
17858 GIVE_UP (19);
17859
17860
17861 #ifdef GLYPH_DEBUG
17862
17863 /* Either there is no unchanged row at the end, or the one we have
17864 now displays text. This is a necessary condition for the window
17865 end pos calculation at the end of this function. */
17866 eassert (first_unchanged_at_end_row == NULL
17867 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17868
17869 debug_last_unchanged_at_beg_vpos
17870 = (last_unchanged_at_beg_row
17871 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17872 : -1);
17873 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17874
17875 #endif /* GLYPH_DEBUG */
17876
17877
17878 /* Display new lines. Set last_text_row to the last new line
17879 displayed which has text on it, i.e. might end up as being the
17880 line where the window_end_vpos is. */
17881 w->cursor.vpos = -1;
17882 last_text_row = NULL;
17883 overlay_arrow_seen = 0;
17884 while (it.current_y < it.last_visible_y
17885 && !f->fonts_changed
17886 && (first_unchanged_at_end_row == NULL
17887 || IT_CHARPOS (it) < stop_pos))
17888 {
17889 if (display_line (&it))
17890 last_text_row = it.glyph_row - 1;
17891 }
17892
17893 if (f->fonts_changed)
17894 return -1;
17895
17896
17897 /* Compute differences in buffer positions, y-positions etc. for
17898 lines reused at the bottom of the window. Compute what we can
17899 scroll. */
17900 if (first_unchanged_at_end_row
17901 /* No lines reused because we displayed everything up to the
17902 bottom of the window. */
17903 && it.current_y < it.last_visible_y)
17904 {
17905 dvpos = (it.vpos
17906 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17907 current_matrix));
17908 dy = it.current_y - first_unchanged_at_end_row->y;
17909 run.current_y = first_unchanged_at_end_row->y;
17910 run.desired_y = run.current_y + dy;
17911 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17912 }
17913 else
17914 {
17915 delta = delta_bytes = dvpos = dy
17916 = run.current_y = run.desired_y = run.height = 0;
17917 first_unchanged_at_end_row = NULL;
17918 }
17919 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17920
17921
17922 /* Find the cursor if not already found. We have to decide whether
17923 PT will appear on this window (it sometimes doesn't, but this is
17924 not a very frequent case.) This decision has to be made before
17925 the current matrix is altered. A value of cursor.vpos < 0 means
17926 that PT is either in one of the lines beginning at
17927 first_unchanged_at_end_row or below the window. Don't care for
17928 lines that might be displayed later at the window end; as
17929 mentioned, this is not a frequent case. */
17930 if (w->cursor.vpos < 0)
17931 {
17932 /* Cursor in unchanged rows at the top? */
17933 if (PT < CHARPOS (start_pos)
17934 && last_unchanged_at_beg_row)
17935 {
17936 row = row_containing_pos (w, PT,
17937 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17938 last_unchanged_at_beg_row + 1, 0);
17939 if (row)
17940 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17941 }
17942
17943 /* Start from first_unchanged_at_end_row looking for PT. */
17944 else if (first_unchanged_at_end_row)
17945 {
17946 row = row_containing_pos (w, PT - delta,
17947 first_unchanged_at_end_row, NULL, 0);
17948 if (row)
17949 set_cursor_from_row (w, row, w->current_matrix, delta,
17950 delta_bytes, dy, dvpos);
17951 }
17952
17953 /* Give up if cursor was not found. */
17954 if (w->cursor.vpos < 0)
17955 {
17956 clear_glyph_matrix (w->desired_matrix);
17957 return -1;
17958 }
17959 }
17960
17961 /* Don't let the cursor end in the scroll margins. */
17962 {
17963 int this_scroll_margin, cursor_height;
17964 int frame_line_height = default_line_pixel_height (w);
17965 int window_total_lines
17966 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17967
17968 this_scroll_margin =
17969 max (0, min (scroll_margin, window_total_lines / 4));
17970 this_scroll_margin *= frame_line_height;
17971 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17972
17973 if ((w->cursor.y < this_scroll_margin
17974 && CHARPOS (start) > BEGV)
17975 /* Old redisplay didn't take scroll margin into account at the bottom,
17976 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17977 || (w->cursor.y + (make_cursor_line_fully_visible_p
17978 ? cursor_height + this_scroll_margin
17979 : 1)) > it.last_visible_y)
17980 {
17981 w->cursor.vpos = -1;
17982 clear_glyph_matrix (w->desired_matrix);
17983 return -1;
17984 }
17985 }
17986
17987 /* Scroll the display. Do it before changing the current matrix so
17988 that xterm.c doesn't get confused about where the cursor glyph is
17989 found. */
17990 if (dy && run.height)
17991 {
17992 update_begin (f);
17993
17994 if (FRAME_WINDOW_P (f))
17995 {
17996 FRAME_RIF (f)->update_window_begin_hook (w);
17997 FRAME_RIF (f)->clear_window_mouse_face (w);
17998 FRAME_RIF (f)->scroll_run_hook (w, &run);
17999 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18000 }
18001 else
18002 {
18003 /* Terminal frame. In this case, dvpos gives the number of
18004 lines to scroll by; dvpos < 0 means scroll up. */
18005 int from_vpos
18006 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18007 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18008 int end = (WINDOW_TOP_EDGE_LINE (w)
18009 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18010 + window_internal_height (w));
18011
18012 #if defined (HAVE_GPM) || defined (MSDOS)
18013 x_clear_window_mouse_face (w);
18014 #endif
18015 /* Perform the operation on the screen. */
18016 if (dvpos > 0)
18017 {
18018 /* Scroll last_unchanged_at_beg_row to the end of the
18019 window down dvpos lines. */
18020 set_terminal_window (f, end);
18021
18022 /* On dumb terminals delete dvpos lines at the end
18023 before inserting dvpos empty lines. */
18024 if (!FRAME_SCROLL_REGION_OK (f))
18025 ins_del_lines (f, end - dvpos, -dvpos);
18026
18027 /* Insert dvpos empty lines in front of
18028 last_unchanged_at_beg_row. */
18029 ins_del_lines (f, from, dvpos);
18030 }
18031 else if (dvpos < 0)
18032 {
18033 /* Scroll up last_unchanged_at_beg_vpos to the end of
18034 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18035 set_terminal_window (f, end);
18036
18037 /* Delete dvpos lines in front of
18038 last_unchanged_at_beg_vpos. ins_del_lines will set
18039 the cursor to the given vpos and emit |dvpos| delete
18040 line sequences. */
18041 ins_del_lines (f, from + dvpos, dvpos);
18042
18043 /* On a dumb terminal insert dvpos empty lines at the
18044 end. */
18045 if (!FRAME_SCROLL_REGION_OK (f))
18046 ins_del_lines (f, end + dvpos, -dvpos);
18047 }
18048
18049 set_terminal_window (f, 0);
18050 }
18051
18052 update_end (f);
18053 }
18054
18055 /* Shift reused rows of the current matrix to the right position.
18056 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18057 text. */
18058 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18059 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18060 if (dvpos < 0)
18061 {
18062 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18063 bottom_vpos, dvpos);
18064 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18065 bottom_vpos);
18066 }
18067 else if (dvpos > 0)
18068 {
18069 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18070 bottom_vpos, dvpos);
18071 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18072 first_unchanged_at_end_vpos + dvpos);
18073 }
18074
18075 /* For frame-based redisplay, make sure that current frame and window
18076 matrix are in sync with respect to glyph memory. */
18077 if (!FRAME_WINDOW_P (f))
18078 sync_frame_with_window_matrix_rows (w);
18079
18080 /* Adjust buffer positions in reused rows. */
18081 if (delta || delta_bytes)
18082 increment_matrix_positions (current_matrix,
18083 first_unchanged_at_end_vpos + dvpos,
18084 bottom_vpos, delta, delta_bytes);
18085
18086 /* Adjust Y positions. */
18087 if (dy)
18088 shift_glyph_matrix (w, current_matrix,
18089 first_unchanged_at_end_vpos + dvpos,
18090 bottom_vpos, dy);
18091
18092 if (first_unchanged_at_end_row)
18093 {
18094 first_unchanged_at_end_row += dvpos;
18095 if (first_unchanged_at_end_row->y >= it.last_visible_y
18096 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18097 first_unchanged_at_end_row = NULL;
18098 }
18099
18100 /* If scrolling up, there may be some lines to display at the end of
18101 the window. */
18102 last_text_row_at_end = NULL;
18103 if (dy < 0)
18104 {
18105 /* Scrolling up can leave for example a partially visible line
18106 at the end of the window to be redisplayed. */
18107 /* Set last_row to the glyph row in the current matrix where the
18108 window end line is found. It has been moved up or down in
18109 the matrix by dvpos. */
18110 int last_vpos = w->window_end_vpos + dvpos;
18111 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18112
18113 /* If last_row is the window end line, it should display text. */
18114 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18115
18116 /* If window end line was partially visible before, begin
18117 displaying at that line. Otherwise begin displaying with the
18118 line following it. */
18119 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18120 {
18121 init_to_row_start (&it, w, last_row);
18122 it.vpos = last_vpos;
18123 it.current_y = last_row->y;
18124 }
18125 else
18126 {
18127 init_to_row_end (&it, w, last_row);
18128 it.vpos = 1 + last_vpos;
18129 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18130 ++last_row;
18131 }
18132
18133 /* We may start in a continuation line. If so, we have to
18134 get the right continuation_lines_width and current_x. */
18135 it.continuation_lines_width = last_row->continuation_lines_width;
18136 it.hpos = it.current_x = 0;
18137
18138 /* Display the rest of the lines at the window end. */
18139 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18140 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18141 {
18142 /* Is it always sure that the display agrees with lines in
18143 the current matrix? I don't think so, so we mark rows
18144 displayed invalid in the current matrix by setting their
18145 enabled_p flag to zero. */
18146 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18147 if (display_line (&it))
18148 last_text_row_at_end = it.glyph_row - 1;
18149 }
18150 }
18151
18152 /* Update window_end_pos and window_end_vpos. */
18153 if (first_unchanged_at_end_row && !last_text_row_at_end)
18154 {
18155 /* Window end line if one of the preserved rows from the current
18156 matrix. Set row to the last row displaying text in current
18157 matrix starting at first_unchanged_at_end_row, after
18158 scrolling. */
18159 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18160 row = find_last_row_displaying_text (w->current_matrix, &it,
18161 first_unchanged_at_end_row);
18162 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18163 adjust_window_ends (w, row, 1);
18164 eassert (w->window_end_bytepos >= 0);
18165 IF_DEBUG (debug_method_add (w, "A"));
18166 }
18167 else if (last_text_row_at_end)
18168 {
18169 adjust_window_ends (w, last_text_row_at_end, 0);
18170 eassert (w->window_end_bytepos >= 0);
18171 IF_DEBUG (debug_method_add (w, "B"));
18172 }
18173 else if (last_text_row)
18174 {
18175 /* We have displayed either to the end of the window or at the
18176 end of the window, i.e. the last row with text is to be found
18177 in the desired matrix. */
18178 adjust_window_ends (w, last_text_row, 0);
18179 eassert (w->window_end_bytepos >= 0);
18180 }
18181 else if (first_unchanged_at_end_row == NULL
18182 && last_text_row == NULL
18183 && last_text_row_at_end == NULL)
18184 {
18185 /* Displayed to end of window, but no line containing text was
18186 displayed. Lines were deleted at the end of the window. */
18187 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18188 int vpos = w->window_end_vpos;
18189 struct glyph_row *current_row = current_matrix->rows + vpos;
18190 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18191
18192 for (row = NULL;
18193 row == NULL && vpos >= first_vpos;
18194 --vpos, --current_row, --desired_row)
18195 {
18196 if (desired_row->enabled_p)
18197 {
18198 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18199 row = desired_row;
18200 }
18201 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18202 row = current_row;
18203 }
18204
18205 eassert (row != NULL);
18206 w->window_end_vpos = vpos + 1;
18207 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18208 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18209 eassert (w->window_end_bytepos >= 0);
18210 IF_DEBUG (debug_method_add (w, "C"));
18211 }
18212 else
18213 emacs_abort ();
18214
18215 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18216 debug_end_vpos = w->window_end_vpos));
18217
18218 /* Record that display has not been completed. */
18219 w->window_end_valid = 0;
18220 w->desired_matrix->no_scrolling_p = 1;
18221 return 3;
18222
18223 #undef GIVE_UP
18224 }
18225
18226
18227 \f
18228 /***********************************************************************
18229 More debugging support
18230 ***********************************************************************/
18231
18232 #ifdef GLYPH_DEBUG
18233
18234 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18235 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18236 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18237
18238
18239 /* Dump the contents of glyph matrix MATRIX on stderr.
18240
18241 GLYPHS 0 means don't show glyph contents.
18242 GLYPHS 1 means show glyphs in short form
18243 GLYPHS > 1 means show glyphs in long form. */
18244
18245 void
18246 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18247 {
18248 int i;
18249 for (i = 0; i < matrix->nrows; ++i)
18250 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18251 }
18252
18253
18254 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18255 the glyph row and area where the glyph comes from. */
18256
18257 void
18258 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18259 {
18260 if (glyph->type == CHAR_GLYPH
18261 || glyph->type == GLYPHLESS_GLYPH)
18262 {
18263 fprintf (stderr,
18264 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18265 glyph - row->glyphs[TEXT_AREA],
18266 (glyph->type == CHAR_GLYPH
18267 ? 'C'
18268 : 'G'),
18269 glyph->charpos,
18270 (BUFFERP (glyph->object)
18271 ? 'B'
18272 : (STRINGP (glyph->object)
18273 ? 'S'
18274 : (INTEGERP (glyph->object)
18275 ? '0'
18276 : '-'))),
18277 glyph->pixel_width,
18278 glyph->u.ch,
18279 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18280 ? glyph->u.ch
18281 : '.'),
18282 glyph->face_id,
18283 glyph->left_box_line_p,
18284 glyph->right_box_line_p);
18285 }
18286 else if (glyph->type == STRETCH_GLYPH)
18287 {
18288 fprintf (stderr,
18289 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18290 glyph - row->glyphs[TEXT_AREA],
18291 'S',
18292 glyph->charpos,
18293 (BUFFERP (glyph->object)
18294 ? 'B'
18295 : (STRINGP (glyph->object)
18296 ? 'S'
18297 : (INTEGERP (glyph->object)
18298 ? '0'
18299 : '-'))),
18300 glyph->pixel_width,
18301 0,
18302 ' ',
18303 glyph->face_id,
18304 glyph->left_box_line_p,
18305 glyph->right_box_line_p);
18306 }
18307 else if (glyph->type == IMAGE_GLYPH)
18308 {
18309 fprintf (stderr,
18310 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18311 glyph - row->glyphs[TEXT_AREA],
18312 'I',
18313 glyph->charpos,
18314 (BUFFERP (glyph->object)
18315 ? 'B'
18316 : (STRINGP (glyph->object)
18317 ? 'S'
18318 : (INTEGERP (glyph->object)
18319 ? '0'
18320 : '-'))),
18321 glyph->pixel_width,
18322 glyph->u.img_id,
18323 '.',
18324 glyph->face_id,
18325 glyph->left_box_line_p,
18326 glyph->right_box_line_p);
18327 }
18328 else if (glyph->type == COMPOSITE_GLYPH)
18329 {
18330 fprintf (stderr,
18331 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18332 glyph - row->glyphs[TEXT_AREA],
18333 '+',
18334 glyph->charpos,
18335 (BUFFERP (glyph->object)
18336 ? 'B'
18337 : (STRINGP (glyph->object)
18338 ? 'S'
18339 : (INTEGERP (glyph->object)
18340 ? '0'
18341 : '-'))),
18342 glyph->pixel_width,
18343 glyph->u.cmp.id);
18344 if (glyph->u.cmp.automatic)
18345 fprintf (stderr,
18346 "[%d-%d]",
18347 glyph->slice.cmp.from, glyph->slice.cmp.to);
18348 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18349 glyph->face_id,
18350 glyph->left_box_line_p,
18351 glyph->right_box_line_p);
18352 }
18353 }
18354
18355
18356 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18357 GLYPHS 0 means don't show glyph contents.
18358 GLYPHS 1 means show glyphs in short form
18359 GLYPHS > 1 means show glyphs in long form. */
18360
18361 void
18362 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18363 {
18364 if (glyphs != 1)
18365 {
18366 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18367 fprintf (stderr, "==============================================================================\n");
18368
18369 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18370 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18371 vpos,
18372 MATRIX_ROW_START_CHARPOS (row),
18373 MATRIX_ROW_END_CHARPOS (row),
18374 row->used[TEXT_AREA],
18375 row->contains_overlapping_glyphs_p,
18376 row->enabled_p,
18377 row->truncated_on_left_p,
18378 row->truncated_on_right_p,
18379 row->continued_p,
18380 MATRIX_ROW_CONTINUATION_LINE_P (row),
18381 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18382 row->ends_at_zv_p,
18383 row->fill_line_p,
18384 row->ends_in_middle_of_char_p,
18385 row->starts_in_middle_of_char_p,
18386 row->mouse_face_p,
18387 row->x,
18388 row->y,
18389 row->pixel_width,
18390 row->height,
18391 row->visible_height,
18392 row->ascent,
18393 row->phys_ascent);
18394 /* The next 3 lines should align to "Start" in the header. */
18395 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18396 row->end.overlay_string_index,
18397 row->continuation_lines_width);
18398 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18399 CHARPOS (row->start.string_pos),
18400 CHARPOS (row->end.string_pos));
18401 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18402 row->end.dpvec_index);
18403 }
18404
18405 if (glyphs > 1)
18406 {
18407 int area;
18408
18409 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18410 {
18411 struct glyph *glyph = row->glyphs[area];
18412 struct glyph *glyph_end = glyph + row->used[area];
18413
18414 /* Glyph for a line end in text. */
18415 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18416 ++glyph_end;
18417
18418 if (glyph < glyph_end)
18419 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18420
18421 for (; glyph < glyph_end; ++glyph)
18422 dump_glyph (row, glyph, area);
18423 }
18424 }
18425 else if (glyphs == 1)
18426 {
18427 int area;
18428
18429 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18430 {
18431 char *s = alloca (row->used[area] + 4);
18432 int i;
18433
18434 for (i = 0; i < row->used[area]; ++i)
18435 {
18436 struct glyph *glyph = row->glyphs[area] + i;
18437 if (i == row->used[area] - 1
18438 && area == TEXT_AREA
18439 && INTEGERP (glyph->object)
18440 && glyph->type == CHAR_GLYPH
18441 && glyph->u.ch == ' ')
18442 {
18443 strcpy (&s[i], "[\\n]");
18444 i += 4;
18445 }
18446 else if (glyph->type == CHAR_GLYPH
18447 && glyph->u.ch < 0x80
18448 && glyph->u.ch >= ' ')
18449 s[i] = glyph->u.ch;
18450 else
18451 s[i] = '.';
18452 }
18453
18454 s[i] = '\0';
18455 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18456 }
18457 }
18458 }
18459
18460
18461 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18462 Sdump_glyph_matrix, 0, 1, "p",
18463 doc: /* Dump the current matrix of the selected window to stderr.
18464 Shows contents of glyph row structures. With non-nil
18465 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18466 glyphs in short form, otherwise show glyphs in long form. */)
18467 (Lisp_Object glyphs)
18468 {
18469 struct window *w = XWINDOW (selected_window);
18470 struct buffer *buffer = XBUFFER (w->contents);
18471
18472 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18473 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18474 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18475 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18476 fprintf (stderr, "=============================================\n");
18477 dump_glyph_matrix (w->current_matrix,
18478 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18479 return Qnil;
18480 }
18481
18482
18483 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18484 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18485 (void)
18486 {
18487 struct frame *f = XFRAME (selected_frame);
18488 dump_glyph_matrix (f->current_matrix, 1);
18489 return Qnil;
18490 }
18491
18492
18493 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18494 doc: /* Dump glyph row ROW to stderr.
18495 GLYPH 0 means don't dump glyphs.
18496 GLYPH 1 means dump glyphs in short form.
18497 GLYPH > 1 or omitted means dump glyphs in long form. */)
18498 (Lisp_Object row, Lisp_Object glyphs)
18499 {
18500 struct glyph_matrix *matrix;
18501 EMACS_INT vpos;
18502
18503 CHECK_NUMBER (row);
18504 matrix = XWINDOW (selected_window)->current_matrix;
18505 vpos = XINT (row);
18506 if (vpos >= 0 && vpos < matrix->nrows)
18507 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18508 vpos,
18509 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18510 return Qnil;
18511 }
18512
18513
18514 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18515 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18516 GLYPH 0 means don't dump glyphs.
18517 GLYPH 1 means dump glyphs in short form.
18518 GLYPH > 1 or omitted means dump glyphs in long form.
18519
18520 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18521 do nothing. */)
18522 (Lisp_Object row, Lisp_Object glyphs)
18523 {
18524 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18525 struct frame *sf = SELECTED_FRAME ();
18526 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18527 EMACS_INT vpos;
18528
18529 CHECK_NUMBER (row);
18530 vpos = XINT (row);
18531 if (vpos >= 0 && vpos < m->nrows)
18532 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18533 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18534 #endif
18535 return Qnil;
18536 }
18537
18538
18539 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18540 doc: /* Toggle tracing of redisplay.
18541 With ARG, turn tracing on if and only if ARG is positive. */)
18542 (Lisp_Object arg)
18543 {
18544 if (NILP (arg))
18545 trace_redisplay_p = !trace_redisplay_p;
18546 else
18547 {
18548 arg = Fprefix_numeric_value (arg);
18549 trace_redisplay_p = XINT (arg) > 0;
18550 }
18551
18552 return Qnil;
18553 }
18554
18555
18556 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18557 doc: /* Like `format', but print result to stderr.
18558 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18559 (ptrdiff_t nargs, Lisp_Object *args)
18560 {
18561 Lisp_Object s = Fformat (nargs, args);
18562 fprintf (stderr, "%s", SDATA (s));
18563 return Qnil;
18564 }
18565
18566 #endif /* GLYPH_DEBUG */
18567
18568
18569 \f
18570 /***********************************************************************
18571 Building Desired Matrix Rows
18572 ***********************************************************************/
18573
18574 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18575 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18576
18577 static struct glyph_row *
18578 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18579 {
18580 struct frame *f = XFRAME (WINDOW_FRAME (w));
18581 struct buffer *buffer = XBUFFER (w->contents);
18582 struct buffer *old = current_buffer;
18583 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18584 int arrow_len = SCHARS (overlay_arrow_string);
18585 const unsigned char *arrow_end = arrow_string + arrow_len;
18586 const unsigned char *p;
18587 struct it it;
18588 bool multibyte_p;
18589 int n_glyphs_before;
18590
18591 set_buffer_temp (buffer);
18592 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18593 it.glyph_row->used[TEXT_AREA] = 0;
18594 SET_TEXT_POS (it.position, 0, 0);
18595
18596 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18597 p = arrow_string;
18598 while (p < arrow_end)
18599 {
18600 Lisp_Object face, ilisp;
18601
18602 /* Get the next character. */
18603 if (multibyte_p)
18604 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18605 else
18606 {
18607 it.c = it.char_to_display = *p, it.len = 1;
18608 if (! ASCII_CHAR_P (it.c))
18609 it.char_to_display = BYTE8_TO_CHAR (it.c);
18610 }
18611 p += it.len;
18612
18613 /* Get its face. */
18614 ilisp = make_number (p - arrow_string);
18615 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18616 it.face_id = compute_char_face (f, it.char_to_display, face);
18617
18618 /* Compute its width, get its glyphs. */
18619 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18620 SET_TEXT_POS (it.position, -1, -1);
18621 PRODUCE_GLYPHS (&it);
18622
18623 /* If this character doesn't fit any more in the line, we have
18624 to remove some glyphs. */
18625 if (it.current_x > it.last_visible_x)
18626 {
18627 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18628 break;
18629 }
18630 }
18631
18632 set_buffer_temp (old);
18633 return it.glyph_row;
18634 }
18635
18636
18637 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18638 glyphs to insert is determined by produce_special_glyphs. */
18639
18640 static void
18641 insert_left_trunc_glyphs (struct it *it)
18642 {
18643 struct it truncate_it;
18644 struct glyph *from, *end, *to, *toend;
18645
18646 eassert (!FRAME_WINDOW_P (it->f)
18647 || (!it->glyph_row->reversed_p
18648 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18649 || (it->glyph_row->reversed_p
18650 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18651
18652 /* Get the truncation glyphs. */
18653 truncate_it = *it;
18654 truncate_it.current_x = 0;
18655 truncate_it.face_id = DEFAULT_FACE_ID;
18656 truncate_it.glyph_row = &scratch_glyph_row;
18657 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18658 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18659 truncate_it.object = make_number (0);
18660 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18661
18662 /* Overwrite glyphs from IT with truncation glyphs. */
18663 if (!it->glyph_row->reversed_p)
18664 {
18665 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18666
18667 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18668 end = from + tused;
18669 to = it->glyph_row->glyphs[TEXT_AREA];
18670 toend = to + it->glyph_row->used[TEXT_AREA];
18671 if (FRAME_WINDOW_P (it->f))
18672 {
18673 /* On GUI frames, when variable-size fonts are displayed,
18674 the truncation glyphs may need more pixels than the row's
18675 glyphs they overwrite. We overwrite more glyphs to free
18676 enough screen real estate, and enlarge the stretch glyph
18677 on the right (see display_line), if there is one, to
18678 preserve the screen position of the truncation glyphs on
18679 the right. */
18680 int w = 0;
18681 struct glyph *g = to;
18682 short used;
18683
18684 /* The first glyph could be partially visible, in which case
18685 it->glyph_row->x will be negative. But we want the left
18686 truncation glyphs to be aligned at the left margin of the
18687 window, so we override the x coordinate at which the row
18688 will begin. */
18689 it->glyph_row->x = 0;
18690 while (g < toend && w < it->truncation_pixel_width)
18691 {
18692 w += g->pixel_width;
18693 ++g;
18694 }
18695 if (g - to - tused > 0)
18696 {
18697 memmove (to + tused, g, (toend - g) * sizeof(*g));
18698 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18699 }
18700 used = it->glyph_row->used[TEXT_AREA];
18701 if (it->glyph_row->truncated_on_right_p
18702 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18703 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18704 == STRETCH_GLYPH)
18705 {
18706 int extra = w - it->truncation_pixel_width;
18707
18708 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18709 }
18710 }
18711
18712 while (from < end)
18713 *to++ = *from++;
18714
18715 /* There may be padding glyphs left over. Overwrite them too. */
18716 if (!FRAME_WINDOW_P (it->f))
18717 {
18718 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18719 {
18720 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18721 while (from < end)
18722 *to++ = *from++;
18723 }
18724 }
18725
18726 if (to > toend)
18727 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18728 }
18729 else
18730 {
18731 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18732
18733 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18734 that back to front. */
18735 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18736 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18737 toend = it->glyph_row->glyphs[TEXT_AREA];
18738 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18739 if (FRAME_WINDOW_P (it->f))
18740 {
18741 int w = 0;
18742 struct glyph *g = to;
18743
18744 while (g >= toend && w < it->truncation_pixel_width)
18745 {
18746 w += g->pixel_width;
18747 --g;
18748 }
18749 if (to - g - tused > 0)
18750 to = g + tused;
18751 if (it->glyph_row->truncated_on_right_p
18752 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18753 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18754 {
18755 int extra = w - it->truncation_pixel_width;
18756
18757 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18758 }
18759 }
18760
18761 while (from >= end && to >= toend)
18762 *to-- = *from--;
18763 if (!FRAME_WINDOW_P (it->f))
18764 {
18765 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18766 {
18767 from =
18768 truncate_it.glyph_row->glyphs[TEXT_AREA]
18769 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18770 while (from >= end && to >= toend)
18771 *to-- = *from--;
18772 }
18773 }
18774 if (from >= end)
18775 {
18776 /* Need to free some room before prepending additional
18777 glyphs. */
18778 int move_by = from - end + 1;
18779 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18780 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18781
18782 for ( ; g >= g0; g--)
18783 g[move_by] = *g;
18784 while (from >= end)
18785 *to-- = *from--;
18786 it->glyph_row->used[TEXT_AREA] += move_by;
18787 }
18788 }
18789 }
18790
18791 /* Compute the hash code for ROW. */
18792 unsigned
18793 row_hash (struct glyph_row *row)
18794 {
18795 int area, k;
18796 unsigned hashval = 0;
18797
18798 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18799 for (k = 0; k < row->used[area]; ++k)
18800 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18801 + row->glyphs[area][k].u.val
18802 + row->glyphs[area][k].face_id
18803 + row->glyphs[area][k].padding_p
18804 + (row->glyphs[area][k].type << 2));
18805
18806 return hashval;
18807 }
18808
18809 /* Compute the pixel height and width of IT->glyph_row.
18810
18811 Most of the time, ascent and height of a display line will be equal
18812 to the max_ascent and max_height values of the display iterator
18813 structure. This is not the case if
18814
18815 1. We hit ZV without displaying anything. In this case, max_ascent
18816 and max_height will be zero.
18817
18818 2. We have some glyphs that don't contribute to the line height.
18819 (The glyph row flag contributes_to_line_height_p is for future
18820 pixmap extensions).
18821
18822 The first case is easily covered by using default values because in
18823 these cases, the line height does not really matter, except that it
18824 must not be zero. */
18825
18826 static void
18827 compute_line_metrics (struct it *it)
18828 {
18829 struct glyph_row *row = it->glyph_row;
18830
18831 if (FRAME_WINDOW_P (it->f))
18832 {
18833 int i, min_y, max_y;
18834
18835 /* The line may consist of one space only, that was added to
18836 place the cursor on it. If so, the row's height hasn't been
18837 computed yet. */
18838 if (row->height == 0)
18839 {
18840 if (it->max_ascent + it->max_descent == 0)
18841 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18842 row->ascent = it->max_ascent;
18843 row->height = it->max_ascent + it->max_descent;
18844 row->phys_ascent = it->max_phys_ascent;
18845 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18846 row->extra_line_spacing = it->max_extra_line_spacing;
18847 }
18848
18849 /* Compute the width of this line. */
18850 row->pixel_width = row->x;
18851 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18852 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18853
18854 eassert (row->pixel_width >= 0);
18855 eassert (row->ascent >= 0 && row->height > 0);
18856
18857 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18858 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18859
18860 /* If first line's physical ascent is larger than its logical
18861 ascent, use the physical ascent, and make the row taller.
18862 This makes accented characters fully visible. */
18863 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18864 && row->phys_ascent > row->ascent)
18865 {
18866 row->height += row->phys_ascent - row->ascent;
18867 row->ascent = row->phys_ascent;
18868 }
18869
18870 /* Compute how much of the line is visible. */
18871 row->visible_height = row->height;
18872
18873 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18874 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18875
18876 if (row->y < min_y)
18877 row->visible_height -= min_y - row->y;
18878 if (row->y + row->height > max_y)
18879 row->visible_height -= row->y + row->height - max_y;
18880 }
18881 else
18882 {
18883 row->pixel_width = row->used[TEXT_AREA];
18884 if (row->continued_p)
18885 row->pixel_width -= it->continuation_pixel_width;
18886 else if (row->truncated_on_right_p)
18887 row->pixel_width -= it->truncation_pixel_width;
18888 row->ascent = row->phys_ascent = 0;
18889 row->height = row->phys_height = row->visible_height = 1;
18890 row->extra_line_spacing = 0;
18891 }
18892
18893 /* Compute a hash code for this row. */
18894 row->hash = row_hash (row);
18895
18896 it->max_ascent = it->max_descent = 0;
18897 it->max_phys_ascent = it->max_phys_descent = 0;
18898 }
18899
18900
18901 /* Append one space to the glyph row of iterator IT if doing a
18902 window-based redisplay. The space has the same face as
18903 IT->face_id. Value is non-zero if a space was added.
18904
18905 This function is called to make sure that there is always one glyph
18906 at the end of a glyph row that the cursor can be set on under
18907 window-systems. (If there weren't such a glyph we would not know
18908 how wide and tall a box cursor should be displayed).
18909
18910 At the same time this space let's a nicely handle clearing to the
18911 end of the line if the row ends in italic text. */
18912
18913 static int
18914 append_space_for_newline (struct it *it, int default_face_p)
18915 {
18916 if (FRAME_WINDOW_P (it->f))
18917 {
18918 int n = it->glyph_row->used[TEXT_AREA];
18919
18920 if (it->glyph_row->glyphs[TEXT_AREA] + n
18921 < it->glyph_row->glyphs[1 + TEXT_AREA])
18922 {
18923 /* Save some values that must not be changed.
18924 Must save IT->c and IT->len because otherwise
18925 ITERATOR_AT_END_P wouldn't work anymore after
18926 append_space_for_newline has been called. */
18927 enum display_element_type saved_what = it->what;
18928 int saved_c = it->c, saved_len = it->len;
18929 int saved_char_to_display = it->char_to_display;
18930 int saved_x = it->current_x;
18931 int saved_face_id = it->face_id;
18932 int saved_box_end = it->end_of_box_run_p;
18933 struct text_pos saved_pos;
18934 Lisp_Object saved_object;
18935 struct face *face;
18936
18937 saved_object = it->object;
18938 saved_pos = it->position;
18939
18940 it->what = IT_CHARACTER;
18941 memset (&it->position, 0, sizeof it->position);
18942 it->object = make_number (0);
18943 it->c = it->char_to_display = ' ';
18944 it->len = 1;
18945
18946 /* If the default face was remapped, be sure to use the
18947 remapped face for the appended newline. */
18948 if (default_face_p)
18949 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18950 else if (it->face_before_selective_p)
18951 it->face_id = it->saved_face_id;
18952 face = FACE_FROM_ID (it->f, it->face_id);
18953 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18954 /* In R2L rows, we will prepend a stretch glyph that will
18955 have the end_of_box_run_p flag set for it, so there's no
18956 need for the appended newline glyph to have that flag
18957 set. */
18958 if (it->glyph_row->reversed_p
18959 /* But if the appended newline glyph goes all the way to
18960 the end of the row, there will be no stretch glyph,
18961 so leave the box flag set. */
18962 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18963 it->end_of_box_run_p = 0;
18964
18965 PRODUCE_GLYPHS (it);
18966
18967 it->override_ascent = -1;
18968 it->constrain_row_ascent_descent_p = 0;
18969 it->current_x = saved_x;
18970 it->object = saved_object;
18971 it->position = saved_pos;
18972 it->what = saved_what;
18973 it->face_id = saved_face_id;
18974 it->len = saved_len;
18975 it->c = saved_c;
18976 it->char_to_display = saved_char_to_display;
18977 it->end_of_box_run_p = saved_box_end;
18978 return 1;
18979 }
18980 }
18981
18982 return 0;
18983 }
18984
18985
18986 /* Extend the face of the last glyph in the text area of IT->glyph_row
18987 to the end of the display line. Called from display_line. If the
18988 glyph row is empty, add a space glyph to it so that we know the
18989 face to draw. Set the glyph row flag fill_line_p. If the glyph
18990 row is R2L, prepend a stretch glyph to cover the empty space to the
18991 left of the leftmost glyph. */
18992
18993 static void
18994 extend_face_to_end_of_line (struct it *it)
18995 {
18996 struct face *face, *default_face;
18997 struct frame *f = it->f;
18998
18999 /* If line is already filled, do nothing. Non window-system frames
19000 get a grace of one more ``pixel'' because their characters are
19001 1-``pixel'' wide, so they hit the equality too early. This grace
19002 is needed only for R2L rows that are not continued, to produce
19003 one extra blank where we could display the cursor. */
19004 if ((it->current_x >= it->last_visible_x
19005 + (!FRAME_WINDOW_P (f)
19006 && it->glyph_row->reversed_p
19007 && !it->glyph_row->continued_p))
19008 /* If the window has display margins, we will need to extend
19009 their face even if the text area is filled. */
19010 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19011 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19012 return;
19013
19014 /* The default face, possibly remapped. */
19015 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19016
19017 /* Face extension extends the background and box of IT->face_id
19018 to the end of the line. If the background equals the background
19019 of the frame, we don't have to do anything. */
19020 if (it->face_before_selective_p)
19021 face = FACE_FROM_ID (f, it->saved_face_id);
19022 else
19023 face = FACE_FROM_ID (f, it->face_id);
19024
19025 if (FRAME_WINDOW_P (f)
19026 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19027 && face->box == FACE_NO_BOX
19028 && face->background == FRAME_BACKGROUND_PIXEL (f)
19029 #ifdef HAVE_WINDOW_SYSTEM
19030 && !face->stipple
19031 #endif
19032 && !it->glyph_row->reversed_p)
19033 return;
19034
19035 /* Set the glyph row flag indicating that the face of the last glyph
19036 in the text area has to be drawn to the end of the text area. */
19037 it->glyph_row->fill_line_p = 1;
19038
19039 /* If current character of IT is not ASCII, make sure we have the
19040 ASCII face. This will be automatically undone the next time
19041 get_next_display_element returns a multibyte character. Note
19042 that the character will always be single byte in unibyte
19043 text. */
19044 if (!ASCII_CHAR_P (it->c))
19045 {
19046 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19047 }
19048
19049 if (FRAME_WINDOW_P (f))
19050 {
19051 /* If the row is empty, add a space with the current face of IT,
19052 so that we know which face to draw. */
19053 if (it->glyph_row->used[TEXT_AREA] == 0)
19054 {
19055 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19056 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19057 it->glyph_row->used[TEXT_AREA] = 1;
19058 }
19059 /* Mode line and the header line don't have margins, and
19060 likewise the frame's tool-bar window, if there is any. */
19061 if (!(it->glyph_row->mode_line_p
19062 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19063 || (WINDOWP (f->tool_bar_window)
19064 && it->w == XWINDOW (f->tool_bar_window))
19065 #endif
19066 ))
19067 {
19068 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19069 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19070 {
19071 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19072 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19073 default_face->id;
19074 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19075 }
19076 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19077 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19078 {
19079 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19080 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19081 default_face->id;
19082 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19083 }
19084 }
19085 #ifdef HAVE_WINDOW_SYSTEM
19086 if (it->glyph_row->reversed_p)
19087 {
19088 /* Prepend a stretch glyph to the row, such that the
19089 rightmost glyph will be drawn flushed all the way to the
19090 right margin of the window. The stretch glyph that will
19091 occupy the empty space, if any, to the left of the
19092 glyphs. */
19093 struct font *font = face->font ? face->font : FRAME_FONT (f);
19094 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19095 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19096 struct glyph *g;
19097 int row_width, stretch_ascent, stretch_width;
19098 struct text_pos saved_pos;
19099 int saved_face_id, saved_avoid_cursor, saved_box_start;
19100
19101 for (row_width = 0, g = row_start; g < row_end; g++)
19102 row_width += g->pixel_width;
19103 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19104 if (stretch_width > 0)
19105 {
19106 stretch_ascent =
19107 (((it->ascent + it->descent)
19108 * FONT_BASE (font)) / FONT_HEIGHT (font));
19109 saved_pos = it->position;
19110 memset (&it->position, 0, sizeof it->position);
19111 saved_avoid_cursor = it->avoid_cursor_p;
19112 it->avoid_cursor_p = 1;
19113 saved_face_id = it->face_id;
19114 saved_box_start = it->start_of_box_run_p;
19115 /* The last row's stretch glyph should get the default
19116 face, to avoid painting the rest of the window with
19117 the region face, if the region ends at ZV. */
19118 if (it->glyph_row->ends_at_zv_p)
19119 it->face_id = default_face->id;
19120 else
19121 it->face_id = face->id;
19122 it->start_of_box_run_p = 0;
19123 append_stretch_glyph (it, make_number (0), stretch_width,
19124 it->ascent + it->descent, stretch_ascent);
19125 it->position = saved_pos;
19126 it->avoid_cursor_p = saved_avoid_cursor;
19127 it->face_id = saved_face_id;
19128 it->start_of_box_run_p = saved_box_start;
19129 }
19130 }
19131 #endif /* HAVE_WINDOW_SYSTEM */
19132 }
19133 else
19134 {
19135 /* Save some values that must not be changed. */
19136 int saved_x = it->current_x;
19137 struct text_pos saved_pos;
19138 Lisp_Object saved_object;
19139 enum display_element_type saved_what = it->what;
19140 int saved_face_id = it->face_id;
19141
19142 saved_object = it->object;
19143 saved_pos = it->position;
19144
19145 it->what = IT_CHARACTER;
19146 memset (&it->position, 0, sizeof it->position);
19147 it->object = make_number (0);
19148 it->c = it->char_to_display = ' ';
19149 it->len = 1;
19150
19151 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19152 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19153 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19154 && !it->glyph_row->mode_line_p
19155 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19156 {
19157 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19158 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19159
19160 for (it->current_x = 0; g < e; g++)
19161 it->current_x += g->pixel_width;
19162
19163 it->area = LEFT_MARGIN_AREA;
19164 it->face_id = default_face->id;
19165 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19166 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19167 {
19168 PRODUCE_GLYPHS (it);
19169 /* term.c:produce_glyphs advances it->current_x only for
19170 TEXT_AREA. */
19171 it->current_x += it->pixel_width;
19172 }
19173
19174 it->current_x = saved_x;
19175 it->area = TEXT_AREA;
19176 }
19177
19178 /* The last row's blank glyphs should get the default face, to
19179 avoid painting the rest of the window with the region face,
19180 if the region ends at ZV. */
19181 if (it->glyph_row->ends_at_zv_p)
19182 it->face_id = default_face->id;
19183 else
19184 it->face_id = face->id;
19185 PRODUCE_GLYPHS (it);
19186
19187 while (it->current_x <= it->last_visible_x)
19188 PRODUCE_GLYPHS (it);
19189
19190 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19191 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19192 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19193 && !it->glyph_row->mode_line_p
19194 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19195 {
19196 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19197 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19198
19199 for ( ; g < e; g++)
19200 it->current_x += g->pixel_width;
19201
19202 it->area = RIGHT_MARGIN_AREA;
19203 it->face_id = default_face->id;
19204 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19205 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19206 {
19207 PRODUCE_GLYPHS (it);
19208 it->current_x += it->pixel_width;
19209 }
19210
19211 it->area = TEXT_AREA;
19212 }
19213
19214 /* Don't count these blanks really. It would let us insert a left
19215 truncation glyph below and make us set the cursor on them, maybe. */
19216 it->current_x = saved_x;
19217 it->object = saved_object;
19218 it->position = saved_pos;
19219 it->what = saved_what;
19220 it->face_id = saved_face_id;
19221 }
19222 }
19223
19224
19225 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19226 trailing whitespace. */
19227
19228 static int
19229 trailing_whitespace_p (ptrdiff_t charpos)
19230 {
19231 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19232 int c = 0;
19233
19234 while (bytepos < ZV_BYTE
19235 && (c = FETCH_CHAR (bytepos),
19236 c == ' ' || c == '\t'))
19237 ++bytepos;
19238
19239 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19240 {
19241 if (bytepos != PT_BYTE)
19242 return 1;
19243 }
19244 return 0;
19245 }
19246
19247
19248 /* Highlight trailing whitespace, if any, in ROW. */
19249
19250 static void
19251 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19252 {
19253 int used = row->used[TEXT_AREA];
19254
19255 if (used)
19256 {
19257 struct glyph *start = row->glyphs[TEXT_AREA];
19258 struct glyph *glyph = start + used - 1;
19259
19260 if (row->reversed_p)
19261 {
19262 /* Right-to-left rows need to be processed in the opposite
19263 direction, so swap the edge pointers. */
19264 glyph = start;
19265 start = row->glyphs[TEXT_AREA] + used - 1;
19266 }
19267
19268 /* Skip over glyphs inserted to display the cursor at the
19269 end of a line, for extending the face of the last glyph
19270 to the end of the line on terminals, and for truncation
19271 and continuation glyphs. */
19272 if (!row->reversed_p)
19273 {
19274 while (glyph >= start
19275 && glyph->type == CHAR_GLYPH
19276 && INTEGERP (glyph->object))
19277 --glyph;
19278 }
19279 else
19280 {
19281 while (glyph <= start
19282 && glyph->type == CHAR_GLYPH
19283 && INTEGERP (glyph->object))
19284 ++glyph;
19285 }
19286
19287 /* If last glyph is a space or stretch, and it's trailing
19288 whitespace, set the face of all trailing whitespace glyphs in
19289 IT->glyph_row to `trailing-whitespace'. */
19290 if ((row->reversed_p ? glyph <= start : glyph >= start)
19291 && BUFFERP (glyph->object)
19292 && (glyph->type == STRETCH_GLYPH
19293 || (glyph->type == CHAR_GLYPH
19294 && glyph->u.ch == ' '))
19295 && trailing_whitespace_p (glyph->charpos))
19296 {
19297 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19298 if (face_id < 0)
19299 return;
19300
19301 if (!row->reversed_p)
19302 {
19303 while (glyph >= start
19304 && BUFFERP (glyph->object)
19305 && (glyph->type == STRETCH_GLYPH
19306 || (glyph->type == CHAR_GLYPH
19307 && glyph->u.ch == ' ')))
19308 (glyph--)->face_id = face_id;
19309 }
19310 else
19311 {
19312 while (glyph <= start
19313 && BUFFERP (glyph->object)
19314 && (glyph->type == STRETCH_GLYPH
19315 || (glyph->type == CHAR_GLYPH
19316 && glyph->u.ch == ' ')))
19317 (glyph++)->face_id = face_id;
19318 }
19319 }
19320 }
19321 }
19322
19323
19324 /* Value is non-zero if glyph row ROW should be
19325 considered to hold the buffer position CHARPOS. */
19326
19327 static int
19328 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19329 {
19330 int result = 1;
19331
19332 if (charpos == CHARPOS (row->end.pos)
19333 || charpos == MATRIX_ROW_END_CHARPOS (row))
19334 {
19335 /* Suppose the row ends on a string.
19336 Unless the row is continued, that means it ends on a newline
19337 in the string. If it's anything other than a display string
19338 (e.g., a before-string from an overlay), we don't want the
19339 cursor there. (This heuristic seems to give the optimal
19340 behavior for the various types of multi-line strings.)
19341 One exception: if the string has `cursor' property on one of
19342 its characters, we _do_ want the cursor there. */
19343 if (CHARPOS (row->end.string_pos) >= 0)
19344 {
19345 if (row->continued_p)
19346 result = 1;
19347 else
19348 {
19349 /* Check for `display' property. */
19350 struct glyph *beg = row->glyphs[TEXT_AREA];
19351 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19352 struct glyph *glyph;
19353
19354 result = 0;
19355 for (glyph = end; glyph >= beg; --glyph)
19356 if (STRINGP (glyph->object))
19357 {
19358 Lisp_Object prop
19359 = Fget_char_property (make_number (charpos),
19360 Qdisplay, Qnil);
19361 result =
19362 (!NILP (prop)
19363 && display_prop_string_p (prop, glyph->object));
19364 /* If there's a `cursor' property on one of the
19365 string's characters, this row is a cursor row,
19366 even though this is not a display string. */
19367 if (!result)
19368 {
19369 Lisp_Object s = glyph->object;
19370
19371 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19372 {
19373 ptrdiff_t gpos = glyph->charpos;
19374
19375 if (!NILP (Fget_char_property (make_number (gpos),
19376 Qcursor, s)))
19377 {
19378 result = 1;
19379 break;
19380 }
19381 }
19382 }
19383 break;
19384 }
19385 }
19386 }
19387 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19388 {
19389 /* If the row ends in middle of a real character,
19390 and the line is continued, we want the cursor here.
19391 That's because CHARPOS (ROW->end.pos) would equal
19392 PT if PT is before the character. */
19393 if (!row->ends_in_ellipsis_p)
19394 result = row->continued_p;
19395 else
19396 /* If the row ends in an ellipsis, then
19397 CHARPOS (ROW->end.pos) will equal point after the
19398 invisible text. We want that position to be displayed
19399 after the ellipsis. */
19400 result = 0;
19401 }
19402 /* If the row ends at ZV, display the cursor at the end of that
19403 row instead of at the start of the row below. */
19404 else if (row->ends_at_zv_p)
19405 result = 1;
19406 else
19407 result = 0;
19408 }
19409
19410 return result;
19411 }
19412
19413 /* Value is non-zero if glyph row ROW should be
19414 used to hold the cursor. */
19415
19416 static int
19417 cursor_row_p (struct glyph_row *row)
19418 {
19419 return row_for_charpos_p (row, PT);
19420 }
19421
19422 \f
19423
19424 /* Push the property PROP so that it will be rendered at the current
19425 position in IT. Return 1 if PROP was successfully pushed, 0
19426 otherwise. Called from handle_line_prefix to handle the
19427 `line-prefix' and `wrap-prefix' properties. */
19428
19429 static int
19430 push_prefix_prop (struct it *it, Lisp_Object prop)
19431 {
19432 struct text_pos pos =
19433 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19434
19435 eassert (it->method == GET_FROM_BUFFER
19436 || it->method == GET_FROM_DISPLAY_VECTOR
19437 || it->method == GET_FROM_STRING);
19438
19439 /* We need to save the current buffer/string position, so it will be
19440 restored by pop_it, because iterate_out_of_display_property
19441 depends on that being set correctly, but some situations leave
19442 it->position not yet set when this function is called. */
19443 push_it (it, &pos);
19444
19445 if (STRINGP (prop))
19446 {
19447 if (SCHARS (prop) == 0)
19448 {
19449 pop_it (it);
19450 return 0;
19451 }
19452
19453 it->string = prop;
19454 it->string_from_prefix_prop_p = 1;
19455 it->multibyte_p = STRING_MULTIBYTE (it->string);
19456 it->current.overlay_string_index = -1;
19457 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19458 it->end_charpos = it->string_nchars = SCHARS (it->string);
19459 it->method = GET_FROM_STRING;
19460 it->stop_charpos = 0;
19461 it->prev_stop = 0;
19462 it->base_level_stop = 0;
19463
19464 /* Force paragraph direction to be that of the parent
19465 buffer/string. */
19466 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19467 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19468 else
19469 it->paragraph_embedding = L2R;
19470
19471 /* Set up the bidi iterator for this display string. */
19472 if (it->bidi_p)
19473 {
19474 it->bidi_it.string.lstring = it->string;
19475 it->bidi_it.string.s = NULL;
19476 it->bidi_it.string.schars = it->end_charpos;
19477 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19478 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19479 it->bidi_it.string.unibyte = !it->multibyte_p;
19480 it->bidi_it.w = it->w;
19481 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19482 }
19483 }
19484 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19485 {
19486 it->method = GET_FROM_STRETCH;
19487 it->object = prop;
19488 }
19489 #ifdef HAVE_WINDOW_SYSTEM
19490 else if (IMAGEP (prop))
19491 {
19492 it->what = IT_IMAGE;
19493 it->image_id = lookup_image (it->f, prop);
19494 it->method = GET_FROM_IMAGE;
19495 }
19496 #endif /* HAVE_WINDOW_SYSTEM */
19497 else
19498 {
19499 pop_it (it); /* bogus display property, give up */
19500 return 0;
19501 }
19502
19503 return 1;
19504 }
19505
19506 /* Return the character-property PROP at the current position in IT. */
19507
19508 static Lisp_Object
19509 get_it_property (struct it *it, Lisp_Object prop)
19510 {
19511 Lisp_Object position, object = it->object;
19512
19513 if (STRINGP (object))
19514 position = make_number (IT_STRING_CHARPOS (*it));
19515 else if (BUFFERP (object))
19516 {
19517 position = make_number (IT_CHARPOS (*it));
19518 object = it->window;
19519 }
19520 else
19521 return Qnil;
19522
19523 return Fget_char_property (position, prop, object);
19524 }
19525
19526 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19527
19528 static void
19529 handle_line_prefix (struct it *it)
19530 {
19531 Lisp_Object prefix;
19532
19533 if (it->continuation_lines_width > 0)
19534 {
19535 prefix = get_it_property (it, Qwrap_prefix);
19536 if (NILP (prefix))
19537 prefix = Vwrap_prefix;
19538 }
19539 else
19540 {
19541 prefix = get_it_property (it, Qline_prefix);
19542 if (NILP (prefix))
19543 prefix = Vline_prefix;
19544 }
19545 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19546 {
19547 /* If the prefix is wider than the window, and we try to wrap
19548 it, it would acquire its own wrap prefix, and so on till the
19549 iterator stack overflows. So, don't wrap the prefix. */
19550 it->line_wrap = TRUNCATE;
19551 it->avoid_cursor_p = 1;
19552 }
19553 }
19554
19555 \f
19556
19557 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19558 only for R2L lines from display_line and display_string, when they
19559 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19560 the line/string needs to be continued on the next glyph row. */
19561 static void
19562 unproduce_glyphs (struct it *it, int n)
19563 {
19564 struct glyph *glyph, *end;
19565
19566 eassert (it->glyph_row);
19567 eassert (it->glyph_row->reversed_p);
19568 eassert (it->area == TEXT_AREA);
19569 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19570
19571 if (n > it->glyph_row->used[TEXT_AREA])
19572 n = it->glyph_row->used[TEXT_AREA];
19573 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19574 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19575 for ( ; glyph < end; glyph++)
19576 glyph[-n] = *glyph;
19577 }
19578
19579 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19580 and ROW->maxpos. */
19581 static void
19582 find_row_edges (struct it *it, struct glyph_row *row,
19583 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19584 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19585 {
19586 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19587 lines' rows is implemented for bidi-reordered rows. */
19588
19589 /* ROW->minpos is the value of min_pos, the minimal buffer position
19590 we have in ROW, or ROW->start.pos if that is smaller. */
19591 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19592 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19593 else
19594 /* We didn't find buffer positions smaller than ROW->start, or
19595 didn't find _any_ valid buffer positions in any of the glyphs,
19596 so we must trust the iterator's computed positions. */
19597 row->minpos = row->start.pos;
19598 if (max_pos <= 0)
19599 {
19600 max_pos = CHARPOS (it->current.pos);
19601 max_bpos = BYTEPOS (it->current.pos);
19602 }
19603
19604 /* Here are the various use-cases for ending the row, and the
19605 corresponding values for ROW->maxpos:
19606
19607 Line ends in a newline from buffer eol_pos + 1
19608 Line is continued from buffer max_pos + 1
19609 Line is truncated on right it->current.pos
19610 Line ends in a newline from string max_pos + 1(*)
19611 (*) + 1 only when line ends in a forward scan
19612 Line is continued from string max_pos
19613 Line is continued from display vector max_pos
19614 Line is entirely from a string min_pos == max_pos
19615 Line is entirely from a display vector min_pos == max_pos
19616 Line that ends at ZV ZV
19617
19618 If you discover other use-cases, please add them here as
19619 appropriate. */
19620 if (row->ends_at_zv_p)
19621 row->maxpos = it->current.pos;
19622 else if (row->used[TEXT_AREA])
19623 {
19624 int seen_this_string = 0;
19625 struct glyph_row *r1 = row - 1;
19626
19627 /* Did we see the same display string on the previous row? */
19628 if (STRINGP (it->object)
19629 /* this is not the first row */
19630 && row > it->w->desired_matrix->rows
19631 /* previous row is not the header line */
19632 && !r1->mode_line_p
19633 /* previous row also ends in a newline from a string */
19634 && r1->ends_in_newline_from_string_p)
19635 {
19636 struct glyph *start, *end;
19637
19638 /* Search for the last glyph of the previous row that came
19639 from buffer or string. Depending on whether the row is
19640 L2R or R2L, we need to process it front to back or the
19641 other way round. */
19642 if (!r1->reversed_p)
19643 {
19644 start = r1->glyphs[TEXT_AREA];
19645 end = start + r1->used[TEXT_AREA];
19646 /* Glyphs inserted by redisplay have an integer (zero)
19647 as their object. */
19648 while (end > start
19649 && INTEGERP ((end - 1)->object)
19650 && (end - 1)->charpos <= 0)
19651 --end;
19652 if (end > start)
19653 {
19654 if (EQ ((end - 1)->object, it->object))
19655 seen_this_string = 1;
19656 }
19657 else
19658 /* If all the glyphs of the previous row were inserted
19659 by redisplay, it means the previous row was
19660 produced from a single newline, which is only
19661 possible if that newline came from the same string
19662 as the one which produced this ROW. */
19663 seen_this_string = 1;
19664 }
19665 else
19666 {
19667 end = r1->glyphs[TEXT_AREA] - 1;
19668 start = end + r1->used[TEXT_AREA];
19669 while (end < start
19670 && INTEGERP ((end + 1)->object)
19671 && (end + 1)->charpos <= 0)
19672 ++end;
19673 if (end < start)
19674 {
19675 if (EQ ((end + 1)->object, it->object))
19676 seen_this_string = 1;
19677 }
19678 else
19679 seen_this_string = 1;
19680 }
19681 }
19682 /* Take note of each display string that covers a newline only
19683 once, the first time we see it. This is for when a display
19684 string includes more than one newline in it. */
19685 if (row->ends_in_newline_from_string_p && !seen_this_string)
19686 {
19687 /* If we were scanning the buffer forward when we displayed
19688 the string, we want to account for at least one buffer
19689 position that belongs to this row (position covered by
19690 the display string), so that cursor positioning will
19691 consider this row as a candidate when point is at the end
19692 of the visual line represented by this row. This is not
19693 required when scanning back, because max_pos will already
19694 have a much larger value. */
19695 if (CHARPOS (row->end.pos) > max_pos)
19696 INC_BOTH (max_pos, max_bpos);
19697 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19698 }
19699 else if (CHARPOS (it->eol_pos) > 0)
19700 SET_TEXT_POS (row->maxpos,
19701 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19702 else if (row->continued_p)
19703 {
19704 /* If max_pos is different from IT's current position, it
19705 means IT->method does not belong to the display element
19706 at max_pos. However, it also means that the display
19707 element at max_pos was displayed in its entirety on this
19708 line, which is equivalent to saying that the next line
19709 starts at the next buffer position. */
19710 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19711 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19712 else
19713 {
19714 INC_BOTH (max_pos, max_bpos);
19715 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19716 }
19717 }
19718 else if (row->truncated_on_right_p)
19719 /* display_line already called reseat_at_next_visible_line_start,
19720 which puts the iterator at the beginning of the next line, in
19721 the logical order. */
19722 row->maxpos = it->current.pos;
19723 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19724 /* A line that is entirely from a string/image/stretch... */
19725 row->maxpos = row->minpos;
19726 else
19727 emacs_abort ();
19728 }
19729 else
19730 row->maxpos = it->current.pos;
19731 }
19732
19733 /* Construct the glyph row IT->glyph_row in the desired matrix of
19734 IT->w from text at the current position of IT. See dispextern.h
19735 for an overview of struct it. Value is non-zero if
19736 IT->glyph_row displays text, as opposed to a line displaying ZV
19737 only. */
19738
19739 static int
19740 display_line (struct it *it)
19741 {
19742 struct glyph_row *row = it->glyph_row;
19743 Lisp_Object overlay_arrow_string;
19744 struct it wrap_it;
19745 void *wrap_data = NULL;
19746 int may_wrap = 0, wrap_x IF_LINT (= 0);
19747 int wrap_row_used = -1;
19748 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19749 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19750 int wrap_row_extra_line_spacing IF_LINT (= 0);
19751 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19752 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19753 int cvpos;
19754 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19755 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19756
19757 /* We always start displaying at hpos zero even if hscrolled. */
19758 eassert (it->hpos == 0 && it->current_x == 0);
19759
19760 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19761 >= it->w->desired_matrix->nrows)
19762 {
19763 it->w->nrows_scale_factor++;
19764 it->f->fonts_changed = 1;
19765 return 0;
19766 }
19767
19768 /* Clear the result glyph row and enable it. */
19769 prepare_desired_row (row);
19770
19771 row->y = it->current_y;
19772 row->start = it->start;
19773 row->continuation_lines_width = it->continuation_lines_width;
19774 row->displays_text_p = 1;
19775 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19776 it->starts_in_middle_of_char_p = 0;
19777
19778 /* Arrange the overlays nicely for our purposes. Usually, we call
19779 display_line on only one line at a time, in which case this
19780 can't really hurt too much, or we call it on lines which appear
19781 one after another in the buffer, in which case all calls to
19782 recenter_overlay_lists but the first will be pretty cheap. */
19783 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19784
19785 /* Move over display elements that are not visible because we are
19786 hscrolled. This may stop at an x-position < IT->first_visible_x
19787 if the first glyph is partially visible or if we hit a line end. */
19788 if (it->current_x < it->first_visible_x)
19789 {
19790 enum move_it_result move_result;
19791
19792 this_line_min_pos = row->start.pos;
19793 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19794 MOVE_TO_POS | MOVE_TO_X);
19795 /* If we are under a large hscroll, move_it_in_display_line_to
19796 could hit the end of the line without reaching
19797 it->first_visible_x. Pretend that we did reach it. This is
19798 especially important on a TTY, where we will call
19799 extend_face_to_end_of_line, which needs to know how many
19800 blank glyphs to produce. */
19801 if (it->current_x < it->first_visible_x
19802 && (move_result == MOVE_NEWLINE_OR_CR
19803 || move_result == MOVE_POS_MATCH_OR_ZV))
19804 it->current_x = it->first_visible_x;
19805
19806 /* Record the smallest positions seen while we moved over
19807 display elements that are not visible. This is needed by
19808 redisplay_internal for optimizing the case where the cursor
19809 stays inside the same line. The rest of this function only
19810 considers positions that are actually displayed, so
19811 RECORD_MAX_MIN_POS will not otherwise record positions that
19812 are hscrolled to the left of the left edge of the window. */
19813 min_pos = CHARPOS (this_line_min_pos);
19814 min_bpos = BYTEPOS (this_line_min_pos);
19815 }
19816 else
19817 {
19818 /* We only do this when not calling `move_it_in_display_line_to'
19819 above, because move_it_in_display_line_to calls
19820 handle_line_prefix itself. */
19821 handle_line_prefix (it);
19822 }
19823
19824 /* Get the initial row height. This is either the height of the
19825 text hscrolled, if there is any, or zero. */
19826 row->ascent = it->max_ascent;
19827 row->height = it->max_ascent + it->max_descent;
19828 row->phys_ascent = it->max_phys_ascent;
19829 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19830 row->extra_line_spacing = it->max_extra_line_spacing;
19831
19832 /* Utility macro to record max and min buffer positions seen until now. */
19833 #define RECORD_MAX_MIN_POS(IT) \
19834 do \
19835 { \
19836 int composition_p = !STRINGP ((IT)->string) \
19837 && ((IT)->what == IT_COMPOSITION); \
19838 ptrdiff_t current_pos = \
19839 composition_p ? (IT)->cmp_it.charpos \
19840 : IT_CHARPOS (*(IT)); \
19841 ptrdiff_t current_bpos = \
19842 composition_p ? CHAR_TO_BYTE (current_pos) \
19843 : IT_BYTEPOS (*(IT)); \
19844 if (current_pos < min_pos) \
19845 { \
19846 min_pos = current_pos; \
19847 min_bpos = current_bpos; \
19848 } \
19849 if (IT_CHARPOS (*it) > max_pos) \
19850 { \
19851 max_pos = IT_CHARPOS (*it); \
19852 max_bpos = IT_BYTEPOS (*it); \
19853 } \
19854 } \
19855 while (0)
19856
19857 /* Loop generating characters. The loop is left with IT on the next
19858 character to display. */
19859 while (1)
19860 {
19861 int n_glyphs_before, hpos_before, x_before;
19862 int x, nglyphs;
19863 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19864
19865 /* Retrieve the next thing to display. Value is zero if end of
19866 buffer reached. */
19867 if (!get_next_display_element (it))
19868 {
19869 /* Maybe add a space at the end of this line that is used to
19870 display the cursor there under X. Set the charpos of the
19871 first glyph of blank lines not corresponding to any text
19872 to -1. */
19873 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19874 row->exact_window_width_line_p = 1;
19875 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19876 || row->used[TEXT_AREA] == 0)
19877 {
19878 row->glyphs[TEXT_AREA]->charpos = -1;
19879 row->displays_text_p = 0;
19880
19881 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19882 && (!MINI_WINDOW_P (it->w)
19883 || (minibuf_level && EQ (it->window, minibuf_window))))
19884 row->indicate_empty_line_p = 1;
19885 }
19886
19887 it->continuation_lines_width = 0;
19888 row->ends_at_zv_p = 1;
19889 /* A row that displays right-to-left text must always have
19890 its last face extended all the way to the end of line,
19891 even if this row ends in ZV, because we still write to
19892 the screen left to right. We also need to extend the
19893 last face if the default face is remapped to some
19894 different face, otherwise the functions that clear
19895 portions of the screen will clear with the default face's
19896 background color. */
19897 if (row->reversed_p
19898 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19899 extend_face_to_end_of_line (it);
19900 break;
19901 }
19902
19903 /* Now, get the metrics of what we want to display. This also
19904 generates glyphs in `row' (which is IT->glyph_row). */
19905 n_glyphs_before = row->used[TEXT_AREA];
19906 x = it->current_x;
19907
19908 /* Remember the line height so far in case the next element doesn't
19909 fit on the line. */
19910 if (it->line_wrap != TRUNCATE)
19911 {
19912 ascent = it->max_ascent;
19913 descent = it->max_descent;
19914 phys_ascent = it->max_phys_ascent;
19915 phys_descent = it->max_phys_descent;
19916
19917 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19918 {
19919 if (IT_DISPLAYING_WHITESPACE (it))
19920 may_wrap = 1;
19921 else if (may_wrap)
19922 {
19923 SAVE_IT (wrap_it, *it, wrap_data);
19924 wrap_x = x;
19925 wrap_row_used = row->used[TEXT_AREA];
19926 wrap_row_ascent = row->ascent;
19927 wrap_row_height = row->height;
19928 wrap_row_phys_ascent = row->phys_ascent;
19929 wrap_row_phys_height = row->phys_height;
19930 wrap_row_extra_line_spacing = row->extra_line_spacing;
19931 wrap_row_min_pos = min_pos;
19932 wrap_row_min_bpos = min_bpos;
19933 wrap_row_max_pos = max_pos;
19934 wrap_row_max_bpos = max_bpos;
19935 may_wrap = 0;
19936 }
19937 }
19938 }
19939
19940 PRODUCE_GLYPHS (it);
19941
19942 /* If this display element was in marginal areas, continue with
19943 the next one. */
19944 if (it->area != TEXT_AREA)
19945 {
19946 row->ascent = max (row->ascent, it->max_ascent);
19947 row->height = max (row->height, it->max_ascent + it->max_descent);
19948 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19949 row->phys_height = max (row->phys_height,
19950 it->max_phys_ascent + it->max_phys_descent);
19951 row->extra_line_spacing = max (row->extra_line_spacing,
19952 it->max_extra_line_spacing);
19953 set_iterator_to_next (it, 1);
19954 continue;
19955 }
19956
19957 /* Does the display element fit on the line? If we truncate
19958 lines, we should draw past the right edge of the window. If
19959 we don't truncate, we want to stop so that we can display the
19960 continuation glyph before the right margin. If lines are
19961 continued, there are two possible strategies for characters
19962 resulting in more than 1 glyph (e.g. tabs): Display as many
19963 glyphs as possible in this line and leave the rest for the
19964 continuation line, or display the whole element in the next
19965 line. Original redisplay did the former, so we do it also. */
19966 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19967 hpos_before = it->hpos;
19968 x_before = x;
19969
19970 if (/* Not a newline. */
19971 nglyphs > 0
19972 /* Glyphs produced fit entirely in the line. */
19973 && it->current_x < it->last_visible_x)
19974 {
19975 it->hpos += nglyphs;
19976 row->ascent = max (row->ascent, it->max_ascent);
19977 row->height = max (row->height, it->max_ascent + it->max_descent);
19978 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19979 row->phys_height = max (row->phys_height,
19980 it->max_phys_ascent + it->max_phys_descent);
19981 row->extra_line_spacing = max (row->extra_line_spacing,
19982 it->max_extra_line_spacing);
19983 if (it->current_x - it->pixel_width < it->first_visible_x)
19984 row->x = x - it->first_visible_x;
19985 /* Record the maximum and minimum buffer positions seen so
19986 far in glyphs that will be displayed by this row. */
19987 if (it->bidi_p)
19988 RECORD_MAX_MIN_POS (it);
19989 }
19990 else
19991 {
19992 int i, new_x;
19993 struct glyph *glyph;
19994
19995 for (i = 0; i < nglyphs; ++i, x = new_x)
19996 {
19997 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19998 new_x = x + glyph->pixel_width;
19999
20000 if (/* Lines are continued. */
20001 it->line_wrap != TRUNCATE
20002 && (/* Glyph doesn't fit on the line. */
20003 new_x > it->last_visible_x
20004 /* Or it fits exactly on a window system frame. */
20005 || (new_x == it->last_visible_x
20006 && FRAME_WINDOW_P (it->f)
20007 && (row->reversed_p
20008 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20009 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20010 {
20011 /* End of a continued line. */
20012
20013 if (it->hpos == 0
20014 || (new_x == it->last_visible_x
20015 && FRAME_WINDOW_P (it->f)
20016 && (row->reversed_p
20017 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20018 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20019 {
20020 /* Current glyph is the only one on the line or
20021 fits exactly on the line. We must continue
20022 the line because we can't draw the cursor
20023 after the glyph. */
20024 row->continued_p = 1;
20025 it->current_x = new_x;
20026 it->continuation_lines_width += new_x;
20027 ++it->hpos;
20028 if (i == nglyphs - 1)
20029 {
20030 /* If line-wrap is on, check if a previous
20031 wrap point was found. */
20032 if (wrap_row_used > 0
20033 /* Even if there is a previous wrap
20034 point, continue the line here as
20035 usual, if (i) the previous character
20036 was a space or tab AND (ii) the
20037 current character is not. */
20038 && (!may_wrap
20039 || IT_DISPLAYING_WHITESPACE (it)))
20040 goto back_to_wrap;
20041
20042 /* Record the maximum and minimum buffer
20043 positions seen so far in glyphs that will be
20044 displayed by this row. */
20045 if (it->bidi_p)
20046 RECORD_MAX_MIN_POS (it);
20047 set_iterator_to_next (it, 1);
20048 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20049 {
20050 if (!get_next_display_element (it))
20051 {
20052 row->exact_window_width_line_p = 1;
20053 it->continuation_lines_width = 0;
20054 row->continued_p = 0;
20055 row->ends_at_zv_p = 1;
20056 }
20057 else if (ITERATOR_AT_END_OF_LINE_P (it))
20058 {
20059 row->continued_p = 0;
20060 row->exact_window_width_line_p = 1;
20061 }
20062 }
20063 }
20064 else if (it->bidi_p)
20065 RECORD_MAX_MIN_POS (it);
20066 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20067 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20068 extend_face_to_end_of_line (it);
20069 }
20070 else if (CHAR_GLYPH_PADDING_P (*glyph)
20071 && !FRAME_WINDOW_P (it->f))
20072 {
20073 /* A padding glyph that doesn't fit on this line.
20074 This means the whole character doesn't fit
20075 on the line. */
20076 if (row->reversed_p)
20077 unproduce_glyphs (it, row->used[TEXT_AREA]
20078 - n_glyphs_before);
20079 row->used[TEXT_AREA] = n_glyphs_before;
20080
20081 /* Fill the rest of the row with continuation
20082 glyphs like in 20.x. */
20083 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20084 < row->glyphs[1 + TEXT_AREA])
20085 produce_special_glyphs (it, IT_CONTINUATION);
20086
20087 row->continued_p = 1;
20088 it->current_x = x_before;
20089 it->continuation_lines_width += x_before;
20090
20091 /* Restore the height to what it was before the
20092 element not fitting on the line. */
20093 it->max_ascent = ascent;
20094 it->max_descent = descent;
20095 it->max_phys_ascent = phys_ascent;
20096 it->max_phys_descent = phys_descent;
20097 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20098 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20099 extend_face_to_end_of_line (it);
20100 }
20101 else if (wrap_row_used > 0)
20102 {
20103 back_to_wrap:
20104 if (row->reversed_p)
20105 unproduce_glyphs (it,
20106 row->used[TEXT_AREA] - wrap_row_used);
20107 RESTORE_IT (it, &wrap_it, wrap_data);
20108 it->continuation_lines_width += wrap_x;
20109 row->used[TEXT_AREA] = wrap_row_used;
20110 row->ascent = wrap_row_ascent;
20111 row->height = wrap_row_height;
20112 row->phys_ascent = wrap_row_phys_ascent;
20113 row->phys_height = wrap_row_phys_height;
20114 row->extra_line_spacing = wrap_row_extra_line_spacing;
20115 min_pos = wrap_row_min_pos;
20116 min_bpos = wrap_row_min_bpos;
20117 max_pos = wrap_row_max_pos;
20118 max_bpos = wrap_row_max_bpos;
20119 row->continued_p = 1;
20120 row->ends_at_zv_p = 0;
20121 row->exact_window_width_line_p = 0;
20122 it->continuation_lines_width += x;
20123
20124 /* Make sure that a non-default face is extended
20125 up to the right margin of the window. */
20126 extend_face_to_end_of_line (it);
20127 }
20128 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20129 {
20130 /* A TAB that extends past the right edge of the
20131 window. This produces a single glyph on
20132 window system frames. We leave the glyph in
20133 this row and let it fill the row, but don't
20134 consume the TAB. */
20135 if ((row->reversed_p
20136 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20137 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20138 produce_special_glyphs (it, IT_CONTINUATION);
20139 it->continuation_lines_width += it->last_visible_x;
20140 row->ends_in_middle_of_char_p = 1;
20141 row->continued_p = 1;
20142 glyph->pixel_width = it->last_visible_x - x;
20143 it->starts_in_middle_of_char_p = 1;
20144 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20145 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20146 extend_face_to_end_of_line (it);
20147 }
20148 else
20149 {
20150 /* Something other than a TAB that draws past
20151 the right edge of the window. Restore
20152 positions to values before the element. */
20153 if (row->reversed_p)
20154 unproduce_glyphs (it, row->used[TEXT_AREA]
20155 - (n_glyphs_before + i));
20156 row->used[TEXT_AREA] = n_glyphs_before + i;
20157
20158 /* Display continuation glyphs. */
20159 it->current_x = x_before;
20160 it->continuation_lines_width += x;
20161 if (!FRAME_WINDOW_P (it->f)
20162 || (row->reversed_p
20163 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20164 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20165 produce_special_glyphs (it, IT_CONTINUATION);
20166 row->continued_p = 1;
20167
20168 extend_face_to_end_of_line (it);
20169
20170 if (nglyphs > 1 && i > 0)
20171 {
20172 row->ends_in_middle_of_char_p = 1;
20173 it->starts_in_middle_of_char_p = 1;
20174 }
20175
20176 /* Restore the height to what it was before the
20177 element not fitting on the line. */
20178 it->max_ascent = ascent;
20179 it->max_descent = descent;
20180 it->max_phys_ascent = phys_ascent;
20181 it->max_phys_descent = phys_descent;
20182 }
20183
20184 break;
20185 }
20186 else if (new_x > it->first_visible_x)
20187 {
20188 /* Increment number of glyphs actually displayed. */
20189 ++it->hpos;
20190
20191 /* Record the maximum and minimum buffer positions
20192 seen so far in glyphs that will be displayed by
20193 this row. */
20194 if (it->bidi_p)
20195 RECORD_MAX_MIN_POS (it);
20196
20197 if (x < it->first_visible_x)
20198 /* Glyph is partially visible, i.e. row starts at
20199 negative X position. */
20200 row->x = x - it->first_visible_x;
20201 }
20202 else
20203 {
20204 /* Glyph is completely off the left margin of the
20205 window. This should not happen because of the
20206 move_it_in_display_line at the start of this
20207 function, unless the text display area of the
20208 window is empty. */
20209 eassert (it->first_visible_x <= it->last_visible_x);
20210 }
20211 }
20212 /* Even if this display element produced no glyphs at all,
20213 we want to record its position. */
20214 if (it->bidi_p && nglyphs == 0)
20215 RECORD_MAX_MIN_POS (it);
20216
20217 row->ascent = max (row->ascent, it->max_ascent);
20218 row->height = max (row->height, it->max_ascent + it->max_descent);
20219 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20220 row->phys_height = max (row->phys_height,
20221 it->max_phys_ascent + it->max_phys_descent);
20222 row->extra_line_spacing = max (row->extra_line_spacing,
20223 it->max_extra_line_spacing);
20224
20225 /* End of this display line if row is continued. */
20226 if (row->continued_p || row->ends_at_zv_p)
20227 break;
20228 }
20229
20230 at_end_of_line:
20231 /* Is this a line end? If yes, we're also done, after making
20232 sure that a non-default face is extended up to the right
20233 margin of the window. */
20234 if (ITERATOR_AT_END_OF_LINE_P (it))
20235 {
20236 int used_before = row->used[TEXT_AREA];
20237
20238 row->ends_in_newline_from_string_p = STRINGP (it->object);
20239
20240 /* Add a space at the end of the line that is used to
20241 display the cursor there. */
20242 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20243 append_space_for_newline (it, 0);
20244
20245 /* Extend the face to the end of the line. */
20246 extend_face_to_end_of_line (it);
20247
20248 /* Make sure we have the position. */
20249 if (used_before == 0)
20250 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20251
20252 /* Record the position of the newline, for use in
20253 find_row_edges. */
20254 it->eol_pos = it->current.pos;
20255
20256 /* Consume the line end. This skips over invisible lines. */
20257 set_iterator_to_next (it, 1);
20258 it->continuation_lines_width = 0;
20259 break;
20260 }
20261
20262 /* Proceed with next display element. Note that this skips
20263 over lines invisible because of selective display. */
20264 set_iterator_to_next (it, 1);
20265
20266 /* If we truncate lines, we are done when the last displayed
20267 glyphs reach past the right margin of the window. */
20268 if (it->line_wrap == TRUNCATE
20269 && ((FRAME_WINDOW_P (it->f)
20270 /* Images are preprocessed in produce_image_glyph such
20271 that they are cropped at the right edge of the
20272 window, so an image glyph will always end exactly at
20273 last_visible_x, even if there's no right fringe. */
20274 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20275 ? (it->current_x >= it->last_visible_x)
20276 : (it->current_x > it->last_visible_x)))
20277 {
20278 /* Maybe add truncation glyphs. */
20279 if (!FRAME_WINDOW_P (it->f)
20280 || (row->reversed_p
20281 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20282 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20283 {
20284 int i, n;
20285
20286 if (!row->reversed_p)
20287 {
20288 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20289 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20290 break;
20291 }
20292 else
20293 {
20294 for (i = 0; i < row->used[TEXT_AREA]; i++)
20295 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20296 break;
20297 /* Remove any padding glyphs at the front of ROW, to
20298 make room for the truncation glyphs we will be
20299 adding below. The loop below always inserts at
20300 least one truncation glyph, so also remove the
20301 last glyph added to ROW. */
20302 unproduce_glyphs (it, i + 1);
20303 /* Adjust i for the loop below. */
20304 i = row->used[TEXT_AREA] - (i + 1);
20305 }
20306
20307 /* produce_special_glyphs overwrites the last glyph, so
20308 we don't want that if we want to keep that last
20309 glyph, which means it's an image. */
20310 if (it->current_x > it->last_visible_x)
20311 {
20312 it->current_x = x_before;
20313 if (!FRAME_WINDOW_P (it->f))
20314 {
20315 for (n = row->used[TEXT_AREA]; i < n; ++i)
20316 {
20317 row->used[TEXT_AREA] = i;
20318 produce_special_glyphs (it, IT_TRUNCATION);
20319 }
20320 }
20321 else
20322 {
20323 row->used[TEXT_AREA] = i;
20324 produce_special_glyphs (it, IT_TRUNCATION);
20325 }
20326 it->hpos = hpos_before;
20327 }
20328 }
20329 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20330 {
20331 /* Don't truncate if we can overflow newline into fringe. */
20332 if (!get_next_display_element (it))
20333 {
20334 it->continuation_lines_width = 0;
20335 row->ends_at_zv_p = 1;
20336 row->exact_window_width_line_p = 1;
20337 break;
20338 }
20339 if (ITERATOR_AT_END_OF_LINE_P (it))
20340 {
20341 row->exact_window_width_line_p = 1;
20342 goto at_end_of_line;
20343 }
20344 it->current_x = x_before;
20345 it->hpos = hpos_before;
20346 }
20347
20348 row->truncated_on_right_p = 1;
20349 it->continuation_lines_width = 0;
20350 reseat_at_next_visible_line_start (it, 0);
20351 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20352 break;
20353 }
20354 }
20355
20356 if (wrap_data)
20357 bidi_unshelve_cache (wrap_data, 1);
20358
20359 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20360 at the left window margin. */
20361 if (it->first_visible_x
20362 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20363 {
20364 if (!FRAME_WINDOW_P (it->f)
20365 || (((row->reversed_p
20366 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20367 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20368 /* Don't let insert_left_trunc_glyphs overwrite the
20369 first glyph of the row if it is an image. */
20370 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20371 insert_left_trunc_glyphs (it);
20372 row->truncated_on_left_p = 1;
20373 }
20374
20375 /* Remember the position at which this line ends.
20376
20377 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20378 cannot be before the call to find_row_edges below, since that is
20379 where these positions are determined. */
20380 row->end = it->current;
20381 if (!it->bidi_p)
20382 {
20383 row->minpos = row->start.pos;
20384 row->maxpos = row->end.pos;
20385 }
20386 else
20387 {
20388 /* ROW->minpos and ROW->maxpos must be the smallest and
20389 `1 + the largest' buffer positions in ROW. But if ROW was
20390 bidi-reordered, these two positions can be anywhere in the
20391 row, so we must determine them now. */
20392 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20393 }
20394
20395 /* If the start of this line is the overlay arrow-position, then
20396 mark this glyph row as the one containing the overlay arrow.
20397 This is clearly a mess with variable size fonts. It would be
20398 better to let it be displayed like cursors under X. */
20399 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20400 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20401 !NILP (overlay_arrow_string)))
20402 {
20403 /* Overlay arrow in window redisplay is a fringe bitmap. */
20404 if (STRINGP (overlay_arrow_string))
20405 {
20406 struct glyph_row *arrow_row
20407 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20408 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20409 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20410 struct glyph *p = row->glyphs[TEXT_AREA];
20411 struct glyph *p2, *end;
20412
20413 /* Copy the arrow glyphs. */
20414 while (glyph < arrow_end)
20415 *p++ = *glyph++;
20416
20417 /* Throw away padding glyphs. */
20418 p2 = p;
20419 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20420 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20421 ++p2;
20422 if (p2 > p)
20423 {
20424 while (p2 < end)
20425 *p++ = *p2++;
20426 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20427 }
20428 }
20429 else
20430 {
20431 eassert (INTEGERP (overlay_arrow_string));
20432 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20433 }
20434 overlay_arrow_seen = 1;
20435 }
20436
20437 /* Highlight trailing whitespace. */
20438 if (!NILP (Vshow_trailing_whitespace))
20439 highlight_trailing_whitespace (it->f, it->glyph_row);
20440
20441 /* Compute pixel dimensions of this line. */
20442 compute_line_metrics (it);
20443
20444 /* Implementation note: No changes in the glyphs of ROW or in their
20445 faces can be done past this point, because compute_line_metrics
20446 computes ROW's hash value and stores it within the glyph_row
20447 structure. */
20448
20449 /* Record whether this row ends inside an ellipsis. */
20450 row->ends_in_ellipsis_p
20451 = (it->method == GET_FROM_DISPLAY_VECTOR
20452 && it->ellipsis_p);
20453
20454 /* Save fringe bitmaps in this row. */
20455 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20456 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20457 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20458 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20459
20460 it->left_user_fringe_bitmap = 0;
20461 it->left_user_fringe_face_id = 0;
20462 it->right_user_fringe_bitmap = 0;
20463 it->right_user_fringe_face_id = 0;
20464
20465 /* Maybe set the cursor. */
20466 cvpos = it->w->cursor.vpos;
20467 if ((cvpos < 0
20468 /* In bidi-reordered rows, keep checking for proper cursor
20469 position even if one has been found already, because buffer
20470 positions in such rows change non-linearly with ROW->VPOS,
20471 when a line is continued. One exception: when we are at ZV,
20472 display cursor on the first suitable glyph row, since all
20473 the empty rows after that also have their position set to ZV. */
20474 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20475 lines' rows is implemented for bidi-reordered rows. */
20476 || (it->bidi_p
20477 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20478 && PT >= MATRIX_ROW_START_CHARPOS (row)
20479 && PT <= MATRIX_ROW_END_CHARPOS (row)
20480 && cursor_row_p (row))
20481 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20482
20483 /* Prepare for the next line. This line starts horizontally at (X
20484 HPOS) = (0 0). Vertical positions are incremented. As a
20485 convenience for the caller, IT->glyph_row is set to the next
20486 row to be used. */
20487 it->current_x = it->hpos = 0;
20488 it->current_y += row->height;
20489 SET_TEXT_POS (it->eol_pos, 0, 0);
20490 ++it->vpos;
20491 ++it->glyph_row;
20492 /* The next row should by default use the same value of the
20493 reversed_p flag as this one. set_iterator_to_next decides when
20494 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20495 the flag accordingly. */
20496 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20497 it->glyph_row->reversed_p = row->reversed_p;
20498 it->start = row->end;
20499 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20500
20501 #undef RECORD_MAX_MIN_POS
20502 }
20503
20504 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20505 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20506 doc: /* Return paragraph direction at point in BUFFER.
20507 Value is either `left-to-right' or `right-to-left'.
20508 If BUFFER is omitted or nil, it defaults to the current buffer.
20509
20510 Paragraph direction determines how the text in the paragraph is displayed.
20511 In left-to-right paragraphs, text begins at the left margin of the window
20512 and the reading direction is generally left to right. In right-to-left
20513 paragraphs, text begins at the right margin and is read from right to left.
20514
20515 See also `bidi-paragraph-direction'. */)
20516 (Lisp_Object buffer)
20517 {
20518 struct buffer *buf = current_buffer;
20519 struct buffer *old = buf;
20520
20521 if (! NILP (buffer))
20522 {
20523 CHECK_BUFFER (buffer);
20524 buf = XBUFFER (buffer);
20525 }
20526
20527 if (NILP (BVAR (buf, bidi_display_reordering))
20528 || NILP (BVAR (buf, enable_multibyte_characters))
20529 /* When we are loading loadup.el, the character property tables
20530 needed for bidi iteration are not yet available. */
20531 || !NILP (Vpurify_flag))
20532 return Qleft_to_right;
20533 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20534 return BVAR (buf, bidi_paragraph_direction);
20535 else
20536 {
20537 /* Determine the direction from buffer text. We could try to
20538 use current_matrix if it is up to date, but this seems fast
20539 enough as it is. */
20540 struct bidi_it itb;
20541 ptrdiff_t pos = BUF_PT (buf);
20542 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20543 int c;
20544 void *itb_data = bidi_shelve_cache ();
20545
20546 set_buffer_temp (buf);
20547 /* bidi_paragraph_init finds the base direction of the paragraph
20548 by searching forward from paragraph start. We need the base
20549 direction of the current or _previous_ paragraph, so we need
20550 to make sure we are within that paragraph. To that end, find
20551 the previous non-empty line. */
20552 if (pos >= ZV && pos > BEGV)
20553 DEC_BOTH (pos, bytepos);
20554 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20555 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20556 {
20557 while ((c = FETCH_BYTE (bytepos)) == '\n'
20558 || c == ' ' || c == '\t' || c == '\f')
20559 {
20560 if (bytepos <= BEGV_BYTE)
20561 break;
20562 bytepos--;
20563 pos--;
20564 }
20565 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20566 bytepos--;
20567 }
20568 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20569 itb.paragraph_dir = NEUTRAL_DIR;
20570 itb.string.s = NULL;
20571 itb.string.lstring = Qnil;
20572 itb.string.bufpos = 0;
20573 itb.string.from_disp_str = 0;
20574 itb.string.unibyte = 0;
20575 /* We have no window to use here for ignoring window-specific
20576 overlays. Using NULL for window pointer will cause
20577 compute_display_string_pos to use the current buffer. */
20578 itb.w = NULL;
20579 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20580 bidi_unshelve_cache (itb_data, 0);
20581 set_buffer_temp (old);
20582 switch (itb.paragraph_dir)
20583 {
20584 case L2R:
20585 return Qleft_to_right;
20586 break;
20587 case R2L:
20588 return Qright_to_left;
20589 break;
20590 default:
20591 emacs_abort ();
20592 }
20593 }
20594 }
20595
20596 DEFUN ("move-point-visually", Fmove_point_visually,
20597 Smove_point_visually, 1, 1, 0,
20598 doc: /* Move point in the visual order in the specified DIRECTION.
20599 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20600 left.
20601
20602 Value is the new character position of point. */)
20603 (Lisp_Object direction)
20604 {
20605 struct window *w = XWINDOW (selected_window);
20606 struct buffer *b = XBUFFER (w->contents);
20607 struct glyph_row *row;
20608 int dir;
20609 Lisp_Object paragraph_dir;
20610
20611 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20612 (!(ROW)->continued_p \
20613 && INTEGERP ((GLYPH)->object) \
20614 && (GLYPH)->type == CHAR_GLYPH \
20615 && (GLYPH)->u.ch == ' ' \
20616 && (GLYPH)->charpos >= 0 \
20617 && !(GLYPH)->avoid_cursor_p)
20618
20619 CHECK_NUMBER (direction);
20620 dir = XINT (direction);
20621 if (dir > 0)
20622 dir = 1;
20623 else
20624 dir = -1;
20625
20626 /* If current matrix is up-to-date, we can use the information
20627 recorded in the glyphs, at least as long as the goal is on the
20628 screen. */
20629 if (w->window_end_valid
20630 && !windows_or_buffers_changed
20631 && b
20632 && !b->clip_changed
20633 && !b->prevent_redisplay_optimizations_p
20634 && !window_outdated (w)
20635 && w->cursor.vpos >= 0
20636 && w->cursor.vpos < w->current_matrix->nrows
20637 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20638 {
20639 struct glyph *g = row->glyphs[TEXT_AREA];
20640 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20641 struct glyph *gpt = g + w->cursor.hpos;
20642
20643 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20644 {
20645 if (BUFFERP (g->object) && g->charpos != PT)
20646 {
20647 SET_PT (g->charpos);
20648 w->cursor.vpos = -1;
20649 return make_number (PT);
20650 }
20651 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20652 {
20653 ptrdiff_t new_pos;
20654
20655 if (BUFFERP (gpt->object))
20656 {
20657 new_pos = PT;
20658 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20659 new_pos += (row->reversed_p ? -dir : dir);
20660 else
20661 new_pos -= (row->reversed_p ? -dir : dir);;
20662 }
20663 else if (BUFFERP (g->object))
20664 new_pos = g->charpos;
20665 else
20666 break;
20667 SET_PT (new_pos);
20668 w->cursor.vpos = -1;
20669 return make_number (PT);
20670 }
20671 else if (ROW_GLYPH_NEWLINE_P (row, g))
20672 {
20673 /* Glyphs inserted at the end of a non-empty line for
20674 positioning the cursor have zero charpos, so we must
20675 deduce the value of point by other means. */
20676 if (g->charpos > 0)
20677 SET_PT (g->charpos);
20678 else if (row->ends_at_zv_p && PT != ZV)
20679 SET_PT (ZV);
20680 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20681 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20682 else
20683 break;
20684 w->cursor.vpos = -1;
20685 return make_number (PT);
20686 }
20687 }
20688 if (g == e || INTEGERP (g->object))
20689 {
20690 if (row->truncated_on_left_p || row->truncated_on_right_p)
20691 goto simulate_display;
20692 if (!row->reversed_p)
20693 row += dir;
20694 else
20695 row -= dir;
20696 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20697 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20698 goto simulate_display;
20699
20700 if (dir > 0)
20701 {
20702 if (row->reversed_p && !row->continued_p)
20703 {
20704 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20705 w->cursor.vpos = -1;
20706 return make_number (PT);
20707 }
20708 g = row->glyphs[TEXT_AREA];
20709 e = g + row->used[TEXT_AREA];
20710 for ( ; g < e; g++)
20711 {
20712 if (BUFFERP (g->object)
20713 /* Empty lines have only one glyph, which stands
20714 for the newline, and whose charpos is the
20715 buffer position of the newline. */
20716 || ROW_GLYPH_NEWLINE_P (row, g)
20717 /* When the buffer ends in a newline, the line at
20718 EOB also has one glyph, but its charpos is -1. */
20719 || (row->ends_at_zv_p
20720 && !row->reversed_p
20721 && INTEGERP (g->object)
20722 && g->type == CHAR_GLYPH
20723 && g->u.ch == ' '))
20724 {
20725 if (g->charpos > 0)
20726 SET_PT (g->charpos);
20727 else if (!row->reversed_p
20728 && row->ends_at_zv_p
20729 && PT != ZV)
20730 SET_PT (ZV);
20731 else
20732 continue;
20733 w->cursor.vpos = -1;
20734 return make_number (PT);
20735 }
20736 }
20737 }
20738 else
20739 {
20740 if (!row->reversed_p && !row->continued_p)
20741 {
20742 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20743 w->cursor.vpos = -1;
20744 return make_number (PT);
20745 }
20746 e = row->glyphs[TEXT_AREA];
20747 g = e + row->used[TEXT_AREA] - 1;
20748 for ( ; g >= e; g--)
20749 {
20750 if (BUFFERP (g->object)
20751 || (ROW_GLYPH_NEWLINE_P (row, g)
20752 && g->charpos > 0)
20753 /* Empty R2L lines on GUI frames have the buffer
20754 position of the newline stored in the stretch
20755 glyph. */
20756 || g->type == STRETCH_GLYPH
20757 || (row->ends_at_zv_p
20758 && row->reversed_p
20759 && INTEGERP (g->object)
20760 && g->type == CHAR_GLYPH
20761 && g->u.ch == ' '))
20762 {
20763 if (g->charpos > 0)
20764 SET_PT (g->charpos);
20765 else if (row->reversed_p
20766 && row->ends_at_zv_p
20767 && PT != ZV)
20768 SET_PT (ZV);
20769 else
20770 continue;
20771 w->cursor.vpos = -1;
20772 return make_number (PT);
20773 }
20774 }
20775 }
20776 }
20777 }
20778
20779 simulate_display:
20780
20781 /* If we wind up here, we failed to move by using the glyphs, so we
20782 need to simulate display instead. */
20783
20784 if (b)
20785 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20786 else
20787 paragraph_dir = Qleft_to_right;
20788 if (EQ (paragraph_dir, Qright_to_left))
20789 dir = -dir;
20790 if (PT <= BEGV && dir < 0)
20791 xsignal0 (Qbeginning_of_buffer);
20792 else if (PT >= ZV && dir > 0)
20793 xsignal0 (Qend_of_buffer);
20794 else
20795 {
20796 struct text_pos pt;
20797 struct it it;
20798 int pt_x, target_x, pixel_width, pt_vpos;
20799 bool at_eol_p;
20800 bool overshoot_expected = false;
20801 bool target_is_eol_p = false;
20802
20803 /* Setup the arena. */
20804 SET_TEXT_POS (pt, PT, PT_BYTE);
20805 start_display (&it, w, pt);
20806
20807 if (it.cmp_it.id < 0
20808 && it.method == GET_FROM_STRING
20809 && it.area == TEXT_AREA
20810 && it.string_from_display_prop_p
20811 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20812 overshoot_expected = true;
20813
20814 /* Find the X coordinate of point. We start from the beginning
20815 of this or previous line to make sure we are before point in
20816 the logical order (since the move_it_* functions can only
20817 move forward). */
20818 reseat:
20819 reseat_at_previous_visible_line_start (&it);
20820 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20821 if (IT_CHARPOS (it) != PT)
20822 {
20823 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20824 -1, -1, -1, MOVE_TO_POS);
20825 /* If we missed point because the character there is
20826 displayed out of a display vector that has more than one
20827 glyph, retry expecting overshoot. */
20828 if (it.method == GET_FROM_DISPLAY_VECTOR
20829 && it.current.dpvec_index > 0
20830 && !overshoot_expected)
20831 {
20832 overshoot_expected = true;
20833 goto reseat;
20834 }
20835 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20836 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20837 }
20838 pt_x = it.current_x;
20839 pt_vpos = it.vpos;
20840 if (dir > 0 || overshoot_expected)
20841 {
20842 struct glyph_row *row = it.glyph_row;
20843
20844 /* When point is at beginning of line, we don't have
20845 information about the glyph there loaded into struct
20846 it. Calling get_next_display_element fixes that. */
20847 if (pt_x == 0)
20848 get_next_display_element (&it);
20849 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20850 it.glyph_row = NULL;
20851 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20852 it.glyph_row = row;
20853 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20854 it, lest it will become out of sync with it's buffer
20855 position. */
20856 it.current_x = pt_x;
20857 }
20858 else
20859 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20860 pixel_width = it.pixel_width;
20861 if (overshoot_expected && at_eol_p)
20862 pixel_width = 0;
20863 else if (pixel_width <= 0)
20864 pixel_width = 1;
20865
20866 /* If there's a display string (or something similar) at point,
20867 we are actually at the glyph to the left of point, so we need
20868 to correct the X coordinate. */
20869 if (overshoot_expected)
20870 {
20871 if (it.bidi_p)
20872 pt_x += pixel_width * it.bidi_it.scan_dir;
20873 else
20874 pt_x += pixel_width;
20875 }
20876
20877 /* Compute target X coordinate, either to the left or to the
20878 right of point. On TTY frames, all characters have the same
20879 pixel width of 1, so we can use that. On GUI frames we don't
20880 have an easy way of getting at the pixel width of the
20881 character to the left of point, so we use a different method
20882 of getting to that place. */
20883 if (dir > 0)
20884 target_x = pt_x + pixel_width;
20885 else
20886 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20887
20888 /* Target X coordinate could be one line above or below the line
20889 of point, in which case we need to adjust the target X
20890 coordinate. Also, if moving to the left, we need to begin at
20891 the left edge of the point's screen line. */
20892 if (dir < 0)
20893 {
20894 if (pt_x > 0)
20895 {
20896 start_display (&it, w, pt);
20897 reseat_at_previous_visible_line_start (&it);
20898 it.current_x = it.current_y = it.hpos = 0;
20899 if (pt_vpos != 0)
20900 move_it_by_lines (&it, pt_vpos);
20901 }
20902 else
20903 {
20904 move_it_by_lines (&it, -1);
20905 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20906 target_is_eol_p = true;
20907 /* Under word-wrap, we don't know the x coordinate of
20908 the last character displayed on the previous line,
20909 which immediately precedes the wrap point. To find
20910 out its x coordinate, we try moving to the right
20911 margin of the window, which will stop at the wrap
20912 point, and then reset target_x to point at the
20913 character that precedes the wrap point. This is not
20914 needed on GUI frames, because (see below) there we
20915 move from the left margin one grapheme cluster at a
20916 time, and stop when we hit the wrap point. */
20917 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
20918 {
20919 void *it_data = NULL;
20920 struct it it2;
20921
20922 SAVE_IT (it2, it, it_data);
20923 move_it_in_display_line_to (&it, ZV, target_x,
20924 MOVE_TO_POS | MOVE_TO_X);
20925 /* If we arrived at target_x, that _is_ the last
20926 character on the previous line. */
20927 if (it.current_x != target_x)
20928 target_x = it.current_x - 1;
20929 RESTORE_IT (&it, &it2, it_data);
20930 }
20931 }
20932 }
20933 else
20934 {
20935 if (at_eol_p
20936 || (target_x >= it.last_visible_x
20937 && it.line_wrap != TRUNCATE))
20938 {
20939 if (pt_x > 0)
20940 move_it_by_lines (&it, 0);
20941 move_it_by_lines (&it, 1);
20942 target_x = 0;
20943 }
20944 }
20945
20946 /* Move to the target X coordinate. */
20947 #ifdef HAVE_WINDOW_SYSTEM
20948 /* On GUI frames, as we don't know the X coordinate of the
20949 character to the left of point, moving point to the left
20950 requires walking, one grapheme cluster at a time, until we
20951 find ourself at a place immediately to the left of the
20952 character at point. */
20953 if (FRAME_WINDOW_P (it.f) && dir < 0)
20954 {
20955 struct text_pos new_pos;
20956 enum move_it_result rc = MOVE_X_REACHED;
20957
20958 if (it.current_x == 0)
20959 get_next_display_element (&it);
20960 if (it.what == IT_COMPOSITION)
20961 {
20962 new_pos.charpos = it.cmp_it.charpos;
20963 new_pos.bytepos = -1;
20964 }
20965 else
20966 new_pos = it.current.pos;
20967
20968 while (it.current_x + it.pixel_width <= target_x
20969 && (rc == MOVE_X_REACHED
20970 /* Under word-wrap, move_it_in_display_line_to
20971 stops at correct coordinates, but sometimes
20972 returns MOVE_POS_MATCH_OR_ZV. */
20973 || (it.line_wrap == WORD_WRAP
20974 && rc == MOVE_POS_MATCH_OR_ZV)))
20975 {
20976 int new_x = it.current_x + it.pixel_width;
20977
20978 /* For composed characters, we want the position of the
20979 first character in the grapheme cluster (usually, the
20980 composition's base character), whereas it.current
20981 might give us the position of the _last_ one, e.g. if
20982 the composition is rendered in reverse due to bidi
20983 reordering. */
20984 if (it.what == IT_COMPOSITION)
20985 {
20986 new_pos.charpos = it.cmp_it.charpos;
20987 new_pos.bytepos = -1;
20988 }
20989 else
20990 new_pos = it.current.pos;
20991 if (new_x == it.current_x)
20992 new_x++;
20993 rc = move_it_in_display_line_to (&it, ZV, new_x,
20994 MOVE_TO_POS | MOVE_TO_X);
20995 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20996 break;
20997 }
20998 /* The previous position we saw in the loop is the one we
20999 want. */
21000 if (new_pos.bytepos == -1)
21001 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21002 it.current.pos = new_pos;
21003 }
21004 else
21005 #endif
21006 if (it.current_x != target_x)
21007 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21008
21009 /* When lines are truncated, the above loop will stop at the
21010 window edge. But we want to get to the end of line, even if
21011 it is beyond the window edge; automatic hscroll will then
21012 scroll the window to show point as appropriate. */
21013 if (target_is_eol_p && it.line_wrap == TRUNCATE
21014 && get_next_display_element (&it))
21015 {
21016 struct text_pos new_pos = it.current.pos;
21017
21018 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21019 {
21020 set_iterator_to_next (&it, 0);
21021 if (it.method == GET_FROM_BUFFER)
21022 new_pos = it.current.pos;
21023 if (!get_next_display_element (&it))
21024 break;
21025 }
21026
21027 it.current.pos = new_pos;
21028 }
21029
21030 /* If we ended up in a display string that covers point, move to
21031 buffer position to the right in the visual order. */
21032 if (dir > 0)
21033 {
21034 while (IT_CHARPOS (it) == PT)
21035 {
21036 set_iterator_to_next (&it, 0);
21037 if (!get_next_display_element (&it))
21038 break;
21039 }
21040 }
21041
21042 /* Move point to that position. */
21043 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21044 }
21045
21046 return make_number (PT);
21047
21048 #undef ROW_GLYPH_NEWLINE_P
21049 }
21050
21051 \f
21052 /***********************************************************************
21053 Menu Bar
21054 ***********************************************************************/
21055
21056 /* Redisplay the menu bar in the frame for window W.
21057
21058 The menu bar of X frames that don't have X toolkit support is
21059 displayed in a special window W->frame->menu_bar_window.
21060
21061 The menu bar of terminal frames is treated specially as far as
21062 glyph matrices are concerned. Menu bar lines are not part of
21063 windows, so the update is done directly on the frame matrix rows
21064 for the menu bar. */
21065
21066 static void
21067 display_menu_bar (struct window *w)
21068 {
21069 struct frame *f = XFRAME (WINDOW_FRAME (w));
21070 struct it it;
21071 Lisp_Object items;
21072 int i;
21073
21074 /* Don't do all this for graphical frames. */
21075 #ifdef HAVE_NTGUI
21076 if (FRAME_W32_P (f))
21077 return;
21078 #endif
21079 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21080 if (FRAME_X_P (f))
21081 return;
21082 #endif
21083
21084 #ifdef HAVE_NS
21085 if (FRAME_NS_P (f))
21086 return;
21087 #endif /* HAVE_NS */
21088
21089 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21090 eassert (!FRAME_WINDOW_P (f));
21091 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21092 it.first_visible_x = 0;
21093 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21094 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21095 if (FRAME_WINDOW_P (f))
21096 {
21097 /* Menu bar lines are displayed in the desired matrix of the
21098 dummy window menu_bar_window. */
21099 struct window *menu_w;
21100 menu_w = XWINDOW (f->menu_bar_window);
21101 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21102 MENU_FACE_ID);
21103 it.first_visible_x = 0;
21104 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21105 }
21106 else
21107 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21108 {
21109 /* This is a TTY frame, i.e. character hpos/vpos are used as
21110 pixel x/y. */
21111 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21112 MENU_FACE_ID);
21113 it.first_visible_x = 0;
21114 it.last_visible_x = FRAME_COLS (f);
21115 }
21116
21117 /* FIXME: This should be controlled by a user option. See the
21118 comments in redisplay_tool_bar and display_mode_line about
21119 this. */
21120 it.paragraph_embedding = L2R;
21121
21122 /* Clear all rows of the menu bar. */
21123 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21124 {
21125 struct glyph_row *row = it.glyph_row + i;
21126 clear_glyph_row (row);
21127 row->enabled_p = true;
21128 row->full_width_p = 1;
21129 }
21130
21131 /* Display all items of the menu bar. */
21132 items = FRAME_MENU_BAR_ITEMS (it.f);
21133 for (i = 0; i < ASIZE (items); i += 4)
21134 {
21135 Lisp_Object string;
21136
21137 /* Stop at nil string. */
21138 string = AREF (items, i + 1);
21139 if (NILP (string))
21140 break;
21141
21142 /* Remember where item was displayed. */
21143 ASET (items, i + 3, make_number (it.hpos));
21144
21145 /* Display the item, pad with one space. */
21146 if (it.current_x < it.last_visible_x)
21147 display_string (NULL, string, Qnil, 0, 0, &it,
21148 SCHARS (string) + 1, 0, 0, -1);
21149 }
21150
21151 /* Fill out the line with spaces. */
21152 if (it.current_x < it.last_visible_x)
21153 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21154
21155 /* Compute the total height of the lines. */
21156 compute_line_metrics (&it);
21157 }
21158
21159 /* Deep copy of a glyph row, including the glyphs. */
21160 static void
21161 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21162 {
21163 struct glyph *pointers[1 + LAST_AREA];
21164 int to_used = to->used[TEXT_AREA];
21165
21166 /* Save glyph pointers of TO. */
21167 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21168
21169 /* Do a structure assignment. */
21170 *to = *from;
21171
21172 /* Restore original glyph pointers of TO. */
21173 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21174
21175 /* Copy the glyphs. */
21176 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21177 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21178
21179 /* If we filled only part of the TO row, fill the rest with
21180 space_glyph (which will display as empty space). */
21181 if (to_used > from->used[TEXT_AREA])
21182 fill_up_frame_row_with_spaces (to, to_used);
21183 }
21184
21185 /* Display one menu item on a TTY, by overwriting the glyphs in the
21186 frame F's desired glyph matrix with glyphs produced from the menu
21187 item text. Called from term.c to display TTY drop-down menus one
21188 item at a time.
21189
21190 ITEM_TEXT is the menu item text as a C string.
21191
21192 FACE_ID is the face ID to be used for this menu item. FACE_ID
21193 could specify one of 3 faces: a face for an enabled item, a face
21194 for a disabled item, or a face for a selected item.
21195
21196 X and Y are coordinates of the first glyph in the frame's desired
21197 matrix to be overwritten by the menu item. Since this is a TTY, Y
21198 is the zero-based number of the glyph row and X is the zero-based
21199 glyph number in the row, starting from left, where to start
21200 displaying the item.
21201
21202 SUBMENU non-zero means this menu item drops down a submenu, which
21203 should be indicated by displaying a proper visual cue after the
21204 item text. */
21205
21206 void
21207 display_tty_menu_item (const char *item_text, int width, int face_id,
21208 int x, int y, int submenu)
21209 {
21210 struct it it;
21211 struct frame *f = SELECTED_FRAME ();
21212 struct window *w = XWINDOW (f->selected_window);
21213 int saved_used, saved_truncated, saved_width, saved_reversed;
21214 struct glyph_row *row;
21215 size_t item_len = strlen (item_text);
21216
21217 eassert (FRAME_TERMCAP_P (f));
21218
21219 /* Don't write beyond the matrix's last row. This can happen for
21220 TTY screens that are not high enough to show the entire menu.
21221 (This is actually a bit of defensive programming, as
21222 tty_menu_display already limits the number of menu items to one
21223 less than the number of screen lines.) */
21224 if (y >= f->desired_matrix->nrows)
21225 return;
21226
21227 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21228 it.first_visible_x = 0;
21229 it.last_visible_x = FRAME_COLS (f) - 1;
21230 row = it.glyph_row;
21231 /* Start with the row contents from the current matrix. */
21232 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21233 saved_width = row->full_width_p;
21234 row->full_width_p = 1;
21235 saved_reversed = row->reversed_p;
21236 row->reversed_p = 0;
21237 row->enabled_p = true;
21238
21239 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21240 desired face. */
21241 eassert (x < f->desired_matrix->matrix_w);
21242 it.current_x = it.hpos = x;
21243 it.current_y = it.vpos = y;
21244 saved_used = row->used[TEXT_AREA];
21245 saved_truncated = row->truncated_on_right_p;
21246 row->used[TEXT_AREA] = x;
21247 it.face_id = face_id;
21248 it.line_wrap = TRUNCATE;
21249
21250 /* FIXME: This should be controlled by a user option. See the
21251 comments in redisplay_tool_bar and display_mode_line about this.
21252 Also, if paragraph_embedding could ever be R2L, changes will be
21253 needed to avoid shifting to the right the row characters in
21254 term.c:append_glyph. */
21255 it.paragraph_embedding = L2R;
21256
21257 /* Pad with a space on the left. */
21258 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21259 width--;
21260 /* Display the menu item, pad with spaces to WIDTH. */
21261 if (submenu)
21262 {
21263 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21264 item_len, 0, FRAME_COLS (f) - 1, -1);
21265 width -= item_len;
21266 /* Indicate with " >" that there's a submenu. */
21267 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21268 FRAME_COLS (f) - 1, -1);
21269 }
21270 else
21271 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21272 width, 0, FRAME_COLS (f) - 1, -1);
21273
21274 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21275 row->truncated_on_right_p = saved_truncated;
21276 row->hash = row_hash (row);
21277 row->full_width_p = saved_width;
21278 row->reversed_p = saved_reversed;
21279 }
21280 \f
21281 /***********************************************************************
21282 Mode Line
21283 ***********************************************************************/
21284
21285 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21286 FORCE is non-zero, redisplay mode lines unconditionally.
21287 Otherwise, redisplay only mode lines that are garbaged. Value is
21288 the number of windows whose mode lines were redisplayed. */
21289
21290 static int
21291 redisplay_mode_lines (Lisp_Object window, bool force)
21292 {
21293 int nwindows = 0;
21294
21295 while (!NILP (window))
21296 {
21297 struct window *w = XWINDOW (window);
21298
21299 if (WINDOWP (w->contents))
21300 nwindows += redisplay_mode_lines (w->contents, force);
21301 else if (force
21302 || FRAME_GARBAGED_P (XFRAME (w->frame))
21303 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21304 {
21305 struct text_pos lpoint;
21306 struct buffer *old = current_buffer;
21307
21308 /* Set the window's buffer for the mode line display. */
21309 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21310 set_buffer_internal_1 (XBUFFER (w->contents));
21311
21312 /* Point refers normally to the selected window. For any
21313 other window, set up appropriate value. */
21314 if (!EQ (window, selected_window))
21315 {
21316 struct text_pos pt;
21317
21318 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21319 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21320 }
21321
21322 /* Display mode lines. */
21323 clear_glyph_matrix (w->desired_matrix);
21324 if (display_mode_lines (w))
21325 ++nwindows;
21326
21327 /* Restore old settings. */
21328 set_buffer_internal_1 (old);
21329 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21330 }
21331
21332 window = w->next;
21333 }
21334
21335 return nwindows;
21336 }
21337
21338
21339 /* Display the mode and/or header line of window W. Value is the
21340 sum number of mode lines and header lines displayed. */
21341
21342 static int
21343 display_mode_lines (struct window *w)
21344 {
21345 Lisp_Object old_selected_window = selected_window;
21346 Lisp_Object old_selected_frame = selected_frame;
21347 Lisp_Object new_frame = w->frame;
21348 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21349 int n = 0;
21350
21351 selected_frame = new_frame;
21352 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21353 or window's point, then we'd need select_window_1 here as well. */
21354 XSETWINDOW (selected_window, w);
21355 XFRAME (new_frame)->selected_window = selected_window;
21356
21357 /* These will be set while the mode line specs are processed. */
21358 line_number_displayed = 0;
21359 w->column_number_displayed = -1;
21360
21361 if (WINDOW_WANTS_MODELINE_P (w))
21362 {
21363 struct window *sel_w = XWINDOW (old_selected_window);
21364
21365 /* Select mode line face based on the real selected window. */
21366 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21367 BVAR (current_buffer, mode_line_format));
21368 ++n;
21369 }
21370
21371 if (WINDOW_WANTS_HEADER_LINE_P (w))
21372 {
21373 display_mode_line (w, HEADER_LINE_FACE_ID,
21374 BVAR (current_buffer, header_line_format));
21375 ++n;
21376 }
21377
21378 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21379 selected_frame = old_selected_frame;
21380 selected_window = old_selected_window;
21381 if (n > 0)
21382 w->must_be_updated_p = true;
21383 return n;
21384 }
21385
21386
21387 /* Display mode or header line of window W. FACE_ID specifies which
21388 line to display; it is either MODE_LINE_FACE_ID or
21389 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21390 display. Value is the pixel height of the mode/header line
21391 displayed. */
21392
21393 static int
21394 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21395 {
21396 struct it it;
21397 struct face *face;
21398 ptrdiff_t count = SPECPDL_INDEX ();
21399
21400 init_iterator (&it, w, -1, -1, NULL, face_id);
21401 /* Don't extend on a previously drawn mode-line.
21402 This may happen if called from pos_visible_p. */
21403 it.glyph_row->enabled_p = false;
21404 prepare_desired_row (it.glyph_row);
21405
21406 it.glyph_row->mode_line_p = 1;
21407
21408 /* FIXME: This should be controlled by a user option. But
21409 supporting such an option is not trivial, since the mode line is
21410 made up of many separate strings. */
21411 it.paragraph_embedding = L2R;
21412
21413 record_unwind_protect (unwind_format_mode_line,
21414 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21415
21416 mode_line_target = MODE_LINE_DISPLAY;
21417
21418 /* Temporarily make frame's keyboard the current kboard so that
21419 kboard-local variables in the mode_line_format will get the right
21420 values. */
21421 push_kboard (FRAME_KBOARD (it.f));
21422 record_unwind_save_match_data ();
21423 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21424 pop_kboard ();
21425
21426 unbind_to (count, Qnil);
21427
21428 /* Fill up with spaces. */
21429 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21430
21431 compute_line_metrics (&it);
21432 it.glyph_row->full_width_p = 1;
21433 it.glyph_row->continued_p = 0;
21434 it.glyph_row->truncated_on_left_p = 0;
21435 it.glyph_row->truncated_on_right_p = 0;
21436
21437 /* Make a 3D mode-line have a shadow at its right end. */
21438 face = FACE_FROM_ID (it.f, face_id);
21439 extend_face_to_end_of_line (&it);
21440 if (face->box != FACE_NO_BOX)
21441 {
21442 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21443 + it.glyph_row->used[TEXT_AREA] - 1);
21444 last->right_box_line_p = 1;
21445 }
21446
21447 return it.glyph_row->height;
21448 }
21449
21450 /* Move element ELT in LIST to the front of LIST.
21451 Return the updated list. */
21452
21453 static Lisp_Object
21454 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21455 {
21456 register Lisp_Object tail, prev;
21457 register Lisp_Object tem;
21458
21459 tail = list;
21460 prev = Qnil;
21461 while (CONSP (tail))
21462 {
21463 tem = XCAR (tail);
21464
21465 if (EQ (elt, tem))
21466 {
21467 /* Splice out the link TAIL. */
21468 if (NILP (prev))
21469 list = XCDR (tail);
21470 else
21471 Fsetcdr (prev, XCDR (tail));
21472
21473 /* Now make it the first. */
21474 Fsetcdr (tail, list);
21475 return tail;
21476 }
21477 else
21478 prev = tail;
21479 tail = XCDR (tail);
21480 QUIT;
21481 }
21482
21483 /* Not found--return unchanged LIST. */
21484 return list;
21485 }
21486
21487 /* Contribute ELT to the mode line for window IT->w. How it
21488 translates into text depends on its data type.
21489
21490 IT describes the display environment in which we display, as usual.
21491
21492 DEPTH is the depth in recursion. It is used to prevent
21493 infinite recursion here.
21494
21495 FIELD_WIDTH is the number of characters the display of ELT should
21496 occupy in the mode line, and PRECISION is the maximum number of
21497 characters to display from ELT's representation. See
21498 display_string for details.
21499
21500 Returns the hpos of the end of the text generated by ELT.
21501
21502 PROPS is a property list to add to any string we encounter.
21503
21504 If RISKY is nonzero, remove (disregard) any properties in any string
21505 we encounter, and ignore :eval and :propertize.
21506
21507 The global variable `mode_line_target' determines whether the
21508 output is passed to `store_mode_line_noprop',
21509 `store_mode_line_string', or `display_string'. */
21510
21511 static int
21512 display_mode_element (struct it *it, int depth, int field_width, int precision,
21513 Lisp_Object elt, Lisp_Object props, int risky)
21514 {
21515 int n = 0, field, prec;
21516 int literal = 0;
21517
21518 tail_recurse:
21519 if (depth > 100)
21520 elt = build_string ("*too-deep*");
21521
21522 depth++;
21523
21524 switch (XTYPE (elt))
21525 {
21526 case Lisp_String:
21527 {
21528 /* A string: output it and check for %-constructs within it. */
21529 unsigned char c;
21530 ptrdiff_t offset = 0;
21531
21532 if (SCHARS (elt) > 0
21533 && (!NILP (props) || risky))
21534 {
21535 Lisp_Object oprops, aelt;
21536 oprops = Ftext_properties_at (make_number (0), elt);
21537
21538 /* If the starting string's properties are not what
21539 we want, translate the string. Also, if the string
21540 is risky, do that anyway. */
21541
21542 if (NILP (Fequal (props, oprops)) || risky)
21543 {
21544 /* If the starting string has properties,
21545 merge the specified ones onto the existing ones. */
21546 if (! NILP (oprops) && !risky)
21547 {
21548 Lisp_Object tem;
21549
21550 oprops = Fcopy_sequence (oprops);
21551 tem = props;
21552 while (CONSP (tem))
21553 {
21554 oprops = Fplist_put (oprops, XCAR (tem),
21555 XCAR (XCDR (tem)));
21556 tem = XCDR (XCDR (tem));
21557 }
21558 props = oprops;
21559 }
21560
21561 aelt = Fassoc (elt, mode_line_proptrans_alist);
21562 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21563 {
21564 /* AELT is what we want. Move it to the front
21565 without consing. */
21566 elt = XCAR (aelt);
21567 mode_line_proptrans_alist
21568 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21569 }
21570 else
21571 {
21572 Lisp_Object tem;
21573
21574 /* If AELT has the wrong props, it is useless.
21575 so get rid of it. */
21576 if (! NILP (aelt))
21577 mode_line_proptrans_alist
21578 = Fdelq (aelt, mode_line_proptrans_alist);
21579
21580 elt = Fcopy_sequence (elt);
21581 Fset_text_properties (make_number (0), Flength (elt),
21582 props, elt);
21583 /* Add this item to mode_line_proptrans_alist. */
21584 mode_line_proptrans_alist
21585 = Fcons (Fcons (elt, props),
21586 mode_line_proptrans_alist);
21587 /* Truncate mode_line_proptrans_alist
21588 to at most 50 elements. */
21589 tem = Fnthcdr (make_number (50),
21590 mode_line_proptrans_alist);
21591 if (! NILP (tem))
21592 XSETCDR (tem, Qnil);
21593 }
21594 }
21595 }
21596
21597 offset = 0;
21598
21599 if (literal)
21600 {
21601 prec = precision - n;
21602 switch (mode_line_target)
21603 {
21604 case MODE_LINE_NOPROP:
21605 case MODE_LINE_TITLE:
21606 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21607 break;
21608 case MODE_LINE_STRING:
21609 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21610 break;
21611 case MODE_LINE_DISPLAY:
21612 n += display_string (NULL, elt, Qnil, 0, 0, it,
21613 0, prec, 0, STRING_MULTIBYTE (elt));
21614 break;
21615 }
21616
21617 break;
21618 }
21619
21620 /* Handle the non-literal case. */
21621
21622 while ((precision <= 0 || n < precision)
21623 && SREF (elt, offset) != 0
21624 && (mode_line_target != MODE_LINE_DISPLAY
21625 || it->current_x < it->last_visible_x))
21626 {
21627 ptrdiff_t last_offset = offset;
21628
21629 /* Advance to end of string or next format specifier. */
21630 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21631 ;
21632
21633 if (offset - 1 != last_offset)
21634 {
21635 ptrdiff_t nchars, nbytes;
21636
21637 /* Output to end of string or up to '%'. Field width
21638 is length of string. Don't output more than
21639 PRECISION allows us. */
21640 offset--;
21641
21642 prec = c_string_width (SDATA (elt) + last_offset,
21643 offset - last_offset, precision - n,
21644 &nchars, &nbytes);
21645
21646 switch (mode_line_target)
21647 {
21648 case MODE_LINE_NOPROP:
21649 case MODE_LINE_TITLE:
21650 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21651 break;
21652 case MODE_LINE_STRING:
21653 {
21654 ptrdiff_t bytepos = last_offset;
21655 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21656 ptrdiff_t endpos = (precision <= 0
21657 ? string_byte_to_char (elt, offset)
21658 : charpos + nchars);
21659
21660 n += store_mode_line_string (NULL,
21661 Fsubstring (elt, make_number (charpos),
21662 make_number (endpos)),
21663 0, 0, 0, Qnil);
21664 }
21665 break;
21666 case MODE_LINE_DISPLAY:
21667 {
21668 ptrdiff_t bytepos = last_offset;
21669 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21670
21671 if (precision <= 0)
21672 nchars = string_byte_to_char (elt, offset) - charpos;
21673 n += display_string (NULL, elt, Qnil, 0, charpos,
21674 it, 0, nchars, 0,
21675 STRING_MULTIBYTE (elt));
21676 }
21677 break;
21678 }
21679 }
21680 else /* c == '%' */
21681 {
21682 ptrdiff_t percent_position = offset;
21683
21684 /* Get the specified minimum width. Zero means
21685 don't pad. */
21686 field = 0;
21687 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21688 field = field * 10 + c - '0';
21689
21690 /* Don't pad beyond the total padding allowed. */
21691 if (field_width - n > 0 && field > field_width - n)
21692 field = field_width - n;
21693
21694 /* Note that either PRECISION <= 0 or N < PRECISION. */
21695 prec = precision - n;
21696
21697 if (c == 'M')
21698 n += display_mode_element (it, depth, field, prec,
21699 Vglobal_mode_string, props,
21700 risky);
21701 else if (c != 0)
21702 {
21703 bool multibyte;
21704 ptrdiff_t bytepos, charpos;
21705 const char *spec;
21706 Lisp_Object string;
21707
21708 bytepos = percent_position;
21709 charpos = (STRING_MULTIBYTE (elt)
21710 ? string_byte_to_char (elt, bytepos)
21711 : bytepos);
21712 spec = decode_mode_spec (it->w, c, field, &string);
21713 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21714
21715 switch (mode_line_target)
21716 {
21717 case MODE_LINE_NOPROP:
21718 case MODE_LINE_TITLE:
21719 n += store_mode_line_noprop (spec, field, prec);
21720 break;
21721 case MODE_LINE_STRING:
21722 {
21723 Lisp_Object tem = build_string (spec);
21724 props = Ftext_properties_at (make_number (charpos), elt);
21725 /* Should only keep face property in props */
21726 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21727 }
21728 break;
21729 case MODE_LINE_DISPLAY:
21730 {
21731 int nglyphs_before, nwritten;
21732
21733 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21734 nwritten = display_string (spec, string, elt,
21735 charpos, 0, it,
21736 field, prec, 0,
21737 multibyte);
21738
21739 /* Assign to the glyphs written above the
21740 string where the `%x' came from, position
21741 of the `%'. */
21742 if (nwritten > 0)
21743 {
21744 struct glyph *glyph
21745 = (it->glyph_row->glyphs[TEXT_AREA]
21746 + nglyphs_before);
21747 int i;
21748
21749 for (i = 0; i < nwritten; ++i)
21750 {
21751 glyph[i].object = elt;
21752 glyph[i].charpos = charpos;
21753 }
21754
21755 n += nwritten;
21756 }
21757 }
21758 break;
21759 }
21760 }
21761 else /* c == 0 */
21762 break;
21763 }
21764 }
21765 }
21766 break;
21767
21768 case Lisp_Symbol:
21769 /* A symbol: process the value of the symbol recursively
21770 as if it appeared here directly. Avoid error if symbol void.
21771 Special case: if value of symbol is a string, output the string
21772 literally. */
21773 {
21774 register Lisp_Object tem;
21775
21776 /* If the variable is not marked as risky to set
21777 then its contents are risky to use. */
21778 if (NILP (Fget (elt, Qrisky_local_variable)))
21779 risky = 1;
21780
21781 tem = Fboundp (elt);
21782 if (!NILP (tem))
21783 {
21784 tem = Fsymbol_value (elt);
21785 /* If value is a string, output that string literally:
21786 don't check for % within it. */
21787 if (STRINGP (tem))
21788 literal = 1;
21789
21790 if (!EQ (tem, elt))
21791 {
21792 /* Give up right away for nil or t. */
21793 elt = tem;
21794 goto tail_recurse;
21795 }
21796 }
21797 }
21798 break;
21799
21800 case Lisp_Cons:
21801 {
21802 register Lisp_Object car, tem;
21803
21804 /* A cons cell: five distinct cases.
21805 If first element is :eval or :propertize, do something special.
21806 If first element is a string or a cons, process all the elements
21807 and effectively concatenate them.
21808 If first element is a negative number, truncate displaying cdr to
21809 at most that many characters. If positive, pad (with spaces)
21810 to at least that many characters.
21811 If first element is a symbol, process the cadr or caddr recursively
21812 according to whether the symbol's value is non-nil or nil. */
21813 car = XCAR (elt);
21814 if (EQ (car, QCeval))
21815 {
21816 /* An element of the form (:eval FORM) means evaluate FORM
21817 and use the result as mode line elements. */
21818
21819 if (risky)
21820 break;
21821
21822 if (CONSP (XCDR (elt)))
21823 {
21824 Lisp_Object spec;
21825 spec = safe_eval (XCAR (XCDR (elt)));
21826 n += display_mode_element (it, depth, field_width - n,
21827 precision - n, spec, props,
21828 risky);
21829 }
21830 }
21831 else if (EQ (car, QCpropertize))
21832 {
21833 /* An element of the form (:propertize ELT PROPS...)
21834 means display ELT but applying properties PROPS. */
21835
21836 if (risky)
21837 break;
21838
21839 if (CONSP (XCDR (elt)))
21840 n += display_mode_element (it, depth, field_width - n,
21841 precision - n, XCAR (XCDR (elt)),
21842 XCDR (XCDR (elt)), risky);
21843 }
21844 else if (SYMBOLP (car))
21845 {
21846 tem = Fboundp (car);
21847 elt = XCDR (elt);
21848 if (!CONSP (elt))
21849 goto invalid;
21850 /* elt is now the cdr, and we know it is a cons cell.
21851 Use its car if CAR has a non-nil value. */
21852 if (!NILP (tem))
21853 {
21854 tem = Fsymbol_value (car);
21855 if (!NILP (tem))
21856 {
21857 elt = XCAR (elt);
21858 goto tail_recurse;
21859 }
21860 }
21861 /* Symbol's value is nil (or symbol is unbound)
21862 Get the cddr of the original list
21863 and if possible find the caddr and use that. */
21864 elt = XCDR (elt);
21865 if (NILP (elt))
21866 break;
21867 else if (!CONSP (elt))
21868 goto invalid;
21869 elt = XCAR (elt);
21870 goto tail_recurse;
21871 }
21872 else if (INTEGERP (car))
21873 {
21874 register int lim = XINT (car);
21875 elt = XCDR (elt);
21876 if (lim < 0)
21877 {
21878 /* Negative int means reduce maximum width. */
21879 if (precision <= 0)
21880 precision = -lim;
21881 else
21882 precision = min (precision, -lim);
21883 }
21884 else if (lim > 0)
21885 {
21886 /* Padding specified. Don't let it be more than
21887 current maximum. */
21888 if (precision > 0)
21889 lim = min (precision, lim);
21890
21891 /* If that's more padding than already wanted, queue it.
21892 But don't reduce padding already specified even if
21893 that is beyond the current truncation point. */
21894 field_width = max (lim, field_width);
21895 }
21896 goto tail_recurse;
21897 }
21898 else if (STRINGP (car) || CONSP (car))
21899 {
21900 Lisp_Object halftail = elt;
21901 int len = 0;
21902
21903 while (CONSP (elt)
21904 && (precision <= 0 || n < precision))
21905 {
21906 n += display_mode_element (it, depth,
21907 /* Do padding only after the last
21908 element in the list. */
21909 (! CONSP (XCDR (elt))
21910 ? field_width - n
21911 : 0),
21912 precision - n, XCAR (elt),
21913 props, risky);
21914 elt = XCDR (elt);
21915 len++;
21916 if ((len & 1) == 0)
21917 halftail = XCDR (halftail);
21918 /* Check for cycle. */
21919 if (EQ (halftail, elt))
21920 break;
21921 }
21922 }
21923 }
21924 break;
21925
21926 default:
21927 invalid:
21928 elt = build_string ("*invalid*");
21929 goto tail_recurse;
21930 }
21931
21932 /* Pad to FIELD_WIDTH. */
21933 if (field_width > 0 && n < field_width)
21934 {
21935 switch (mode_line_target)
21936 {
21937 case MODE_LINE_NOPROP:
21938 case MODE_LINE_TITLE:
21939 n += store_mode_line_noprop ("", field_width - n, 0);
21940 break;
21941 case MODE_LINE_STRING:
21942 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21943 break;
21944 case MODE_LINE_DISPLAY:
21945 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21946 0, 0, 0);
21947 break;
21948 }
21949 }
21950
21951 return n;
21952 }
21953
21954 /* Store a mode-line string element in mode_line_string_list.
21955
21956 If STRING is non-null, display that C string. Otherwise, the Lisp
21957 string LISP_STRING is displayed.
21958
21959 FIELD_WIDTH is the minimum number of output glyphs to produce.
21960 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21961 with spaces. FIELD_WIDTH <= 0 means don't pad.
21962
21963 PRECISION is the maximum number of characters to output from
21964 STRING. PRECISION <= 0 means don't truncate the string.
21965
21966 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21967 properties to the string.
21968
21969 PROPS are the properties to add to the string.
21970 The mode_line_string_face face property is always added to the string.
21971 */
21972
21973 static int
21974 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21975 int field_width, int precision, Lisp_Object props)
21976 {
21977 ptrdiff_t len;
21978 int n = 0;
21979
21980 if (string != NULL)
21981 {
21982 len = strlen (string);
21983 if (precision > 0 && len > precision)
21984 len = precision;
21985 lisp_string = make_string (string, len);
21986 if (NILP (props))
21987 props = mode_line_string_face_prop;
21988 else if (!NILP (mode_line_string_face))
21989 {
21990 Lisp_Object face = Fplist_get (props, Qface);
21991 props = Fcopy_sequence (props);
21992 if (NILP (face))
21993 face = mode_line_string_face;
21994 else
21995 face = list2 (face, mode_line_string_face);
21996 props = Fplist_put (props, Qface, face);
21997 }
21998 Fadd_text_properties (make_number (0), make_number (len),
21999 props, lisp_string);
22000 }
22001 else
22002 {
22003 len = XFASTINT (Flength (lisp_string));
22004 if (precision > 0 && len > precision)
22005 {
22006 len = precision;
22007 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22008 precision = -1;
22009 }
22010 if (!NILP (mode_line_string_face))
22011 {
22012 Lisp_Object face;
22013 if (NILP (props))
22014 props = Ftext_properties_at (make_number (0), lisp_string);
22015 face = Fplist_get (props, Qface);
22016 if (NILP (face))
22017 face = mode_line_string_face;
22018 else
22019 face = list2 (face, mode_line_string_face);
22020 props = list2 (Qface, face);
22021 if (copy_string)
22022 lisp_string = Fcopy_sequence (lisp_string);
22023 }
22024 if (!NILP (props))
22025 Fadd_text_properties (make_number (0), make_number (len),
22026 props, lisp_string);
22027 }
22028
22029 if (len > 0)
22030 {
22031 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22032 n += len;
22033 }
22034
22035 if (field_width > len)
22036 {
22037 field_width -= len;
22038 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22039 if (!NILP (props))
22040 Fadd_text_properties (make_number (0), make_number (field_width),
22041 props, lisp_string);
22042 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22043 n += field_width;
22044 }
22045
22046 return n;
22047 }
22048
22049
22050 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22051 1, 4, 0,
22052 doc: /* Format a string out of a mode line format specification.
22053 First arg FORMAT specifies the mode line format (see `mode-line-format'
22054 for details) to use.
22055
22056 By default, the format is evaluated for the currently selected window.
22057
22058 Optional second arg FACE specifies the face property to put on all
22059 characters for which no face is specified. The value nil means the
22060 default face. The value t means whatever face the window's mode line
22061 currently uses (either `mode-line' or `mode-line-inactive',
22062 depending on whether the window is the selected window or not).
22063 An integer value means the value string has no text
22064 properties.
22065
22066 Optional third and fourth args WINDOW and BUFFER specify the window
22067 and buffer to use as the context for the formatting (defaults
22068 are the selected window and the WINDOW's buffer). */)
22069 (Lisp_Object format, Lisp_Object face,
22070 Lisp_Object window, Lisp_Object buffer)
22071 {
22072 struct it it;
22073 int len;
22074 struct window *w;
22075 struct buffer *old_buffer = NULL;
22076 int face_id;
22077 int no_props = INTEGERP (face);
22078 ptrdiff_t count = SPECPDL_INDEX ();
22079 Lisp_Object str;
22080 int string_start = 0;
22081
22082 w = decode_any_window (window);
22083 XSETWINDOW (window, w);
22084
22085 if (NILP (buffer))
22086 buffer = w->contents;
22087 CHECK_BUFFER (buffer);
22088
22089 /* Make formatting the modeline a non-op when noninteractive, otherwise
22090 there will be problems later caused by a partially initialized frame. */
22091 if (NILP (format) || noninteractive)
22092 return empty_unibyte_string;
22093
22094 if (no_props)
22095 face = Qnil;
22096
22097 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22098 : EQ (face, Qt) ? (EQ (window, selected_window)
22099 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22100 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22101 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22102 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22103 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22104 : DEFAULT_FACE_ID;
22105
22106 old_buffer = current_buffer;
22107
22108 /* Save things including mode_line_proptrans_alist,
22109 and set that to nil so that we don't alter the outer value. */
22110 record_unwind_protect (unwind_format_mode_line,
22111 format_mode_line_unwind_data
22112 (XFRAME (WINDOW_FRAME (w)),
22113 old_buffer, selected_window, 1));
22114 mode_line_proptrans_alist = Qnil;
22115
22116 Fselect_window (window, Qt);
22117 set_buffer_internal_1 (XBUFFER (buffer));
22118
22119 init_iterator (&it, w, -1, -1, NULL, face_id);
22120
22121 if (no_props)
22122 {
22123 mode_line_target = MODE_LINE_NOPROP;
22124 mode_line_string_face_prop = Qnil;
22125 mode_line_string_list = Qnil;
22126 string_start = MODE_LINE_NOPROP_LEN (0);
22127 }
22128 else
22129 {
22130 mode_line_target = MODE_LINE_STRING;
22131 mode_line_string_list = Qnil;
22132 mode_line_string_face = face;
22133 mode_line_string_face_prop
22134 = NILP (face) ? Qnil : list2 (Qface, face);
22135 }
22136
22137 push_kboard (FRAME_KBOARD (it.f));
22138 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22139 pop_kboard ();
22140
22141 if (no_props)
22142 {
22143 len = MODE_LINE_NOPROP_LEN (string_start);
22144 str = make_string (mode_line_noprop_buf + string_start, len);
22145 }
22146 else
22147 {
22148 mode_line_string_list = Fnreverse (mode_line_string_list);
22149 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22150 empty_unibyte_string);
22151 }
22152
22153 unbind_to (count, Qnil);
22154 return str;
22155 }
22156
22157 /* Write a null-terminated, right justified decimal representation of
22158 the positive integer D to BUF using a minimal field width WIDTH. */
22159
22160 static void
22161 pint2str (register char *buf, register int width, register ptrdiff_t d)
22162 {
22163 register char *p = buf;
22164
22165 if (d <= 0)
22166 *p++ = '0';
22167 else
22168 {
22169 while (d > 0)
22170 {
22171 *p++ = d % 10 + '0';
22172 d /= 10;
22173 }
22174 }
22175
22176 for (width -= (int) (p - buf); width > 0; --width)
22177 *p++ = ' ';
22178 *p-- = '\0';
22179 while (p > buf)
22180 {
22181 d = *buf;
22182 *buf++ = *p;
22183 *p-- = d;
22184 }
22185 }
22186
22187 /* Write a null-terminated, right justified decimal and "human
22188 readable" representation of the nonnegative integer D to BUF using
22189 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22190
22191 static const char power_letter[] =
22192 {
22193 0, /* no letter */
22194 'k', /* kilo */
22195 'M', /* mega */
22196 'G', /* giga */
22197 'T', /* tera */
22198 'P', /* peta */
22199 'E', /* exa */
22200 'Z', /* zetta */
22201 'Y' /* yotta */
22202 };
22203
22204 static void
22205 pint2hrstr (char *buf, int width, ptrdiff_t d)
22206 {
22207 /* We aim to represent the nonnegative integer D as
22208 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22209 ptrdiff_t quotient = d;
22210 int remainder = 0;
22211 /* -1 means: do not use TENTHS. */
22212 int tenths = -1;
22213 int exponent = 0;
22214
22215 /* Length of QUOTIENT.TENTHS as a string. */
22216 int length;
22217
22218 char * psuffix;
22219 char * p;
22220
22221 if (quotient >= 1000)
22222 {
22223 /* Scale to the appropriate EXPONENT. */
22224 do
22225 {
22226 remainder = quotient % 1000;
22227 quotient /= 1000;
22228 exponent++;
22229 }
22230 while (quotient >= 1000);
22231
22232 /* Round to nearest and decide whether to use TENTHS or not. */
22233 if (quotient <= 9)
22234 {
22235 tenths = remainder / 100;
22236 if (remainder % 100 >= 50)
22237 {
22238 if (tenths < 9)
22239 tenths++;
22240 else
22241 {
22242 quotient++;
22243 if (quotient == 10)
22244 tenths = -1;
22245 else
22246 tenths = 0;
22247 }
22248 }
22249 }
22250 else
22251 if (remainder >= 500)
22252 {
22253 if (quotient < 999)
22254 quotient++;
22255 else
22256 {
22257 quotient = 1;
22258 exponent++;
22259 tenths = 0;
22260 }
22261 }
22262 }
22263
22264 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22265 if (tenths == -1 && quotient <= 99)
22266 if (quotient <= 9)
22267 length = 1;
22268 else
22269 length = 2;
22270 else
22271 length = 3;
22272 p = psuffix = buf + max (width, length);
22273
22274 /* Print EXPONENT. */
22275 *psuffix++ = power_letter[exponent];
22276 *psuffix = '\0';
22277
22278 /* Print TENTHS. */
22279 if (tenths >= 0)
22280 {
22281 *--p = '0' + tenths;
22282 *--p = '.';
22283 }
22284
22285 /* Print QUOTIENT. */
22286 do
22287 {
22288 int digit = quotient % 10;
22289 *--p = '0' + digit;
22290 }
22291 while ((quotient /= 10) != 0);
22292
22293 /* Print leading spaces. */
22294 while (buf < p)
22295 *--p = ' ';
22296 }
22297
22298 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22299 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22300 type of CODING_SYSTEM. Return updated pointer into BUF. */
22301
22302 static unsigned char invalid_eol_type[] = "(*invalid*)";
22303
22304 static char *
22305 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22306 {
22307 Lisp_Object val;
22308 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22309 const unsigned char *eol_str;
22310 int eol_str_len;
22311 /* The EOL conversion we are using. */
22312 Lisp_Object eoltype;
22313
22314 val = CODING_SYSTEM_SPEC (coding_system);
22315 eoltype = Qnil;
22316
22317 if (!VECTORP (val)) /* Not yet decided. */
22318 {
22319 *buf++ = multibyte ? '-' : ' ';
22320 if (eol_flag)
22321 eoltype = eol_mnemonic_undecided;
22322 /* Don't mention EOL conversion if it isn't decided. */
22323 }
22324 else
22325 {
22326 Lisp_Object attrs;
22327 Lisp_Object eolvalue;
22328
22329 attrs = AREF (val, 0);
22330 eolvalue = AREF (val, 2);
22331
22332 *buf++ = multibyte
22333 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22334 : ' ';
22335
22336 if (eol_flag)
22337 {
22338 /* The EOL conversion that is normal on this system. */
22339
22340 if (NILP (eolvalue)) /* Not yet decided. */
22341 eoltype = eol_mnemonic_undecided;
22342 else if (VECTORP (eolvalue)) /* Not yet decided. */
22343 eoltype = eol_mnemonic_undecided;
22344 else /* eolvalue is Qunix, Qdos, or Qmac. */
22345 eoltype = (EQ (eolvalue, Qunix)
22346 ? eol_mnemonic_unix
22347 : (EQ (eolvalue, Qdos) == 1
22348 ? eol_mnemonic_dos : eol_mnemonic_mac));
22349 }
22350 }
22351
22352 if (eol_flag)
22353 {
22354 /* Mention the EOL conversion if it is not the usual one. */
22355 if (STRINGP (eoltype))
22356 {
22357 eol_str = SDATA (eoltype);
22358 eol_str_len = SBYTES (eoltype);
22359 }
22360 else if (CHARACTERP (eoltype))
22361 {
22362 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22363 int c = XFASTINT (eoltype);
22364 eol_str_len = CHAR_STRING (c, tmp);
22365 eol_str = tmp;
22366 }
22367 else
22368 {
22369 eol_str = invalid_eol_type;
22370 eol_str_len = sizeof (invalid_eol_type) - 1;
22371 }
22372 memcpy (buf, eol_str, eol_str_len);
22373 buf += eol_str_len;
22374 }
22375
22376 return buf;
22377 }
22378
22379 /* Return a string for the output of a mode line %-spec for window W,
22380 generated by character C. FIELD_WIDTH > 0 means pad the string
22381 returned with spaces to that value. Return a Lisp string in
22382 *STRING if the resulting string is taken from that Lisp string.
22383
22384 Note we operate on the current buffer for most purposes. */
22385
22386 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22387
22388 static const char *
22389 decode_mode_spec (struct window *w, register int c, int field_width,
22390 Lisp_Object *string)
22391 {
22392 Lisp_Object obj;
22393 struct frame *f = XFRAME (WINDOW_FRAME (w));
22394 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22395 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22396 produce strings from numerical values, so limit preposterously
22397 large values of FIELD_WIDTH to avoid overrunning the buffer's
22398 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22399 bytes plus the terminating null. */
22400 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22401 struct buffer *b = current_buffer;
22402
22403 obj = Qnil;
22404 *string = Qnil;
22405
22406 switch (c)
22407 {
22408 case '*':
22409 if (!NILP (BVAR (b, read_only)))
22410 return "%";
22411 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22412 return "*";
22413 return "-";
22414
22415 case '+':
22416 /* This differs from %* only for a modified read-only buffer. */
22417 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22418 return "*";
22419 if (!NILP (BVAR (b, read_only)))
22420 return "%";
22421 return "-";
22422
22423 case '&':
22424 /* This differs from %* in ignoring read-only-ness. */
22425 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22426 return "*";
22427 return "-";
22428
22429 case '%':
22430 return "%";
22431
22432 case '[':
22433 {
22434 int i;
22435 char *p;
22436
22437 if (command_loop_level > 5)
22438 return "[[[... ";
22439 p = decode_mode_spec_buf;
22440 for (i = 0; i < command_loop_level; i++)
22441 *p++ = '[';
22442 *p = 0;
22443 return decode_mode_spec_buf;
22444 }
22445
22446 case ']':
22447 {
22448 int i;
22449 char *p;
22450
22451 if (command_loop_level > 5)
22452 return " ...]]]";
22453 p = decode_mode_spec_buf;
22454 for (i = 0; i < command_loop_level; i++)
22455 *p++ = ']';
22456 *p = 0;
22457 return decode_mode_spec_buf;
22458 }
22459
22460 case '-':
22461 {
22462 register int i;
22463
22464 /* Let lots_of_dashes be a string of infinite length. */
22465 if (mode_line_target == MODE_LINE_NOPROP
22466 || mode_line_target == MODE_LINE_STRING)
22467 return "--";
22468 if (field_width <= 0
22469 || field_width > sizeof (lots_of_dashes))
22470 {
22471 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22472 decode_mode_spec_buf[i] = '-';
22473 decode_mode_spec_buf[i] = '\0';
22474 return decode_mode_spec_buf;
22475 }
22476 else
22477 return lots_of_dashes;
22478 }
22479
22480 case 'b':
22481 obj = BVAR (b, name);
22482 break;
22483
22484 case 'c':
22485 /* %c and %l are ignored in `frame-title-format'.
22486 (In redisplay_internal, the frame title is drawn _before_ the
22487 windows are updated, so the stuff which depends on actual
22488 window contents (such as %l) may fail to render properly, or
22489 even crash emacs.) */
22490 if (mode_line_target == MODE_LINE_TITLE)
22491 return "";
22492 else
22493 {
22494 ptrdiff_t col = current_column ();
22495 w->column_number_displayed = col;
22496 pint2str (decode_mode_spec_buf, width, col);
22497 return decode_mode_spec_buf;
22498 }
22499
22500 case 'e':
22501 #ifndef SYSTEM_MALLOC
22502 {
22503 if (NILP (Vmemory_full))
22504 return "";
22505 else
22506 return "!MEM FULL! ";
22507 }
22508 #else
22509 return "";
22510 #endif
22511
22512 case 'F':
22513 /* %F displays the frame name. */
22514 if (!NILP (f->title))
22515 return SSDATA (f->title);
22516 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22517 return SSDATA (f->name);
22518 return "Emacs";
22519
22520 case 'f':
22521 obj = BVAR (b, filename);
22522 break;
22523
22524 case 'i':
22525 {
22526 ptrdiff_t size = ZV - BEGV;
22527 pint2str (decode_mode_spec_buf, width, size);
22528 return decode_mode_spec_buf;
22529 }
22530
22531 case 'I':
22532 {
22533 ptrdiff_t size = ZV - BEGV;
22534 pint2hrstr (decode_mode_spec_buf, width, size);
22535 return decode_mode_spec_buf;
22536 }
22537
22538 case 'l':
22539 {
22540 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22541 ptrdiff_t topline, nlines, height;
22542 ptrdiff_t junk;
22543
22544 /* %c and %l are ignored in `frame-title-format'. */
22545 if (mode_line_target == MODE_LINE_TITLE)
22546 return "";
22547
22548 startpos = marker_position (w->start);
22549 startpos_byte = marker_byte_position (w->start);
22550 height = WINDOW_TOTAL_LINES (w);
22551
22552 /* If we decided that this buffer isn't suitable for line numbers,
22553 don't forget that too fast. */
22554 if (w->base_line_pos == -1)
22555 goto no_value;
22556
22557 /* If the buffer is very big, don't waste time. */
22558 if (INTEGERP (Vline_number_display_limit)
22559 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22560 {
22561 w->base_line_pos = 0;
22562 w->base_line_number = 0;
22563 goto no_value;
22564 }
22565
22566 if (w->base_line_number > 0
22567 && w->base_line_pos > 0
22568 && w->base_line_pos <= startpos)
22569 {
22570 line = w->base_line_number;
22571 linepos = w->base_line_pos;
22572 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22573 }
22574 else
22575 {
22576 line = 1;
22577 linepos = BUF_BEGV (b);
22578 linepos_byte = BUF_BEGV_BYTE (b);
22579 }
22580
22581 /* Count lines from base line to window start position. */
22582 nlines = display_count_lines (linepos_byte,
22583 startpos_byte,
22584 startpos, &junk);
22585
22586 topline = nlines + line;
22587
22588 /* Determine a new base line, if the old one is too close
22589 or too far away, or if we did not have one.
22590 "Too close" means it's plausible a scroll-down would
22591 go back past it. */
22592 if (startpos == BUF_BEGV (b))
22593 {
22594 w->base_line_number = topline;
22595 w->base_line_pos = BUF_BEGV (b);
22596 }
22597 else if (nlines < height + 25 || nlines > height * 3 + 50
22598 || linepos == BUF_BEGV (b))
22599 {
22600 ptrdiff_t limit = BUF_BEGV (b);
22601 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22602 ptrdiff_t position;
22603 ptrdiff_t distance =
22604 (height * 2 + 30) * line_number_display_limit_width;
22605
22606 if (startpos - distance > limit)
22607 {
22608 limit = startpos - distance;
22609 limit_byte = CHAR_TO_BYTE (limit);
22610 }
22611
22612 nlines = display_count_lines (startpos_byte,
22613 limit_byte,
22614 - (height * 2 + 30),
22615 &position);
22616 /* If we couldn't find the lines we wanted within
22617 line_number_display_limit_width chars per line,
22618 give up on line numbers for this window. */
22619 if (position == limit_byte && limit == startpos - distance)
22620 {
22621 w->base_line_pos = -1;
22622 w->base_line_number = 0;
22623 goto no_value;
22624 }
22625
22626 w->base_line_number = topline - nlines;
22627 w->base_line_pos = BYTE_TO_CHAR (position);
22628 }
22629
22630 /* Now count lines from the start pos to point. */
22631 nlines = display_count_lines (startpos_byte,
22632 PT_BYTE, PT, &junk);
22633
22634 /* Record that we did display the line number. */
22635 line_number_displayed = 1;
22636
22637 /* Make the string to show. */
22638 pint2str (decode_mode_spec_buf, width, topline + nlines);
22639 return decode_mode_spec_buf;
22640 no_value:
22641 {
22642 char *p = decode_mode_spec_buf;
22643 int pad = width - 2;
22644 while (pad-- > 0)
22645 *p++ = ' ';
22646 *p++ = '?';
22647 *p++ = '?';
22648 *p = '\0';
22649 return decode_mode_spec_buf;
22650 }
22651 }
22652 break;
22653
22654 case 'm':
22655 obj = BVAR (b, mode_name);
22656 break;
22657
22658 case 'n':
22659 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22660 return " Narrow";
22661 break;
22662
22663 case 'p':
22664 {
22665 ptrdiff_t pos = marker_position (w->start);
22666 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22667
22668 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22669 {
22670 if (pos <= BUF_BEGV (b))
22671 return "All";
22672 else
22673 return "Bottom";
22674 }
22675 else if (pos <= BUF_BEGV (b))
22676 return "Top";
22677 else
22678 {
22679 if (total > 1000000)
22680 /* Do it differently for a large value, to avoid overflow. */
22681 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22682 else
22683 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22684 /* We can't normally display a 3-digit number,
22685 so get us a 2-digit number that is close. */
22686 if (total == 100)
22687 total = 99;
22688 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22689 return decode_mode_spec_buf;
22690 }
22691 }
22692
22693 /* Display percentage of size above the bottom of the screen. */
22694 case 'P':
22695 {
22696 ptrdiff_t toppos = marker_position (w->start);
22697 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22698 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22699
22700 if (botpos >= BUF_ZV (b))
22701 {
22702 if (toppos <= BUF_BEGV (b))
22703 return "All";
22704 else
22705 return "Bottom";
22706 }
22707 else
22708 {
22709 if (total > 1000000)
22710 /* Do it differently for a large value, to avoid overflow. */
22711 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22712 else
22713 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22714 /* We can't normally display a 3-digit number,
22715 so get us a 2-digit number that is close. */
22716 if (total == 100)
22717 total = 99;
22718 if (toppos <= BUF_BEGV (b))
22719 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22720 else
22721 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22722 return decode_mode_spec_buf;
22723 }
22724 }
22725
22726 case 's':
22727 /* status of process */
22728 obj = Fget_buffer_process (Fcurrent_buffer ());
22729 if (NILP (obj))
22730 return "no process";
22731 #ifndef MSDOS
22732 obj = Fsymbol_name (Fprocess_status (obj));
22733 #endif
22734 break;
22735
22736 case '@':
22737 {
22738 ptrdiff_t count = inhibit_garbage_collection ();
22739 Lisp_Object val = call1 (intern ("file-remote-p"),
22740 BVAR (current_buffer, directory));
22741 unbind_to (count, Qnil);
22742
22743 if (NILP (val))
22744 return "-";
22745 else
22746 return "@";
22747 }
22748
22749 case 'z':
22750 /* coding-system (not including end-of-line format) */
22751 case 'Z':
22752 /* coding-system (including end-of-line type) */
22753 {
22754 int eol_flag = (c == 'Z');
22755 char *p = decode_mode_spec_buf;
22756
22757 if (! FRAME_WINDOW_P (f))
22758 {
22759 /* No need to mention EOL here--the terminal never needs
22760 to do EOL conversion. */
22761 p = decode_mode_spec_coding (CODING_ID_NAME
22762 (FRAME_KEYBOARD_CODING (f)->id),
22763 p, 0);
22764 p = decode_mode_spec_coding (CODING_ID_NAME
22765 (FRAME_TERMINAL_CODING (f)->id),
22766 p, 0);
22767 }
22768 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22769 p, eol_flag);
22770
22771 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22772 #ifdef subprocesses
22773 obj = Fget_buffer_process (Fcurrent_buffer ());
22774 if (PROCESSP (obj))
22775 {
22776 p = decode_mode_spec_coding
22777 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22778 p = decode_mode_spec_coding
22779 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22780 }
22781 #endif /* subprocesses */
22782 #endif /* 0 */
22783 *p = 0;
22784 return decode_mode_spec_buf;
22785 }
22786 }
22787
22788 if (STRINGP (obj))
22789 {
22790 *string = obj;
22791 return SSDATA (obj);
22792 }
22793 else
22794 return "";
22795 }
22796
22797
22798 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22799 means count lines back from START_BYTE. But don't go beyond
22800 LIMIT_BYTE. Return the number of lines thus found (always
22801 nonnegative).
22802
22803 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22804 either the position COUNT lines after/before START_BYTE, if we
22805 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22806 COUNT lines. */
22807
22808 static ptrdiff_t
22809 display_count_lines (ptrdiff_t start_byte,
22810 ptrdiff_t limit_byte, ptrdiff_t count,
22811 ptrdiff_t *byte_pos_ptr)
22812 {
22813 register unsigned char *cursor;
22814 unsigned char *base;
22815
22816 register ptrdiff_t ceiling;
22817 register unsigned char *ceiling_addr;
22818 ptrdiff_t orig_count = count;
22819
22820 /* If we are not in selective display mode,
22821 check only for newlines. */
22822 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22823 && !INTEGERP (BVAR (current_buffer, selective_display)));
22824
22825 if (count > 0)
22826 {
22827 while (start_byte < limit_byte)
22828 {
22829 ceiling = BUFFER_CEILING_OF (start_byte);
22830 ceiling = min (limit_byte - 1, ceiling);
22831 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22832 base = (cursor = BYTE_POS_ADDR (start_byte));
22833
22834 do
22835 {
22836 if (selective_display)
22837 {
22838 while (*cursor != '\n' && *cursor != 015
22839 && ++cursor != ceiling_addr)
22840 continue;
22841 if (cursor == ceiling_addr)
22842 break;
22843 }
22844 else
22845 {
22846 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22847 if (! cursor)
22848 break;
22849 }
22850
22851 cursor++;
22852
22853 if (--count == 0)
22854 {
22855 start_byte += cursor - base;
22856 *byte_pos_ptr = start_byte;
22857 return orig_count;
22858 }
22859 }
22860 while (cursor < ceiling_addr);
22861
22862 start_byte += ceiling_addr - base;
22863 }
22864 }
22865 else
22866 {
22867 while (start_byte > limit_byte)
22868 {
22869 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22870 ceiling = max (limit_byte, ceiling);
22871 ceiling_addr = BYTE_POS_ADDR (ceiling);
22872 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22873 while (1)
22874 {
22875 if (selective_display)
22876 {
22877 while (--cursor >= ceiling_addr
22878 && *cursor != '\n' && *cursor != 015)
22879 continue;
22880 if (cursor < ceiling_addr)
22881 break;
22882 }
22883 else
22884 {
22885 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22886 if (! cursor)
22887 break;
22888 }
22889
22890 if (++count == 0)
22891 {
22892 start_byte += cursor - base + 1;
22893 *byte_pos_ptr = start_byte;
22894 /* When scanning backwards, we should
22895 not count the newline posterior to which we stop. */
22896 return - orig_count - 1;
22897 }
22898 }
22899 start_byte += ceiling_addr - base;
22900 }
22901 }
22902
22903 *byte_pos_ptr = limit_byte;
22904
22905 if (count < 0)
22906 return - orig_count + count;
22907 return orig_count - count;
22908
22909 }
22910
22911
22912 \f
22913 /***********************************************************************
22914 Displaying strings
22915 ***********************************************************************/
22916
22917 /* Display a NUL-terminated string, starting with index START.
22918
22919 If STRING is non-null, display that C string. Otherwise, the Lisp
22920 string LISP_STRING is displayed. There's a case that STRING is
22921 non-null and LISP_STRING is not nil. It means STRING is a string
22922 data of LISP_STRING. In that case, we display LISP_STRING while
22923 ignoring its text properties.
22924
22925 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22926 FACE_STRING. Display STRING or LISP_STRING with the face at
22927 FACE_STRING_POS in FACE_STRING:
22928
22929 Display the string in the environment given by IT, but use the
22930 standard display table, temporarily.
22931
22932 FIELD_WIDTH is the minimum number of output glyphs to produce.
22933 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22934 with spaces. If STRING has more characters, more than FIELD_WIDTH
22935 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22936
22937 PRECISION is the maximum number of characters to output from
22938 STRING. PRECISION < 0 means don't truncate the string.
22939
22940 This is roughly equivalent to printf format specifiers:
22941
22942 FIELD_WIDTH PRECISION PRINTF
22943 ----------------------------------------
22944 -1 -1 %s
22945 -1 10 %.10s
22946 10 -1 %10s
22947 20 10 %20.10s
22948
22949 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22950 display them, and < 0 means obey the current buffer's value of
22951 enable_multibyte_characters.
22952
22953 Value is the number of columns displayed. */
22954
22955 static int
22956 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22957 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22958 int field_width, int precision, int max_x, int multibyte)
22959 {
22960 int hpos_at_start = it->hpos;
22961 int saved_face_id = it->face_id;
22962 struct glyph_row *row = it->glyph_row;
22963 ptrdiff_t it_charpos;
22964
22965 /* Initialize the iterator IT for iteration over STRING beginning
22966 with index START. */
22967 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22968 precision, field_width, multibyte);
22969 if (string && STRINGP (lisp_string))
22970 /* LISP_STRING is the one returned by decode_mode_spec. We should
22971 ignore its text properties. */
22972 it->stop_charpos = it->end_charpos;
22973
22974 /* If displaying STRING, set up the face of the iterator from
22975 FACE_STRING, if that's given. */
22976 if (STRINGP (face_string))
22977 {
22978 ptrdiff_t endptr;
22979 struct face *face;
22980
22981 it->face_id
22982 = face_at_string_position (it->w, face_string, face_string_pos,
22983 0, &endptr, it->base_face_id, 0);
22984 face = FACE_FROM_ID (it->f, it->face_id);
22985 it->face_box_p = face->box != FACE_NO_BOX;
22986 }
22987
22988 /* Set max_x to the maximum allowed X position. Don't let it go
22989 beyond the right edge of the window. */
22990 if (max_x <= 0)
22991 max_x = it->last_visible_x;
22992 else
22993 max_x = min (max_x, it->last_visible_x);
22994
22995 /* Skip over display elements that are not visible. because IT->w is
22996 hscrolled. */
22997 if (it->current_x < it->first_visible_x)
22998 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22999 MOVE_TO_POS | MOVE_TO_X);
23000
23001 row->ascent = it->max_ascent;
23002 row->height = it->max_ascent + it->max_descent;
23003 row->phys_ascent = it->max_phys_ascent;
23004 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23005 row->extra_line_spacing = it->max_extra_line_spacing;
23006
23007 if (STRINGP (it->string))
23008 it_charpos = IT_STRING_CHARPOS (*it);
23009 else
23010 it_charpos = IT_CHARPOS (*it);
23011
23012 /* This condition is for the case that we are called with current_x
23013 past last_visible_x. */
23014 while (it->current_x < max_x)
23015 {
23016 int x_before, x, n_glyphs_before, i, nglyphs;
23017
23018 /* Get the next display element. */
23019 if (!get_next_display_element (it))
23020 break;
23021
23022 /* Produce glyphs. */
23023 x_before = it->current_x;
23024 n_glyphs_before = row->used[TEXT_AREA];
23025 PRODUCE_GLYPHS (it);
23026
23027 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23028 i = 0;
23029 x = x_before;
23030 while (i < nglyphs)
23031 {
23032 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23033
23034 if (it->line_wrap != TRUNCATE
23035 && x + glyph->pixel_width > max_x)
23036 {
23037 /* End of continued line or max_x reached. */
23038 if (CHAR_GLYPH_PADDING_P (*glyph))
23039 {
23040 /* A wide character is unbreakable. */
23041 if (row->reversed_p)
23042 unproduce_glyphs (it, row->used[TEXT_AREA]
23043 - n_glyphs_before);
23044 row->used[TEXT_AREA] = n_glyphs_before;
23045 it->current_x = x_before;
23046 }
23047 else
23048 {
23049 if (row->reversed_p)
23050 unproduce_glyphs (it, row->used[TEXT_AREA]
23051 - (n_glyphs_before + i));
23052 row->used[TEXT_AREA] = n_glyphs_before + i;
23053 it->current_x = x;
23054 }
23055 break;
23056 }
23057 else if (x + glyph->pixel_width >= it->first_visible_x)
23058 {
23059 /* Glyph is at least partially visible. */
23060 ++it->hpos;
23061 if (x < it->first_visible_x)
23062 row->x = x - it->first_visible_x;
23063 }
23064 else
23065 {
23066 /* Glyph is off the left margin of the display area.
23067 Should not happen. */
23068 emacs_abort ();
23069 }
23070
23071 row->ascent = max (row->ascent, it->max_ascent);
23072 row->height = max (row->height, it->max_ascent + it->max_descent);
23073 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23074 row->phys_height = max (row->phys_height,
23075 it->max_phys_ascent + it->max_phys_descent);
23076 row->extra_line_spacing = max (row->extra_line_spacing,
23077 it->max_extra_line_spacing);
23078 x += glyph->pixel_width;
23079 ++i;
23080 }
23081
23082 /* Stop if max_x reached. */
23083 if (i < nglyphs)
23084 break;
23085
23086 /* Stop at line ends. */
23087 if (ITERATOR_AT_END_OF_LINE_P (it))
23088 {
23089 it->continuation_lines_width = 0;
23090 break;
23091 }
23092
23093 set_iterator_to_next (it, 1);
23094 if (STRINGP (it->string))
23095 it_charpos = IT_STRING_CHARPOS (*it);
23096 else
23097 it_charpos = IT_CHARPOS (*it);
23098
23099 /* Stop if truncating at the right edge. */
23100 if (it->line_wrap == TRUNCATE
23101 && it->current_x >= it->last_visible_x)
23102 {
23103 /* Add truncation mark, but don't do it if the line is
23104 truncated at a padding space. */
23105 if (it_charpos < it->string_nchars)
23106 {
23107 if (!FRAME_WINDOW_P (it->f))
23108 {
23109 int ii, n;
23110
23111 if (it->current_x > it->last_visible_x)
23112 {
23113 if (!row->reversed_p)
23114 {
23115 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23116 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23117 break;
23118 }
23119 else
23120 {
23121 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23122 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23123 break;
23124 unproduce_glyphs (it, ii + 1);
23125 ii = row->used[TEXT_AREA] - (ii + 1);
23126 }
23127 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23128 {
23129 row->used[TEXT_AREA] = ii;
23130 produce_special_glyphs (it, IT_TRUNCATION);
23131 }
23132 }
23133 produce_special_glyphs (it, IT_TRUNCATION);
23134 }
23135 row->truncated_on_right_p = 1;
23136 }
23137 break;
23138 }
23139 }
23140
23141 /* Maybe insert a truncation at the left. */
23142 if (it->first_visible_x
23143 && it_charpos > 0)
23144 {
23145 if (!FRAME_WINDOW_P (it->f)
23146 || (row->reversed_p
23147 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23148 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23149 insert_left_trunc_glyphs (it);
23150 row->truncated_on_left_p = 1;
23151 }
23152
23153 it->face_id = saved_face_id;
23154
23155 /* Value is number of columns displayed. */
23156 return it->hpos - hpos_at_start;
23157 }
23158
23159
23160 \f
23161 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23162 appears as an element of LIST or as the car of an element of LIST.
23163 If PROPVAL is a list, compare each element against LIST in that
23164 way, and return 1/2 if any element of PROPVAL is found in LIST.
23165 Otherwise return 0. This function cannot quit.
23166 The return value is 2 if the text is invisible but with an ellipsis
23167 and 1 if it's invisible and without an ellipsis. */
23168
23169 int
23170 invisible_p (register Lisp_Object propval, Lisp_Object list)
23171 {
23172 register Lisp_Object tail, proptail;
23173
23174 for (tail = list; CONSP (tail); tail = XCDR (tail))
23175 {
23176 register Lisp_Object tem;
23177 tem = XCAR (tail);
23178 if (EQ (propval, tem))
23179 return 1;
23180 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23181 return NILP (XCDR (tem)) ? 1 : 2;
23182 }
23183
23184 if (CONSP (propval))
23185 {
23186 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23187 {
23188 Lisp_Object propelt;
23189 propelt = XCAR (proptail);
23190 for (tail = list; CONSP (tail); tail = XCDR (tail))
23191 {
23192 register Lisp_Object tem;
23193 tem = XCAR (tail);
23194 if (EQ (propelt, tem))
23195 return 1;
23196 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23197 return NILP (XCDR (tem)) ? 1 : 2;
23198 }
23199 }
23200 }
23201
23202 return 0;
23203 }
23204
23205 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23206 doc: /* Non-nil if the property makes the text invisible.
23207 POS-OR-PROP can be a marker or number, in which case it is taken to be
23208 a position in the current buffer and the value of the `invisible' property
23209 is checked; or it can be some other value, which is then presumed to be the
23210 value of the `invisible' property of the text of interest.
23211 The non-nil value returned can be t for truly invisible text or something
23212 else if the text is replaced by an ellipsis. */)
23213 (Lisp_Object pos_or_prop)
23214 {
23215 Lisp_Object prop
23216 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23217 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23218 : pos_or_prop);
23219 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23220 return (invis == 0 ? Qnil
23221 : invis == 1 ? Qt
23222 : make_number (invis));
23223 }
23224
23225 /* Calculate a width or height in pixels from a specification using
23226 the following elements:
23227
23228 SPEC ::=
23229 NUM - a (fractional) multiple of the default font width/height
23230 (NUM) - specifies exactly NUM pixels
23231 UNIT - a fixed number of pixels, see below.
23232 ELEMENT - size of a display element in pixels, see below.
23233 (NUM . SPEC) - equals NUM * SPEC
23234 (+ SPEC SPEC ...) - add pixel values
23235 (- SPEC SPEC ...) - subtract pixel values
23236 (- SPEC) - negate pixel value
23237
23238 NUM ::=
23239 INT or FLOAT - a number constant
23240 SYMBOL - use symbol's (buffer local) variable binding.
23241
23242 UNIT ::=
23243 in - pixels per inch *)
23244 mm - pixels per 1/1000 meter *)
23245 cm - pixels per 1/100 meter *)
23246 width - width of current font in pixels.
23247 height - height of current font in pixels.
23248
23249 *) using the ratio(s) defined in display-pixels-per-inch.
23250
23251 ELEMENT ::=
23252
23253 left-fringe - left fringe width in pixels
23254 right-fringe - right fringe width in pixels
23255
23256 left-margin - left margin width in pixels
23257 right-margin - right margin width in pixels
23258
23259 scroll-bar - scroll-bar area width in pixels
23260
23261 Examples:
23262
23263 Pixels corresponding to 5 inches:
23264 (5 . in)
23265
23266 Total width of non-text areas on left side of window (if scroll-bar is on left):
23267 '(space :width (+ left-fringe left-margin scroll-bar))
23268
23269 Align to first text column (in header line):
23270 '(space :align-to 0)
23271
23272 Align to middle of text area minus half the width of variable `my-image'
23273 containing a loaded image:
23274 '(space :align-to (0.5 . (- text my-image)))
23275
23276 Width of left margin minus width of 1 character in the default font:
23277 '(space :width (- left-margin 1))
23278
23279 Width of left margin minus width of 2 characters in the current font:
23280 '(space :width (- left-margin (2 . width)))
23281
23282 Center 1 character over left-margin (in header line):
23283 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23284
23285 Different ways to express width of left fringe plus left margin minus one pixel:
23286 '(space :width (- (+ left-fringe left-margin) (1)))
23287 '(space :width (+ left-fringe left-margin (- (1))))
23288 '(space :width (+ left-fringe left-margin (-1)))
23289
23290 */
23291
23292 static int
23293 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23294 struct font *font, int width_p, int *align_to)
23295 {
23296 double pixels;
23297
23298 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23299 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23300
23301 if (NILP (prop))
23302 return OK_PIXELS (0);
23303
23304 eassert (FRAME_LIVE_P (it->f));
23305
23306 if (SYMBOLP (prop))
23307 {
23308 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23309 {
23310 char *unit = SSDATA (SYMBOL_NAME (prop));
23311
23312 if (unit[0] == 'i' && unit[1] == 'n')
23313 pixels = 1.0;
23314 else if (unit[0] == 'm' && unit[1] == 'm')
23315 pixels = 25.4;
23316 else if (unit[0] == 'c' && unit[1] == 'm')
23317 pixels = 2.54;
23318 else
23319 pixels = 0;
23320 if (pixels > 0)
23321 {
23322 double ppi = (width_p ? FRAME_RES_X (it->f)
23323 : FRAME_RES_Y (it->f));
23324
23325 if (ppi > 0)
23326 return OK_PIXELS (ppi / pixels);
23327 return 0;
23328 }
23329 }
23330
23331 #ifdef HAVE_WINDOW_SYSTEM
23332 if (EQ (prop, Qheight))
23333 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23334 if (EQ (prop, Qwidth))
23335 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23336 #else
23337 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23338 return OK_PIXELS (1);
23339 #endif
23340
23341 if (EQ (prop, Qtext))
23342 return OK_PIXELS (width_p
23343 ? window_box_width (it->w, TEXT_AREA)
23344 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23345
23346 if (align_to && *align_to < 0)
23347 {
23348 *res = 0;
23349 if (EQ (prop, Qleft))
23350 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23351 if (EQ (prop, Qright))
23352 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23353 if (EQ (prop, Qcenter))
23354 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23355 + window_box_width (it->w, TEXT_AREA) / 2);
23356 if (EQ (prop, Qleft_fringe))
23357 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23358 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23359 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23360 if (EQ (prop, Qright_fringe))
23361 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23362 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23363 : window_box_right_offset (it->w, TEXT_AREA));
23364 if (EQ (prop, Qleft_margin))
23365 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23366 if (EQ (prop, Qright_margin))
23367 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23368 if (EQ (prop, Qscroll_bar))
23369 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23370 ? 0
23371 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23372 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23373 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23374 : 0)));
23375 }
23376 else
23377 {
23378 if (EQ (prop, Qleft_fringe))
23379 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23380 if (EQ (prop, Qright_fringe))
23381 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23382 if (EQ (prop, Qleft_margin))
23383 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23384 if (EQ (prop, Qright_margin))
23385 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23386 if (EQ (prop, Qscroll_bar))
23387 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23388 }
23389
23390 prop = buffer_local_value_1 (prop, it->w->contents);
23391 if (EQ (prop, Qunbound))
23392 prop = Qnil;
23393 }
23394
23395 if (INTEGERP (prop) || FLOATP (prop))
23396 {
23397 int base_unit = (width_p
23398 ? FRAME_COLUMN_WIDTH (it->f)
23399 : FRAME_LINE_HEIGHT (it->f));
23400 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23401 }
23402
23403 if (CONSP (prop))
23404 {
23405 Lisp_Object car = XCAR (prop);
23406 Lisp_Object cdr = XCDR (prop);
23407
23408 if (SYMBOLP (car))
23409 {
23410 #ifdef HAVE_WINDOW_SYSTEM
23411 if (FRAME_WINDOW_P (it->f)
23412 && valid_image_p (prop))
23413 {
23414 ptrdiff_t id = lookup_image (it->f, prop);
23415 struct image *img = IMAGE_FROM_ID (it->f, id);
23416
23417 return OK_PIXELS (width_p ? img->width : img->height);
23418 }
23419 #endif
23420 if (EQ (car, Qplus) || EQ (car, Qminus))
23421 {
23422 int first = 1;
23423 double px;
23424
23425 pixels = 0;
23426 while (CONSP (cdr))
23427 {
23428 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23429 font, width_p, align_to))
23430 return 0;
23431 if (first)
23432 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23433 else
23434 pixels += px;
23435 cdr = XCDR (cdr);
23436 }
23437 if (EQ (car, Qminus))
23438 pixels = -pixels;
23439 return OK_PIXELS (pixels);
23440 }
23441
23442 car = buffer_local_value_1 (car, it->w->contents);
23443 if (EQ (car, Qunbound))
23444 car = Qnil;
23445 }
23446
23447 if (INTEGERP (car) || FLOATP (car))
23448 {
23449 double fact;
23450 pixels = XFLOATINT (car);
23451 if (NILP (cdr))
23452 return OK_PIXELS (pixels);
23453 if (calc_pixel_width_or_height (&fact, it, cdr,
23454 font, width_p, align_to))
23455 return OK_PIXELS (pixels * fact);
23456 return 0;
23457 }
23458
23459 return 0;
23460 }
23461
23462 return 0;
23463 }
23464
23465 \f
23466 /***********************************************************************
23467 Glyph Display
23468 ***********************************************************************/
23469
23470 #ifdef HAVE_WINDOW_SYSTEM
23471
23472 #ifdef GLYPH_DEBUG
23473
23474 void
23475 dump_glyph_string (struct glyph_string *s)
23476 {
23477 fprintf (stderr, "glyph string\n");
23478 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23479 s->x, s->y, s->width, s->height);
23480 fprintf (stderr, " ybase = %d\n", s->ybase);
23481 fprintf (stderr, " hl = %d\n", s->hl);
23482 fprintf (stderr, " left overhang = %d, right = %d\n",
23483 s->left_overhang, s->right_overhang);
23484 fprintf (stderr, " nchars = %d\n", s->nchars);
23485 fprintf (stderr, " extends to end of line = %d\n",
23486 s->extends_to_end_of_line_p);
23487 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23488 fprintf (stderr, " bg width = %d\n", s->background_width);
23489 }
23490
23491 #endif /* GLYPH_DEBUG */
23492
23493 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23494 of XChar2b structures for S; it can't be allocated in
23495 init_glyph_string because it must be allocated via `alloca'. W
23496 is the window on which S is drawn. ROW and AREA are the glyph row
23497 and area within the row from which S is constructed. START is the
23498 index of the first glyph structure covered by S. HL is a
23499 face-override for drawing S. */
23500
23501 #ifdef HAVE_NTGUI
23502 #define OPTIONAL_HDC(hdc) HDC hdc,
23503 #define DECLARE_HDC(hdc) HDC hdc;
23504 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23505 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23506 #endif
23507
23508 #ifndef OPTIONAL_HDC
23509 #define OPTIONAL_HDC(hdc)
23510 #define DECLARE_HDC(hdc)
23511 #define ALLOCATE_HDC(hdc, f)
23512 #define RELEASE_HDC(hdc, f)
23513 #endif
23514
23515 static void
23516 init_glyph_string (struct glyph_string *s,
23517 OPTIONAL_HDC (hdc)
23518 XChar2b *char2b, struct window *w, struct glyph_row *row,
23519 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23520 {
23521 memset (s, 0, sizeof *s);
23522 s->w = w;
23523 s->f = XFRAME (w->frame);
23524 #ifdef HAVE_NTGUI
23525 s->hdc = hdc;
23526 #endif
23527 s->display = FRAME_X_DISPLAY (s->f);
23528 s->window = FRAME_X_WINDOW (s->f);
23529 s->char2b = char2b;
23530 s->hl = hl;
23531 s->row = row;
23532 s->area = area;
23533 s->first_glyph = row->glyphs[area] + start;
23534 s->height = row->height;
23535 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23536 s->ybase = s->y + row->ascent;
23537 }
23538
23539
23540 /* Append the list of glyph strings with head H and tail T to the list
23541 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23542
23543 static void
23544 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23545 struct glyph_string *h, struct glyph_string *t)
23546 {
23547 if (h)
23548 {
23549 if (*head)
23550 (*tail)->next = h;
23551 else
23552 *head = h;
23553 h->prev = *tail;
23554 *tail = t;
23555 }
23556 }
23557
23558
23559 /* Prepend the list of glyph strings with head H and tail T to the
23560 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23561 result. */
23562
23563 static void
23564 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23565 struct glyph_string *h, struct glyph_string *t)
23566 {
23567 if (h)
23568 {
23569 if (*head)
23570 (*head)->prev = t;
23571 else
23572 *tail = t;
23573 t->next = *head;
23574 *head = h;
23575 }
23576 }
23577
23578
23579 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23580 Set *HEAD and *TAIL to the resulting list. */
23581
23582 static void
23583 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23584 struct glyph_string *s)
23585 {
23586 s->next = s->prev = NULL;
23587 append_glyph_string_lists (head, tail, s, s);
23588 }
23589
23590
23591 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23592 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23593 make sure that X resources for the face returned are allocated.
23594 Value is a pointer to a realized face that is ready for display if
23595 DISPLAY_P is non-zero. */
23596
23597 static struct face *
23598 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23599 XChar2b *char2b, int display_p)
23600 {
23601 struct face *face = FACE_FROM_ID (f, face_id);
23602 unsigned code = 0;
23603
23604 if (face->font)
23605 {
23606 code = face->font->driver->encode_char (face->font, c);
23607
23608 if (code == FONT_INVALID_CODE)
23609 code = 0;
23610 }
23611 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23612
23613 /* Make sure X resources of the face are allocated. */
23614 #ifdef HAVE_X_WINDOWS
23615 if (display_p)
23616 #endif
23617 {
23618 eassert (face != NULL);
23619 PREPARE_FACE_FOR_DISPLAY (f, face);
23620 }
23621
23622 return face;
23623 }
23624
23625
23626 /* Get face and two-byte form of character glyph GLYPH on frame F.
23627 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23628 a pointer to a realized face that is ready for display. */
23629
23630 static struct face *
23631 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23632 XChar2b *char2b, int *two_byte_p)
23633 {
23634 struct face *face;
23635 unsigned code = 0;
23636
23637 eassert (glyph->type == CHAR_GLYPH);
23638 face = FACE_FROM_ID (f, glyph->face_id);
23639
23640 /* Make sure X resources of the face are allocated. */
23641 eassert (face != NULL);
23642 PREPARE_FACE_FOR_DISPLAY (f, face);
23643
23644 if (two_byte_p)
23645 *two_byte_p = 0;
23646
23647 if (face->font)
23648 {
23649 if (CHAR_BYTE8_P (glyph->u.ch))
23650 code = CHAR_TO_BYTE8 (glyph->u.ch);
23651 else
23652 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23653
23654 if (code == FONT_INVALID_CODE)
23655 code = 0;
23656 }
23657
23658 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23659 return face;
23660 }
23661
23662
23663 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23664 Return 1 if FONT has a glyph for C, otherwise return 0. */
23665
23666 static int
23667 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23668 {
23669 unsigned code;
23670
23671 if (CHAR_BYTE8_P (c))
23672 code = CHAR_TO_BYTE8 (c);
23673 else
23674 code = font->driver->encode_char (font, c);
23675
23676 if (code == FONT_INVALID_CODE)
23677 return 0;
23678 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23679 return 1;
23680 }
23681
23682
23683 /* Fill glyph string S with composition components specified by S->cmp.
23684
23685 BASE_FACE is the base face of the composition.
23686 S->cmp_from is the index of the first component for S.
23687
23688 OVERLAPS non-zero means S should draw the foreground only, and use
23689 its physical height for clipping. See also draw_glyphs.
23690
23691 Value is the index of a component not in S. */
23692
23693 static int
23694 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23695 int overlaps)
23696 {
23697 int i;
23698 /* For all glyphs of this composition, starting at the offset
23699 S->cmp_from, until we reach the end of the definition or encounter a
23700 glyph that requires the different face, add it to S. */
23701 struct face *face;
23702
23703 eassert (s);
23704
23705 s->for_overlaps = overlaps;
23706 s->face = NULL;
23707 s->font = NULL;
23708 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23709 {
23710 int c = COMPOSITION_GLYPH (s->cmp, i);
23711
23712 /* TAB in a composition means display glyphs with padding space
23713 on the left or right. */
23714 if (c != '\t')
23715 {
23716 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23717 -1, Qnil);
23718
23719 face = get_char_face_and_encoding (s->f, c, face_id,
23720 s->char2b + i, 1);
23721 if (face)
23722 {
23723 if (! s->face)
23724 {
23725 s->face = face;
23726 s->font = s->face->font;
23727 }
23728 else if (s->face != face)
23729 break;
23730 }
23731 }
23732 ++s->nchars;
23733 }
23734 s->cmp_to = i;
23735
23736 if (s->face == NULL)
23737 {
23738 s->face = base_face->ascii_face;
23739 s->font = s->face->font;
23740 }
23741
23742 /* All glyph strings for the same composition has the same width,
23743 i.e. the width set for the first component of the composition. */
23744 s->width = s->first_glyph->pixel_width;
23745
23746 /* If the specified font could not be loaded, use the frame's
23747 default font, but record the fact that we couldn't load it in
23748 the glyph string so that we can draw rectangles for the
23749 characters of the glyph string. */
23750 if (s->font == NULL)
23751 {
23752 s->font_not_found_p = 1;
23753 s->font = FRAME_FONT (s->f);
23754 }
23755
23756 /* Adjust base line for subscript/superscript text. */
23757 s->ybase += s->first_glyph->voffset;
23758
23759 /* This glyph string must always be drawn with 16-bit functions. */
23760 s->two_byte_p = 1;
23761
23762 return s->cmp_to;
23763 }
23764
23765 static int
23766 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23767 int start, int end, int overlaps)
23768 {
23769 struct glyph *glyph, *last;
23770 Lisp_Object lgstring;
23771 int i;
23772
23773 s->for_overlaps = overlaps;
23774 glyph = s->row->glyphs[s->area] + start;
23775 last = s->row->glyphs[s->area] + end;
23776 s->cmp_id = glyph->u.cmp.id;
23777 s->cmp_from = glyph->slice.cmp.from;
23778 s->cmp_to = glyph->slice.cmp.to + 1;
23779 s->face = FACE_FROM_ID (s->f, face_id);
23780 lgstring = composition_gstring_from_id (s->cmp_id);
23781 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23782 glyph++;
23783 while (glyph < last
23784 && glyph->u.cmp.automatic
23785 && glyph->u.cmp.id == s->cmp_id
23786 && s->cmp_to == glyph->slice.cmp.from)
23787 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23788
23789 for (i = s->cmp_from; i < s->cmp_to; i++)
23790 {
23791 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23792 unsigned code = LGLYPH_CODE (lglyph);
23793
23794 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23795 }
23796 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23797 return glyph - s->row->glyphs[s->area];
23798 }
23799
23800
23801 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23802 See the comment of fill_glyph_string for arguments.
23803 Value is the index of the first glyph not in S. */
23804
23805
23806 static int
23807 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23808 int start, int end, int overlaps)
23809 {
23810 struct glyph *glyph, *last;
23811 int voffset;
23812
23813 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23814 s->for_overlaps = overlaps;
23815 glyph = s->row->glyphs[s->area] + start;
23816 last = s->row->glyphs[s->area] + end;
23817 voffset = glyph->voffset;
23818 s->face = FACE_FROM_ID (s->f, face_id);
23819 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23820 s->nchars = 1;
23821 s->width = glyph->pixel_width;
23822 glyph++;
23823 while (glyph < last
23824 && glyph->type == GLYPHLESS_GLYPH
23825 && glyph->voffset == voffset
23826 && glyph->face_id == face_id)
23827 {
23828 s->nchars++;
23829 s->width += glyph->pixel_width;
23830 glyph++;
23831 }
23832 s->ybase += voffset;
23833 return glyph - s->row->glyphs[s->area];
23834 }
23835
23836
23837 /* Fill glyph string S from a sequence of character glyphs.
23838
23839 FACE_ID is the face id of the string. START is the index of the
23840 first glyph to consider, END is the index of the last + 1.
23841 OVERLAPS non-zero means S should draw the foreground only, and use
23842 its physical height for clipping. See also draw_glyphs.
23843
23844 Value is the index of the first glyph not in S. */
23845
23846 static int
23847 fill_glyph_string (struct glyph_string *s, int face_id,
23848 int start, int end, int overlaps)
23849 {
23850 struct glyph *glyph, *last;
23851 int voffset;
23852 int glyph_not_available_p;
23853
23854 eassert (s->f == XFRAME (s->w->frame));
23855 eassert (s->nchars == 0);
23856 eassert (start >= 0 && end > start);
23857
23858 s->for_overlaps = overlaps;
23859 glyph = s->row->glyphs[s->area] + start;
23860 last = s->row->glyphs[s->area] + end;
23861 voffset = glyph->voffset;
23862 s->padding_p = glyph->padding_p;
23863 glyph_not_available_p = glyph->glyph_not_available_p;
23864
23865 while (glyph < last
23866 && glyph->type == CHAR_GLYPH
23867 && glyph->voffset == voffset
23868 /* Same face id implies same font, nowadays. */
23869 && glyph->face_id == face_id
23870 && glyph->glyph_not_available_p == glyph_not_available_p)
23871 {
23872 int two_byte_p;
23873
23874 s->face = get_glyph_face_and_encoding (s->f, glyph,
23875 s->char2b + s->nchars,
23876 &two_byte_p);
23877 s->two_byte_p = two_byte_p;
23878 ++s->nchars;
23879 eassert (s->nchars <= end - start);
23880 s->width += glyph->pixel_width;
23881 if (glyph++->padding_p != s->padding_p)
23882 break;
23883 }
23884
23885 s->font = s->face->font;
23886
23887 /* If the specified font could not be loaded, use the frame's font,
23888 but record the fact that we couldn't load it in
23889 S->font_not_found_p so that we can draw rectangles for the
23890 characters of the glyph string. */
23891 if (s->font == NULL || glyph_not_available_p)
23892 {
23893 s->font_not_found_p = 1;
23894 s->font = FRAME_FONT (s->f);
23895 }
23896
23897 /* Adjust base line for subscript/superscript text. */
23898 s->ybase += voffset;
23899
23900 eassert (s->face && s->face->gc);
23901 return glyph - s->row->glyphs[s->area];
23902 }
23903
23904
23905 /* Fill glyph string S from image glyph S->first_glyph. */
23906
23907 static void
23908 fill_image_glyph_string (struct glyph_string *s)
23909 {
23910 eassert (s->first_glyph->type == IMAGE_GLYPH);
23911 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23912 eassert (s->img);
23913 s->slice = s->first_glyph->slice.img;
23914 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23915 s->font = s->face->font;
23916 s->width = s->first_glyph->pixel_width;
23917
23918 /* Adjust base line for subscript/superscript text. */
23919 s->ybase += s->first_glyph->voffset;
23920 }
23921
23922
23923 /* Fill glyph string S from a sequence of stretch glyphs.
23924
23925 START is the index of the first glyph to consider,
23926 END is the index of the last + 1.
23927
23928 Value is the index of the first glyph not in S. */
23929
23930 static int
23931 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23932 {
23933 struct glyph *glyph, *last;
23934 int voffset, face_id;
23935
23936 eassert (s->first_glyph->type == STRETCH_GLYPH);
23937
23938 glyph = s->row->glyphs[s->area] + start;
23939 last = s->row->glyphs[s->area] + end;
23940 face_id = glyph->face_id;
23941 s->face = FACE_FROM_ID (s->f, face_id);
23942 s->font = s->face->font;
23943 s->width = glyph->pixel_width;
23944 s->nchars = 1;
23945 voffset = glyph->voffset;
23946
23947 for (++glyph;
23948 (glyph < last
23949 && glyph->type == STRETCH_GLYPH
23950 && glyph->voffset == voffset
23951 && glyph->face_id == face_id);
23952 ++glyph)
23953 s->width += glyph->pixel_width;
23954
23955 /* Adjust base line for subscript/superscript text. */
23956 s->ybase += voffset;
23957
23958 /* The case that face->gc == 0 is handled when drawing the glyph
23959 string by calling PREPARE_FACE_FOR_DISPLAY. */
23960 eassert (s->face);
23961 return glyph - s->row->glyphs[s->area];
23962 }
23963
23964 static struct font_metrics *
23965 get_per_char_metric (struct font *font, XChar2b *char2b)
23966 {
23967 static struct font_metrics metrics;
23968 unsigned code;
23969
23970 if (! font)
23971 return NULL;
23972 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23973 if (code == FONT_INVALID_CODE)
23974 return NULL;
23975 font->driver->text_extents (font, &code, 1, &metrics);
23976 return &metrics;
23977 }
23978
23979 /* EXPORT for RIF:
23980 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23981 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23982 assumed to be zero. */
23983
23984 void
23985 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23986 {
23987 *left = *right = 0;
23988
23989 if (glyph->type == CHAR_GLYPH)
23990 {
23991 struct face *face;
23992 XChar2b char2b;
23993 struct font_metrics *pcm;
23994
23995 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23996 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23997 {
23998 if (pcm->rbearing > pcm->width)
23999 *right = pcm->rbearing - pcm->width;
24000 if (pcm->lbearing < 0)
24001 *left = -pcm->lbearing;
24002 }
24003 }
24004 else if (glyph->type == COMPOSITE_GLYPH)
24005 {
24006 if (! glyph->u.cmp.automatic)
24007 {
24008 struct composition *cmp = composition_table[glyph->u.cmp.id];
24009
24010 if (cmp->rbearing > cmp->pixel_width)
24011 *right = cmp->rbearing - cmp->pixel_width;
24012 if (cmp->lbearing < 0)
24013 *left = - cmp->lbearing;
24014 }
24015 else
24016 {
24017 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24018 struct font_metrics metrics;
24019
24020 composition_gstring_width (gstring, glyph->slice.cmp.from,
24021 glyph->slice.cmp.to + 1, &metrics);
24022 if (metrics.rbearing > metrics.width)
24023 *right = metrics.rbearing - metrics.width;
24024 if (metrics.lbearing < 0)
24025 *left = - metrics.lbearing;
24026 }
24027 }
24028 }
24029
24030
24031 /* Return the index of the first glyph preceding glyph string S that
24032 is overwritten by S because of S's left overhang. Value is -1
24033 if no glyphs are overwritten. */
24034
24035 static int
24036 left_overwritten (struct glyph_string *s)
24037 {
24038 int k;
24039
24040 if (s->left_overhang)
24041 {
24042 int x = 0, i;
24043 struct glyph *glyphs = s->row->glyphs[s->area];
24044 int first = s->first_glyph - glyphs;
24045
24046 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24047 x -= glyphs[i].pixel_width;
24048
24049 k = i + 1;
24050 }
24051 else
24052 k = -1;
24053
24054 return k;
24055 }
24056
24057
24058 /* Return the index of the first glyph preceding glyph string S that
24059 is overwriting S because of its right overhang. Value is -1 if no
24060 glyph in front of S overwrites S. */
24061
24062 static int
24063 left_overwriting (struct glyph_string *s)
24064 {
24065 int i, k, x;
24066 struct glyph *glyphs = s->row->glyphs[s->area];
24067 int first = s->first_glyph - glyphs;
24068
24069 k = -1;
24070 x = 0;
24071 for (i = first - 1; i >= 0; --i)
24072 {
24073 int left, right;
24074 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24075 if (x + right > 0)
24076 k = i;
24077 x -= glyphs[i].pixel_width;
24078 }
24079
24080 return k;
24081 }
24082
24083
24084 /* Return the index of the last glyph following glyph string S that is
24085 overwritten by S because of S's right overhang. Value is -1 if
24086 no such glyph is found. */
24087
24088 static int
24089 right_overwritten (struct glyph_string *s)
24090 {
24091 int k = -1;
24092
24093 if (s->right_overhang)
24094 {
24095 int x = 0, i;
24096 struct glyph *glyphs = s->row->glyphs[s->area];
24097 int first = (s->first_glyph - glyphs
24098 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24099 int end = s->row->used[s->area];
24100
24101 for (i = first; i < end && s->right_overhang > x; ++i)
24102 x += glyphs[i].pixel_width;
24103
24104 k = i;
24105 }
24106
24107 return k;
24108 }
24109
24110
24111 /* Return the index of the last glyph following glyph string S that
24112 overwrites S because of its left overhang. Value is negative
24113 if no such glyph is found. */
24114
24115 static int
24116 right_overwriting (struct glyph_string *s)
24117 {
24118 int i, k, x;
24119 int end = s->row->used[s->area];
24120 struct glyph *glyphs = s->row->glyphs[s->area];
24121 int first = (s->first_glyph - glyphs
24122 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24123
24124 k = -1;
24125 x = 0;
24126 for (i = first; i < end; ++i)
24127 {
24128 int left, right;
24129 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24130 if (x - left < 0)
24131 k = i;
24132 x += glyphs[i].pixel_width;
24133 }
24134
24135 return k;
24136 }
24137
24138
24139 /* Set background width of glyph string S. START is the index of the
24140 first glyph following S. LAST_X is the right-most x-position + 1
24141 in the drawing area. */
24142
24143 static void
24144 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24145 {
24146 /* If the face of this glyph string has to be drawn to the end of
24147 the drawing area, set S->extends_to_end_of_line_p. */
24148
24149 if (start == s->row->used[s->area]
24150 && ((s->row->fill_line_p
24151 && (s->hl == DRAW_NORMAL_TEXT
24152 || s->hl == DRAW_IMAGE_RAISED
24153 || s->hl == DRAW_IMAGE_SUNKEN))
24154 || s->hl == DRAW_MOUSE_FACE))
24155 s->extends_to_end_of_line_p = 1;
24156
24157 /* If S extends its face to the end of the line, set its
24158 background_width to the distance to the right edge of the drawing
24159 area. */
24160 if (s->extends_to_end_of_line_p)
24161 s->background_width = last_x - s->x + 1;
24162 else
24163 s->background_width = s->width;
24164 }
24165
24166
24167 /* Compute overhangs and x-positions for glyph string S and its
24168 predecessors, or successors. X is the starting x-position for S.
24169 BACKWARD_P non-zero means process predecessors. */
24170
24171 static void
24172 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24173 {
24174 if (backward_p)
24175 {
24176 while (s)
24177 {
24178 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24179 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24180 x -= s->width;
24181 s->x = x;
24182 s = s->prev;
24183 }
24184 }
24185 else
24186 {
24187 while (s)
24188 {
24189 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24190 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24191 s->x = x;
24192 x += s->width;
24193 s = s->next;
24194 }
24195 }
24196 }
24197
24198
24199
24200 /* The following macros are only called from draw_glyphs below.
24201 They reference the following parameters of that function directly:
24202 `w', `row', `area', and `overlap_p'
24203 as well as the following local variables:
24204 `s', `f', and `hdc' (in W32) */
24205
24206 #ifdef HAVE_NTGUI
24207 /* On W32, silently add local `hdc' variable to argument list of
24208 init_glyph_string. */
24209 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24210 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24211 #else
24212 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24213 init_glyph_string (s, char2b, w, row, area, start, hl)
24214 #endif
24215
24216 /* Add a glyph string for a stretch glyph to the list of strings
24217 between HEAD and TAIL. START is the index of the stretch glyph in
24218 row area AREA of glyph row ROW. END is the index of the last glyph
24219 in that glyph row area. X is the current output position assigned
24220 to the new glyph string constructed. HL overrides that face of the
24221 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24222 is the right-most x-position of the drawing area. */
24223
24224 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24225 and below -- keep them on one line. */
24226 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24227 do \
24228 { \
24229 s = alloca (sizeof *s); \
24230 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24231 START = fill_stretch_glyph_string (s, START, END); \
24232 append_glyph_string (&HEAD, &TAIL, s); \
24233 s->x = (X); \
24234 } \
24235 while (0)
24236
24237
24238 /* Add a glyph string for an image glyph to the list of strings
24239 between HEAD and TAIL. START is the index of the image glyph in
24240 row area AREA of glyph row ROW. END is the index of the last glyph
24241 in that glyph row area. X is the current output position assigned
24242 to the new glyph string constructed. HL overrides that face of the
24243 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24244 is the right-most x-position of the drawing area. */
24245
24246 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24247 do \
24248 { \
24249 s = alloca (sizeof *s); \
24250 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24251 fill_image_glyph_string (s); \
24252 append_glyph_string (&HEAD, &TAIL, s); \
24253 ++START; \
24254 s->x = (X); \
24255 } \
24256 while (0)
24257
24258
24259 /* Add a glyph string for a sequence of character glyphs to the list
24260 of strings between HEAD and TAIL. START is the index of the first
24261 glyph in row area AREA of glyph row ROW that is part of the new
24262 glyph string. END is the index of the last glyph in that glyph row
24263 area. X is the current output position assigned to the new glyph
24264 string constructed. HL overrides that face of the glyph; e.g. it
24265 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24266 right-most x-position of the drawing area. */
24267
24268 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24269 do \
24270 { \
24271 int face_id; \
24272 XChar2b *char2b; \
24273 \
24274 face_id = (row)->glyphs[area][START].face_id; \
24275 \
24276 s = alloca (sizeof *s); \
24277 char2b = alloca ((END - START) * sizeof *char2b); \
24278 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24279 append_glyph_string (&HEAD, &TAIL, s); \
24280 s->x = (X); \
24281 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24282 } \
24283 while (0)
24284
24285
24286 /* Add a glyph string for a composite sequence to the list of strings
24287 between HEAD and TAIL. START is the index of the first glyph in
24288 row area AREA of glyph row ROW that is part of the new glyph
24289 string. END is the index of the last glyph in that glyph row area.
24290 X is the current output position assigned to the new glyph string
24291 constructed. HL overrides that face of the glyph; e.g. it is
24292 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24293 x-position of the drawing area. */
24294
24295 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24296 do { \
24297 int face_id = (row)->glyphs[area][START].face_id; \
24298 struct face *base_face = FACE_FROM_ID (f, face_id); \
24299 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24300 struct composition *cmp = composition_table[cmp_id]; \
24301 XChar2b *char2b; \
24302 struct glyph_string *first_s = NULL; \
24303 int n; \
24304 \
24305 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24306 \
24307 /* Make glyph_strings for each glyph sequence that is drawable by \
24308 the same face, and append them to HEAD/TAIL. */ \
24309 for (n = 0; n < cmp->glyph_len;) \
24310 { \
24311 s = alloca (sizeof *s); \
24312 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24313 append_glyph_string (&(HEAD), &(TAIL), s); \
24314 s->cmp = cmp; \
24315 s->cmp_from = n; \
24316 s->x = (X); \
24317 if (n == 0) \
24318 first_s = s; \
24319 n = fill_composite_glyph_string (s, base_face, overlaps); \
24320 } \
24321 \
24322 ++START; \
24323 s = first_s; \
24324 } while (0)
24325
24326
24327 /* Add a glyph string for a glyph-string sequence to the list of strings
24328 between HEAD and TAIL. */
24329
24330 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24331 do { \
24332 int face_id; \
24333 XChar2b *char2b; \
24334 Lisp_Object gstring; \
24335 \
24336 face_id = (row)->glyphs[area][START].face_id; \
24337 gstring = (composition_gstring_from_id \
24338 ((row)->glyphs[area][START].u.cmp.id)); \
24339 s = alloca (sizeof *s); \
24340 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24341 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24342 append_glyph_string (&(HEAD), &(TAIL), s); \
24343 s->x = (X); \
24344 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24345 } while (0)
24346
24347
24348 /* Add a glyph string for a sequence of glyphless character's glyphs
24349 to the list of strings between HEAD and TAIL. The meanings of
24350 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24351
24352 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24353 do \
24354 { \
24355 int face_id; \
24356 \
24357 face_id = (row)->glyphs[area][START].face_id; \
24358 \
24359 s = alloca (sizeof *s); \
24360 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24361 append_glyph_string (&HEAD, &TAIL, s); \
24362 s->x = (X); \
24363 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24364 overlaps); \
24365 } \
24366 while (0)
24367
24368
24369 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24370 of AREA of glyph row ROW on window W between indices START and END.
24371 HL overrides the face for drawing glyph strings, e.g. it is
24372 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24373 x-positions of the drawing area.
24374
24375 This is an ugly monster macro construct because we must use alloca
24376 to allocate glyph strings (because draw_glyphs can be called
24377 asynchronously). */
24378
24379 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24380 do \
24381 { \
24382 HEAD = TAIL = NULL; \
24383 while (START < END) \
24384 { \
24385 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24386 switch (first_glyph->type) \
24387 { \
24388 case CHAR_GLYPH: \
24389 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24390 HL, X, LAST_X); \
24391 break; \
24392 \
24393 case COMPOSITE_GLYPH: \
24394 if (first_glyph->u.cmp.automatic) \
24395 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24396 HL, X, LAST_X); \
24397 else \
24398 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24399 HL, X, LAST_X); \
24400 break; \
24401 \
24402 case STRETCH_GLYPH: \
24403 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24404 HL, X, LAST_X); \
24405 break; \
24406 \
24407 case IMAGE_GLYPH: \
24408 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24409 HL, X, LAST_X); \
24410 break; \
24411 \
24412 case GLYPHLESS_GLYPH: \
24413 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24414 HL, X, LAST_X); \
24415 break; \
24416 \
24417 default: \
24418 emacs_abort (); \
24419 } \
24420 \
24421 if (s) \
24422 { \
24423 set_glyph_string_background_width (s, START, LAST_X); \
24424 (X) += s->width; \
24425 } \
24426 } \
24427 } while (0)
24428
24429
24430 /* Draw glyphs between START and END in AREA of ROW on window W,
24431 starting at x-position X. X is relative to AREA in W. HL is a
24432 face-override with the following meaning:
24433
24434 DRAW_NORMAL_TEXT draw normally
24435 DRAW_CURSOR draw in cursor face
24436 DRAW_MOUSE_FACE draw in mouse face.
24437 DRAW_INVERSE_VIDEO draw in mode line face
24438 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24439 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24440
24441 If OVERLAPS is non-zero, draw only the foreground of characters and
24442 clip to the physical height of ROW. Non-zero value also defines
24443 the overlapping part to be drawn:
24444
24445 OVERLAPS_PRED overlap with preceding rows
24446 OVERLAPS_SUCC overlap with succeeding rows
24447 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24448 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24449
24450 Value is the x-position reached, relative to AREA of W. */
24451
24452 static int
24453 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24454 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24455 enum draw_glyphs_face hl, int overlaps)
24456 {
24457 struct glyph_string *head, *tail;
24458 struct glyph_string *s;
24459 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24460 int i, j, x_reached, last_x, area_left = 0;
24461 struct frame *f = XFRAME (WINDOW_FRAME (w));
24462 DECLARE_HDC (hdc);
24463
24464 ALLOCATE_HDC (hdc, f);
24465
24466 /* Let's rather be paranoid than getting a SEGV. */
24467 end = min (end, row->used[area]);
24468 start = clip_to_bounds (0, start, end);
24469
24470 /* Translate X to frame coordinates. Set last_x to the right
24471 end of the drawing area. */
24472 if (row->full_width_p)
24473 {
24474 /* X is relative to the left edge of W, without scroll bars
24475 or fringes. */
24476 area_left = WINDOW_LEFT_EDGE_X (w);
24477 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24478 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24479 }
24480 else
24481 {
24482 area_left = window_box_left (w, area);
24483 last_x = area_left + window_box_width (w, area);
24484 }
24485 x += area_left;
24486
24487 /* Build a doubly-linked list of glyph_string structures between
24488 head and tail from what we have to draw. Note that the macro
24489 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24490 the reason we use a separate variable `i'. */
24491 i = start;
24492 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24493 if (tail)
24494 x_reached = tail->x + tail->background_width;
24495 else
24496 x_reached = x;
24497
24498 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24499 the row, redraw some glyphs in front or following the glyph
24500 strings built above. */
24501 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24502 {
24503 struct glyph_string *h, *t;
24504 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24505 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24506 int check_mouse_face = 0;
24507 int dummy_x = 0;
24508
24509 /* If mouse highlighting is on, we may need to draw adjacent
24510 glyphs using mouse-face highlighting. */
24511 if (area == TEXT_AREA && row->mouse_face_p
24512 && hlinfo->mouse_face_beg_row >= 0
24513 && hlinfo->mouse_face_end_row >= 0)
24514 {
24515 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24516
24517 if (row_vpos >= hlinfo->mouse_face_beg_row
24518 && row_vpos <= hlinfo->mouse_face_end_row)
24519 {
24520 check_mouse_face = 1;
24521 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24522 ? hlinfo->mouse_face_beg_col : 0;
24523 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24524 ? hlinfo->mouse_face_end_col
24525 : row->used[TEXT_AREA];
24526 }
24527 }
24528
24529 /* Compute overhangs for all glyph strings. */
24530 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24531 for (s = head; s; s = s->next)
24532 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24533
24534 /* Prepend glyph strings for glyphs in front of the first glyph
24535 string that are overwritten because of the first glyph
24536 string's left overhang. The background of all strings
24537 prepended must be drawn because the first glyph string
24538 draws over it. */
24539 i = left_overwritten (head);
24540 if (i >= 0)
24541 {
24542 enum draw_glyphs_face overlap_hl;
24543
24544 /* If this row contains mouse highlighting, attempt to draw
24545 the overlapped glyphs with the correct highlight. This
24546 code fails if the overlap encompasses more than one glyph
24547 and mouse-highlight spans only some of these glyphs.
24548 However, making it work perfectly involves a lot more
24549 code, and I don't know if the pathological case occurs in
24550 practice, so we'll stick to this for now. --- cyd */
24551 if (check_mouse_face
24552 && mouse_beg_col < start && mouse_end_col > i)
24553 overlap_hl = DRAW_MOUSE_FACE;
24554 else
24555 overlap_hl = DRAW_NORMAL_TEXT;
24556
24557 j = i;
24558 BUILD_GLYPH_STRINGS (j, start, h, t,
24559 overlap_hl, dummy_x, last_x);
24560 start = i;
24561 compute_overhangs_and_x (t, head->x, 1);
24562 prepend_glyph_string_lists (&head, &tail, h, t);
24563 clip_head = head;
24564 }
24565
24566 /* Prepend glyph strings for glyphs in front of the first glyph
24567 string that overwrite that glyph string because of their
24568 right overhang. For these strings, only the foreground must
24569 be drawn, because it draws over the glyph string at `head'.
24570 The background must not be drawn because this would overwrite
24571 right overhangs of preceding glyphs for which no glyph
24572 strings exist. */
24573 i = left_overwriting (head);
24574 if (i >= 0)
24575 {
24576 enum draw_glyphs_face overlap_hl;
24577
24578 if (check_mouse_face
24579 && mouse_beg_col < start && mouse_end_col > i)
24580 overlap_hl = DRAW_MOUSE_FACE;
24581 else
24582 overlap_hl = DRAW_NORMAL_TEXT;
24583
24584 clip_head = head;
24585 BUILD_GLYPH_STRINGS (i, start, h, t,
24586 overlap_hl, dummy_x, last_x);
24587 for (s = h; s; s = s->next)
24588 s->background_filled_p = 1;
24589 compute_overhangs_and_x (t, head->x, 1);
24590 prepend_glyph_string_lists (&head, &tail, h, t);
24591 }
24592
24593 /* Append glyphs strings for glyphs following the last glyph
24594 string tail that are overwritten by tail. The background of
24595 these strings has to be drawn because tail's foreground draws
24596 over it. */
24597 i = right_overwritten (tail);
24598 if (i >= 0)
24599 {
24600 enum draw_glyphs_face overlap_hl;
24601
24602 if (check_mouse_face
24603 && mouse_beg_col < i && mouse_end_col > end)
24604 overlap_hl = DRAW_MOUSE_FACE;
24605 else
24606 overlap_hl = DRAW_NORMAL_TEXT;
24607
24608 BUILD_GLYPH_STRINGS (end, i, h, t,
24609 overlap_hl, x, last_x);
24610 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24611 we don't have `end = i;' here. */
24612 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24613 append_glyph_string_lists (&head, &tail, h, t);
24614 clip_tail = tail;
24615 }
24616
24617 /* Append glyph strings for glyphs following the last glyph
24618 string tail that overwrite tail. The foreground of such
24619 glyphs has to be drawn because it writes into the background
24620 of tail. The background must not be drawn because it could
24621 paint over the foreground of following glyphs. */
24622 i = right_overwriting (tail);
24623 if (i >= 0)
24624 {
24625 enum draw_glyphs_face overlap_hl;
24626 if (check_mouse_face
24627 && mouse_beg_col < i && mouse_end_col > end)
24628 overlap_hl = DRAW_MOUSE_FACE;
24629 else
24630 overlap_hl = DRAW_NORMAL_TEXT;
24631
24632 clip_tail = tail;
24633 i++; /* We must include the Ith glyph. */
24634 BUILD_GLYPH_STRINGS (end, i, h, t,
24635 overlap_hl, x, last_x);
24636 for (s = h; s; s = s->next)
24637 s->background_filled_p = 1;
24638 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24639 append_glyph_string_lists (&head, &tail, h, t);
24640 }
24641 if (clip_head || clip_tail)
24642 for (s = head; s; s = s->next)
24643 {
24644 s->clip_head = clip_head;
24645 s->clip_tail = clip_tail;
24646 }
24647 }
24648
24649 /* Draw all strings. */
24650 for (s = head; s; s = s->next)
24651 FRAME_RIF (f)->draw_glyph_string (s);
24652
24653 #ifndef HAVE_NS
24654 /* When focus a sole frame and move horizontally, this sets on_p to 0
24655 causing a failure to erase prev cursor position. */
24656 if (area == TEXT_AREA
24657 && !row->full_width_p
24658 /* When drawing overlapping rows, only the glyph strings'
24659 foreground is drawn, which doesn't erase a cursor
24660 completely. */
24661 && !overlaps)
24662 {
24663 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24664 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24665 : (tail ? tail->x + tail->background_width : x));
24666 x0 -= area_left;
24667 x1 -= area_left;
24668
24669 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24670 row->y, MATRIX_ROW_BOTTOM_Y (row));
24671 }
24672 #endif
24673
24674 /* Value is the x-position up to which drawn, relative to AREA of W.
24675 This doesn't include parts drawn because of overhangs. */
24676 if (row->full_width_p)
24677 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24678 else
24679 x_reached -= area_left;
24680
24681 RELEASE_HDC (hdc, f);
24682
24683 return x_reached;
24684 }
24685
24686 /* Expand row matrix if too narrow. Don't expand if area
24687 is not present. */
24688
24689 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24690 { \
24691 if (!it->f->fonts_changed \
24692 && (it->glyph_row->glyphs[area] \
24693 < it->glyph_row->glyphs[area + 1])) \
24694 { \
24695 it->w->ncols_scale_factor++; \
24696 it->f->fonts_changed = 1; \
24697 } \
24698 }
24699
24700 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24701 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24702
24703 static void
24704 append_glyph (struct it *it)
24705 {
24706 struct glyph *glyph;
24707 enum glyph_row_area area = it->area;
24708
24709 eassert (it->glyph_row);
24710 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24711
24712 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24713 if (glyph < it->glyph_row->glyphs[area + 1])
24714 {
24715 /* If the glyph row is reversed, we need to prepend the glyph
24716 rather than append it. */
24717 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24718 {
24719 struct glyph *g;
24720
24721 /* Make room for the additional glyph. */
24722 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24723 g[1] = *g;
24724 glyph = it->glyph_row->glyphs[area];
24725 }
24726 glyph->charpos = CHARPOS (it->position);
24727 glyph->object = it->object;
24728 if (it->pixel_width > 0)
24729 {
24730 glyph->pixel_width = it->pixel_width;
24731 glyph->padding_p = 0;
24732 }
24733 else
24734 {
24735 /* Assure at least 1-pixel width. Otherwise, cursor can't
24736 be displayed correctly. */
24737 glyph->pixel_width = 1;
24738 glyph->padding_p = 1;
24739 }
24740 glyph->ascent = it->ascent;
24741 glyph->descent = it->descent;
24742 glyph->voffset = it->voffset;
24743 glyph->type = CHAR_GLYPH;
24744 glyph->avoid_cursor_p = it->avoid_cursor_p;
24745 glyph->multibyte_p = it->multibyte_p;
24746 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24747 {
24748 /* In R2L rows, the left and the right box edges need to be
24749 drawn in reverse direction. */
24750 glyph->right_box_line_p = it->start_of_box_run_p;
24751 glyph->left_box_line_p = it->end_of_box_run_p;
24752 }
24753 else
24754 {
24755 glyph->left_box_line_p = it->start_of_box_run_p;
24756 glyph->right_box_line_p = it->end_of_box_run_p;
24757 }
24758 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24759 || it->phys_descent > it->descent);
24760 glyph->glyph_not_available_p = it->glyph_not_available_p;
24761 glyph->face_id = it->face_id;
24762 glyph->u.ch = it->char_to_display;
24763 glyph->slice.img = null_glyph_slice;
24764 glyph->font_type = FONT_TYPE_UNKNOWN;
24765 if (it->bidi_p)
24766 {
24767 glyph->resolved_level = it->bidi_it.resolved_level;
24768 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24769 emacs_abort ();
24770 glyph->bidi_type = it->bidi_it.type;
24771 }
24772 else
24773 {
24774 glyph->resolved_level = 0;
24775 glyph->bidi_type = UNKNOWN_BT;
24776 }
24777 ++it->glyph_row->used[area];
24778 }
24779 else
24780 IT_EXPAND_MATRIX_WIDTH (it, area);
24781 }
24782
24783 /* Store one glyph for the composition IT->cmp_it.id in
24784 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24785 non-null. */
24786
24787 static void
24788 append_composite_glyph (struct it *it)
24789 {
24790 struct glyph *glyph;
24791 enum glyph_row_area area = it->area;
24792
24793 eassert (it->glyph_row);
24794
24795 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24796 if (glyph < it->glyph_row->glyphs[area + 1])
24797 {
24798 /* If the glyph row is reversed, we need to prepend the glyph
24799 rather than append it. */
24800 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24801 {
24802 struct glyph *g;
24803
24804 /* Make room for the new glyph. */
24805 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24806 g[1] = *g;
24807 glyph = it->glyph_row->glyphs[it->area];
24808 }
24809 glyph->charpos = it->cmp_it.charpos;
24810 glyph->object = it->object;
24811 glyph->pixel_width = it->pixel_width;
24812 glyph->ascent = it->ascent;
24813 glyph->descent = it->descent;
24814 glyph->voffset = it->voffset;
24815 glyph->type = COMPOSITE_GLYPH;
24816 if (it->cmp_it.ch < 0)
24817 {
24818 glyph->u.cmp.automatic = 0;
24819 glyph->u.cmp.id = it->cmp_it.id;
24820 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24821 }
24822 else
24823 {
24824 glyph->u.cmp.automatic = 1;
24825 glyph->u.cmp.id = it->cmp_it.id;
24826 glyph->slice.cmp.from = it->cmp_it.from;
24827 glyph->slice.cmp.to = it->cmp_it.to - 1;
24828 }
24829 glyph->avoid_cursor_p = it->avoid_cursor_p;
24830 glyph->multibyte_p = it->multibyte_p;
24831 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24832 {
24833 /* In R2L rows, the left and the right box edges need to be
24834 drawn in reverse direction. */
24835 glyph->right_box_line_p = it->start_of_box_run_p;
24836 glyph->left_box_line_p = it->end_of_box_run_p;
24837 }
24838 else
24839 {
24840 glyph->left_box_line_p = it->start_of_box_run_p;
24841 glyph->right_box_line_p = it->end_of_box_run_p;
24842 }
24843 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24844 || it->phys_descent > it->descent);
24845 glyph->padding_p = 0;
24846 glyph->glyph_not_available_p = 0;
24847 glyph->face_id = it->face_id;
24848 glyph->font_type = FONT_TYPE_UNKNOWN;
24849 if (it->bidi_p)
24850 {
24851 glyph->resolved_level = it->bidi_it.resolved_level;
24852 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24853 emacs_abort ();
24854 glyph->bidi_type = it->bidi_it.type;
24855 }
24856 ++it->glyph_row->used[area];
24857 }
24858 else
24859 IT_EXPAND_MATRIX_WIDTH (it, area);
24860 }
24861
24862
24863 /* Change IT->ascent and IT->height according to the setting of
24864 IT->voffset. */
24865
24866 static void
24867 take_vertical_position_into_account (struct it *it)
24868 {
24869 if (it->voffset)
24870 {
24871 if (it->voffset < 0)
24872 /* Increase the ascent so that we can display the text higher
24873 in the line. */
24874 it->ascent -= it->voffset;
24875 else
24876 /* Increase the descent so that we can display the text lower
24877 in the line. */
24878 it->descent += it->voffset;
24879 }
24880 }
24881
24882
24883 /* Produce glyphs/get display metrics for the image IT is loaded with.
24884 See the description of struct display_iterator in dispextern.h for
24885 an overview of struct display_iterator. */
24886
24887 static void
24888 produce_image_glyph (struct it *it)
24889 {
24890 struct image *img;
24891 struct face *face;
24892 int glyph_ascent, crop;
24893 struct glyph_slice slice;
24894
24895 eassert (it->what == IT_IMAGE);
24896
24897 face = FACE_FROM_ID (it->f, it->face_id);
24898 eassert (face);
24899 /* Make sure X resources of the face is loaded. */
24900 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24901
24902 if (it->image_id < 0)
24903 {
24904 /* Fringe bitmap. */
24905 it->ascent = it->phys_ascent = 0;
24906 it->descent = it->phys_descent = 0;
24907 it->pixel_width = 0;
24908 it->nglyphs = 0;
24909 return;
24910 }
24911
24912 img = IMAGE_FROM_ID (it->f, it->image_id);
24913 eassert (img);
24914 /* Make sure X resources of the image is loaded. */
24915 prepare_image_for_display (it->f, img);
24916
24917 slice.x = slice.y = 0;
24918 slice.width = img->width;
24919 slice.height = img->height;
24920
24921 if (INTEGERP (it->slice.x))
24922 slice.x = XINT (it->slice.x);
24923 else if (FLOATP (it->slice.x))
24924 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24925
24926 if (INTEGERP (it->slice.y))
24927 slice.y = XINT (it->slice.y);
24928 else if (FLOATP (it->slice.y))
24929 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24930
24931 if (INTEGERP (it->slice.width))
24932 slice.width = XINT (it->slice.width);
24933 else if (FLOATP (it->slice.width))
24934 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24935
24936 if (INTEGERP (it->slice.height))
24937 slice.height = XINT (it->slice.height);
24938 else if (FLOATP (it->slice.height))
24939 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24940
24941 if (slice.x >= img->width)
24942 slice.x = img->width;
24943 if (slice.y >= img->height)
24944 slice.y = img->height;
24945 if (slice.x + slice.width >= img->width)
24946 slice.width = img->width - slice.x;
24947 if (slice.y + slice.height > img->height)
24948 slice.height = img->height - slice.y;
24949
24950 if (slice.width == 0 || slice.height == 0)
24951 return;
24952
24953 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24954
24955 it->descent = slice.height - glyph_ascent;
24956 if (slice.y == 0)
24957 it->descent += img->vmargin;
24958 if (slice.y + slice.height == img->height)
24959 it->descent += img->vmargin;
24960 it->phys_descent = it->descent;
24961
24962 it->pixel_width = slice.width;
24963 if (slice.x == 0)
24964 it->pixel_width += img->hmargin;
24965 if (slice.x + slice.width == img->width)
24966 it->pixel_width += img->hmargin;
24967
24968 /* It's quite possible for images to have an ascent greater than
24969 their height, so don't get confused in that case. */
24970 if (it->descent < 0)
24971 it->descent = 0;
24972
24973 it->nglyphs = 1;
24974
24975 if (face->box != FACE_NO_BOX)
24976 {
24977 if (face->box_line_width > 0)
24978 {
24979 if (slice.y == 0)
24980 it->ascent += face->box_line_width;
24981 if (slice.y + slice.height == img->height)
24982 it->descent += face->box_line_width;
24983 }
24984
24985 if (it->start_of_box_run_p && slice.x == 0)
24986 it->pixel_width += eabs (face->box_line_width);
24987 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24988 it->pixel_width += eabs (face->box_line_width);
24989 }
24990
24991 take_vertical_position_into_account (it);
24992
24993 /* Automatically crop wide image glyphs at right edge so we can
24994 draw the cursor on same display row. */
24995 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24996 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24997 {
24998 it->pixel_width -= crop;
24999 slice.width -= crop;
25000 }
25001
25002 if (it->glyph_row)
25003 {
25004 struct glyph *glyph;
25005 enum glyph_row_area area = it->area;
25006
25007 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25008 if (glyph < it->glyph_row->glyphs[area + 1])
25009 {
25010 glyph->charpos = CHARPOS (it->position);
25011 glyph->object = it->object;
25012 glyph->pixel_width = it->pixel_width;
25013 glyph->ascent = glyph_ascent;
25014 glyph->descent = it->descent;
25015 glyph->voffset = it->voffset;
25016 glyph->type = IMAGE_GLYPH;
25017 glyph->avoid_cursor_p = it->avoid_cursor_p;
25018 glyph->multibyte_p = it->multibyte_p;
25019 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25020 {
25021 /* In R2L rows, the left and the right box edges need to be
25022 drawn in reverse direction. */
25023 glyph->right_box_line_p = it->start_of_box_run_p;
25024 glyph->left_box_line_p = it->end_of_box_run_p;
25025 }
25026 else
25027 {
25028 glyph->left_box_line_p = it->start_of_box_run_p;
25029 glyph->right_box_line_p = it->end_of_box_run_p;
25030 }
25031 glyph->overlaps_vertically_p = 0;
25032 glyph->padding_p = 0;
25033 glyph->glyph_not_available_p = 0;
25034 glyph->face_id = it->face_id;
25035 glyph->u.img_id = img->id;
25036 glyph->slice.img = slice;
25037 glyph->font_type = FONT_TYPE_UNKNOWN;
25038 if (it->bidi_p)
25039 {
25040 glyph->resolved_level = it->bidi_it.resolved_level;
25041 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25042 emacs_abort ();
25043 glyph->bidi_type = it->bidi_it.type;
25044 }
25045 ++it->glyph_row->used[area];
25046 }
25047 else
25048 IT_EXPAND_MATRIX_WIDTH (it, area);
25049 }
25050 }
25051
25052
25053 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25054 of the glyph, WIDTH and HEIGHT are the width and height of the
25055 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25056
25057 static void
25058 append_stretch_glyph (struct it *it, Lisp_Object object,
25059 int width, int height, int ascent)
25060 {
25061 struct glyph *glyph;
25062 enum glyph_row_area area = it->area;
25063
25064 eassert (ascent >= 0 && ascent <= height);
25065
25066 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25067 if (glyph < it->glyph_row->glyphs[area + 1])
25068 {
25069 /* If the glyph row is reversed, we need to prepend the glyph
25070 rather than append it. */
25071 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25072 {
25073 struct glyph *g;
25074
25075 /* Make room for the additional glyph. */
25076 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25077 g[1] = *g;
25078 glyph = it->glyph_row->glyphs[area];
25079 }
25080 glyph->charpos = CHARPOS (it->position);
25081 glyph->object = object;
25082 glyph->pixel_width = width;
25083 glyph->ascent = ascent;
25084 glyph->descent = height - ascent;
25085 glyph->voffset = it->voffset;
25086 glyph->type = STRETCH_GLYPH;
25087 glyph->avoid_cursor_p = it->avoid_cursor_p;
25088 glyph->multibyte_p = it->multibyte_p;
25089 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25090 {
25091 /* In R2L rows, the left and the right box edges need to be
25092 drawn in reverse direction. */
25093 glyph->right_box_line_p = it->start_of_box_run_p;
25094 glyph->left_box_line_p = it->end_of_box_run_p;
25095 }
25096 else
25097 {
25098 glyph->left_box_line_p = it->start_of_box_run_p;
25099 glyph->right_box_line_p = it->end_of_box_run_p;
25100 }
25101 glyph->overlaps_vertically_p = 0;
25102 glyph->padding_p = 0;
25103 glyph->glyph_not_available_p = 0;
25104 glyph->face_id = it->face_id;
25105 glyph->u.stretch.ascent = ascent;
25106 glyph->u.stretch.height = height;
25107 glyph->slice.img = null_glyph_slice;
25108 glyph->font_type = FONT_TYPE_UNKNOWN;
25109 if (it->bidi_p)
25110 {
25111 glyph->resolved_level = it->bidi_it.resolved_level;
25112 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25113 emacs_abort ();
25114 glyph->bidi_type = it->bidi_it.type;
25115 }
25116 else
25117 {
25118 glyph->resolved_level = 0;
25119 glyph->bidi_type = UNKNOWN_BT;
25120 }
25121 ++it->glyph_row->used[area];
25122 }
25123 else
25124 IT_EXPAND_MATRIX_WIDTH (it, area);
25125 }
25126
25127 #endif /* HAVE_WINDOW_SYSTEM */
25128
25129 /* Produce a stretch glyph for iterator IT. IT->object is the value
25130 of the glyph property displayed. The value must be a list
25131 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25132 being recognized:
25133
25134 1. `:width WIDTH' specifies that the space should be WIDTH *
25135 canonical char width wide. WIDTH may be an integer or floating
25136 point number.
25137
25138 2. `:relative-width FACTOR' specifies that the width of the stretch
25139 should be computed from the width of the first character having the
25140 `glyph' property, and should be FACTOR times that width.
25141
25142 3. `:align-to HPOS' specifies that the space should be wide enough
25143 to reach HPOS, a value in canonical character units.
25144
25145 Exactly one of the above pairs must be present.
25146
25147 4. `:height HEIGHT' specifies that the height of the stretch produced
25148 should be HEIGHT, measured in canonical character units.
25149
25150 5. `:relative-height FACTOR' specifies that the height of the
25151 stretch should be FACTOR times the height of the characters having
25152 the glyph property.
25153
25154 Either none or exactly one of 4 or 5 must be present.
25155
25156 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25157 of the stretch should be used for the ascent of the stretch.
25158 ASCENT must be in the range 0 <= ASCENT <= 100. */
25159
25160 void
25161 produce_stretch_glyph (struct it *it)
25162 {
25163 /* (space :width WIDTH :height HEIGHT ...) */
25164 Lisp_Object prop, plist;
25165 int width = 0, height = 0, align_to = -1;
25166 int zero_width_ok_p = 0;
25167 double tem;
25168 struct font *font = NULL;
25169
25170 #ifdef HAVE_WINDOW_SYSTEM
25171 int ascent = 0;
25172 int zero_height_ok_p = 0;
25173
25174 if (FRAME_WINDOW_P (it->f))
25175 {
25176 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25177 font = face->font ? face->font : FRAME_FONT (it->f);
25178 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25179 }
25180 #endif
25181
25182 /* List should start with `space'. */
25183 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25184 plist = XCDR (it->object);
25185
25186 /* Compute the width of the stretch. */
25187 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25188 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25189 {
25190 /* Absolute width `:width WIDTH' specified and valid. */
25191 zero_width_ok_p = 1;
25192 width = (int)tem;
25193 }
25194 #ifdef HAVE_WINDOW_SYSTEM
25195 else if (FRAME_WINDOW_P (it->f)
25196 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25197 {
25198 /* Relative width `:relative-width FACTOR' specified and valid.
25199 Compute the width of the characters having the `glyph'
25200 property. */
25201 struct it it2;
25202 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25203
25204 it2 = *it;
25205 if (it->multibyte_p)
25206 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25207 else
25208 {
25209 it2.c = it2.char_to_display = *p, it2.len = 1;
25210 if (! ASCII_CHAR_P (it2.c))
25211 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25212 }
25213
25214 it2.glyph_row = NULL;
25215 it2.what = IT_CHARACTER;
25216 x_produce_glyphs (&it2);
25217 width = NUMVAL (prop) * it2.pixel_width;
25218 }
25219 #endif /* HAVE_WINDOW_SYSTEM */
25220 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25221 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25222 {
25223 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25224 align_to = (align_to < 0
25225 ? 0
25226 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25227 else if (align_to < 0)
25228 align_to = window_box_left_offset (it->w, TEXT_AREA);
25229 width = max (0, (int)tem + align_to - it->current_x);
25230 zero_width_ok_p = 1;
25231 }
25232 else
25233 /* Nothing specified -> width defaults to canonical char width. */
25234 width = FRAME_COLUMN_WIDTH (it->f);
25235
25236 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25237 width = 1;
25238
25239 #ifdef HAVE_WINDOW_SYSTEM
25240 /* Compute height. */
25241 if (FRAME_WINDOW_P (it->f))
25242 {
25243 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25244 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25245 {
25246 height = (int)tem;
25247 zero_height_ok_p = 1;
25248 }
25249 else if (prop = Fplist_get (plist, QCrelative_height),
25250 NUMVAL (prop) > 0)
25251 height = FONT_HEIGHT (font) * NUMVAL (prop);
25252 else
25253 height = FONT_HEIGHT (font);
25254
25255 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25256 height = 1;
25257
25258 /* Compute percentage of height used for ascent. If
25259 `:ascent ASCENT' is present and valid, use that. Otherwise,
25260 derive the ascent from the font in use. */
25261 if (prop = Fplist_get (plist, QCascent),
25262 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25263 ascent = height * NUMVAL (prop) / 100.0;
25264 else if (!NILP (prop)
25265 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25266 ascent = min (max (0, (int)tem), height);
25267 else
25268 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25269 }
25270 else
25271 #endif /* HAVE_WINDOW_SYSTEM */
25272 height = 1;
25273
25274 if (width > 0 && it->line_wrap != TRUNCATE
25275 && it->current_x + width > it->last_visible_x)
25276 {
25277 width = it->last_visible_x - it->current_x;
25278 #ifdef HAVE_WINDOW_SYSTEM
25279 /* Subtract one more pixel from the stretch width, but only on
25280 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25281 width -= FRAME_WINDOW_P (it->f);
25282 #endif
25283 }
25284
25285 if (width > 0 && height > 0 && it->glyph_row)
25286 {
25287 Lisp_Object o_object = it->object;
25288 Lisp_Object object = it->stack[it->sp - 1].string;
25289 int n = width;
25290
25291 if (!STRINGP (object))
25292 object = it->w->contents;
25293 #ifdef HAVE_WINDOW_SYSTEM
25294 if (FRAME_WINDOW_P (it->f))
25295 append_stretch_glyph (it, object, width, height, ascent);
25296 else
25297 #endif
25298 {
25299 it->object = object;
25300 it->char_to_display = ' ';
25301 it->pixel_width = it->len = 1;
25302 while (n--)
25303 tty_append_glyph (it);
25304 it->object = o_object;
25305 }
25306 }
25307
25308 it->pixel_width = width;
25309 #ifdef HAVE_WINDOW_SYSTEM
25310 if (FRAME_WINDOW_P (it->f))
25311 {
25312 it->ascent = it->phys_ascent = ascent;
25313 it->descent = it->phys_descent = height - it->ascent;
25314 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25315 take_vertical_position_into_account (it);
25316 }
25317 else
25318 #endif
25319 it->nglyphs = width;
25320 }
25321
25322 /* Get information about special display element WHAT in an
25323 environment described by IT. WHAT is one of IT_TRUNCATION or
25324 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25325 non-null glyph_row member. This function ensures that fields like
25326 face_id, c, len of IT are left untouched. */
25327
25328 static void
25329 produce_special_glyphs (struct it *it, enum display_element_type what)
25330 {
25331 struct it temp_it;
25332 Lisp_Object gc;
25333 GLYPH glyph;
25334
25335 temp_it = *it;
25336 temp_it.object = make_number (0);
25337 memset (&temp_it.current, 0, sizeof temp_it.current);
25338
25339 if (what == IT_CONTINUATION)
25340 {
25341 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25342 if (it->bidi_it.paragraph_dir == R2L)
25343 SET_GLYPH_FROM_CHAR (glyph, '/');
25344 else
25345 SET_GLYPH_FROM_CHAR (glyph, '\\');
25346 if (it->dp
25347 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25348 {
25349 /* FIXME: Should we mirror GC for R2L lines? */
25350 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25351 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25352 }
25353 }
25354 else if (what == IT_TRUNCATION)
25355 {
25356 /* Truncation glyph. */
25357 SET_GLYPH_FROM_CHAR (glyph, '$');
25358 if (it->dp
25359 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25360 {
25361 /* FIXME: Should we mirror GC for R2L lines? */
25362 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25363 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25364 }
25365 }
25366 else
25367 emacs_abort ();
25368
25369 #ifdef HAVE_WINDOW_SYSTEM
25370 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25371 is turned off, we precede the truncation/continuation glyphs by a
25372 stretch glyph whose width is computed such that these special
25373 glyphs are aligned at the window margin, even when very different
25374 fonts are used in different glyph rows. */
25375 if (FRAME_WINDOW_P (temp_it.f)
25376 /* init_iterator calls this with it->glyph_row == NULL, and it
25377 wants only the pixel width of the truncation/continuation
25378 glyphs. */
25379 && temp_it.glyph_row
25380 /* insert_left_trunc_glyphs calls us at the beginning of the
25381 row, and it has its own calculation of the stretch glyph
25382 width. */
25383 && temp_it.glyph_row->used[TEXT_AREA] > 0
25384 && (temp_it.glyph_row->reversed_p
25385 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25386 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25387 {
25388 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25389
25390 if (stretch_width > 0)
25391 {
25392 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25393 struct font *font =
25394 face->font ? face->font : FRAME_FONT (temp_it.f);
25395 int stretch_ascent =
25396 (((temp_it.ascent + temp_it.descent)
25397 * FONT_BASE (font)) / FONT_HEIGHT (font));
25398
25399 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25400 temp_it.ascent + temp_it.descent,
25401 stretch_ascent);
25402 }
25403 }
25404 #endif
25405
25406 temp_it.dp = NULL;
25407 temp_it.what = IT_CHARACTER;
25408 temp_it.len = 1;
25409 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25410 temp_it.face_id = GLYPH_FACE (glyph);
25411 temp_it.len = CHAR_BYTES (temp_it.c);
25412
25413 PRODUCE_GLYPHS (&temp_it);
25414 it->pixel_width = temp_it.pixel_width;
25415 it->nglyphs = temp_it.pixel_width;
25416 }
25417
25418 #ifdef HAVE_WINDOW_SYSTEM
25419
25420 /* Calculate line-height and line-spacing properties.
25421 An integer value specifies explicit pixel value.
25422 A float value specifies relative value to current face height.
25423 A cons (float . face-name) specifies relative value to
25424 height of specified face font.
25425
25426 Returns height in pixels, or nil. */
25427
25428
25429 static Lisp_Object
25430 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25431 int boff, int override)
25432 {
25433 Lisp_Object face_name = Qnil;
25434 int ascent, descent, height;
25435
25436 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25437 return val;
25438
25439 if (CONSP (val))
25440 {
25441 face_name = XCAR (val);
25442 val = XCDR (val);
25443 if (!NUMBERP (val))
25444 val = make_number (1);
25445 if (NILP (face_name))
25446 {
25447 height = it->ascent + it->descent;
25448 goto scale;
25449 }
25450 }
25451
25452 if (NILP (face_name))
25453 {
25454 font = FRAME_FONT (it->f);
25455 boff = FRAME_BASELINE_OFFSET (it->f);
25456 }
25457 else if (EQ (face_name, Qt))
25458 {
25459 override = 0;
25460 }
25461 else
25462 {
25463 int face_id;
25464 struct face *face;
25465
25466 face_id = lookup_named_face (it->f, face_name, 0);
25467 if (face_id < 0)
25468 return make_number (-1);
25469
25470 face = FACE_FROM_ID (it->f, face_id);
25471 font = face->font;
25472 if (font == NULL)
25473 return make_number (-1);
25474 boff = font->baseline_offset;
25475 if (font->vertical_centering)
25476 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25477 }
25478
25479 ascent = FONT_BASE (font) + boff;
25480 descent = FONT_DESCENT (font) - boff;
25481
25482 if (override)
25483 {
25484 it->override_ascent = ascent;
25485 it->override_descent = descent;
25486 it->override_boff = boff;
25487 }
25488
25489 height = ascent + descent;
25490
25491 scale:
25492 if (FLOATP (val))
25493 height = (int)(XFLOAT_DATA (val) * height);
25494 else if (INTEGERP (val))
25495 height *= XINT (val);
25496
25497 return make_number (height);
25498 }
25499
25500
25501 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25502 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25503 and only if this is for a character for which no font was found.
25504
25505 If the display method (it->glyphless_method) is
25506 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25507 length of the acronym or the hexadecimal string, UPPER_XOFF and
25508 UPPER_YOFF are pixel offsets for the upper part of the string,
25509 LOWER_XOFF and LOWER_YOFF are for the lower part.
25510
25511 For the other display methods, LEN through LOWER_YOFF are zero. */
25512
25513 static void
25514 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25515 short upper_xoff, short upper_yoff,
25516 short lower_xoff, short lower_yoff)
25517 {
25518 struct glyph *glyph;
25519 enum glyph_row_area area = it->area;
25520
25521 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25522 if (glyph < it->glyph_row->glyphs[area + 1])
25523 {
25524 /* If the glyph row is reversed, we need to prepend the glyph
25525 rather than append it. */
25526 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25527 {
25528 struct glyph *g;
25529
25530 /* Make room for the additional glyph. */
25531 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25532 g[1] = *g;
25533 glyph = it->glyph_row->glyphs[area];
25534 }
25535 glyph->charpos = CHARPOS (it->position);
25536 glyph->object = it->object;
25537 glyph->pixel_width = it->pixel_width;
25538 glyph->ascent = it->ascent;
25539 glyph->descent = it->descent;
25540 glyph->voffset = it->voffset;
25541 glyph->type = GLYPHLESS_GLYPH;
25542 glyph->u.glyphless.method = it->glyphless_method;
25543 glyph->u.glyphless.for_no_font = for_no_font;
25544 glyph->u.glyphless.len = len;
25545 glyph->u.glyphless.ch = it->c;
25546 glyph->slice.glyphless.upper_xoff = upper_xoff;
25547 glyph->slice.glyphless.upper_yoff = upper_yoff;
25548 glyph->slice.glyphless.lower_xoff = lower_xoff;
25549 glyph->slice.glyphless.lower_yoff = lower_yoff;
25550 glyph->avoid_cursor_p = it->avoid_cursor_p;
25551 glyph->multibyte_p = it->multibyte_p;
25552 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25553 {
25554 /* In R2L rows, the left and the right box edges need to be
25555 drawn in reverse direction. */
25556 glyph->right_box_line_p = it->start_of_box_run_p;
25557 glyph->left_box_line_p = it->end_of_box_run_p;
25558 }
25559 else
25560 {
25561 glyph->left_box_line_p = it->start_of_box_run_p;
25562 glyph->right_box_line_p = it->end_of_box_run_p;
25563 }
25564 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25565 || it->phys_descent > it->descent);
25566 glyph->padding_p = 0;
25567 glyph->glyph_not_available_p = 0;
25568 glyph->face_id = face_id;
25569 glyph->font_type = FONT_TYPE_UNKNOWN;
25570 if (it->bidi_p)
25571 {
25572 glyph->resolved_level = it->bidi_it.resolved_level;
25573 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25574 emacs_abort ();
25575 glyph->bidi_type = it->bidi_it.type;
25576 }
25577 ++it->glyph_row->used[area];
25578 }
25579 else
25580 IT_EXPAND_MATRIX_WIDTH (it, area);
25581 }
25582
25583
25584 /* Produce a glyph for a glyphless character for iterator IT.
25585 IT->glyphless_method specifies which method to use for displaying
25586 the character. See the description of enum
25587 glyphless_display_method in dispextern.h for the detail.
25588
25589 FOR_NO_FONT is nonzero if and only if this is for a character for
25590 which no font was found. ACRONYM, if non-nil, is an acronym string
25591 for the character. */
25592
25593 static void
25594 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25595 {
25596 int face_id;
25597 struct face *face;
25598 struct font *font;
25599 int base_width, base_height, width, height;
25600 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25601 int len;
25602
25603 /* Get the metrics of the base font. We always refer to the current
25604 ASCII face. */
25605 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25606 font = face->font ? face->font : FRAME_FONT (it->f);
25607 it->ascent = FONT_BASE (font) + font->baseline_offset;
25608 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25609 base_height = it->ascent + it->descent;
25610 base_width = font->average_width;
25611
25612 face_id = merge_glyphless_glyph_face (it);
25613
25614 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25615 {
25616 it->pixel_width = THIN_SPACE_WIDTH;
25617 len = 0;
25618 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25619 }
25620 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25621 {
25622 width = CHAR_WIDTH (it->c);
25623 if (width == 0)
25624 width = 1;
25625 else if (width > 4)
25626 width = 4;
25627 it->pixel_width = base_width * width;
25628 len = 0;
25629 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25630 }
25631 else
25632 {
25633 char buf[7];
25634 const char *str;
25635 unsigned int code[6];
25636 int upper_len;
25637 int ascent, descent;
25638 struct font_metrics metrics_upper, metrics_lower;
25639
25640 face = FACE_FROM_ID (it->f, face_id);
25641 font = face->font ? face->font : FRAME_FONT (it->f);
25642 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25643
25644 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25645 {
25646 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25647 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25648 if (CONSP (acronym))
25649 acronym = XCAR (acronym);
25650 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25651 }
25652 else
25653 {
25654 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25655 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25656 str = buf;
25657 }
25658 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25659 code[len] = font->driver->encode_char (font, str[len]);
25660 upper_len = (len + 1) / 2;
25661 font->driver->text_extents (font, code, upper_len,
25662 &metrics_upper);
25663 font->driver->text_extents (font, code + upper_len, len - upper_len,
25664 &metrics_lower);
25665
25666
25667
25668 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25669 width = max (metrics_upper.width, metrics_lower.width) + 4;
25670 upper_xoff = upper_yoff = 2; /* the typical case */
25671 if (base_width >= width)
25672 {
25673 /* Align the upper to the left, the lower to the right. */
25674 it->pixel_width = base_width;
25675 lower_xoff = base_width - 2 - metrics_lower.width;
25676 }
25677 else
25678 {
25679 /* Center the shorter one. */
25680 it->pixel_width = width;
25681 if (metrics_upper.width >= metrics_lower.width)
25682 lower_xoff = (width - metrics_lower.width) / 2;
25683 else
25684 {
25685 /* FIXME: This code doesn't look right. It formerly was
25686 missing the "lower_xoff = 0;", which couldn't have
25687 been right since it left lower_xoff uninitialized. */
25688 lower_xoff = 0;
25689 upper_xoff = (width - metrics_upper.width) / 2;
25690 }
25691 }
25692
25693 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25694 top, bottom, and between upper and lower strings. */
25695 height = (metrics_upper.ascent + metrics_upper.descent
25696 + metrics_lower.ascent + metrics_lower.descent) + 5;
25697 /* Center vertically.
25698 H:base_height, D:base_descent
25699 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25700
25701 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25702 descent = D - H/2 + h/2;
25703 lower_yoff = descent - 2 - ld;
25704 upper_yoff = lower_yoff - la - 1 - ud; */
25705 ascent = - (it->descent - (base_height + height + 1) / 2);
25706 descent = it->descent - (base_height - height) / 2;
25707 lower_yoff = descent - 2 - metrics_lower.descent;
25708 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25709 - metrics_upper.descent);
25710 /* Don't make the height shorter than the base height. */
25711 if (height > base_height)
25712 {
25713 it->ascent = ascent;
25714 it->descent = descent;
25715 }
25716 }
25717
25718 it->phys_ascent = it->ascent;
25719 it->phys_descent = it->descent;
25720 if (it->glyph_row)
25721 append_glyphless_glyph (it, face_id, for_no_font, len,
25722 upper_xoff, upper_yoff,
25723 lower_xoff, lower_yoff);
25724 it->nglyphs = 1;
25725 take_vertical_position_into_account (it);
25726 }
25727
25728
25729 /* RIF:
25730 Produce glyphs/get display metrics for the display element IT is
25731 loaded with. See the description of struct it in dispextern.h
25732 for an overview of struct it. */
25733
25734 void
25735 x_produce_glyphs (struct it *it)
25736 {
25737 int extra_line_spacing = it->extra_line_spacing;
25738
25739 it->glyph_not_available_p = 0;
25740
25741 if (it->what == IT_CHARACTER)
25742 {
25743 XChar2b char2b;
25744 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25745 struct font *font = face->font;
25746 struct font_metrics *pcm = NULL;
25747 int boff; /* Baseline offset. */
25748
25749 if (font == NULL)
25750 {
25751 /* When no suitable font is found, display this character by
25752 the method specified in the first extra slot of
25753 Vglyphless_char_display. */
25754 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25755
25756 eassert (it->what == IT_GLYPHLESS);
25757 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25758 goto done;
25759 }
25760
25761 boff = font->baseline_offset;
25762 if (font->vertical_centering)
25763 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25764
25765 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25766 {
25767 int stretched_p;
25768
25769 it->nglyphs = 1;
25770
25771 if (it->override_ascent >= 0)
25772 {
25773 it->ascent = it->override_ascent;
25774 it->descent = it->override_descent;
25775 boff = it->override_boff;
25776 }
25777 else
25778 {
25779 it->ascent = FONT_BASE (font) + boff;
25780 it->descent = FONT_DESCENT (font) - boff;
25781 }
25782
25783 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25784 {
25785 pcm = get_per_char_metric (font, &char2b);
25786 if (pcm->width == 0
25787 && pcm->rbearing == 0 && pcm->lbearing == 0)
25788 pcm = NULL;
25789 }
25790
25791 if (pcm)
25792 {
25793 it->phys_ascent = pcm->ascent + boff;
25794 it->phys_descent = pcm->descent - boff;
25795 it->pixel_width = pcm->width;
25796 }
25797 else
25798 {
25799 it->glyph_not_available_p = 1;
25800 it->phys_ascent = it->ascent;
25801 it->phys_descent = it->descent;
25802 it->pixel_width = font->space_width;
25803 }
25804
25805 if (it->constrain_row_ascent_descent_p)
25806 {
25807 if (it->descent > it->max_descent)
25808 {
25809 it->ascent += it->descent - it->max_descent;
25810 it->descent = it->max_descent;
25811 }
25812 if (it->ascent > it->max_ascent)
25813 {
25814 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25815 it->ascent = it->max_ascent;
25816 }
25817 it->phys_ascent = min (it->phys_ascent, it->ascent);
25818 it->phys_descent = min (it->phys_descent, it->descent);
25819 extra_line_spacing = 0;
25820 }
25821
25822 /* If this is a space inside a region of text with
25823 `space-width' property, change its width. */
25824 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25825 if (stretched_p)
25826 it->pixel_width *= XFLOATINT (it->space_width);
25827
25828 /* If face has a box, add the box thickness to the character
25829 height. If character has a box line to the left and/or
25830 right, add the box line width to the character's width. */
25831 if (face->box != FACE_NO_BOX)
25832 {
25833 int thick = face->box_line_width;
25834
25835 if (thick > 0)
25836 {
25837 it->ascent += thick;
25838 it->descent += thick;
25839 }
25840 else
25841 thick = -thick;
25842
25843 if (it->start_of_box_run_p)
25844 it->pixel_width += thick;
25845 if (it->end_of_box_run_p)
25846 it->pixel_width += thick;
25847 }
25848
25849 /* If face has an overline, add the height of the overline
25850 (1 pixel) and a 1 pixel margin to the character height. */
25851 if (face->overline_p)
25852 it->ascent += overline_margin;
25853
25854 if (it->constrain_row_ascent_descent_p)
25855 {
25856 if (it->ascent > it->max_ascent)
25857 it->ascent = it->max_ascent;
25858 if (it->descent > it->max_descent)
25859 it->descent = it->max_descent;
25860 }
25861
25862 take_vertical_position_into_account (it);
25863
25864 /* If we have to actually produce glyphs, do it. */
25865 if (it->glyph_row)
25866 {
25867 if (stretched_p)
25868 {
25869 /* Translate a space with a `space-width' property
25870 into a stretch glyph. */
25871 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25872 / FONT_HEIGHT (font));
25873 append_stretch_glyph (it, it->object, it->pixel_width,
25874 it->ascent + it->descent, ascent);
25875 }
25876 else
25877 append_glyph (it);
25878
25879 /* If characters with lbearing or rbearing are displayed
25880 in this line, record that fact in a flag of the
25881 glyph row. This is used to optimize X output code. */
25882 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25883 it->glyph_row->contains_overlapping_glyphs_p = 1;
25884 }
25885 if (! stretched_p && it->pixel_width == 0)
25886 /* We assure that all visible glyphs have at least 1-pixel
25887 width. */
25888 it->pixel_width = 1;
25889 }
25890 else if (it->char_to_display == '\n')
25891 {
25892 /* A newline has no width, but we need the height of the
25893 line. But if previous part of the line sets a height,
25894 don't increase that height. */
25895
25896 Lisp_Object height;
25897 Lisp_Object total_height = Qnil;
25898
25899 it->override_ascent = -1;
25900 it->pixel_width = 0;
25901 it->nglyphs = 0;
25902
25903 height = get_it_property (it, Qline_height);
25904 /* Split (line-height total-height) list. */
25905 if (CONSP (height)
25906 && CONSP (XCDR (height))
25907 && NILP (XCDR (XCDR (height))))
25908 {
25909 total_height = XCAR (XCDR (height));
25910 height = XCAR (height);
25911 }
25912 height = calc_line_height_property (it, height, font, boff, 1);
25913
25914 if (it->override_ascent >= 0)
25915 {
25916 it->ascent = it->override_ascent;
25917 it->descent = it->override_descent;
25918 boff = it->override_boff;
25919 }
25920 else
25921 {
25922 it->ascent = FONT_BASE (font) + boff;
25923 it->descent = FONT_DESCENT (font) - boff;
25924 }
25925
25926 if (EQ (height, Qt))
25927 {
25928 if (it->descent > it->max_descent)
25929 {
25930 it->ascent += it->descent - it->max_descent;
25931 it->descent = it->max_descent;
25932 }
25933 if (it->ascent > it->max_ascent)
25934 {
25935 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25936 it->ascent = it->max_ascent;
25937 }
25938 it->phys_ascent = min (it->phys_ascent, it->ascent);
25939 it->phys_descent = min (it->phys_descent, it->descent);
25940 it->constrain_row_ascent_descent_p = 1;
25941 extra_line_spacing = 0;
25942 }
25943 else
25944 {
25945 Lisp_Object spacing;
25946
25947 it->phys_ascent = it->ascent;
25948 it->phys_descent = it->descent;
25949
25950 if ((it->max_ascent > 0 || it->max_descent > 0)
25951 && face->box != FACE_NO_BOX
25952 && face->box_line_width > 0)
25953 {
25954 it->ascent += face->box_line_width;
25955 it->descent += face->box_line_width;
25956 }
25957 if (!NILP (height)
25958 && XINT (height) > it->ascent + it->descent)
25959 it->ascent = XINT (height) - it->descent;
25960
25961 if (!NILP (total_height))
25962 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25963 else
25964 {
25965 spacing = get_it_property (it, Qline_spacing);
25966 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25967 }
25968 if (INTEGERP (spacing))
25969 {
25970 extra_line_spacing = XINT (spacing);
25971 if (!NILP (total_height))
25972 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25973 }
25974 }
25975 }
25976 else /* i.e. (it->char_to_display == '\t') */
25977 {
25978 if (font->space_width > 0)
25979 {
25980 int tab_width = it->tab_width * font->space_width;
25981 int x = it->current_x + it->continuation_lines_width;
25982 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25983
25984 /* If the distance from the current position to the next tab
25985 stop is less than a space character width, use the
25986 tab stop after that. */
25987 if (next_tab_x - x < font->space_width)
25988 next_tab_x += tab_width;
25989
25990 it->pixel_width = next_tab_x - x;
25991 it->nglyphs = 1;
25992 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25993 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25994
25995 if (it->glyph_row)
25996 {
25997 append_stretch_glyph (it, it->object, it->pixel_width,
25998 it->ascent + it->descent, it->ascent);
25999 }
26000 }
26001 else
26002 {
26003 it->pixel_width = 0;
26004 it->nglyphs = 1;
26005 }
26006 }
26007 }
26008 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26009 {
26010 /* A static composition.
26011
26012 Note: A composition is represented as one glyph in the
26013 glyph matrix. There are no padding glyphs.
26014
26015 Important note: pixel_width, ascent, and descent are the
26016 values of what is drawn by draw_glyphs (i.e. the values of
26017 the overall glyphs composed). */
26018 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26019 int boff; /* baseline offset */
26020 struct composition *cmp = composition_table[it->cmp_it.id];
26021 int glyph_len = cmp->glyph_len;
26022 struct font *font = face->font;
26023
26024 it->nglyphs = 1;
26025
26026 /* If we have not yet calculated pixel size data of glyphs of
26027 the composition for the current face font, calculate them
26028 now. Theoretically, we have to check all fonts for the
26029 glyphs, but that requires much time and memory space. So,
26030 here we check only the font of the first glyph. This may
26031 lead to incorrect display, but it's very rare, and C-l
26032 (recenter-top-bottom) can correct the display anyway. */
26033 if (! cmp->font || cmp->font != font)
26034 {
26035 /* Ascent and descent of the font of the first character
26036 of this composition (adjusted by baseline offset).
26037 Ascent and descent of overall glyphs should not be less
26038 than these, respectively. */
26039 int font_ascent, font_descent, font_height;
26040 /* Bounding box of the overall glyphs. */
26041 int leftmost, rightmost, lowest, highest;
26042 int lbearing, rbearing;
26043 int i, width, ascent, descent;
26044 int left_padded = 0, right_padded = 0;
26045 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26046 XChar2b char2b;
26047 struct font_metrics *pcm;
26048 int font_not_found_p;
26049 ptrdiff_t pos;
26050
26051 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26052 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26053 break;
26054 if (glyph_len < cmp->glyph_len)
26055 right_padded = 1;
26056 for (i = 0; i < glyph_len; i++)
26057 {
26058 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26059 break;
26060 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26061 }
26062 if (i > 0)
26063 left_padded = 1;
26064
26065 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26066 : IT_CHARPOS (*it));
26067 /* If no suitable font is found, use the default font. */
26068 font_not_found_p = font == NULL;
26069 if (font_not_found_p)
26070 {
26071 face = face->ascii_face;
26072 font = face->font;
26073 }
26074 boff = font->baseline_offset;
26075 if (font->vertical_centering)
26076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26077 font_ascent = FONT_BASE (font) + boff;
26078 font_descent = FONT_DESCENT (font) - boff;
26079 font_height = FONT_HEIGHT (font);
26080
26081 cmp->font = font;
26082
26083 pcm = NULL;
26084 if (! font_not_found_p)
26085 {
26086 get_char_face_and_encoding (it->f, c, it->face_id,
26087 &char2b, 0);
26088 pcm = get_per_char_metric (font, &char2b);
26089 }
26090
26091 /* Initialize the bounding box. */
26092 if (pcm)
26093 {
26094 width = cmp->glyph_len > 0 ? pcm->width : 0;
26095 ascent = pcm->ascent;
26096 descent = pcm->descent;
26097 lbearing = pcm->lbearing;
26098 rbearing = pcm->rbearing;
26099 }
26100 else
26101 {
26102 width = cmp->glyph_len > 0 ? font->space_width : 0;
26103 ascent = FONT_BASE (font);
26104 descent = FONT_DESCENT (font);
26105 lbearing = 0;
26106 rbearing = width;
26107 }
26108
26109 rightmost = width;
26110 leftmost = 0;
26111 lowest = - descent + boff;
26112 highest = ascent + boff;
26113
26114 if (! font_not_found_p
26115 && font->default_ascent
26116 && CHAR_TABLE_P (Vuse_default_ascent)
26117 && !NILP (Faref (Vuse_default_ascent,
26118 make_number (it->char_to_display))))
26119 highest = font->default_ascent + boff;
26120
26121 /* Draw the first glyph at the normal position. It may be
26122 shifted to right later if some other glyphs are drawn
26123 at the left. */
26124 cmp->offsets[i * 2] = 0;
26125 cmp->offsets[i * 2 + 1] = boff;
26126 cmp->lbearing = lbearing;
26127 cmp->rbearing = rbearing;
26128
26129 /* Set cmp->offsets for the remaining glyphs. */
26130 for (i++; i < glyph_len; i++)
26131 {
26132 int left, right, btm, top;
26133 int ch = COMPOSITION_GLYPH (cmp, i);
26134 int face_id;
26135 struct face *this_face;
26136
26137 if (ch == '\t')
26138 ch = ' ';
26139 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26140 this_face = FACE_FROM_ID (it->f, face_id);
26141 font = this_face->font;
26142
26143 if (font == NULL)
26144 pcm = NULL;
26145 else
26146 {
26147 get_char_face_and_encoding (it->f, ch, face_id,
26148 &char2b, 0);
26149 pcm = get_per_char_metric (font, &char2b);
26150 }
26151 if (! pcm)
26152 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26153 else
26154 {
26155 width = pcm->width;
26156 ascent = pcm->ascent;
26157 descent = pcm->descent;
26158 lbearing = pcm->lbearing;
26159 rbearing = pcm->rbearing;
26160 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26161 {
26162 /* Relative composition with or without
26163 alternate chars. */
26164 left = (leftmost + rightmost - width) / 2;
26165 btm = - descent + boff;
26166 if (font->relative_compose
26167 && (! CHAR_TABLE_P (Vignore_relative_composition)
26168 || NILP (Faref (Vignore_relative_composition,
26169 make_number (ch)))))
26170 {
26171
26172 if (- descent >= font->relative_compose)
26173 /* One extra pixel between two glyphs. */
26174 btm = highest + 1;
26175 else if (ascent <= 0)
26176 /* One extra pixel between two glyphs. */
26177 btm = lowest - 1 - ascent - descent;
26178 }
26179 }
26180 else
26181 {
26182 /* A composition rule is specified by an integer
26183 value that encodes global and new reference
26184 points (GREF and NREF). GREF and NREF are
26185 specified by numbers as below:
26186
26187 0---1---2 -- ascent
26188 | |
26189 | |
26190 | |
26191 9--10--11 -- center
26192 | |
26193 ---3---4---5--- baseline
26194 | |
26195 6---7---8 -- descent
26196 */
26197 int rule = COMPOSITION_RULE (cmp, i);
26198 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26199
26200 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26201 grefx = gref % 3, nrefx = nref % 3;
26202 grefy = gref / 3, nrefy = nref / 3;
26203 if (xoff)
26204 xoff = font_height * (xoff - 128) / 256;
26205 if (yoff)
26206 yoff = font_height * (yoff - 128) / 256;
26207
26208 left = (leftmost
26209 + grefx * (rightmost - leftmost) / 2
26210 - nrefx * width / 2
26211 + xoff);
26212
26213 btm = ((grefy == 0 ? highest
26214 : grefy == 1 ? 0
26215 : grefy == 2 ? lowest
26216 : (highest + lowest) / 2)
26217 - (nrefy == 0 ? ascent + descent
26218 : nrefy == 1 ? descent - boff
26219 : nrefy == 2 ? 0
26220 : (ascent + descent) / 2)
26221 + yoff);
26222 }
26223
26224 cmp->offsets[i * 2] = left;
26225 cmp->offsets[i * 2 + 1] = btm + descent;
26226
26227 /* Update the bounding box of the overall glyphs. */
26228 if (width > 0)
26229 {
26230 right = left + width;
26231 if (left < leftmost)
26232 leftmost = left;
26233 if (right > rightmost)
26234 rightmost = right;
26235 }
26236 top = btm + descent + ascent;
26237 if (top > highest)
26238 highest = top;
26239 if (btm < lowest)
26240 lowest = btm;
26241
26242 if (cmp->lbearing > left + lbearing)
26243 cmp->lbearing = left + lbearing;
26244 if (cmp->rbearing < left + rbearing)
26245 cmp->rbearing = left + rbearing;
26246 }
26247 }
26248
26249 /* If there are glyphs whose x-offsets are negative,
26250 shift all glyphs to the right and make all x-offsets
26251 non-negative. */
26252 if (leftmost < 0)
26253 {
26254 for (i = 0; i < cmp->glyph_len; i++)
26255 cmp->offsets[i * 2] -= leftmost;
26256 rightmost -= leftmost;
26257 cmp->lbearing -= leftmost;
26258 cmp->rbearing -= leftmost;
26259 }
26260
26261 if (left_padded && cmp->lbearing < 0)
26262 {
26263 for (i = 0; i < cmp->glyph_len; i++)
26264 cmp->offsets[i * 2] -= cmp->lbearing;
26265 rightmost -= cmp->lbearing;
26266 cmp->rbearing -= cmp->lbearing;
26267 cmp->lbearing = 0;
26268 }
26269 if (right_padded && rightmost < cmp->rbearing)
26270 {
26271 rightmost = cmp->rbearing;
26272 }
26273
26274 cmp->pixel_width = rightmost;
26275 cmp->ascent = highest;
26276 cmp->descent = - lowest;
26277 if (cmp->ascent < font_ascent)
26278 cmp->ascent = font_ascent;
26279 if (cmp->descent < font_descent)
26280 cmp->descent = font_descent;
26281 }
26282
26283 if (it->glyph_row
26284 && (cmp->lbearing < 0
26285 || cmp->rbearing > cmp->pixel_width))
26286 it->glyph_row->contains_overlapping_glyphs_p = 1;
26287
26288 it->pixel_width = cmp->pixel_width;
26289 it->ascent = it->phys_ascent = cmp->ascent;
26290 it->descent = it->phys_descent = cmp->descent;
26291 if (face->box != FACE_NO_BOX)
26292 {
26293 int thick = face->box_line_width;
26294
26295 if (thick > 0)
26296 {
26297 it->ascent += thick;
26298 it->descent += thick;
26299 }
26300 else
26301 thick = - thick;
26302
26303 if (it->start_of_box_run_p)
26304 it->pixel_width += thick;
26305 if (it->end_of_box_run_p)
26306 it->pixel_width += thick;
26307 }
26308
26309 /* If face has an overline, add the height of the overline
26310 (1 pixel) and a 1 pixel margin to the character height. */
26311 if (face->overline_p)
26312 it->ascent += overline_margin;
26313
26314 take_vertical_position_into_account (it);
26315 if (it->ascent < 0)
26316 it->ascent = 0;
26317 if (it->descent < 0)
26318 it->descent = 0;
26319
26320 if (it->glyph_row && cmp->glyph_len > 0)
26321 append_composite_glyph (it);
26322 }
26323 else if (it->what == IT_COMPOSITION)
26324 {
26325 /* A dynamic (automatic) composition. */
26326 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26327 Lisp_Object gstring;
26328 struct font_metrics metrics;
26329
26330 it->nglyphs = 1;
26331
26332 gstring = composition_gstring_from_id (it->cmp_it.id);
26333 it->pixel_width
26334 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26335 &metrics);
26336 if (it->glyph_row
26337 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26338 it->glyph_row->contains_overlapping_glyphs_p = 1;
26339 it->ascent = it->phys_ascent = metrics.ascent;
26340 it->descent = it->phys_descent = metrics.descent;
26341 if (face->box != FACE_NO_BOX)
26342 {
26343 int thick = face->box_line_width;
26344
26345 if (thick > 0)
26346 {
26347 it->ascent += thick;
26348 it->descent += thick;
26349 }
26350 else
26351 thick = - thick;
26352
26353 if (it->start_of_box_run_p)
26354 it->pixel_width += thick;
26355 if (it->end_of_box_run_p)
26356 it->pixel_width += thick;
26357 }
26358 /* If face has an overline, add the height of the overline
26359 (1 pixel) and a 1 pixel margin to the character height. */
26360 if (face->overline_p)
26361 it->ascent += overline_margin;
26362 take_vertical_position_into_account (it);
26363 if (it->ascent < 0)
26364 it->ascent = 0;
26365 if (it->descent < 0)
26366 it->descent = 0;
26367
26368 if (it->glyph_row)
26369 append_composite_glyph (it);
26370 }
26371 else if (it->what == IT_GLYPHLESS)
26372 produce_glyphless_glyph (it, 0, Qnil);
26373 else if (it->what == IT_IMAGE)
26374 produce_image_glyph (it);
26375 else if (it->what == IT_STRETCH)
26376 produce_stretch_glyph (it);
26377
26378 done:
26379 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26380 because this isn't true for images with `:ascent 100'. */
26381 eassert (it->ascent >= 0 && it->descent >= 0);
26382 if (it->area == TEXT_AREA)
26383 it->current_x += it->pixel_width;
26384
26385 if (extra_line_spacing > 0)
26386 {
26387 it->descent += extra_line_spacing;
26388 if (extra_line_spacing > it->max_extra_line_spacing)
26389 it->max_extra_line_spacing = extra_line_spacing;
26390 }
26391
26392 it->max_ascent = max (it->max_ascent, it->ascent);
26393 it->max_descent = max (it->max_descent, it->descent);
26394 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26395 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26396 }
26397
26398 /* EXPORT for RIF:
26399 Output LEN glyphs starting at START at the nominal cursor position.
26400 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26401 being updated, and UPDATED_AREA is the area of that row being updated. */
26402
26403 void
26404 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26405 struct glyph *start, enum glyph_row_area updated_area, int len)
26406 {
26407 int x, hpos, chpos = w->phys_cursor.hpos;
26408
26409 eassert (updated_row);
26410 /* When the window is hscrolled, cursor hpos can legitimately be out
26411 of bounds, but we draw the cursor at the corresponding window
26412 margin in that case. */
26413 if (!updated_row->reversed_p && chpos < 0)
26414 chpos = 0;
26415 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26416 chpos = updated_row->used[TEXT_AREA] - 1;
26417
26418 block_input ();
26419
26420 /* Write glyphs. */
26421
26422 hpos = start - updated_row->glyphs[updated_area];
26423 x = draw_glyphs (w, w->output_cursor.x,
26424 updated_row, updated_area,
26425 hpos, hpos + len,
26426 DRAW_NORMAL_TEXT, 0);
26427
26428 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26429 if (updated_area == TEXT_AREA
26430 && w->phys_cursor_on_p
26431 && w->phys_cursor.vpos == w->output_cursor.vpos
26432 && chpos >= hpos
26433 && chpos < hpos + len)
26434 w->phys_cursor_on_p = 0;
26435
26436 unblock_input ();
26437
26438 /* Advance the output cursor. */
26439 w->output_cursor.hpos += len;
26440 w->output_cursor.x = x;
26441 }
26442
26443
26444 /* EXPORT for RIF:
26445 Insert LEN glyphs from START at the nominal cursor position. */
26446
26447 void
26448 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26449 struct glyph *start, enum glyph_row_area updated_area, int len)
26450 {
26451 struct frame *f;
26452 int line_height, shift_by_width, shifted_region_width;
26453 struct glyph_row *row;
26454 struct glyph *glyph;
26455 int frame_x, frame_y;
26456 ptrdiff_t hpos;
26457
26458 eassert (updated_row);
26459 block_input ();
26460 f = XFRAME (WINDOW_FRAME (w));
26461
26462 /* Get the height of the line we are in. */
26463 row = updated_row;
26464 line_height = row->height;
26465
26466 /* Get the width of the glyphs to insert. */
26467 shift_by_width = 0;
26468 for (glyph = start; glyph < start + len; ++glyph)
26469 shift_by_width += glyph->pixel_width;
26470
26471 /* Get the width of the region to shift right. */
26472 shifted_region_width = (window_box_width (w, updated_area)
26473 - w->output_cursor.x
26474 - shift_by_width);
26475
26476 /* Shift right. */
26477 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26478 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26479
26480 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26481 line_height, shift_by_width);
26482
26483 /* Write the glyphs. */
26484 hpos = start - row->glyphs[updated_area];
26485 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26486 hpos, hpos + len,
26487 DRAW_NORMAL_TEXT, 0);
26488
26489 /* Advance the output cursor. */
26490 w->output_cursor.hpos += len;
26491 w->output_cursor.x += shift_by_width;
26492 unblock_input ();
26493 }
26494
26495
26496 /* EXPORT for RIF:
26497 Erase the current text line from the nominal cursor position
26498 (inclusive) to pixel column TO_X (exclusive). The idea is that
26499 everything from TO_X onward is already erased.
26500
26501 TO_X is a pixel position relative to UPDATED_AREA of currently
26502 updated window W. TO_X == -1 means clear to the end of this area. */
26503
26504 void
26505 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26506 enum glyph_row_area updated_area, int to_x)
26507 {
26508 struct frame *f;
26509 int max_x, min_y, max_y;
26510 int from_x, from_y, to_y;
26511
26512 eassert (updated_row);
26513 f = XFRAME (w->frame);
26514
26515 if (updated_row->full_width_p)
26516 max_x = (WINDOW_PIXEL_WIDTH (w)
26517 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26518 else
26519 max_x = window_box_width (w, updated_area);
26520 max_y = window_text_bottom_y (w);
26521
26522 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26523 of window. For TO_X > 0, truncate to end of drawing area. */
26524 if (to_x == 0)
26525 return;
26526 else if (to_x < 0)
26527 to_x = max_x;
26528 else
26529 to_x = min (to_x, max_x);
26530
26531 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26532
26533 /* Notice if the cursor will be cleared by this operation. */
26534 if (!updated_row->full_width_p)
26535 notice_overwritten_cursor (w, updated_area,
26536 w->output_cursor.x, -1,
26537 updated_row->y,
26538 MATRIX_ROW_BOTTOM_Y (updated_row));
26539
26540 from_x = w->output_cursor.x;
26541
26542 /* Translate to frame coordinates. */
26543 if (updated_row->full_width_p)
26544 {
26545 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26546 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26547 }
26548 else
26549 {
26550 int area_left = window_box_left (w, updated_area);
26551 from_x += area_left;
26552 to_x += area_left;
26553 }
26554
26555 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26556 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26557 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26558
26559 /* Prevent inadvertently clearing to end of the X window. */
26560 if (to_x > from_x && to_y > from_y)
26561 {
26562 block_input ();
26563 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26564 to_x - from_x, to_y - from_y);
26565 unblock_input ();
26566 }
26567 }
26568
26569 #endif /* HAVE_WINDOW_SYSTEM */
26570
26571
26572 \f
26573 /***********************************************************************
26574 Cursor types
26575 ***********************************************************************/
26576
26577 /* Value is the internal representation of the specified cursor type
26578 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26579 of the bar cursor. */
26580
26581 static enum text_cursor_kinds
26582 get_specified_cursor_type (Lisp_Object arg, int *width)
26583 {
26584 enum text_cursor_kinds type;
26585
26586 if (NILP (arg))
26587 return NO_CURSOR;
26588
26589 if (EQ (arg, Qbox))
26590 return FILLED_BOX_CURSOR;
26591
26592 if (EQ (arg, Qhollow))
26593 return HOLLOW_BOX_CURSOR;
26594
26595 if (EQ (arg, Qbar))
26596 {
26597 *width = 2;
26598 return BAR_CURSOR;
26599 }
26600
26601 if (CONSP (arg)
26602 && EQ (XCAR (arg), Qbar)
26603 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26604 {
26605 *width = XINT (XCDR (arg));
26606 return BAR_CURSOR;
26607 }
26608
26609 if (EQ (arg, Qhbar))
26610 {
26611 *width = 2;
26612 return HBAR_CURSOR;
26613 }
26614
26615 if (CONSP (arg)
26616 && EQ (XCAR (arg), Qhbar)
26617 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26618 {
26619 *width = XINT (XCDR (arg));
26620 return HBAR_CURSOR;
26621 }
26622
26623 /* Treat anything unknown as "hollow box cursor".
26624 It was bad to signal an error; people have trouble fixing
26625 .Xdefaults with Emacs, when it has something bad in it. */
26626 type = HOLLOW_BOX_CURSOR;
26627
26628 return type;
26629 }
26630
26631 /* Set the default cursor types for specified frame. */
26632 void
26633 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26634 {
26635 int width = 1;
26636 Lisp_Object tem;
26637
26638 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26639 FRAME_CURSOR_WIDTH (f) = width;
26640
26641 /* By default, set up the blink-off state depending on the on-state. */
26642
26643 tem = Fassoc (arg, Vblink_cursor_alist);
26644 if (!NILP (tem))
26645 {
26646 FRAME_BLINK_OFF_CURSOR (f)
26647 = get_specified_cursor_type (XCDR (tem), &width);
26648 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26649 }
26650 else
26651 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26652
26653 /* Make sure the cursor gets redrawn. */
26654 f->cursor_type_changed = 1;
26655 }
26656
26657
26658 #ifdef HAVE_WINDOW_SYSTEM
26659
26660 /* Return the cursor we want to be displayed in window W. Return
26661 width of bar/hbar cursor through WIDTH arg. Return with
26662 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26663 (i.e. if the `system caret' should track this cursor).
26664
26665 In a mini-buffer window, we want the cursor only to appear if we
26666 are reading input from this window. For the selected window, we
26667 want the cursor type given by the frame parameter or buffer local
26668 setting of cursor-type. If explicitly marked off, draw no cursor.
26669 In all other cases, we want a hollow box cursor. */
26670
26671 static enum text_cursor_kinds
26672 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26673 int *active_cursor)
26674 {
26675 struct frame *f = XFRAME (w->frame);
26676 struct buffer *b = XBUFFER (w->contents);
26677 int cursor_type = DEFAULT_CURSOR;
26678 Lisp_Object alt_cursor;
26679 int non_selected = 0;
26680
26681 *active_cursor = 1;
26682
26683 /* Echo area */
26684 if (cursor_in_echo_area
26685 && FRAME_HAS_MINIBUF_P (f)
26686 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26687 {
26688 if (w == XWINDOW (echo_area_window))
26689 {
26690 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26691 {
26692 *width = FRAME_CURSOR_WIDTH (f);
26693 return FRAME_DESIRED_CURSOR (f);
26694 }
26695 else
26696 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26697 }
26698
26699 *active_cursor = 0;
26700 non_selected = 1;
26701 }
26702
26703 /* Detect a nonselected window or nonselected frame. */
26704 else if (w != XWINDOW (f->selected_window)
26705 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26706 {
26707 *active_cursor = 0;
26708
26709 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26710 return NO_CURSOR;
26711
26712 non_selected = 1;
26713 }
26714
26715 /* Never display a cursor in a window in which cursor-type is nil. */
26716 if (NILP (BVAR (b, cursor_type)))
26717 return NO_CURSOR;
26718
26719 /* Get the normal cursor type for this window. */
26720 if (EQ (BVAR (b, cursor_type), Qt))
26721 {
26722 cursor_type = FRAME_DESIRED_CURSOR (f);
26723 *width = FRAME_CURSOR_WIDTH (f);
26724 }
26725 else
26726 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26727
26728 /* Use cursor-in-non-selected-windows instead
26729 for non-selected window or frame. */
26730 if (non_selected)
26731 {
26732 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26733 if (!EQ (Qt, alt_cursor))
26734 return get_specified_cursor_type (alt_cursor, width);
26735 /* t means modify the normal cursor type. */
26736 if (cursor_type == FILLED_BOX_CURSOR)
26737 cursor_type = HOLLOW_BOX_CURSOR;
26738 else if (cursor_type == BAR_CURSOR && *width > 1)
26739 --*width;
26740 return cursor_type;
26741 }
26742
26743 /* Use normal cursor if not blinked off. */
26744 if (!w->cursor_off_p)
26745 {
26746 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26747 {
26748 if (cursor_type == FILLED_BOX_CURSOR)
26749 {
26750 /* Using a block cursor on large images can be very annoying.
26751 So use a hollow cursor for "large" images.
26752 If image is not transparent (no mask), also use hollow cursor. */
26753 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26754 if (img != NULL && IMAGEP (img->spec))
26755 {
26756 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26757 where N = size of default frame font size.
26758 This should cover most of the "tiny" icons people may use. */
26759 if (!img->mask
26760 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26761 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26762 cursor_type = HOLLOW_BOX_CURSOR;
26763 }
26764 }
26765 else if (cursor_type != NO_CURSOR)
26766 {
26767 /* Display current only supports BOX and HOLLOW cursors for images.
26768 So for now, unconditionally use a HOLLOW cursor when cursor is
26769 not a solid box cursor. */
26770 cursor_type = HOLLOW_BOX_CURSOR;
26771 }
26772 }
26773 return cursor_type;
26774 }
26775
26776 /* Cursor is blinked off, so determine how to "toggle" it. */
26777
26778 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26779 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26780 return get_specified_cursor_type (XCDR (alt_cursor), width);
26781
26782 /* Then see if frame has specified a specific blink off cursor type. */
26783 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26784 {
26785 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26786 return FRAME_BLINK_OFF_CURSOR (f);
26787 }
26788
26789 #if 0
26790 /* Some people liked having a permanently visible blinking cursor,
26791 while others had very strong opinions against it. So it was
26792 decided to remove it. KFS 2003-09-03 */
26793
26794 /* Finally perform built-in cursor blinking:
26795 filled box <-> hollow box
26796 wide [h]bar <-> narrow [h]bar
26797 narrow [h]bar <-> no cursor
26798 other type <-> no cursor */
26799
26800 if (cursor_type == FILLED_BOX_CURSOR)
26801 return HOLLOW_BOX_CURSOR;
26802
26803 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26804 {
26805 *width = 1;
26806 return cursor_type;
26807 }
26808 #endif
26809
26810 return NO_CURSOR;
26811 }
26812
26813
26814 /* Notice when the text cursor of window W has been completely
26815 overwritten by a drawing operation that outputs glyphs in AREA
26816 starting at X0 and ending at X1 in the line starting at Y0 and
26817 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26818 the rest of the line after X0 has been written. Y coordinates
26819 are window-relative. */
26820
26821 static void
26822 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26823 int x0, int x1, int y0, int y1)
26824 {
26825 int cx0, cx1, cy0, cy1;
26826 struct glyph_row *row;
26827
26828 if (!w->phys_cursor_on_p)
26829 return;
26830 if (area != TEXT_AREA)
26831 return;
26832
26833 if (w->phys_cursor.vpos < 0
26834 || w->phys_cursor.vpos >= w->current_matrix->nrows
26835 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26836 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26837 return;
26838
26839 if (row->cursor_in_fringe_p)
26840 {
26841 row->cursor_in_fringe_p = 0;
26842 draw_fringe_bitmap (w, row, row->reversed_p);
26843 w->phys_cursor_on_p = 0;
26844 return;
26845 }
26846
26847 cx0 = w->phys_cursor.x;
26848 cx1 = cx0 + w->phys_cursor_width;
26849 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26850 return;
26851
26852 /* The cursor image will be completely removed from the
26853 screen if the output area intersects the cursor area in
26854 y-direction. When we draw in [y0 y1[, and some part of
26855 the cursor is at y < y0, that part must have been drawn
26856 before. When scrolling, the cursor is erased before
26857 actually scrolling, so we don't come here. When not
26858 scrolling, the rows above the old cursor row must have
26859 changed, and in this case these rows must have written
26860 over the cursor image.
26861
26862 Likewise if part of the cursor is below y1, with the
26863 exception of the cursor being in the first blank row at
26864 the buffer and window end because update_text_area
26865 doesn't draw that row. (Except when it does, but
26866 that's handled in update_text_area.) */
26867
26868 cy0 = w->phys_cursor.y;
26869 cy1 = cy0 + w->phys_cursor_height;
26870 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26871 return;
26872
26873 w->phys_cursor_on_p = 0;
26874 }
26875
26876 #endif /* HAVE_WINDOW_SYSTEM */
26877
26878 \f
26879 /************************************************************************
26880 Mouse Face
26881 ************************************************************************/
26882
26883 #ifdef HAVE_WINDOW_SYSTEM
26884
26885 /* EXPORT for RIF:
26886 Fix the display of area AREA of overlapping row ROW in window W
26887 with respect to the overlapping part OVERLAPS. */
26888
26889 void
26890 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26891 enum glyph_row_area area, int overlaps)
26892 {
26893 int i, x;
26894
26895 block_input ();
26896
26897 x = 0;
26898 for (i = 0; i < row->used[area];)
26899 {
26900 if (row->glyphs[area][i].overlaps_vertically_p)
26901 {
26902 int start = i, start_x = x;
26903
26904 do
26905 {
26906 x += row->glyphs[area][i].pixel_width;
26907 ++i;
26908 }
26909 while (i < row->used[area]
26910 && row->glyphs[area][i].overlaps_vertically_p);
26911
26912 draw_glyphs (w, start_x, row, area,
26913 start, i,
26914 DRAW_NORMAL_TEXT, overlaps);
26915 }
26916 else
26917 {
26918 x += row->glyphs[area][i].pixel_width;
26919 ++i;
26920 }
26921 }
26922
26923 unblock_input ();
26924 }
26925
26926
26927 /* EXPORT:
26928 Draw the cursor glyph of window W in glyph row ROW. See the
26929 comment of draw_glyphs for the meaning of HL. */
26930
26931 void
26932 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26933 enum draw_glyphs_face hl)
26934 {
26935 /* If cursor hpos is out of bounds, don't draw garbage. This can
26936 happen in mini-buffer windows when switching between echo area
26937 glyphs and mini-buffer. */
26938 if ((row->reversed_p
26939 ? (w->phys_cursor.hpos >= 0)
26940 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26941 {
26942 int on_p = w->phys_cursor_on_p;
26943 int x1;
26944 int hpos = w->phys_cursor.hpos;
26945
26946 /* When the window is hscrolled, cursor hpos can legitimately be
26947 out of bounds, but we draw the cursor at the corresponding
26948 window margin in that case. */
26949 if (!row->reversed_p && hpos < 0)
26950 hpos = 0;
26951 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26952 hpos = row->used[TEXT_AREA] - 1;
26953
26954 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26955 hl, 0);
26956 w->phys_cursor_on_p = on_p;
26957
26958 if (hl == DRAW_CURSOR)
26959 w->phys_cursor_width = x1 - w->phys_cursor.x;
26960 /* When we erase the cursor, and ROW is overlapped by other
26961 rows, make sure that these overlapping parts of other rows
26962 are redrawn. */
26963 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26964 {
26965 w->phys_cursor_width = x1 - w->phys_cursor.x;
26966
26967 if (row > w->current_matrix->rows
26968 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26969 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26970 OVERLAPS_ERASED_CURSOR);
26971
26972 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26973 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26974 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26975 OVERLAPS_ERASED_CURSOR);
26976 }
26977 }
26978 }
26979
26980
26981 /* Erase the image of a cursor of window W from the screen. */
26982
26983 #ifndef HAVE_NTGUI
26984 static
26985 #endif
26986 void
26987 erase_phys_cursor (struct window *w)
26988 {
26989 struct frame *f = XFRAME (w->frame);
26990 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26991 int hpos = w->phys_cursor.hpos;
26992 int vpos = w->phys_cursor.vpos;
26993 int mouse_face_here_p = 0;
26994 struct glyph_matrix *active_glyphs = w->current_matrix;
26995 struct glyph_row *cursor_row;
26996 struct glyph *cursor_glyph;
26997 enum draw_glyphs_face hl;
26998
26999 /* No cursor displayed or row invalidated => nothing to do on the
27000 screen. */
27001 if (w->phys_cursor_type == NO_CURSOR)
27002 goto mark_cursor_off;
27003
27004 /* VPOS >= active_glyphs->nrows means that window has been resized.
27005 Don't bother to erase the cursor. */
27006 if (vpos >= active_glyphs->nrows)
27007 goto mark_cursor_off;
27008
27009 /* If row containing cursor is marked invalid, there is nothing we
27010 can do. */
27011 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27012 if (!cursor_row->enabled_p)
27013 goto mark_cursor_off;
27014
27015 /* If line spacing is > 0, old cursor may only be partially visible in
27016 window after split-window. So adjust visible height. */
27017 cursor_row->visible_height = min (cursor_row->visible_height,
27018 window_text_bottom_y (w) - cursor_row->y);
27019
27020 /* If row is completely invisible, don't attempt to delete a cursor which
27021 isn't there. This can happen if cursor is at top of a window, and
27022 we switch to a buffer with a header line in that window. */
27023 if (cursor_row->visible_height <= 0)
27024 goto mark_cursor_off;
27025
27026 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27027 if (cursor_row->cursor_in_fringe_p)
27028 {
27029 cursor_row->cursor_in_fringe_p = 0;
27030 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27031 goto mark_cursor_off;
27032 }
27033
27034 /* This can happen when the new row is shorter than the old one.
27035 In this case, either draw_glyphs or clear_end_of_line
27036 should have cleared the cursor. Note that we wouldn't be
27037 able to erase the cursor in this case because we don't have a
27038 cursor glyph at hand. */
27039 if ((cursor_row->reversed_p
27040 ? (w->phys_cursor.hpos < 0)
27041 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27042 goto mark_cursor_off;
27043
27044 /* When the window is hscrolled, cursor hpos can legitimately be out
27045 of bounds, but we draw the cursor at the corresponding window
27046 margin in that case. */
27047 if (!cursor_row->reversed_p && hpos < 0)
27048 hpos = 0;
27049 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27050 hpos = cursor_row->used[TEXT_AREA] - 1;
27051
27052 /* If the cursor is in the mouse face area, redisplay that when
27053 we clear the cursor. */
27054 if (! NILP (hlinfo->mouse_face_window)
27055 && coords_in_mouse_face_p (w, hpos, vpos)
27056 /* Don't redraw the cursor's spot in mouse face if it is at the
27057 end of a line (on a newline). The cursor appears there, but
27058 mouse highlighting does not. */
27059 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27060 mouse_face_here_p = 1;
27061
27062 /* Maybe clear the display under the cursor. */
27063 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27064 {
27065 int x, y, left_x;
27066 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27067 int width;
27068
27069 cursor_glyph = get_phys_cursor_glyph (w);
27070 if (cursor_glyph == NULL)
27071 goto mark_cursor_off;
27072
27073 width = cursor_glyph->pixel_width;
27074 left_x = window_box_left_offset (w, TEXT_AREA);
27075 x = w->phys_cursor.x;
27076 if (x < left_x)
27077 width -= left_x - x;
27078 width = min (width, window_box_width (w, TEXT_AREA) - x);
27079 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27080 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27081
27082 if (width > 0)
27083 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27084 }
27085
27086 /* Erase the cursor by redrawing the character underneath it. */
27087 if (mouse_face_here_p)
27088 hl = DRAW_MOUSE_FACE;
27089 else
27090 hl = DRAW_NORMAL_TEXT;
27091 draw_phys_cursor_glyph (w, cursor_row, hl);
27092
27093 mark_cursor_off:
27094 w->phys_cursor_on_p = 0;
27095 w->phys_cursor_type = NO_CURSOR;
27096 }
27097
27098
27099 /* EXPORT:
27100 Display or clear cursor of window W. If ON is zero, clear the
27101 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27102 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27103
27104 void
27105 display_and_set_cursor (struct window *w, bool on,
27106 int hpos, int vpos, int x, int y)
27107 {
27108 struct frame *f = XFRAME (w->frame);
27109 int new_cursor_type;
27110 int new_cursor_width;
27111 int active_cursor;
27112 struct glyph_row *glyph_row;
27113 struct glyph *glyph;
27114
27115 /* This is pointless on invisible frames, and dangerous on garbaged
27116 windows and frames; in the latter case, the frame or window may
27117 be in the midst of changing its size, and x and y may be off the
27118 window. */
27119 if (! FRAME_VISIBLE_P (f)
27120 || FRAME_GARBAGED_P (f)
27121 || vpos >= w->current_matrix->nrows
27122 || hpos >= w->current_matrix->matrix_w)
27123 return;
27124
27125 /* If cursor is off and we want it off, return quickly. */
27126 if (!on && !w->phys_cursor_on_p)
27127 return;
27128
27129 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27130 /* If cursor row is not enabled, we don't really know where to
27131 display the cursor. */
27132 if (!glyph_row->enabled_p)
27133 {
27134 w->phys_cursor_on_p = 0;
27135 return;
27136 }
27137
27138 glyph = NULL;
27139 if (!glyph_row->exact_window_width_line_p
27140 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27141 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27142
27143 eassert (input_blocked_p ());
27144
27145 /* Set new_cursor_type to the cursor we want to be displayed. */
27146 new_cursor_type = get_window_cursor_type (w, glyph,
27147 &new_cursor_width, &active_cursor);
27148
27149 /* If cursor is currently being shown and we don't want it to be or
27150 it is in the wrong place, or the cursor type is not what we want,
27151 erase it. */
27152 if (w->phys_cursor_on_p
27153 && (!on
27154 || w->phys_cursor.x != x
27155 || w->phys_cursor.y != y
27156 || new_cursor_type != w->phys_cursor_type
27157 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27158 && new_cursor_width != w->phys_cursor_width)))
27159 erase_phys_cursor (w);
27160
27161 /* Don't check phys_cursor_on_p here because that flag is only set
27162 to zero in some cases where we know that the cursor has been
27163 completely erased, to avoid the extra work of erasing the cursor
27164 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27165 still not be visible, or it has only been partly erased. */
27166 if (on)
27167 {
27168 w->phys_cursor_ascent = glyph_row->ascent;
27169 w->phys_cursor_height = glyph_row->height;
27170
27171 /* Set phys_cursor_.* before x_draw_.* is called because some
27172 of them may need the information. */
27173 w->phys_cursor.x = x;
27174 w->phys_cursor.y = glyph_row->y;
27175 w->phys_cursor.hpos = hpos;
27176 w->phys_cursor.vpos = vpos;
27177 }
27178
27179 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27180 new_cursor_type, new_cursor_width,
27181 on, active_cursor);
27182 }
27183
27184
27185 /* Switch the display of W's cursor on or off, according to the value
27186 of ON. */
27187
27188 static void
27189 update_window_cursor (struct window *w, bool on)
27190 {
27191 /* Don't update cursor in windows whose frame is in the process
27192 of being deleted. */
27193 if (w->current_matrix)
27194 {
27195 int hpos = w->phys_cursor.hpos;
27196 int vpos = w->phys_cursor.vpos;
27197 struct glyph_row *row;
27198
27199 if (vpos >= w->current_matrix->nrows
27200 || hpos >= w->current_matrix->matrix_w)
27201 return;
27202
27203 row = MATRIX_ROW (w->current_matrix, vpos);
27204
27205 /* When the window is hscrolled, cursor hpos can legitimately be
27206 out of bounds, but we draw the cursor at the corresponding
27207 window margin in that case. */
27208 if (!row->reversed_p && hpos < 0)
27209 hpos = 0;
27210 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27211 hpos = row->used[TEXT_AREA] - 1;
27212
27213 block_input ();
27214 display_and_set_cursor (w, on, hpos, vpos,
27215 w->phys_cursor.x, w->phys_cursor.y);
27216 unblock_input ();
27217 }
27218 }
27219
27220
27221 /* Call update_window_cursor with parameter ON_P on all leaf windows
27222 in the window tree rooted at W. */
27223
27224 static void
27225 update_cursor_in_window_tree (struct window *w, bool on_p)
27226 {
27227 while (w)
27228 {
27229 if (WINDOWP (w->contents))
27230 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27231 else
27232 update_window_cursor (w, on_p);
27233
27234 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27235 }
27236 }
27237
27238
27239 /* EXPORT:
27240 Display the cursor on window W, or clear it, according to ON_P.
27241 Don't change the cursor's position. */
27242
27243 void
27244 x_update_cursor (struct frame *f, bool on_p)
27245 {
27246 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27247 }
27248
27249
27250 /* EXPORT:
27251 Clear the cursor of window W to background color, and mark the
27252 cursor as not shown. This is used when the text where the cursor
27253 is about to be rewritten. */
27254
27255 void
27256 x_clear_cursor (struct window *w)
27257 {
27258 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27259 update_window_cursor (w, 0);
27260 }
27261
27262 #endif /* HAVE_WINDOW_SYSTEM */
27263
27264 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27265 and MSDOS. */
27266 static void
27267 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27268 int start_hpos, int end_hpos,
27269 enum draw_glyphs_face draw)
27270 {
27271 #ifdef HAVE_WINDOW_SYSTEM
27272 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27273 {
27274 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27275 return;
27276 }
27277 #endif
27278 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27279 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27280 #endif
27281 }
27282
27283 /* Display the active region described by mouse_face_* according to DRAW. */
27284
27285 static void
27286 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27287 {
27288 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27289 struct frame *f = XFRAME (WINDOW_FRAME (w));
27290
27291 if (/* If window is in the process of being destroyed, don't bother
27292 to do anything. */
27293 w->current_matrix != NULL
27294 /* Don't update mouse highlight if hidden. */
27295 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27296 /* Recognize when we are called to operate on rows that don't exist
27297 anymore. This can happen when a window is split. */
27298 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27299 {
27300 int phys_cursor_on_p = w->phys_cursor_on_p;
27301 struct glyph_row *row, *first, *last;
27302
27303 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27304 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27305
27306 for (row = first; row <= last && row->enabled_p; ++row)
27307 {
27308 int start_hpos, end_hpos, start_x;
27309
27310 /* For all but the first row, the highlight starts at column 0. */
27311 if (row == first)
27312 {
27313 /* R2L rows have BEG and END in reversed order, but the
27314 screen drawing geometry is always left to right. So
27315 we need to mirror the beginning and end of the
27316 highlighted area in R2L rows. */
27317 if (!row->reversed_p)
27318 {
27319 start_hpos = hlinfo->mouse_face_beg_col;
27320 start_x = hlinfo->mouse_face_beg_x;
27321 }
27322 else if (row == last)
27323 {
27324 start_hpos = hlinfo->mouse_face_end_col;
27325 start_x = hlinfo->mouse_face_end_x;
27326 }
27327 else
27328 {
27329 start_hpos = 0;
27330 start_x = 0;
27331 }
27332 }
27333 else if (row->reversed_p && row == last)
27334 {
27335 start_hpos = hlinfo->mouse_face_end_col;
27336 start_x = hlinfo->mouse_face_end_x;
27337 }
27338 else
27339 {
27340 start_hpos = 0;
27341 start_x = 0;
27342 }
27343
27344 if (row == last)
27345 {
27346 if (!row->reversed_p)
27347 end_hpos = hlinfo->mouse_face_end_col;
27348 else if (row == first)
27349 end_hpos = hlinfo->mouse_face_beg_col;
27350 else
27351 {
27352 end_hpos = row->used[TEXT_AREA];
27353 if (draw == DRAW_NORMAL_TEXT)
27354 row->fill_line_p = 1; /* Clear to end of line */
27355 }
27356 }
27357 else if (row->reversed_p && row == first)
27358 end_hpos = hlinfo->mouse_face_beg_col;
27359 else
27360 {
27361 end_hpos = row->used[TEXT_AREA];
27362 if (draw == DRAW_NORMAL_TEXT)
27363 row->fill_line_p = 1; /* Clear to end of line */
27364 }
27365
27366 if (end_hpos > start_hpos)
27367 {
27368 draw_row_with_mouse_face (w, start_x, row,
27369 start_hpos, end_hpos, draw);
27370
27371 row->mouse_face_p
27372 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27373 }
27374 }
27375
27376 #ifdef HAVE_WINDOW_SYSTEM
27377 /* When we've written over the cursor, arrange for it to
27378 be displayed again. */
27379 if (FRAME_WINDOW_P (f)
27380 && phys_cursor_on_p && !w->phys_cursor_on_p)
27381 {
27382 int hpos = w->phys_cursor.hpos;
27383
27384 /* When the window is hscrolled, cursor hpos can legitimately be
27385 out of bounds, but we draw the cursor at the corresponding
27386 window margin in that case. */
27387 if (!row->reversed_p && hpos < 0)
27388 hpos = 0;
27389 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27390 hpos = row->used[TEXT_AREA] - 1;
27391
27392 block_input ();
27393 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27394 w->phys_cursor.x, w->phys_cursor.y);
27395 unblock_input ();
27396 }
27397 #endif /* HAVE_WINDOW_SYSTEM */
27398 }
27399
27400 #ifdef HAVE_WINDOW_SYSTEM
27401 /* Change the mouse cursor. */
27402 if (FRAME_WINDOW_P (f))
27403 {
27404 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27405 if (draw == DRAW_NORMAL_TEXT
27406 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27407 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27408 else
27409 #endif
27410 if (draw == DRAW_MOUSE_FACE)
27411 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27412 else
27413 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27414 }
27415 #endif /* HAVE_WINDOW_SYSTEM */
27416 }
27417
27418 /* EXPORT:
27419 Clear out the mouse-highlighted active region.
27420 Redraw it un-highlighted first. Value is non-zero if mouse
27421 face was actually drawn unhighlighted. */
27422
27423 int
27424 clear_mouse_face (Mouse_HLInfo *hlinfo)
27425 {
27426 int cleared = 0;
27427
27428 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27429 {
27430 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27431 cleared = 1;
27432 }
27433
27434 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27435 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27436 hlinfo->mouse_face_window = Qnil;
27437 hlinfo->mouse_face_overlay = Qnil;
27438 return cleared;
27439 }
27440
27441 /* Return true if the coordinates HPOS and VPOS on windows W are
27442 within the mouse face on that window. */
27443 static bool
27444 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27445 {
27446 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27447
27448 /* Quickly resolve the easy cases. */
27449 if (!(WINDOWP (hlinfo->mouse_face_window)
27450 && XWINDOW (hlinfo->mouse_face_window) == w))
27451 return false;
27452 if (vpos < hlinfo->mouse_face_beg_row
27453 || vpos > hlinfo->mouse_face_end_row)
27454 return false;
27455 if (vpos > hlinfo->mouse_face_beg_row
27456 && vpos < hlinfo->mouse_face_end_row)
27457 return true;
27458
27459 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27460 {
27461 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27462 {
27463 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27464 return true;
27465 }
27466 else if ((vpos == hlinfo->mouse_face_beg_row
27467 && hpos >= hlinfo->mouse_face_beg_col)
27468 || (vpos == hlinfo->mouse_face_end_row
27469 && hpos < hlinfo->mouse_face_end_col))
27470 return true;
27471 }
27472 else
27473 {
27474 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27475 {
27476 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27477 return true;
27478 }
27479 else if ((vpos == hlinfo->mouse_face_beg_row
27480 && hpos <= hlinfo->mouse_face_beg_col)
27481 || (vpos == hlinfo->mouse_face_end_row
27482 && hpos > hlinfo->mouse_face_end_col))
27483 return true;
27484 }
27485 return false;
27486 }
27487
27488
27489 /* EXPORT:
27490 True if physical cursor of window W is within mouse face. */
27491
27492 bool
27493 cursor_in_mouse_face_p (struct window *w)
27494 {
27495 int hpos = w->phys_cursor.hpos;
27496 int vpos = w->phys_cursor.vpos;
27497 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27498
27499 /* When the window is hscrolled, cursor hpos can legitimately be out
27500 of bounds, but we draw the cursor at the corresponding window
27501 margin in that case. */
27502 if (!row->reversed_p && hpos < 0)
27503 hpos = 0;
27504 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27505 hpos = row->used[TEXT_AREA] - 1;
27506
27507 return coords_in_mouse_face_p (w, hpos, vpos);
27508 }
27509
27510
27511 \f
27512 /* Find the glyph rows START_ROW and END_ROW of window W that display
27513 characters between buffer positions START_CHARPOS and END_CHARPOS
27514 (excluding END_CHARPOS). DISP_STRING is a display string that
27515 covers these buffer positions. This is similar to
27516 row_containing_pos, but is more accurate when bidi reordering makes
27517 buffer positions change non-linearly with glyph rows. */
27518 static void
27519 rows_from_pos_range (struct window *w,
27520 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27521 Lisp_Object disp_string,
27522 struct glyph_row **start, struct glyph_row **end)
27523 {
27524 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27525 int last_y = window_text_bottom_y (w);
27526 struct glyph_row *row;
27527
27528 *start = NULL;
27529 *end = NULL;
27530
27531 while (!first->enabled_p
27532 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27533 first++;
27534
27535 /* Find the START row. */
27536 for (row = first;
27537 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27538 row++)
27539 {
27540 /* A row can potentially be the START row if the range of the
27541 characters it displays intersects the range
27542 [START_CHARPOS..END_CHARPOS). */
27543 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27544 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27545 /* See the commentary in row_containing_pos, for the
27546 explanation of the complicated way to check whether
27547 some position is beyond the end of the characters
27548 displayed by a row. */
27549 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27550 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27551 && !row->ends_at_zv_p
27552 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27553 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27554 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27555 && !row->ends_at_zv_p
27556 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27557 {
27558 /* Found a candidate row. Now make sure at least one of the
27559 glyphs it displays has a charpos from the range
27560 [START_CHARPOS..END_CHARPOS).
27561
27562 This is not obvious because bidi reordering could make
27563 buffer positions of a row be 1,2,3,102,101,100, and if we
27564 want to highlight characters in [50..60), we don't want
27565 this row, even though [50..60) does intersect [1..103),
27566 the range of character positions given by the row's start
27567 and end positions. */
27568 struct glyph *g = row->glyphs[TEXT_AREA];
27569 struct glyph *e = g + row->used[TEXT_AREA];
27570
27571 while (g < e)
27572 {
27573 if (((BUFFERP (g->object) || INTEGERP (g->object))
27574 && start_charpos <= g->charpos && g->charpos < end_charpos)
27575 /* A glyph that comes from DISP_STRING is by
27576 definition to be highlighted. */
27577 || EQ (g->object, disp_string))
27578 *start = row;
27579 g++;
27580 }
27581 if (*start)
27582 break;
27583 }
27584 }
27585
27586 /* Find the END row. */
27587 if (!*start
27588 /* If the last row is partially visible, start looking for END
27589 from that row, instead of starting from FIRST. */
27590 && !(row->enabled_p
27591 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27592 row = first;
27593 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27594 {
27595 struct glyph_row *next = row + 1;
27596 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27597
27598 if (!next->enabled_p
27599 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27600 /* The first row >= START whose range of displayed characters
27601 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27602 is the row END + 1. */
27603 || (start_charpos < next_start
27604 && end_charpos < next_start)
27605 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27606 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27607 && !next->ends_at_zv_p
27608 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27609 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27610 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27611 && !next->ends_at_zv_p
27612 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27613 {
27614 *end = row;
27615 break;
27616 }
27617 else
27618 {
27619 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27620 but none of the characters it displays are in the range, it is
27621 also END + 1. */
27622 struct glyph *g = next->glyphs[TEXT_AREA];
27623 struct glyph *s = g;
27624 struct glyph *e = g + next->used[TEXT_AREA];
27625
27626 while (g < e)
27627 {
27628 if (((BUFFERP (g->object) || INTEGERP (g->object))
27629 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27630 /* If the buffer position of the first glyph in
27631 the row is equal to END_CHARPOS, it means
27632 the last character to be highlighted is the
27633 newline of ROW, and we must consider NEXT as
27634 END, not END+1. */
27635 || (((!next->reversed_p && g == s)
27636 || (next->reversed_p && g == e - 1))
27637 && (g->charpos == end_charpos
27638 /* Special case for when NEXT is an
27639 empty line at ZV. */
27640 || (g->charpos == -1
27641 && !row->ends_at_zv_p
27642 && next_start == end_charpos)))))
27643 /* A glyph that comes from DISP_STRING is by
27644 definition to be highlighted. */
27645 || EQ (g->object, disp_string))
27646 break;
27647 g++;
27648 }
27649 if (g == e)
27650 {
27651 *end = row;
27652 break;
27653 }
27654 /* The first row that ends at ZV must be the last to be
27655 highlighted. */
27656 else if (next->ends_at_zv_p)
27657 {
27658 *end = next;
27659 break;
27660 }
27661 }
27662 }
27663 }
27664
27665 /* This function sets the mouse_face_* elements of HLINFO, assuming
27666 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27667 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27668 for the overlay or run of text properties specifying the mouse
27669 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27670 before-string and after-string that must also be highlighted.
27671 DISP_STRING, if non-nil, is a display string that may cover some
27672 or all of the highlighted text. */
27673
27674 static void
27675 mouse_face_from_buffer_pos (Lisp_Object window,
27676 Mouse_HLInfo *hlinfo,
27677 ptrdiff_t mouse_charpos,
27678 ptrdiff_t start_charpos,
27679 ptrdiff_t end_charpos,
27680 Lisp_Object before_string,
27681 Lisp_Object after_string,
27682 Lisp_Object disp_string)
27683 {
27684 struct window *w = XWINDOW (window);
27685 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27686 struct glyph_row *r1, *r2;
27687 struct glyph *glyph, *end;
27688 ptrdiff_t ignore, pos;
27689 int x;
27690
27691 eassert (NILP (disp_string) || STRINGP (disp_string));
27692 eassert (NILP (before_string) || STRINGP (before_string));
27693 eassert (NILP (after_string) || STRINGP (after_string));
27694
27695 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27696 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27697 if (r1 == NULL)
27698 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27699 /* If the before-string or display-string contains newlines,
27700 rows_from_pos_range skips to its last row. Move back. */
27701 if (!NILP (before_string) || !NILP (disp_string))
27702 {
27703 struct glyph_row *prev;
27704 while ((prev = r1 - 1, prev >= first)
27705 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27706 && prev->used[TEXT_AREA] > 0)
27707 {
27708 struct glyph *beg = prev->glyphs[TEXT_AREA];
27709 glyph = beg + prev->used[TEXT_AREA];
27710 while (--glyph >= beg && INTEGERP (glyph->object));
27711 if (glyph < beg
27712 || !(EQ (glyph->object, before_string)
27713 || EQ (glyph->object, disp_string)))
27714 break;
27715 r1 = prev;
27716 }
27717 }
27718 if (r2 == NULL)
27719 {
27720 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27721 hlinfo->mouse_face_past_end = 1;
27722 }
27723 else if (!NILP (after_string))
27724 {
27725 /* If the after-string has newlines, advance to its last row. */
27726 struct glyph_row *next;
27727 struct glyph_row *last
27728 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27729
27730 for (next = r2 + 1;
27731 next <= last
27732 && next->used[TEXT_AREA] > 0
27733 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27734 ++next)
27735 r2 = next;
27736 }
27737 /* The rest of the display engine assumes that mouse_face_beg_row is
27738 either above mouse_face_end_row or identical to it. But with
27739 bidi-reordered continued lines, the row for START_CHARPOS could
27740 be below the row for END_CHARPOS. If so, swap the rows and store
27741 them in correct order. */
27742 if (r1->y > r2->y)
27743 {
27744 struct glyph_row *tem = r2;
27745
27746 r2 = r1;
27747 r1 = tem;
27748 }
27749
27750 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27751 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27752
27753 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27754 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27755 could be anywhere in the row and in any order. The strategy
27756 below is to find the leftmost and the rightmost glyph that
27757 belongs to either of these 3 strings, or whose position is
27758 between START_CHARPOS and END_CHARPOS, and highlight all the
27759 glyphs between those two. This may cover more than just the text
27760 between START_CHARPOS and END_CHARPOS if the range of characters
27761 strides the bidi level boundary, e.g. if the beginning is in R2L
27762 text while the end is in L2R text or vice versa. */
27763 if (!r1->reversed_p)
27764 {
27765 /* This row is in a left to right paragraph. Scan it left to
27766 right. */
27767 glyph = r1->glyphs[TEXT_AREA];
27768 end = glyph + r1->used[TEXT_AREA];
27769 x = r1->x;
27770
27771 /* Skip truncation glyphs at the start of the glyph row. */
27772 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27773 for (; glyph < end
27774 && INTEGERP (glyph->object)
27775 && glyph->charpos < 0;
27776 ++glyph)
27777 x += glyph->pixel_width;
27778
27779 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27780 or DISP_STRING, and the first glyph from buffer whose
27781 position is between START_CHARPOS and END_CHARPOS. */
27782 for (; glyph < end
27783 && !INTEGERP (glyph->object)
27784 && !EQ (glyph->object, disp_string)
27785 && !(BUFFERP (glyph->object)
27786 && (glyph->charpos >= start_charpos
27787 && glyph->charpos < end_charpos));
27788 ++glyph)
27789 {
27790 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27791 are present at buffer positions between START_CHARPOS and
27792 END_CHARPOS, or if they come from an overlay. */
27793 if (EQ (glyph->object, before_string))
27794 {
27795 pos = string_buffer_position (before_string,
27796 start_charpos);
27797 /* If pos == 0, it means before_string came from an
27798 overlay, not from a buffer position. */
27799 if (!pos || (pos >= start_charpos && pos < end_charpos))
27800 break;
27801 }
27802 else if (EQ (glyph->object, after_string))
27803 {
27804 pos = string_buffer_position (after_string, end_charpos);
27805 if (!pos || (pos >= start_charpos && pos < end_charpos))
27806 break;
27807 }
27808 x += glyph->pixel_width;
27809 }
27810 hlinfo->mouse_face_beg_x = x;
27811 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27812 }
27813 else
27814 {
27815 /* This row is in a right to left paragraph. Scan it right to
27816 left. */
27817 struct glyph *g;
27818
27819 end = r1->glyphs[TEXT_AREA] - 1;
27820 glyph = end + r1->used[TEXT_AREA];
27821
27822 /* Skip truncation glyphs at the start of the glyph row. */
27823 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27824 for (; glyph > end
27825 && INTEGERP (glyph->object)
27826 && glyph->charpos < 0;
27827 --glyph)
27828 ;
27829
27830 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27831 or DISP_STRING, and the first glyph from buffer whose
27832 position is between START_CHARPOS and END_CHARPOS. */
27833 for (; glyph > end
27834 && !INTEGERP (glyph->object)
27835 && !EQ (glyph->object, disp_string)
27836 && !(BUFFERP (glyph->object)
27837 && (glyph->charpos >= start_charpos
27838 && glyph->charpos < end_charpos));
27839 --glyph)
27840 {
27841 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27842 are present at buffer positions between START_CHARPOS and
27843 END_CHARPOS, or if they come from an overlay. */
27844 if (EQ (glyph->object, before_string))
27845 {
27846 pos = string_buffer_position (before_string, start_charpos);
27847 /* If pos == 0, it means before_string came from an
27848 overlay, not from a buffer position. */
27849 if (!pos || (pos >= start_charpos && pos < end_charpos))
27850 break;
27851 }
27852 else if (EQ (glyph->object, after_string))
27853 {
27854 pos = string_buffer_position (after_string, end_charpos);
27855 if (!pos || (pos >= start_charpos && pos < end_charpos))
27856 break;
27857 }
27858 }
27859
27860 glyph++; /* first glyph to the right of the highlighted area */
27861 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27862 x += g->pixel_width;
27863 hlinfo->mouse_face_beg_x = x;
27864 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27865 }
27866
27867 /* If the highlight ends in a different row, compute GLYPH and END
27868 for the end row. Otherwise, reuse the values computed above for
27869 the row where the highlight begins. */
27870 if (r2 != r1)
27871 {
27872 if (!r2->reversed_p)
27873 {
27874 glyph = r2->glyphs[TEXT_AREA];
27875 end = glyph + r2->used[TEXT_AREA];
27876 x = r2->x;
27877 }
27878 else
27879 {
27880 end = r2->glyphs[TEXT_AREA] - 1;
27881 glyph = end + r2->used[TEXT_AREA];
27882 }
27883 }
27884
27885 if (!r2->reversed_p)
27886 {
27887 /* Skip truncation and continuation glyphs near the end of the
27888 row, and also blanks and stretch glyphs inserted by
27889 extend_face_to_end_of_line. */
27890 while (end > glyph
27891 && INTEGERP ((end - 1)->object))
27892 --end;
27893 /* Scan the rest of the glyph row from the end, looking for the
27894 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27895 DISP_STRING, or whose position is between START_CHARPOS
27896 and END_CHARPOS */
27897 for (--end;
27898 end > glyph
27899 && !INTEGERP (end->object)
27900 && !EQ (end->object, disp_string)
27901 && !(BUFFERP (end->object)
27902 && (end->charpos >= start_charpos
27903 && end->charpos < end_charpos));
27904 --end)
27905 {
27906 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27907 are present at buffer positions between START_CHARPOS and
27908 END_CHARPOS, or if they come from an overlay. */
27909 if (EQ (end->object, before_string))
27910 {
27911 pos = string_buffer_position (before_string, start_charpos);
27912 if (!pos || (pos >= start_charpos && pos < end_charpos))
27913 break;
27914 }
27915 else if (EQ (end->object, after_string))
27916 {
27917 pos = string_buffer_position (after_string, end_charpos);
27918 if (!pos || (pos >= start_charpos && pos < end_charpos))
27919 break;
27920 }
27921 }
27922 /* Find the X coordinate of the last glyph to be highlighted. */
27923 for (; glyph <= end; ++glyph)
27924 x += glyph->pixel_width;
27925
27926 hlinfo->mouse_face_end_x = x;
27927 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27928 }
27929 else
27930 {
27931 /* Skip truncation and continuation glyphs near the end of the
27932 row, and also blanks and stretch glyphs inserted by
27933 extend_face_to_end_of_line. */
27934 x = r2->x;
27935 end++;
27936 while (end < glyph
27937 && INTEGERP (end->object))
27938 {
27939 x += end->pixel_width;
27940 ++end;
27941 }
27942 /* Scan the rest of the glyph row from the end, looking for the
27943 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27944 DISP_STRING, or whose position is between START_CHARPOS
27945 and END_CHARPOS */
27946 for ( ;
27947 end < glyph
27948 && !INTEGERP (end->object)
27949 && !EQ (end->object, disp_string)
27950 && !(BUFFERP (end->object)
27951 && (end->charpos >= start_charpos
27952 && end->charpos < end_charpos));
27953 ++end)
27954 {
27955 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27956 are present at buffer positions between START_CHARPOS and
27957 END_CHARPOS, or if they come from an overlay. */
27958 if (EQ (end->object, before_string))
27959 {
27960 pos = string_buffer_position (before_string, start_charpos);
27961 if (!pos || (pos >= start_charpos && pos < end_charpos))
27962 break;
27963 }
27964 else if (EQ (end->object, after_string))
27965 {
27966 pos = string_buffer_position (after_string, end_charpos);
27967 if (!pos || (pos >= start_charpos && pos < end_charpos))
27968 break;
27969 }
27970 x += end->pixel_width;
27971 }
27972 /* If we exited the above loop because we arrived at the last
27973 glyph of the row, and its buffer position is still not in
27974 range, it means the last character in range is the preceding
27975 newline. Bump the end column and x values to get past the
27976 last glyph. */
27977 if (end == glyph
27978 && BUFFERP (end->object)
27979 && (end->charpos < start_charpos
27980 || end->charpos >= end_charpos))
27981 {
27982 x += end->pixel_width;
27983 ++end;
27984 }
27985 hlinfo->mouse_face_end_x = x;
27986 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27987 }
27988
27989 hlinfo->mouse_face_window = window;
27990 hlinfo->mouse_face_face_id
27991 = face_at_buffer_position (w, mouse_charpos, &ignore,
27992 mouse_charpos + 1,
27993 !hlinfo->mouse_face_hidden, -1);
27994 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27995 }
27996
27997 /* The following function is not used anymore (replaced with
27998 mouse_face_from_string_pos), but I leave it here for the time
27999 being, in case someone would. */
28000
28001 #if 0 /* not used */
28002
28003 /* Find the position of the glyph for position POS in OBJECT in
28004 window W's current matrix, and return in *X, *Y the pixel
28005 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28006
28007 RIGHT_P non-zero means return the position of the right edge of the
28008 glyph, RIGHT_P zero means return the left edge position.
28009
28010 If no glyph for POS exists in the matrix, return the position of
28011 the glyph with the next smaller position that is in the matrix, if
28012 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28013 exists in the matrix, return the position of the glyph with the
28014 next larger position in OBJECT.
28015
28016 Value is non-zero if a glyph was found. */
28017
28018 static int
28019 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28020 int *hpos, int *vpos, int *x, int *y, int right_p)
28021 {
28022 int yb = window_text_bottom_y (w);
28023 struct glyph_row *r;
28024 struct glyph *best_glyph = NULL;
28025 struct glyph_row *best_row = NULL;
28026 int best_x = 0;
28027
28028 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28029 r->enabled_p && r->y < yb;
28030 ++r)
28031 {
28032 struct glyph *g = r->glyphs[TEXT_AREA];
28033 struct glyph *e = g + r->used[TEXT_AREA];
28034 int gx;
28035
28036 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28037 if (EQ (g->object, object))
28038 {
28039 if (g->charpos == pos)
28040 {
28041 best_glyph = g;
28042 best_x = gx;
28043 best_row = r;
28044 goto found;
28045 }
28046 else if (best_glyph == NULL
28047 || ((eabs (g->charpos - pos)
28048 < eabs (best_glyph->charpos - pos))
28049 && (right_p
28050 ? g->charpos < pos
28051 : g->charpos > pos)))
28052 {
28053 best_glyph = g;
28054 best_x = gx;
28055 best_row = r;
28056 }
28057 }
28058 }
28059
28060 found:
28061
28062 if (best_glyph)
28063 {
28064 *x = best_x;
28065 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28066
28067 if (right_p)
28068 {
28069 *x += best_glyph->pixel_width;
28070 ++*hpos;
28071 }
28072
28073 *y = best_row->y;
28074 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28075 }
28076
28077 return best_glyph != NULL;
28078 }
28079 #endif /* not used */
28080
28081 /* Find the positions of the first and the last glyphs in window W's
28082 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28083 (assumed to be a string), and return in HLINFO's mouse_face_*
28084 members the pixel and column/row coordinates of those glyphs. */
28085
28086 static void
28087 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28088 Lisp_Object object,
28089 ptrdiff_t startpos, ptrdiff_t endpos)
28090 {
28091 int yb = window_text_bottom_y (w);
28092 struct glyph_row *r;
28093 struct glyph *g, *e;
28094 int gx;
28095 int found = 0;
28096
28097 /* Find the glyph row with at least one position in the range
28098 [STARTPOS..ENDPOS), and the first glyph in that row whose
28099 position belongs to that range. */
28100 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28101 r->enabled_p && r->y < yb;
28102 ++r)
28103 {
28104 if (!r->reversed_p)
28105 {
28106 g = r->glyphs[TEXT_AREA];
28107 e = g + r->used[TEXT_AREA];
28108 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28109 if (EQ (g->object, object)
28110 && startpos <= g->charpos && g->charpos < endpos)
28111 {
28112 hlinfo->mouse_face_beg_row
28113 = MATRIX_ROW_VPOS (r, w->current_matrix);
28114 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28115 hlinfo->mouse_face_beg_x = gx;
28116 found = 1;
28117 break;
28118 }
28119 }
28120 else
28121 {
28122 struct glyph *g1;
28123
28124 e = r->glyphs[TEXT_AREA];
28125 g = e + r->used[TEXT_AREA];
28126 for ( ; g > e; --g)
28127 if (EQ ((g-1)->object, object)
28128 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28129 {
28130 hlinfo->mouse_face_beg_row
28131 = MATRIX_ROW_VPOS (r, w->current_matrix);
28132 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28133 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28134 gx += g1->pixel_width;
28135 hlinfo->mouse_face_beg_x = gx;
28136 found = 1;
28137 break;
28138 }
28139 }
28140 if (found)
28141 break;
28142 }
28143
28144 if (!found)
28145 return;
28146
28147 /* Starting with the next row, look for the first row which does NOT
28148 include any glyphs whose positions are in the range. */
28149 for (++r; r->enabled_p && r->y < yb; ++r)
28150 {
28151 g = r->glyphs[TEXT_AREA];
28152 e = g + r->used[TEXT_AREA];
28153 found = 0;
28154 for ( ; g < e; ++g)
28155 if (EQ (g->object, object)
28156 && startpos <= g->charpos && g->charpos < endpos)
28157 {
28158 found = 1;
28159 break;
28160 }
28161 if (!found)
28162 break;
28163 }
28164
28165 /* The highlighted region ends on the previous row. */
28166 r--;
28167
28168 /* Set the end row. */
28169 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28170
28171 /* Compute and set the end column and the end column's horizontal
28172 pixel coordinate. */
28173 if (!r->reversed_p)
28174 {
28175 g = r->glyphs[TEXT_AREA];
28176 e = g + r->used[TEXT_AREA];
28177 for ( ; e > g; --e)
28178 if (EQ ((e-1)->object, object)
28179 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28180 break;
28181 hlinfo->mouse_face_end_col = e - g;
28182
28183 for (gx = r->x; g < e; ++g)
28184 gx += g->pixel_width;
28185 hlinfo->mouse_face_end_x = gx;
28186 }
28187 else
28188 {
28189 e = r->glyphs[TEXT_AREA];
28190 g = e + r->used[TEXT_AREA];
28191 for (gx = r->x ; e < g; ++e)
28192 {
28193 if (EQ (e->object, object)
28194 && startpos <= e->charpos && e->charpos < endpos)
28195 break;
28196 gx += e->pixel_width;
28197 }
28198 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28199 hlinfo->mouse_face_end_x = gx;
28200 }
28201 }
28202
28203 #ifdef HAVE_WINDOW_SYSTEM
28204
28205 /* See if position X, Y is within a hot-spot of an image. */
28206
28207 static int
28208 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28209 {
28210 if (!CONSP (hot_spot))
28211 return 0;
28212
28213 if (EQ (XCAR (hot_spot), Qrect))
28214 {
28215 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28216 Lisp_Object rect = XCDR (hot_spot);
28217 Lisp_Object tem;
28218 if (!CONSP (rect))
28219 return 0;
28220 if (!CONSP (XCAR (rect)))
28221 return 0;
28222 if (!CONSP (XCDR (rect)))
28223 return 0;
28224 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28225 return 0;
28226 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28227 return 0;
28228 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28229 return 0;
28230 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28231 return 0;
28232 return 1;
28233 }
28234 else if (EQ (XCAR (hot_spot), Qcircle))
28235 {
28236 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28237 Lisp_Object circ = XCDR (hot_spot);
28238 Lisp_Object lr, lx0, ly0;
28239 if (CONSP (circ)
28240 && CONSP (XCAR (circ))
28241 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28242 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28243 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28244 {
28245 double r = XFLOATINT (lr);
28246 double dx = XINT (lx0) - x;
28247 double dy = XINT (ly0) - y;
28248 return (dx * dx + dy * dy <= r * r);
28249 }
28250 }
28251 else if (EQ (XCAR (hot_spot), Qpoly))
28252 {
28253 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28254 if (VECTORP (XCDR (hot_spot)))
28255 {
28256 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28257 Lisp_Object *poly = v->contents;
28258 ptrdiff_t n = v->header.size;
28259 ptrdiff_t i;
28260 int inside = 0;
28261 Lisp_Object lx, ly;
28262 int x0, y0;
28263
28264 /* Need an even number of coordinates, and at least 3 edges. */
28265 if (n < 6 || n & 1)
28266 return 0;
28267
28268 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28269 If count is odd, we are inside polygon. Pixels on edges
28270 may or may not be included depending on actual geometry of the
28271 polygon. */
28272 if ((lx = poly[n-2], !INTEGERP (lx))
28273 || (ly = poly[n-1], !INTEGERP (lx)))
28274 return 0;
28275 x0 = XINT (lx), y0 = XINT (ly);
28276 for (i = 0; i < n; i += 2)
28277 {
28278 int x1 = x0, y1 = y0;
28279 if ((lx = poly[i], !INTEGERP (lx))
28280 || (ly = poly[i+1], !INTEGERP (ly)))
28281 return 0;
28282 x0 = XINT (lx), y0 = XINT (ly);
28283
28284 /* Does this segment cross the X line? */
28285 if (x0 >= x)
28286 {
28287 if (x1 >= x)
28288 continue;
28289 }
28290 else if (x1 < x)
28291 continue;
28292 if (y > y0 && y > y1)
28293 continue;
28294 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28295 inside = !inside;
28296 }
28297 return inside;
28298 }
28299 }
28300 return 0;
28301 }
28302
28303 Lisp_Object
28304 find_hot_spot (Lisp_Object map, int x, int y)
28305 {
28306 while (CONSP (map))
28307 {
28308 if (CONSP (XCAR (map))
28309 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28310 return XCAR (map);
28311 map = XCDR (map);
28312 }
28313
28314 return Qnil;
28315 }
28316
28317 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28318 3, 3, 0,
28319 doc: /* Lookup in image map MAP coordinates X and Y.
28320 An image map is an alist where each element has the format (AREA ID PLIST).
28321 An AREA is specified as either a rectangle, a circle, or a polygon:
28322 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28323 pixel coordinates of the upper left and bottom right corners.
28324 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28325 and the radius of the circle; r may be a float or integer.
28326 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28327 vector describes one corner in the polygon.
28328 Returns the alist element for the first matching AREA in MAP. */)
28329 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28330 {
28331 if (NILP (map))
28332 return Qnil;
28333
28334 CHECK_NUMBER (x);
28335 CHECK_NUMBER (y);
28336
28337 return find_hot_spot (map,
28338 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28339 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28340 }
28341
28342
28343 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28344 static void
28345 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28346 {
28347 /* Do not change cursor shape while dragging mouse. */
28348 if (!NILP (do_mouse_tracking))
28349 return;
28350
28351 if (!NILP (pointer))
28352 {
28353 if (EQ (pointer, Qarrow))
28354 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28355 else if (EQ (pointer, Qhand))
28356 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28357 else if (EQ (pointer, Qtext))
28358 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28359 else if (EQ (pointer, intern ("hdrag")))
28360 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28361 else if (EQ (pointer, intern ("nhdrag")))
28362 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28363 #ifdef HAVE_X_WINDOWS
28364 else if (EQ (pointer, intern ("vdrag")))
28365 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28366 #endif
28367 else if (EQ (pointer, intern ("hourglass")))
28368 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28369 else if (EQ (pointer, Qmodeline))
28370 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28371 else
28372 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28373 }
28374
28375 if (cursor != No_Cursor)
28376 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28377 }
28378
28379 #endif /* HAVE_WINDOW_SYSTEM */
28380
28381 /* Take proper action when mouse has moved to the mode or header line
28382 or marginal area AREA of window W, x-position X and y-position Y.
28383 X is relative to the start of the text display area of W, so the
28384 width of bitmap areas and scroll bars must be subtracted to get a
28385 position relative to the start of the mode line. */
28386
28387 static void
28388 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28389 enum window_part area)
28390 {
28391 struct window *w = XWINDOW (window);
28392 struct frame *f = XFRAME (w->frame);
28393 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28394 #ifdef HAVE_WINDOW_SYSTEM
28395 Display_Info *dpyinfo;
28396 #endif
28397 Cursor cursor = No_Cursor;
28398 Lisp_Object pointer = Qnil;
28399 int dx, dy, width, height;
28400 ptrdiff_t charpos;
28401 Lisp_Object string, object = Qnil;
28402 Lisp_Object pos IF_LINT (= Qnil), help;
28403
28404 Lisp_Object mouse_face;
28405 int original_x_pixel = x;
28406 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28407 struct glyph_row *row IF_LINT (= 0);
28408
28409 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28410 {
28411 int x0;
28412 struct glyph *end;
28413
28414 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28415 returns them in row/column units! */
28416 string = mode_line_string (w, area, &x, &y, &charpos,
28417 &object, &dx, &dy, &width, &height);
28418
28419 row = (area == ON_MODE_LINE
28420 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28421 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28422
28423 /* Find the glyph under the mouse pointer. */
28424 if (row->mode_line_p && row->enabled_p)
28425 {
28426 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28427 end = glyph + row->used[TEXT_AREA];
28428
28429 for (x0 = original_x_pixel;
28430 glyph < end && x0 >= glyph->pixel_width;
28431 ++glyph)
28432 x0 -= glyph->pixel_width;
28433
28434 if (glyph >= end)
28435 glyph = NULL;
28436 }
28437 }
28438 else
28439 {
28440 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28441 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28442 returns them in row/column units! */
28443 string = marginal_area_string (w, area, &x, &y, &charpos,
28444 &object, &dx, &dy, &width, &height);
28445 }
28446
28447 help = Qnil;
28448
28449 #ifdef HAVE_WINDOW_SYSTEM
28450 if (IMAGEP (object))
28451 {
28452 Lisp_Object image_map, hotspot;
28453 if ((image_map = Fplist_get (XCDR (object), QCmap),
28454 !NILP (image_map))
28455 && (hotspot = find_hot_spot (image_map, dx, dy),
28456 CONSP (hotspot))
28457 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28458 {
28459 Lisp_Object plist;
28460
28461 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28462 If so, we could look for mouse-enter, mouse-leave
28463 properties in PLIST (and do something...). */
28464 hotspot = XCDR (hotspot);
28465 if (CONSP (hotspot)
28466 && (plist = XCAR (hotspot), CONSP (plist)))
28467 {
28468 pointer = Fplist_get (plist, Qpointer);
28469 if (NILP (pointer))
28470 pointer = Qhand;
28471 help = Fplist_get (plist, Qhelp_echo);
28472 if (!NILP (help))
28473 {
28474 help_echo_string = help;
28475 XSETWINDOW (help_echo_window, w);
28476 help_echo_object = w->contents;
28477 help_echo_pos = charpos;
28478 }
28479 }
28480 }
28481 if (NILP (pointer))
28482 pointer = Fplist_get (XCDR (object), QCpointer);
28483 }
28484 #endif /* HAVE_WINDOW_SYSTEM */
28485
28486 if (STRINGP (string))
28487 pos = make_number (charpos);
28488
28489 /* Set the help text and mouse pointer. If the mouse is on a part
28490 of the mode line without any text (e.g. past the right edge of
28491 the mode line text), use the default help text and pointer. */
28492 if (STRINGP (string) || area == ON_MODE_LINE)
28493 {
28494 /* Arrange to display the help by setting the global variables
28495 help_echo_string, help_echo_object, and help_echo_pos. */
28496 if (NILP (help))
28497 {
28498 if (STRINGP (string))
28499 help = Fget_text_property (pos, Qhelp_echo, string);
28500
28501 if (!NILP (help))
28502 {
28503 help_echo_string = help;
28504 XSETWINDOW (help_echo_window, w);
28505 help_echo_object = string;
28506 help_echo_pos = charpos;
28507 }
28508 else if (area == ON_MODE_LINE)
28509 {
28510 Lisp_Object default_help
28511 = buffer_local_value_1 (Qmode_line_default_help_echo,
28512 w->contents);
28513
28514 if (STRINGP (default_help))
28515 {
28516 help_echo_string = default_help;
28517 XSETWINDOW (help_echo_window, w);
28518 help_echo_object = Qnil;
28519 help_echo_pos = -1;
28520 }
28521 }
28522 }
28523
28524 #ifdef HAVE_WINDOW_SYSTEM
28525 /* Change the mouse pointer according to what is under it. */
28526 if (FRAME_WINDOW_P (f))
28527 {
28528 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28529 || minibuf_level
28530 || NILP (Vresize_mini_windows));
28531
28532 dpyinfo = FRAME_DISPLAY_INFO (f);
28533 if (STRINGP (string))
28534 {
28535 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28536
28537 if (NILP (pointer))
28538 pointer = Fget_text_property (pos, Qpointer, string);
28539
28540 /* Change the mouse pointer according to what is under X/Y. */
28541 if (NILP (pointer)
28542 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28543 {
28544 Lisp_Object map;
28545 map = Fget_text_property (pos, Qlocal_map, string);
28546 if (!KEYMAPP (map))
28547 map = Fget_text_property (pos, Qkeymap, string);
28548 if (!KEYMAPP (map) && draggable)
28549 cursor = dpyinfo->vertical_scroll_bar_cursor;
28550 }
28551 }
28552 else if (draggable)
28553 /* Default mode-line pointer. */
28554 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28555 }
28556 #endif
28557 }
28558
28559 /* Change the mouse face according to what is under X/Y. */
28560 if (STRINGP (string))
28561 {
28562 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28563 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28564 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28565 && glyph)
28566 {
28567 Lisp_Object b, e;
28568
28569 struct glyph * tmp_glyph;
28570
28571 int gpos;
28572 int gseq_length;
28573 int total_pixel_width;
28574 ptrdiff_t begpos, endpos, ignore;
28575
28576 int vpos, hpos;
28577
28578 b = Fprevious_single_property_change (make_number (charpos + 1),
28579 Qmouse_face, string, Qnil);
28580 if (NILP (b))
28581 begpos = 0;
28582 else
28583 begpos = XINT (b);
28584
28585 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28586 if (NILP (e))
28587 endpos = SCHARS (string);
28588 else
28589 endpos = XINT (e);
28590
28591 /* Calculate the glyph position GPOS of GLYPH in the
28592 displayed string, relative to the beginning of the
28593 highlighted part of the string.
28594
28595 Note: GPOS is different from CHARPOS. CHARPOS is the
28596 position of GLYPH in the internal string object. A mode
28597 line string format has structures which are converted to
28598 a flattened string by the Emacs Lisp interpreter. The
28599 internal string is an element of those structures. The
28600 displayed string is the flattened string. */
28601 tmp_glyph = row_start_glyph;
28602 while (tmp_glyph < glyph
28603 && (!(EQ (tmp_glyph->object, glyph->object)
28604 && begpos <= tmp_glyph->charpos
28605 && tmp_glyph->charpos < endpos)))
28606 tmp_glyph++;
28607 gpos = glyph - tmp_glyph;
28608
28609 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28610 the highlighted part of the displayed string to which
28611 GLYPH belongs. Note: GSEQ_LENGTH is different from
28612 SCHARS (STRING), because the latter returns the length of
28613 the internal string. */
28614 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28615 tmp_glyph > glyph
28616 && (!(EQ (tmp_glyph->object, glyph->object)
28617 && begpos <= tmp_glyph->charpos
28618 && tmp_glyph->charpos < endpos));
28619 tmp_glyph--)
28620 ;
28621 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28622
28623 /* Calculate the total pixel width of all the glyphs between
28624 the beginning of the highlighted area and GLYPH. */
28625 total_pixel_width = 0;
28626 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28627 total_pixel_width += tmp_glyph->pixel_width;
28628
28629 /* Pre calculation of re-rendering position. Note: X is in
28630 column units here, after the call to mode_line_string or
28631 marginal_area_string. */
28632 hpos = x - gpos;
28633 vpos = (area == ON_MODE_LINE
28634 ? (w->current_matrix)->nrows - 1
28635 : 0);
28636
28637 /* If GLYPH's position is included in the region that is
28638 already drawn in mouse face, we have nothing to do. */
28639 if ( EQ (window, hlinfo->mouse_face_window)
28640 && (!row->reversed_p
28641 ? (hlinfo->mouse_face_beg_col <= hpos
28642 && hpos < hlinfo->mouse_face_end_col)
28643 /* In R2L rows we swap BEG and END, see below. */
28644 : (hlinfo->mouse_face_end_col <= hpos
28645 && hpos < hlinfo->mouse_face_beg_col))
28646 && hlinfo->mouse_face_beg_row == vpos )
28647 return;
28648
28649 if (clear_mouse_face (hlinfo))
28650 cursor = No_Cursor;
28651
28652 if (!row->reversed_p)
28653 {
28654 hlinfo->mouse_face_beg_col = hpos;
28655 hlinfo->mouse_face_beg_x = original_x_pixel
28656 - (total_pixel_width + dx);
28657 hlinfo->mouse_face_end_col = hpos + gseq_length;
28658 hlinfo->mouse_face_end_x = 0;
28659 }
28660 else
28661 {
28662 /* In R2L rows, show_mouse_face expects BEG and END
28663 coordinates to be swapped. */
28664 hlinfo->mouse_face_end_col = hpos;
28665 hlinfo->mouse_face_end_x = original_x_pixel
28666 - (total_pixel_width + dx);
28667 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28668 hlinfo->mouse_face_beg_x = 0;
28669 }
28670
28671 hlinfo->mouse_face_beg_row = vpos;
28672 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28673 hlinfo->mouse_face_past_end = 0;
28674 hlinfo->mouse_face_window = window;
28675
28676 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28677 charpos,
28678 0, &ignore,
28679 glyph->face_id,
28680 1);
28681 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28682
28683 if (NILP (pointer))
28684 pointer = Qhand;
28685 }
28686 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28687 clear_mouse_face (hlinfo);
28688 }
28689 #ifdef HAVE_WINDOW_SYSTEM
28690 if (FRAME_WINDOW_P (f))
28691 define_frame_cursor1 (f, cursor, pointer);
28692 #endif
28693 }
28694
28695
28696 /* EXPORT:
28697 Take proper action when the mouse has moved to position X, Y on
28698 frame F with regards to highlighting portions of display that have
28699 mouse-face properties. Also de-highlight portions of display where
28700 the mouse was before, set the mouse pointer shape as appropriate
28701 for the mouse coordinates, and activate help echo (tooltips).
28702 X and Y can be negative or out of range. */
28703
28704 void
28705 note_mouse_highlight (struct frame *f, int x, int y)
28706 {
28707 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28708 enum window_part part = ON_NOTHING;
28709 Lisp_Object window;
28710 struct window *w;
28711 Cursor cursor = No_Cursor;
28712 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28713 struct buffer *b;
28714
28715 /* When a menu is active, don't highlight because this looks odd. */
28716 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28717 if (popup_activated ())
28718 return;
28719 #endif
28720
28721 if (!f->glyphs_initialized_p
28722 || f->pointer_invisible)
28723 return;
28724
28725 hlinfo->mouse_face_mouse_x = x;
28726 hlinfo->mouse_face_mouse_y = y;
28727 hlinfo->mouse_face_mouse_frame = f;
28728
28729 if (hlinfo->mouse_face_defer)
28730 return;
28731
28732 /* Which window is that in? */
28733 window = window_from_coordinates (f, x, y, &part, 1);
28734
28735 /* If displaying active text in another window, clear that. */
28736 if (! EQ (window, hlinfo->mouse_face_window)
28737 /* Also clear if we move out of text area in same window. */
28738 || (!NILP (hlinfo->mouse_face_window)
28739 && !NILP (window)
28740 && part != ON_TEXT
28741 && part != ON_MODE_LINE
28742 && part != ON_HEADER_LINE))
28743 clear_mouse_face (hlinfo);
28744
28745 /* Not on a window -> return. */
28746 if (!WINDOWP (window))
28747 return;
28748
28749 /* Reset help_echo_string. It will get recomputed below. */
28750 help_echo_string = Qnil;
28751
28752 /* Convert to window-relative pixel coordinates. */
28753 w = XWINDOW (window);
28754 frame_to_window_pixel_xy (w, &x, &y);
28755
28756 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28757 /* Handle tool-bar window differently since it doesn't display a
28758 buffer. */
28759 if (EQ (window, f->tool_bar_window))
28760 {
28761 note_tool_bar_highlight (f, x, y);
28762 return;
28763 }
28764 #endif
28765
28766 /* Mouse is on the mode, header line or margin? */
28767 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28768 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28769 {
28770 note_mode_line_or_margin_highlight (window, x, y, part);
28771
28772 #ifdef HAVE_WINDOW_SYSTEM
28773 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28774 {
28775 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28776 /* Show non-text cursor (Bug#16647). */
28777 goto set_cursor;
28778 }
28779 else
28780 #endif
28781 return;
28782 }
28783
28784 #ifdef HAVE_WINDOW_SYSTEM
28785 if (part == ON_VERTICAL_BORDER)
28786 {
28787 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28788 help_echo_string = build_string ("drag-mouse-1: resize");
28789 }
28790 else if (part == ON_RIGHT_DIVIDER)
28791 {
28792 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28793 help_echo_string = build_string ("drag-mouse-1: resize");
28794 }
28795 else if (part == ON_BOTTOM_DIVIDER)
28796 if (! WINDOW_BOTTOMMOST_P (w)
28797 || minibuf_level
28798 || NILP (Vresize_mini_windows))
28799 {
28800 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28801 help_echo_string = build_string ("drag-mouse-1: resize");
28802 }
28803 else
28804 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28805 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28806 || part == ON_SCROLL_BAR)
28807 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28808 else
28809 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28810 #endif
28811
28812 /* Are we in a window whose display is up to date?
28813 And verify the buffer's text has not changed. */
28814 b = XBUFFER (w->contents);
28815 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28816 {
28817 int hpos, vpos, dx, dy, area = LAST_AREA;
28818 ptrdiff_t pos;
28819 struct glyph *glyph;
28820 Lisp_Object object;
28821 Lisp_Object mouse_face = Qnil, position;
28822 Lisp_Object *overlay_vec = NULL;
28823 ptrdiff_t i, noverlays;
28824 struct buffer *obuf;
28825 ptrdiff_t obegv, ozv;
28826 int same_region;
28827
28828 /* Find the glyph under X/Y. */
28829 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28830
28831 #ifdef HAVE_WINDOW_SYSTEM
28832 /* Look for :pointer property on image. */
28833 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28834 {
28835 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28836 if (img != NULL && IMAGEP (img->spec))
28837 {
28838 Lisp_Object image_map, hotspot;
28839 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28840 !NILP (image_map))
28841 && (hotspot = find_hot_spot (image_map,
28842 glyph->slice.img.x + dx,
28843 glyph->slice.img.y + dy),
28844 CONSP (hotspot))
28845 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28846 {
28847 Lisp_Object plist;
28848
28849 /* Could check XCAR (hotspot) to see if we enter/leave
28850 this hot-spot.
28851 If so, we could look for mouse-enter, mouse-leave
28852 properties in PLIST (and do something...). */
28853 hotspot = XCDR (hotspot);
28854 if (CONSP (hotspot)
28855 && (plist = XCAR (hotspot), CONSP (plist)))
28856 {
28857 pointer = Fplist_get (plist, Qpointer);
28858 if (NILP (pointer))
28859 pointer = Qhand;
28860 help_echo_string = Fplist_get (plist, Qhelp_echo);
28861 if (!NILP (help_echo_string))
28862 {
28863 help_echo_window = window;
28864 help_echo_object = glyph->object;
28865 help_echo_pos = glyph->charpos;
28866 }
28867 }
28868 }
28869 if (NILP (pointer))
28870 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28871 }
28872 }
28873 #endif /* HAVE_WINDOW_SYSTEM */
28874
28875 /* Clear mouse face if X/Y not over text. */
28876 if (glyph == NULL
28877 || area != TEXT_AREA
28878 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28879 /* Glyph's OBJECT is an integer for glyphs inserted by the
28880 display engine for its internal purposes, like truncation
28881 and continuation glyphs and blanks beyond the end of
28882 line's text on text terminals. If we are over such a
28883 glyph, we are not over any text. */
28884 || INTEGERP (glyph->object)
28885 /* R2L rows have a stretch glyph at their front, which
28886 stands for no text, whereas L2R rows have no glyphs at
28887 all beyond the end of text. Treat such stretch glyphs
28888 like we do with NULL glyphs in L2R rows. */
28889 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28890 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28891 && glyph->type == STRETCH_GLYPH
28892 && glyph->avoid_cursor_p))
28893 {
28894 if (clear_mouse_face (hlinfo))
28895 cursor = No_Cursor;
28896 #ifdef HAVE_WINDOW_SYSTEM
28897 if (FRAME_WINDOW_P (f) && NILP (pointer))
28898 {
28899 if (area != TEXT_AREA)
28900 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28901 else
28902 pointer = Vvoid_text_area_pointer;
28903 }
28904 #endif
28905 goto set_cursor;
28906 }
28907
28908 pos = glyph->charpos;
28909 object = glyph->object;
28910 if (!STRINGP (object) && !BUFFERP (object))
28911 goto set_cursor;
28912
28913 /* If we get an out-of-range value, return now; avoid an error. */
28914 if (BUFFERP (object) && pos > BUF_Z (b))
28915 goto set_cursor;
28916
28917 /* Make the window's buffer temporarily current for
28918 overlays_at and compute_char_face. */
28919 obuf = current_buffer;
28920 current_buffer = b;
28921 obegv = BEGV;
28922 ozv = ZV;
28923 BEGV = BEG;
28924 ZV = Z;
28925
28926 /* Is this char mouse-active or does it have help-echo? */
28927 position = make_number (pos);
28928
28929 if (BUFFERP (object))
28930 {
28931 /* Put all the overlays we want in a vector in overlay_vec. */
28932 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28933 /* Sort overlays into increasing priority order. */
28934 noverlays = sort_overlays (overlay_vec, noverlays, w);
28935 }
28936 else
28937 noverlays = 0;
28938
28939 if (NILP (Vmouse_highlight))
28940 {
28941 clear_mouse_face (hlinfo);
28942 goto check_help_echo;
28943 }
28944
28945 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28946
28947 if (same_region)
28948 cursor = No_Cursor;
28949
28950 /* Check mouse-face highlighting. */
28951 if (! same_region
28952 /* If there exists an overlay with mouse-face overlapping
28953 the one we are currently highlighting, we have to
28954 check if we enter the overlapping overlay, and then
28955 highlight only that. */
28956 || (OVERLAYP (hlinfo->mouse_face_overlay)
28957 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28958 {
28959 /* Find the highest priority overlay with a mouse-face. */
28960 Lisp_Object overlay = Qnil;
28961 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28962 {
28963 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28964 if (!NILP (mouse_face))
28965 overlay = overlay_vec[i];
28966 }
28967
28968 /* If we're highlighting the same overlay as before, there's
28969 no need to do that again. */
28970 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28971 goto check_help_echo;
28972 hlinfo->mouse_face_overlay = overlay;
28973
28974 /* Clear the display of the old active region, if any. */
28975 if (clear_mouse_face (hlinfo))
28976 cursor = No_Cursor;
28977
28978 /* If no overlay applies, get a text property. */
28979 if (NILP (overlay))
28980 mouse_face = Fget_text_property (position, Qmouse_face, object);
28981
28982 /* Next, compute the bounds of the mouse highlighting and
28983 display it. */
28984 if (!NILP (mouse_face) && STRINGP (object))
28985 {
28986 /* The mouse-highlighting comes from a display string
28987 with a mouse-face. */
28988 Lisp_Object s, e;
28989 ptrdiff_t ignore;
28990
28991 s = Fprevious_single_property_change
28992 (make_number (pos + 1), Qmouse_face, object, Qnil);
28993 e = Fnext_single_property_change
28994 (position, Qmouse_face, object, Qnil);
28995 if (NILP (s))
28996 s = make_number (0);
28997 if (NILP (e))
28998 e = make_number (SCHARS (object));
28999 mouse_face_from_string_pos (w, hlinfo, object,
29000 XINT (s), XINT (e));
29001 hlinfo->mouse_face_past_end = 0;
29002 hlinfo->mouse_face_window = window;
29003 hlinfo->mouse_face_face_id
29004 = face_at_string_position (w, object, pos, 0, &ignore,
29005 glyph->face_id, 1);
29006 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29007 cursor = No_Cursor;
29008 }
29009 else
29010 {
29011 /* The mouse-highlighting, if any, comes from an overlay
29012 or text property in the buffer. */
29013 Lisp_Object buffer IF_LINT (= Qnil);
29014 Lisp_Object disp_string IF_LINT (= Qnil);
29015
29016 if (STRINGP (object))
29017 {
29018 /* If we are on a display string with no mouse-face,
29019 check if the text under it has one. */
29020 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29021 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29022 pos = string_buffer_position (object, start);
29023 if (pos > 0)
29024 {
29025 mouse_face = get_char_property_and_overlay
29026 (make_number (pos), Qmouse_face, w->contents, &overlay);
29027 buffer = w->contents;
29028 disp_string = object;
29029 }
29030 }
29031 else
29032 {
29033 buffer = object;
29034 disp_string = Qnil;
29035 }
29036
29037 if (!NILP (mouse_face))
29038 {
29039 Lisp_Object before, after;
29040 Lisp_Object before_string, after_string;
29041 /* To correctly find the limits of mouse highlight
29042 in a bidi-reordered buffer, we must not use the
29043 optimization of limiting the search in
29044 previous-single-property-change and
29045 next-single-property-change, because
29046 rows_from_pos_range needs the real start and end
29047 positions to DTRT in this case. That's because
29048 the first row visible in a window does not
29049 necessarily display the character whose position
29050 is the smallest. */
29051 Lisp_Object lim1
29052 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29053 ? Fmarker_position (w->start)
29054 : Qnil;
29055 Lisp_Object lim2
29056 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29057 ? make_number (BUF_Z (XBUFFER (buffer))
29058 - w->window_end_pos)
29059 : Qnil;
29060
29061 if (NILP (overlay))
29062 {
29063 /* Handle the text property case. */
29064 before = Fprevious_single_property_change
29065 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29066 after = Fnext_single_property_change
29067 (make_number (pos), Qmouse_face, buffer, lim2);
29068 before_string = after_string = Qnil;
29069 }
29070 else
29071 {
29072 /* Handle the overlay case. */
29073 before = Foverlay_start (overlay);
29074 after = Foverlay_end (overlay);
29075 before_string = Foverlay_get (overlay, Qbefore_string);
29076 after_string = Foverlay_get (overlay, Qafter_string);
29077
29078 if (!STRINGP (before_string)) before_string = Qnil;
29079 if (!STRINGP (after_string)) after_string = Qnil;
29080 }
29081
29082 mouse_face_from_buffer_pos (window, hlinfo, pos,
29083 NILP (before)
29084 ? 1
29085 : XFASTINT (before),
29086 NILP (after)
29087 ? BUF_Z (XBUFFER (buffer))
29088 : XFASTINT (after),
29089 before_string, after_string,
29090 disp_string);
29091 cursor = No_Cursor;
29092 }
29093 }
29094 }
29095
29096 check_help_echo:
29097
29098 /* Look for a `help-echo' property. */
29099 if (NILP (help_echo_string)) {
29100 Lisp_Object help, overlay;
29101
29102 /* Check overlays first. */
29103 help = overlay = Qnil;
29104 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29105 {
29106 overlay = overlay_vec[i];
29107 help = Foverlay_get (overlay, Qhelp_echo);
29108 }
29109
29110 if (!NILP (help))
29111 {
29112 help_echo_string = help;
29113 help_echo_window = window;
29114 help_echo_object = overlay;
29115 help_echo_pos = pos;
29116 }
29117 else
29118 {
29119 Lisp_Object obj = glyph->object;
29120 ptrdiff_t charpos = glyph->charpos;
29121
29122 /* Try text properties. */
29123 if (STRINGP (obj)
29124 && charpos >= 0
29125 && charpos < SCHARS (obj))
29126 {
29127 help = Fget_text_property (make_number (charpos),
29128 Qhelp_echo, obj);
29129 if (NILP (help))
29130 {
29131 /* If the string itself doesn't specify a help-echo,
29132 see if the buffer text ``under'' it does. */
29133 struct glyph_row *r
29134 = MATRIX_ROW (w->current_matrix, vpos);
29135 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29136 ptrdiff_t p = string_buffer_position (obj, start);
29137 if (p > 0)
29138 {
29139 help = Fget_char_property (make_number (p),
29140 Qhelp_echo, w->contents);
29141 if (!NILP (help))
29142 {
29143 charpos = p;
29144 obj = w->contents;
29145 }
29146 }
29147 }
29148 }
29149 else if (BUFFERP (obj)
29150 && charpos >= BEGV
29151 && charpos < ZV)
29152 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29153 obj);
29154
29155 if (!NILP (help))
29156 {
29157 help_echo_string = help;
29158 help_echo_window = window;
29159 help_echo_object = obj;
29160 help_echo_pos = charpos;
29161 }
29162 }
29163 }
29164
29165 #ifdef HAVE_WINDOW_SYSTEM
29166 /* Look for a `pointer' property. */
29167 if (FRAME_WINDOW_P (f) && NILP (pointer))
29168 {
29169 /* Check overlays first. */
29170 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29171 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29172
29173 if (NILP (pointer))
29174 {
29175 Lisp_Object obj = glyph->object;
29176 ptrdiff_t charpos = glyph->charpos;
29177
29178 /* Try text properties. */
29179 if (STRINGP (obj)
29180 && charpos >= 0
29181 && charpos < SCHARS (obj))
29182 {
29183 pointer = Fget_text_property (make_number (charpos),
29184 Qpointer, obj);
29185 if (NILP (pointer))
29186 {
29187 /* If the string itself doesn't specify a pointer,
29188 see if the buffer text ``under'' it does. */
29189 struct glyph_row *r
29190 = MATRIX_ROW (w->current_matrix, vpos);
29191 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29192 ptrdiff_t p = string_buffer_position (obj, start);
29193 if (p > 0)
29194 pointer = Fget_char_property (make_number (p),
29195 Qpointer, w->contents);
29196 }
29197 }
29198 else if (BUFFERP (obj)
29199 && charpos >= BEGV
29200 && charpos < ZV)
29201 pointer = Fget_text_property (make_number (charpos),
29202 Qpointer, obj);
29203 }
29204 }
29205 #endif /* HAVE_WINDOW_SYSTEM */
29206
29207 BEGV = obegv;
29208 ZV = ozv;
29209 current_buffer = obuf;
29210 }
29211
29212 set_cursor:
29213
29214 #ifdef HAVE_WINDOW_SYSTEM
29215 if (FRAME_WINDOW_P (f))
29216 define_frame_cursor1 (f, cursor, pointer);
29217 #else
29218 /* This is here to prevent a compiler error, about "label at end of
29219 compound statement". */
29220 return;
29221 #endif
29222 }
29223
29224
29225 /* EXPORT for RIF:
29226 Clear any mouse-face on window W. This function is part of the
29227 redisplay interface, and is called from try_window_id and similar
29228 functions to ensure the mouse-highlight is off. */
29229
29230 void
29231 x_clear_window_mouse_face (struct window *w)
29232 {
29233 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29234 Lisp_Object window;
29235
29236 block_input ();
29237 XSETWINDOW (window, w);
29238 if (EQ (window, hlinfo->mouse_face_window))
29239 clear_mouse_face (hlinfo);
29240 unblock_input ();
29241 }
29242
29243
29244 /* EXPORT:
29245 Just discard the mouse face information for frame F, if any.
29246 This is used when the size of F is changed. */
29247
29248 void
29249 cancel_mouse_face (struct frame *f)
29250 {
29251 Lisp_Object window;
29252 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29253
29254 window = hlinfo->mouse_face_window;
29255 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29256 reset_mouse_highlight (hlinfo);
29257 }
29258
29259
29260 \f
29261 /***********************************************************************
29262 Exposure Events
29263 ***********************************************************************/
29264
29265 #ifdef HAVE_WINDOW_SYSTEM
29266
29267 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29268 which intersects rectangle R. R is in window-relative coordinates. */
29269
29270 static void
29271 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29272 enum glyph_row_area area)
29273 {
29274 struct glyph *first = row->glyphs[area];
29275 struct glyph *end = row->glyphs[area] + row->used[area];
29276 struct glyph *last;
29277 int first_x, start_x, x;
29278
29279 if (area == TEXT_AREA && row->fill_line_p)
29280 /* If row extends face to end of line write the whole line. */
29281 draw_glyphs (w, 0, row, area,
29282 0, row->used[area],
29283 DRAW_NORMAL_TEXT, 0);
29284 else
29285 {
29286 /* Set START_X to the window-relative start position for drawing glyphs of
29287 AREA. The first glyph of the text area can be partially visible.
29288 The first glyphs of other areas cannot. */
29289 start_x = window_box_left_offset (w, area);
29290 x = start_x;
29291 if (area == TEXT_AREA)
29292 x += row->x;
29293
29294 /* Find the first glyph that must be redrawn. */
29295 while (first < end
29296 && x + first->pixel_width < r->x)
29297 {
29298 x += first->pixel_width;
29299 ++first;
29300 }
29301
29302 /* Find the last one. */
29303 last = first;
29304 first_x = x;
29305 while (last < end
29306 && x < r->x + r->width)
29307 {
29308 x += last->pixel_width;
29309 ++last;
29310 }
29311
29312 /* Repaint. */
29313 if (last > first)
29314 draw_glyphs (w, first_x - start_x, row, area,
29315 first - row->glyphs[area], last - row->glyphs[area],
29316 DRAW_NORMAL_TEXT, 0);
29317 }
29318 }
29319
29320
29321 /* Redraw the parts of the glyph row ROW on window W intersecting
29322 rectangle R. R is in window-relative coordinates. Value is
29323 non-zero if mouse-face was overwritten. */
29324
29325 static int
29326 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29327 {
29328 eassert (row->enabled_p);
29329
29330 if (row->mode_line_p || w->pseudo_window_p)
29331 draw_glyphs (w, 0, row, TEXT_AREA,
29332 0, row->used[TEXT_AREA],
29333 DRAW_NORMAL_TEXT, 0);
29334 else
29335 {
29336 if (row->used[LEFT_MARGIN_AREA])
29337 expose_area (w, row, r, LEFT_MARGIN_AREA);
29338 if (row->used[TEXT_AREA])
29339 expose_area (w, row, r, TEXT_AREA);
29340 if (row->used[RIGHT_MARGIN_AREA])
29341 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29342 draw_row_fringe_bitmaps (w, row);
29343 }
29344
29345 return row->mouse_face_p;
29346 }
29347
29348
29349 /* Redraw those parts of glyphs rows during expose event handling that
29350 overlap other rows. Redrawing of an exposed line writes over parts
29351 of lines overlapping that exposed line; this function fixes that.
29352
29353 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29354 row in W's current matrix that is exposed and overlaps other rows.
29355 LAST_OVERLAPPING_ROW is the last such row. */
29356
29357 static void
29358 expose_overlaps (struct window *w,
29359 struct glyph_row *first_overlapping_row,
29360 struct glyph_row *last_overlapping_row,
29361 XRectangle *r)
29362 {
29363 struct glyph_row *row;
29364
29365 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29366 if (row->overlapping_p)
29367 {
29368 eassert (row->enabled_p && !row->mode_line_p);
29369
29370 row->clip = r;
29371 if (row->used[LEFT_MARGIN_AREA])
29372 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29373
29374 if (row->used[TEXT_AREA])
29375 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29376
29377 if (row->used[RIGHT_MARGIN_AREA])
29378 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29379 row->clip = NULL;
29380 }
29381 }
29382
29383
29384 /* Return non-zero if W's cursor intersects rectangle R. */
29385
29386 static int
29387 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29388 {
29389 XRectangle cr, result;
29390 struct glyph *cursor_glyph;
29391 struct glyph_row *row;
29392
29393 if (w->phys_cursor.vpos >= 0
29394 && w->phys_cursor.vpos < w->current_matrix->nrows
29395 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29396 row->enabled_p)
29397 && row->cursor_in_fringe_p)
29398 {
29399 /* Cursor is in the fringe. */
29400 cr.x = window_box_right_offset (w,
29401 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29402 ? RIGHT_MARGIN_AREA
29403 : TEXT_AREA));
29404 cr.y = row->y;
29405 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29406 cr.height = row->height;
29407 return x_intersect_rectangles (&cr, r, &result);
29408 }
29409
29410 cursor_glyph = get_phys_cursor_glyph (w);
29411 if (cursor_glyph)
29412 {
29413 /* r is relative to W's box, but w->phys_cursor.x is relative
29414 to left edge of W's TEXT area. Adjust it. */
29415 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29416 cr.y = w->phys_cursor.y;
29417 cr.width = cursor_glyph->pixel_width;
29418 cr.height = w->phys_cursor_height;
29419 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29420 I assume the effect is the same -- and this is portable. */
29421 return x_intersect_rectangles (&cr, r, &result);
29422 }
29423 /* If we don't understand the format, pretend we're not in the hot-spot. */
29424 return 0;
29425 }
29426
29427
29428 /* EXPORT:
29429 Draw a vertical window border to the right of window W if W doesn't
29430 have vertical scroll bars. */
29431
29432 void
29433 x_draw_vertical_border (struct window *w)
29434 {
29435 struct frame *f = XFRAME (WINDOW_FRAME (w));
29436
29437 /* We could do better, if we knew what type of scroll-bar the adjacent
29438 windows (on either side) have... But we don't :-(
29439 However, I think this works ok. ++KFS 2003-04-25 */
29440
29441 /* Redraw borders between horizontally adjacent windows. Don't
29442 do it for frames with vertical scroll bars because either the
29443 right scroll bar of a window, or the left scroll bar of its
29444 neighbor will suffice as a border. */
29445 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29446 return;
29447
29448 /* Note: It is necessary to redraw both the left and the right
29449 borders, for when only this single window W is being
29450 redisplayed. */
29451 if (!WINDOW_RIGHTMOST_P (w)
29452 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29453 {
29454 int x0, x1, y0, y1;
29455
29456 window_box_edges (w, &x0, &y0, &x1, &y1);
29457 y1 -= 1;
29458
29459 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29460 x1 -= 1;
29461
29462 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29463 }
29464
29465 if (!WINDOW_LEFTMOST_P (w)
29466 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29467 {
29468 int x0, x1, y0, y1;
29469
29470 window_box_edges (w, &x0, &y0, &x1, &y1);
29471 y1 -= 1;
29472
29473 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29474 x0 -= 1;
29475
29476 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29477 }
29478 }
29479
29480
29481 /* Draw window dividers for window W. */
29482
29483 void
29484 x_draw_right_divider (struct window *w)
29485 {
29486 struct frame *f = WINDOW_XFRAME (w);
29487
29488 if (w->mini || w->pseudo_window_p)
29489 return;
29490 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29491 {
29492 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29493 int x1 = WINDOW_RIGHT_EDGE_X (w);
29494 int y0 = WINDOW_TOP_EDGE_Y (w);
29495 /* The bottom divider prevails. */
29496 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29497
29498 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29499 }
29500 }
29501
29502 static void
29503 x_draw_bottom_divider (struct window *w)
29504 {
29505 struct frame *f = XFRAME (WINDOW_FRAME (w));
29506
29507 if (w->mini || w->pseudo_window_p)
29508 return;
29509 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29510 {
29511 int x0 = WINDOW_LEFT_EDGE_X (w);
29512 int x1 = WINDOW_RIGHT_EDGE_X (w);
29513 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29514 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29515
29516 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29517 }
29518 }
29519
29520 /* Redraw the part of window W intersection rectangle FR. Pixel
29521 coordinates in FR are frame-relative. Call this function with
29522 input blocked. Value is non-zero if the exposure overwrites
29523 mouse-face. */
29524
29525 static int
29526 expose_window (struct window *w, XRectangle *fr)
29527 {
29528 struct frame *f = XFRAME (w->frame);
29529 XRectangle wr, r;
29530 int mouse_face_overwritten_p = 0;
29531
29532 /* If window is not yet fully initialized, do nothing. This can
29533 happen when toolkit scroll bars are used and a window is split.
29534 Reconfiguring the scroll bar will generate an expose for a newly
29535 created window. */
29536 if (w->current_matrix == NULL)
29537 return 0;
29538
29539 /* When we're currently updating the window, display and current
29540 matrix usually don't agree. Arrange for a thorough display
29541 later. */
29542 if (w->must_be_updated_p)
29543 {
29544 SET_FRAME_GARBAGED (f);
29545 return 0;
29546 }
29547
29548 /* Frame-relative pixel rectangle of W. */
29549 wr.x = WINDOW_LEFT_EDGE_X (w);
29550 wr.y = WINDOW_TOP_EDGE_Y (w);
29551 wr.width = WINDOW_PIXEL_WIDTH (w);
29552 wr.height = WINDOW_PIXEL_HEIGHT (w);
29553
29554 if (x_intersect_rectangles (fr, &wr, &r))
29555 {
29556 int yb = window_text_bottom_y (w);
29557 struct glyph_row *row;
29558 int cursor_cleared_p, phys_cursor_on_p;
29559 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29560
29561 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29562 r.x, r.y, r.width, r.height));
29563
29564 /* Convert to window coordinates. */
29565 r.x -= WINDOW_LEFT_EDGE_X (w);
29566 r.y -= WINDOW_TOP_EDGE_Y (w);
29567
29568 /* Turn off the cursor. */
29569 if (!w->pseudo_window_p
29570 && phys_cursor_in_rect_p (w, &r))
29571 {
29572 x_clear_cursor (w);
29573 cursor_cleared_p = 1;
29574 }
29575 else
29576 cursor_cleared_p = 0;
29577
29578 /* If the row containing the cursor extends face to end of line,
29579 then expose_area might overwrite the cursor outside the
29580 rectangle and thus notice_overwritten_cursor might clear
29581 w->phys_cursor_on_p. We remember the original value and
29582 check later if it is changed. */
29583 phys_cursor_on_p = w->phys_cursor_on_p;
29584
29585 /* Update lines intersecting rectangle R. */
29586 first_overlapping_row = last_overlapping_row = NULL;
29587 for (row = w->current_matrix->rows;
29588 row->enabled_p;
29589 ++row)
29590 {
29591 int y0 = row->y;
29592 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29593
29594 if ((y0 >= r.y && y0 < r.y + r.height)
29595 || (y1 > r.y && y1 < r.y + r.height)
29596 || (r.y >= y0 && r.y < y1)
29597 || (r.y + r.height > y0 && r.y + r.height < y1))
29598 {
29599 /* A header line may be overlapping, but there is no need
29600 to fix overlapping areas for them. KFS 2005-02-12 */
29601 if (row->overlapping_p && !row->mode_line_p)
29602 {
29603 if (first_overlapping_row == NULL)
29604 first_overlapping_row = row;
29605 last_overlapping_row = row;
29606 }
29607
29608 row->clip = fr;
29609 if (expose_line (w, row, &r))
29610 mouse_face_overwritten_p = 1;
29611 row->clip = NULL;
29612 }
29613 else if (row->overlapping_p)
29614 {
29615 /* We must redraw a row overlapping the exposed area. */
29616 if (y0 < r.y
29617 ? y0 + row->phys_height > r.y
29618 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29619 {
29620 if (first_overlapping_row == NULL)
29621 first_overlapping_row = row;
29622 last_overlapping_row = row;
29623 }
29624 }
29625
29626 if (y1 >= yb)
29627 break;
29628 }
29629
29630 /* Display the mode line if there is one. */
29631 if (WINDOW_WANTS_MODELINE_P (w)
29632 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29633 row->enabled_p)
29634 && row->y < r.y + r.height)
29635 {
29636 if (expose_line (w, row, &r))
29637 mouse_face_overwritten_p = 1;
29638 }
29639
29640 if (!w->pseudo_window_p)
29641 {
29642 /* Fix the display of overlapping rows. */
29643 if (first_overlapping_row)
29644 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29645 fr);
29646
29647 /* Draw border between windows. */
29648 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29649 x_draw_right_divider (w);
29650 else
29651 x_draw_vertical_border (w);
29652
29653 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29654 x_draw_bottom_divider (w);
29655
29656 /* Turn the cursor on again. */
29657 if (cursor_cleared_p
29658 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29659 update_window_cursor (w, 1);
29660 }
29661 }
29662
29663 return mouse_face_overwritten_p;
29664 }
29665
29666
29667
29668 /* Redraw (parts) of all windows in the window tree rooted at W that
29669 intersect R. R contains frame pixel coordinates. Value is
29670 non-zero if the exposure overwrites mouse-face. */
29671
29672 static int
29673 expose_window_tree (struct window *w, XRectangle *r)
29674 {
29675 struct frame *f = XFRAME (w->frame);
29676 int mouse_face_overwritten_p = 0;
29677
29678 while (w && !FRAME_GARBAGED_P (f))
29679 {
29680 if (WINDOWP (w->contents))
29681 mouse_face_overwritten_p
29682 |= expose_window_tree (XWINDOW (w->contents), r);
29683 else
29684 mouse_face_overwritten_p |= expose_window (w, r);
29685
29686 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29687 }
29688
29689 return mouse_face_overwritten_p;
29690 }
29691
29692
29693 /* EXPORT:
29694 Redisplay an exposed area of frame F. X and Y are the upper-left
29695 corner of the exposed rectangle. W and H are width and height of
29696 the exposed area. All are pixel values. W or H zero means redraw
29697 the entire frame. */
29698
29699 void
29700 expose_frame (struct frame *f, int x, int y, int w, int h)
29701 {
29702 XRectangle r;
29703 int mouse_face_overwritten_p = 0;
29704
29705 TRACE ((stderr, "expose_frame "));
29706
29707 /* No need to redraw if frame will be redrawn soon. */
29708 if (FRAME_GARBAGED_P (f))
29709 {
29710 TRACE ((stderr, " garbaged\n"));
29711 return;
29712 }
29713
29714 /* If basic faces haven't been realized yet, there is no point in
29715 trying to redraw anything. This can happen when we get an expose
29716 event while Emacs is starting, e.g. by moving another window. */
29717 if (FRAME_FACE_CACHE (f) == NULL
29718 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29719 {
29720 TRACE ((stderr, " no faces\n"));
29721 return;
29722 }
29723
29724 if (w == 0 || h == 0)
29725 {
29726 r.x = r.y = 0;
29727 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29728 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29729 }
29730 else
29731 {
29732 r.x = x;
29733 r.y = y;
29734 r.width = w;
29735 r.height = h;
29736 }
29737
29738 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29739 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29740
29741 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29742 if (WINDOWP (f->tool_bar_window))
29743 mouse_face_overwritten_p
29744 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29745 #endif
29746
29747 #ifdef HAVE_X_WINDOWS
29748 #ifndef MSDOS
29749 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29750 if (WINDOWP (f->menu_bar_window))
29751 mouse_face_overwritten_p
29752 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29753 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29754 #endif
29755 #endif
29756
29757 /* Some window managers support a focus-follows-mouse style with
29758 delayed raising of frames. Imagine a partially obscured frame,
29759 and moving the mouse into partially obscured mouse-face on that
29760 frame. The visible part of the mouse-face will be highlighted,
29761 then the WM raises the obscured frame. With at least one WM, KDE
29762 2.1, Emacs is not getting any event for the raising of the frame
29763 (even tried with SubstructureRedirectMask), only Expose events.
29764 These expose events will draw text normally, i.e. not
29765 highlighted. Which means we must redo the highlight here.
29766 Subsume it under ``we love X''. --gerd 2001-08-15 */
29767 /* Included in Windows version because Windows most likely does not
29768 do the right thing if any third party tool offers
29769 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29770 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29771 {
29772 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29773 if (f == hlinfo->mouse_face_mouse_frame)
29774 {
29775 int mouse_x = hlinfo->mouse_face_mouse_x;
29776 int mouse_y = hlinfo->mouse_face_mouse_y;
29777 clear_mouse_face (hlinfo);
29778 note_mouse_highlight (f, mouse_x, mouse_y);
29779 }
29780 }
29781 }
29782
29783
29784 /* EXPORT:
29785 Determine the intersection of two rectangles R1 and R2. Return
29786 the intersection in *RESULT. Value is non-zero if RESULT is not
29787 empty. */
29788
29789 int
29790 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29791 {
29792 XRectangle *left, *right;
29793 XRectangle *upper, *lower;
29794 int intersection_p = 0;
29795
29796 /* Rearrange so that R1 is the left-most rectangle. */
29797 if (r1->x < r2->x)
29798 left = r1, right = r2;
29799 else
29800 left = r2, right = r1;
29801
29802 /* X0 of the intersection is right.x0, if this is inside R1,
29803 otherwise there is no intersection. */
29804 if (right->x <= left->x + left->width)
29805 {
29806 result->x = right->x;
29807
29808 /* The right end of the intersection is the minimum of
29809 the right ends of left and right. */
29810 result->width = (min (left->x + left->width, right->x + right->width)
29811 - result->x);
29812
29813 /* Same game for Y. */
29814 if (r1->y < r2->y)
29815 upper = r1, lower = r2;
29816 else
29817 upper = r2, lower = r1;
29818
29819 /* The upper end of the intersection is lower.y0, if this is inside
29820 of upper. Otherwise, there is no intersection. */
29821 if (lower->y <= upper->y + upper->height)
29822 {
29823 result->y = lower->y;
29824
29825 /* The lower end of the intersection is the minimum of the lower
29826 ends of upper and lower. */
29827 result->height = (min (lower->y + lower->height,
29828 upper->y + upper->height)
29829 - result->y);
29830 intersection_p = 1;
29831 }
29832 }
29833
29834 return intersection_p;
29835 }
29836
29837 #endif /* HAVE_WINDOW_SYSTEM */
29838
29839 \f
29840 /***********************************************************************
29841 Initialization
29842 ***********************************************************************/
29843
29844 void
29845 syms_of_xdisp (void)
29846 {
29847 Vwith_echo_area_save_vector = Qnil;
29848 staticpro (&Vwith_echo_area_save_vector);
29849
29850 Vmessage_stack = Qnil;
29851 staticpro (&Vmessage_stack);
29852
29853 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29854 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29855
29856 message_dolog_marker1 = Fmake_marker ();
29857 staticpro (&message_dolog_marker1);
29858 message_dolog_marker2 = Fmake_marker ();
29859 staticpro (&message_dolog_marker2);
29860 message_dolog_marker3 = Fmake_marker ();
29861 staticpro (&message_dolog_marker3);
29862
29863 #ifdef GLYPH_DEBUG
29864 defsubr (&Sdump_frame_glyph_matrix);
29865 defsubr (&Sdump_glyph_matrix);
29866 defsubr (&Sdump_glyph_row);
29867 defsubr (&Sdump_tool_bar_row);
29868 defsubr (&Strace_redisplay);
29869 defsubr (&Strace_to_stderr);
29870 #endif
29871 #ifdef HAVE_WINDOW_SYSTEM
29872 defsubr (&Stool_bar_height);
29873 defsubr (&Slookup_image_map);
29874 #endif
29875 defsubr (&Sline_pixel_height);
29876 defsubr (&Sformat_mode_line);
29877 defsubr (&Sinvisible_p);
29878 defsubr (&Scurrent_bidi_paragraph_direction);
29879 defsubr (&Swindow_text_pixel_size);
29880 defsubr (&Smove_point_visually);
29881
29882 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29883 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29884 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29885 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29886 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29887 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29888 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29889 DEFSYM (Qeval, "eval");
29890 DEFSYM (QCdata, ":data");
29891 DEFSYM (Qdisplay, "display");
29892 DEFSYM (Qspace_width, "space-width");
29893 DEFSYM (Qraise, "raise");
29894 DEFSYM (Qslice, "slice");
29895 DEFSYM (Qspace, "space");
29896 DEFSYM (Qmargin, "margin");
29897 DEFSYM (Qpointer, "pointer");
29898 DEFSYM (Qleft_margin, "left-margin");
29899 DEFSYM (Qright_margin, "right-margin");
29900 DEFSYM (Qcenter, "center");
29901 DEFSYM (Qline_height, "line-height");
29902 DEFSYM (QCalign_to, ":align-to");
29903 DEFSYM (QCrelative_width, ":relative-width");
29904 DEFSYM (QCrelative_height, ":relative-height");
29905 DEFSYM (QCeval, ":eval");
29906 DEFSYM (QCpropertize, ":propertize");
29907 DEFSYM (QCfile, ":file");
29908 DEFSYM (Qfontified, "fontified");
29909 DEFSYM (Qfontification_functions, "fontification-functions");
29910 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29911 DEFSYM (Qescape_glyph, "escape-glyph");
29912 DEFSYM (Qnobreak_space, "nobreak-space");
29913 DEFSYM (Qimage, "image");
29914 DEFSYM (Qtext, "text");
29915 DEFSYM (Qboth, "both");
29916 DEFSYM (Qboth_horiz, "both-horiz");
29917 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29918 DEFSYM (QCmap, ":map");
29919 DEFSYM (QCpointer, ":pointer");
29920 DEFSYM (Qrect, "rect");
29921 DEFSYM (Qcircle, "circle");
29922 DEFSYM (Qpoly, "poly");
29923 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29924 DEFSYM (Qgrow_only, "grow-only");
29925 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29926 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29927 DEFSYM (Qposition, "position");
29928 DEFSYM (Qbuffer_position, "buffer-position");
29929 DEFSYM (Qobject, "object");
29930 DEFSYM (Qbar, "bar");
29931 DEFSYM (Qhbar, "hbar");
29932 DEFSYM (Qbox, "box");
29933 DEFSYM (Qhollow, "hollow");
29934 DEFSYM (Qhand, "hand");
29935 DEFSYM (Qarrow, "arrow");
29936 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29937
29938 list_of_error = list1 (list2 (intern_c_string ("error"),
29939 intern_c_string ("void-variable")));
29940 staticpro (&list_of_error);
29941
29942 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29943 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29944 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29945 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29946
29947 echo_buffer[0] = echo_buffer[1] = Qnil;
29948 staticpro (&echo_buffer[0]);
29949 staticpro (&echo_buffer[1]);
29950
29951 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29952 staticpro (&echo_area_buffer[0]);
29953 staticpro (&echo_area_buffer[1]);
29954
29955 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29956 staticpro (&Vmessages_buffer_name);
29957
29958 mode_line_proptrans_alist = Qnil;
29959 staticpro (&mode_line_proptrans_alist);
29960 mode_line_string_list = Qnil;
29961 staticpro (&mode_line_string_list);
29962 mode_line_string_face = Qnil;
29963 staticpro (&mode_line_string_face);
29964 mode_line_string_face_prop = Qnil;
29965 staticpro (&mode_line_string_face_prop);
29966 Vmode_line_unwind_vector = Qnil;
29967 staticpro (&Vmode_line_unwind_vector);
29968
29969 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29970
29971 help_echo_string = Qnil;
29972 staticpro (&help_echo_string);
29973 help_echo_object = Qnil;
29974 staticpro (&help_echo_object);
29975 help_echo_window = Qnil;
29976 staticpro (&help_echo_window);
29977 previous_help_echo_string = Qnil;
29978 staticpro (&previous_help_echo_string);
29979 help_echo_pos = -1;
29980
29981 DEFSYM (Qright_to_left, "right-to-left");
29982 DEFSYM (Qleft_to_right, "left-to-right");
29983
29984 #ifdef HAVE_WINDOW_SYSTEM
29985 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29986 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29987 For example, if a block cursor is over a tab, it will be drawn as
29988 wide as that tab on the display. */);
29989 x_stretch_cursor_p = 0;
29990 #endif
29991
29992 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29993 doc: /* Non-nil means highlight trailing whitespace.
29994 The face used for trailing whitespace is `trailing-whitespace'. */);
29995 Vshow_trailing_whitespace = Qnil;
29996
29997 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29998 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29999 If the value is t, Emacs highlights non-ASCII chars which have the
30000 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30001 or `escape-glyph' face respectively.
30002
30003 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30004 U+2011 (non-breaking hyphen) are affected.
30005
30006 Any other non-nil value means to display these characters as a escape
30007 glyph followed by an ordinary space or hyphen.
30008
30009 A value of nil means no special handling of these characters. */);
30010 Vnobreak_char_display = Qt;
30011
30012 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30013 doc: /* The pointer shape to show in void text areas.
30014 A value of nil means to show the text pointer. Other options are `arrow',
30015 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30016 Vvoid_text_area_pointer = Qarrow;
30017
30018 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30019 doc: /* Non-nil means don't actually do any redisplay.
30020 This is used for internal purposes. */);
30021 Vinhibit_redisplay = Qnil;
30022
30023 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30024 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30025 Vglobal_mode_string = Qnil;
30026
30027 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30028 doc: /* Marker for where to display an arrow on top of the buffer text.
30029 This must be the beginning of a line in order to work.
30030 See also `overlay-arrow-string'. */);
30031 Voverlay_arrow_position = Qnil;
30032
30033 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30034 doc: /* String to display as an arrow in non-window frames.
30035 See also `overlay-arrow-position'. */);
30036 Voverlay_arrow_string = build_pure_c_string ("=>");
30037
30038 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30039 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30040 The symbols on this list are examined during redisplay to determine
30041 where to display overlay arrows. */);
30042 Voverlay_arrow_variable_list
30043 = list1 (intern_c_string ("overlay-arrow-position"));
30044
30045 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30046 doc: /* The number of lines to try scrolling a window by when point moves out.
30047 If that fails to bring point back on frame, point is centered instead.
30048 If this is zero, point is always centered after it moves off frame.
30049 If you want scrolling to always be a line at a time, you should set
30050 `scroll-conservatively' to a large value rather than set this to 1. */);
30051
30052 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30053 doc: /* Scroll up to this many lines, to bring point back on screen.
30054 If point moves off-screen, redisplay will scroll by up to
30055 `scroll-conservatively' lines in order to bring point just barely
30056 onto the screen again. If that cannot be done, then redisplay
30057 recenters point as usual.
30058
30059 If the value is greater than 100, redisplay will never recenter point,
30060 but will always scroll just enough text to bring point into view, even
30061 if you move far away.
30062
30063 A value of zero means always recenter point if it moves off screen. */);
30064 scroll_conservatively = 0;
30065
30066 DEFVAR_INT ("scroll-margin", scroll_margin,
30067 doc: /* Number of lines of margin at the top and bottom of a window.
30068 Recenter the window whenever point gets within this many lines
30069 of the top or bottom of the window. */);
30070 scroll_margin = 0;
30071
30072 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30073 doc: /* Pixels per inch value for non-window system displays.
30074 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30075 Vdisplay_pixels_per_inch = make_float (72.0);
30076
30077 #ifdef GLYPH_DEBUG
30078 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30079 #endif
30080
30081 DEFVAR_LISP ("truncate-partial-width-windows",
30082 Vtruncate_partial_width_windows,
30083 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30084 For an integer value, truncate lines in each window narrower than the
30085 full frame width, provided the window width is less than that integer;
30086 otherwise, respect the value of `truncate-lines'.
30087
30088 For any other non-nil value, truncate lines in all windows that do
30089 not span the full frame width.
30090
30091 A value of nil means to respect the value of `truncate-lines'.
30092
30093 If `word-wrap' is enabled, you might want to reduce this. */);
30094 Vtruncate_partial_width_windows = make_number (50);
30095
30096 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30097 doc: /* Maximum buffer size for which line number should be displayed.
30098 If the buffer is bigger than this, the line number does not appear
30099 in the mode line. A value of nil means no limit. */);
30100 Vline_number_display_limit = Qnil;
30101
30102 DEFVAR_INT ("line-number-display-limit-width",
30103 line_number_display_limit_width,
30104 doc: /* Maximum line width (in characters) for line number display.
30105 If the average length of the lines near point is bigger than this, then the
30106 line number may be omitted from the mode line. */);
30107 line_number_display_limit_width = 200;
30108
30109 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30110 doc: /* Non-nil means highlight region even in nonselected windows. */);
30111 highlight_nonselected_windows = 0;
30112
30113 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30114 doc: /* Non-nil if more than one frame is visible on this display.
30115 Minibuffer-only frames don't count, but iconified frames do.
30116 This variable is not guaranteed to be accurate except while processing
30117 `frame-title-format' and `icon-title-format'. */);
30118
30119 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30120 doc: /* Template for displaying the title bar of visible frames.
30121 \(Assuming the window manager supports this feature.)
30122
30123 This variable has the same structure as `mode-line-format', except that
30124 the %c and %l constructs are ignored. It is used only on frames for
30125 which no explicit name has been set \(see `modify-frame-parameters'). */);
30126
30127 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30128 doc: /* Template for displaying the title bar of an iconified frame.
30129 \(Assuming the window manager supports this feature.)
30130 This variable has the same structure as `mode-line-format' (which see),
30131 and is used only on frames for which no explicit name has been set
30132 \(see `modify-frame-parameters'). */);
30133 Vicon_title_format
30134 = Vframe_title_format
30135 = listn (CONSTYPE_PURE, 3,
30136 intern_c_string ("multiple-frames"),
30137 build_pure_c_string ("%b"),
30138 listn (CONSTYPE_PURE, 4,
30139 empty_unibyte_string,
30140 intern_c_string ("invocation-name"),
30141 build_pure_c_string ("@"),
30142 intern_c_string ("system-name")));
30143
30144 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30145 doc: /* Maximum number of lines to keep in the message log buffer.
30146 If nil, disable message logging. If t, log messages but don't truncate
30147 the buffer when it becomes large. */);
30148 Vmessage_log_max = make_number (1000);
30149
30150 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30151 doc: /* Functions called before redisplay, if window sizes have changed.
30152 The value should be a list of functions that take one argument.
30153 Just before redisplay, for each frame, if any of its windows have changed
30154 size since the last redisplay, or have been split or deleted,
30155 all the functions in the list are called, with the frame as argument. */);
30156 Vwindow_size_change_functions = Qnil;
30157
30158 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30159 doc: /* List of functions to call before redisplaying a window with scrolling.
30160 Each function is called with two arguments, the window and its new
30161 display-start position. Note that these functions are also called by
30162 `set-window-buffer'. Also note that the value of `window-end' is not
30163 valid when these functions are called.
30164
30165 Warning: Do not use this feature to alter the way the window
30166 is scrolled. It is not designed for that, and such use probably won't
30167 work. */);
30168 Vwindow_scroll_functions = Qnil;
30169
30170 DEFVAR_LISP ("window-text-change-functions",
30171 Vwindow_text_change_functions,
30172 doc: /* Functions to call in redisplay when text in the window might change. */);
30173 Vwindow_text_change_functions = Qnil;
30174
30175 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30176 doc: /* Functions called when redisplay of a window reaches the end trigger.
30177 Each function is called with two arguments, the window and the end trigger value.
30178 See `set-window-redisplay-end-trigger'. */);
30179 Vredisplay_end_trigger_functions = Qnil;
30180
30181 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30182 doc: /* Non-nil means autoselect window with mouse pointer.
30183 If nil, do not autoselect windows.
30184 A positive number means delay autoselection by that many seconds: a
30185 window is autoselected only after the mouse has remained in that
30186 window for the duration of the delay.
30187 A negative number has a similar effect, but causes windows to be
30188 autoselected only after the mouse has stopped moving. \(Because of
30189 the way Emacs compares mouse events, you will occasionally wait twice
30190 that time before the window gets selected.\)
30191 Any other value means to autoselect window instantaneously when the
30192 mouse pointer enters it.
30193
30194 Autoselection selects the minibuffer only if it is active, and never
30195 unselects the minibuffer if it is active.
30196
30197 When customizing this variable make sure that the actual value of
30198 `focus-follows-mouse' matches the behavior of your window manager. */);
30199 Vmouse_autoselect_window = Qnil;
30200
30201 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30202 doc: /* Non-nil means automatically resize tool-bars.
30203 This dynamically changes the tool-bar's height to the minimum height
30204 that is needed to make all tool-bar items visible.
30205 If value is `grow-only', the tool-bar's height is only increased
30206 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30207 Vauto_resize_tool_bars = Qt;
30208
30209 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30210 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30211 auto_raise_tool_bar_buttons_p = 1;
30212
30213 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30214 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30215 make_cursor_line_fully_visible_p = 1;
30216
30217 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30218 doc: /* Border below tool-bar in pixels.
30219 If an integer, use it as the height of the border.
30220 If it is one of `internal-border-width' or `border-width', use the
30221 value of the corresponding frame parameter.
30222 Otherwise, no border is added below the tool-bar. */);
30223 Vtool_bar_border = Qinternal_border_width;
30224
30225 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30226 doc: /* Margin around tool-bar buttons in pixels.
30227 If an integer, use that for both horizontal and vertical margins.
30228 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30229 HORZ specifying the horizontal margin, and VERT specifying the
30230 vertical margin. */);
30231 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30232
30233 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30234 doc: /* Relief thickness of tool-bar buttons. */);
30235 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30236
30237 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30238 doc: /* Tool bar style to use.
30239 It can be one of
30240 image - show images only
30241 text - show text only
30242 both - show both, text below image
30243 both-horiz - show text to the right of the image
30244 text-image-horiz - show text to the left of the image
30245 any other - use system default or image if no system default.
30246
30247 This variable only affects the GTK+ toolkit version of Emacs. */);
30248 Vtool_bar_style = Qnil;
30249
30250 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30251 doc: /* Maximum number of characters a label can have to be shown.
30252 The tool bar style must also show labels for this to have any effect, see
30253 `tool-bar-style'. */);
30254 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30255
30256 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30257 doc: /* List of functions to call to fontify regions of text.
30258 Each function is called with one argument POS. Functions must
30259 fontify a region starting at POS in the current buffer, and give
30260 fontified regions the property `fontified'. */);
30261 Vfontification_functions = Qnil;
30262 Fmake_variable_buffer_local (Qfontification_functions);
30263
30264 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30265 unibyte_display_via_language_environment,
30266 doc: /* Non-nil means display unibyte text according to language environment.
30267 Specifically, this means that raw bytes in the range 160-255 decimal
30268 are displayed by converting them to the equivalent multibyte characters
30269 according to the current language environment. As a result, they are
30270 displayed according to the current fontset.
30271
30272 Note that this variable affects only how these bytes are displayed,
30273 but does not change the fact they are interpreted as raw bytes. */);
30274 unibyte_display_via_language_environment = 0;
30275
30276 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30277 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30278 If a float, it specifies a fraction of the mini-window frame's height.
30279 If an integer, it specifies a number of lines. */);
30280 Vmax_mini_window_height = make_float (0.25);
30281
30282 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30283 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30284 A value of nil means don't automatically resize mini-windows.
30285 A value of t means resize them to fit the text displayed in them.
30286 A value of `grow-only', the default, means let mini-windows grow only;
30287 they return to their normal size when the minibuffer is closed, or the
30288 echo area becomes empty. */);
30289 Vresize_mini_windows = Qgrow_only;
30290
30291 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30292 doc: /* Alist specifying how to blink the cursor off.
30293 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30294 `cursor-type' frame-parameter or variable equals ON-STATE,
30295 comparing using `equal', Emacs uses OFF-STATE to specify
30296 how to blink it off. ON-STATE and OFF-STATE are values for
30297 the `cursor-type' frame parameter.
30298
30299 If a frame's ON-STATE has no entry in this list,
30300 the frame's other specifications determine how to blink the cursor off. */);
30301 Vblink_cursor_alist = Qnil;
30302
30303 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30304 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30305 If non-nil, windows are automatically scrolled horizontally to make
30306 point visible. */);
30307 automatic_hscrolling_p = 1;
30308 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30309
30310 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30311 doc: /* How many columns away from the window edge point is allowed to get
30312 before automatic hscrolling will horizontally scroll the window. */);
30313 hscroll_margin = 5;
30314
30315 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30316 doc: /* How many columns to scroll the window when point gets too close to the edge.
30317 When point is less than `hscroll-margin' columns from the window
30318 edge, automatic hscrolling will scroll the window by the amount of columns
30319 determined by this variable. If its value is a positive integer, scroll that
30320 many columns. If it's a positive floating-point number, it specifies the
30321 fraction of the window's width to scroll. If it's nil or zero, point will be
30322 centered horizontally after the scroll. Any other value, including negative
30323 numbers, are treated as if the value were zero.
30324
30325 Automatic hscrolling always moves point outside the scroll margin, so if
30326 point was more than scroll step columns inside the margin, the window will
30327 scroll more than the value given by the scroll step.
30328
30329 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30330 and `scroll-right' overrides this variable's effect. */);
30331 Vhscroll_step = make_number (0);
30332
30333 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30334 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30335 Bind this around calls to `message' to let it take effect. */);
30336 message_truncate_lines = 0;
30337
30338 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30339 doc: /* Normal hook run to update the menu bar definitions.
30340 Redisplay runs this hook before it redisplays the menu bar.
30341 This is used to update menus such as Buffers, whose contents depend on
30342 various data. */);
30343 Vmenu_bar_update_hook = Qnil;
30344
30345 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30346 doc: /* Frame for which we are updating a menu.
30347 The enable predicate for a menu binding should check this variable. */);
30348 Vmenu_updating_frame = Qnil;
30349
30350 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30351 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30352 inhibit_menubar_update = 0;
30353
30354 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30355 doc: /* Prefix prepended to all continuation lines at display time.
30356 The value may be a string, an image, or a stretch-glyph; it is
30357 interpreted in the same way as the value of a `display' text property.
30358
30359 This variable is overridden by any `wrap-prefix' text or overlay
30360 property.
30361
30362 To add a prefix to non-continuation lines, use `line-prefix'. */);
30363 Vwrap_prefix = Qnil;
30364 DEFSYM (Qwrap_prefix, "wrap-prefix");
30365 Fmake_variable_buffer_local (Qwrap_prefix);
30366
30367 DEFVAR_LISP ("line-prefix", Vline_prefix,
30368 doc: /* Prefix prepended to all non-continuation lines at display time.
30369 The value may be a string, an image, or a stretch-glyph; it is
30370 interpreted in the same way as the value of a `display' text property.
30371
30372 This variable is overridden by any `line-prefix' text or overlay
30373 property.
30374
30375 To add a prefix to continuation lines, use `wrap-prefix'. */);
30376 Vline_prefix = Qnil;
30377 DEFSYM (Qline_prefix, "line-prefix");
30378 Fmake_variable_buffer_local (Qline_prefix);
30379
30380 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30381 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30382 inhibit_eval_during_redisplay = 0;
30383
30384 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30385 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30386 inhibit_free_realized_faces = 0;
30387
30388 #ifdef GLYPH_DEBUG
30389 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30390 doc: /* Inhibit try_window_id display optimization. */);
30391 inhibit_try_window_id = 0;
30392
30393 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30394 doc: /* Inhibit try_window_reusing display optimization. */);
30395 inhibit_try_window_reusing = 0;
30396
30397 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30398 doc: /* Inhibit try_cursor_movement display optimization. */);
30399 inhibit_try_cursor_movement = 0;
30400 #endif /* GLYPH_DEBUG */
30401
30402 DEFVAR_INT ("overline-margin", overline_margin,
30403 doc: /* Space between overline and text, in pixels.
30404 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30405 margin to the character height. */);
30406 overline_margin = 2;
30407
30408 DEFVAR_INT ("underline-minimum-offset",
30409 underline_minimum_offset,
30410 doc: /* Minimum distance between baseline and underline.
30411 This can improve legibility of underlined text at small font sizes,
30412 particularly when using variable `x-use-underline-position-properties'
30413 with fonts that specify an UNDERLINE_POSITION relatively close to the
30414 baseline. The default value is 1. */);
30415 underline_minimum_offset = 1;
30416
30417 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30418 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30419 This feature only works when on a window system that can change
30420 cursor shapes. */);
30421 display_hourglass_p = 1;
30422
30423 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30424 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30425 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30426
30427 #ifdef HAVE_WINDOW_SYSTEM
30428 hourglass_atimer = NULL;
30429 hourglass_shown_p = 0;
30430 #endif /* HAVE_WINDOW_SYSTEM */
30431
30432 DEFSYM (Qglyphless_char, "glyphless-char");
30433 DEFSYM (Qhex_code, "hex-code");
30434 DEFSYM (Qempty_box, "empty-box");
30435 DEFSYM (Qthin_space, "thin-space");
30436 DEFSYM (Qzero_width, "zero-width");
30437
30438 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30439 doc: /* Function run just before redisplay.
30440 It is called with one argument, which is the set of windows that are to
30441 be redisplayed. This set can be nil (meaning, only the selected window),
30442 or t (meaning all windows). */);
30443 Vpre_redisplay_function = intern ("ignore");
30444
30445 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30446 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30447
30448 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30449 doc: /* Char-table defining glyphless characters.
30450 Each element, if non-nil, should be one of the following:
30451 an ASCII acronym string: display this string in a box
30452 `hex-code': display the hexadecimal code of a character in a box
30453 `empty-box': display as an empty box
30454 `thin-space': display as 1-pixel width space
30455 `zero-width': don't display
30456 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30457 display method for graphical terminals and text terminals respectively.
30458 GRAPHICAL and TEXT should each have one of the values listed above.
30459
30460 The char-table has one extra slot to control the display of a character for
30461 which no font is found. This slot only takes effect on graphical terminals.
30462 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30463 `thin-space'. The default is `empty-box'.
30464
30465 If a character has a non-nil entry in an active display table, the
30466 display table takes effect; in this case, Emacs does not consult
30467 `glyphless-char-display' at all. */);
30468 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30469 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30470 Qempty_box);
30471
30472 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30473 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30474 Vdebug_on_message = Qnil;
30475
30476 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30477 doc: /* */);
30478 Vredisplay__all_windows_cause
30479 = Fmake_vector (make_number (100), make_number (0));
30480
30481 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30482 doc: /* */);
30483 Vredisplay__mode_lines_cause
30484 = Fmake_vector (make_number (100), make_number (0));
30485 }
30486
30487
30488 /* Initialize this module when Emacs starts. */
30489
30490 void
30491 init_xdisp (void)
30492 {
30493 CHARPOS (this_line_start_pos) = 0;
30494
30495 if (!noninteractive)
30496 {
30497 struct window *m = XWINDOW (minibuf_window);
30498 Lisp_Object frame = m->frame;
30499 struct frame *f = XFRAME (frame);
30500 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30501 struct window *r = XWINDOW (root);
30502 int i;
30503
30504 echo_area_window = minibuf_window;
30505
30506 r->top_line = FRAME_TOP_MARGIN (f);
30507 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30508 r->total_cols = FRAME_COLS (f);
30509 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30510 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30511 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30512
30513 m->top_line = FRAME_LINES (f) - 1;
30514 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30515 m->total_cols = FRAME_COLS (f);
30516 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30517 m->total_lines = 1;
30518 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30519
30520 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30521 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30522 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30523
30524 /* The default ellipsis glyphs `...'. */
30525 for (i = 0; i < 3; ++i)
30526 default_invis_vector[i] = make_number ('.');
30527 }
30528
30529 {
30530 /* Allocate the buffer for frame titles.
30531 Also used for `format-mode-line'. */
30532 int size = 100;
30533 mode_line_noprop_buf = xmalloc (size);
30534 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30535 mode_line_noprop_ptr = mode_line_noprop_buf;
30536 mode_line_target = MODE_LINE_DISPLAY;
30537 }
30538
30539 help_echo_showing_p = 0;
30540 }
30541
30542 #ifdef HAVE_WINDOW_SYSTEM
30543
30544 /* Platform-independent portion of hourglass implementation. */
30545
30546 /* Cancel a currently active hourglass timer, and start a new one. */
30547 void
30548 start_hourglass (void)
30549 {
30550 struct timespec delay;
30551
30552 cancel_hourglass ();
30553
30554 if (INTEGERP (Vhourglass_delay)
30555 && XINT (Vhourglass_delay) > 0)
30556 delay = make_timespec (min (XINT (Vhourglass_delay),
30557 TYPE_MAXIMUM (time_t)),
30558 0);
30559 else if (FLOATP (Vhourglass_delay)
30560 && XFLOAT_DATA (Vhourglass_delay) > 0)
30561 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30562 else
30563 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30564
30565 #ifdef HAVE_NTGUI
30566 {
30567 extern void w32_note_current_window (void);
30568 w32_note_current_window ();
30569 }
30570 #endif /* HAVE_NTGUI */
30571
30572 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30573 show_hourglass, NULL);
30574 }
30575
30576
30577 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30578 shown. */
30579 void
30580 cancel_hourglass (void)
30581 {
30582 if (hourglass_atimer)
30583 {
30584 cancel_atimer (hourglass_atimer);
30585 hourglass_atimer = NULL;
30586 }
30587
30588 if (hourglass_shown_p)
30589 hide_hourglass ();
30590 }
30591
30592 #endif /* HAVE_WINDOW_SYSTEM */