Follow-up improvements for last change in gmalloc.c.
[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 {
8656 ++it->hpos;
8657 it->current_x = new_x;
8658
8659 /* The character's last glyph just barely fits
8660 in this row. */
8661 if (i == it->nglyphs - 1)
8662 {
8663 /* If this is the destination position,
8664 return a position *before* it in this row,
8665 now that we know it fits in this row. */
8666 if (BUFFER_POS_REACHED_P ())
8667 {
8668 if (it->line_wrap != WORD_WRAP
8669 || wrap_it.sp < 0)
8670 {
8671 it->hpos = hpos_before_this_char;
8672 it->current_x = x_before_this_char;
8673 result = MOVE_POS_MATCH_OR_ZV;
8674 break;
8675 }
8676 if (it->line_wrap == WORD_WRAP
8677 && atpos_it.sp < 0)
8678 {
8679 SAVE_IT (atpos_it, *it, atpos_data);
8680 atpos_it.current_x = x_before_this_char;
8681 atpos_it.hpos = hpos_before_this_char;
8682 }
8683 }
8684
8685 prev_method = it->method;
8686 if (it->method == GET_FROM_BUFFER)
8687 prev_pos = IT_CHARPOS (*it);
8688 set_iterator_to_next (it, 1);
8689 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8690 SET_TEXT_POS (this_line_min_pos,
8691 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8692 /* On graphical terminals, newlines may
8693 "overflow" into the fringe if
8694 overflow-newline-into-fringe is non-nil.
8695 On text terminals, and on graphical
8696 terminals with no right margin, newlines
8697 may overflow into the last glyph on the
8698 display line.*/
8699 if (!FRAME_WINDOW_P (it->f)
8700 || ((it->bidi_p
8701 && it->bidi_it.paragraph_dir == R2L)
8702 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8703 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8704 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8705 {
8706 if (!get_next_display_element (it))
8707 {
8708 result = MOVE_POS_MATCH_OR_ZV;
8709 break;
8710 }
8711 if (BUFFER_POS_REACHED_P ())
8712 {
8713 if (ITERATOR_AT_END_OF_LINE_P (it))
8714 result = MOVE_POS_MATCH_OR_ZV;
8715 else
8716 result = MOVE_LINE_CONTINUED;
8717 break;
8718 }
8719 if (ITERATOR_AT_END_OF_LINE_P (it)
8720 && (it->line_wrap != WORD_WRAP
8721 || wrap_it.sp < 0))
8722 {
8723 result = MOVE_NEWLINE_OR_CR;
8724 break;
8725 }
8726 }
8727 }
8728 }
8729 else
8730 IT_RESET_X_ASCENT_DESCENT (it);
8731
8732 if (wrap_it.sp >= 0)
8733 {
8734 RESTORE_IT (it, &wrap_it, wrap_data);
8735 atpos_it.sp = -1;
8736 atx_it.sp = -1;
8737 }
8738
8739 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8740 IT_CHARPOS (*it)));
8741 result = MOVE_LINE_CONTINUED;
8742 break;
8743 }
8744
8745 if (BUFFER_POS_REACHED_P ())
8746 {
8747 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8748 goto buffer_pos_reached;
8749 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8750 {
8751 SAVE_IT (atpos_it, *it, atpos_data);
8752 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8753 }
8754 }
8755
8756 if (new_x > it->first_visible_x)
8757 {
8758 /* Glyph is visible. Increment number of glyphs that
8759 would be displayed. */
8760 ++it->hpos;
8761 }
8762 }
8763
8764 if (result != MOVE_UNDEFINED)
8765 break;
8766 }
8767 else if (BUFFER_POS_REACHED_P ())
8768 {
8769 buffer_pos_reached:
8770 IT_RESET_X_ASCENT_DESCENT (it);
8771 result = MOVE_POS_MATCH_OR_ZV;
8772 break;
8773 }
8774 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8775 {
8776 /* Stop when TO_X specified and reached. This check is
8777 necessary here because of lines consisting of a line end,
8778 only. The line end will not produce any glyphs and we
8779 would never get MOVE_X_REACHED. */
8780 eassert (it->nglyphs == 0);
8781 result = MOVE_X_REACHED;
8782 break;
8783 }
8784
8785 /* Is this a line end? If yes, we're done. */
8786 if (ITERATOR_AT_END_OF_LINE_P (it))
8787 {
8788 /* If we are past TO_CHARPOS, but never saw any character
8789 positions smaller than TO_CHARPOS, return
8790 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8791 did. */
8792 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8793 {
8794 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8795 {
8796 if (closest_pos < ZV)
8797 {
8798 RESTORE_IT (it, &ppos_it, ppos_data);
8799 move_it_in_display_line_to (it, closest_pos, -1,
8800 MOVE_TO_POS);
8801 result = MOVE_POS_MATCH_OR_ZV;
8802 }
8803 else
8804 goto buffer_pos_reached;
8805 }
8806 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8807 && IT_CHARPOS (*it) > to_charpos)
8808 goto buffer_pos_reached;
8809 else
8810 result = MOVE_NEWLINE_OR_CR;
8811 }
8812 else
8813 result = MOVE_NEWLINE_OR_CR;
8814 break;
8815 }
8816
8817 prev_method = it->method;
8818 if (it->method == GET_FROM_BUFFER)
8819 prev_pos = IT_CHARPOS (*it);
8820 /* The current display element has been consumed. Advance
8821 to the next. */
8822 set_iterator_to_next (it, 1);
8823 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8824 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8825 if (IT_CHARPOS (*it) < to_charpos)
8826 saw_smaller_pos = 1;
8827 if (it->bidi_p
8828 && (op & MOVE_TO_POS)
8829 && IT_CHARPOS (*it) >= to_charpos
8830 && IT_CHARPOS (*it) < closest_pos)
8831 closest_pos = IT_CHARPOS (*it);
8832
8833 /* Stop if lines are truncated and IT's current x-position is
8834 past the right edge of the window now. */
8835 if (it->line_wrap == TRUNCATE
8836 && it->current_x >= it->last_visible_x)
8837 {
8838 if (!FRAME_WINDOW_P (it->f)
8839 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8840 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8841 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8842 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8843 {
8844 int at_eob_p = 0;
8845
8846 if ((at_eob_p = !get_next_display_element (it))
8847 || BUFFER_POS_REACHED_P ()
8848 /* If we are past TO_CHARPOS, but never saw any
8849 character positions smaller than TO_CHARPOS,
8850 return MOVE_POS_MATCH_OR_ZV, like the
8851 unidirectional display did. */
8852 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8853 && !saw_smaller_pos
8854 && IT_CHARPOS (*it) > to_charpos))
8855 {
8856 if (it->bidi_p
8857 && !BUFFER_POS_REACHED_P ()
8858 && !at_eob_p && closest_pos < ZV)
8859 {
8860 RESTORE_IT (it, &ppos_it, ppos_data);
8861 move_it_in_display_line_to (it, closest_pos, -1,
8862 MOVE_TO_POS);
8863 }
8864 result = MOVE_POS_MATCH_OR_ZV;
8865 break;
8866 }
8867 if (ITERATOR_AT_END_OF_LINE_P (it))
8868 {
8869 result = MOVE_NEWLINE_OR_CR;
8870 break;
8871 }
8872 }
8873 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8874 && !saw_smaller_pos
8875 && IT_CHARPOS (*it) > to_charpos)
8876 {
8877 if (closest_pos < ZV)
8878 {
8879 RESTORE_IT (it, &ppos_it, ppos_data);
8880 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8881 }
8882 result = MOVE_POS_MATCH_OR_ZV;
8883 break;
8884 }
8885 result = MOVE_LINE_TRUNCATED;
8886 break;
8887 }
8888 #undef IT_RESET_X_ASCENT_DESCENT
8889 }
8890
8891 #undef BUFFER_POS_REACHED_P
8892
8893 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8894 restore the saved iterator. */
8895 if (atpos_it.sp >= 0)
8896 RESTORE_IT (it, &atpos_it, atpos_data);
8897 else if (atx_it.sp >= 0)
8898 RESTORE_IT (it, &atx_it, atx_data);
8899
8900 done:
8901
8902 if (atpos_data)
8903 bidi_unshelve_cache (atpos_data, 1);
8904 if (atx_data)
8905 bidi_unshelve_cache (atx_data, 1);
8906 if (wrap_data)
8907 bidi_unshelve_cache (wrap_data, 1);
8908 if (ppos_data)
8909 bidi_unshelve_cache (ppos_data, 1);
8910
8911 /* Restore the iterator settings altered at the beginning of this
8912 function. */
8913 it->glyph_row = saved_glyph_row;
8914 return result;
8915 }
8916
8917 /* For external use. */
8918 void
8919 move_it_in_display_line (struct it *it,
8920 ptrdiff_t to_charpos, int to_x,
8921 enum move_operation_enum op)
8922 {
8923 if (it->line_wrap == WORD_WRAP
8924 && (op & MOVE_TO_X))
8925 {
8926 struct it save_it;
8927 void *save_data = NULL;
8928 int skip;
8929
8930 SAVE_IT (save_it, *it, save_data);
8931 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8932 /* When word-wrap is on, TO_X may lie past the end
8933 of a wrapped line. Then it->current is the
8934 character on the next line, so backtrack to the
8935 space before the wrap point. */
8936 if (skip == MOVE_LINE_CONTINUED)
8937 {
8938 int prev_x = max (it->current_x - 1, 0);
8939 RESTORE_IT (it, &save_it, save_data);
8940 move_it_in_display_line_to
8941 (it, -1, prev_x, MOVE_TO_X);
8942 }
8943 else
8944 bidi_unshelve_cache (save_data, 1);
8945 }
8946 else
8947 move_it_in_display_line_to (it, to_charpos, to_x, op);
8948 }
8949
8950
8951 /* Move IT forward until it satisfies one or more of the criteria in
8952 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8953
8954 OP is a bit-mask that specifies where to stop, and in particular,
8955 which of those four position arguments makes a difference. See the
8956 description of enum move_operation_enum.
8957
8958 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8959 screen line, this function will set IT to the next position that is
8960 displayed to the right of TO_CHARPOS on the screen.
8961
8962 Return the maximum pixel length of any line scanned but never more
8963 than it.last_visible_x. */
8964
8965 int
8966 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8967 {
8968 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8969 int line_height, line_start_x = 0, reached = 0;
8970 int max_current_x = 0;
8971 void *backup_data = NULL;
8972
8973 for (;;)
8974 {
8975 if (op & MOVE_TO_VPOS)
8976 {
8977 /* If no TO_CHARPOS and no TO_X specified, stop at the
8978 start of the line TO_VPOS. */
8979 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8980 {
8981 if (it->vpos == to_vpos)
8982 {
8983 reached = 1;
8984 break;
8985 }
8986 else
8987 skip = move_it_in_display_line_to (it, -1, -1, 0);
8988 }
8989 else
8990 {
8991 /* TO_VPOS >= 0 means stop at TO_X in the line at
8992 TO_VPOS, or at TO_POS, whichever comes first. */
8993 if (it->vpos == to_vpos)
8994 {
8995 reached = 2;
8996 break;
8997 }
8998
8999 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9000
9001 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9002 {
9003 reached = 3;
9004 break;
9005 }
9006 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9007 {
9008 /* We have reached TO_X but not in the line we want. */
9009 skip = move_it_in_display_line_to (it, to_charpos,
9010 -1, MOVE_TO_POS);
9011 if (skip == MOVE_POS_MATCH_OR_ZV)
9012 {
9013 reached = 4;
9014 break;
9015 }
9016 }
9017 }
9018 }
9019 else if (op & MOVE_TO_Y)
9020 {
9021 struct it it_backup;
9022
9023 if (it->line_wrap == WORD_WRAP)
9024 SAVE_IT (it_backup, *it, backup_data);
9025
9026 /* TO_Y specified means stop at TO_X in the line containing
9027 TO_Y---or at TO_CHARPOS if this is reached first. The
9028 problem is that we can't really tell whether the line
9029 contains TO_Y before we have completely scanned it, and
9030 this may skip past TO_X. What we do is to first scan to
9031 TO_X.
9032
9033 If TO_X is not specified, use a TO_X of zero. The reason
9034 is to make the outcome of this function more predictable.
9035 If we didn't use TO_X == 0, we would stop at the end of
9036 the line which is probably not what a caller would expect
9037 to happen. */
9038 skip = move_it_in_display_line_to
9039 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9040 (MOVE_TO_X | (op & MOVE_TO_POS)));
9041
9042 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9043 if (skip == MOVE_POS_MATCH_OR_ZV)
9044 reached = 5;
9045 else if (skip == MOVE_X_REACHED)
9046 {
9047 /* If TO_X was reached, we want to know whether TO_Y is
9048 in the line. We know this is the case if the already
9049 scanned glyphs make the line tall enough. Otherwise,
9050 we must check by scanning the rest of the line. */
9051 line_height = it->max_ascent + it->max_descent;
9052 if (to_y >= it->current_y
9053 && to_y < it->current_y + line_height)
9054 {
9055 reached = 6;
9056 break;
9057 }
9058 SAVE_IT (it_backup, *it, backup_data);
9059 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9060 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9061 op & MOVE_TO_POS);
9062 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9063 line_height = it->max_ascent + it->max_descent;
9064 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9065
9066 if (to_y >= it->current_y
9067 && to_y < it->current_y + line_height)
9068 {
9069 /* If TO_Y is in this line and TO_X was reached
9070 above, we scanned too far. We have to restore
9071 IT's settings to the ones before skipping. But
9072 keep the more accurate values of max_ascent and
9073 max_descent we've found while skipping the rest
9074 of the line, for the sake of callers, such as
9075 pos_visible_p, that need to know the line
9076 height. */
9077 int max_ascent = it->max_ascent;
9078 int max_descent = it->max_descent;
9079
9080 RESTORE_IT (it, &it_backup, backup_data);
9081 it->max_ascent = max_ascent;
9082 it->max_descent = max_descent;
9083 reached = 6;
9084 }
9085 else
9086 {
9087 skip = skip2;
9088 if (skip == MOVE_POS_MATCH_OR_ZV)
9089 reached = 7;
9090 }
9091 }
9092 else
9093 {
9094 /* Check whether TO_Y is in this line. */
9095 line_height = it->max_ascent + it->max_descent;
9096 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9097
9098 if (to_y >= it->current_y
9099 && to_y < it->current_y + line_height)
9100 {
9101 if (to_y > it->current_y)
9102 max_current_x = max (it->current_x, max_current_x);
9103
9104 /* When word-wrap is on, TO_X may lie past the end
9105 of a wrapped line. Then it->current is the
9106 character on the next line, so backtrack to the
9107 space before the wrap point. */
9108 if (skip == MOVE_LINE_CONTINUED
9109 && it->line_wrap == WORD_WRAP)
9110 {
9111 int prev_x = max (it->current_x - 1, 0);
9112 RESTORE_IT (it, &it_backup, backup_data);
9113 skip = move_it_in_display_line_to
9114 (it, -1, prev_x, MOVE_TO_X);
9115 }
9116
9117 reached = 6;
9118 }
9119 }
9120
9121 if (reached)
9122 {
9123 max_current_x = max (it->current_x, max_current_x);
9124 break;
9125 }
9126 }
9127 else if (BUFFERP (it->object)
9128 && (it->method == GET_FROM_BUFFER
9129 || it->method == GET_FROM_STRETCH)
9130 && IT_CHARPOS (*it) >= to_charpos
9131 /* Under bidi iteration, a call to set_iterator_to_next
9132 can scan far beyond to_charpos if the initial
9133 portion of the next line needs to be reordered. In
9134 that case, give move_it_in_display_line_to another
9135 chance below. */
9136 && !(it->bidi_p
9137 && it->bidi_it.scan_dir == -1))
9138 skip = MOVE_POS_MATCH_OR_ZV;
9139 else
9140 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9141
9142 switch (skip)
9143 {
9144 case MOVE_POS_MATCH_OR_ZV:
9145 max_current_x = max (it->current_x, max_current_x);
9146 reached = 8;
9147 goto out;
9148
9149 case MOVE_NEWLINE_OR_CR:
9150 max_current_x = max (it->current_x, max_current_x);
9151 set_iterator_to_next (it, 1);
9152 it->continuation_lines_width = 0;
9153 break;
9154
9155 case MOVE_LINE_TRUNCATED:
9156 max_current_x = it->last_visible_x;
9157 it->continuation_lines_width = 0;
9158 reseat_at_next_visible_line_start (it, 0);
9159 if ((op & MOVE_TO_POS) != 0
9160 && IT_CHARPOS (*it) > to_charpos)
9161 {
9162 reached = 9;
9163 goto out;
9164 }
9165 break;
9166
9167 case MOVE_LINE_CONTINUED:
9168 max_current_x = it->last_visible_x;
9169 /* For continued lines ending in a tab, some of the glyphs
9170 associated with the tab are displayed on the current
9171 line. Since it->current_x does not include these glyphs,
9172 we use it->last_visible_x instead. */
9173 if (it->c == '\t')
9174 {
9175 it->continuation_lines_width += it->last_visible_x;
9176 /* When moving by vpos, ensure that the iterator really
9177 advances to the next line (bug#847, bug#969). Fixme:
9178 do we need to do this in other circumstances? */
9179 if (it->current_x != it->last_visible_x
9180 && (op & MOVE_TO_VPOS)
9181 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9182 {
9183 line_start_x = it->current_x + it->pixel_width
9184 - it->last_visible_x;
9185 set_iterator_to_next (it, 0);
9186 }
9187 }
9188 else
9189 it->continuation_lines_width += it->current_x;
9190 break;
9191
9192 default:
9193 emacs_abort ();
9194 }
9195
9196 /* Reset/increment for the next run. */
9197 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9198 it->current_x = line_start_x;
9199 line_start_x = 0;
9200 it->hpos = 0;
9201 it->current_y += it->max_ascent + it->max_descent;
9202 ++it->vpos;
9203 last_height = it->max_ascent + it->max_descent;
9204 it->max_ascent = it->max_descent = 0;
9205 }
9206
9207 out:
9208
9209 /* On text terminals, we may stop at the end of a line in the middle
9210 of a multi-character glyph. If the glyph itself is continued,
9211 i.e. it is actually displayed on the next line, don't treat this
9212 stopping point as valid; move to the next line instead (unless
9213 that brings us offscreen). */
9214 if (!FRAME_WINDOW_P (it->f)
9215 && op & MOVE_TO_POS
9216 && IT_CHARPOS (*it) == to_charpos
9217 && it->what == IT_CHARACTER
9218 && it->nglyphs > 1
9219 && it->line_wrap == WINDOW_WRAP
9220 && it->current_x == it->last_visible_x - 1
9221 && it->c != '\n'
9222 && it->c != '\t'
9223 && it->vpos < it->w->window_end_vpos)
9224 {
9225 it->continuation_lines_width += it->current_x;
9226 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9227 it->current_y += it->max_ascent + it->max_descent;
9228 ++it->vpos;
9229 last_height = it->max_ascent + it->max_descent;
9230 }
9231
9232 if (backup_data)
9233 bidi_unshelve_cache (backup_data, 1);
9234
9235 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9236
9237 return max_current_x;
9238 }
9239
9240
9241 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9242
9243 If DY > 0, move IT backward at least that many pixels. DY = 0
9244 means move IT backward to the preceding line start or BEGV. This
9245 function may move over more than DY pixels if IT->current_y - DY
9246 ends up in the middle of a line; in this case IT->current_y will be
9247 set to the top of the line moved to. */
9248
9249 void
9250 move_it_vertically_backward (struct it *it, int dy)
9251 {
9252 int nlines, h;
9253 struct it it2, it3;
9254 void *it2data = NULL, *it3data = NULL;
9255 ptrdiff_t start_pos;
9256 int nchars_per_row
9257 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9258 ptrdiff_t pos_limit;
9259
9260 move_further_back:
9261 eassert (dy >= 0);
9262
9263 start_pos = IT_CHARPOS (*it);
9264
9265 /* Estimate how many newlines we must move back. */
9266 nlines = max (1, dy / default_line_pixel_height (it->w));
9267 if (it->line_wrap == TRUNCATE)
9268 pos_limit = BEGV;
9269 else
9270 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9271
9272 /* Set the iterator's position that many lines back. But don't go
9273 back more than NLINES full screen lines -- this wins a day with
9274 buffers which have very long lines. */
9275 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9276 back_to_previous_visible_line_start (it);
9277
9278 /* Reseat the iterator here. When moving backward, we don't want
9279 reseat to skip forward over invisible text, set up the iterator
9280 to deliver from overlay strings at the new position etc. So,
9281 use reseat_1 here. */
9282 reseat_1 (it, it->current.pos, 1);
9283
9284 /* We are now surely at a line start. */
9285 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9286 reordering is in effect. */
9287 it->continuation_lines_width = 0;
9288
9289 /* Move forward and see what y-distance we moved. First move to the
9290 start of the next line so that we get its height. We need this
9291 height to be able to tell whether we reached the specified
9292 y-distance. */
9293 SAVE_IT (it2, *it, it2data);
9294 it2.max_ascent = it2.max_descent = 0;
9295 do
9296 {
9297 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9298 MOVE_TO_POS | MOVE_TO_VPOS);
9299 }
9300 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9301 /* If we are in a display string which starts at START_POS,
9302 and that display string includes a newline, and we are
9303 right after that newline (i.e. at the beginning of a
9304 display line), exit the loop, because otherwise we will
9305 infloop, since move_it_to will see that it is already at
9306 START_POS and will not move. */
9307 || (it2.method == GET_FROM_STRING
9308 && IT_CHARPOS (it2) == start_pos
9309 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9310 eassert (IT_CHARPOS (*it) >= BEGV);
9311 SAVE_IT (it3, it2, it3data);
9312
9313 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9314 eassert (IT_CHARPOS (*it) >= BEGV);
9315 /* H is the actual vertical distance from the position in *IT
9316 and the starting position. */
9317 h = it2.current_y - it->current_y;
9318 /* NLINES is the distance in number of lines. */
9319 nlines = it2.vpos - it->vpos;
9320
9321 /* Correct IT's y and vpos position
9322 so that they are relative to the starting point. */
9323 it->vpos -= nlines;
9324 it->current_y -= h;
9325
9326 if (dy == 0)
9327 {
9328 /* DY == 0 means move to the start of the screen line. The
9329 value of nlines is > 0 if continuation lines were involved,
9330 or if the original IT position was at start of a line. */
9331 RESTORE_IT (it, it, it2data);
9332 if (nlines > 0)
9333 move_it_by_lines (it, nlines);
9334 /* The above code moves us to some position NLINES down,
9335 usually to its first glyph (leftmost in an L2R line), but
9336 that's not necessarily the start of the line, under bidi
9337 reordering. We want to get to the character position
9338 that is immediately after the newline of the previous
9339 line. */
9340 if (it->bidi_p
9341 && !it->continuation_lines_width
9342 && !STRINGP (it->string)
9343 && IT_CHARPOS (*it) > BEGV
9344 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9345 {
9346 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9347
9348 DEC_BOTH (cp, bp);
9349 cp = find_newline_no_quit (cp, bp, -1, NULL);
9350 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9351 }
9352 bidi_unshelve_cache (it3data, 1);
9353 }
9354 else
9355 {
9356 /* The y-position we try to reach, relative to *IT.
9357 Note that H has been subtracted in front of the if-statement. */
9358 int target_y = it->current_y + h - dy;
9359 int y0 = it3.current_y;
9360 int y1;
9361 int line_height;
9362
9363 RESTORE_IT (&it3, &it3, it3data);
9364 y1 = line_bottom_y (&it3);
9365 line_height = y1 - y0;
9366 RESTORE_IT (it, it, it2data);
9367 /* If we did not reach target_y, try to move further backward if
9368 we can. If we moved too far backward, try to move forward. */
9369 if (target_y < it->current_y
9370 /* This is heuristic. In a window that's 3 lines high, with
9371 a line height of 13 pixels each, recentering with point
9372 on the bottom line will try to move -39/2 = 19 pixels
9373 backward. Try to avoid moving into the first line. */
9374 && (it->current_y - target_y
9375 > min (window_box_height (it->w), line_height * 2 / 3))
9376 && IT_CHARPOS (*it) > BEGV)
9377 {
9378 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9379 target_y - it->current_y));
9380 dy = it->current_y - target_y;
9381 goto move_further_back;
9382 }
9383 else if (target_y >= it->current_y + line_height
9384 && IT_CHARPOS (*it) < ZV)
9385 {
9386 /* Should move forward by at least one line, maybe more.
9387
9388 Note: Calling move_it_by_lines can be expensive on
9389 terminal frames, where compute_motion is used (via
9390 vmotion) to do the job, when there are very long lines
9391 and truncate-lines is nil. That's the reason for
9392 treating terminal frames specially here. */
9393
9394 if (!FRAME_WINDOW_P (it->f))
9395 move_it_vertically (it, target_y - (it->current_y + line_height));
9396 else
9397 {
9398 do
9399 {
9400 move_it_by_lines (it, 1);
9401 }
9402 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9403 }
9404 }
9405 }
9406 }
9407
9408
9409 /* Move IT by a specified amount of pixel lines DY. DY negative means
9410 move backwards. DY = 0 means move to start of screen line. At the
9411 end, IT will be on the start of a screen line. */
9412
9413 void
9414 move_it_vertically (struct it *it, int dy)
9415 {
9416 if (dy <= 0)
9417 move_it_vertically_backward (it, -dy);
9418 else
9419 {
9420 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9421 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9422 MOVE_TO_POS | MOVE_TO_Y);
9423 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9424
9425 /* If buffer ends in ZV without a newline, move to the start of
9426 the line to satisfy the post-condition. */
9427 if (IT_CHARPOS (*it) == ZV
9428 && ZV > BEGV
9429 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9430 move_it_by_lines (it, 0);
9431 }
9432 }
9433
9434
9435 /* Move iterator IT past the end of the text line it is in. */
9436
9437 void
9438 move_it_past_eol (struct it *it)
9439 {
9440 enum move_it_result rc;
9441
9442 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9443 if (rc == MOVE_NEWLINE_OR_CR)
9444 set_iterator_to_next (it, 0);
9445 }
9446
9447
9448 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9449 negative means move up. DVPOS == 0 means move to the start of the
9450 screen line.
9451
9452 Optimization idea: If we would know that IT->f doesn't use
9453 a face with proportional font, we could be faster for
9454 truncate-lines nil. */
9455
9456 void
9457 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9458 {
9459
9460 /* The commented-out optimization uses vmotion on terminals. This
9461 gives bad results, because elements like it->what, on which
9462 callers such as pos_visible_p rely, aren't updated. */
9463 /* struct position pos;
9464 if (!FRAME_WINDOW_P (it->f))
9465 {
9466 struct text_pos textpos;
9467
9468 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9469 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9470 reseat (it, textpos, 1);
9471 it->vpos += pos.vpos;
9472 it->current_y += pos.vpos;
9473 }
9474 else */
9475
9476 if (dvpos == 0)
9477 {
9478 /* DVPOS == 0 means move to the start of the screen line. */
9479 move_it_vertically_backward (it, 0);
9480 /* Let next call to line_bottom_y calculate real line height. */
9481 last_height = 0;
9482 }
9483 else if (dvpos > 0)
9484 {
9485 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9486 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9487 {
9488 /* Only move to the next buffer position if we ended up in a
9489 string from display property, not in an overlay string
9490 (before-string or after-string). That is because the
9491 latter don't conceal the underlying buffer position, so
9492 we can ask to move the iterator to the exact position we
9493 are interested in. Note that, even if we are already at
9494 IT_CHARPOS (*it), the call below is not a no-op, as it
9495 will detect that we are at the end of the string, pop the
9496 iterator, and compute it->current_x and it->hpos
9497 correctly. */
9498 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9499 -1, -1, -1, MOVE_TO_POS);
9500 }
9501 }
9502 else
9503 {
9504 struct it it2;
9505 void *it2data = NULL;
9506 ptrdiff_t start_charpos, i;
9507 int nchars_per_row
9508 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9509 ptrdiff_t pos_limit;
9510
9511 /* Start at the beginning of the screen line containing IT's
9512 position. This may actually move vertically backwards,
9513 in case of overlays, so adjust dvpos accordingly. */
9514 dvpos += it->vpos;
9515 move_it_vertically_backward (it, 0);
9516 dvpos -= it->vpos;
9517
9518 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9519 screen lines, and reseat the iterator there. */
9520 start_charpos = IT_CHARPOS (*it);
9521 if (it->line_wrap == TRUNCATE)
9522 pos_limit = BEGV;
9523 else
9524 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9525 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9526 back_to_previous_visible_line_start (it);
9527 reseat (it, it->current.pos, 1);
9528
9529 /* Move further back if we end up in a string or an image. */
9530 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9531 {
9532 /* First try to move to start of display line. */
9533 dvpos += it->vpos;
9534 move_it_vertically_backward (it, 0);
9535 dvpos -= it->vpos;
9536 if (IT_POS_VALID_AFTER_MOVE_P (it))
9537 break;
9538 /* If start of line is still in string or image,
9539 move further back. */
9540 back_to_previous_visible_line_start (it);
9541 reseat (it, it->current.pos, 1);
9542 dvpos--;
9543 }
9544
9545 it->current_x = it->hpos = 0;
9546
9547 /* Above call may have moved too far if continuation lines
9548 are involved. Scan forward and see if it did. */
9549 SAVE_IT (it2, *it, it2data);
9550 it2.vpos = it2.current_y = 0;
9551 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9552 it->vpos -= it2.vpos;
9553 it->current_y -= it2.current_y;
9554 it->current_x = it->hpos = 0;
9555
9556 /* If we moved too far back, move IT some lines forward. */
9557 if (it2.vpos > -dvpos)
9558 {
9559 int delta = it2.vpos + dvpos;
9560
9561 RESTORE_IT (&it2, &it2, it2data);
9562 SAVE_IT (it2, *it, it2data);
9563 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9564 /* Move back again if we got too far ahead. */
9565 if (IT_CHARPOS (*it) >= start_charpos)
9566 RESTORE_IT (it, &it2, it2data);
9567 else
9568 bidi_unshelve_cache (it2data, 1);
9569 }
9570 else
9571 RESTORE_IT (it, it, it2data);
9572 }
9573 }
9574
9575 /* Return true if IT points into the middle of a display vector. */
9576
9577 bool
9578 in_display_vector_p (struct it *it)
9579 {
9580 return (it->method == GET_FROM_DISPLAY_VECTOR
9581 && it->current.dpvec_index > 0
9582 && it->dpvec + it->current.dpvec_index != it->dpend);
9583 }
9584
9585 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9586 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9587 WINDOW must be a live window and defaults to the selected one. The
9588 return value is a cons of the maximum pixel-width of any text line and
9589 the maximum pixel-height of all text lines.
9590
9591 The optional argument FROM, if non-nil, specifies the first text
9592 position and defaults to the minimum accessible position of the buffer.
9593 If FROM is t, use the minimum accessible position that is not a newline
9594 character. TO, if non-nil, specifies the last text position and
9595 defaults to the maximum accessible position of the buffer. If TO is t,
9596 use the maximum accessible position that is not a newline character.
9597
9598 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9599 width that can be returned. X_LIMIT nil or omitted, means to use the
9600 pixel-width of WINDOW's body; use this if you do not intend to change
9601 the width of WINDOW. Use the maximum width WINDOW may assume if you
9602 intend to change WINDOW's width.
9603
9604 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9605 height that can be returned. Text lines whose y-coordinate is beyond
9606 Y_LIMIT are ignored. Since calculating the text height of a large
9607 buffer can take some time, it makes sense to specify this argument if
9608 the size of the buffer is unknown.
9609
9610 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9611 include the height of the mode- or header-line of WINDOW in the return
9612 value. If it is either the symbol `mode-line' or `header-line', include
9613 only the height of that line, if present, in the return value. If t,
9614 include the height of both, if present, in the return value. */)
9615 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9616 Lisp_Object mode_and_header_line)
9617 {
9618 struct window *w = decode_live_window (window);
9619 Lisp_Object buf;
9620 struct buffer *b;
9621 struct it it;
9622 struct buffer *old_buffer = NULL;
9623 ptrdiff_t start, end, pos;
9624 struct text_pos startp;
9625 void *itdata = NULL;
9626 int c, max_y = -1, x = 0, y = 0;
9627
9628 buf = w->contents;
9629 CHECK_BUFFER (buf);
9630 b = XBUFFER (buf);
9631
9632 if (b != current_buffer)
9633 {
9634 old_buffer = current_buffer;
9635 set_buffer_internal (b);
9636 }
9637
9638 if (NILP (from))
9639 start = BEGV;
9640 else if (EQ (from, Qt))
9641 {
9642 start = pos = BEGV;
9643 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9644 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9645 start = pos;
9646 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9647 start = pos;
9648 }
9649 else
9650 {
9651 CHECK_NUMBER_COERCE_MARKER (from);
9652 start = min (max (XINT (from), BEGV), ZV);
9653 }
9654
9655 if (NILP (to))
9656 end = ZV;
9657 else if (EQ (to, Qt))
9658 {
9659 end = pos = ZV;
9660 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9661 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9662 end = pos;
9663 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9664 end = pos;
9665 }
9666 else
9667 {
9668 CHECK_NUMBER_COERCE_MARKER (to);
9669 end = max (start, min (XINT (to), ZV));
9670 }
9671
9672 if (!NILP (y_limit))
9673 {
9674 CHECK_NUMBER (y_limit);
9675 max_y = min (XINT (y_limit), INT_MAX);
9676 }
9677
9678 itdata = bidi_shelve_cache ();
9679 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9680 start_display (&it, w, startp);
9681
9682 if (NILP (x_limit))
9683 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9684 else
9685 {
9686 CHECK_NUMBER (x_limit);
9687 it.last_visible_x = min (XINT (x_limit), INFINITY);
9688 /* Actually, we never want move_it_to stop at to_x. But to make
9689 sure that move_it_in_display_line_to always moves far enough,
9690 we set it to INT_MAX and specify MOVE_TO_X. */
9691 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9692 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9693 }
9694
9695 y = it.current_y + it.max_ascent + it.max_descent;
9696
9697 if (!EQ (mode_and_header_line, Qheader_line)
9698 && !EQ (mode_and_header_line, Qt))
9699 /* Do not count the header-line which was counted automatically by
9700 start_display. */
9701 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9702
9703 if (EQ (mode_and_header_line, Qmode_line)
9704 || EQ (mode_and_header_line, Qt))
9705 /* Do count the mode-line which is not included automatically by
9706 start_display. */
9707 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9708
9709 bidi_unshelve_cache (itdata, 0);
9710
9711 if (old_buffer)
9712 set_buffer_internal (old_buffer);
9713
9714 return Fcons (make_number (x), make_number (y));
9715 }
9716 \f
9717 /***********************************************************************
9718 Messages
9719 ***********************************************************************/
9720
9721
9722 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9723 to *Messages*. */
9724
9725 void
9726 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9727 {
9728 Lisp_Object args[3];
9729 Lisp_Object msg, fmt;
9730 char *buffer;
9731 ptrdiff_t len;
9732 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9733 USE_SAFE_ALLOCA;
9734
9735 fmt = msg = Qnil;
9736 GCPRO4 (fmt, msg, arg1, arg2);
9737
9738 args[0] = fmt = build_string (format);
9739 args[1] = arg1;
9740 args[2] = arg2;
9741 msg = Fformat (3, args);
9742
9743 len = SBYTES (msg) + 1;
9744 buffer = SAFE_ALLOCA (len);
9745 memcpy (buffer, SDATA (msg), len);
9746
9747 message_dolog (buffer, len - 1, 1, 0);
9748 SAFE_FREE ();
9749
9750 UNGCPRO;
9751 }
9752
9753
9754 /* Output a newline in the *Messages* buffer if "needs" one. */
9755
9756 void
9757 message_log_maybe_newline (void)
9758 {
9759 if (message_log_need_newline)
9760 message_dolog ("", 0, 1, 0);
9761 }
9762
9763
9764 /* Add a string M of length NBYTES to the message log, optionally
9765 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9766 true, means interpret the contents of M as multibyte. This
9767 function calls low-level routines in order to bypass text property
9768 hooks, etc. which might not be safe to run.
9769
9770 This may GC (insert may run before/after change hooks),
9771 so the buffer M must NOT point to a Lisp string. */
9772
9773 void
9774 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9775 {
9776 const unsigned char *msg = (const unsigned char *) m;
9777
9778 if (!NILP (Vmemory_full))
9779 return;
9780
9781 if (!NILP (Vmessage_log_max))
9782 {
9783 struct buffer *oldbuf;
9784 Lisp_Object oldpoint, oldbegv, oldzv;
9785 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9786 ptrdiff_t point_at_end = 0;
9787 ptrdiff_t zv_at_end = 0;
9788 Lisp_Object old_deactivate_mark;
9789 struct gcpro gcpro1;
9790
9791 old_deactivate_mark = Vdeactivate_mark;
9792 oldbuf = current_buffer;
9793
9794 /* Ensure the Messages buffer exists, and switch to it.
9795 If we created it, set the major-mode. */
9796 {
9797 int newbuffer = 0;
9798 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9799
9800 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9801
9802 if (newbuffer
9803 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9804 call0 (intern ("messages-buffer-mode"));
9805 }
9806
9807 bset_undo_list (current_buffer, Qt);
9808 bset_cache_long_scans (current_buffer, Qnil);
9809
9810 oldpoint = message_dolog_marker1;
9811 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9812 oldbegv = message_dolog_marker2;
9813 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9814 oldzv = message_dolog_marker3;
9815 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9816 GCPRO1 (old_deactivate_mark);
9817
9818 if (PT == Z)
9819 point_at_end = 1;
9820 if (ZV == Z)
9821 zv_at_end = 1;
9822
9823 BEGV = BEG;
9824 BEGV_BYTE = BEG_BYTE;
9825 ZV = Z;
9826 ZV_BYTE = Z_BYTE;
9827 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9828
9829 /* Insert the string--maybe converting multibyte to single byte
9830 or vice versa, so that all the text fits the buffer. */
9831 if (multibyte
9832 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9833 {
9834 ptrdiff_t i;
9835 int c, char_bytes;
9836 char work[1];
9837
9838 /* Convert a multibyte string to single-byte
9839 for the *Message* buffer. */
9840 for (i = 0; i < nbytes; i += char_bytes)
9841 {
9842 c = string_char_and_length (msg + i, &char_bytes);
9843 work[0] = (ASCII_CHAR_P (c)
9844 ? c
9845 : multibyte_char_to_unibyte (c));
9846 insert_1_both (work, 1, 1, 1, 0, 0);
9847 }
9848 }
9849 else if (! multibyte
9850 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9851 {
9852 ptrdiff_t i;
9853 int c, char_bytes;
9854 unsigned char str[MAX_MULTIBYTE_LENGTH];
9855 /* Convert a single-byte string to multibyte
9856 for the *Message* buffer. */
9857 for (i = 0; i < nbytes; i++)
9858 {
9859 c = msg[i];
9860 MAKE_CHAR_MULTIBYTE (c);
9861 char_bytes = CHAR_STRING (c, str);
9862 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9863 }
9864 }
9865 else if (nbytes)
9866 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9867
9868 if (nlflag)
9869 {
9870 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9871 printmax_t dups;
9872
9873 insert_1_both ("\n", 1, 1, 1, 0, 0);
9874
9875 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9876 this_bol = PT;
9877 this_bol_byte = PT_BYTE;
9878
9879 /* See if this line duplicates the previous one.
9880 If so, combine duplicates. */
9881 if (this_bol > BEG)
9882 {
9883 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9884 prev_bol = PT;
9885 prev_bol_byte = PT_BYTE;
9886
9887 dups = message_log_check_duplicate (prev_bol_byte,
9888 this_bol_byte);
9889 if (dups)
9890 {
9891 del_range_both (prev_bol, prev_bol_byte,
9892 this_bol, this_bol_byte, 0);
9893 if (dups > 1)
9894 {
9895 char dupstr[sizeof " [ times]"
9896 + INT_STRLEN_BOUND (printmax_t)];
9897
9898 /* If you change this format, don't forget to also
9899 change message_log_check_duplicate. */
9900 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9901 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9902 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9903 }
9904 }
9905 }
9906
9907 /* If we have more than the desired maximum number of lines
9908 in the *Messages* buffer now, delete the oldest ones.
9909 This is safe because we don't have undo in this buffer. */
9910
9911 if (NATNUMP (Vmessage_log_max))
9912 {
9913 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9914 -XFASTINT (Vmessage_log_max) - 1, 0);
9915 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9916 }
9917 }
9918 BEGV = marker_position (oldbegv);
9919 BEGV_BYTE = marker_byte_position (oldbegv);
9920
9921 if (zv_at_end)
9922 {
9923 ZV = Z;
9924 ZV_BYTE = Z_BYTE;
9925 }
9926 else
9927 {
9928 ZV = marker_position (oldzv);
9929 ZV_BYTE = marker_byte_position (oldzv);
9930 }
9931
9932 if (point_at_end)
9933 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9934 else
9935 /* We can't do Fgoto_char (oldpoint) because it will run some
9936 Lisp code. */
9937 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9938 marker_byte_position (oldpoint));
9939
9940 UNGCPRO;
9941 unchain_marker (XMARKER (oldpoint));
9942 unchain_marker (XMARKER (oldbegv));
9943 unchain_marker (XMARKER (oldzv));
9944
9945 /* We called insert_1_both above with its 5th argument (PREPARE)
9946 zero, which prevents insert_1_both from calling
9947 prepare_to_modify_buffer, which in turns prevents us from
9948 incrementing windows_or_buffers_changed even if *Messages* is
9949 shown in some window. So we must manually set
9950 windows_or_buffers_changed here to make up for that. */
9951 windows_or_buffers_changed = old_windows_or_buffers_changed;
9952 bset_redisplay (current_buffer);
9953
9954 set_buffer_internal (oldbuf);
9955
9956 message_log_need_newline = !nlflag;
9957 Vdeactivate_mark = old_deactivate_mark;
9958 }
9959 }
9960
9961
9962 /* We are at the end of the buffer after just having inserted a newline.
9963 (Note: We depend on the fact we won't be crossing the gap.)
9964 Check to see if the most recent message looks a lot like the previous one.
9965 Return 0 if different, 1 if the new one should just replace it, or a
9966 value N > 1 if we should also append " [N times]". */
9967
9968 static intmax_t
9969 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9970 {
9971 ptrdiff_t i;
9972 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9973 int seen_dots = 0;
9974 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9975 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9976
9977 for (i = 0; i < len; i++)
9978 {
9979 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9980 seen_dots = 1;
9981 if (p1[i] != p2[i])
9982 return seen_dots;
9983 }
9984 p1 += len;
9985 if (*p1 == '\n')
9986 return 2;
9987 if (*p1++ == ' ' && *p1++ == '[')
9988 {
9989 char *pend;
9990 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9991 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9992 return n + 1;
9993 }
9994 return 0;
9995 }
9996 \f
9997
9998 /* Display an echo area message M with a specified length of NBYTES
9999 bytes. The string may include null characters. If M is not a
10000 string, clear out any existing message, and let the mini-buffer
10001 text show through.
10002
10003 This function cancels echoing. */
10004
10005 void
10006 message3 (Lisp_Object m)
10007 {
10008 struct gcpro gcpro1;
10009
10010 GCPRO1 (m);
10011 clear_message (true, true);
10012 cancel_echoing ();
10013
10014 /* First flush out any partial line written with print. */
10015 message_log_maybe_newline ();
10016 if (STRINGP (m))
10017 {
10018 ptrdiff_t nbytes = SBYTES (m);
10019 bool multibyte = STRING_MULTIBYTE (m);
10020 USE_SAFE_ALLOCA;
10021 char *buffer = SAFE_ALLOCA (nbytes);
10022 memcpy (buffer, SDATA (m), nbytes);
10023 message_dolog (buffer, nbytes, 1, multibyte);
10024 SAFE_FREE ();
10025 }
10026 message3_nolog (m);
10027
10028 UNGCPRO;
10029 }
10030
10031
10032 /* The non-logging version of message3.
10033 This does not cancel echoing, because it is used for echoing.
10034 Perhaps we need to make a separate function for echoing
10035 and make this cancel echoing. */
10036
10037 void
10038 message3_nolog (Lisp_Object m)
10039 {
10040 struct frame *sf = SELECTED_FRAME ();
10041
10042 if (FRAME_INITIAL_P (sf))
10043 {
10044 if (noninteractive_need_newline)
10045 putc ('\n', stderr);
10046 noninteractive_need_newline = 0;
10047 if (STRINGP (m))
10048 {
10049 Lisp_Object s = ENCODE_SYSTEM (m);
10050
10051 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10052 }
10053 if (cursor_in_echo_area == 0)
10054 fprintf (stderr, "\n");
10055 fflush (stderr);
10056 }
10057 /* Error messages get reported properly by cmd_error, so this must be just an
10058 informative message; if the frame hasn't really been initialized yet, just
10059 toss it. */
10060 else if (INTERACTIVE && sf->glyphs_initialized_p)
10061 {
10062 /* Get the frame containing the mini-buffer
10063 that the selected frame is using. */
10064 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10065 Lisp_Object frame = XWINDOW (mini_window)->frame;
10066 struct frame *f = XFRAME (frame);
10067
10068 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10069 Fmake_frame_visible (frame);
10070
10071 if (STRINGP (m) && SCHARS (m) > 0)
10072 {
10073 set_message (m);
10074 if (minibuffer_auto_raise)
10075 Fraise_frame (frame);
10076 /* Assume we are not echoing.
10077 (If we are, echo_now will override this.) */
10078 echo_message_buffer = Qnil;
10079 }
10080 else
10081 clear_message (true, true);
10082
10083 do_pending_window_change (0);
10084 echo_area_display (1);
10085 do_pending_window_change (0);
10086 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10087 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10088 }
10089 }
10090
10091
10092 /* Display a null-terminated echo area message M. If M is 0, clear
10093 out any existing message, and let the mini-buffer text show through.
10094
10095 The buffer M must continue to exist until after the echo area gets
10096 cleared or some other message gets displayed there. Do not pass
10097 text that is stored in a Lisp string. Do not pass text in a buffer
10098 that was alloca'd. */
10099
10100 void
10101 message1 (const char *m)
10102 {
10103 message3 (m ? build_unibyte_string (m) : Qnil);
10104 }
10105
10106
10107 /* The non-logging counterpart of message1. */
10108
10109 void
10110 message1_nolog (const char *m)
10111 {
10112 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10113 }
10114
10115 /* Display a message M which contains a single %s
10116 which gets replaced with STRING. */
10117
10118 void
10119 message_with_string (const char *m, Lisp_Object string, int log)
10120 {
10121 CHECK_STRING (string);
10122
10123 if (noninteractive)
10124 {
10125 if (m)
10126 {
10127 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10128 String whose data pointer might be passed to us in M. So
10129 we use a local copy. */
10130 char *fmt = xstrdup (m);
10131
10132 if (noninteractive_need_newline)
10133 putc ('\n', stderr);
10134 noninteractive_need_newline = 0;
10135 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10136 if (!cursor_in_echo_area)
10137 fprintf (stderr, "\n");
10138 fflush (stderr);
10139 xfree (fmt);
10140 }
10141 }
10142 else if (INTERACTIVE)
10143 {
10144 /* The frame whose minibuffer we're going to display the message on.
10145 It may be larger than the selected frame, so we need
10146 to use its buffer, not the selected frame's buffer. */
10147 Lisp_Object mini_window;
10148 struct frame *f, *sf = SELECTED_FRAME ();
10149
10150 /* Get the frame containing the minibuffer
10151 that the selected frame is using. */
10152 mini_window = FRAME_MINIBUF_WINDOW (sf);
10153 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10154
10155 /* Error messages get reported properly by cmd_error, so this must be
10156 just an informative message; if the frame hasn't really been
10157 initialized yet, just toss it. */
10158 if (f->glyphs_initialized_p)
10159 {
10160 Lisp_Object args[2], msg;
10161 struct gcpro gcpro1, gcpro2;
10162
10163 args[0] = build_string (m);
10164 args[1] = msg = string;
10165 GCPRO2 (args[0], msg);
10166 gcpro1.nvars = 2;
10167
10168 msg = Fformat (2, args);
10169
10170 if (log)
10171 message3 (msg);
10172 else
10173 message3_nolog (msg);
10174
10175 UNGCPRO;
10176
10177 /* Print should start at the beginning of the message
10178 buffer next time. */
10179 message_buf_print = 0;
10180 }
10181 }
10182 }
10183
10184
10185 /* Dump an informative message to the minibuf. If M is 0, clear out
10186 any existing message, and let the mini-buffer text show through. */
10187
10188 static void
10189 vmessage (const char *m, va_list ap)
10190 {
10191 if (noninteractive)
10192 {
10193 if (m)
10194 {
10195 if (noninteractive_need_newline)
10196 putc ('\n', stderr);
10197 noninteractive_need_newline = 0;
10198 vfprintf (stderr, m, ap);
10199 if (cursor_in_echo_area == 0)
10200 fprintf (stderr, "\n");
10201 fflush (stderr);
10202 }
10203 }
10204 else if (INTERACTIVE)
10205 {
10206 /* The frame whose mini-buffer we're going to display the message
10207 on. It may be larger than the selected frame, so we need to
10208 use its buffer, not the selected frame's buffer. */
10209 Lisp_Object mini_window;
10210 struct frame *f, *sf = SELECTED_FRAME ();
10211
10212 /* Get the frame containing the mini-buffer
10213 that the selected frame is using. */
10214 mini_window = FRAME_MINIBUF_WINDOW (sf);
10215 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10216
10217 /* Error messages get reported properly by cmd_error, so this must be
10218 just an informative message; if the frame hasn't really been
10219 initialized yet, just toss it. */
10220 if (f->glyphs_initialized_p)
10221 {
10222 if (m)
10223 {
10224 ptrdiff_t len;
10225 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10226 char *message_buf = alloca (maxsize + 1);
10227
10228 len = doprnt (message_buf, maxsize, m, 0, ap);
10229
10230 message3 (make_string (message_buf, len));
10231 }
10232 else
10233 message1 (0);
10234
10235 /* Print should start at the beginning of the message
10236 buffer next time. */
10237 message_buf_print = 0;
10238 }
10239 }
10240 }
10241
10242 void
10243 message (const char *m, ...)
10244 {
10245 va_list ap;
10246 va_start (ap, m);
10247 vmessage (m, ap);
10248 va_end (ap);
10249 }
10250
10251
10252 #if 0
10253 /* The non-logging version of message. */
10254
10255 void
10256 message_nolog (const char *m, ...)
10257 {
10258 Lisp_Object old_log_max;
10259 va_list ap;
10260 va_start (ap, m);
10261 old_log_max = Vmessage_log_max;
10262 Vmessage_log_max = Qnil;
10263 vmessage (m, ap);
10264 Vmessage_log_max = old_log_max;
10265 va_end (ap);
10266 }
10267 #endif
10268
10269
10270 /* Display the current message in the current mini-buffer. This is
10271 only called from error handlers in process.c, and is not time
10272 critical. */
10273
10274 void
10275 update_echo_area (void)
10276 {
10277 if (!NILP (echo_area_buffer[0]))
10278 {
10279 Lisp_Object string;
10280 string = Fcurrent_message ();
10281 message3 (string);
10282 }
10283 }
10284
10285
10286 /* Make sure echo area buffers in `echo_buffers' are live.
10287 If they aren't, make new ones. */
10288
10289 static void
10290 ensure_echo_area_buffers (void)
10291 {
10292 int i;
10293
10294 for (i = 0; i < 2; ++i)
10295 if (!BUFFERP (echo_buffer[i])
10296 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10297 {
10298 char name[30];
10299 Lisp_Object old_buffer;
10300 int j;
10301
10302 old_buffer = echo_buffer[i];
10303 echo_buffer[i] = Fget_buffer_create
10304 (make_formatted_string (name, " *Echo Area %d*", i));
10305 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10306 /* to force word wrap in echo area -
10307 it was decided to postpone this*/
10308 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10309
10310 for (j = 0; j < 2; ++j)
10311 if (EQ (old_buffer, echo_area_buffer[j]))
10312 echo_area_buffer[j] = echo_buffer[i];
10313 }
10314 }
10315
10316
10317 /* Call FN with args A1..A2 with either the current or last displayed
10318 echo_area_buffer as current buffer.
10319
10320 WHICH zero means use the current message buffer
10321 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10322 from echo_buffer[] and clear it.
10323
10324 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10325 suitable buffer from echo_buffer[] and clear it.
10326
10327 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10328 that the current message becomes the last displayed one, make
10329 choose a suitable buffer for echo_area_buffer[0], and clear it.
10330
10331 Value is what FN returns. */
10332
10333 static int
10334 with_echo_area_buffer (struct window *w, int which,
10335 int (*fn) (ptrdiff_t, Lisp_Object),
10336 ptrdiff_t a1, Lisp_Object a2)
10337 {
10338 Lisp_Object buffer;
10339 int this_one, the_other, clear_buffer_p, rc;
10340 ptrdiff_t count = SPECPDL_INDEX ();
10341
10342 /* If buffers aren't live, make new ones. */
10343 ensure_echo_area_buffers ();
10344
10345 clear_buffer_p = 0;
10346
10347 if (which == 0)
10348 this_one = 0, the_other = 1;
10349 else if (which > 0)
10350 this_one = 1, the_other = 0;
10351 else
10352 {
10353 this_one = 0, the_other = 1;
10354 clear_buffer_p = true;
10355
10356 /* We need a fresh one in case the current echo buffer equals
10357 the one containing the last displayed echo area message. */
10358 if (!NILP (echo_area_buffer[this_one])
10359 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10360 echo_area_buffer[this_one] = Qnil;
10361 }
10362
10363 /* Choose a suitable buffer from echo_buffer[] is we don't
10364 have one. */
10365 if (NILP (echo_area_buffer[this_one]))
10366 {
10367 echo_area_buffer[this_one]
10368 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10369 ? echo_buffer[the_other]
10370 : echo_buffer[this_one]);
10371 clear_buffer_p = true;
10372 }
10373
10374 buffer = echo_area_buffer[this_one];
10375
10376 /* Don't get confused by reusing the buffer used for echoing
10377 for a different purpose. */
10378 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10379 cancel_echoing ();
10380
10381 record_unwind_protect (unwind_with_echo_area_buffer,
10382 with_echo_area_buffer_unwind_data (w));
10383
10384 /* Make the echo area buffer current. Note that for display
10385 purposes, it is not necessary that the displayed window's buffer
10386 == current_buffer, except for text property lookup. So, let's
10387 only set that buffer temporarily here without doing a full
10388 Fset_window_buffer. We must also change w->pointm, though,
10389 because otherwise an assertions in unshow_buffer fails, and Emacs
10390 aborts. */
10391 set_buffer_internal_1 (XBUFFER (buffer));
10392 if (w)
10393 {
10394 wset_buffer (w, buffer);
10395 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10396 }
10397
10398 bset_undo_list (current_buffer, Qt);
10399 bset_read_only (current_buffer, Qnil);
10400 specbind (Qinhibit_read_only, Qt);
10401 specbind (Qinhibit_modification_hooks, Qt);
10402
10403 if (clear_buffer_p && Z > BEG)
10404 del_range (BEG, Z);
10405
10406 eassert (BEGV >= BEG);
10407 eassert (ZV <= Z && ZV >= BEGV);
10408
10409 rc = fn (a1, a2);
10410
10411 eassert (BEGV >= BEG);
10412 eassert (ZV <= Z && ZV >= BEGV);
10413
10414 unbind_to (count, Qnil);
10415 return rc;
10416 }
10417
10418
10419 /* Save state that should be preserved around the call to the function
10420 FN called in with_echo_area_buffer. */
10421
10422 static Lisp_Object
10423 with_echo_area_buffer_unwind_data (struct window *w)
10424 {
10425 int i = 0;
10426 Lisp_Object vector, tmp;
10427
10428 /* Reduce consing by keeping one vector in
10429 Vwith_echo_area_save_vector. */
10430 vector = Vwith_echo_area_save_vector;
10431 Vwith_echo_area_save_vector = Qnil;
10432
10433 if (NILP (vector))
10434 vector = Fmake_vector (make_number (9), Qnil);
10435
10436 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10437 ASET (vector, i, Vdeactivate_mark); ++i;
10438 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10439
10440 if (w)
10441 {
10442 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10443 ASET (vector, i, w->contents); ++i;
10444 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10445 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10446 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10447 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10448 }
10449 else
10450 {
10451 int end = i + 6;
10452 for (; i < end; ++i)
10453 ASET (vector, i, Qnil);
10454 }
10455
10456 eassert (i == ASIZE (vector));
10457 return vector;
10458 }
10459
10460
10461 /* Restore global state from VECTOR which was created by
10462 with_echo_area_buffer_unwind_data. */
10463
10464 static void
10465 unwind_with_echo_area_buffer (Lisp_Object vector)
10466 {
10467 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10468 Vdeactivate_mark = AREF (vector, 1);
10469 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10470
10471 if (WINDOWP (AREF (vector, 3)))
10472 {
10473 struct window *w;
10474 Lisp_Object buffer;
10475
10476 w = XWINDOW (AREF (vector, 3));
10477 buffer = AREF (vector, 4);
10478
10479 wset_buffer (w, buffer);
10480 set_marker_both (w->pointm, buffer,
10481 XFASTINT (AREF (vector, 5)),
10482 XFASTINT (AREF (vector, 6)));
10483 set_marker_both (w->start, buffer,
10484 XFASTINT (AREF (vector, 7)),
10485 XFASTINT (AREF (vector, 8)));
10486 }
10487
10488 Vwith_echo_area_save_vector = vector;
10489 }
10490
10491
10492 /* Set up the echo area for use by print functions. MULTIBYTE_P
10493 non-zero means we will print multibyte. */
10494
10495 void
10496 setup_echo_area_for_printing (int multibyte_p)
10497 {
10498 /* If we can't find an echo area any more, exit. */
10499 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10500 Fkill_emacs (Qnil);
10501
10502 ensure_echo_area_buffers ();
10503
10504 if (!message_buf_print)
10505 {
10506 /* A message has been output since the last time we printed.
10507 Choose a fresh echo area buffer. */
10508 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10509 echo_area_buffer[0] = echo_buffer[1];
10510 else
10511 echo_area_buffer[0] = echo_buffer[0];
10512
10513 /* Switch to that buffer and clear it. */
10514 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10515 bset_truncate_lines (current_buffer, Qnil);
10516
10517 if (Z > BEG)
10518 {
10519 ptrdiff_t count = SPECPDL_INDEX ();
10520 specbind (Qinhibit_read_only, Qt);
10521 /* Note that undo recording is always disabled. */
10522 del_range (BEG, Z);
10523 unbind_to (count, Qnil);
10524 }
10525 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10526
10527 /* Set up the buffer for the multibyteness we need. */
10528 if (multibyte_p
10529 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10530 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10531
10532 /* Raise the frame containing the echo area. */
10533 if (minibuffer_auto_raise)
10534 {
10535 struct frame *sf = SELECTED_FRAME ();
10536 Lisp_Object mini_window;
10537 mini_window = FRAME_MINIBUF_WINDOW (sf);
10538 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10539 }
10540
10541 message_log_maybe_newline ();
10542 message_buf_print = 1;
10543 }
10544 else
10545 {
10546 if (NILP (echo_area_buffer[0]))
10547 {
10548 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10549 echo_area_buffer[0] = echo_buffer[1];
10550 else
10551 echo_area_buffer[0] = echo_buffer[0];
10552 }
10553
10554 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10555 {
10556 /* Someone switched buffers between print requests. */
10557 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10558 bset_truncate_lines (current_buffer, Qnil);
10559 }
10560 }
10561 }
10562
10563
10564 /* Display an echo area message in window W. Value is non-zero if W's
10565 height is changed. If display_last_displayed_message_p is
10566 non-zero, display the message that was last displayed, otherwise
10567 display the current message. */
10568
10569 static int
10570 display_echo_area (struct window *w)
10571 {
10572 int i, no_message_p, window_height_changed_p;
10573
10574 /* Temporarily disable garbage collections while displaying the echo
10575 area. This is done because a GC can print a message itself.
10576 That message would modify the echo area buffer's contents while a
10577 redisplay of the buffer is going on, and seriously confuse
10578 redisplay. */
10579 ptrdiff_t count = inhibit_garbage_collection ();
10580
10581 /* If there is no message, we must call display_echo_area_1
10582 nevertheless because it resizes the window. But we will have to
10583 reset the echo_area_buffer in question to nil at the end because
10584 with_echo_area_buffer will sets it to an empty buffer. */
10585 i = display_last_displayed_message_p ? 1 : 0;
10586 no_message_p = NILP (echo_area_buffer[i]);
10587
10588 window_height_changed_p
10589 = with_echo_area_buffer (w, display_last_displayed_message_p,
10590 display_echo_area_1,
10591 (intptr_t) w, Qnil);
10592
10593 if (no_message_p)
10594 echo_area_buffer[i] = Qnil;
10595
10596 unbind_to (count, Qnil);
10597 return window_height_changed_p;
10598 }
10599
10600
10601 /* Helper for display_echo_area. Display the current buffer which
10602 contains the current echo area message in window W, a mini-window,
10603 a pointer to which is passed in A1. A2..A4 are currently not used.
10604 Change the height of W so that all of the message is displayed.
10605 Value is non-zero if height of W was changed. */
10606
10607 static int
10608 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10609 {
10610 intptr_t i1 = a1;
10611 struct window *w = (struct window *) i1;
10612 Lisp_Object window;
10613 struct text_pos start;
10614 int window_height_changed_p = 0;
10615
10616 /* Do this before displaying, so that we have a large enough glyph
10617 matrix for the display. If we can't get enough space for the
10618 whole text, display the last N lines. That works by setting w->start. */
10619 window_height_changed_p = resize_mini_window (w, 0);
10620
10621 /* Use the starting position chosen by resize_mini_window. */
10622 SET_TEXT_POS_FROM_MARKER (start, w->start);
10623
10624 /* Display. */
10625 clear_glyph_matrix (w->desired_matrix);
10626 XSETWINDOW (window, w);
10627 try_window (window, start, 0);
10628
10629 return window_height_changed_p;
10630 }
10631
10632
10633 /* Resize the echo area window to exactly the size needed for the
10634 currently displayed message, if there is one. If a mini-buffer
10635 is active, don't shrink it. */
10636
10637 void
10638 resize_echo_area_exactly (void)
10639 {
10640 if (BUFFERP (echo_area_buffer[0])
10641 && WINDOWP (echo_area_window))
10642 {
10643 struct window *w = XWINDOW (echo_area_window);
10644 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10645 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10646 (intptr_t) w, resize_exactly);
10647 if (resized_p)
10648 {
10649 windows_or_buffers_changed = 42;
10650 update_mode_lines = 30;
10651 redisplay_internal ();
10652 }
10653 }
10654 }
10655
10656
10657 /* Callback function for with_echo_area_buffer, when used from
10658 resize_echo_area_exactly. A1 contains a pointer to the window to
10659 resize, EXACTLY non-nil means resize the mini-window exactly to the
10660 size of the text displayed. A3 and A4 are not used. Value is what
10661 resize_mini_window returns. */
10662
10663 static int
10664 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10665 {
10666 intptr_t i1 = a1;
10667 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10668 }
10669
10670
10671 /* Resize mini-window W to fit the size of its contents. EXACT_P
10672 means size the window exactly to the size needed. Otherwise, it's
10673 only enlarged until W's buffer is empty.
10674
10675 Set W->start to the right place to begin display. If the whole
10676 contents fit, start at the beginning. Otherwise, start so as
10677 to make the end of the contents appear. This is particularly
10678 important for y-or-n-p, but seems desirable generally.
10679
10680 Value is non-zero if the window height has been changed. */
10681
10682 int
10683 resize_mini_window (struct window *w, int exact_p)
10684 {
10685 struct frame *f = XFRAME (w->frame);
10686 int window_height_changed_p = 0;
10687
10688 eassert (MINI_WINDOW_P (w));
10689
10690 /* By default, start display at the beginning. */
10691 set_marker_both (w->start, w->contents,
10692 BUF_BEGV (XBUFFER (w->contents)),
10693 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10694
10695 /* Don't resize windows while redisplaying a window; it would
10696 confuse redisplay functions when the size of the window they are
10697 displaying changes from under them. Such a resizing can happen,
10698 for instance, when which-func prints a long message while
10699 we are running fontification-functions. We're running these
10700 functions with safe_call which binds inhibit-redisplay to t. */
10701 if (!NILP (Vinhibit_redisplay))
10702 return 0;
10703
10704 /* Nil means don't try to resize. */
10705 if (NILP (Vresize_mini_windows)
10706 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10707 return 0;
10708
10709 if (!FRAME_MINIBUF_ONLY_P (f))
10710 {
10711 struct it it;
10712 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10713 + WINDOW_PIXEL_HEIGHT (w));
10714 int unit = FRAME_LINE_HEIGHT (f);
10715 int height, max_height;
10716 struct text_pos start;
10717 struct buffer *old_current_buffer = NULL;
10718
10719 if (current_buffer != XBUFFER (w->contents))
10720 {
10721 old_current_buffer = current_buffer;
10722 set_buffer_internal (XBUFFER (w->contents));
10723 }
10724
10725 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10726
10727 /* Compute the max. number of lines specified by the user. */
10728 if (FLOATP (Vmax_mini_window_height))
10729 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10730 else if (INTEGERP (Vmax_mini_window_height))
10731 max_height = XINT (Vmax_mini_window_height) * unit;
10732 else
10733 max_height = total_height / 4;
10734
10735 /* Correct that max. height if it's bogus. */
10736 max_height = clip_to_bounds (unit, max_height, total_height);
10737
10738 /* Find out the height of the text in the window. */
10739 if (it.line_wrap == TRUNCATE)
10740 height = unit;
10741 else
10742 {
10743 last_height = 0;
10744 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10745 if (it.max_ascent == 0 && it.max_descent == 0)
10746 height = it.current_y + last_height;
10747 else
10748 height = it.current_y + it.max_ascent + it.max_descent;
10749 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10750 }
10751
10752 /* Compute a suitable window start. */
10753 if (height > max_height)
10754 {
10755 height = (max_height / unit) * unit;
10756 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10757 move_it_vertically_backward (&it, height - unit);
10758 start = it.current.pos;
10759 }
10760 else
10761 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10762 SET_MARKER_FROM_TEXT_POS (w->start, start);
10763
10764 if (EQ (Vresize_mini_windows, Qgrow_only))
10765 {
10766 /* Let it grow only, until we display an empty message, in which
10767 case the window shrinks again. */
10768 if (height > WINDOW_PIXEL_HEIGHT (w))
10769 {
10770 int old_height = WINDOW_PIXEL_HEIGHT (w);
10771
10772 FRAME_WINDOWS_FROZEN (f) = 1;
10773 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10774 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10775 }
10776 else if (height < WINDOW_PIXEL_HEIGHT (w)
10777 && (exact_p || BEGV == ZV))
10778 {
10779 int old_height = WINDOW_PIXEL_HEIGHT (w);
10780
10781 FRAME_WINDOWS_FROZEN (f) = 0;
10782 shrink_mini_window (w, 1);
10783 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10784 }
10785 }
10786 else
10787 {
10788 /* Always resize to exact size needed. */
10789 if (height > WINDOW_PIXEL_HEIGHT (w))
10790 {
10791 int old_height = WINDOW_PIXEL_HEIGHT (w);
10792
10793 FRAME_WINDOWS_FROZEN (f) = 1;
10794 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10795 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10796 }
10797 else if (height < WINDOW_PIXEL_HEIGHT (w))
10798 {
10799 int old_height = WINDOW_PIXEL_HEIGHT (w);
10800
10801 FRAME_WINDOWS_FROZEN (f) = 0;
10802 shrink_mini_window (w, 1);
10803
10804 if (height)
10805 {
10806 FRAME_WINDOWS_FROZEN (f) = 1;
10807 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10808 }
10809
10810 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10811 }
10812 }
10813
10814 if (old_current_buffer)
10815 set_buffer_internal (old_current_buffer);
10816 }
10817
10818 return window_height_changed_p;
10819 }
10820
10821
10822 /* Value is the current message, a string, or nil if there is no
10823 current message. */
10824
10825 Lisp_Object
10826 current_message (void)
10827 {
10828 Lisp_Object msg;
10829
10830 if (!BUFFERP (echo_area_buffer[0]))
10831 msg = Qnil;
10832 else
10833 {
10834 with_echo_area_buffer (0, 0, current_message_1,
10835 (intptr_t) &msg, Qnil);
10836 if (NILP (msg))
10837 echo_area_buffer[0] = Qnil;
10838 }
10839
10840 return msg;
10841 }
10842
10843
10844 static int
10845 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10846 {
10847 intptr_t i1 = a1;
10848 Lisp_Object *msg = (Lisp_Object *) i1;
10849
10850 if (Z > BEG)
10851 *msg = make_buffer_string (BEG, Z, 1);
10852 else
10853 *msg = Qnil;
10854 return 0;
10855 }
10856
10857
10858 /* Push the current message on Vmessage_stack for later restoration
10859 by restore_message. Value is non-zero if the current message isn't
10860 empty. This is a relatively infrequent operation, so it's not
10861 worth optimizing. */
10862
10863 bool
10864 push_message (void)
10865 {
10866 Lisp_Object msg = current_message ();
10867 Vmessage_stack = Fcons (msg, Vmessage_stack);
10868 return STRINGP (msg);
10869 }
10870
10871
10872 /* Restore message display from the top of Vmessage_stack. */
10873
10874 void
10875 restore_message (void)
10876 {
10877 eassert (CONSP (Vmessage_stack));
10878 message3_nolog (XCAR (Vmessage_stack));
10879 }
10880
10881
10882 /* Handler for unwind-protect calling pop_message. */
10883
10884 void
10885 pop_message_unwind (void)
10886 {
10887 /* Pop the top-most entry off Vmessage_stack. */
10888 eassert (CONSP (Vmessage_stack));
10889 Vmessage_stack = XCDR (Vmessage_stack);
10890 }
10891
10892
10893 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10894 exits. If the stack is not empty, we have a missing pop_message
10895 somewhere. */
10896
10897 void
10898 check_message_stack (void)
10899 {
10900 if (!NILP (Vmessage_stack))
10901 emacs_abort ();
10902 }
10903
10904
10905 /* Truncate to NCHARS what will be displayed in the echo area the next
10906 time we display it---but don't redisplay it now. */
10907
10908 void
10909 truncate_echo_area (ptrdiff_t nchars)
10910 {
10911 if (nchars == 0)
10912 echo_area_buffer[0] = Qnil;
10913 else if (!noninteractive
10914 && INTERACTIVE
10915 && !NILP (echo_area_buffer[0]))
10916 {
10917 struct frame *sf = SELECTED_FRAME ();
10918 /* Error messages get reported properly by cmd_error, so this must be
10919 just an informative message; if the frame hasn't really been
10920 initialized yet, just toss it. */
10921 if (sf->glyphs_initialized_p)
10922 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10923 }
10924 }
10925
10926
10927 /* Helper function for truncate_echo_area. Truncate the current
10928 message to at most NCHARS characters. */
10929
10930 static int
10931 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10932 {
10933 if (BEG + nchars < Z)
10934 del_range (BEG + nchars, Z);
10935 if (Z == BEG)
10936 echo_area_buffer[0] = Qnil;
10937 return 0;
10938 }
10939
10940 /* Set the current message to STRING. */
10941
10942 static void
10943 set_message (Lisp_Object string)
10944 {
10945 eassert (STRINGP (string));
10946
10947 message_enable_multibyte = STRING_MULTIBYTE (string);
10948
10949 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10950 message_buf_print = 0;
10951 help_echo_showing_p = 0;
10952
10953 if (STRINGP (Vdebug_on_message)
10954 && STRINGP (string)
10955 && fast_string_match (Vdebug_on_message, string) >= 0)
10956 call_debugger (list2 (Qerror, string));
10957 }
10958
10959
10960 /* Helper function for set_message. First argument is ignored and second
10961 argument has the same meaning as for set_message.
10962 This function is called with the echo area buffer being current. */
10963
10964 static int
10965 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10966 {
10967 eassert (STRINGP (string));
10968
10969 /* Change multibyteness of the echo buffer appropriately. */
10970 if (message_enable_multibyte
10971 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10972 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10973
10974 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10975 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10976 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10977
10978 /* Insert new message at BEG. */
10979 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10980
10981 /* This function takes care of single/multibyte conversion.
10982 We just have to ensure that the echo area buffer has the right
10983 setting of enable_multibyte_characters. */
10984 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10985
10986 return 0;
10987 }
10988
10989
10990 /* Clear messages. CURRENT_P non-zero means clear the current
10991 message. LAST_DISPLAYED_P non-zero means clear the message
10992 last displayed. */
10993
10994 void
10995 clear_message (bool current_p, bool last_displayed_p)
10996 {
10997 if (current_p)
10998 {
10999 echo_area_buffer[0] = Qnil;
11000 message_cleared_p = true;
11001 }
11002
11003 if (last_displayed_p)
11004 echo_area_buffer[1] = Qnil;
11005
11006 message_buf_print = 0;
11007 }
11008
11009 /* Clear garbaged frames.
11010
11011 This function is used where the old redisplay called
11012 redraw_garbaged_frames which in turn called redraw_frame which in
11013 turn called clear_frame. The call to clear_frame was a source of
11014 flickering. I believe a clear_frame is not necessary. It should
11015 suffice in the new redisplay to invalidate all current matrices,
11016 and ensure a complete redisplay of all windows. */
11017
11018 static void
11019 clear_garbaged_frames (void)
11020 {
11021 if (frame_garbaged)
11022 {
11023 Lisp_Object tail, frame;
11024
11025 FOR_EACH_FRAME (tail, frame)
11026 {
11027 struct frame *f = XFRAME (frame);
11028
11029 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11030 {
11031 if (f->resized_p)
11032 redraw_frame (f);
11033 else
11034 clear_current_matrices (f);
11035 fset_redisplay (f);
11036 f->garbaged = false;
11037 f->resized_p = false;
11038 }
11039 }
11040
11041 frame_garbaged = false;
11042 }
11043 }
11044
11045
11046 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11047 is non-zero update selected_frame. Value is non-zero if the
11048 mini-windows height has been changed. */
11049
11050 static int
11051 echo_area_display (int update_frame_p)
11052 {
11053 Lisp_Object mini_window;
11054 struct window *w;
11055 struct frame *f;
11056 int window_height_changed_p = 0;
11057 struct frame *sf = SELECTED_FRAME ();
11058
11059 mini_window = FRAME_MINIBUF_WINDOW (sf);
11060 w = XWINDOW (mini_window);
11061 f = XFRAME (WINDOW_FRAME (w));
11062
11063 /* Don't display if frame is invisible or not yet initialized. */
11064 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11065 return 0;
11066
11067 #ifdef HAVE_WINDOW_SYSTEM
11068 /* When Emacs starts, selected_frame may be the initial terminal
11069 frame. If we let this through, a message would be displayed on
11070 the terminal. */
11071 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11072 return 0;
11073 #endif /* HAVE_WINDOW_SYSTEM */
11074
11075 /* Redraw garbaged frames. */
11076 clear_garbaged_frames ();
11077
11078 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11079 {
11080 echo_area_window = mini_window;
11081 window_height_changed_p = display_echo_area (w);
11082 w->must_be_updated_p = true;
11083
11084 /* Update the display, unless called from redisplay_internal.
11085 Also don't update the screen during redisplay itself. The
11086 update will happen at the end of redisplay, and an update
11087 here could cause confusion. */
11088 if (update_frame_p && !redisplaying_p)
11089 {
11090 int n = 0;
11091
11092 /* If the display update has been interrupted by pending
11093 input, update mode lines in the frame. Due to the
11094 pending input, it might have been that redisplay hasn't
11095 been called, so that mode lines above the echo area are
11096 garbaged. This looks odd, so we prevent it here. */
11097 if (!display_completed)
11098 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11099
11100 if (window_height_changed_p
11101 /* Don't do this if Emacs is shutting down. Redisplay
11102 needs to run hooks. */
11103 && !NILP (Vrun_hooks))
11104 {
11105 /* Must update other windows. Likewise as in other
11106 cases, don't let this update be interrupted by
11107 pending input. */
11108 ptrdiff_t count = SPECPDL_INDEX ();
11109 specbind (Qredisplay_dont_pause, Qt);
11110 windows_or_buffers_changed = 44;
11111 redisplay_internal ();
11112 unbind_to (count, Qnil);
11113 }
11114 else if (FRAME_WINDOW_P (f) && n == 0)
11115 {
11116 /* Window configuration is the same as before.
11117 Can do with a display update of the echo area,
11118 unless we displayed some mode lines. */
11119 update_single_window (w, 1);
11120 flush_frame (f);
11121 }
11122 else
11123 update_frame (f, 1, 1);
11124
11125 /* If cursor is in the echo area, make sure that the next
11126 redisplay displays the minibuffer, so that the cursor will
11127 be replaced with what the minibuffer wants. */
11128 if (cursor_in_echo_area)
11129 wset_redisplay (XWINDOW (mini_window));
11130 }
11131 }
11132 else if (!EQ (mini_window, selected_window))
11133 wset_redisplay (XWINDOW (mini_window));
11134
11135 /* Last displayed message is now the current message. */
11136 echo_area_buffer[1] = echo_area_buffer[0];
11137 /* Inform read_char that we're not echoing. */
11138 echo_message_buffer = Qnil;
11139
11140 /* Prevent redisplay optimization in redisplay_internal by resetting
11141 this_line_start_pos. This is done because the mini-buffer now
11142 displays the message instead of its buffer text. */
11143 if (EQ (mini_window, selected_window))
11144 CHARPOS (this_line_start_pos) = 0;
11145
11146 return window_height_changed_p;
11147 }
11148
11149 /* Nonzero if W's buffer was changed but not saved. */
11150
11151 static int
11152 window_buffer_changed (struct window *w)
11153 {
11154 struct buffer *b = XBUFFER (w->contents);
11155
11156 eassert (BUFFER_LIVE_P (b));
11157
11158 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11159 }
11160
11161 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11162
11163 static int
11164 mode_line_update_needed (struct window *w)
11165 {
11166 return (w->column_number_displayed != -1
11167 && !(PT == w->last_point && !window_outdated (w))
11168 && (w->column_number_displayed != current_column ()));
11169 }
11170
11171 /* Nonzero if window start of W is frozen and may not be changed during
11172 redisplay. */
11173
11174 static bool
11175 window_frozen_p (struct window *w)
11176 {
11177 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11178 {
11179 Lisp_Object window;
11180
11181 XSETWINDOW (window, w);
11182 if (MINI_WINDOW_P (w))
11183 return 0;
11184 else if (EQ (window, selected_window))
11185 return 0;
11186 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11187 && EQ (window, Vminibuf_scroll_window))
11188 /* This special window can't be frozen too. */
11189 return 0;
11190 else
11191 return 1;
11192 }
11193 return 0;
11194 }
11195
11196 /***********************************************************************
11197 Mode Lines and Frame Titles
11198 ***********************************************************************/
11199
11200 /* A buffer for constructing non-propertized mode-line strings and
11201 frame titles in it; allocated from the heap in init_xdisp and
11202 resized as needed in store_mode_line_noprop_char. */
11203
11204 static char *mode_line_noprop_buf;
11205
11206 /* The buffer's end, and a current output position in it. */
11207
11208 static char *mode_line_noprop_buf_end;
11209 static char *mode_line_noprop_ptr;
11210
11211 #define MODE_LINE_NOPROP_LEN(start) \
11212 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11213
11214 static enum {
11215 MODE_LINE_DISPLAY = 0,
11216 MODE_LINE_TITLE,
11217 MODE_LINE_NOPROP,
11218 MODE_LINE_STRING
11219 } mode_line_target;
11220
11221 /* Alist that caches the results of :propertize.
11222 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11223 static Lisp_Object mode_line_proptrans_alist;
11224
11225 /* List of strings making up the mode-line. */
11226 static Lisp_Object mode_line_string_list;
11227
11228 /* Base face property when building propertized mode line string. */
11229 static Lisp_Object mode_line_string_face;
11230 static Lisp_Object mode_line_string_face_prop;
11231
11232
11233 /* Unwind data for mode line strings */
11234
11235 static Lisp_Object Vmode_line_unwind_vector;
11236
11237 static Lisp_Object
11238 format_mode_line_unwind_data (struct frame *target_frame,
11239 struct buffer *obuf,
11240 Lisp_Object owin,
11241 int save_proptrans)
11242 {
11243 Lisp_Object vector, tmp;
11244
11245 /* Reduce consing by keeping one vector in
11246 Vwith_echo_area_save_vector. */
11247 vector = Vmode_line_unwind_vector;
11248 Vmode_line_unwind_vector = Qnil;
11249
11250 if (NILP (vector))
11251 vector = Fmake_vector (make_number (10), Qnil);
11252
11253 ASET (vector, 0, make_number (mode_line_target));
11254 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11255 ASET (vector, 2, mode_line_string_list);
11256 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11257 ASET (vector, 4, mode_line_string_face);
11258 ASET (vector, 5, mode_line_string_face_prop);
11259
11260 if (obuf)
11261 XSETBUFFER (tmp, obuf);
11262 else
11263 tmp = Qnil;
11264 ASET (vector, 6, tmp);
11265 ASET (vector, 7, owin);
11266 if (target_frame)
11267 {
11268 /* Similarly to `with-selected-window', if the operation selects
11269 a window on another frame, we must restore that frame's
11270 selected window, and (for a tty) the top-frame. */
11271 ASET (vector, 8, target_frame->selected_window);
11272 if (FRAME_TERMCAP_P (target_frame))
11273 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11274 }
11275
11276 return vector;
11277 }
11278
11279 static void
11280 unwind_format_mode_line (Lisp_Object vector)
11281 {
11282 Lisp_Object old_window = AREF (vector, 7);
11283 Lisp_Object target_frame_window = AREF (vector, 8);
11284 Lisp_Object old_top_frame = AREF (vector, 9);
11285
11286 mode_line_target = XINT (AREF (vector, 0));
11287 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11288 mode_line_string_list = AREF (vector, 2);
11289 if (! EQ (AREF (vector, 3), Qt))
11290 mode_line_proptrans_alist = AREF (vector, 3);
11291 mode_line_string_face = AREF (vector, 4);
11292 mode_line_string_face_prop = AREF (vector, 5);
11293
11294 /* Select window before buffer, since it may change the buffer. */
11295 if (!NILP (old_window))
11296 {
11297 /* If the operation that we are unwinding had selected a window
11298 on a different frame, reset its frame-selected-window. For a
11299 text terminal, reset its top-frame if necessary. */
11300 if (!NILP (target_frame_window))
11301 {
11302 Lisp_Object frame
11303 = WINDOW_FRAME (XWINDOW (target_frame_window));
11304
11305 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11306 Fselect_window (target_frame_window, Qt);
11307
11308 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11309 Fselect_frame (old_top_frame, Qt);
11310 }
11311
11312 Fselect_window (old_window, Qt);
11313 }
11314
11315 if (!NILP (AREF (vector, 6)))
11316 {
11317 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11318 ASET (vector, 6, Qnil);
11319 }
11320
11321 Vmode_line_unwind_vector = vector;
11322 }
11323
11324
11325 /* Store a single character C for the frame title in mode_line_noprop_buf.
11326 Re-allocate mode_line_noprop_buf if necessary. */
11327
11328 static void
11329 store_mode_line_noprop_char (char c)
11330 {
11331 /* If output position has reached the end of the allocated buffer,
11332 increase the buffer's size. */
11333 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11334 {
11335 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11336 ptrdiff_t size = len;
11337 mode_line_noprop_buf =
11338 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11339 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11340 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11341 }
11342
11343 *mode_line_noprop_ptr++ = c;
11344 }
11345
11346
11347 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11348 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11349 characters that yield more columns than PRECISION; PRECISION <= 0
11350 means copy the whole string. Pad with spaces until FIELD_WIDTH
11351 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11352 pad. Called from display_mode_element when it is used to build a
11353 frame title. */
11354
11355 static int
11356 store_mode_line_noprop (const char *string, int field_width, int precision)
11357 {
11358 const unsigned char *str = (const unsigned char *) string;
11359 int n = 0;
11360 ptrdiff_t dummy, nbytes;
11361
11362 /* Copy at most PRECISION chars from STR. */
11363 nbytes = strlen (string);
11364 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11365 while (nbytes--)
11366 store_mode_line_noprop_char (*str++);
11367
11368 /* Fill up with spaces until FIELD_WIDTH reached. */
11369 while (field_width > 0
11370 && n < field_width)
11371 {
11372 store_mode_line_noprop_char (' ');
11373 ++n;
11374 }
11375
11376 return n;
11377 }
11378
11379 /***********************************************************************
11380 Frame Titles
11381 ***********************************************************************/
11382
11383 #ifdef HAVE_WINDOW_SYSTEM
11384
11385 /* Set the title of FRAME, if it has changed. The title format is
11386 Vicon_title_format if FRAME is iconified, otherwise it is
11387 frame_title_format. */
11388
11389 static void
11390 x_consider_frame_title (Lisp_Object frame)
11391 {
11392 struct frame *f = XFRAME (frame);
11393
11394 if (FRAME_WINDOW_P (f)
11395 || FRAME_MINIBUF_ONLY_P (f)
11396 || f->explicit_name)
11397 {
11398 /* Do we have more than one visible frame on this X display? */
11399 Lisp_Object tail, other_frame, fmt;
11400 ptrdiff_t title_start;
11401 char *title;
11402 ptrdiff_t len;
11403 struct it it;
11404 ptrdiff_t count = SPECPDL_INDEX ();
11405
11406 FOR_EACH_FRAME (tail, other_frame)
11407 {
11408 struct frame *tf = XFRAME (other_frame);
11409
11410 if (tf != f
11411 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11412 && !FRAME_MINIBUF_ONLY_P (tf)
11413 && !EQ (other_frame, tip_frame)
11414 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11415 break;
11416 }
11417
11418 /* Set global variable indicating that multiple frames exist. */
11419 multiple_frames = CONSP (tail);
11420
11421 /* Switch to the buffer of selected window of the frame. Set up
11422 mode_line_target so that display_mode_element will output into
11423 mode_line_noprop_buf; then display the title. */
11424 record_unwind_protect (unwind_format_mode_line,
11425 format_mode_line_unwind_data
11426 (f, current_buffer, selected_window, 0));
11427
11428 Fselect_window (f->selected_window, Qt);
11429 set_buffer_internal_1
11430 (XBUFFER (XWINDOW (f->selected_window)->contents));
11431 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11432
11433 mode_line_target = MODE_LINE_TITLE;
11434 title_start = MODE_LINE_NOPROP_LEN (0);
11435 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11436 NULL, DEFAULT_FACE_ID);
11437 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11438 len = MODE_LINE_NOPROP_LEN (title_start);
11439 title = mode_line_noprop_buf + title_start;
11440 unbind_to (count, Qnil);
11441
11442 /* Set the title only if it's changed. This avoids consing in
11443 the common case where it hasn't. (If it turns out that we've
11444 already wasted too much time by walking through the list with
11445 display_mode_element, then we might need to optimize at a
11446 higher level than this.) */
11447 if (! STRINGP (f->name)
11448 || SBYTES (f->name) != len
11449 || memcmp (title, SDATA (f->name), len) != 0)
11450 x_implicitly_set_name (f, make_string (title, len), Qnil);
11451 }
11452 }
11453
11454 #endif /* not HAVE_WINDOW_SYSTEM */
11455
11456 \f
11457 /***********************************************************************
11458 Menu Bars
11459 ***********************************************************************/
11460
11461 /* Non-zero if we will not redisplay all visible windows. */
11462 #define REDISPLAY_SOME_P() \
11463 ((windows_or_buffers_changed == 0 \
11464 || windows_or_buffers_changed == REDISPLAY_SOME) \
11465 && (update_mode_lines == 0 \
11466 || update_mode_lines == REDISPLAY_SOME))
11467
11468 /* Prepare for redisplay by updating menu-bar item lists when
11469 appropriate. This can call eval. */
11470
11471 static void
11472 prepare_menu_bars (void)
11473 {
11474 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11475 bool some_windows = REDISPLAY_SOME_P ();
11476 struct gcpro gcpro1, gcpro2;
11477 Lisp_Object tooltip_frame;
11478
11479 #ifdef HAVE_WINDOW_SYSTEM
11480 tooltip_frame = tip_frame;
11481 #else
11482 tooltip_frame = Qnil;
11483 #endif
11484
11485 if (FUNCTIONP (Vpre_redisplay_function))
11486 {
11487 Lisp_Object windows = all_windows ? Qt : Qnil;
11488 if (all_windows && some_windows)
11489 {
11490 Lisp_Object ws = window_list ();
11491 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11492 {
11493 Lisp_Object this = XCAR (ws);
11494 struct window *w = XWINDOW (this);
11495 if (w->redisplay
11496 || XFRAME (w->frame)->redisplay
11497 || XBUFFER (w->contents)->text->redisplay)
11498 {
11499 windows = Fcons (this, windows);
11500 }
11501 }
11502 }
11503 safe_call1 (Vpre_redisplay_function, windows);
11504 }
11505
11506 /* Update all frame titles based on their buffer names, etc. We do
11507 this before the menu bars so that the buffer-menu will show the
11508 up-to-date frame titles. */
11509 #ifdef HAVE_WINDOW_SYSTEM
11510 if (all_windows)
11511 {
11512 Lisp_Object tail, frame;
11513
11514 FOR_EACH_FRAME (tail, frame)
11515 {
11516 struct frame *f = XFRAME (frame);
11517 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11518 if (some_windows
11519 && !f->redisplay
11520 && !w->redisplay
11521 && !XBUFFER (w->contents)->text->redisplay)
11522 continue;
11523
11524 if (!EQ (frame, tooltip_frame)
11525 && (FRAME_ICONIFIED_P (f)
11526 || FRAME_VISIBLE_P (f) == 1
11527 /* Exclude TTY frames that are obscured because they
11528 are not the top frame on their console. This is
11529 because x_consider_frame_title actually switches
11530 to the frame, which for TTY frames means it is
11531 marked as garbaged, and will be completely
11532 redrawn on the next redisplay cycle. This causes
11533 TTY frames to be completely redrawn, when there
11534 are more than one of them, even though nothing
11535 should be changed on display. */
11536 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11537 x_consider_frame_title (frame);
11538 }
11539 }
11540 #endif /* HAVE_WINDOW_SYSTEM */
11541
11542 /* Update the menu bar item lists, if appropriate. This has to be
11543 done before any actual redisplay or generation of display lines. */
11544
11545 if (all_windows)
11546 {
11547 Lisp_Object tail, frame;
11548 ptrdiff_t count = SPECPDL_INDEX ();
11549 /* 1 means that update_menu_bar has run its hooks
11550 so any further calls to update_menu_bar shouldn't do so again. */
11551 int menu_bar_hooks_run = 0;
11552
11553 record_unwind_save_match_data ();
11554
11555 FOR_EACH_FRAME (tail, frame)
11556 {
11557 struct frame *f = XFRAME (frame);
11558 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11559
11560 /* Ignore tooltip frame. */
11561 if (EQ (frame, tooltip_frame))
11562 continue;
11563
11564 if (some_windows
11565 && !f->redisplay
11566 && !w->redisplay
11567 && !XBUFFER (w->contents)->text->redisplay)
11568 continue;
11569
11570 /* If a window on this frame changed size, report that to
11571 the user and clear the size-change flag. */
11572 if (FRAME_WINDOW_SIZES_CHANGED (f))
11573 {
11574 Lisp_Object functions;
11575
11576 /* Clear flag first in case we get an error below. */
11577 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11578 functions = Vwindow_size_change_functions;
11579 GCPRO2 (tail, functions);
11580
11581 while (CONSP (functions))
11582 {
11583 if (!EQ (XCAR (functions), Qt))
11584 call1 (XCAR (functions), frame);
11585 functions = XCDR (functions);
11586 }
11587 UNGCPRO;
11588 }
11589
11590 GCPRO1 (tail);
11591 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11592 #ifdef HAVE_WINDOW_SYSTEM
11593 update_tool_bar (f, 0);
11594 #endif
11595 #ifdef HAVE_NS
11596 if (windows_or_buffers_changed
11597 && FRAME_NS_P (f))
11598 ns_set_doc_edited
11599 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11600 #endif
11601 UNGCPRO;
11602 }
11603
11604 unbind_to (count, Qnil);
11605 }
11606 else
11607 {
11608 struct frame *sf = SELECTED_FRAME ();
11609 update_menu_bar (sf, 1, 0);
11610 #ifdef HAVE_WINDOW_SYSTEM
11611 update_tool_bar (sf, 1);
11612 #endif
11613 }
11614 }
11615
11616
11617 /* Update the menu bar item list for frame F. This has to be done
11618 before we start to fill in any display lines, because it can call
11619 eval.
11620
11621 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11622
11623 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11624 already ran the menu bar hooks for this redisplay, so there
11625 is no need to run them again. The return value is the
11626 updated value of this flag, to pass to the next call. */
11627
11628 static int
11629 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11630 {
11631 Lisp_Object window;
11632 register struct window *w;
11633
11634 /* If called recursively during a menu update, do nothing. This can
11635 happen when, for instance, an activate-menubar-hook causes a
11636 redisplay. */
11637 if (inhibit_menubar_update)
11638 return hooks_run;
11639
11640 window = FRAME_SELECTED_WINDOW (f);
11641 w = XWINDOW (window);
11642
11643 if (FRAME_WINDOW_P (f)
11644 ?
11645 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11646 || defined (HAVE_NS) || defined (USE_GTK)
11647 FRAME_EXTERNAL_MENU_BAR (f)
11648 #else
11649 FRAME_MENU_BAR_LINES (f) > 0
11650 #endif
11651 : FRAME_MENU_BAR_LINES (f) > 0)
11652 {
11653 /* If the user has switched buffers or windows, we need to
11654 recompute to reflect the new bindings. But we'll
11655 recompute when update_mode_lines is set too; that means
11656 that people can use force-mode-line-update to request
11657 that the menu bar be recomputed. The adverse effect on
11658 the rest of the redisplay algorithm is about the same as
11659 windows_or_buffers_changed anyway. */
11660 if (windows_or_buffers_changed
11661 /* This used to test w->update_mode_line, but we believe
11662 there is no need to recompute the menu in that case. */
11663 || update_mode_lines
11664 || window_buffer_changed (w))
11665 {
11666 struct buffer *prev = current_buffer;
11667 ptrdiff_t count = SPECPDL_INDEX ();
11668
11669 specbind (Qinhibit_menubar_update, Qt);
11670
11671 set_buffer_internal_1 (XBUFFER (w->contents));
11672 if (save_match_data)
11673 record_unwind_save_match_data ();
11674 if (NILP (Voverriding_local_map_menu_flag))
11675 {
11676 specbind (Qoverriding_terminal_local_map, Qnil);
11677 specbind (Qoverriding_local_map, Qnil);
11678 }
11679
11680 if (!hooks_run)
11681 {
11682 /* Run the Lucid hook. */
11683 safe_run_hooks (Qactivate_menubar_hook);
11684
11685 /* If it has changed current-menubar from previous value,
11686 really recompute the menu-bar from the value. */
11687 if (! NILP (Vlucid_menu_bar_dirty_flag))
11688 call0 (Qrecompute_lucid_menubar);
11689
11690 safe_run_hooks (Qmenu_bar_update_hook);
11691
11692 hooks_run = 1;
11693 }
11694
11695 XSETFRAME (Vmenu_updating_frame, f);
11696 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11697
11698 /* Redisplay the menu bar in case we changed it. */
11699 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11700 || defined (HAVE_NS) || defined (USE_GTK)
11701 if (FRAME_WINDOW_P (f))
11702 {
11703 #if defined (HAVE_NS)
11704 /* All frames on Mac OS share the same menubar. So only
11705 the selected frame should be allowed to set it. */
11706 if (f == SELECTED_FRAME ())
11707 #endif
11708 set_frame_menubar (f, 0, 0);
11709 }
11710 else
11711 /* On a terminal screen, the menu bar is an ordinary screen
11712 line, and this makes it get updated. */
11713 w->update_mode_line = 1;
11714 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11715 /* In the non-toolkit version, the menu bar is an ordinary screen
11716 line, and this makes it get updated. */
11717 w->update_mode_line = 1;
11718 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11719
11720 unbind_to (count, Qnil);
11721 set_buffer_internal_1 (prev);
11722 }
11723 }
11724
11725 return hooks_run;
11726 }
11727
11728 /***********************************************************************
11729 Tool-bars
11730 ***********************************************************************/
11731
11732 #ifdef HAVE_WINDOW_SYSTEM
11733
11734 /* Tool-bar item index of the item on which a mouse button was pressed
11735 or -1. */
11736
11737 int last_tool_bar_item;
11738
11739 /* Select `frame' temporarily without running all the code in
11740 do_switch_frame.
11741 FIXME: Maybe do_switch_frame should be trimmed down similarly
11742 when `norecord' is set. */
11743 static void
11744 fast_set_selected_frame (Lisp_Object frame)
11745 {
11746 if (!EQ (selected_frame, frame))
11747 {
11748 selected_frame = frame;
11749 selected_window = XFRAME (frame)->selected_window;
11750 }
11751 }
11752
11753 /* Update the tool-bar item list for frame F. This has to be done
11754 before we start to fill in any display lines. Called from
11755 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11756 and restore it here. */
11757
11758 static void
11759 update_tool_bar (struct frame *f, int save_match_data)
11760 {
11761 #if defined (USE_GTK) || defined (HAVE_NS)
11762 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11763 #else
11764 int do_update = (WINDOWP (f->tool_bar_window)
11765 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11766 #endif
11767
11768 if (do_update)
11769 {
11770 Lisp_Object window;
11771 struct window *w;
11772
11773 window = FRAME_SELECTED_WINDOW (f);
11774 w = XWINDOW (window);
11775
11776 /* If the user has switched buffers or windows, we need to
11777 recompute to reflect the new bindings. But we'll
11778 recompute when update_mode_lines is set too; that means
11779 that people can use force-mode-line-update to request
11780 that the menu bar be recomputed. The adverse effect on
11781 the rest of the redisplay algorithm is about the same as
11782 windows_or_buffers_changed anyway. */
11783 if (windows_or_buffers_changed
11784 || w->update_mode_line
11785 || update_mode_lines
11786 || window_buffer_changed (w))
11787 {
11788 struct buffer *prev = current_buffer;
11789 ptrdiff_t count = SPECPDL_INDEX ();
11790 Lisp_Object frame, new_tool_bar;
11791 int new_n_tool_bar;
11792 struct gcpro gcpro1;
11793
11794 /* Set current_buffer to the buffer of the selected
11795 window of the frame, so that we get the right local
11796 keymaps. */
11797 set_buffer_internal_1 (XBUFFER (w->contents));
11798
11799 /* Save match data, if we must. */
11800 if (save_match_data)
11801 record_unwind_save_match_data ();
11802
11803 /* Make sure that we don't accidentally use bogus keymaps. */
11804 if (NILP (Voverriding_local_map_menu_flag))
11805 {
11806 specbind (Qoverriding_terminal_local_map, Qnil);
11807 specbind (Qoverriding_local_map, Qnil);
11808 }
11809
11810 GCPRO1 (new_tool_bar);
11811
11812 /* We must temporarily set the selected frame to this frame
11813 before calling tool_bar_items, because the calculation of
11814 the tool-bar keymap uses the selected frame (see
11815 `tool-bar-make-keymap' in tool-bar.el). */
11816 eassert (EQ (selected_window,
11817 /* Since we only explicitly preserve selected_frame,
11818 check that selected_window would be redundant. */
11819 XFRAME (selected_frame)->selected_window));
11820 record_unwind_protect (fast_set_selected_frame, selected_frame);
11821 XSETFRAME (frame, f);
11822 fast_set_selected_frame (frame);
11823
11824 /* Build desired tool-bar items from keymaps. */
11825 new_tool_bar
11826 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11827 &new_n_tool_bar);
11828
11829 /* Redisplay the tool-bar if we changed it. */
11830 if (new_n_tool_bar != f->n_tool_bar_items
11831 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11832 {
11833 /* Redisplay that happens asynchronously due to an expose event
11834 may access f->tool_bar_items. Make sure we update both
11835 variables within BLOCK_INPUT so no such event interrupts. */
11836 block_input ();
11837 fset_tool_bar_items (f, new_tool_bar);
11838 f->n_tool_bar_items = new_n_tool_bar;
11839 w->update_mode_line = 1;
11840 unblock_input ();
11841 }
11842
11843 UNGCPRO;
11844
11845 unbind_to (count, Qnil);
11846 set_buffer_internal_1 (prev);
11847 }
11848 }
11849 }
11850
11851 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11852
11853 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11854 F's desired tool-bar contents. F->tool_bar_items must have
11855 been set up previously by calling prepare_menu_bars. */
11856
11857 static void
11858 build_desired_tool_bar_string (struct frame *f)
11859 {
11860 int i, size, size_needed;
11861 struct gcpro gcpro1, gcpro2, gcpro3;
11862 Lisp_Object image, plist, props;
11863
11864 image = plist = props = Qnil;
11865 GCPRO3 (image, plist, props);
11866
11867 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11868 Otherwise, make a new string. */
11869
11870 /* The size of the string we might be able to reuse. */
11871 size = (STRINGP (f->desired_tool_bar_string)
11872 ? SCHARS (f->desired_tool_bar_string)
11873 : 0);
11874
11875 /* We need one space in the string for each image. */
11876 size_needed = f->n_tool_bar_items;
11877
11878 /* Reuse f->desired_tool_bar_string, if possible. */
11879 if (size < size_needed || NILP (f->desired_tool_bar_string))
11880 fset_desired_tool_bar_string
11881 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11882 else
11883 {
11884 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11885 Fremove_text_properties (make_number (0), make_number (size),
11886 props, f->desired_tool_bar_string);
11887 }
11888
11889 /* Put a `display' property on the string for the images to display,
11890 put a `menu_item' property on tool-bar items with a value that
11891 is the index of the item in F's tool-bar item vector. */
11892 for (i = 0; i < f->n_tool_bar_items; ++i)
11893 {
11894 #define PROP(IDX) \
11895 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11896
11897 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11898 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11899 int hmargin, vmargin, relief, idx, end;
11900
11901 /* If image is a vector, choose the image according to the
11902 button state. */
11903 image = PROP (TOOL_BAR_ITEM_IMAGES);
11904 if (VECTORP (image))
11905 {
11906 if (enabled_p)
11907 idx = (selected_p
11908 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11909 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11910 else
11911 idx = (selected_p
11912 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11913 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11914
11915 eassert (ASIZE (image) >= idx);
11916 image = AREF (image, idx);
11917 }
11918 else
11919 idx = -1;
11920
11921 /* Ignore invalid image specifications. */
11922 if (!valid_image_p (image))
11923 continue;
11924
11925 /* Display the tool-bar button pressed, or depressed. */
11926 plist = Fcopy_sequence (XCDR (image));
11927
11928 /* Compute margin and relief to draw. */
11929 relief = (tool_bar_button_relief >= 0
11930 ? tool_bar_button_relief
11931 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11932 hmargin = vmargin = relief;
11933
11934 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11935 INT_MAX - max (hmargin, vmargin)))
11936 {
11937 hmargin += XFASTINT (Vtool_bar_button_margin);
11938 vmargin += XFASTINT (Vtool_bar_button_margin);
11939 }
11940 else if (CONSP (Vtool_bar_button_margin))
11941 {
11942 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11943 INT_MAX - hmargin))
11944 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11945
11946 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11947 INT_MAX - vmargin))
11948 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11949 }
11950
11951 if (auto_raise_tool_bar_buttons_p)
11952 {
11953 /* Add a `:relief' property to the image spec if the item is
11954 selected. */
11955 if (selected_p)
11956 {
11957 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11958 hmargin -= relief;
11959 vmargin -= relief;
11960 }
11961 }
11962 else
11963 {
11964 /* If image is selected, display it pressed, i.e. with a
11965 negative relief. If it's not selected, display it with a
11966 raised relief. */
11967 plist = Fplist_put (plist, QCrelief,
11968 (selected_p
11969 ? make_number (-relief)
11970 : make_number (relief)));
11971 hmargin -= relief;
11972 vmargin -= relief;
11973 }
11974
11975 /* Put a margin around the image. */
11976 if (hmargin || vmargin)
11977 {
11978 if (hmargin == vmargin)
11979 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11980 else
11981 plist = Fplist_put (plist, QCmargin,
11982 Fcons (make_number (hmargin),
11983 make_number (vmargin)));
11984 }
11985
11986 /* If button is not enabled, and we don't have special images
11987 for the disabled state, make the image appear disabled by
11988 applying an appropriate algorithm to it. */
11989 if (!enabled_p && idx < 0)
11990 plist = Fplist_put (plist, QCconversion, Qdisabled);
11991
11992 /* Put a `display' text property on the string for the image to
11993 display. Put a `menu-item' property on the string that gives
11994 the start of this item's properties in the tool-bar items
11995 vector. */
11996 image = Fcons (Qimage, plist);
11997 props = list4 (Qdisplay, image,
11998 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11999
12000 /* Let the last image hide all remaining spaces in the tool bar
12001 string. The string can be longer than needed when we reuse a
12002 previous string. */
12003 if (i + 1 == f->n_tool_bar_items)
12004 end = SCHARS (f->desired_tool_bar_string);
12005 else
12006 end = i + 1;
12007 Fadd_text_properties (make_number (i), make_number (end),
12008 props, f->desired_tool_bar_string);
12009 #undef PROP
12010 }
12011
12012 UNGCPRO;
12013 }
12014
12015
12016 /* Display one line of the tool-bar of frame IT->f.
12017
12018 HEIGHT specifies the desired height of the tool-bar line.
12019 If the actual height of the glyph row is less than HEIGHT, the
12020 row's height is increased to HEIGHT, and the icons are centered
12021 vertically in the new height.
12022
12023 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12024 count a final empty row in case the tool-bar width exactly matches
12025 the window width.
12026 */
12027
12028 static void
12029 display_tool_bar_line (struct it *it, int height)
12030 {
12031 struct glyph_row *row = it->glyph_row;
12032 int max_x = it->last_visible_x;
12033 struct glyph *last;
12034
12035 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12036 clear_glyph_row (row);
12037 row->enabled_p = true;
12038 row->y = it->current_y;
12039
12040 /* Note that this isn't made use of if the face hasn't a box,
12041 so there's no need to check the face here. */
12042 it->start_of_box_run_p = 1;
12043
12044 while (it->current_x < max_x)
12045 {
12046 int x, n_glyphs_before, i, nglyphs;
12047 struct it it_before;
12048
12049 /* Get the next display element. */
12050 if (!get_next_display_element (it))
12051 {
12052 /* Don't count empty row if we are counting needed tool-bar lines. */
12053 if (height < 0 && !it->hpos)
12054 return;
12055 break;
12056 }
12057
12058 /* Produce glyphs. */
12059 n_glyphs_before = row->used[TEXT_AREA];
12060 it_before = *it;
12061
12062 PRODUCE_GLYPHS (it);
12063
12064 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12065 i = 0;
12066 x = it_before.current_x;
12067 while (i < nglyphs)
12068 {
12069 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12070
12071 if (x + glyph->pixel_width > max_x)
12072 {
12073 /* Glyph doesn't fit on line. Backtrack. */
12074 row->used[TEXT_AREA] = n_glyphs_before;
12075 *it = it_before;
12076 /* If this is the only glyph on this line, it will never fit on the
12077 tool-bar, so skip it. But ensure there is at least one glyph,
12078 so we don't accidentally disable the tool-bar. */
12079 if (n_glyphs_before == 0
12080 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12081 break;
12082 goto out;
12083 }
12084
12085 ++it->hpos;
12086 x += glyph->pixel_width;
12087 ++i;
12088 }
12089
12090 /* Stop at line end. */
12091 if (ITERATOR_AT_END_OF_LINE_P (it))
12092 break;
12093
12094 set_iterator_to_next (it, 1);
12095 }
12096
12097 out:;
12098
12099 row->displays_text_p = row->used[TEXT_AREA] != 0;
12100
12101 /* Use default face for the border below the tool bar.
12102
12103 FIXME: When auto-resize-tool-bars is grow-only, there is
12104 no additional border below the possibly empty tool-bar lines.
12105 So to make the extra empty lines look "normal", we have to
12106 use the tool-bar face for the border too. */
12107 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12108 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12109 it->face_id = DEFAULT_FACE_ID;
12110
12111 extend_face_to_end_of_line (it);
12112 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12113 last->right_box_line_p = 1;
12114 if (last == row->glyphs[TEXT_AREA])
12115 last->left_box_line_p = 1;
12116
12117 /* Make line the desired height and center it vertically. */
12118 if ((height -= it->max_ascent + it->max_descent) > 0)
12119 {
12120 /* Don't add more than one line height. */
12121 height %= FRAME_LINE_HEIGHT (it->f);
12122 it->max_ascent += height / 2;
12123 it->max_descent += (height + 1) / 2;
12124 }
12125
12126 compute_line_metrics (it);
12127
12128 /* If line is empty, make it occupy the rest of the tool-bar. */
12129 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12130 {
12131 row->height = row->phys_height = it->last_visible_y - row->y;
12132 row->visible_height = row->height;
12133 row->ascent = row->phys_ascent = 0;
12134 row->extra_line_spacing = 0;
12135 }
12136
12137 row->full_width_p = 1;
12138 row->continued_p = 0;
12139 row->truncated_on_left_p = 0;
12140 row->truncated_on_right_p = 0;
12141
12142 it->current_x = it->hpos = 0;
12143 it->current_y += row->height;
12144 ++it->vpos;
12145 ++it->glyph_row;
12146 }
12147
12148
12149 /* Max tool-bar height. Basically, this is what makes all other windows
12150 disappear when the frame gets too small. Rethink this! */
12151
12152 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12153 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12154
12155 /* Value is the number of pixels needed to make all tool-bar items of
12156 frame F visible. The actual number of glyph rows needed is
12157 returned in *N_ROWS if non-NULL. */
12158
12159 static int
12160 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12161 {
12162 struct window *w = XWINDOW (f->tool_bar_window);
12163 struct it it;
12164 /* tool_bar_height is called from redisplay_tool_bar after building
12165 the desired matrix, so use (unused) mode-line row as temporary row to
12166 avoid destroying the first tool-bar row. */
12167 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12168
12169 /* Initialize an iterator for iteration over
12170 F->desired_tool_bar_string in the tool-bar window of frame F. */
12171 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12172 it.first_visible_x = 0;
12173 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12174 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12175 it.paragraph_embedding = L2R;
12176
12177 while (!ITERATOR_AT_END_P (&it))
12178 {
12179 clear_glyph_row (temp_row);
12180 it.glyph_row = temp_row;
12181 display_tool_bar_line (&it, -1);
12182 }
12183 clear_glyph_row (temp_row);
12184
12185 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12186 if (n_rows)
12187 *n_rows = it.vpos > 0 ? it.vpos : -1;
12188
12189 if (pixelwise)
12190 return it.current_y;
12191 else
12192 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12193 }
12194
12195 #endif /* !USE_GTK && !HAVE_NS */
12196
12197 #if defined USE_GTK || defined HAVE_NS
12198 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12199 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12200 #endif
12201
12202 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12203 0, 2, 0,
12204 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12205 If FRAME is nil or omitted, use the selected frame. Optional argument
12206 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12207 (Lisp_Object frame, Lisp_Object pixelwise)
12208 {
12209 int height = 0;
12210
12211 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12212 struct frame *f = decode_any_frame (frame);
12213
12214 if (WINDOWP (f->tool_bar_window)
12215 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12216 {
12217 update_tool_bar (f, 1);
12218 if (f->n_tool_bar_items)
12219 {
12220 build_desired_tool_bar_string (f);
12221 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12222 }
12223 }
12224 #endif
12225
12226 return make_number (height);
12227 }
12228
12229
12230 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12231 height should be changed. */
12232
12233 static int
12234 redisplay_tool_bar (struct frame *f)
12235 {
12236 #if defined (USE_GTK) || defined (HAVE_NS)
12237
12238 if (FRAME_EXTERNAL_TOOL_BAR (f))
12239 update_frame_tool_bar (f);
12240 return 0;
12241
12242 #else /* !USE_GTK && !HAVE_NS */
12243
12244 struct window *w;
12245 struct it it;
12246 struct glyph_row *row;
12247
12248 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12249 do anything. This means you must start with tool-bar-lines
12250 non-zero to get the auto-sizing effect. Or in other words, you
12251 can turn off tool-bars by specifying tool-bar-lines zero. */
12252 if (!WINDOWP (f->tool_bar_window)
12253 || (w = XWINDOW (f->tool_bar_window),
12254 WINDOW_PIXEL_HEIGHT (w) == 0))
12255 return 0;
12256
12257 /* Set up an iterator for the tool-bar window. */
12258 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12259 it.first_visible_x = 0;
12260 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12261 row = it.glyph_row;
12262
12263 /* Build a string that represents the contents of the tool-bar. */
12264 build_desired_tool_bar_string (f);
12265 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12266 /* FIXME: This should be controlled by a user option. But it
12267 doesn't make sense to have an R2L tool bar if the menu bar cannot
12268 be drawn also R2L, and making the menu bar R2L is tricky due
12269 toolkit-specific code that implements it. If an R2L tool bar is
12270 ever supported, display_tool_bar_line should also be augmented to
12271 call unproduce_glyphs like display_line and display_string
12272 do. */
12273 it.paragraph_embedding = L2R;
12274
12275 if (f->n_tool_bar_rows == 0)
12276 {
12277 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12278
12279 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12280 {
12281 Lisp_Object frame;
12282 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12283 / FRAME_LINE_HEIGHT (f));
12284
12285 XSETFRAME (frame, f);
12286 Fmodify_frame_parameters (frame,
12287 list1 (Fcons (Qtool_bar_lines,
12288 make_number (new_lines))));
12289 /* Always do that now. */
12290 clear_glyph_matrix (w->desired_matrix);
12291 f->fonts_changed = 1;
12292 return 1;
12293 }
12294 }
12295
12296 /* Display as many lines as needed to display all tool-bar items. */
12297
12298 if (f->n_tool_bar_rows > 0)
12299 {
12300 int border, rows, height, extra;
12301
12302 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12303 border = XINT (Vtool_bar_border);
12304 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12305 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12306 else if (EQ (Vtool_bar_border, Qborder_width))
12307 border = f->border_width;
12308 else
12309 border = 0;
12310 if (border < 0)
12311 border = 0;
12312
12313 rows = f->n_tool_bar_rows;
12314 height = max (1, (it.last_visible_y - border) / rows);
12315 extra = it.last_visible_y - border - height * rows;
12316
12317 while (it.current_y < it.last_visible_y)
12318 {
12319 int h = 0;
12320 if (extra > 0 && rows-- > 0)
12321 {
12322 h = (extra + rows - 1) / rows;
12323 extra -= h;
12324 }
12325 display_tool_bar_line (&it, height + h);
12326 }
12327 }
12328 else
12329 {
12330 while (it.current_y < it.last_visible_y)
12331 display_tool_bar_line (&it, 0);
12332 }
12333
12334 /* It doesn't make much sense to try scrolling in the tool-bar
12335 window, so don't do it. */
12336 w->desired_matrix->no_scrolling_p = 1;
12337 w->must_be_updated_p = 1;
12338
12339 if (!NILP (Vauto_resize_tool_bars))
12340 {
12341 /* Do we really allow the toolbar to occupy the whole frame? */
12342 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12343 int change_height_p = 0;
12344
12345 /* If we couldn't display everything, change the tool-bar's
12346 height if there is room for more. */
12347 if (IT_STRING_CHARPOS (it) < it.end_charpos
12348 && it.current_y < max_tool_bar_height)
12349 change_height_p = 1;
12350
12351 /* We subtract 1 because display_tool_bar_line advances the
12352 glyph_row pointer before returning to its caller. We want to
12353 examine the last glyph row produced by
12354 display_tool_bar_line. */
12355 row = it.glyph_row - 1;
12356
12357 /* If there are blank lines at the end, except for a partially
12358 visible blank line at the end that is smaller than
12359 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12360 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12361 && row->height >= FRAME_LINE_HEIGHT (f))
12362 change_height_p = 1;
12363
12364 /* If row displays tool-bar items, but is partially visible,
12365 change the tool-bar's height. */
12366 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12367 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12368 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12369 change_height_p = 1;
12370
12371 /* Resize windows as needed by changing the `tool-bar-lines'
12372 frame parameter. */
12373 if (change_height_p)
12374 {
12375 Lisp_Object frame;
12376 int nrows;
12377 int new_height = tool_bar_height (f, &nrows, 1);
12378
12379 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12380 && !f->minimize_tool_bar_window_p)
12381 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12382 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12383 f->minimize_tool_bar_window_p = 0;
12384
12385 if (change_height_p)
12386 {
12387 /* Current size of the tool-bar window in canonical line
12388 units. */
12389 int old_lines = WINDOW_TOTAL_LINES (w);
12390 /* Required size of the tool-bar window in canonical
12391 line units. */
12392 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12393 / FRAME_LINE_HEIGHT (f));
12394 /* Maximum size of the tool-bar window in canonical line
12395 units that this frame can allow. */
12396 int max_lines =
12397 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12398
12399 /* Don't try to change the tool-bar window size and set
12400 the fonts_changed flag unless really necessary. That
12401 flag causes redisplay to give up and retry
12402 redisplaying the frame from scratch, so setting it
12403 unnecessarily can lead to nasty redisplay loops. */
12404 if (new_lines <= max_lines
12405 && eabs (new_lines - old_lines) >= 1)
12406 {
12407 XSETFRAME (frame, f);
12408 Fmodify_frame_parameters (frame,
12409 list1 (Fcons (Qtool_bar_lines,
12410 make_number (new_lines))));
12411 clear_glyph_matrix (w->desired_matrix);
12412 f->n_tool_bar_rows = nrows;
12413 f->fonts_changed = 1;
12414 return 1;
12415 }
12416 }
12417 }
12418 }
12419
12420 f->minimize_tool_bar_window_p = 0;
12421 return 0;
12422
12423 #endif /* USE_GTK || HAVE_NS */
12424 }
12425
12426 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12427
12428 /* Get information about the tool-bar item which is displayed in GLYPH
12429 on frame F. Return in *PROP_IDX the index where tool-bar item
12430 properties start in F->tool_bar_items. Value is zero if
12431 GLYPH doesn't display a tool-bar item. */
12432
12433 static int
12434 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12435 {
12436 Lisp_Object prop;
12437 int success_p;
12438 int charpos;
12439
12440 /* This function can be called asynchronously, which means we must
12441 exclude any possibility that Fget_text_property signals an
12442 error. */
12443 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12444 charpos = max (0, charpos);
12445
12446 /* Get the text property `menu-item' at pos. The value of that
12447 property is the start index of this item's properties in
12448 F->tool_bar_items. */
12449 prop = Fget_text_property (make_number (charpos),
12450 Qmenu_item, f->current_tool_bar_string);
12451 if (INTEGERP (prop))
12452 {
12453 *prop_idx = XINT (prop);
12454 success_p = 1;
12455 }
12456 else
12457 success_p = 0;
12458
12459 return success_p;
12460 }
12461
12462 \f
12463 /* Get information about the tool-bar item at position X/Y on frame F.
12464 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12465 the current matrix of the tool-bar window of F, or NULL if not
12466 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12467 item in F->tool_bar_items. Value is
12468
12469 -1 if X/Y is not on a tool-bar item
12470 0 if X/Y is on the same item that was highlighted before.
12471 1 otherwise. */
12472
12473 static int
12474 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12475 int *hpos, int *vpos, int *prop_idx)
12476 {
12477 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12478 struct window *w = XWINDOW (f->tool_bar_window);
12479 int area;
12480
12481 /* Find the glyph under X/Y. */
12482 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12483 if (*glyph == NULL)
12484 return -1;
12485
12486 /* Get the start of this tool-bar item's properties in
12487 f->tool_bar_items. */
12488 if (!tool_bar_item_info (f, *glyph, prop_idx))
12489 return -1;
12490
12491 /* Is mouse on the highlighted item? */
12492 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12493 && *vpos >= hlinfo->mouse_face_beg_row
12494 && *vpos <= hlinfo->mouse_face_end_row
12495 && (*vpos > hlinfo->mouse_face_beg_row
12496 || *hpos >= hlinfo->mouse_face_beg_col)
12497 && (*vpos < hlinfo->mouse_face_end_row
12498 || *hpos < hlinfo->mouse_face_end_col
12499 || hlinfo->mouse_face_past_end))
12500 return 0;
12501
12502 return 1;
12503 }
12504
12505
12506 /* EXPORT:
12507 Handle mouse button event on the tool-bar of frame F, at
12508 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12509 0 for button release. MODIFIERS is event modifiers for button
12510 release. */
12511
12512 void
12513 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12514 int modifiers)
12515 {
12516 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12517 struct window *w = XWINDOW (f->tool_bar_window);
12518 int hpos, vpos, prop_idx;
12519 struct glyph *glyph;
12520 Lisp_Object enabled_p;
12521 int ts;
12522
12523 /* If not on the highlighted tool-bar item, and mouse-highlight is
12524 non-nil, return. This is so we generate the tool-bar button
12525 click only when the mouse button is released on the same item as
12526 where it was pressed. However, when mouse-highlight is disabled,
12527 generate the click when the button is released regardless of the
12528 highlight, since tool-bar items are not highlighted in that
12529 case. */
12530 frame_to_window_pixel_xy (w, &x, &y);
12531 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12532 if (ts == -1
12533 || (ts != 0 && !NILP (Vmouse_highlight)))
12534 return;
12535
12536 /* When mouse-highlight is off, generate the click for the item
12537 where the button was pressed, disregarding where it was
12538 released. */
12539 if (NILP (Vmouse_highlight) && !down_p)
12540 prop_idx = last_tool_bar_item;
12541
12542 /* If item is disabled, do nothing. */
12543 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12544 if (NILP (enabled_p))
12545 return;
12546
12547 if (down_p)
12548 {
12549 /* Show item in pressed state. */
12550 if (!NILP (Vmouse_highlight))
12551 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12552 last_tool_bar_item = prop_idx;
12553 }
12554 else
12555 {
12556 Lisp_Object key, frame;
12557 struct input_event event;
12558 EVENT_INIT (event);
12559
12560 /* Show item in released state. */
12561 if (!NILP (Vmouse_highlight))
12562 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12563
12564 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12565
12566 XSETFRAME (frame, f);
12567 event.kind = TOOL_BAR_EVENT;
12568 event.frame_or_window = frame;
12569 event.arg = frame;
12570 kbd_buffer_store_event (&event);
12571
12572 event.kind = TOOL_BAR_EVENT;
12573 event.frame_or_window = frame;
12574 event.arg = key;
12575 event.modifiers = modifiers;
12576 kbd_buffer_store_event (&event);
12577 last_tool_bar_item = -1;
12578 }
12579 }
12580
12581
12582 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12583 tool-bar window-relative coordinates X/Y. Called from
12584 note_mouse_highlight. */
12585
12586 static void
12587 note_tool_bar_highlight (struct frame *f, int x, int y)
12588 {
12589 Lisp_Object window = f->tool_bar_window;
12590 struct window *w = XWINDOW (window);
12591 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12593 int hpos, vpos;
12594 struct glyph *glyph;
12595 struct glyph_row *row;
12596 int i;
12597 Lisp_Object enabled_p;
12598 int prop_idx;
12599 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12600 int mouse_down_p, rc;
12601
12602 /* Function note_mouse_highlight is called with negative X/Y
12603 values when mouse moves outside of the frame. */
12604 if (x <= 0 || y <= 0)
12605 {
12606 clear_mouse_face (hlinfo);
12607 return;
12608 }
12609
12610 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12611 if (rc < 0)
12612 {
12613 /* Not on tool-bar item. */
12614 clear_mouse_face (hlinfo);
12615 return;
12616 }
12617 else if (rc == 0)
12618 /* On same tool-bar item as before. */
12619 goto set_help_echo;
12620
12621 clear_mouse_face (hlinfo);
12622
12623 /* Mouse is down, but on different tool-bar item? */
12624 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12625 && f == dpyinfo->last_mouse_frame);
12626
12627 if (mouse_down_p
12628 && last_tool_bar_item != prop_idx)
12629 return;
12630
12631 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12632
12633 /* If tool-bar item is not enabled, don't highlight it. */
12634 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12635 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12636 {
12637 /* Compute the x-position of the glyph. In front and past the
12638 image is a space. We include this in the highlighted area. */
12639 row = MATRIX_ROW (w->current_matrix, vpos);
12640 for (i = x = 0; i < hpos; ++i)
12641 x += row->glyphs[TEXT_AREA][i].pixel_width;
12642
12643 /* Record this as the current active region. */
12644 hlinfo->mouse_face_beg_col = hpos;
12645 hlinfo->mouse_face_beg_row = vpos;
12646 hlinfo->mouse_face_beg_x = x;
12647 hlinfo->mouse_face_past_end = 0;
12648
12649 hlinfo->mouse_face_end_col = hpos + 1;
12650 hlinfo->mouse_face_end_row = vpos;
12651 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12652 hlinfo->mouse_face_window = window;
12653 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12654
12655 /* Display it as active. */
12656 show_mouse_face (hlinfo, draw);
12657 }
12658
12659 set_help_echo:
12660
12661 /* Set help_echo_string to a help string to display for this tool-bar item.
12662 XTread_socket does the rest. */
12663 help_echo_object = help_echo_window = Qnil;
12664 help_echo_pos = -1;
12665 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12666 if (NILP (help_echo_string))
12667 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12668 }
12669
12670 #endif /* !USE_GTK && !HAVE_NS */
12671
12672 #endif /* HAVE_WINDOW_SYSTEM */
12673
12674
12675 \f
12676 /************************************************************************
12677 Horizontal scrolling
12678 ************************************************************************/
12679
12680 static int hscroll_window_tree (Lisp_Object);
12681 static int hscroll_windows (Lisp_Object);
12682
12683 /* For all leaf windows in the window tree rooted at WINDOW, set their
12684 hscroll value so that PT is (i) visible in the window, and (ii) so
12685 that it is not within a certain margin at the window's left and
12686 right border. Value is non-zero if any window's hscroll has been
12687 changed. */
12688
12689 static int
12690 hscroll_window_tree (Lisp_Object window)
12691 {
12692 int hscrolled_p = 0;
12693 int hscroll_relative_p = FLOATP (Vhscroll_step);
12694 int hscroll_step_abs = 0;
12695 double hscroll_step_rel = 0;
12696
12697 if (hscroll_relative_p)
12698 {
12699 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12700 if (hscroll_step_rel < 0)
12701 {
12702 hscroll_relative_p = 0;
12703 hscroll_step_abs = 0;
12704 }
12705 }
12706 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12707 {
12708 hscroll_step_abs = XINT (Vhscroll_step);
12709 if (hscroll_step_abs < 0)
12710 hscroll_step_abs = 0;
12711 }
12712 else
12713 hscroll_step_abs = 0;
12714
12715 while (WINDOWP (window))
12716 {
12717 struct window *w = XWINDOW (window);
12718
12719 if (WINDOWP (w->contents))
12720 hscrolled_p |= hscroll_window_tree (w->contents);
12721 else if (w->cursor.vpos >= 0)
12722 {
12723 int h_margin;
12724 int text_area_width;
12725 struct glyph_row *cursor_row;
12726 struct glyph_row *bottom_row;
12727 int row_r2l_p;
12728
12729 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12730 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12731 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12732 else
12733 cursor_row = bottom_row - 1;
12734
12735 if (!cursor_row->enabled_p)
12736 {
12737 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12738 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12739 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12740 else
12741 cursor_row = bottom_row - 1;
12742 }
12743 row_r2l_p = cursor_row->reversed_p;
12744
12745 text_area_width = window_box_width (w, TEXT_AREA);
12746
12747 /* Scroll when cursor is inside this scroll margin. */
12748 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12749
12750 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12751 /* For left-to-right rows, hscroll when cursor is either
12752 (i) inside the right hscroll margin, or (ii) if it is
12753 inside the left margin and the window is already
12754 hscrolled. */
12755 && ((!row_r2l_p
12756 && ((w->hscroll
12757 && w->cursor.x <= h_margin)
12758 || (cursor_row->enabled_p
12759 && cursor_row->truncated_on_right_p
12760 && (w->cursor.x >= text_area_width - h_margin))))
12761 /* For right-to-left rows, the logic is similar,
12762 except that rules for scrolling to left and right
12763 are reversed. E.g., if cursor.x <= h_margin, we
12764 need to hscroll "to the right" unconditionally,
12765 and that will scroll the screen to the left so as
12766 to reveal the next portion of the row. */
12767 || (row_r2l_p
12768 && ((cursor_row->enabled_p
12769 /* FIXME: It is confusing to set the
12770 truncated_on_right_p flag when R2L rows
12771 are actually truncated on the left. */
12772 && cursor_row->truncated_on_right_p
12773 && w->cursor.x <= h_margin)
12774 || (w->hscroll
12775 && (w->cursor.x >= text_area_width - h_margin))))))
12776 {
12777 struct it it;
12778 ptrdiff_t hscroll;
12779 struct buffer *saved_current_buffer;
12780 ptrdiff_t pt;
12781 int wanted_x;
12782
12783 /* Find point in a display of infinite width. */
12784 saved_current_buffer = current_buffer;
12785 current_buffer = XBUFFER (w->contents);
12786
12787 if (w == XWINDOW (selected_window))
12788 pt = PT;
12789 else
12790 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12791
12792 /* Move iterator to pt starting at cursor_row->start in
12793 a line with infinite width. */
12794 init_to_row_start (&it, w, cursor_row);
12795 it.last_visible_x = INFINITY;
12796 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12797 current_buffer = saved_current_buffer;
12798
12799 /* Position cursor in window. */
12800 if (!hscroll_relative_p && hscroll_step_abs == 0)
12801 hscroll = max (0, (it.current_x
12802 - (ITERATOR_AT_END_OF_LINE_P (&it)
12803 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12804 : (text_area_width / 2))))
12805 / FRAME_COLUMN_WIDTH (it.f);
12806 else if ((!row_r2l_p
12807 && w->cursor.x >= text_area_width - h_margin)
12808 || (row_r2l_p && w->cursor.x <= h_margin))
12809 {
12810 if (hscroll_relative_p)
12811 wanted_x = text_area_width * (1 - hscroll_step_rel)
12812 - h_margin;
12813 else
12814 wanted_x = text_area_width
12815 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12816 - h_margin;
12817 hscroll
12818 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12819 }
12820 else
12821 {
12822 if (hscroll_relative_p)
12823 wanted_x = text_area_width * hscroll_step_rel
12824 + h_margin;
12825 else
12826 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12827 + h_margin;
12828 hscroll
12829 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12830 }
12831 hscroll = max (hscroll, w->min_hscroll);
12832
12833 /* Don't prevent redisplay optimizations if hscroll
12834 hasn't changed, as it will unnecessarily slow down
12835 redisplay. */
12836 if (w->hscroll != hscroll)
12837 {
12838 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12839 w->hscroll = hscroll;
12840 hscrolled_p = 1;
12841 }
12842 }
12843 }
12844
12845 window = w->next;
12846 }
12847
12848 /* Value is non-zero if hscroll of any leaf window has been changed. */
12849 return hscrolled_p;
12850 }
12851
12852
12853 /* Set hscroll so that cursor is visible and not inside horizontal
12854 scroll margins for all windows in the tree rooted at WINDOW. See
12855 also hscroll_window_tree above. Value is non-zero if any window's
12856 hscroll has been changed. If it has, desired matrices on the frame
12857 of WINDOW are cleared. */
12858
12859 static int
12860 hscroll_windows (Lisp_Object window)
12861 {
12862 int hscrolled_p = hscroll_window_tree (window);
12863 if (hscrolled_p)
12864 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12865 return hscrolled_p;
12866 }
12867
12868
12869 \f
12870 /************************************************************************
12871 Redisplay
12872 ************************************************************************/
12873
12874 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12875 to a non-zero value. This is sometimes handy to have in a debugger
12876 session. */
12877
12878 #ifdef GLYPH_DEBUG
12879
12880 /* First and last unchanged row for try_window_id. */
12881
12882 static int debug_first_unchanged_at_end_vpos;
12883 static int debug_last_unchanged_at_beg_vpos;
12884
12885 /* Delta vpos and y. */
12886
12887 static int debug_dvpos, debug_dy;
12888
12889 /* Delta in characters and bytes for try_window_id. */
12890
12891 static ptrdiff_t debug_delta, debug_delta_bytes;
12892
12893 /* Values of window_end_pos and window_end_vpos at the end of
12894 try_window_id. */
12895
12896 static ptrdiff_t debug_end_vpos;
12897
12898 /* Append a string to W->desired_matrix->method. FMT is a printf
12899 format string. If trace_redisplay_p is true also printf the
12900 resulting string to stderr. */
12901
12902 static void debug_method_add (struct window *, char const *, ...)
12903 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12904
12905 static void
12906 debug_method_add (struct window *w, char const *fmt, ...)
12907 {
12908 void *ptr = w;
12909 char *method = w->desired_matrix->method;
12910 int len = strlen (method);
12911 int size = sizeof w->desired_matrix->method;
12912 int remaining = size - len - 1;
12913 va_list ap;
12914
12915 if (len && remaining)
12916 {
12917 method[len] = '|';
12918 --remaining, ++len;
12919 }
12920
12921 va_start (ap, fmt);
12922 vsnprintf (method + len, remaining + 1, fmt, ap);
12923 va_end (ap);
12924
12925 if (trace_redisplay_p)
12926 fprintf (stderr, "%p (%s): %s\n",
12927 ptr,
12928 ((BUFFERP (w->contents)
12929 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12930 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12931 : "no buffer"),
12932 method + len);
12933 }
12934
12935 #endif /* GLYPH_DEBUG */
12936
12937
12938 /* Value is non-zero if all changes in window W, which displays
12939 current_buffer, are in the text between START and END. START is a
12940 buffer position, END is given as a distance from Z. Used in
12941 redisplay_internal for display optimization. */
12942
12943 static int
12944 text_outside_line_unchanged_p (struct window *w,
12945 ptrdiff_t start, ptrdiff_t end)
12946 {
12947 int unchanged_p = 1;
12948
12949 /* If text or overlays have changed, see where. */
12950 if (window_outdated (w))
12951 {
12952 /* Gap in the line? */
12953 if (GPT < start || Z - GPT < end)
12954 unchanged_p = 0;
12955
12956 /* Changes start in front of the line, or end after it? */
12957 if (unchanged_p
12958 && (BEG_UNCHANGED < start - 1
12959 || END_UNCHANGED < end))
12960 unchanged_p = 0;
12961
12962 /* If selective display, can't optimize if changes start at the
12963 beginning of the line. */
12964 if (unchanged_p
12965 && INTEGERP (BVAR (current_buffer, selective_display))
12966 && XINT (BVAR (current_buffer, selective_display)) > 0
12967 && (BEG_UNCHANGED < start || GPT <= start))
12968 unchanged_p = 0;
12969
12970 /* If there are overlays at the start or end of the line, these
12971 may have overlay strings with newlines in them. A change at
12972 START, for instance, may actually concern the display of such
12973 overlay strings as well, and they are displayed on different
12974 lines. So, quickly rule out this case. (For the future, it
12975 might be desirable to implement something more telling than
12976 just BEG/END_UNCHANGED.) */
12977 if (unchanged_p)
12978 {
12979 if (BEG + BEG_UNCHANGED == start
12980 && overlay_touches_p (start))
12981 unchanged_p = 0;
12982 if (END_UNCHANGED == end
12983 && overlay_touches_p (Z - end))
12984 unchanged_p = 0;
12985 }
12986
12987 /* Under bidi reordering, adding or deleting a character in the
12988 beginning of a paragraph, before the first strong directional
12989 character, can change the base direction of the paragraph (unless
12990 the buffer specifies a fixed paragraph direction), which will
12991 require to redisplay the whole paragraph. It might be worthwhile
12992 to find the paragraph limits and widen the range of redisplayed
12993 lines to that, but for now just give up this optimization. */
12994 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12995 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12996 unchanged_p = 0;
12997 }
12998
12999 return unchanged_p;
13000 }
13001
13002
13003 /* Do a frame update, taking possible shortcuts into account. This is
13004 the main external entry point for redisplay.
13005
13006 If the last redisplay displayed an echo area message and that message
13007 is no longer requested, we clear the echo area or bring back the
13008 mini-buffer if that is in use. */
13009
13010 void
13011 redisplay (void)
13012 {
13013 redisplay_internal ();
13014 }
13015
13016
13017 static Lisp_Object
13018 overlay_arrow_string_or_property (Lisp_Object var)
13019 {
13020 Lisp_Object val;
13021
13022 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13023 return val;
13024
13025 return Voverlay_arrow_string;
13026 }
13027
13028 /* Return 1 if there are any overlay-arrows in current_buffer. */
13029 static int
13030 overlay_arrow_in_current_buffer_p (void)
13031 {
13032 Lisp_Object vlist;
13033
13034 for (vlist = Voverlay_arrow_variable_list;
13035 CONSP (vlist);
13036 vlist = XCDR (vlist))
13037 {
13038 Lisp_Object var = XCAR (vlist);
13039 Lisp_Object val;
13040
13041 if (!SYMBOLP (var))
13042 continue;
13043 val = find_symbol_value (var);
13044 if (MARKERP (val)
13045 && current_buffer == XMARKER (val)->buffer)
13046 return 1;
13047 }
13048 return 0;
13049 }
13050
13051
13052 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13053 has changed. */
13054
13055 static int
13056 overlay_arrows_changed_p (void)
13057 {
13058 Lisp_Object vlist;
13059
13060 for (vlist = Voverlay_arrow_variable_list;
13061 CONSP (vlist);
13062 vlist = XCDR (vlist))
13063 {
13064 Lisp_Object var = XCAR (vlist);
13065 Lisp_Object val, pstr;
13066
13067 if (!SYMBOLP (var))
13068 continue;
13069 val = find_symbol_value (var);
13070 if (!MARKERP (val))
13071 continue;
13072 if (! EQ (COERCE_MARKER (val),
13073 Fget (var, Qlast_arrow_position))
13074 || ! (pstr = overlay_arrow_string_or_property (var),
13075 EQ (pstr, Fget (var, Qlast_arrow_string))))
13076 return 1;
13077 }
13078 return 0;
13079 }
13080
13081 /* Mark overlay arrows to be updated on next redisplay. */
13082
13083 static void
13084 update_overlay_arrows (int up_to_date)
13085 {
13086 Lisp_Object vlist;
13087
13088 for (vlist = Voverlay_arrow_variable_list;
13089 CONSP (vlist);
13090 vlist = XCDR (vlist))
13091 {
13092 Lisp_Object var = XCAR (vlist);
13093
13094 if (!SYMBOLP (var))
13095 continue;
13096
13097 if (up_to_date > 0)
13098 {
13099 Lisp_Object val = find_symbol_value (var);
13100 Fput (var, Qlast_arrow_position,
13101 COERCE_MARKER (val));
13102 Fput (var, Qlast_arrow_string,
13103 overlay_arrow_string_or_property (var));
13104 }
13105 else if (up_to_date < 0
13106 || !NILP (Fget (var, Qlast_arrow_position)))
13107 {
13108 Fput (var, Qlast_arrow_position, Qt);
13109 Fput (var, Qlast_arrow_string, Qt);
13110 }
13111 }
13112 }
13113
13114
13115 /* Return overlay arrow string to display at row.
13116 Return integer (bitmap number) for arrow bitmap in left fringe.
13117 Return nil if no overlay arrow. */
13118
13119 static Lisp_Object
13120 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13121 {
13122 Lisp_Object vlist;
13123
13124 for (vlist = Voverlay_arrow_variable_list;
13125 CONSP (vlist);
13126 vlist = XCDR (vlist))
13127 {
13128 Lisp_Object var = XCAR (vlist);
13129 Lisp_Object val;
13130
13131 if (!SYMBOLP (var))
13132 continue;
13133
13134 val = find_symbol_value (var);
13135
13136 if (MARKERP (val)
13137 && current_buffer == XMARKER (val)->buffer
13138 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13139 {
13140 if (FRAME_WINDOW_P (it->f)
13141 /* FIXME: if ROW->reversed_p is set, this should test
13142 the right fringe, not the left one. */
13143 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13144 {
13145 #ifdef HAVE_WINDOW_SYSTEM
13146 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13147 {
13148 int fringe_bitmap;
13149 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13150 return make_number (fringe_bitmap);
13151 }
13152 #endif
13153 return make_number (-1); /* Use default arrow bitmap. */
13154 }
13155 return overlay_arrow_string_or_property (var);
13156 }
13157 }
13158
13159 return Qnil;
13160 }
13161
13162 /* Return 1 if point moved out of or into a composition. Otherwise
13163 return 0. PREV_BUF and PREV_PT are the last point buffer and
13164 position. BUF and PT are the current point buffer and position. */
13165
13166 static int
13167 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13168 struct buffer *buf, ptrdiff_t pt)
13169 {
13170 ptrdiff_t start, end;
13171 Lisp_Object prop;
13172 Lisp_Object buffer;
13173
13174 XSETBUFFER (buffer, buf);
13175 /* Check a composition at the last point if point moved within the
13176 same buffer. */
13177 if (prev_buf == buf)
13178 {
13179 if (prev_pt == pt)
13180 /* Point didn't move. */
13181 return 0;
13182
13183 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13184 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13185 && composition_valid_p (start, end, prop)
13186 && start < prev_pt && end > prev_pt)
13187 /* The last point was within the composition. Return 1 iff
13188 point moved out of the composition. */
13189 return (pt <= start || pt >= end);
13190 }
13191
13192 /* Check a composition at the current point. */
13193 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13194 && find_composition (pt, -1, &start, &end, &prop, buffer)
13195 && composition_valid_p (start, end, prop)
13196 && start < pt && end > pt);
13197 }
13198
13199 /* Reconsider the clip changes of buffer which is displayed in W. */
13200
13201 static void
13202 reconsider_clip_changes (struct window *w)
13203 {
13204 struct buffer *b = XBUFFER (w->contents);
13205
13206 if (b->clip_changed
13207 && w->window_end_valid
13208 && w->current_matrix->buffer == b
13209 && w->current_matrix->zv == BUF_ZV (b)
13210 && w->current_matrix->begv == BUF_BEGV (b))
13211 b->clip_changed = 0;
13212
13213 /* If display wasn't paused, and W is not a tool bar window, see if
13214 point has been moved into or out of a composition. In that case,
13215 we set b->clip_changed to 1 to force updating the screen. If
13216 b->clip_changed has already been set to 1, we can skip this
13217 check. */
13218 if (!b->clip_changed && w->window_end_valid)
13219 {
13220 ptrdiff_t pt = (w == XWINDOW (selected_window)
13221 ? PT : marker_position (w->pointm));
13222
13223 if ((w->current_matrix->buffer != b || pt != w->last_point)
13224 && check_point_in_composition (w->current_matrix->buffer,
13225 w->last_point, b, pt))
13226 b->clip_changed = 1;
13227 }
13228 }
13229
13230 static void
13231 propagate_buffer_redisplay (void)
13232 { /* Resetting b->text->redisplay is problematic!
13233 We can't just reset it in the case that some window that displays
13234 it has not been redisplayed; and such a window can stay
13235 unredisplayed for a long time if it's currently invisible.
13236 But we do want to reset it at the end of redisplay otherwise
13237 its displayed windows will keep being redisplayed over and over
13238 again.
13239 So we copy all b->text->redisplay flags up to their windows here,
13240 such that mark_window_display_accurate can safely reset
13241 b->text->redisplay. */
13242 Lisp_Object ws = window_list ();
13243 for (; CONSP (ws); ws = XCDR (ws))
13244 {
13245 struct window *thisw = XWINDOW (XCAR (ws));
13246 struct buffer *thisb = XBUFFER (thisw->contents);
13247 if (thisb->text->redisplay)
13248 thisw->redisplay = true;
13249 }
13250 }
13251
13252 #define STOP_POLLING \
13253 do { if (! polling_stopped_here) stop_polling (); \
13254 polling_stopped_here = 1; } while (0)
13255
13256 #define RESUME_POLLING \
13257 do { if (polling_stopped_here) start_polling (); \
13258 polling_stopped_here = 0; } while (0)
13259
13260
13261 /* Perhaps in the future avoid recentering windows if it
13262 is not necessary; currently that causes some problems. */
13263
13264 static void
13265 redisplay_internal (void)
13266 {
13267 struct window *w = XWINDOW (selected_window);
13268 struct window *sw;
13269 struct frame *fr;
13270 int pending;
13271 bool must_finish = 0, match_p;
13272 struct text_pos tlbufpos, tlendpos;
13273 int number_of_visible_frames;
13274 ptrdiff_t count;
13275 struct frame *sf;
13276 int polling_stopped_here = 0;
13277 Lisp_Object tail, frame;
13278
13279 /* True means redisplay has to consider all windows on all
13280 frames. False, only selected_window is considered. */
13281 bool consider_all_windows_p;
13282
13283 /* True means redisplay has to redisplay the miniwindow. */
13284 bool update_miniwindow_p = false;
13285
13286 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13287
13288 /* No redisplay if running in batch mode or frame is not yet fully
13289 initialized, or redisplay is explicitly turned off by setting
13290 Vinhibit_redisplay. */
13291 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13292 || !NILP (Vinhibit_redisplay))
13293 return;
13294
13295 /* Don't examine these until after testing Vinhibit_redisplay.
13296 When Emacs is shutting down, perhaps because its connection to
13297 X has dropped, we should not look at them at all. */
13298 fr = XFRAME (w->frame);
13299 sf = SELECTED_FRAME ();
13300
13301 if (!fr->glyphs_initialized_p)
13302 return;
13303
13304 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13305 if (popup_activated ())
13306 return;
13307 #endif
13308
13309 /* I don't think this happens but let's be paranoid. */
13310 if (redisplaying_p)
13311 return;
13312
13313 /* Record a function that clears redisplaying_p
13314 when we leave this function. */
13315 count = SPECPDL_INDEX ();
13316 record_unwind_protect_void (unwind_redisplay);
13317 redisplaying_p = 1;
13318 specbind (Qinhibit_free_realized_faces, Qnil);
13319
13320 /* Record this function, so it appears on the profiler's backtraces. */
13321 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13322
13323 FOR_EACH_FRAME (tail, frame)
13324 XFRAME (frame)->already_hscrolled_p = 0;
13325
13326 retry:
13327 /* Remember the currently selected window. */
13328 sw = w;
13329
13330 pending = 0;
13331 last_escape_glyph_frame = NULL;
13332 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13333 last_glyphless_glyph_frame = NULL;
13334 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13335
13336 /* If face_change_count is non-zero, init_iterator will free all
13337 realized faces, which includes the faces referenced from current
13338 matrices. So, we can't reuse current matrices in this case. */
13339 if (face_change_count)
13340 windows_or_buffers_changed = 47;
13341
13342 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13343 && FRAME_TTY (sf)->previous_frame != sf)
13344 {
13345 /* Since frames on a single ASCII terminal share the same
13346 display area, displaying a different frame means redisplay
13347 the whole thing. */
13348 SET_FRAME_GARBAGED (sf);
13349 #ifndef DOS_NT
13350 set_tty_color_mode (FRAME_TTY (sf), sf);
13351 #endif
13352 FRAME_TTY (sf)->previous_frame = sf;
13353 }
13354
13355 /* Set the visible flags for all frames. Do this before checking for
13356 resized or garbaged frames; they want to know if their frames are
13357 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13358 number_of_visible_frames = 0;
13359
13360 FOR_EACH_FRAME (tail, frame)
13361 {
13362 struct frame *f = XFRAME (frame);
13363
13364 if (FRAME_VISIBLE_P (f))
13365 {
13366 ++number_of_visible_frames;
13367 /* Adjust matrices for visible frames only. */
13368 if (f->fonts_changed)
13369 {
13370 adjust_frame_glyphs (f);
13371 f->fonts_changed = 0;
13372 }
13373 /* If cursor type has been changed on the frame
13374 other than selected, consider all frames. */
13375 if (f != sf && f->cursor_type_changed)
13376 update_mode_lines = 31;
13377 }
13378 clear_desired_matrices (f);
13379 }
13380
13381 /* Notice any pending interrupt request to change frame size. */
13382 do_pending_window_change (1);
13383
13384 /* do_pending_window_change could change the selected_window due to
13385 frame resizing which makes the selected window too small. */
13386 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13387 sw = w;
13388
13389 /* Clear frames marked as garbaged. */
13390 clear_garbaged_frames ();
13391
13392 /* Build menubar and tool-bar items. */
13393 if (NILP (Vmemory_full))
13394 prepare_menu_bars ();
13395
13396 reconsider_clip_changes (w);
13397
13398 /* In most cases selected window displays current buffer. */
13399 match_p = XBUFFER (w->contents) == current_buffer;
13400 if (match_p)
13401 {
13402 /* Detect case that we need to write or remove a star in the mode line. */
13403 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13404 w->update_mode_line = 1;
13405
13406 if (mode_line_update_needed (w))
13407 w->update_mode_line = 1;
13408 }
13409
13410 /* Normally the message* functions will have already displayed and
13411 updated the echo area, but the frame may have been trashed, or
13412 the update may have been preempted, so display the echo area
13413 again here. Checking message_cleared_p captures the case that
13414 the echo area should be cleared. */
13415 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13416 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13417 || (message_cleared_p
13418 && minibuf_level == 0
13419 /* If the mini-window is currently selected, this means the
13420 echo-area doesn't show through. */
13421 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13422 {
13423 int window_height_changed_p = echo_area_display (0);
13424
13425 if (message_cleared_p)
13426 update_miniwindow_p = true;
13427
13428 must_finish = 1;
13429
13430 /* If we don't display the current message, don't clear the
13431 message_cleared_p flag, because, if we did, we wouldn't clear
13432 the echo area in the next redisplay which doesn't preserve
13433 the echo area. */
13434 if (!display_last_displayed_message_p)
13435 message_cleared_p = 0;
13436
13437 if (window_height_changed_p)
13438 {
13439 windows_or_buffers_changed = 50;
13440
13441 /* If window configuration was changed, frames may have been
13442 marked garbaged. Clear them or we will experience
13443 surprises wrt scrolling. */
13444 clear_garbaged_frames ();
13445 }
13446 }
13447 else if (EQ (selected_window, minibuf_window)
13448 && (current_buffer->clip_changed || window_outdated (w))
13449 && resize_mini_window (w, 0))
13450 {
13451 /* Resized active mini-window to fit the size of what it is
13452 showing if its contents might have changed. */
13453 must_finish = 1;
13454
13455 /* If window configuration was changed, frames may have been
13456 marked garbaged. Clear them or we will experience
13457 surprises wrt scrolling. */
13458 clear_garbaged_frames ();
13459 }
13460
13461 if (windows_or_buffers_changed && !update_mode_lines)
13462 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13463 only the windows's contents needs to be refreshed, or whether the
13464 mode-lines also need a refresh. */
13465 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13466 ? REDISPLAY_SOME : 32);
13467
13468 /* If specs for an arrow have changed, do thorough redisplay
13469 to ensure we remove any arrow that should no longer exist. */
13470 if (overlay_arrows_changed_p ())
13471 /* Apparently, this is the only case where we update other windows,
13472 without updating other mode-lines. */
13473 windows_or_buffers_changed = 49;
13474
13475 consider_all_windows_p = (update_mode_lines
13476 || windows_or_buffers_changed);
13477
13478 #define AINC(a,i) \
13479 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13480 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13481
13482 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13483 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13484
13485 /* Optimize the case that only the line containing the cursor in the
13486 selected window has changed. Variables starting with this_ are
13487 set in display_line and record information about the line
13488 containing the cursor. */
13489 tlbufpos = this_line_start_pos;
13490 tlendpos = this_line_end_pos;
13491 if (!consider_all_windows_p
13492 && CHARPOS (tlbufpos) > 0
13493 && !w->update_mode_line
13494 && !current_buffer->clip_changed
13495 && !current_buffer->prevent_redisplay_optimizations_p
13496 && FRAME_VISIBLE_P (XFRAME (w->frame))
13497 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13498 && !XFRAME (w->frame)->cursor_type_changed
13499 /* Make sure recorded data applies to current buffer, etc. */
13500 && this_line_buffer == current_buffer
13501 && match_p
13502 && !w->force_start
13503 && !w->optional_new_start
13504 /* Point must be on the line that we have info recorded about. */
13505 && PT >= CHARPOS (tlbufpos)
13506 && PT <= Z - CHARPOS (tlendpos)
13507 /* All text outside that line, including its final newline,
13508 must be unchanged. */
13509 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13510 CHARPOS (tlendpos)))
13511 {
13512 if (CHARPOS (tlbufpos) > BEGV
13513 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13514 && (CHARPOS (tlbufpos) == ZV
13515 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13516 /* Former continuation line has disappeared by becoming empty. */
13517 goto cancel;
13518 else if (window_outdated (w) || MINI_WINDOW_P (w))
13519 {
13520 /* We have to handle the case of continuation around a
13521 wide-column character (see the comment in indent.c around
13522 line 1340).
13523
13524 For instance, in the following case:
13525
13526 -------- Insert --------
13527 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13528 J_I_ ==> J_I_ `^^' are cursors.
13529 ^^ ^^
13530 -------- --------
13531
13532 As we have to redraw the line above, we cannot use this
13533 optimization. */
13534
13535 struct it it;
13536 int line_height_before = this_line_pixel_height;
13537
13538 /* Note that start_display will handle the case that the
13539 line starting at tlbufpos is a continuation line. */
13540 start_display (&it, w, tlbufpos);
13541
13542 /* Implementation note: It this still necessary? */
13543 if (it.current_x != this_line_start_x)
13544 goto cancel;
13545
13546 TRACE ((stderr, "trying display optimization 1\n"));
13547 w->cursor.vpos = -1;
13548 overlay_arrow_seen = 0;
13549 it.vpos = this_line_vpos;
13550 it.current_y = this_line_y;
13551 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13552 display_line (&it);
13553
13554 /* If line contains point, is not continued,
13555 and ends at same distance from eob as before, we win. */
13556 if (w->cursor.vpos >= 0
13557 /* Line is not continued, otherwise this_line_start_pos
13558 would have been set to 0 in display_line. */
13559 && CHARPOS (this_line_start_pos)
13560 /* Line ends as before. */
13561 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13562 /* Line has same height as before. Otherwise other lines
13563 would have to be shifted up or down. */
13564 && this_line_pixel_height == line_height_before)
13565 {
13566 /* If this is not the window's last line, we must adjust
13567 the charstarts of the lines below. */
13568 if (it.current_y < it.last_visible_y)
13569 {
13570 struct glyph_row *row
13571 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13572 ptrdiff_t delta, delta_bytes;
13573
13574 /* We used to distinguish between two cases here,
13575 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13576 when the line ends in a newline or the end of the
13577 buffer's accessible portion. But both cases did
13578 the same, so they were collapsed. */
13579 delta = (Z
13580 - CHARPOS (tlendpos)
13581 - MATRIX_ROW_START_CHARPOS (row));
13582 delta_bytes = (Z_BYTE
13583 - BYTEPOS (tlendpos)
13584 - MATRIX_ROW_START_BYTEPOS (row));
13585
13586 increment_matrix_positions (w->current_matrix,
13587 this_line_vpos + 1,
13588 w->current_matrix->nrows,
13589 delta, delta_bytes);
13590 }
13591
13592 /* If this row displays text now but previously didn't,
13593 or vice versa, w->window_end_vpos may have to be
13594 adjusted. */
13595 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13596 {
13597 if (w->window_end_vpos < this_line_vpos)
13598 w->window_end_vpos = this_line_vpos;
13599 }
13600 else if (w->window_end_vpos == this_line_vpos
13601 && this_line_vpos > 0)
13602 w->window_end_vpos = this_line_vpos - 1;
13603 w->window_end_valid = 0;
13604
13605 /* Update hint: No need to try to scroll in update_window. */
13606 w->desired_matrix->no_scrolling_p = 1;
13607
13608 #ifdef GLYPH_DEBUG
13609 *w->desired_matrix->method = 0;
13610 debug_method_add (w, "optimization 1");
13611 #endif
13612 #ifdef HAVE_WINDOW_SYSTEM
13613 update_window_fringes (w, 0);
13614 #endif
13615 goto update;
13616 }
13617 else
13618 goto cancel;
13619 }
13620 else if (/* Cursor position hasn't changed. */
13621 PT == w->last_point
13622 /* Make sure the cursor was last displayed
13623 in this window. Otherwise we have to reposition it. */
13624
13625 /* PXW: Must be converted to pixels, probably. */
13626 && 0 <= w->cursor.vpos
13627 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13628 {
13629 if (!must_finish)
13630 {
13631 do_pending_window_change (1);
13632 /* If selected_window changed, redisplay again. */
13633 if (WINDOWP (selected_window)
13634 && (w = XWINDOW (selected_window)) != sw)
13635 goto retry;
13636
13637 /* We used to always goto end_of_redisplay here, but this
13638 isn't enough if we have a blinking cursor. */
13639 if (w->cursor_off_p == w->last_cursor_off_p)
13640 goto end_of_redisplay;
13641 }
13642 goto update;
13643 }
13644 /* If highlighting the region, or if the cursor is in the echo area,
13645 then we can't just move the cursor. */
13646 else if (NILP (Vshow_trailing_whitespace)
13647 && !cursor_in_echo_area)
13648 {
13649 struct it it;
13650 struct glyph_row *row;
13651
13652 /* Skip from tlbufpos to PT and see where it is. Note that
13653 PT may be in invisible text. If so, we will end at the
13654 next visible position. */
13655 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13656 NULL, DEFAULT_FACE_ID);
13657 it.current_x = this_line_start_x;
13658 it.current_y = this_line_y;
13659 it.vpos = this_line_vpos;
13660
13661 /* The call to move_it_to stops in front of PT, but
13662 moves over before-strings. */
13663 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13664
13665 if (it.vpos == this_line_vpos
13666 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13667 row->enabled_p))
13668 {
13669 eassert (this_line_vpos == it.vpos);
13670 eassert (this_line_y == it.current_y);
13671 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13672 #ifdef GLYPH_DEBUG
13673 *w->desired_matrix->method = 0;
13674 debug_method_add (w, "optimization 3");
13675 #endif
13676 goto update;
13677 }
13678 else
13679 goto cancel;
13680 }
13681
13682 cancel:
13683 /* Text changed drastically or point moved off of line. */
13684 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13685 }
13686
13687 CHARPOS (this_line_start_pos) = 0;
13688 ++clear_face_cache_count;
13689 #ifdef HAVE_WINDOW_SYSTEM
13690 ++clear_image_cache_count;
13691 #endif
13692
13693 /* Build desired matrices, and update the display. If
13694 consider_all_windows_p is non-zero, do it for all windows on all
13695 frames. Otherwise do it for selected_window, only. */
13696
13697 if (consider_all_windows_p)
13698 {
13699 FOR_EACH_FRAME (tail, frame)
13700 XFRAME (frame)->updated_p = 0;
13701
13702 propagate_buffer_redisplay ();
13703
13704 FOR_EACH_FRAME (tail, frame)
13705 {
13706 struct frame *f = XFRAME (frame);
13707
13708 /* We don't have to do anything for unselected terminal
13709 frames. */
13710 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13711 && !EQ (FRAME_TTY (f)->top_frame, frame))
13712 continue;
13713
13714 retry_frame:
13715
13716 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13717 {
13718 bool gcscrollbars
13719 /* Only GC scrollbars when we redisplay the whole frame. */
13720 = f->redisplay || !REDISPLAY_SOME_P ();
13721 /* Mark all the scroll bars to be removed; we'll redeem
13722 the ones we want when we redisplay their windows. */
13723 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13724 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13725
13726 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13727 redisplay_windows (FRAME_ROOT_WINDOW (f));
13728 /* Remember that the invisible frames need to be redisplayed next
13729 time they're visible. */
13730 else if (!REDISPLAY_SOME_P ())
13731 f->redisplay = true;
13732
13733 /* The X error handler may have deleted that frame. */
13734 if (!FRAME_LIVE_P (f))
13735 continue;
13736
13737 /* Any scroll bars which redisplay_windows should have
13738 nuked should now go away. */
13739 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13740 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13741
13742 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13743 {
13744 /* If fonts changed on visible frame, display again. */
13745 if (f->fonts_changed)
13746 {
13747 adjust_frame_glyphs (f);
13748 f->fonts_changed = 0;
13749 goto retry_frame;
13750 }
13751
13752 /* See if we have to hscroll. */
13753 if (!f->already_hscrolled_p)
13754 {
13755 f->already_hscrolled_p = 1;
13756 if (hscroll_windows (f->root_window))
13757 goto retry_frame;
13758 }
13759
13760 /* Prevent various kinds of signals during display
13761 update. stdio is not robust about handling
13762 signals, which can cause an apparent I/O error. */
13763 if (interrupt_input)
13764 unrequest_sigio ();
13765 STOP_POLLING;
13766
13767 pending |= update_frame (f, 0, 0);
13768 f->cursor_type_changed = 0;
13769 f->updated_p = 1;
13770 }
13771 }
13772 }
13773
13774 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13775
13776 if (!pending)
13777 {
13778 /* Do the mark_window_display_accurate after all windows have
13779 been redisplayed because this call resets flags in buffers
13780 which are needed for proper redisplay. */
13781 FOR_EACH_FRAME (tail, frame)
13782 {
13783 struct frame *f = XFRAME (frame);
13784 if (f->updated_p)
13785 {
13786 f->redisplay = false;
13787 mark_window_display_accurate (f->root_window, 1);
13788 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13789 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13790 }
13791 }
13792 }
13793 }
13794 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13795 {
13796 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13797 struct frame *mini_frame;
13798
13799 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13800 /* Use list_of_error, not Qerror, so that
13801 we catch only errors and don't run the debugger. */
13802 internal_condition_case_1 (redisplay_window_1, selected_window,
13803 list_of_error,
13804 redisplay_window_error);
13805 if (update_miniwindow_p)
13806 internal_condition_case_1 (redisplay_window_1, mini_window,
13807 list_of_error,
13808 redisplay_window_error);
13809
13810 /* Compare desired and current matrices, perform output. */
13811
13812 update:
13813 /* If fonts changed, display again. */
13814 if (sf->fonts_changed)
13815 goto retry;
13816
13817 /* Prevent various kinds of signals during display update.
13818 stdio is not robust about handling signals,
13819 which can cause an apparent I/O error. */
13820 if (interrupt_input)
13821 unrequest_sigio ();
13822 STOP_POLLING;
13823
13824 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13825 {
13826 if (hscroll_windows (selected_window))
13827 goto retry;
13828
13829 XWINDOW (selected_window)->must_be_updated_p = true;
13830 pending = update_frame (sf, 0, 0);
13831 sf->cursor_type_changed = 0;
13832 }
13833
13834 /* We may have called echo_area_display at the top of this
13835 function. If the echo area is on another frame, that may
13836 have put text on a frame other than the selected one, so the
13837 above call to update_frame would not have caught it. Catch
13838 it here. */
13839 mini_window = FRAME_MINIBUF_WINDOW (sf);
13840 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13841
13842 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13843 {
13844 XWINDOW (mini_window)->must_be_updated_p = true;
13845 pending |= update_frame (mini_frame, 0, 0);
13846 mini_frame->cursor_type_changed = 0;
13847 if (!pending && hscroll_windows (mini_window))
13848 goto retry;
13849 }
13850 }
13851
13852 /* If display was paused because of pending input, make sure we do a
13853 thorough update the next time. */
13854 if (pending)
13855 {
13856 /* Prevent the optimization at the beginning of
13857 redisplay_internal that tries a single-line update of the
13858 line containing the cursor in the selected window. */
13859 CHARPOS (this_line_start_pos) = 0;
13860
13861 /* Let the overlay arrow be updated the next time. */
13862 update_overlay_arrows (0);
13863
13864 /* If we pause after scrolling, some rows in the current
13865 matrices of some windows are not valid. */
13866 if (!WINDOW_FULL_WIDTH_P (w)
13867 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13868 update_mode_lines = 36;
13869 }
13870 else
13871 {
13872 if (!consider_all_windows_p)
13873 {
13874 /* This has already been done above if
13875 consider_all_windows_p is set. */
13876 if (XBUFFER (w->contents)->text->redisplay
13877 && buffer_window_count (XBUFFER (w->contents)) > 1)
13878 /* This can happen if b->text->redisplay was set during
13879 jit-lock. */
13880 propagate_buffer_redisplay ();
13881 mark_window_display_accurate_1 (w, 1);
13882
13883 /* Say overlay arrows are up to date. */
13884 update_overlay_arrows (1);
13885
13886 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13887 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13888 }
13889
13890 update_mode_lines = 0;
13891 windows_or_buffers_changed = 0;
13892 }
13893
13894 /* Start SIGIO interrupts coming again. Having them off during the
13895 code above makes it less likely one will discard output, but not
13896 impossible, since there might be stuff in the system buffer here.
13897 But it is much hairier to try to do anything about that. */
13898 if (interrupt_input)
13899 request_sigio ();
13900 RESUME_POLLING;
13901
13902 /* If a frame has become visible which was not before, redisplay
13903 again, so that we display it. Expose events for such a frame
13904 (which it gets when becoming visible) don't call the parts of
13905 redisplay constructing glyphs, so simply exposing a frame won't
13906 display anything in this case. So, we have to display these
13907 frames here explicitly. */
13908 if (!pending)
13909 {
13910 int new_count = 0;
13911
13912 FOR_EACH_FRAME (tail, frame)
13913 {
13914 if (XFRAME (frame)->visible)
13915 new_count++;
13916 }
13917
13918 if (new_count != number_of_visible_frames)
13919 windows_or_buffers_changed = 52;
13920 }
13921
13922 /* Change frame size now if a change is pending. */
13923 do_pending_window_change (1);
13924
13925 /* If we just did a pending size change, or have additional
13926 visible frames, or selected_window changed, redisplay again. */
13927 if ((windows_or_buffers_changed && !pending)
13928 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13929 goto retry;
13930
13931 /* Clear the face and image caches.
13932
13933 We used to do this only if consider_all_windows_p. But the cache
13934 needs to be cleared if a timer creates images in the current
13935 buffer (e.g. the test case in Bug#6230). */
13936
13937 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13938 {
13939 clear_face_cache (0);
13940 clear_face_cache_count = 0;
13941 }
13942
13943 #ifdef HAVE_WINDOW_SYSTEM
13944 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13945 {
13946 clear_image_caches (Qnil);
13947 clear_image_cache_count = 0;
13948 }
13949 #endif /* HAVE_WINDOW_SYSTEM */
13950
13951 end_of_redisplay:
13952 if (interrupt_input && interrupts_deferred)
13953 request_sigio ();
13954
13955 unbind_to (count, Qnil);
13956 RESUME_POLLING;
13957 }
13958
13959
13960 /* Redisplay, but leave alone any recent echo area message unless
13961 another message has been requested in its place.
13962
13963 This is useful in situations where you need to redisplay but no
13964 user action has occurred, making it inappropriate for the message
13965 area to be cleared. See tracking_off and
13966 wait_reading_process_output for examples of these situations.
13967
13968 FROM_WHERE is an integer saying from where this function was
13969 called. This is useful for debugging. */
13970
13971 void
13972 redisplay_preserve_echo_area (int from_where)
13973 {
13974 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13975
13976 if (!NILP (echo_area_buffer[1]))
13977 {
13978 /* We have a previously displayed message, but no current
13979 message. Redisplay the previous message. */
13980 display_last_displayed_message_p = 1;
13981 redisplay_internal ();
13982 display_last_displayed_message_p = 0;
13983 }
13984 else
13985 redisplay_internal ();
13986
13987 flush_frame (SELECTED_FRAME ());
13988 }
13989
13990
13991 /* Function registered with record_unwind_protect in redisplay_internal. */
13992
13993 static void
13994 unwind_redisplay (void)
13995 {
13996 redisplaying_p = 0;
13997 }
13998
13999
14000 /* Mark the display of leaf window W as accurate or inaccurate.
14001 If ACCURATE_P is non-zero mark display of W as accurate. If
14002 ACCURATE_P is zero, arrange for W to be redisplayed the next
14003 time redisplay_internal is called. */
14004
14005 static void
14006 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14007 {
14008 struct buffer *b = XBUFFER (w->contents);
14009
14010 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14011 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14012 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14013
14014 if (accurate_p)
14015 {
14016 b->clip_changed = false;
14017 b->prevent_redisplay_optimizations_p = false;
14018 eassert (buffer_window_count (b) > 0);
14019 /* Resetting b->text->redisplay is problematic!
14020 In order to make it safer to do it here, redisplay_internal must
14021 have copied all b->text->redisplay to their respective windows. */
14022 b->text->redisplay = false;
14023
14024 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14025 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14026 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14027 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14028
14029 w->current_matrix->buffer = b;
14030 w->current_matrix->begv = BUF_BEGV (b);
14031 w->current_matrix->zv = BUF_ZV (b);
14032
14033 w->last_cursor_vpos = w->cursor.vpos;
14034 w->last_cursor_off_p = w->cursor_off_p;
14035
14036 if (w == XWINDOW (selected_window))
14037 w->last_point = BUF_PT (b);
14038 else
14039 w->last_point = marker_position (w->pointm);
14040
14041 w->window_end_valid = true;
14042 w->update_mode_line = false;
14043 }
14044
14045 w->redisplay = !accurate_p;
14046 }
14047
14048
14049 /* Mark the display of windows in the window tree rooted at WINDOW as
14050 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14051 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14052 be redisplayed the next time redisplay_internal is called. */
14053
14054 void
14055 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14056 {
14057 struct window *w;
14058
14059 for (; !NILP (window); window = w->next)
14060 {
14061 w = XWINDOW (window);
14062 if (WINDOWP (w->contents))
14063 mark_window_display_accurate (w->contents, accurate_p);
14064 else
14065 mark_window_display_accurate_1 (w, accurate_p);
14066 }
14067
14068 if (accurate_p)
14069 update_overlay_arrows (1);
14070 else
14071 /* Force a thorough redisplay the next time by setting
14072 last_arrow_position and last_arrow_string to t, which is
14073 unequal to any useful value of Voverlay_arrow_... */
14074 update_overlay_arrows (-1);
14075 }
14076
14077
14078 /* Return value in display table DP (Lisp_Char_Table *) for character
14079 C. Since a display table doesn't have any parent, we don't have to
14080 follow parent. Do not call this function directly but use the
14081 macro DISP_CHAR_VECTOR. */
14082
14083 Lisp_Object
14084 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14085 {
14086 Lisp_Object val;
14087
14088 if (ASCII_CHAR_P (c))
14089 {
14090 val = dp->ascii;
14091 if (SUB_CHAR_TABLE_P (val))
14092 val = XSUB_CHAR_TABLE (val)->contents[c];
14093 }
14094 else
14095 {
14096 Lisp_Object table;
14097
14098 XSETCHAR_TABLE (table, dp);
14099 val = char_table_ref (table, c);
14100 }
14101 if (NILP (val))
14102 val = dp->defalt;
14103 return val;
14104 }
14105
14106
14107 \f
14108 /***********************************************************************
14109 Window Redisplay
14110 ***********************************************************************/
14111
14112 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14113
14114 static void
14115 redisplay_windows (Lisp_Object window)
14116 {
14117 while (!NILP (window))
14118 {
14119 struct window *w = XWINDOW (window);
14120
14121 if (WINDOWP (w->contents))
14122 redisplay_windows (w->contents);
14123 else if (BUFFERP (w->contents))
14124 {
14125 displayed_buffer = XBUFFER (w->contents);
14126 /* Use list_of_error, not Qerror, so that
14127 we catch only errors and don't run the debugger. */
14128 internal_condition_case_1 (redisplay_window_0, window,
14129 list_of_error,
14130 redisplay_window_error);
14131 }
14132
14133 window = w->next;
14134 }
14135 }
14136
14137 static Lisp_Object
14138 redisplay_window_error (Lisp_Object ignore)
14139 {
14140 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14141 return Qnil;
14142 }
14143
14144 static Lisp_Object
14145 redisplay_window_0 (Lisp_Object window)
14146 {
14147 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14148 redisplay_window (window, false);
14149 return Qnil;
14150 }
14151
14152 static Lisp_Object
14153 redisplay_window_1 (Lisp_Object window)
14154 {
14155 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14156 redisplay_window (window, true);
14157 return Qnil;
14158 }
14159 \f
14160
14161 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14162 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14163 which positions recorded in ROW differ from current buffer
14164 positions.
14165
14166 Return 0 if cursor is not on this row, 1 otherwise. */
14167
14168 static int
14169 set_cursor_from_row (struct window *w, struct glyph_row *row,
14170 struct glyph_matrix *matrix,
14171 ptrdiff_t delta, ptrdiff_t delta_bytes,
14172 int dy, int dvpos)
14173 {
14174 struct glyph *glyph = row->glyphs[TEXT_AREA];
14175 struct glyph *end = glyph + row->used[TEXT_AREA];
14176 struct glyph *cursor = NULL;
14177 /* The last known character position in row. */
14178 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14179 int x = row->x;
14180 ptrdiff_t pt_old = PT - delta;
14181 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14182 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14183 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14184 /* A glyph beyond the edge of TEXT_AREA which we should never
14185 touch. */
14186 struct glyph *glyphs_end = end;
14187 /* Non-zero means we've found a match for cursor position, but that
14188 glyph has the avoid_cursor_p flag set. */
14189 int match_with_avoid_cursor = 0;
14190 /* Non-zero means we've seen at least one glyph that came from a
14191 display string. */
14192 int string_seen = 0;
14193 /* Largest and smallest buffer positions seen so far during scan of
14194 glyph row. */
14195 ptrdiff_t bpos_max = pos_before;
14196 ptrdiff_t bpos_min = pos_after;
14197 /* Last buffer position covered by an overlay string with an integer
14198 `cursor' property. */
14199 ptrdiff_t bpos_covered = 0;
14200 /* Non-zero means the display string on which to display the cursor
14201 comes from a text property, not from an overlay. */
14202 int string_from_text_prop = 0;
14203
14204 /* Don't even try doing anything if called for a mode-line or
14205 header-line row, since the rest of the code isn't prepared to
14206 deal with such calamities. */
14207 eassert (!row->mode_line_p);
14208 if (row->mode_line_p)
14209 return 0;
14210
14211 /* Skip over glyphs not having an object at the start and the end of
14212 the row. These are special glyphs like truncation marks on
14213 terminal frames. */
14214 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14215 {
14216 if (!row->reversed_p)
14217 {
14218 while (glyph < end
14219 && INTEGERP (glyph->object)
14220 && glyph->charpos < 0)
14221 {
14222 x += glyph->pixel_width;
14223 ++glyph;
14224 }
14225 while (end > glyph
14226 && INTEGERP ((end - 1)->object)
14227 /* CHARPOS is zero for blanks and stretch glyphs
14228 inserted by extend_face_to_end_of_line. */
14229 && (end - 1)->charpos <= 0)
14230 --end;
14231 glyph_before = glyph - 1;
14232 glyph_after = end;
14233 }
14234 else
14235 {
14236 struct glyph *g;
14237
14238 /* If the glyph row is reversed, we need to process it from back
14239 to front, so swap the edge pointers. */
14240 glyphs_end = end = glyph - 1;
14241 glyph += row->used[TEXT_AREA] - 1;
14242
14243 while (glyph > end + 1
14244 && INTEGERP (glyph->object)
14245 && glyph->charpos < 0)
14246 {
14247 --glyph;
14248 x -= glyph->pixel_width;
14249 }
14250 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14251 --glyph;
14252 /* By default, in reversed rows we put the cursor on the
14253 rightmost (first in the reading order) glyph. */
14254 for (g = end + 1; g < glyph; g++)
14255 x += g->pixel_width;
14256 while (end < glyph
14257 && INTEGERP ((end + 1)->object)
14258 && (end + 1)->charpos <= 0)
14259 ++end;
14260 glyph_before = glyph + 1;
14261 glyph_after = end;
14262 }
14263 }
14264 else if (row->reversed_p)
14265 {
14266 /* In R2L rows that don't display text, put the cursor on the
14267 rightmost glyph. Case in point: an empty last line that is
14268 part of an R2L paragraph. */
14269 cursor = end - 1;
14270 /* Avoid placing the cursor on the last glyph of the row, where
14271 on terminal frames we hold the vertical border between
14272 adjacent windows. */
14273 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14274 && !WINDOW_RIGHTMOST_P (w)
14275 && cursor == row->glyphs[LAST_AREA] - 1)
14276 cursor--;
14277 x = -1; /* will be computed below, at label compute_x */
14278 }
14279
14280 /* Step 1: Try to find the glyph whose character position
14281 corresponds to point. If that's not possible, find 2 glyphs
14282 whose character positions are the closest to point, one before
14283 point, the other after it. */
14284 if (!row->reversed_p)
14285 while (/* not marched to end of glyph row */
14286 glyph < end
14287 /* glyph was not inserted by redisplay for internal purposes */
14288 && !INTEGERP (glyph->object))
14289 {
14290 if (BUFFERP (glyph->object))
14291 {
14292 ptrdiff_t dpos = glyph->charpos - pt_old;
14293
14294 if (glyph->charpos > bpos_max)
14295 bpos_max = glyph->charpos;
14296 if (glyph->charpos < bpos_min)
14297 bpos_min = glyph->charpos;
14298 if (!glyph->avoid_cursor_p)
14299 {
14300 /* If we hit point, we've found the glyph on which to
14301 display the cursor. */
14302 if (dpos == 0)
14303 {
14304 match_with_avoid_cursor = 0;
14305 break;
14306 }
14307 /* See if we've found a better approximation to
14308 POS_BEFORE or to POS_AFTER. */
14309 if (0 > dpos && dpos > pos_before - pt_old)
14310 {
14311 pos_before = glyph->charpos;
14312 glyph_before = glyph;
14313 }
14314 else if (0 < dpos && dpos < pos_after - pt_old)
14315 {
14316 pos_after = glyph->charpos;
14317 glyph_after = glyph;
14318 }
14319 }
14320 else if (dpos == 0)
14321 match_with_avoid_cursor = 1;
14322 }
14323 else if (STRINGP (glyph->object))
14324 {
14325 Lisp_Object chprop;
14326 ptrdiff_t glyph_pos = glyph->charpos;
14327
14328 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14329 glyph->object);
14330 if (!NILP (chprop))
14331 {
14332 /* If the string came from a `display' text property,
14333 look up the buffer position of that property and
14334 use that position to update bpos_max, as if we
14335 actually saw such a position in one of the row's
14336 glyphs. This helps with supporting integer values
14337 of `cursor' property on the display string in
14338 situations where most or all of the row's buffer
14339 text is completely covered by display properties,
14340 so that no glyph with valid buffer positions is
14341 ever seen in the row. */
14342 ptrdiff_t prop_pos =
14343 string_buffer_position_lim (glyph->object, pos_before,
14344 pos_after, 0);
14345
14346 if (prop_pos >= pos_before)
14347 bpos_max = prop_pos - 1;
14348 }
14349 if (INTEGERP (chprop))
14350 {
14351 bpos_covered = bpos_max + XINT (chprop);
14352 /* If the `cursor' property covers buffer positions up
14353 to and including point, we should display cursor on
14354 this glyph. Note that, if a `cursor' property on one
14355 of the string's characters has an integer value, we
14356 will break out of the loop below _before_ we get to
14357 the position match above. IOW, integer values of
14358 the `cursor' property override the "exact match for
14359 point" strategy of positioning the cursor. */
14360 /* Implementation note: bpos_max == pt_old when, e.g.,
14361 we are in an empty line, where bpos_max is set to
14362 MATRIX_ROW_START_CHARPOS, see above. */
14363 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14364 {
14365 cursor = glyph;
14366 break;
14367 }
14368 }
14369
14370 string_seen = 1;
14371 }
14372 x += glyph->pixel_width;
14373 ++glyph;
14374 }
14375 else if (glyph > end) /* row is reversed */
14376 while (!INTEGERP (glyph->object))
14377 {
14378 if (BUFFERP (glyph->object))
14379 {
14380 ptrdiff_t dpos = glyph->charpos - pt_old;
14381
14382 if (glyph->charpos > bpos_max)
14383 bpos_max = glyph->charpos;
14384 if (glyph->charpos < bpos_min)
14385 bpos_min = glyph->charpos;
14386 if (!glyph->avoid_cursor_p)
14387 {
14388 if (dpos == 0)
14389 {
14390 match_with_avoid_cursor = 0;
14391 break;
14392 }
14393 if (0 > dpos && dpos > pos_before - pt_old)
14394 {
14395 pos_before = glyph->charpos;
14396 glyph_before = glyph;
14397 }
14398 else if (0 < dpos && dpos < pos_after - pt_old)
14399 {
14400 pos_after = glyph->charpos;
14401 glyph_after = glyph;
14402 }
14403 }
14404 else if (dpos == 0)
14405 match_with_avoid_cursor = 1;
14406 }
14407 else if (STRINGP (glyph->object))
14408 {
14409 Lisp_Object chprop;
14410 ptrdiff_t glyph_pos = glyph->charpos;
14411
14412 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14413 glyph->object);
14414 if (!NILP (chprop))
14415 {
14416 ptrdiff_t prop_pos =
14417 string_buffer_position_lim (glyph->object, pos_before,
14418 pos_after, 0);
14419
14420 if (prop_pos >= pos_before)
14421 bpos_max = prop_pos - 1;
14422 }
14423 if (INTEGERP (chprop))
14424 {
14425 bpos_covered = bpos_max + XINT (chprop);
14426 /* If the `cursor' property covers buffer positions up
14427 to and including point, we should display cursor on
14428 this glyph. */
14429 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14430 {
14431 cursor = glyph;
14432 break;
14433 }
14434 }
14435 string_seen = 1;
14436 }
14437 --glyph;
14438 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14439 {
14440 x--; /* can't use any pixel_width */
14441 break;
14442 }
14443 x -= glyph->pixel_width;
14444 }
14445
14446 /* Step 2: If we didn't find an exact match for point, we need to
14447 look for a proper place to put the cursor among glyphs between
14448 GLYPH_BEFORE and GLYPH_AFTER. */
14449 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14450 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14451 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14452 {
14453 /* An empty line has a single glyph whose OBJECT is zero and
14454 whose CHARPOS is the position of a newline on that line.
14455 Note that on a TTY, there are more glyphs after that, which
14456 were produced by extend_face_to_end_of_line, but their
14457 CHARPOS is zero or negative. */
14458 int empty_line_p =
14459 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14460 && INTEGERP (glyph->object) && glyph->charpos > 0
14461 /* On a TTY, continued and truncated rows also have a glyph at
14462 their end whose OBJECT is zero and whose CHARPOS is
14463 positive (the continuation and truncation glyphs), but such
14464 rows are obviously not "empty". */
14465 && !(row->continued_p || row->truncated_on_right_p);
14466
14467 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14468 {
14469 ptrdiff_t ellipsis_pos;
14470
14471 /* Scan back over the ellipsis glyphs. */
14472 if (!row->reversed_p)
14473 {
14474 ellipsis_pos = (glyph - 1)->charpos;
14475 while (glyph > row->glyphs[TEXT_AREA]
14476 && (glyph - 1)->charpos == ellipsis_pos)
14477 glyph--, x -= glyph->pixel_width;
14478 /* That loop always goes one position too far, including
14479 the glyph before the ellipsis. So scan forward over
14480 that one. */
14481 x += glyph->pixel_width;
14482 glyph++;
14483 }
14484 else /* row is reversed */
14485 {
14486 ellipsis_pos = (glyph + 1)->charpos;
14487 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14488 && (glyph + 1)->charpos == ellipsis_pos)
14489 glyph++, x += glyph->pixel_width;
14490 x -= glyph->pixel_width;
14491 glyph--;
14492 }
14493 }
14494 else if (match_with_avoid_cursor)
14495 {
14496 cursor = glyph_after;
14497 x = -1;
14498 }
14499 else if (string_seen)
14500 {
14501 int incr = row->reversed_p ? -1 : +1;
14502
14503 /* Need to find the glyph that came out of a string which is
14504 present at point. That glyph is somewhere between
14505 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14506 positioned between POS_BEFORE and POS_AFTER in the
14507 buffer. */
14508 struct glyph *start, *stop;
14509 ptrdiff_t pos = pos_before;
14510
14511 x = -1;
14512
14513 /* If the row ends in a newline from a display string,
14514 reordering could have moved the glyphs belonging to the
14515 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14516 in this case we extend the search to the last glyph in
14517 the row that was not inserted by redisplay. */
14518 if (row->ends_in_newline_from_string_p)
14519 {
14520 glyph_after = end;
14521 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14522 }
14523
14524 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14525 correspond to POS_BEFORE and POS_AFTER, respectively. We
14526 need START and STOP in the order that corresponds to the
14527 row's direction as given by its reversed_p flag. If the
14528 directionality of characters between POS_BEFORE and
14529 POS_AFTER is the opposite of the row's base direction,
14530 these characters will have been reordered for display,
14531 and we need to reverse START and STOP. */
14532 if (!row->reversed_p)
14533 {
14534 start = min (glyph_before, glyph_after);
14535 stop = max (glyph_before, glyph_after);
14536 }
14537 else
14538 {
14539 start = max (glyph_before, glyph_after);
14540 stop = min (glyph_before, glyph_after);
14541 }
14542 for (glyph = start + incr;
14543 row->reversed_p ? glyph > stop : glyph < stop; )
14544 {
14545
14546 /* Any glyphs that come from the buffer are here because
14547 of bidi reordering. Skip them, and only pay
14548 attention to glyphs that came from some string. */
14549 if (STRINGP (glyph->object))
14550 {
14551 Lisp_Object str;
14552 ptrdiff_t tem;
14553 /* If the display property covers the newline, we
14554 need to search for it one position farther. */
14555 ptrdiff_t lim = pos_after
14556 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14557
14558 string_from_text_prop = 0;
14559 str = glyph->object;
14560 tem = string_buffer_position_lim (str, pos, lim, 0);
14561 if (tem == 0 /* from overlay */
14562 || pos <= tem)
14563 {
14564 /* If the string from which this glyph came is
14565 found in the buffer at point, or at position
14566 that is closer to point than pos_after, then
14567 we've found the glyph we've been looking for.
14568 If it comes from an overlay (tem == 0), and
14569 it has the `cursor' property on one of its
14570 glyphs, record that glyph as a candidate for
14571 displaying the cursor. (As in the
14572 unidirectional version, we will display the
14573 cursor on the last candidate we find.) */
14574 if (tem == 0
14575 || tem == pt_old
14576 || (tem - pt_old > 0 && tem < pos_after))
14577 {
14578 /* The glyphs from this string could have
14579 been reordered. Find the one with the
14580 smallest string position. Or there could
14581 be a character in the string with the
14582 `cursor' property, which means display
14583 cursor on that character's glyph. */
14584 ptrdiff_t strpos = glyph->charpos;
14585
14586 if (tem)
14587 {
14588 cursor = glyph;
14589 string_from_text_prop = 1;
14590 }
14591 for ( ;
14592 (row->reversed_p ? glyph > stop : glyph < stop)
14593 && EQ (glyph->object, str);
14594 glyph += incr)
14595 {
14596 Lisp_Object cprop;
14597 ptrdiff_t gpos = glyph->charpos;
14598
14599 cprop = Fget_char_property (make_number (gpos),
14600 Qcursor,
14601 glyph->object);
14602 if (!NILP (cprop))
14603 {
14604 cursor = glyph;
14605 break;
14606 }
14607 if (tem && glyph->charpos < strpos)
14608 {
14609 strpos = glyph->charpos;
14610 cursor = glyph;
14611 }
14612 }
14613
14614 if (tem == pt_old
14615 || (tem - pt_old > 0 && tem < pos_after))
14616 goto compute_x;
14617 }
14618 if (tem)
14619 pos = tem + 1; /* don't find previous instances */
14620 }
14621 /* This string is not what we want; skip all of the
14622 glyphs that came from it. */
14623 while ((row->reversed_p ? glyph > stop : glyph < stop)
14624 && EQ (glyph->object, str))
14625 glyph += incr;
14626 }
14627 else
14628 glyph += incr;
14629 }
14630
14631 /* If we reached the end of the line, and END was from a string,
14632 the cursor is not on this line. */
14633 if (cursor == NULL
14634 && (row->reversed_p ? glyph <= end : glyph >= end)
14635 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14636 && STRINGP (end->object)
14637 && row->continued_p)
14638 return 0;
14639 }
14640 /* A truncated row may not include PT among its character positions.
14641 Setting the cursor inside the scroll margin will trigger
14642 recalculation of hscroll in hscroll_window_tree. But if a
14643 display string covers point, defer to the string-handling
14644 code below to figure this out. */
14645 else if (row->truncated_on_left_p && pt_old < bpos_min)
14646 {
14647 cursor = glyph_before;
14648 x = -1;
14649 }
14650 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14651 /* Zero-width characters produce no glyphs. */
14652 || (!empty_line_p
14653 && (row->reversed_p
14654 ? glyph_after > glyphs_end
14655 : glyph_after < glyphs_end)))
14656 {
14657 cursor = glyph_after;
14658 x = -1;
14659 }
14660 }
14661
14662 compute_x:
14663 if (cursor != NULL)
14664 glyph = cursor;
14665 else if (glyph == glyphs_end
14666 && pos_before == pos_after
14667 && STRINGP ((row->reversed_p
14668 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14669 : row->glyphs[TEXT_AREA])->object))
14670 {
14671 /* If all the glyphs of this row came from strings, put the
14672 cursor on the first glyph of the row. This avoids having the
14673 cursor outside of the text area in this very rare and hard
14674 use case. */
14675 glyph =
14676 row->reversed_p
14677 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14678 : row->glyphs[TEXT_AREA];
14679 }
14680 if (x < 0)
14681 {
14682 struct glyph *g;
14683
14684 /* Need to compute x that corresponds to GLYPH. */
14685 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14686 {
14687 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14688 emacs_abort ();
14689 x += g->pixel_width;
14690 }
14691 }
14692
14693 /* ROW could be part of a continued line, which, under bidi
14694 reordering, might have other rows whose start and end charpos
14695 occlude point. Only set w->cursor if we found a better
14696 approximation to the cursor position than we have from previously
14697 examined candidate rows belonging to the same continued line. */
14698 if (/* We already have a candidate row. */
14699 w->cursor.vpos >= 0
14700 /* That candidate is not the row we are processing. */
14701 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14702 /* Make sure cursor.vpos specifies a row whose start and end
14703 charpos occlude point, and it is valid candidate for being a
14704 cursor-row. This is because some callers of this function
14705 leave cursor.vpos at the row where the cursor was displayed
14706 during the last redisplay cycle. */
14707 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14708 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14709 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14710 {
14711 struct glyph *g1
14712 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14713
14714 /* Don't consider glyphs that are outside TEXT_AREA. */
14715 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14716 return 0;
14717 /* Keep the candidate whose buffer position is the closest to
14718 point or has the `cursor' property. */
14719 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14720 w->cursor.hpos >= 0
14721 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14722 && ((BUFFERP (g1->object)
14723 && (g1->charpos == pt_old /* An exact match always wins. */
14724 || (BUFFERP (glyph->object)
14725 && eabs (g1->charpos - pt_old)
14726 < eabs (glyph->charpos - pt_old))))
14727 /* Previous candidate is a glyph from a string that has
14728 a non-nil `cursor' property. */
14729 || (STRINGP (g1->object)
14730 && (!NILP (Fget_char_property (make_number (g1->charpos),
14731 Qcursor, g1->object))
14732 /* Previous candidate is from the same display
14733 string as this one, and the display string
14734 came from a text property. */
14735 || (EQ (g1->object, glyph->object)
14736 && string_from_text_prop)
14737 /* this candidate is from newline and its
14738 position is not an exact match */
14739 || (INTEGERP (glyph->object)
14740 && glyph->charpos != pt_old)))))
14741 return 0;
14742 /* If this candidate gives an exact match, use that. */
14743 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14744 /* If this candidate is a glyph created for the
14745 terminating newline of a line, and point is on that
14746 newline, it wins because it's an exact match. */
14747 || (!row->continued_p
14748 && INTEGERP (glyph->object)
14749 && glyph->charpos == 0
14750 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14751 /* Otherwise, keep the candidate that comes from a row
14752 spanning less buffer positions. This may win when one or
14753 both candidate positions are on glyphs that came from
14754 display strings, for which we cannot compare buffer
14755 positions. */
14756 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14757 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14758 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14759 return 0;
14760 }
14761 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14762 w->cursor.x = x;
14763 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14764 w->cursor.y = row->y + dy;
14765
14766 if (w == XWINDOW (selected_window))
14767 {
14768 if (!row->continued_p
14769 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14770 && row->x == 0)
14771 {
14772 this_line_buffer = XBUFFER (w->contents);
14773
14774 CHARPOS (this_line_start_pos)
14775 = MATRIX_ROW_START_CHARPOS (row) + delta;
14776 BYTEPOS (this_line_start_pos)
14777 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14778
14779 CHARPOS (this_line_end_pos)
14780 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14781 BYTEPOS (this_line_end_pos)
14782 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14783
14784 this_line_y = w->cursor.y;
14785 this_line_pixel_height = row->height;
14786 this_line_vpos = w->cursor.vpos;
14787 this_line_start_x = row->x;
14788 }
14789 else
14790 CHARPOS (this_line_start_pos) = 0;
14791 }
14792
14793 return 1;
14794 }
14795
14796
14797 /* Run window scroll functions, if any, for WINDOW with new window
14798 start STARTP. Sets the window start of WINDOW to that position.
14799
14800 We assume that the window's buffer is really current. */
14801
14802 static struct text_pos
14803 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14804 {
14805 struct window *w = XWINDOW (window);
14806 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14807
14808 eassert (current_buffer == XBUFFER (w->contents));
14809
14810 if (!NILP (Vwindow_scroll_functions))
14811 {
14812 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14813 make_number (CHARPOS (startp)));
14814 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14815 /* In case the hook functions switch buffers. */
14816 set_buffer_internal (XBUFFER (w->contents));
14817 }
14818
14819 return startp;
14820 }
14821
14822
14823 /* Make sure the line containing the cursor is fully visible.
14824 A value of 1 means there is nothing to be done.
14825 (Either the line is fully visible, or it cannot be made so,
14826 or we cannot tell.)
14827
14828 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14829 is higher than window.
14830
14831 A value of 0 means the caller should do scrolling
14832 as if point had gone off the screen. */
14833
14834 static int
14835 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14836 {
14837 struct glyph_matrix *matrix;
14838 struct glyph_row *row;
14839 int window_height;
14840
14841 if (!make_cursor_line_fully_visible_p)
14842 return 1;
14843
14844 /* It's not always possible to find the cursor, e.g, when a window
14845 is full of overlay strings. Don't do anything in that case. */
14846 if (w->cursor.vpos < 0)
14847 return 1;
14848
14849 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14850 row = MATRIX_ROW (matrix, w->cursor.vpos);
14851
14852 /* If the cursor row is not partially visible, there's nothing to do. */
14853 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14854 return 1;
14855
14856 /* If the row the cursor is in is taller than the window's height,
14857 it's not clear what to do, so do nothing. */
14858 window_height = window_box_height (w);
14859 if (row->height >= window_height)
14860 {
14861 if (!force_p || MINI_WINDOW_P (w)
14862 || w->vscroll || w->cursor.vpos == 0)
14863 return 1;
14864 }
14865 return 0;
14866 }
14867
14868
14869 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14870 non-zero means only WINDOW is redisplayed in redisplay_internal.
14871 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14872 in redisplay_window to bring a partially visible line into view in
14873 the case that only the cursor has moved.
14874
14875 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14876 last screen line's vertical height extends past the end of the screen.
14877
14878 Value is
14879
14880 1 if scrolling succeeded
14881
14882 0 if scrolling didn't find point.
14883
14884 -1 if new fonts have been loaded so that we must interrupt
14885 redisplay, adjust glyph matrices, and try again. */
14886
14887 enum
14888 {
14889 SCROLLING_SUCCESS,
14890 SCROLLING_FAILED,
14891 SCROLLING_NEED_LARGER_MATRICES
14892 };
14893
14894 /* If scroll-conservatively is more than this, never recenter.
14895
14896 If you change this, don't forget to update the doc string of
14897 `scroll-conservatively' and the Emacs manual. */
14898 #define SCROLL_LIMIT 100
14899
14900 static int
14901 try_scrolling (Lisp_Object window, int just_this_one_p,
14902 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14903 int temp_scroll_step, int last_line_misfit)
14904 {
14905 struct window *w = XWINDOW (window);
14906 struct frame *f = XFRAME (w->frame);
14907 struct text_pos pos, startp;
14908 struct it it;
14909 int this_scroll_margin, scroll_max, rc, height;
14910 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14911 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14912 Lisp_Object aggressive;
14913 /* We will never try scrolling more than this number of lines. */
14914 int scroll_limit = SCROLL_LIMIT;
14915 int frame_line_height = default_line_pixel_height (w);
14916 int window_total_lines
14917 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14918
14919 #ifdef GLYPH_DEBUG
14920 debug_method_add (w, "try_scrolling");
14921 #endif
14922
14923 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14924
14925 /* Compute scroll margin height in pixels. We scroll when point is
14926 within this distance from the top or bottom of the window. */
14927 if (scroll_margin > 0)
14928 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14929 * frame_line_height;
14930 else
14931 this_scroll_margin = 0;
14932
14933 /* Force arg_scroll_conservatively to have a reasonable value, to
14934 avoid scrolling too far away with slow move_it_* functions. Note
14935 that the user can supply scroll-conservatively equal to
14936 `most-positive-fixnum', which can be larger than INT_MAX. */
14937 if (arg_scroll_conservatively > scroll_limit)
14938 {
14939 arg_scroll_conservatively = scroll_limit + 1;
14940 scroll_max = scroll_limit * frame_line_height;
14941 }
14942 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14943 /* Compute how much we should try to scroll maximally to bring
14944 point into view. */
14945 scroll_max = (max (scroll_step,
14946 max (arg_scroll_conservatively, temp_scroll_step))
14947 * frame_line_height);
14948 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14949 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14950 /* We're trying to scroll because of aggressive scrolling but no
14951 scroll_step is set. Choose an arbitrary one. */
14952 scroll_max = 10 * frame_line_height;
14953 else
14954 scroll_max = 0;
14955
14956 too_near_end:
14957
14958 /* Decide whether to scroll down. */
14959 if (PT > CHARPOS (startp))
14960 {
14961 int scroll_margin_y;
14962
14963 /* Compute the pixel ypos of the scroll margin, then move IT to
14964 either that ypos or PT, whichever comes first. */
14965 start_display (&it, w, startp);
14966 scroll_margin_y = it.last_visible_y - this_scroll_margin
14967 - frame_line_height * extra_scroll_margin_lines;
14968 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14969 (MOVE_TO_POS | MOVE_TO_Y));
14970
14971 if (PT > CHARPOS (it.current.pos))
14972 {
14973 int y0 = line_bottom_y (&it);
14974 /* Compute how many pixels below window bottom to stop searching
14975 for PT. This avoids costly search for PT that is far away if
14976 the user limited scrolling by a small number of lines, but
14977 always finds PT if scroll_conservatively is set to a large
14978 number, such as most-positive-fixnum. */
14979 int slack = max (scroll_max, 10 * frame_line_height);
14980 int y_to_move = it.last_visible_y + slack;
14981
14982 /* Compute the distance from the scroll margin to PT or to
14983 the scroll limit, whichever comes first. This should
14984 include the height of the cursor line, to make that line
14985 fully visible. */
14986 move_it_to (&it, PT, -1, y_to_move,
14987 -1, MOVE_TO_POS | MOVE_TO_Y);
14988 dy = line_bottom_y (&it) - y0;
14989
14990 if (dy > scroll_max)
14991 return SCROLLING_FAILED;
14992
14993 if (dy > 0)
14994 scroll_down_p = 1;
14995 }
14996 }
14997
14998 if (scroll_down_p)
14999 {
15000 /* Point is in or below the bottom scroll margin, so move the
15001 window start down. If scrolling conservatively, move it just
15002 enough down to make point visible. If scroll_step is set,
15003 move it down by scroll_step. */
15004 if (arg_scroll_conservatively)
15005 amount_to_scroll
15006 = min (max (dy, frame_line_height),
15007 frame_line_height * arg_scroll_conservatively);
15008 else if (scroll_step || temp_scroll_step)
15009 amount_to_scroll = scroll_max;
15010 else
15011 {
15012 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15013 height = WINDOW_BOX_TEXT_HEIGHT (w);
15014 if (NUMBERP (aggressive))
15015 {
15016 double float_amount = XFLOATINT (aggressive) * height;
15017 int aggressive_scroll = float_amount;
15018 if (aggressive_scroll == 0 && float_amount > 0)
15019 aggressive_scroll = 1;
15020 /* Don't let point enter the scroll margin near top of
15021 the window. This could happen if the value of
15022 scroll_up_aggressively is too large and there are
15023 non-zero margins, because scroll_up_aggressively
15024 means put point that fraction of window height
15025 _from_the_bottom_margin_. */
15026 if (aggressive_scroll + 2*this_scroll_margin > height)
15027 aggressive_scroll = height - 2*this_scroll_margin;
15028 amount_to_scroll = dy + aggressive_scroll;
15029 }
15030 }
15031
15032 if (amount_to_scroll <= 0)
15033 return SCROLLING_FAILED;
15034
15035 start_display (&it, w, startp);
15036 if (arg_scroll_conservatively <= scroll_limit)
15037 move_it_vertically (&it, amount_to_scroll);
15038 else
15039 {
15040 /* Extra precision for users who set scroll-conservatively
15041 to a large number: make sure the amount we scroll
15042 the window start is never less than amount_to_scroll,
15043 which was computed as distance from window bottom to
15044 point. This matters when lines at window top and lines
15045 below window bottom have different height. */
15046 struct it it1;
15047 void *it1data = NULL;
15048 /* We use a temporary it1 because line_bottom_y can modify
15049 its argument, if it moves one line down; see there. */
15050 int start_y;
15051
15052 SAVE_IT (it1, it, it1data);
15053 start_y = line_bottom_y (&it1);
15054 do {
15055 RESTORE_IT (&it, &it, it1data);
15056 move_it_by_lines (&it, 1);
15057 SAVE_IT (it1, it, it1data);
15058 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15059 }
15060
15061 /* If STARTP is unchanged, move it down another screen line. */
15062 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15063 move_it_by_lines (&it, 1);
15064 startp = it.current.pos;
15065 }
15066 else
15067 {
15068 struct text_pos scroll_margin_pos = startp;
15069 int y_offset = 0;
15070
15071 /* See if point is inside the scroll margin at the top of the
15072 window. */
15073 if (this_scroll_margin)
15074 {
15075 int y_start;
15076
15077 start_display (&it, w, startp);
15078 y_start = it.current_y;
15079 move_it_vertically (&it, this_scroll_margin);
15080 scroll_margin_pos = it.current.pos;
15081 /* If we didn't move enough before hitting ZV, request
15082 additional amount of scroll, to move point out of the
15083 scroll margin. */
15084 if (IT_CHARPOS (it) == ZV
15085 && it.current_y - y_start < this_scroll_margin)
15086 y_offset = this_scroll_margin - (it.current_y - y_start);
15087 }
15088
15089 if (PT < CHARPOS (scroll_margin_pos))
15090 {
15091 /* Point is in the scroll margin at the top of the window or
15092 above what is displayed in the window. */
15093 int y0, y_to_move;
15094
15095 /* Compute the vertical distance from PT to the scroll
15096 margin position. Move as far as scroll_max allows, or
15097 one screenful, or 10 screen lines, whichever is largest.
15098 Give up if distance is greater than scroll_max or if we
15099 didn't reach the scroll margin position. */
15100 SET_TEXT_POS (pos, PT, PT_BYTE);
15101 start_display (&it, w, pos);
15102 y0 = it.current_y;
15103 y_to_move = max (it.last_visible_y,
15104 max (scroll_max, 10 * frame_line_height));
15105 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15106 y_to_move, -1,
15107 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15108 dy = it.current_y - y0;
15109 if (dy > scroll_max
15110 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15111 return SCROLLING_FAILED;
15112
15113 /* Additional scroll for when ZV was too close to point. */
15114 dy += y_offset;
15115
15116 /* Compute new window start. */
15117 start_display (&it, w, startp);
15118
15119 if (arg_scroll_conservatively)
15120 amount_to_scroll = max (dy, frame_line_height *
15121 max (scroll_step, temp_scroll_step));
15122 else if (scroll_step || temp_scroll_step)
15123 amount_to_scroll = scroll_max;
15124 else
15125 {
15126 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15127 height = WINDOW_BOX_TEXT_HEIGHT (w);
15128 if (NUMBERP (aggressive))
15129 {
15130 double float_amount = XFLOATINT (aggressive) * height;
15131 int aggressive_scroll = float_amount;
15132 if (aggressive_scroll == 0 && float_amount > 0)
15133 aggressive_scroll = 1;
15134 /* Don't let point enter the scroll margin near
15135 bottom of the window, if the value of
15136 scroll_down_aggressively happens to be too
15137 large. */
15138 if (aggressive_scroll + 2*this_scroll_margin > height)
15139 aggressive_scroll = height - 2*this_scroll_margin;
15140 amount_to_scroll = dy + aggressive_scroll;
15141 }
15142 }
15143
15144 if (amount_to_scroll <= 0)
15145 return SCROLLING_FAILED;
15146
15147 move_it_vertically_backward (&it, amount_to_scroll);
15148 startp = it.current.pos;
15149 }
15150 }
15151
15152 /* Run window scroll functions. */
15153 startp = run_window_scroll_functions (window, startp);
15154
15155 /* Display the window. Give up if new fonts are loaded, or if point
15156 doesn't appear. */
15157 if (!try_window (window, startp, 0))
15158 rc = SCROLLING_NEED_LARGER_MATRICES;
15159 else if (w->cursor.vpos < 0)
15160 {
15161 clear_glyph_matrix (w->desired_matrix);
15162 rc = SCROLLING_FAILED;
15163 }
15164 else
15165 {
15166 /* Maybe forget recorded base line for line number display. */
15167 if (!just_this_one_p
15168 || current_buffer->clip_changed
15169 || BEG_UNCHANGED < CHARPOS (startp))
15170 w->base_line_number = 0;
15171
15172 /* If cursor ends up on a partially visible line,
15173 treat that as being off the bottom of the screen. */
15174 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15175 /* It's possible that the cursor is on the first line of the
15176 buffer, which is partially obscured due to a vscroll
15177 (Bug#7537). In that case, avoid looping forever. */
15178 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15179 {
15180 clear_glyph_matrix (w->desired_matrix);
15181 ++extra_scroll_margin_lines;
15182 goto too_near_end;
15183 }
15184 rc = SCROLLING_SUCCESS;
15185 }
15186
15187 return rc;
15188 }
15189
15190
15191 /* Compute a suitable window start for window W if display of W starts
15192 on a continuation line. Value is non-zero if a new window start
15193 was computed.
15194
15195 The new window start will be computed, based on W's width, starting
15196 from the start of the continued line. It is the start of the
15197 screen line with the minimum distance from the old start W->start. */
15198
15199 static int
15200 compute_window_start_on_continuation_line (struct window *w)
15201 {
15202 struct text_pos pos, start_pos;
15203 int window_start_changed_p = 0;
15204
15205 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15206
15207 /* If window start is on a continuation line... Window start may be
15208 < BEGV in case there's invisible text at the start of the
15209 buffer (M-x rmail, for example). */
15210 if (CHARPOS (start_pos) > BEGV
15211 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15212 {
15213 struct it it;
15214 struct glyph_row *row;
15215
15216 /* Handle the case that the window start is out of range. */
15217 if (CHARPOS (start_pos) < BEGV)
15218 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15219 else if (CHARPOS (start_pos) > ZV)
15220 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15221
15222 /* Find the start of the continued line. This should be fast
15223 because find_newline is fast (newline cache). */
15224 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15225 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15226 row, DEFAULT_FACE_ID);
15227 reseat_at_previous_visible_line_start (&it);
15228
15229 /* If the line start is "too far" away from the window start,
15230 say it takes too much time to compute a new window start. */
15231 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15232 /* PXW: Do we need upper bounds here? */
15233 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15234 {
15235 int min_distance, distance;
15236
15237 /* Move forward by display lines to find the new window
15238 start. If window width was enlarged, the new start can
15239 be expected to be > the old start. If window width was
15240 decreased, the new window start will be < the old start.
15241 So, we're looking for the display line start with the
15242 minimum distance from the old window start. */
15243 pos = it.current.pos;
15244 min_distance = INFINITY;
15245 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15246 distance < min_distance)
15247 {
15248 min_distance = distance;
15249 pos = it.current.pos;
15250 if (it.line_wrap == WORD_WRAP)
15251 {
15252 /* Under WORD_WRAP, move_it_by_lines is likely to
15253 overshoot and stop not at the first, but the
15254 second character from the left margin. So in
15255 that case, we need a more tight control on the X
15256 coordinate of the iterator than move_it_by_lines
15257 promises in its contract. The method is to first
15258 go to the last (rightmost) visible character of a
15259 line, then move to the leftmost character on the
15260 next line in a separate call. */
15261 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15262 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15263 move_it_to (&it, ZV, 0,
15264 it.current_y + it.max_ascent + it.max_descent, -1,
15265 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15266 }
15267 else
15268 move_it_by_lines (&it, 1);
15269 }
15270
15271 /* Set the window start there. */
15272 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15273 window_start_changed_p = 1;
15274 }
15275 }
15276
15277 return window_start_changed_p;
15278 }
15279
15280
15281 /* Try cursor movement in case text has not changed in window WINDOW,
15282 with window start STARTP. Value is
15283
15284 CURSOR_MOVEMENT_SUCCESS if successful
15285
15286 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15287
15288 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15289 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15290 we want to scroll as if scroll-step were set to 1. See the code.
15291
15292 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15293 which case we have to abort this redisplay, and adjust matrices
15294 first. */
15295
15296 enum
15297 {
15298 CURSOR_MOVEMENT_SUCCESS,
15299 CURSOR_MOVEMENT_CANNOT_BE_USED,
15300 CURSOR_MOVEMENT_MUST_SCROLL,
15301 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15302 };
15303
15304 static int
15305 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15306 {
15307 struct window *w = XWINDOW (window);
15308 struct frame *f = XFRAME (w->frame);
15309 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15310
15311 #ifdef GLYPH_DEBUG
15312 if (inhibit_try_cursor_movement)
15313 return rc;
15314 #endif
15315
15316 /* Previously, there was a check for Lisp integer in the
15317 if-statement below. Now, this field is converted to
15318 ptrdiff_t, thus zero means invalid position in a buffer. */
15319 eassert (w->last_point > 0);
15320 /* Likewise there was a check whether window_end_vpos is nil or larger
15321 than the window. Now window_end_vpos is int and so never nil, but
15322 let's leave eassert to check whether it fits in the window. */
15323 eassert (w->window_end_vpos < w->current_matrix->nrows);
15324
15325 /* Handle case where text has not changed, only point, and it has
15326 not moved off the frame. */
15327 if (/* Point may be in this window. */
15328 PT >= CHARPOS (startp)
15329 /* Selective display hasn't changed. */
15330 && !current_buffer->clip_changed
15331 /* Function force-mode-line-update is used to force a thorough
15332 redisplay. It sets either windows_or_buffers_changed or
15333 update_mode_lines. So don't take a shortcut here for these
15334 cases. */
15335 && !update_mode_lines
15336 && !windows_or_buffers_changed
15337 && !f->cursor_type_changed
15338 && NILP (Vshow_trailing_whitespace)
15339 /* This code is not used for mini-buffer for the sake of the case
15340 of redisplaying to replace an echo area message; since in
15341 that case the mini-buffer contents per se are usually
15342 unchanged. This code is of no real use in the mini-buffer
15343 since the handling of this_line_start_pos, etc., in redisplay
15344 handles the same cases. */
15345 && !EQ (window, minibuf_window)
15346 && (FRAME_WINDOW_P (f)
15347 || !overlay_arrow_in_current_buffer_p ()))
15348 {
15349 int this_scroll_margin, top_scroll_margin;
15350 struct glyph_row *row = NULL;
15351 int frame_line_height = default_line_pixel_height (w);
15352 int window_total_lines
15353 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15354
15355 #ifdef GLYPH_DEBUG
15356 debug_method_add (w, "cursor movement");
15357 #endif
15358
15359 /* Scroll if point within this distance from the top or bottom
15360 of the window. This is a pixel value. */
15361 if (scroll_margin > 0)
15362 {
15363 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15364 this_scroll_margin *= frame_line_height;
15365 }
15366 else
15367 this_scroll_margin = 0;
15368
15369 top_scroll_margin = this_scroll_margin;
15370 if (WINDOW_WANTS_HEADER_LINE_P (w))
15371 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15372
15373 /* Start with the row the cursor was displayed during the last
15374 not paused redisplay. Give up if that row is not valid. */
15375 if (w->last_cursor_vpos < 0
15376 || w->last_cursor_vpos >= w->current_matrix->nrows)
15377 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15378 else
15379 {
15380 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15381 if (row->mode_line_p)
15382 ++row;
15383 if (!row->enabled_p)
15384 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15385 }
15386
15387 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15388 {
15389 int scroll_p = 0, must_scroll = 0;
15390 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15391
15392 if (PT > w->last_point)
15393 {
15394 /* Point has moved forward. */
15395 while (MATRIX_ROW_END_CHARPOS (row) < PT
15396 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15397 {
15398 eassert (row->enabled_p);
15399 ++row;
15400 }
15401
15402 /* If the end position of a row equals the start
15403 position of the next row, and PT is at that position,
15404 we would rather display cursor in the next line. */
15405 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15406 && MATRIX_ROW_END_CHARPOS (row) == PT
15407 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15408 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15409 && !cursor_row_p (row))
15410 ++row;
15411
15412 /* If within the scroll margin, scroll. Note that
15413 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15414 the next line would be drawn, and that
15415 this_scroll_margin can be zero. */
15416 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15417 || PT > MATRIX_ROW_END_CHARPOS (row)
15418 /* Line is completely visible last line in window
15419 and PT is to be set in the next line. */
15420 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15421 && PT == MATRIX_ROW_END_CHARPOS (row)
15422 && !row->ends_at_zv_p
15423 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15424 scroll_p = 1;
15425 }
15426 else if (PT < w->last_point)
15427 {
15428 /* Cursor has to be moved backward. Note that PT >=
15429 CHARPOS (startp) because of the outer if-statement. */
15430 while (!row->mode_line_p
15431 && (MATRIX_ROW_START_CHARPOS (row) > PT
15432 || (MATRIX_ROW_START_CHARPOS (row) == PT
15433 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15434 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15435 row > w->current_matrix->rows
15436 && (row-1)->ends_in_newline_from_string_p))))
15437 && (row->y > top_scroll_margin
15438 || CHARPOS (startp) == BEGV))
15439 {
15440 eassert (row->enabled_p);
15441 --row;
15442 }
15443
15444 /* Consider the following case: Window starts at BEGV,
15445 there is invisible, intangible text at BEGV, so that
15446 display starts at some point START > BEGV. It can
15447 happen that we are called with PT somewhere between
15448 BEGV and START. Try to handle that case. */
15449 if (row < w->current_matrix->rows
15450 || row->mode_line_p)
15451 {
15452 row = w->current_matrix->rows;
15453 if (row->mode_line_p)
15454 ++row;
15455 }
15456
15457 /* Due to newlines in overlay strings, we may have to
15458 skip forward over overlay strings. */
15459 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15460 && MATRIX_ROW_END_CHARPOS (row) == PT
15461 && !cursor_row_p (row))
15462 ++row;
15463
15464 /* If within the scroll margin, scroll. */
15465 if (row->y < top_scroll_margin
15466 && CHARPOS (startp) != BEGV)
15467 scroll_p = 1;
15468 }
15469 else
15470 {
15471 /* Cursor did not move. So don't scroll even if cursor line
15472 is partially visible, as it was so before. */
15473 rc = CURSOR_MOVEMENT_SUCCESS;
15474 }
15475
15476 if (PT < MATRIX_ROW_START_CHARPOS (row)
15477 || PT > MATRIX_ROW_END_CHARPOS (row))
15478 {
15479 /* if PT is not in the glyph row, give up. */
15480 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15481 must_scroll = 1;
15482 }
15483 else if (rc != CURSOR_MOVEMENT_SUCCESS
15484 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15485 {
15486 struct glyph_row *row1;
15487
15488 /* If rows are bidi-reordered and point moved, back up
15489 until we find a row that does not belong to a
15490 continuation line. This is because we must consider
15491 all rows of a continued line as candidates for the
15492 new cursor positioning, since row start and end
15493 positions change non-linearly with vertical position
15494 in such rows. */
15495 /* FIXME: Revisit this when glyph ``spilling'' in
15496 continuation lines' rows is implemented for
15497 bidi-reordered rows. */
15498 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15499 MATRIX_ROW_CONTINUATION_LINE_P (row);
15500 --row)
15501 {
15502 /* If we hit the beginning of the displayed portion
15503 without finding the first row of a continued
15504 line, give up. */
15505 if (row <= row1)
15506 {
15507 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15508 break;
15509 }
15510 eassert (row->enabled_p);
15511 }
15512 }
15513 if (must_scroll)
15514 ;
15515 else if (rc != CURSOR_MOVEMENT_SUCCESS
15516 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15517 /* Make sure this isn't a header line by any chance, since
15518 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15519 && !row->mode_line_p
15520 && make_cursor_line_fully_visible_p)
15521 {
15522 if (PT == MATRIX_ROW_END_CHARPOS (row)
15523 && !row->ends_at_zv_p
15524 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15525 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15526 else if (row->height > window_box_height (w))
15527 {
15528 /* If we end up in a partially visible line, let's
15529 make it fully visible, except when it's taller
15530 than the window, in which case we can't do much
15531 about it. */
15532 *scroll_step = 1;
15533 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15534 }
15535 else
15536 {
15537 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15538 if (!cursor_row_fully_visible_p (w, 0, 1))
15539 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15540 else
15541 rc = CURSOR_MOVEMENT_SUCCESS;
15542 }
15543 }
15544 else if (scroll_p)
15545 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15546 else if (rc != CURSOR_MOVEMENT_SUCCESS
15547 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15548 {
15549 /* With bidi-reordered rows, there could be more than
15550 one candidate row whose start and end positions
15551 occlude point. We need to let set_cursor_from_row
15552 find the best candidate. */
15553 /* FIXME: Revisit this when glyph ``spilling'' in
15554 continuation lines' rows is implemented for
15555 bidi-reordered rows. */
15556 int rv = 0;
15557
15558 do
15559 {
15560 int at_zv_p = 0, exact_match_p = 0;
15561
15562 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15563 && PT <= MATRIX_ROW_END_CHARPOS (row)
15564 && cursor_row_p (row))
15565 rv |= set_cursor_from_row (w, row, w->current_matrix,
15566 0, 0, 0, 0);
15567 /* As soon as we've found the exact match for point,
15568 or the first suitable row whose ends_at_zv_p flag
15569 is set, we are done. */
15570 if (rv)
15571 {
15572 at_zv_p = MATRIX_ROW (w->current_matrix,
15573 w->cursor.vpos)->ends_at_zv_p;
15574 if (!at_zv_p
15575 && w->cursor.hpos >= 0
15576 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15577 w->cursor.vpos))
15578 {
15579 struct glyph_row *candidate =
15580 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15581 struct glyph *g =
15582 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15583 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15584
15585 exact_match_p =
15586 (BUFFERP (g->object) && g->charpos == PT)
15587 || (INTEGERP (g->object)
15588 && (g->charpos == PT
15589 || (g->charpos == 0 && endpos - 1 == PT)));
15590 }
15591 if (at_zv_p || exact_match_p)
15592 {
15593 rc = CURSOR_MOVEMENT_SUCCESS;
15594 break;
15595 }
15596 }
15597 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15598 break;
15599 ++row;
15600 }
15601 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15602 || row->continued_p)
15603 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15604 || (MATRIX_ROW_START_CHARPOS (row) == PT
15605 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15606 /* If we didn't find any candidate rows, or exited the
15607 loop before all the candidates were examined, signal
15608 to the caller that this method failed. */
15609 if (rc != CURSOR_MOVEMENT_SUCCESS
15610 && !(rv
15611 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15612 && !row->continued_p))
15613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15614 else if (rv)
15615 rc = CURSOR_MOVEMENT_SUCCESS;
15616 }
15617 else
15618 {
15619 do
15620 {
15621 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15622 {
15623 rc = CURSOR_MOVEMENT_SUCCESS;
15624 break;
15625 }
15626 ++row;
15627 }
15628 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15629 && MATRIX_ROW_START_CHARPOS (row) == PT
15630 && cursor_row_p (row));
15631 }
15632 }
15633 }
15634
15635 return rc;
15636 }
15637
15638 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15639 static
15640 #endif
15641 void
15642 set_vertical_scroll_bar (struct window *w)
15643 {
15644 ptrdiff_t start, end, whole;
15645
15646 /* Calculate the start and end positions for the current window.
15647 At some point, it would be nice to choose between scrollbars
15648 which reflect the whole buffer size, with special markers
15649 indicating narrowing, and scrollbars which reflect only the
15650 visible region.
15651
15652 Note that mini-buffers sometimes aren't displaying any text. */
15653 if (!MINI_WINDOW_P (w)
15654 || (w == XWINDOW (minibuf_window)
15655 && NILP (echo_area_buffer[0])))
15656 {
15657 struct buffer *buf = XBUFFER (w->contents);
15658 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15659 start = marker_position (w->start) - BUF_BEGV (buf);
15660 /* I don't think this is guaranteed to be right. For the
15661 moment, we'll pretend it is. */
15662 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15663
15664 if (end < start)
15665 end = start;
15666 if (whole < (end - start))
15667 whole = end - start;
15668 }
15669 else
15670 start = end = whole = 0;
15671
15672 /* Indicate what this scroll bar ought to be displaying now. */
15673 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15674 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15675 (w, end - start, whole, start);
15676 }
15677
15678
15679 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15680 selected_window is redisplayed.
15681
15682 We can return without actually redisplaying the window if fonts has been
15683 changed on window's frame. In that case, redisplay_internal will retry. */
15684
15685 static void
15686 redisplay_window (Lisp_Object window, bool just_this_one_p)
15687 {
15688 struct window *w = XWINDOW (window);
15689 struct frame *f = XFRAME (w->frame);
15690 struct buffer *buffer = XBUFFER (w->contents);
15691 struct buffer *old = current_buffer;
15692 struct text_pos lpoint, opoint, startp;
15693 int update_mode_line;
15694 int tem;
15695 struct it it;
15696 /* Record it now because it's overwritten. */
15697 bool current_matrix_up_to_date_p = false;
15698 bool used_current_matrix_p = false;
15699 /* This is less strict than current_matrix_up_to_date_p.
15700 It indicates that the buffer contents and narrowing are unchanged. */
15701 bool buffer_unchanged_p = false;
15702 int temp_scroll_step = 0;
15703 ptrdiff_t count = SPECPDL_INDEX ();
15704 int rc;
15705 int centering_position = -1;
15706 int last_line_misfit = 0;
15707 ptrdiff_t beg_unchanged, end_unchanged;
15708 int frame_line_height;
15709
15710 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15711 opoint = lpoint;
15712
15713 #ifdef GLYPH_DEBUG
15714 *w->desired_matrix->method = 0;
15715 #endif
15716
15717 if (!just_this_one_p
15718 && REDISPLAY_SOME_P ()
15719 && !w->redisplay
15720 && !f->redisplay
15721 && !buffer->text->redisplay
15722 && BUF_PT (buffer) == w->last_point)
15723 return;
15724
15725 /* Make sure that both W's markers are valid. */
15726 eassert (XMARKER (w->start)->buffer == buffer);
15727 eassert (XMARKER (w->pointm)->buffer == buffer);
15728
15729 restart:
15730 reconsider_clip_changes (w);
15731 frame_line_height = default_line_pixel_height (w);
15732
15733 /* Has the mode line to be updated? */
15734 update_mode_line = (w->update_mode_line
15735 || update_mode_lines
15736 || buffer->clip_changed
15737 || buffer->prevent_redisplay_optimizations_p);
15738
15739 if (!just_this_one_p)
15740 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15741 cleverly elsewhere. */
15742 w->must_be_updated_p = true;
15743
15744 if (MINI_WINDOW_P (w))
15745 {
15746 if (w == XWINDOW (echo_area_window)
15747 && !NILP (echo_area_buffer[0]))
15748 {
15749 if (update_mode_line)
15750 /* We may have to update a tty frame's menu bar or a
15751 tool-bar. Example `M-x C-h C-h C-g'. */
15752 goto finish_menu_bars;
15753 else
15754 /* We've already displayed the echo area glyphs in this window. */
15755 goto finish_scroll_bars;
15756 }
15757 else if ((w != XWINDOW (minibuf_window)
15758 || minibuf_level == 0)
15759 /* When buffer is nonempty, redisplay window normally. */
15760 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15761 /* Quail displays non-mini buffers in minibuffer window.
15762 In that case, redisplay the window normally. */
15763 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15764 {
15765 /* W is a mini-buffer window, but it's not active, so clear
15766 it. */
15767 int yb = window_text_bottom_y (w);
15768 struct glyph_row *row;
15769 int y;
15770
15771 for (y = 0, row = w->desired_matrix->rows;
15772 y < yb;
15773 y += row->height, ++row)
15774 blank_row (w, row, y);
15775 goto finish_scroll_bars;
15776 }
15777
15778 clear_glyph_matrix (w->desired_matrix);
15779 }
15780
15781 /* Otherwise set up data on this window; select its buffer and point
15782 value. */
15783 /* Really select the buffer, for the sake of buffer-local
15784 variables. */
15785 set_buffer_internal_1 (XBUFFER (w->contents));
15786
15787 current_matrix_up_to_date_p
15788 = (w->window_end_valid
15789 && !current_buffer->clip_changed
15790 && !current_buffer->prevent_redisplay_optimizations_p
15791 && !window_outdated (w));
15792
15793 /* Run the window-bottom-change-functions
15794 if it is possible that the text on the screen has changed
15795 (either due to modification of the text, or any other reason). */
15796 if (!current_matrix_up_to_date_p
15797 && !NILP (Vwindow_text_change_functions))
15798 {
15799 safe_run_hooks (Qwindow_text_change_functions);
15800 goto restart;
15801 }
15802
15803 beg_unchanged = BEG_UNCHANGED;
15804 end_unchanged = END_UNCHANGED;
15805
15806 SET_TEXT_POS (opoint, PT, PT_BYTE);
15807
15808 specbind (Qinhibit_point_motion_hooks, Qt);
15809
15810 buffer_unchanged_p
15811 = (w->window_end_valid
15812 && !current_buffer->clip_changed
15813 && !window_outdated (w));
15814
15815 /* When windows_or_buffers_changed is non-zero, we can't rely
15816 on the window end being valid, so set it to zero there. */
15817 if (windows_or_buffers_changed)
15818 {
15819 /* If window starts on a continuation line, maybe adjust the
15820 window start in case the window's width changed. */
15821 if (XMARKER (w->start)->buffer == current_buffer)
15822 compute_window_start_on_continuation_line (w);
15823
15824 w->window_end_valid = false;
15825 /* If so, we also can't rely on current matrix
15826 and should not fool try_cursor_movement below. */
15827 current_matrix_up_to_date_p = false;
15828 }
15829
15830 /* Some sanity checks. */
15831 CHECK_WINDOW_END (w);
15832 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15833 emacs_abort ();
15834 if (BYTEPOS (opoint) < CHARPOS (opoint))
15835 emacs_abort ();
15836
15837 if (mode_line_update_needed (w))
15838 update_mode_line = 1;
15839
15840 /* Point refers normally to the selected window. For any other
15841 window, set up appropriate value. */
15842 if (!EQ (window, selected_window))
15843 {
15844 ptrdiff_t new_pt = marker_position (w->pointm);
15845 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15846 if (new_pt < BEGV)
15847 {
15848 new_pt = BEGV;
15849 new_pt_byte = BEGV_BYTE;
15850 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15851 }
15852 else if (new_pt > (ZV - 1))
15853 {
15854 new_pt = ZV;
15855 new_pt_byte = ZV_BYTE;
15856 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15857 }
15858
15859 /* We don't use SET_PT so that the point-motion hooks don't run. */
15860 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15861 }
15862
15863 /* If any of the character widths specified in the display table
15864 have changed, invalidate the width run cache. It's true that
15865 this may be a bit late to catch such changes, but the rest of
15866 redisplay goes (non-fatally) haywire when the display table is
15867 changed, so why should we worry about doing any better? */
15868 if (current_buffer->width_run_cache
15869 || (current_buffer->base_buffer
15870 && current_buffer->base_buffer->width_run_cache))
15871 {
15872 struct Lisp_Char_Table *disptab = buffer_display_table ();
15873
15874 if (! disptab_matches_widthtab
15875 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15876 {
15877 struct buffer *buf = current_buffer;
15878
15879 if (buf->base_buffer)
15880 buf = buf->base_buffer;
15881 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15882 recompute_width_table (current_buffer, disptab);
15883 }
15884 }
15885
15886 /* If window-start is screwed up, choose a new one. */
15887 if (XMARKER (w->start)->buffer != current_buffer)
15888 goto recenter;
15889
15890 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15891
15892 /* If someone specified a new starting point but did not insist,
15893 check whether it can be used. */
15894 if (w->optional_new_start
15895 && CHARPOS (startp) >= BEGV
15896 && CHARPOS (startp) <= ZV)
15897 {
15898 w->optional_new_start = 0;
15899 start_display (&it, w, startp);
15900 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15901 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15902 if (IT_CHARPOS (it) == PT)
15903 w->force_start = 1;
15904 /* IT may overshoot PT if text at PT is invisible. */
15905 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15906 w->force_start = 1;
15907 }
15908
15909 force_start:
15910
15911 /* Handle case where place to start displaying has been specified,
15912 unless the specified location is outside the accessible range. */
15913 if (w->force_start || window_frozen_p (w))
15914 {
15915 /* We set this later on if we have to adjust point. */
15916 int new_vpos = -1;
15917
15918 w->force_start = 0;
15919 w->vscroll = 0;
15920 w->window_end_valid = 0;
15921
15922 /* Forget any recorded base line for line number display. */
15923 if (!buffer_unchanged_p)
15924 w->base_line_number = 0;
15925
15926 /* Redisplay the mode line. Select the buffer properly for that.
15927 Also, run the hook window-scroll-functions
15928 because we have scrolled. */
15929 /* Note, we do this after clearing force_start because
15930 if there's an error, it is better to forget about force_start
15931 than to get into an infinite loop calling the hook functions
15932 and having them get more errors. */
15933 if (!update_mode_line
15934 || ! NILP (Vwindow_scroll_functions))
15935 {
15936 update_mode_line = 1;
15937 w->update_mode_line = 1;
15938 startp = run_window_scroll_functions (window, startp);
15939 }
15940
15941 if (CHARPOS (startp) < BEGV)
15942 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15943 else if (CHARPOS (startp) > ZV)
15944 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15945
15946 /* Redisplay, then check if cursor has been set during the
15947 redisplay. Give up if new fonts were loaded. */
15948 /* We used to issue a CHECK_MARGINS argument to try_window here,
15949 but this causes scrolling to fail when point begins inside
15950 the scroll margin (bug#148) -- cyd */
15951 if (!try_window (window, startp, 0))
15952 {
15953 w->force_start = 1;
15954 clear_glyph_matrix (w->desired_matrix);
15955 goto need_larger_matrices;
15956 }
15957
15958 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15959 {
15960 /* If point does not appear, try to move point so it does
15961 appear. The desired matrix has been built above, so we
15962 can use it here. */
15963 new_vpos = window_box_height (w) / 2;
15964 }
15965
15966 if (!cursor_row_fully_visible_p (w, 0, 0))
15967 {
15968 /* Point does appear, but on a line partly visible at end of window.
15969 Move it back to a fully-visible line. */
15970 new_vpos = window_box_height (w);
15971 }
15972 else if (w->cursor.vpos >= 0)
15973 {
15974 /* Some people insist on not letting point enter the scroll
15975 margin, even though this part handles windows that didn't
15976 scroll at all. */
15977 int window_total_lines
15978 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15979 int margin = min (scroll_margin, window_total_lines / 4);
15980 int pixel_margin = margin * frame_line_height;
15981 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15982
15983 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15984 below, which finds the row to move point to, advances by
15985 the Y coordinate of the _next_ row, see the definition of
15986 MATRIX_ROW_BOTTOM_Y. */
15987 if (w->cursor.vpos < margin + header_line)
15988 {
15989 w->cursor.vpos = -1;
15990 clear_glyph_matrix (w->desired_matrix);
15991 goto try_to_scroll;
15992 }
15993 else
15994 {
15995 int window_height = window_box_height (w);
15996
15997 if (header_line)
15998 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15999 if (w->cursor.y >= window_height - pixel_margin)
16000 {
16001 w->cursor.vpos = -1;
16002 clear_glyph_matrix (w->desired_matrix);
16003 goto try_to_scroll;
16004 }
16005 }
16006 }
16007
16008 /* If we need to move point for either of the above reasons,
16009 now actually do it. */
16010 if (new_vpos >= 0)
16011 {
16012 struct glyph_row *row;
16013
16014 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16015 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16016 ++row;
16017
16018 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16019 MATRIX_ROW_START_BYTEPOS (row));
16020
16021 if (w != XWINDOW (selected_window))
16022 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16023 else if (current_buffer == old)
16024 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16025
16026 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16027
16028 /* If we are highlighting the region, then we just changed
16029 the region, so redisplay to show it. */
16030 /* FIXME: We need to (re)run pre-redisplay-function! */
16031 /* if (markpos_of_region () >= 0)
16032 {
16033 clear_glyph_matrix (w->desired_matrix);
16034 if (!try_window (window, startp, 0))
16035 goto need_larger_matrices;
16036 }
16037 */
16038 }
16039
16040 #ifdef GLYPH_DEBUG
16041 debug_method_add (w, "forced window start");
16042 #endif
16043 goto done;
16044 }
16045
16046 /* Handle case where text has not changed, only point, and it has
16047 not moved off the frame, and we are not retrying after hscroll.
16048 (current_matrix_up_to_date_p is nonzero when retrying.) */
16049 if (current_matrix_up_to_date_p
16050 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16051 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16052 {
16053 switch (rc)
16054 {
16055 case CURSOR_MOVEMENT_SUCCESS:
16056 used_current_matrix_p = 1;
16057 goto done;
16058
16059 case CURSOR_MOVEMENT_MUST_SCROLL:
16060 goto try_to_scroll;
16061
16062 default:
16063 emacs_abort ();
16064 }
16065 }
16066 /* If current starting point was originally the beginning of a line
16067 but no longer is, find a new starting point. */
16068 else if (w->start_at_line_beg
16069 && !(CHARPOS (startp) <= BEGV
16070 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16071 {
16072 #ifdef GLYPH_DEBUG
16073 debug_method_add (w, "recenter 1");
16074 #endif
16075 goto recenter;
16076 }
16077
16078 /* Try scrolling with try_window_id. Value is > 0 if update has
16079 been done, it is -1 if we know that the same window start will
16080 not work. It is 0 if unsuccessful for some other reason. */
16081 else if ((tem = try_window_id (w)) != 0)
16082 {
16083 #ifdef GLYPH_DEBUG
16084 debug_method_add (w, "try_window_id %d", tem);
16085 #endif
16086
16087 if (f->fonts_changed)
16088 goto need_larger_matrices;
16089 if (tem > 0)
16090 goto done;
16091
16092 /* Otherwise try_window_id has returned -1 which means that we
16093 don't want the alternative below this comment to execute. */
16094 }
16095 else if (CHARPOS (startp) >= BEGV
16096 && CHARPOS (startp) <= ZV
16097 && PT >= CHARPOS (startp)
16098 && (CHARPOS (startp) < ZV
16099 /* Avoid starting at end of buffer. */
16100 || CHARPOS (startp) == BEGV
16101 || !window_outdated (w)))
16102 {
16103 int d1, d2, d3, d4, d5, d6;
16104
16105 /* If first window line is a continuation line, and window start
16106 is inside the modified region, but the first change is before
16107 current window start, we must select a new window start.
16108
16109 However, if this is the result of a down-mouse event (e.g. by
16110 extending the mouse-drag-overlay), we don't want to select a
16111 new window start, since that would change the position under
16112 the mouse, resulting in an unwanted mouse-movement rather
16113 than a simple mouse-click. */
16114 if (!w->start_at_line_beg
16115 && NILP (do_mouse_tracking)
16116 && CHARPOS (startp) > BEGV
16117 && CHARPOS (startp) > BEG + beg_unchanged
16118 && CHARPOS (startp) <= Z - end_unchanged
16119 /* Even if w->start_at_line_beg is nil, a new window may
16120 start at a line_beg, since that's how set_buffer_window
16121 sets it. So, we need to check the return value of
16122 compute_window_start_on_continuation_line. (See also
16123 bug#197). */
16124 && XMARKER (w->start)->buffer == current_buffer
16125 && compute_window_start_on_continuation_line (w)
16126 /* It doesn't make sense to force the window start like we
16127 do at label force_start if it is already known that point
16128 will not be visible in the resulting window, because
16129 doing so will move point from its correct position
16130 instead of scrolling the window to bring point into view.
16131 See bug#9324. */
16132 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16133 {
16134 w->force_start = 1;
16135 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16136 goto force_start;
16137 }
16138
16139 #ifdef GLYPH_DEBUG
16140 debug_method_add (w, "same window start");
16141 #endif
16142
16143 /* Try to redisplay starting at same place as before.
16144 If point has not moved off frame, accept the results. */
16145 if (!current_matrix_up_to_date_p
16146 /* Don't use try_window_reusing_current_matrix in this case
16147 because a window scroll function can have changed the
16148 buffer. */
16149 || !NILP (Vwindow_scroll_functions)
16150 || MINI_WINDOW_P (w)
16151 || !(used_current_matrix_p
16152 = try_window_reusing_current_matrix (w)))
16153 {
16154 IF_DEBUG (debug_method_add (w, "1"));
16155 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16156 /* -1 means we need to scroll.
16157 0 means we need new matrices, but fonts_changed
16158 is set in that case, so we will detect it below. */
16159 goto try_to_scroll;
16160 }
16161
16162 if (f->fonts_changed)
16163 goto need_larger_matrices;
16164
16165 if (w->cursor.vpos >= 0)
16166 {
16167 if (!just_this_one_p
16168 || current_buffer->clip_changed
16169 || BEG_UNCHANGED < CHARPOS (startp))
16170 /* Forget any recorded base line for line number display. */
16171 w->base_line_number = 0;
16172
16173 if (!cursor_row_fully_visible_p (w, 1, 0))
16174 {
16175 clear_glyph_matrix (w->desired_matrix);
16176 last_line_misfit = 1;
16177 }
16178 /* Drop through and scroll. */
16179 else
16180 goto done;
16181 }
16182 else
16183 clear_glyph_matrix (w->desired_matrix);
16184 }
16185
16186 try_to_scroll:
16187
16188 /* Redisplay the mode line. Select the buffer properly for that. */
16189 if (!update_mode_line)
16190 {
16191 update_mode_line = 1;
16192 w->update_mode_line = 1;
16193 }
16194
16195 /* Try to scroll by specified few lines. */
16196 if ((scroll_conservatively
16197 || emacs_scroll_step
16198 || temp_scroll_step
16199 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16200 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16201 && CHARPOS (startp) >= BEGV
16202 && CHARPOS (startp) <= ZV)
16203 {
16204 /* The function returns -1 if new fonts were loaded, 1 if
16205 successful, 0 if not successful. */
16206 int ss = try_scrolling (window, just_this_one_p,
16207 scroll_conservatively,
16208 emacs_scroll_step,
16209 temp_scroll_step, last_line_misfit);
16210 switch (ss)
16211 {
16212 case SCROLLING_SUCCESS:
16213 goto done;
16214
16215 case SCROLLING_NEED_LARGER_MATRICES:
16216 goto need_larger_matrices;
16217
16218 case SCROLLING_FAILED:
16219 break;
16220
16221 default:
16222 emacs_abort ();
16223 }
16224 }
16225
16226 /* Finally, just choose a place to start which positions point
16227 according to user preferences. */
16228
16229 recenter:
16230
16231 #ifdef GLYPH_DEBUG
16232 debug_method_add (w, "recenter");
16233 #endif
16234
16235 /* Forget any previously recorded base line for line number display. */
16236 if (!buffer_unchanged_p)
16237 w->base_line_number = 0;
16238
16239 /* Determine the window start relative to point. */
16240 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16241 it.current_y = it.last_visible_y;
16242 if (centering_position < 0)
16243 {
16244 int window_total_lines
16245 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16246 int margin =
16247 scroll_margin > 0
16248 ? min (scroll_margin, window_total_lines / 4)
16249 : 0;
16250 ptrdiff_t margin_pos = CHARPOS (startp);
16251 Lisp_Object aggressive;
16252 int scrolling_up;
16253
16254 /* If there is a scroll margin at the top of the window, find
16255 its character position. */
16256 if (margin
16257 /* Cannot call start_display if startp is not in the
16258 accessible region of the buffer. This can happen when we
16259 have just switched to a different buffer and/or changed
16260 its restriction. In that case, startp is initialized to
16261 the character position 1 (BEGV) because we did not yet
16262 have chance to display the buffer even once. */
16263 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16264 {
16265 struct it it1;
16266 void *it1data = NULL;
16267
16268 SAVE_IT (it1, it, it1data);
16269 start_display (&it1, w, startp);
16270 move_it_vertically (&it1, margin * frame_line_height);
16271 margin_pos = IT_CHARPOS (it1);
16272 RESTORE_IT (&it, &it, it1data);
16273 }
16274 scrolling_up = PT > margin_pos;
16275 aggressive =
16276 scrolling_up
16277 ? BVAR (current_buffer, scroll_up_aggressively)
16278 : BVAR (current_buffer, scroll_down_aggressively);
16279
16280 if (!MINI_WINDOW_P (w)
16281 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16282 {
16283 int pt_offset = 0;
16284
16285 /* Setting scroll-conservatively overrides
16286 scroll-*-aggressively. */
16287 if (!scroll_conservatively && NUMBERP (aggressive))
16288 {
16289 double float_amount = XFLOATINT (aggressive);
16290
16291 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16292 if (pt_offset == 0 && float_amount > 0)
16293 pt_offset = 1;
16294 if (pt_offset && margin > 0)
16295 margin -= 1;
16296 }
16297 /* Compute how much to move the window start backward from
16298 point so that point will be displayed where the user
16299 wants it. */
16300 if (scrolling_up)
16301 {
16302 centering_position = it.last_visible_y;
16303 if (pt_offset)
16304 centering_position -= pt_offset;
16305 centering_position -=
16306 frame_line_height * (1 + margin + (last_line_misfit != 0))
16307 + WINDOW_HEADER_LINE_HEIGHT (w);
16308 /* Don't let point enter the scroll margin near top of
16309 the window. */
16310 if (centering_position < margin * frame_line_height)
16311 centering_position = margin * frame_line_height;
16312 }
16313 else
16314 centering_position = margin * frame_line_height + pt_offset;
16315 }
16316 else
16317 /* Set the window start half the height of the window backward
16318 from point. */
16319 centering_position = window_box_height (w) / 2;
16320 }
16321 move_it_vertically_backward (&it, centering_position);
16322
16323 eassert (IT_CHARPOS (it) >= BEGV);
16324
16325 /* The function move_it_vertically_backward may move over more
16326 than the specified y-distance. If it->w is small, e.g. a
16327 mini-buffer window, we may end up in front of the window's
16328 display area. Start displaying at the start of the line
16329 containing PT in this case. */
16330 if (it.current_y <= 0)
16331 {
16332 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16333 move_it_vertically_backward (&it, 0);
16334 it.current_y = 0;
16335 }
16336
16337 it.current_x = it.hpos = 0;
16338
16339 /* Set the window start position here explicitly, to avoid an
16340 infinite loop in case the functions in window-scroll-functions
16341 get errors. */
16342 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16343
16344 /* Run scroll hooks. */
16345 startp = run_window_scroll_functions (window, it.current.pos);
16346
16347 /* Redisplay the window. */
16348 if (!current_matrix_up_to_date_p
16349 || windows_or_buffers_changed
16350 || f->cursor_type_changed
16351 /* Don't use try_window_reusing_current_matrix in this case
16352 because it can have changed the buffer. */
16353 || !NILP (Vwindow_scroll_functions)
16354 || !just_this_one_p
16355 || MINI_WINDOW_P (w)
16356 || !(used_current_matrix_p
16357 = try_window_reusing_current_matrix (w)))
16358 try_window (window, startp, 0);
16359
16360 /* If new fonts have been loaded (due to fontsets), give up. We
16361 have to start a new redisplay since we need to re-adjust glyph
16362 matrices. */
16363 if (f->fonts_changed)
16364 goto need_larger_matrices;
16365
16366 /* If cursor did not appear assume that the middle of the window is
16367 in the first line of the window. Do it again with the next line.
16368 (Imagine a window of height 100, displaying two lines of height
16369 60. Moving back 50 from it->last_visible_y will end in the first
16370 line.) */
16371 if (w->cursor.vpos < 0)
16372 {
16373 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16374 {
16375 clear_glyph_matrix (w->desired_matrix);
16376 move_it_by_lines (&it, 1);
16377 try_window (window, it.current.pos, 0);
16378 }
16379 else if (PT < IT_CHARPOS (it))
16380 {
16381 clear_glyph_matrix (w->desired_matrix);
16382 move_it_by_lines (&it, -1);
16383 try_window (window, it.current.pos, 0);
16384 }
16385 else
16386 {
16387 /* Not much we can do about it. */
16388 }
16389 }
16390
16391 /* Consider the following case: Window starts at BEGV, there is
16392 invisible, intangible text at BEGV, so that display starts at
16393 some point START > BEGV. It can happen that we are called with
16394 PT somewhere between BEGV and START. Try to handle that case. */
16395 if (w->cursor.vpos < 0)
16396 {
16397 struct glyph_row *row = w->current_matrix->rows;
16398 if (row->mode_line_p)
16399 ++row;
16400 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16401 }
16402
16403 if (!cursor_row_fully_visible_p (w, 0, 0))
16404 {
16405 /* If vscroll is enabled, disable it and try again. */
16406 if (w->vscroll)
16407 {
16408 w->vscroll = 0;
16409 clear_glyph_matrix (w->desired_matrix);
16410 goto recenter;
16411 }
16412
16413 /* Users who set scroll-conservatively to a large number want
16414 point just above/below the scroll margin. If we ended up
16415 with point's row partially visible, move the window start to
16416 make that row fully visible and out of the margin. */
16417 if (scroll_conservatively > SCROLL_LIMIT)
16418 {
16419 int window_total_lines
16420 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16421 int margin =
16422 scroll_margin > 0
16423 ? min (scroll_margin, window_total_lines / 4)
16424 : 0;
16425 int move_down = w->cursor.vpos >= window_total_lines / 2;
16426
16427 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16428 clear_glyph_matrix (w->desired_matrix);
16429 if (1 == try_window (window, it.current.pos,
16430 TRY_WINDOW_CHECK_MARGINS))
16431 goto done;
16432 }
16433
16434 /* If centering point failed to make the whole line visible,
16435 put point at the top instead. That has to make the whole line
16436 visible, if it can be done. */
16437 if (centering_position == 0)
16438 goto done;
16439
16440 clear_glyph_matrix (w->desired_matrix);
16441 centering_position = 0;
16442 goto recenter;
16443 }
16444
16445 done:
16446
16447 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16448 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16449 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16450
16451 /* Display the mode line, if we must. */
16452 if ((update_mode_line
16453 /* If window not full width, must redo its mode line
16454 if (a) the window to its side is being redone and
16455 (b) we do a frame-based redisplay. This is a consequence
16456 of how inverted lines are drawn in frame-based redisplay. */
16457 || (!just_this_one_p
16458 && !FRAME_WINDOW_P (f)
16459 && !WINDOW_FULL_WIDTH_P (w))
16460 /* Line number to display. */
16461 || w->base_line_pos > 0
16462 /* Column number is displayed and different from the one displayed. */
16463 || (w->column_number_displayed != -1
16464 && (w->column_number_displayed != current_column ())))
16465 /* This means that the window has a mode line. */
16466 && (WINDOW_WANTS_MODELINE_P (w)
16467 || WINDOW_WANTS_HEADER_LINE_P (w)))
16468 {
16469
16470 display_mode_lines (w);
16471
16472 /* If mode line height has changed, arrange for a thorough
16473 immediate redisplay using the correct mode line height. */
16474 if (WINDOW_WANTS_MODELINE_P (w)
16475 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16476 {
16477 f->fonts_changed = 1;
16478 w->mode_line_height = -1;
16479 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16480 = DESIRED_MODE_LINE_HEIGHT (w);
16481 }
16482
16483 /* If header line height has changed, arrange for a thorough
16484 immediate redisplay using the correct header line height. */
16485 if (WINDOW_WANTS_HEADER_LINE_P (w)
16486 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16487 {
16488 f->fonts_changed = 1;
16489 w->header_line_height = -1;
16490 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16491 = DESIRED_HEADER_LINE_HEIGHT (w);
16492 }
16493
16494 if (f->fonts_changed)
16495 goto need_larger_matrices;
16496 }
16497
16498 if (!line_number_displayed && w->base_line_pos != -1)
16499 {
16500 w->base_line_pos = 0;
16501 w->base_line_number = 0;
16502 }
16503
16504 finish_menu_bars:
16505
16506 /* When we reach a frame's selected window, redo the frame's menu bar. */
16507 if (update_mode_line
16508 && EQ (FRAME_SELECTED_WINDOW (f), window))
16509 {
16510 int redisplay_menu_p = 0;
16511
16512 if (FRAME_WINDOW_P (f))
16513 {
16514 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16515 || defined (HAVE_NS) || defined (USE_GTK)
16516 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16517 #else
16518 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16519 #endif
16520 }
16521 else
16522 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16523
16524 if (redisplay_menu_p)
16525 display_menu_bar (w);
16526
16527 #ifdef HAVE_WINDOW_SYSTEM
16528 if (FRAME_WINDOW_P (f))
16529 {
16530 #if defined (USE_GTK) || defined (HAVE_NS)
16531 if (FRAME_EXTERNAL_TOOL_BAR (f))
16532 redisplay_tool_bar (f);
16533 #else
16534 if (WINDOWP (f->tool_bar_window)
16535 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16536 || !NILP (Vauto_resize_tool_bars))
16537 && redisplay_tool_bar (f))
16538 ignore_mouse_drag_p = 1;
16539 #endif
16540 }
16541 #endif
16542 }
16543
16544 #ifdef HAVE_WINDOW_SYSTEM
16545 if (FRAME_WINDOW_P (f)
16546 && update_window_fringes (w, (just_this_one_p
16547 || (!used_current_matrix_p && !overlay_arrow_seen)
16548 || w->pseudo_window_p)))
16549 {
16550 update_begin (f);
16551 block_input ();
16552 if (draw_window_fringes (w, 1))
16553 {
16554 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16555 x_draw_right_divider (w);
16556 else
16557 x_draw_vertical_border (w);
16558 }
16559 unblock_input ();
16560 update_end (f);
16561 }
16562
16563 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16564 x_draw_bottom_divider (w);
16565 #endif /* HAVE_WINDOW_SYSTEM */
16566
16567 /* We go to this label, with fonts_changed set, if it is
16568 necessary to try again using larger glyph matrices.
16569 We have to redeem the scroll bar even in this case,
16570 because the loop in redisplay_internal expects that. */
16571 need_larger_matrices:
16572 ;
16573 finish_scroll_bars:
16574
16575 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16576 {
16577 /* Set the thumb's position and size. */
16578 set_vertical_scroll_bar (w);
16579
16580 /* Note that we actually used the scroll bar attached to this
16581 window, so it shouldn't be deleted at the end of redisplay. */
16582 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16583 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16584 }
16585
16586 /* Restore current_buffer and value of point in it. The window
16587 update may have changed the buffer, so first make sure `opoint'
16588 is still valid (Bug#6177). */
16589 if (CHARPOS (opoint) < BEGV)
16590 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16591 else if (CHARPOS (opoint) > ZV)
16592 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16593 else
16594 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16595
16596 set_buffer_internal_1 (old);
16597 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16598 shorter. This can be caused by log truncation in *Messages*. */
16599 if (CHARPOS (lpoint) <= ZV)
16600 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16601
16602 unbind_to (count, Qnil);
16603 }
16604
16605
16606 /* Build the complete desired matrix of WINDOW with a window start
16607 buffer position POS.
16608
16609 Value is 1 if successful. It is zero if fonts were loaded during
16610 redisplay which makes re-adjusting glyph matrices necessary, and -1
16611 if point would appear in the scroll margins.
16612 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16613 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16614 set in FLAGS.) */
16615
16616 int
16617 try_window (Lisp_Object window, struct text_pos pos, int flags)
16618 {
16619 struct window *w = XWINDOW (window);
16620 struct it it;
16621 struct glyph_row *last_text_row = NULL;
16622 struct frame *f = XFRAME (w->frame);
16623 int frame_line_height = default_line_pixel_height (w);
16624
16625 /* Make POS the new window start. */
16626 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16627
16628 /* Mark cursor position as unknown. No overlay arrow seen. */
16629 w->cursor.vpos = -1;
16630 overlay_arrow_seen = 0;
16631
16632 /* Initialize iterator and info to start at POS. */
16633 start_display (&it, w, pos);
16634
16635 /* Display all lines of W. */
16636 while (it.current_y < it.last_visible_y)
16637 {
16638 if (display_line (&it))
16639 last_text_row = it.glyph_row - 1;
16640 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16641 return 0;
16642 }
16643
16644 /* Don't let the cursor end in the scroll margins. */
16645 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16646 && !MINI_WINDOW_P (w))
16647 {
16648 int this_scroll_margin;
16649 int window_total_lines
16650 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16651
16652 if (scroll_margin > 0)
16653 {
16654 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16655 this_scroll_margin *= frame_line_height;
16656 }
16657 else
16658 this_scroll_margin = 0;
16659
16660 if ((w->cursor.y >= 0 /* not vscrolled */
16661 && w->cursor.y < this_scroll_margin
16662 && CHARPOS (pos) > BEGV
16663 && IT_CHARPOS (it) < ZV)
16664 /* rms: considering make_cursor_line_fully_visible_p here
16665 seems to give wrong results. We don't want to recenter
16666 when the last line is partly visible, we want to allow
16667 that case to be handled in the usual way. */
16668 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16669 {
16670 w->cursor.vpos = -1;
16671 clear_glyph_matrix (w->desired_matrix);
16672 return -1;
16673 }
16674 }
16675
16676 /* If bottom moved off end of frame, change mode line percentage. */
16677 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16678 w->update_mode_line = 1;
16679
16680 /* Set window_end_pos to the offset of the last character displayed
16681 on the window from the end of current_buffer. Set
16682 window_end_vpos to its row number. */
16683 if (last_text_row)
16684 {
16685 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16686 adjust_window_ends (w, last_text_row, 0);
16687 eassert
16688 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16689 w->window_end_vpos)));
16690 }
16691 else
16692 {
16693 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16694 w->window_end_pos = Z - ZV;
16695 w->window_end_vpos = 0;
16696 }
16697
16698 /* But that is not valid info until redisplay finishes. */
16699 w->window_end_valid = 0;
16700 return 1;
16701 }
16702
16703
16704 \f
16705 /************************************************************************
16706 Window redisplay reusing current matrix when buffer has not changed
16707 ************************************************************************/
16708
16709 /* Try redisplay of window W showing an unchanged buffer with a
16710 different window start than the last time it was displayed by
16711 reusing its current matrix. Value is non-zero if successful.
16712 W->start is the new window start. */
16713
16714 static int
16715 try_window_reusing_current_matrix (struct window *w)
16716 {
16717 struct frame *f = XFRAME (w->frame);
16718 struct glyph_row *bottom_row;
16719 struct it it;
16720 struct run run;
16721 struct text_pos start, new_start;
16722 int nrows_scrolled, i;
16723 struct glyph_row *last_text_row;
16724 struct glyph_row *last_reused_text_row;
16725 struct glyph_row *start_row;
16726 int start_vpos, min_y, max_y;
16727
16728 #ifdef GLYPH_DEBUG
16729 if (inhibit_try_window_reusing)
16730 return 0;
16731 #endif
16732
16733 if (/* This function doesn't handle terminal frames. */
16734 !FRAME_WINDOW_P (f)
16735 /* Don't try to reuse the display if windows have been split
16736 or such. */
16737 || windows_or_buffers_changed
16738 || f->cursor_type_changed)
16739 return 0;
16740
16741 /* Can't do this if showing trailing whitespace. */
16742 if (!NILP (Vshow_trailing_whitespace))
16743 return 0;
16744
16745 /* If top-line visibility has changed, give up. */
16746 if (WINDOW_WANTS_HEADER_LINE_P (w)
16747 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16748 return 0;
16749
16750 /* Give up if old or new display is scrolled vertically. We could
16751 make this function handle this, but right now it doesn't. */
16752 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16753 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16754 return 0;
16755
16756 /* The variable new_start now holds the new window start. The old
16757 start `start' can be determined from the current matrix. */
16758 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16759 start = start_row->minpos;
16760 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16761
16762 /* Clear the desired matrix for the display below. */
16763 clear_glyph_matrix (w->desired_matrix);
16764
16765 if (CHARPOS (new_start) <= CHARPOS (start))
16766 {
16767 /* Don't use this method if the display starts with an ellipsis
16768 displayed for invisible text. It's not easy to handle that case
16769 below, and it's certainly not worth the effort since this is
16770 not a frequent case. */
16771 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16772 return 0;
16773
16774 IF_DEBUG (debug_method_add (w, "twu1"));
16775
16776 /* Display up to a row that can be reused. The variable
16777 last_text_row is set to the last row displayed that displays
16778 text. Note that it.vpos == 0 if or if not there is a
16779 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16780 start_display (&it, w, new_start);
16781 w->cursor.vpos = -1;
16782 last_text_row = last_reused_text_row = NULL;
16783
16784 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16785 {
16786 /* If we have reached into the characters in the START row,
16787 that means the line boundaries have changed. So we
16788 can't start copying with the row START. Maybe it will
16789 work to start copying with the following row. */
16790 while (IT_CHARPOS (it) > CHARPOS (start))
16791 {
16792 /* Advance to the next row as the "start". */
16793 start_row++;
16794 start = start_row->minpos;
16795 /* If there are no more rows to try, or just one, give up. */
16796 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16797 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16798 || CHARPOS (start) == ZV)
16799 {
16800 clear_glyph_matrix (w->desired_matrix);
16801 return 0;
16802 }
16803
16804 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16805 }
16806 /* If we have reached alignment, we can copy the rest of the
16807 rows. */
16808 if (IT_CHARPOS (it) == CHARPOS (start)
16809 /* Don't accept "alignment" inside a display vector,
16810 since start_row could have started in the middle of
16811 that same display vector (thus their character
16812 positions match), and we have no way of telling if
16813 that is the case. */
16814 && it.current.dpvec_index < 0)
16815 break;
16816
16817 if (display_line (&it))
16818 last_text_row = it.glyph_row - 1;
16819
16820 }
16821
16822 /* A value of current_y < last_visible_y means that we stopped
16823 at the previous window start, which in turn means that we
16824 have at least one reusable row. */
16825 if (it.current_y < it.last_visible_y)
16826 {
16827 struct glyph_row *row;
16828
16829 /* IT.vpos always starts from 0; it counts text lines. */
16830 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16831
16832 /* Find PT if not already found in the lines displayed. */
16833 if (w->cursor.vpos < 0)
16834 {
16835 int dy = it.current_y - start_row->y;
16836
16837 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16838 row = row_containing_pos (w, PT, row, NULL, dy);
16839 if (row)
16840 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16841 dy, nrows_scrolled);
16842 else
16843 {
16844 clear_glyph_matrix (w->desired_matrix);
16845 return 0;
16846 }
16847 }
16848
16849 /* Scroll the display. Do it before the current matrix is
16850 changed. The problem here is that update has not yet
16851 run, i.e. part of the current matrix is not up to date.
16852 scroll_run_hook will clear the cursor, and use the
16853 current matrix to get the height of the row the cursor is
16854 in. */
16855 run.current_y = start_row->y;
16856 run.desired_y = it.current_y;
16857 run.height = it.last_visible_y - it.current_y;
16858
16859 if (run.height > 0 && run.current_y != run.desired_y)
16860 {
16861 update_begin (f);
16862 FRAME_RIF (f)->update_window_begin_hook (w);
16863 FRAME_RIF (f)->clear_window_mouse_face (w);
16864 FRAME_RIF (f)->scroll_run_hook (w, &run);
16865 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16866 update_end (f);
16867 }
16868
16869 /* Shift current matrix down by nrows_scrolled lines. */
16870 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16871 rotate_matrix (w->current_matrix,
16872 start_vpos,
16873 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16874 nrows_scrolled);
16875
16876 /* Disable lines that must be updated. */
16877 for (i = 0; i < nrows_scrolled; ++i)
16878 (start_row + i)->enabled_p = false;
16879
16880 /* Re-compute Y positions. */
16881 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16882 max_y = it.last_visible_y;
16883 for (row = start_row + nrows_scrolled;
16884 row < bottom_row;
16885 ++row)
16886 {
16887 row->y = it.current_y;
16888 row->visible_height = row->height;
16889
16890 if (row->y < min_y)
16891 row->visible_height -= min_y - row->y;
16892 if (row->y + row->height > max_y)
16893 row->visible_height -= row->y + row->height - max_y;
16894 if (row->fringe_bitmap_periodic_p)
16895 row->redraw_fringe_bitmaps_p = 1;
16896
16897 it.current_y += row->height;
16898
16899 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16900 last_reused_text_row = row;
16901 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16902 break;
16903 }
16904
16905 /* Disable lines in the current matrix which are now
16906 below the window. */
16907 for (++row; row < bottom_row; ++row)
16908 row->enabled_p = row->mode_line_p = 0;
16909 }
16910
16911 /* Update window_end_pos etc.; last_reused_text_row is the last
16912 reused row from the current matrix containing text, if any.
16913 The value of last_text_row is the last displayed line
16914 containing text. */
16915 if (last_reused_text_row)
16916 adjust_window_ends (w, last_reused_text_row, 1);
16917 else if (last_text_row)
16918 adjust_window_ends (w, last_text_row, 0);
16919 else
16920 {
16921 /* This window must be completely empty. */
16922 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16923 w->window_end_pos = Z - ZV;
16924 w->window_end_vpos = 0;
16925 }
16926 w->window_end_valid = 0;
16927
16928 /* Update hint: don't try scrolling again in update_window. */
16929 w->desired_matrix->no_scrolling_p = 1;
16930
16931 #ifdef GLYPH_DEBUG
16932 debug_method_add (w, "try_window_reusing_current_matrix 1");
16933 #endif
16934 return 1;
16935 }
16936 else if (CHARPOS (new_start) > CHARPOS (start))
16937 {
16938 struct glyph_row *pt_row, *row;
16939 struct glyph_row *first_reusable_row;
16940 struct glyph_row *first_row_to_display;
16941 int dy;
16942 int yb = window_text_bottom_y (w);
16943
16944 /* Find the row starting at new_start, if there is one. Don't
16945 reuse a partially visible line at the end. */
16946 first_reusable_row = start_row;
16947 while (first_reusable_row->enabled_p
16948 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16949 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16950 < CHARPOS (new_start)))
16951 ++first_reusable_row;
16952
16953 /* Give up if there is no row to reuse. */
16954 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16955 || !first_reusable_row->enabled_p
16956 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16957 != CHARPOS (new_start)))
16958 return 0;
16959
16960 /* We can reuse fully visible rows beginning with
16961 first_reusable_row to the end of the window. Set
16962 first_row_to_display to the first row that cannot be reused.
16963 Set pt_row to the row containing point, if there is any. */
16964 pt_row = NULL;
16965 for (first_row_to_display = first_reusable_row;
16966 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16967 ++first_row_to_display)
16968 {
16969 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16970 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16971 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16972 && first_row_to_display->ends_at_zv_p
16973 && pt_row == NULL)))
16974 pt_row = first_row_to_display;
16975 }
16976
16977 /* Start displaying at the start of first_row_to_display. */
16978 eassert (first_row_to_display->y < yb);
16979 init_to_row_start (&it, w, first_row_to_display);
16980
16981 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16982 - start_vpos);
16983 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16984 - nrows_scrolled);
16985 it.current_y = (first_row_to_display->y - first_reusable_row->y
16986 + WINDOW_HEADER_LINE_HEIGHT (w));
16987
16988 /* Display lines beginning with first_row_to_display in the
16989 desired matrix. Set last_text_row to the last row displayed
16990 that displays text. */
16991 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16992 if (pt_row == NULL)
16993 w->cursor.vpos = -1;
16994 last_text_row = NULL;
16995 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16996 if (display_line (&it))
16997 last_text_row = it.glyph_row - 1;
16998
16999 /* If point is in a reused row, adjust y and vpos of the cursor
17000 position. */
17001 if (pt_row)
17002 {
17003 w->cursor.vpos -= nrows_scrolled;
17004 w->cursor.y -= first_reusable_row->y - start_row->y;
17005 }
17006
17007 /* Give up if point isn't in a row displayed or reused. (This
17008 also handles the case where w->cursor.vpos < nrows_scrolled
17009 after the calls to display_line, which can happen with scroll
17010 margins. See bug#1295.) */
17011 if (w->cursor.vpos < 0)
17012 {
17013 clear_glyph_matrix (w->desired_matrix);
17014 return 0;
17015 }
17016
17017 /* Scroll the display. */
17018 run.current_y = first_reusable_row->y;
17019 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17020 run.height = it.last_visible_y - run.current_y;
17021 dy = run.current_y - run.desired_y;
17022
17023 if (run.height)
17024 {
17025 update_begin (f);
17026 FRAME_RIF (f)->update_window_begin_hook (w);
17027 FRAME_RIF (f)->clear_window_mouse_face (w);
17028 FRAME_RIF (f)->scroll_run_hook (w, &run);
17029 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17030 update_end (f);
17031 }
17032
17033 /* Adjust Y positions of reused rows. */
17034 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17035 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17036 max_y = it.last_visible_y;
17037 for (row = first_reusable_row; row < first_row_to_display; ++row)
17038 {
17039 row->y -= dy;
17040 row->visible_height = row->height;
17041 if (row->y < min_y)
17042 row->visible_height -= min_y - row->y;
17043 if (row->y + row->height > max_y)
17044 row->visible_height -= row->y + row->height - max_y;
17045 if (row->fringe_bitmap_periodic_p)
17046 row->redraw_fringe_bitmaps_p = 1;
17047 }
17048
17049 /* Scroll the current matrix. */
17050 eassert (nrows_scrolled > 0);
17051 rotate_matrix (w->current_matrix,
17052 start_vpos,
17053 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17054 -nrows_scrolled);
17055
17056 /* Disable rows not reused. */
17057 for (row -= nrows_scrolled; row < bottom_row; ++row)
17058 row->enabled_p = false;
17059
17060 /* Point may have moved to a different line, so we cannot assume that
17061 the previous cursor position is valid; locate the correct row. */
17062 if (pt_row)
17063 {
17064 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17065 row < bottom_row
17066 && PT >= MATRIX_ROW_END_CHARPOS (row)
17067 && !row->ends_at_zv_p;
17068 row++)
17069 {
17070 w->cursor.vpos++;
17071 w->cursor.y = row->y;
17072 }
17073 if (row < bottom_row)
17074 {
17075 /* Can't simply scan the row for point with
17076 bidi-reordered glyph rows. Let set_cursor_from_row
17077 figure out where to put the cursor, and if it fails,
17078 give up. */
17079 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17080 {
17081 if (!set_cursor_from_row (w, row, w->current_matrix,
17082 0, 0, 0, 0))
17083 {
17084 clear_glyph_matrix (w->desired_matrix);
17085 return 0;
17086 }
17087 }
17088 else
17089 {
17090 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17091 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17092
17093 for (; glyph < end
17094 && (!BUFFERP (glyph->object)
17095 || glyph->charpos < PT);
17096 glyph++)
17097 {
17098 w->cursor.hpos++;
17099 w->cursor.x += glyph->pixel_width;
17100 }
17101 }
17102 }
17103 }
17104
17105 /* Adjust window end. A null value of last_text_row means that
17106 the window end is in reused rows which in turn means that
17107 only its vpos can have changed. */
17108 if (last_text_row)
17109 adjust_window_ends (w, last_text_row, 0);
17110 else
17111 w->window_end_vpos -= nrows_scrolled;
17112
17113 w->window_end_valid = 0;
17114 w->desired_matrix->no_scrolling_p = 1;
17115
17116 #ifdef GLYPH_DEBUG
17117 debug_method_add (w, "try_window_reusing_current_matrix 2");
17118 #endif
17119 return 1;
17120 }
17121
17122 return 0;
17123 }
17124
17125
17126 \f
17127 /************************************************************************
17128 Window redisplay reusing current matrix when buffer has changed
17129 ************************************************************************/
17130
17131 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17132 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17133 ptrdiff_t *, ptrdiff_t *);
17134 static struct glyph_row *
17135 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17136 struct glyph_row *);
17137
17138
17139 /* Return the last row in MATRIX displaying text. If row START is
17140 non-null, start searching with that row. IT gives the dimensions
17141 of the display. Value is null if matrix is empty; otherwise it is
17142 a pointer to the row found. */
17143
17144 static struct glyph_row *
17145 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17146 struct glyph_row *start)
17147 {
17148 struct glyph_row *row, *row_found;
17149
17150 /* Set row_found to the last row in IT->w's current matrix
17151 displaying text. The loop looks funny but think of partially
17152 visible lines. */
17153 row_found = NULL;
17154 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17155 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17156 {
17157 eassert (row->enabled_p);
17158 row_found = row;
17159 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17160 break;
17161 ++row;
17162 }
17163
17164 return row_found;
17165 }
17166
17167
17168 /* Return the last row in the current matrix of W that is not affected
17169 by changes at the start of current_buffer that occurred since W's
17170 current matrix was built. Value is null if no such row exists.
17171
17172 BEG_UNCHANGED us the number of characters unchanged at the start of
17173 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17174 first changed character in current_buffer. Characters at positions <
17175 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17176 when the current matrix was built. */
17177
17178 static struct glyph_row *
17179 find_last_unchanged_at_beg_row (struct window *w)
17180 {
17181 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17182 struct glyph_row *row;
17183 struct glyph_row *row_found = NULL;
17184 int yb = window_text_bottom_y (w);
17185
17186 /* Find the last row displaying unchanged text. */
17187 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17188 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17189 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17190 ++row)
17191 {
17192 if (/* If row ends before first_changed_pos, it is unchanged,
17193 except in some case. */
17194 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17195 /* When row ends in ZV and we write at ZV it is not
17196 unchanged. */
17197 && !row->ends_at_zv_p
17198 /* When first_changed_pos is the end of a continued line,
17199 row is not unchanged because it may be no longer
17200 continued. */
17201 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17202 && (row->continued_p
17203 || row->exact_window_width_line_p))
17204 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17205 needs to be recomputed, so don't consider this row as
17206 unchanged. This happens when the last line was
17207 bidi-reordered and was killed immediately before this
17208 redisplay cycle. In that case, ROW->end stores the
17209 buffer position of the first visual-order character of
17210 the killed text, which is now beyond ZV. */
17211 && CHARPOS (row->end.pos) <= ZV)
17212 row_found = row;
17213
17214 /* Stop if last visible row. */
17215 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17216 break;
17217 }
17218
17219 return row_found;
17220 }
17221
17222
17223 /* Find the first glyph row in the current matrix of W that is not
17224 affected by changes at the end of current_buffer since the
17225 time W's current matrix was built.
17226
17227 Return in *DELTA the number of chars by which buffer positions in
17228 unchanged text at the end of current_buffer must be adjusted.
17229
17230 Return in *DELTA_BYTES the corresponding number of bytes.
17231
17232 Value is null if no such row exists, i.e. all rows are affected by
17233 changes. */
17234
17235 static struct glyph_row *
17236 find_first_unchanged_at_end_row (struct window *w,
17237 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17238 {
17239 struct glyph_row *row;
17240 struct glyph_row *row_found = NULL;
17241
17242 *delta = *delta_bytes = 0;
17243
17244 /* Display must not have been paused, otherwise the current matrix
17245 is not up to date. */
17246 eassert (w->window_end_valid);
17247
17248 /* A value of window_end_pos >= END_UNCHANGED means that the window
17249 end is in the range of changed text. If so, there is no
17250 unchanged row at the end of W's current matrix. */
17251 if (w->window_end_pos >= END_UNCHANGED)
17252 return NULL;
17253
17254 /* Set row to the last row in W's current matrix displaying text. */
17255 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17256
17257 /* If matrix is entirely empty, no unchanged row exists. */
17258 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17259 {
17260 /* The value of row is the last glyph row in the matrix having a
17261 meaningful buffer position in it. The end position of row
17262 corresponds to window_end_pos. This allows us to translate
17263 buffer positions in the current matrix to current buffer
17264 positions for characters not in changed text. */
17265 ptrdiff_t Z_old =
17266 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17267 ptrdiff_t Z_BYTE_old =
17268 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17269 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17270 struct glyph_row *first_text_row
17271 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17272
17273 *delta = Z - Z_old;
17274 *delta_bytes = Z_BYTE - Z_BYTE_old;
17275
17276 /* Set last_unchanged_pos to the buffer position of the last
17277 character in the buffer that has not been changed. Z is the
17278 index + 1 of the last character in current_buffer, i.e. by
17279 subtracting END_UNCHANGED we get the index of the last
17280 unchanged character, and we have to add BEG to get its buffer
17281 position. */
17282 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17283 last_unchanged_pos_old = last_unchanged_pos - *delta;
17284
17285 /* Search backward from ROW for a row displaying a line that
17286 starts at a minimum position >= last_unchanged_pos_old. */
17287 for (; row > first_text_row; --row)
17288 {
17289 /* This used to abort, but it can happen.
17290 It is ok to just stop the search instead here. KFS. */
17291 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17292 break;
17293
17294 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17295 row_found = row;
17296 }
17297 }
17298
17299 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17300
17301 return row_found;
17302 }
17303
17304
17305 /* Make sure that glyph rows in the current matrix of window W
17306 reference the same glyph memory as corresponding rows in the
17307 frame's frame matrix. This function is called after scrolling W's
17308 current matrix on a terminal frame in try_window_id and
17309 try_window_reusing_current_matrix. */
17310
17311 static void
17312 sync_frame_with_window_matrix_rows (struct window *w)
17313 {
17314 struct frame *f = XFRAME (w->frame);
17315 struct glyph_row *window_row, *window_row_end, *frame_row;
17316
17317 /* Preconditions: W must be a leaf window and full-width. Its frame
17318 must have a frame matrix. */
17319 eassert (BUFFERP (w->contents));
17320 eassert (WINDOW_FULL_WIDTH_P (w));
17321 eassert (!FRAME_WINDOW_P (f));
17322
17323 /* If W is a full-width window, glyph pointers in W's current matrix
17324 have, by definition, to be the same as glyph pointers in the
17325 corresponding frame matrix. Note that frame matrices have no
17326 marginal areas (see build_frame_matrix). */
17327 window_row = w->current_matrix->rows;
17328 window_row_end = window_row + w->current_matrix->nrows;
17329 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17330 while (window_row < window_row_end)
17331 {
17332 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17333 struct glyph *end = window_row->glyphs[LAST_AREA];
17334
17335 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17336 frame_row->glyphs[TEXT_AREA] = start;
17337 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17338 frame_row->glyphs[LAST_AREA] = end;
17339
17340 /* Disable frame rows whose corresponding window rows have
17341 been disabled in try_window_id. */
17342 if (!window_row->enabled_p)
17343 frame_row->enabled_p = false;
17344
17345 ++window_row, ++frame_row;
17346 }
17347 }
17348
17349
17350 /* Find the glyph row in window W containing CHARPOS. Consider all
17351 rows between START and END (not inclusive). END null means search
17352 all rows to the end of the display area of W. Value is the row
17353 containing CHARPOS or null. */
17354
17355 struct glyph_row *
17356 row_containing_pos (struct window *w, ptrdiff_t charpos,
17357 struct glyph_row *start, struct glyph_row *end, int dy)
17358 {
17359 struct glyph_row *row = start;
17360 struct glyph_row *best_row = NULL;
17361 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17362 int last_y;
17363
17364 /* If we happen to start on a header-line, skip that. */
17365 if (row->mode_line_p)
17366 ++row;
17367
17368 if ((end && row >= end) || !row->enabled_p)
17369 return NULL;
17370
17371 last_y = window_text_bottom_y (w) - dy;
17372
17373 while (1)
17374 {
17375 /* Give up if we have gone too far. */
17376 if (end && row >= end)
17377 return NULL;
17378 /* This formerly returned if they were equal.
17379 I think that both quantities are of a "last plus one" type;
17380 if so, when they are equal, the row is within the screen. -- rms. */
17381 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17382 return NULL;
17383
17384 /* If it is in this row, return this row. */
17385 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17386 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17387 /* The end position of a row equals the start
17388 position of the next row. If CHARPOS is there, we
17389 would rather consider it displayed in the next
17390 line, except when this line ends in ZV. */
17391 && !row_for_charpos_p (row, charpos)))
17392 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17393 {
17394 struct glyph *g;
17395
17396 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17397 || (!best_row && !row->continued_p))
17398 return row;
17399 /* In bidi-reordered rows, there could be several rows whose
17400 edges surround CHARPOS, all of these rows belonging to
17401 the same continued line. We need to find the row which
17402 fits CHARPOS the best. */
17403 for (g = row->glyphs[TEXT_AREA];
17404 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17405 g++)
17406 {
17407 if (!STRINGP (g->object))
17408 {
17409 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17410 {
17411 mindif = eabs (g->charpos - charpos);
17412 best_row = row;
17413 /* Exact match always wins. */
17414 if (mindif == 0)
17415 return best_row;
17416 }
17417 }
17418 }
17419 }
17420 else if (best_row && !row->continued_p)
17421 return best_row;
17422 ++row;
17423 }
17424 }
17425
17426
17427 /* Try to redisplay window W by reusing its existing display. W's
17428 current matrix must be up to date when this function is called,
17429 i.e. window_end_valid must be nonzero.
17430
17431 Value is
17432
17433 >= 1 if successful, i.e. display has been updated
17434 specifically:
17435 1 means the changes were in front of a newline that precedes
17436 the window start, and the whole current matrix was reused
17437 2 means the changes were after the last position displayed
17438 in the window, and the whole current matrix was reused
17439 3 means portions of the current matrix were reused, while
17440 some of the screen lines were redrawn
17441 -1 if redisplay with same window start is known not to succeed
17442 0 if otherwise unsuccessful
17443
17444 The following steps are performed:
17445
17446 1. Find the last row in the current matrix of W that is not
17447 affected by changes at the start of current_buffer. If no such row
17448 is found, give up.
17449
17450 2. Find the first row in W's current matrix that is not affected by
17451 changes at the end of current_buffer. Maybe there is no such row.
17452
17453 3. Display lines beginning with the row + 1 found in step 1 to the
17454 row found in step 2 or, if step 2 didn't find a row, to the end of
17455 the window.
17456
17457 4. If cursor is not known to appear on the window, give up.
17458
17459 5. If display stopped at the row found in step 2, scroll the
17460 display and current matrix as needed.
17461
17462 6. Maybe display some lines at the end of W, if we must. This can
17463 happen under various circumstances, like a partially visible line
17464 becoming fully visible, or because newly displayed lines are displayed
17465 in smaller font sizes.
17466
17467 7. Update W's window end information. */
17468
17469 static int
17470 try_window_id (struct window *w)
17471 {
17472 struct frame *f = XFRAME (w->frame);
17473 struct glyph_matrix *current_matrix = w->current_matrix;
17474 struct glyph_matrix *desired_matrix = w->desired_matrix;
17475 struct glyph_row *last_unchanged_at_beg_row;
17476 struct glyph_row *first_unchanged_at_end_row;
17477 struct glyph_row *row;
17478 struct glyph_row *bottom_row;
17479 int bottom_vpos;
17480 struct it it;
17481 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17482 int dvpos, dy;
17483 struct text_pos start_pos;
17484 struct run run;
17485 int first_unchanged_at_end_vpos = 0;
17486 struct glyph_row *last_text_row, *last_text_row_at_end;
17487 struct text_pos start;
17488 ptrdiff_t first_changed_charpos, last_changed_charpos;
17489
17490 #ifdef GLYPH_DEBUG
17491 if (inhibit_try_window_id)
17492 return 0;
17493 #endif
17494
17495 /* This is handy for debugging. */
17496 #if 0
17497 #define GIVE_UP(X) \
17498 do { \
17499 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17500 return 0; \
17501 } while (0)
17502 #else
17503 #define GIVE_UP(X) return 0
17504 #endif
17505
17506 SET_TEXT_POS_FROM_MARKER (start, w->start);
17507
17508 /* Don't use this for mini-windows because these can show
17509 messages and mini-buffers, and we don't handle that here. */
17510 if (MINI_WINDOW_P (w))
17511 GIVE_UP (1);
17512
17513 /* This flag is used to prevent redisplay optimizations. */
17514 if (windows_or_buffers_changed || f->cursor_type_changed)
17515 GIVE_UP (2);
17516
17517 /* This function's optimizations cannot be used if overlays have
17518 changed in the buffer displayed by the window, so give up if they
17519 have. */
17520 if (w->last_overlay_modified != OVERLAY_MODIFF)
17521 GIVE_UP (21);
17522
17523 /* Verify that narrowing has not changed.
17524 Also verify that we were not told to prevent redisplay optimizations.
17525 It would be nice to further
17526 reduce the number of cases where this prevents try_window_id. */
17527 if (current_buffer->clip_changed
17528 || current_buffer->prevent_redisplay_optimizations_p)
17529 GIVE_UP (3);
17530
17531 /* Window must either use window-based redisplay or be full width. */
17532 if (!FRAME_WINDOW_P (f)
17533 && (!FRAME_LINE_INS_DEL_OK (f)
17534 || !WINDOW_FULL_WIDTH_P (w)))
17535 GIVE_UP (4);
17536
17537 /* Give up if point is known NOT to appear in W. */
17538 if (PT < CHARPOS (start))
17539 GIVE_UP (5);
17540
17541 /* Another way to prevent redisplay optimizations. */
17542 if (w->last_modified == 0)
17543 GIVE_UP (6);
17544
17545 /* Verify that window is not hscrolled. */
17546 if (w->hscroll != 0)
17547 GIVE_UP (7);
17548
17549 /* Verify that display wasn't paused. */
17550 if (!w->window_end_valid)
17551 GIVE_UP (8);
17552
17553 /* Likewise if highlighting trailing whitespace. */
17554 if (!NILP (Vshow_trailing_whitespace))
17555 GIVE_UP (11);
17556
17557 /* Can't use this if overlay arrow position and/or string have
17558 changed. */
17559 if (overlay_arrows_changed_p ())
17560 GIVE_UP (12);
17561
17562 /* When word-wrap is on, adding a space to the first word of a
17563 wrapped line can change the wrap position, altering the line
17564 above it. It might be worthwhile to handle this more
17565 intelligently, but for now just redisplay from scratch. */
17566 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17567 GIVE_UP (21);
17568
17569 /* Under bidi reordering, adding or deleting a character in the
17570 beginning of a paragraph, before the first strong directional
17571 character, can change the base direction of the paragraph (unless
17572 the buffer specifies a fixed paragraph direction), which will
17573 require to redisplay the whole paragraph. It might be worthwhile
17574 to find the paragraph limits and widen the range of redisplayed
17575 lines to that, but for now just give up this optimization and
17576 redisplay from scratch. */
17577 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17578 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17579 GIVE_UP (22);
17580
17581 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17582 only if buffer has really changed. The reason is that the gap is
17583 initially at Z for freshly visited files. The code below would
17584 set end_unchanged to 0 in that case. */
17585 if (MODIFF > SAVE_MODIFF
17586 /* This seems to happen sometimes after saving a buffer. */
17587 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17588 {
17589 if (GPT - BEG < BEG_UNCHANGED)
17590 BEG_UNCHANGED = GPT - BEG;
17591 if (Z - GPT < END_UNCHANGED)
17592 END_UNCHANGED = Z - GPT;
17593 }
17594
17595 /* The position of the first and last character that has been changed. */
17596 first_changed_charpos = BEG + BEG_UNCHANGED;
17597 last_changed_charpos = Z - END_UNCHANGED;
17598
17599 /* If window starts after a line end, and the last change is in
17600 front of that newline, then changes don't affect the display.
17601 This case happens with stealth-fontification. Note that although
17602 the display is unchanged, glyph positions in the matrix have to
17603 be adjusted, of course. */
17604 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17605 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17606 && ((last_changed_charpos < CHARPOS (start)
17607 && CHARPOS (start) == BEGV)
17608 || (last_changed_charpos < CHARPOS (start) - 1
17609 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17610 {
17611 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17612 struct glyph_row *r0;
17613
17614 /* Compute how many chars/bytes have been added to or removed
17615 from the buffer. */
17616 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17617 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17618 Z_delta = Z - Z_old;
17619 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17620
17621 /* Give up if PT is not in the window. Note that it already has
17622 been checked at the start of try_window_id that PT is not in
17623 front of the window start. */
17624 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17625 GIVE_UP (13);
17626
17627 /* If window start is unchanged, we can reuse the whole matrix
17628 as is, after adjusting glyph positions. No need to compute
17629 the window end again, since its offset from Z hasn't changed. */
17630 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17631 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17632 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17633 /* PT must not be in a partially visible line. */
17634 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17635 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17636 {
17637 /* Adjust positions in the glyph matrix. */
17638 if (Z_delta || Z_delta_bytes)
17639 {
17640 struct glyph_row *r1
17641 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17642 increment_matrix_positions (w->current_matrix,
17643 MATRIX_ROW_VPOS (r0, current_matrix),
17644 MATRIX_ROW_VPOS (r1, current_matrix),
17645 Z_delta, Z_delta_bytes);
17646 }
17647
17648 /* Set the cursor. */
17649 row = row_containing_pos (w, PT, r0, NULL, 0);
17650 if (row)
17651 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17652 return 1;
17653 }
17654 }
17655
17656 /* Handle the case that changes are all below what is displayed in
17657 the window, and that PT is in the window. This shortcut cannot
17658 be taken if ZV is visible in the window, and text has been added
17659 there that is visible in the window. */
17660 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17661 /* ZV is not visible in the window, or there are no
17662 changes at ZV, actually. */
17663 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17664 || first_changed_charpos == last_changed_charpos))
17665 {
17666 struct glyph_row *r0;
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))
17672 GIVE_UP (14);
17673
17674 /* If window start is unchanged, we can reuse the whole matrix
17675 as is, without changing glyph positions since no text has
17676 been added/removed in front of the window end. */
17677 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17678 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17679 /* PT must not be in a partially visible line. */
17680 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17681 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17682 {
17683 /* We have to compute the window end anew since text
17684 could have been added/removed after it. */
17685 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17686 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17687
17688 /* Set the cursor. */
17689 row = row_containing_pos (w, PT, r0, NULL, 0);
17690 if (row)
17691 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17692 return 2;
17693 }
17694 }
17695
17696 /* Give up if window start is in the changed area.
17697
17698 The condition used to read
17699
17700 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17701
17702 but why that was tested escapes me at the moment. */
17703 if (CHARPOS (start) >= first_changed_charpos
17704 && CHARPOS (start) <= last_changed_charpos)
17705 GIVE_UP (15);
17706
17707 /* Check that window start agrees with the start of the first glyph
17708 row in its current matrix. Check this after we know the window
17709 start is not in changed text, otherwise positions would not be
17710 comparable. */
17711 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17712 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17713 GIVE_UP (16);
17714
17715 /* Give up if the window ends in strings. Overlay strings
17716 at the end are difficult to handle, so don't try. */
17717 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17718 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17719 GIVE_UP (20);
17720
17721 /* Compute the position at which we have to start displaying new
17722 lines. Some of the lines at the top of the window might be
17723 reusable because they are not displaying changed text. Find the
17724 last row in W's current matrix not affected by changes at the
17725 start of current_buffer. Value is null if changes start in the
17726 first line of window. */
17727 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17728 if (last_unchanged_at_beg_row)
17729 {
17730 /* Avoid starting to display in the middle of a character, a TAB
17731 for instance. This is easier than to set up the iterator
17732 exactly, and it's not a frequent case, so the additional
17733 effort wouldn't really pay off. */
17734 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17735 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17736 && last_unchanged_at_beg_row > w->current_matrix->rows)
17737 --last_unchanged_at_beg_row;
17738
17739 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17740 GIVE_UP (17);
17741
17742 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17743 GIVE_UP (18);
17744 start_pos = it.current.pos;
17745
17746 /* Start displaying new lines in the desired matrix at the same
17747 vpos we would use in the current matrix, i.e. below
17748 last_unchanged_at_beg_row. */
17749 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17750 current_matrix);
17751 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17752 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17753
17754 eassert (it.hpos == 0 && it.current_x == 0);
17755 }
17756 else
17757 {
17758 /* There are no reusable lines at the start of the window.
17759 Start displaying in the first text line. */
17760 start_display (&it, w, start);
17761 it.vpos = it.first_vpos;
17762 start_pos = it.current.pos;
17763 }
17764
17765 /* Find the first row that is not affected by changes at the end of
17766 the buffer. Value will be null if there is no unchanged row, in
17767 which case we must redisplay to the end of the window. delta
17768 will be set to the value by which buffer positions beginning with
17769 first_unchanged_at_end_row have to be adjusted due to text
17770 changes. */
17771 first_unchanged_at_end_row
17772 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17773 IF_DEBUG (debug_delta = delta);
17774 IF_DEBUG (debug_delta_bytes = delta_bytes);
17775
17776 /* Set stop_pos to the buffer position up to which we will have to
17777 display new lines. If first_unchanged_at_end_row != NULL, this
17778 is the buffer position of the start of the line displayed in that
17779 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17780 that we don't stop at a buffer position. */
17781 stop_pos = 0;
17782 if (first_unchanged_at_end_row)
17783 {
17784 eassert (last_unchanged_at_beg_row == NULL
17785 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17786
17787 /* If this is a continuation line, move forward to the next one
17788 that isn't. Changes in lines above affect this line.
17789 Caution: this may move first_unchanged_at_end_row to a row
17790 not displaying text. */
17791 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17792 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17793 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17794 < it.last_visible_y))
17795 ++first_unchanged_at_end_row;
17796
17797 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17798 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17799 >= it.last_visible_y))
17800 first_unchanged_at_end_row = NULL;
17801 else
17802 {
17803 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17804 + delta);
17805 first_unchanged_at_end_vpos
17806 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17807 eassert (stop_pos >= Z - END_UNCHANGED);
17808 }
17809 }
17810 else if (last_unchanged_at_beg_row == NULL)
17811 GIVE_UP (19);
17812
17813
17814 #ifdef GLYPH_DEBUG
17815
17816 /* Either there is no unchanged row at the end, or the one we have
17817 now displays text. This is a necessary condition for the window
17818 end pos calculation at the end of this function. */
17819 eassert (first_unchanged_at_end_row == NULL
17820 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17821
17822 debug_last_unchanged_at_beg_vpos
17823 = (last_unchanged_at_beg_row
17824 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17825 : -1);
17826 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17827
17828 #endif /* GLYPH_DEBUG */
17829
17830
17831 /* Display new lines. Set last_text_row to the last new line
17832 displayed which has text on it, i.e. might end up as being the
17833 line where the window_end_vpos is. */
17834 w->cursor.vpos = -1;
17835 last_text_row = NULL;
17836 overlay_arrow_seen = 0;
17837 while (it.current_y < it.last_visible_y
17838 && !f->fonts_changed
17839 && (first_unchanged_at_end_row == NULL
17840 || IT_CHARPOS (it) < stop_pos))
17841 {
17842 if (display_line (&it))
17843 last_text_row = it.glyph_row - 1;
17844 }
17845
17846 if (f->fonts_changed)
17847 return -1;
17848
17849
17850 /* Compute differences in buffer positions, y-positions etc. for
17851 lines reused at the bottom of the window. Compute what we can
17852 scroll. */
17853 if (first_unchanged_at_end_row
17854 /* No lines reused because we displayed everything up to the
17855 bottom of the window. */
17856 && it.current_y < it.last_visible_y)
17857 {
17858 dvpos = (it.vpos
17859 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17860 current_matrix));
17861 dy = it.current_y - first_unchanged_at_end_row->y;
17862 run.current_y = first_unchanged_at_end_row->y;
17863 run.desired_y = run.current_y + dy;
17864 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17865 }
17866 else
17867 {
17868 delta = delta_bytes = dvpos = dy
17869 = run.current_y = run.desired_y = run.height = 0;
17870 first_unchanged_at_end_row = NULL;
17871 }
17872 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17873
17874
17875 /* Find the cursor if not already found. We have to decide whether
17876 PT will appear on this window (it sometimes doesn't, but this is
17877 not a very frequent case.) This decision has to be made before
17878 the current matrix is altered. A value of cursor.vpos < 0 means
17879 that PT is either in one of the lines beginning at
17880 first_unchanged_at_end_row or below the window. Don't care for
17881 lines that might be displayed later at the window end; as
17882 mentioned, this is not a frequent case. */
17883 if (w->cursor.vpos < 0)
17884 {
17885 /* Cursor in unchanged rows at the top? */
17886 if (PT < CHARPOS (start_pos)
17887 && last_unchanged_at_beg_row)
17888 {
17889 row = row_containing_pos (w, PT,
17890 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17891 last_unchanged_at_beg_row + 1, 0);
17892 if (row)
17893 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17894 }
17895
17896 /* Start from first_unchanged_at_end_row looking for PT. */
17897 else if (first_unchanged_at_end_row)
17898 {
17899 row = row_containing_pos (w, PT - delta,
17900 first_unchanged_at_end_row, NULL, 0);
17901 if (row)
17902 set_cursor_from_row (w, row, w->current_matrix, delta,
17903 delta_bytes, dy, dvpos);
17904 }
17905
17906 /* Give up if cursor was not found. */
17907 if (w->cursor.vpos < 0)
17908 {
17909 clear_glyph_matrix (w->desired_matrix);
17910 return -1;
17911 }
17912 }
17913
17914 /* Don't let the cursor end in the scroll margins. */
17915 {
17916 int this_scroll_margin, cursor_height;
17917 int frame_line_height = default_line_pixel_height (w);
17918 int window_total_lines
17919 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17920
17921 this_scroll_margin =
17922 max (0, min (scroll_margin, window_total_lines / 4));
17923 this_scroll_margin *= frame_line_height;
17924 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17925
17926 if ((w->cursor.y < this_scroll_margin
17927 && CHARPOS (start) > BEGV)
17928 /* Old redisplay didn't take scroll margin into account at the bottom,
17929 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17930 || (w->cursor.y + (make_cursor_line_fully_visible_p
17931 ? cursor_height + this_scroll_margin
17932 : 1)) > it.last_visible_y)
17933 {
17934 w->cursor.vpos = -1;
17935 clear_glyph_matrix (w->desired_matrix);
17936 return -1;
17937 }
17938 }
17939
17940 /* Scroll the display. Do it before changing the current matrix so
17941 that xterm.c doesn't get confused about where the cursor glyph is
17942 found. */
17943 if (dy && run.height)
17944 {
17945 update_begin (f);
17946
17947 if (FRAME_WINDOW_P (f))
17948 {
17949 FRAME_RIF (f)->update_window_begin_hook (w);
17950 FRAME_RIF (f)->clear_window_mouse_face (w);
17951 FRAME_RIF (f)->scroll_run_hook (w, &run);
17952 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17953 }
17954 else
17955 {
17956 /* Terminal frame. In this case, dvpos gives the number of
17957 lines to scroll by; dvpos < 0 means scroll up. */
17958 int from_vpos
17959 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17960 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17961 int end = (WINDOW_TOP_EDGE_LINE (w)
17962 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17963 + window_internal_height (w));
17964
17965 #if defined (HAVE_GPM) || defined (MSDOS)
17966 x_clear_window_mouse_face (w);
17967 #endif
17968 /* Perform the operation on the screen. */
17969 if (dvpos > 0)
17970 {
17971 /* Scroll last_unchanged_at_beg_row to the end of the
17972 window down dvpos lines. */
17973 set_terminal_window (f, end);
17974
17975 /* On dumb terminals delete dvpos lines at the end
17976 before inserting dvpos empty lines. */
17977 if (!FRAME_SCROLL_REGION_OK (f))
17978 ins_del_lines (f, end - dvpos, -dvpos);
17979
17980 /* Insert dvpos empty lines in front of
17981 last_unchanged_at_beg_row. */
17982 ins_del_lines (f, from, dvpos);
17983 }
17984 else if (dvpos < 0)
17985 {
17986 /* Scroll up last_unchanged_at_beg_vpos to the end of
17987 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17988 set_terminal_window (f, end);
17989
17990 /* Delete dvpos lines in front of
17991 last_unchanged_at_beg_vpos. ins_del_lines will set
17992 the cursor to the given vpos and emit |dvpos| delete
17993 line sequences. */
17994 ins_del_lines (f, from + dvpos, dvpos);
17995
17996 /* On a dumb terminal insert dvpos empty lines at the
17997 end. */
17998 if (!FRAME_SCROLL_REGION_OK (f))
17999 ins_del_lines (f, end + dvpos, -dvpos);
18000 }
18001
18002 set_terminal_window (f, 0);
18003 }
18004
18005 update_end (f);
18006 }
18007
18008 /* Shift reused rows of the current matrix to the right position.
18009 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18010 text. */
18011 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18012 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18013 if (dvpos < 0)
18014 {
18015 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18016 bottom_vpos, dvpos);
18017 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18018 bottom_vpos);
18019 }
18020 else if (dvpos > 0)
18021 {
18022 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18023 bottom_vpos, dvpos);
18024 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18025 first_unchanged_at_end_vpos + dvpos);
18026 }
18027
18028 /* For frame-based redisplay, make sure that current frame and window
18029 matrix are in sync with respect to glyph memory. */
18030 if (!FRAME_WINDOW_P (f))
18031 sync_frame_with_window_matrix_rows (w);
18032
18033 /* Adjust buffer positions in reused rows. */
18034 if (delta || delta_bytes)
18035 increment_matrix_positions (current_matrix,
18036 first_unchanged_at_end_vpos + dvpos,
18037 bottom_vpos, delta, delta_bytes);
18038
18039 /* Adjust Y positions. */
18040 if (dy)
18041 shift_glyph_matrix (w, current_matrix,
18042 first_unchanged_at_end_vpos + dvpos,
18043 bottom_vpos, dy);
18044
18045 if (first_unchanged_at_end_row)
18046 {
18047 first_unchanged_at_end_row += dvpos;
18048 if (first_unchanged_at_end_row->y >= it.last_visible_y
18049 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18050 first_unchanged_at_end_row = NULL;
18051 }
18052
18053 /* If scrolling up, there may be some lines to display at the end of
18054 the window. */
18055 last_text_row_at_end = NULL;
18056 if (dy < 0)
18057 {
18058 /* Scrolling up can leave for example a partially visible line
18059 at the end of the window to be redisplayed. */
18060 /* Set last_row to the glyph row in the current matrix where the
18061 window end line is found. It has been moved up or down in
18062 the matrix by dvpos. */
18063 int last_vpos = w->window_end_vpos + dvpos;
18064 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18065
18066 /* If last_row is the window end line, it should display text. */
18067 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18068
18069 /* If window end line was partially visible before, begin
18070 displaying at that line. Otherwise begin displaying with the
18071 line following it. */
18072 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18073 {
18074 init_to_row_start (&it, w, last_row);
18075 it.vpos = last_vpos;
18076 it.current_y = last_row->y;
18077 }
18078 else
18079 {
18080 init_to_row_end (&it, w, last_row);
18081 it.vpos = 1 + last_vpos;
18082 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18083 ++last_row;
18084 }
18085
18086 /* We may start in a continuation line. If so, we have to
18087 get the right continuation_lines_width and current_x. */
18088 it.continuation_lines_width = last_row->continuation_lines_width;
18089 it.hpos = it.current_x = 0;
18090
18091 /* Display the rest of the lines at the window end. */
18092 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18093 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18094 {
18095 /* Is it always sure that the display agrees with lines in
18096 the current matrix? I don't think so, so we mark rows
18097 displayed invalid in the current matrix by setting their
18098 enabled_p flag to zero. */
18099 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18100 if (display_line (&it))
18101 last_text_row_at_end = it.glyph_row - 1;
18102 }
18103 }
18104
18105 /* Update window_end_pos and window_end_vpos. */
18106 if (first_unchanged_at_end_row && !last_text_row_at_end)
18107 {
18108 /* Window end line if one of the preserved rows from the current
18109 matrix. Set row to the last row displaying text in current
18110 matrix starting at first_unchanged_at_end_row, after
18111 scrolling. */
18112 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18113 row = find_last_row_displaying_text (w->current_matrix, &it,
18114 first_unchanged_at_end_row);
18115 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18116 adjust_window_ends (w, row, 1);
18117 eassert (w->window_end_bytepos >= 0);
18118 IF_DEBUG (debug_method_add (w, "A"));
18119 }
18120 else if (last_text_row_at_end)
18121 {
18122 adjust_window_ends (w, last_text_row_at_end, 0);
18123 eassert (w->window_end_bytepos >= 0);
18124 IF_DEBUG (debug_method_add (w, "B"));
18125 }
18126 else if (last_text_row)
18127 {
18128 /* We have displayed either to the end of the window or at the
18129 end of the window, i.e. the last row with text is to be found
18130 in the desired matrix. */
18131 adjust_window_ends (w, last_text_row, 0);
18132 eassert (w->window_end_bytepos >= 0);
18133 }
18134 else if (first_unchanged_at_end_row == NULL
18135 && last_text_row == NULL
18136 && last_text_row_at_end == NULL)
18137 {
18138 /* Displayed to end of window, but no line containing text was
18139 displayed. Lines were deleted at the end of the window. */
18140 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18141 int vpos = w->window_end_vpos;
18142 struct glyph_row *current_row = current_matrix->rows + vpos;
18143 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18144
18145 for (row = NULL;
18146 row == NULL && vpos >= first_vpos;
18147 --vpos, --current_row, --desired_row)
18148 {
18149 if (desired_row->enabled_p)
18150 {
18151 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18152 row = desired_row;
18153 }
18154 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18155 row = current_row;
18156 }
18157
18158 eassert (row != NULL);
18159 w->window_end_vpos = vpos + 1;
18160 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18161 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18162 eassert (w->window_end_bytepos >= 0);
18163 IF_DEBUG (debug_method_add (w, "C"));
18164 }
18165 else
18166 emacs_abort ();
18167
18168 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18169 debug_end_vpos = w->window_end_vpos));
18170
18171 /* Record that display has not been completed. */
18172 w->window_end_valid = 0;
18173 w->desired_matrix->no_scrolling_p = 1;
18174 return 3;
18175
18176 #undef GIVE_UP
18177 }
18178
18179
18180 \f
18181 /***********************************************************************
18182 More debugging support
18183 ***********************************************************************/
18184
18185 #ifdef GLYPH_DEBUG
18186
18187 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18188 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18189 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18190
18191
18192 /* Dump the contents of glyph matrix MATRIX on stderr.
18193
18194 GLYPHS 0 means don't show glyph contents.
18195 GLYPHS 1 means show glyphs in short form
18196 GLYPHS > 1 means show glyphs in long form. */
18197
18198 void
18199 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18200 {
18201 int i;
18202 for (i = 0; i < matrix->nrows; ++i)
18203 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18204 }
18205
18206
18207 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18208 the glyph row and area where the glyph comes from. */
18209
18210 void
18211 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18212 {
18213 if (glyph->type == CHAR_GLYPH
18214 || glyph->type == GLYPHLESS_GLYPH)
18215 {
18216 fprintf (stderr,
18217 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18218 glyph - row->glyphs[TEXT_AREA],
18219 (glyph->type == CHAR_GLYPH
18220 ? 'C'
18221 : 'G'),
18222 glyph->charpos,
18223 (BUFFERP (glyph->object)
18224 ? 'B'
18225 : (STRINGP (glyph->object)
18226 ? 'S'
18227 : (INTEGERP (glyph->object)
18228 ? '0'
18229 : '-'))),
18230 glyph->pixel_width,
18231 glyph->u.ch,
18232 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18233 ? glyph->u.ch
18234 : '.'),
18235 glyph->face_id,
18236 glyph->left_box_line_p,
18237 glyph->right_box_line_p);
18238 }
18239 else if (glyph->type == STRETCH_GLYPH)
18240 {
18241 fprintf (stderr,
18242 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18243 glyph - row->glyphs[TEXT_AREA],
18244 'S',
18245 glyph->charpos,
18246 (BUFFERP (glyph->object)
18247 ? 'B'
18248 : (STRINGP (glyph->object)
18249 ? 'S'
18250 : (INTEGERP (glyph->object)
18251 ? '0'
18252 : '-'))),
18253 glyph->pixel_width,
18254 0,
18255 ' ',
18256 glyph->face_id,
18257 glyph->left_box_line_p,
18258 glyph->right_box_line_p);
18259 }
18260 else if (glyph->type == IMAGE_GLYPH)
18261 {
18262 fprintf (stderr,
18263 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18264 glyph - row->glyphs[TEXT_AREA],
18265 'I',
18266 glyph->charpos,
18267 (BUFFERP (glyph->object)
18268 ? 'B'
18269 : (STRINGP (glyph->object)
18270 ? 'S'
18271 : (INTEGERP (glyph->object)
18272 ? '0'
18273 : '-'))),
18274 glyph->pixel_width,
18275 glyph->u.img_id,
18276 '.',
18277 glyph->face_id,
18278 glyph->left_box_line_p,
18279 glyph->right_box_line_p);
18280 }
18281 else if (glyph->type == COMPOSITE_GLYPH)
18282 {
18283 fprintf (stderr,
18284 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18285 glyph - row->glyphs[TEXT_AREA],
18286 '+',
18287 glyph->charpos,
18288 (BUFFERP (glyph->object)
18289 ? 'B'
18290 : (STRINGP (glyph->object)
18291 ? 'S'
18292 : (INTEGERP (glyph->object)
18293 ? '0'
18294 : '-'))),
18295 glyph->pixel_width,
18296 glyph->u.cmp.id);
18297 if (glyph->u.cmp.automatic)
18298 fprintf (stderr,
18299 "[%d-%d]",
18300 glyph->slice.cmp.from, glyph->slice.cmp.to);
18301 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18302 glyph->face_id,
18303 glyph->left_box_line_p,
18304 glyph->right_box_line_p);
18305 }
18306 }
18307
18308
18309 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18310 GLYPHS 0 means don't show glyph contents.
18311 GLYPHS 1 means show glyphs in short form
18312 GLYPHS > 1 means show glyphs in long form. */
18313
18314 void
18315 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18316 {
18317 if (glyphs != 1)
18318 {
18319 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18320 fprintf (stderr, "==============================================================================\n");
18321
18322 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18323 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18324 vpos,
18325 MATRIX_ROW_START_CHARPOS (row),
18326 MATRIX_ROW_END_CHARPOS (row),
18327 row->used[TEXT_AREA],
18328 row->contains_overlapping_glyphs_p,
18329 row->enabled_p,
18330 row->truncated_on_left_p,
18331 row->truncated_on_right_p,
18332 row->continued_p,
18333 MATRIX_ROW_CONTINUATION_LINE_P (row),
18334 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18335 row->ends_at_zv_p,
18336 row->fill_line_p,
18337 row->ends_in_middle_of_char_p,
18338 row->starts_in_middle_of_char_p,
18339 row->mouse_face_p,
18340 row->x,
18341 row->y,
18342 row->pixel_width,
18343 row->height,
18344 row->visible_height,
18345 row->ascent,
18346 row->phys_ascent);
18347 /* The next 3 lines should align to "Start" in the header. */
18348 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18349 row->end.overlay_string_index,
18350 row->continuation_lines_width);
18351 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18352 CHARPOS (row->start.string_pos),
18353 CHARPOS (row->end.string_pos));
18354 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18355 row->end.dpvec_index);
18356 }
18357
18358 if (glyphs > 1)
18359 {
18360 int area;
18361
18362 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18363 {
18364 struct glyph *glyph = row->glyphs[area];
18365 struct glyph *glyph_end = glyph + row->used[area];
18366
18367 /* Glyph for a line end in text. */
18368 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18369 ++glyph_end;
18370
18371 if (glyph < glyph_end)
18372 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18373
18374 for (; glyph < glyph_end; ++glyph)
18375 dump_glyph (row, glyph, area);
18376 }
18377 }
18378 else if (glyphs == 1)
18379 {
18380 int area;
18381
18382 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18383 {
18384 char *s = alloca (row->used[area] + 4);
18385 int i;
18386
18387 for (i = 0; i < row->used[area]; ++i)
18388 {
18389 struct glyph *glyph = row->glyphs[area] + i;
18390 if (i == row->used[area] - 1
18391 && area == TEXT_AREA
18392 && INTEGERP (glyph->object)
18393 && glyph->type == CHAR_GLYPH
18394 && glyph->u.ch == ' ')
18395 {
18396 strcpy (&s[i], "[\\n]");
18397 i += 4;
18398 }
18399 else if (glyph->type == CHAR_GLYPH
18400 && glyph->u.ch < 0x80
18401 && glyph->u.ch >= ' ')
18402 s[i] = glyph->u.ch;
18403 else
18404 s[i] = '.';
18405 }
18406
18407 s[i] = '\0';
18408 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18409 }
18410 }
18411 }
18412
18413
18414 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18415 Sdump_glyph_matrix, 0, 1, "p",
18416 doc: /* Dump the current matrix of the selected window to stderr.
18417 Shows contents of glyph row structures. With non-nil
18418 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18419 glyphs in short form, otherwise show glyphs in long form. */)
18420 (Lisp_Object glyphs)
18421 {
18422 struct window *w = XWINDOW (selected_window);
18423 struct buffer *buffer = XBUFFER (w->contents);
18424
18425 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18426 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18427 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18428 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18429 fprintf (stderr, "=============================================\n");
18430 dump_glyph_matrix (w->current_matrix,
18431 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18432 return Qnil;
18433 }
18434
18435
18436 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18437 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18438 (void)
18439 {
18440 struct frame *f = XFRAME (selected_frame);
18441 dump_glyph_matrix (f->current_matrix, 1);
18442 return Qnil;
18443 }
18444
18445
18446 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18447 doc: /* Dump glyph row ROW to stderr.
18448 GLYPH 0 means don't dump glyphs.
18449 GLYPH 1 means dump glyphs in short form.
18450 GLYPH > 1 or omitted means dump glyphs in long form. */)
18451 (Lisp_Object row, Lisp_Object glyphs)
18452 {
18453 struct glyph_matrix *matrix;
18454 EMACS_INT vpos;
18455
18456 CHECK_NUMBER (row);
18457 matrix = XWINDOW (selected_window)->current_matrix;
18458 vpos = XINT (row);
18459 if (vpos >= 0 && vpos < matrix->nrows)
18460 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18461 vpos,
18462 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18463 return Qnil;
18464 }
18465
18466
18467 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18468 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18469 GLYPH 0 means don't dump glyphs.
18470 GLYPH 1 means dump glyphs in short form.
18471 GLYPH > 1 or omitted means dump glyphs in long form.
18472
18473 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18474 do nothing. */)
18475 (Lisp_Object row, Lisp_Object glyphs)
18476 {
18477 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18478 struct frame *sf = SELECTED_FRAME ();
18479 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18480 EMACS_INT vpos;
18481
18482 CHECK_NUMBER (row);
18483 vpos = XINT (row);
18484 if (vpos >= 0 && vpos < m->nrows)
18485 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18486 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18487 #endif
18488 return Qnil;
18489 }
18490
18491
18492 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18493 doc: /* Toggle tracing of redisplay.
18494 With ARG, turn tracing on if and only if ARG is positive. */)
18495 (Lisp_Object arg)
18496 {
18497 if (NILP (arg))
18498 trace_redisplay_p = !trace_redisplay_p;
18499 else
18500 {
18501 arg = Fprefix_numeric_value (arg);
18502 trace_redisplay_p = XINT (arg) > 0;
18503 }
18504
18505 return Qnil;
18506 }
18507
18508
18509 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18510 doc: /* Like `format', but print result to stderr.
18511 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18512 (ptrdiff_t nargs, Lisp_Object *args)
18513 {
18514 Lisp_Object s = Fformat (nargs, args);
18515 fprintf (stderr, "%s", SDATA (s));
18516 return Qnil;
18517 }
18518
18519 #endif /* GLYPH_DEBUG */
18520
18521
18522 \f
18523 /***********************************************************************
18524 Building Desired Matrix Rows
18525 ***********************************************************************/
18526
18527 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18528 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18529
18530 static struct glyph_row *
18531 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18532 {
18533 struct frame *f = XFRAME (WINDOW_FRAME (w));
18534 struct buffer *buffer = XBUFFER (w->contents);
18535 struct buffer *old = current_buffer;
18536 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18537 int arrow_len = SCHARS (overlay_arrow_string);
18538 const unsigned char *arrow_end = arrow_string + arrow_len;
18539 const unsigned char *p;
18540 struct it it;
18541 bool multibyte_p;
18542 int n_glyphs_before;
18543
18544 set_buffer_temp (buffer);
18545 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18546 it.glyph_row->used[TEXT_AREA] = 0;
18547 SET_TEXT_POS (it.position, 0, 0);
18548
18549 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18550 p = arrow_string;
18551 while (p < arrow_end)
18552 {
18553 Lisp_Object face, ilisp;
18554
18555 /* Get the next character. */
18556 if (multibyte_p)
18557 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18558 else
18559 {
18560 it.c = it.char_to_display = *p, it.len = 1;
18561 if (! ASCII_CHAR_P (it.c))
18562 it.char_to_display = BYTE8_TO_CHAR (it.c);
18563 }
18564 p += it.len;
18565
18566 /* Get its face. */
18567 ilisp = make_number (p - arrow_string);
18568 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18569 it.face_id = compute_char_face (f, it.char_to_display, face);
18570
18571 /* Compute its width, get its glyphs. */
18572 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18573 SET_TEXT_POS (it.position, -1, -1);
18574 PRODUCE_GLYPHS (&it);
18575
18576 /* If this character doesn't fit any more in the line, we have
18577 to remove some glyphs. */
18578 if (it.current_x > it.last_visible_x)
18579 {
18580 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18581 break;
18582 }
18583 }
18584
18585 set_buffer_temp (old);
18586 return it.glyph_row;
18587 }
18588
18589
18590 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18591 glyphs to insert is determined by produce_special_glyphs. */
18592
18593 static void
18594 insert_left_trunc_glyphs (struct it *it)
18595 {
18596 struct it truncate_it;
18597 struct glyph *from, *end, *to, *toend;
18598
18599 eassert (!FRAME_WINDOW_P (it->f)
18600 || (!it->glyph_row->reversed_p
18601 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18602 || (it->glyph_row->reversed_p
18603 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18604
18605 /* Get the truncation glyphs. */
18606 truncate_it = *it;
18607 truncate_it.current_x = 0;
18608 truncate_it.face_id = DEFAULT_FACE_ID;
18609 truncate_it.glyph_row = &scratch_glyph_row;
18610 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18611 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18612 truncate_it.object = make_number (0);
18613 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18614
18615 /* Overwrite glyphs from IT with truncation glyphs. */
18616 if (!it->glyph_row->reversed_p)
18617 {
18618 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18619
18620 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18621 end = from + tused;
18622 to = it->glyph_row->glyphs[TEXT_AREA];
18623 toend = to + it->glyph_row->used[TEXT_AREA];
18624 if (FRAME_WINDOW_P (it->f))
18625 {
18626 /* On GUI frames, when variable-size fonts are displayed,
18627 the truncation glyphs may need more pixels than the row's
18628 glyphs they overwrite. We overwrite more glyphs to free
18629 enough screen real estate, and enlarge the stretch glyph
18630 on the right (see display_line), if there is one, to
18631 preserve the screen position of the truncation glyphs on
18632 the right. */
18633 int w = 0;
18634 struct glyph *g = to;
18635 short used;
18636
18637 /* The first glyph could be partially visible, in which case
18638 it->glyph_row->x will be negative. But we want the left
18639 truncation glyphs to be aligned at the left margin of the
18640 window, so we override the x coordinate at which the row
18641 will begin. */
18642 it->glyph_row->x = 0;
18643 while (g < toend && w < it->truncation_pixel_width)
18644 {
18645 w += g->pixel_width;
18646 ++g;
18647 }
18648 if (g - to - tused > 0)
18649 {
18650 memmove (to + tused, g, (toend - g) * sizeof(*g));
18651 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18652 }
18653 used = it->glyph_row->used[TEXT_AREA];
18654 if (it->glyph_row->truncated_on_right_p
18655 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18656 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18657 == STRETCH_GLYPH)
18658 {
18659 int extra = w - it->truncation_pixel_width;
18660
18661 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18662 }
18663 }
18664
18665 while (from < end)
18666 *to++ = *from++;
18667
18668 /* There may be padding glyphs left over. Overwrite them too. */
18669 if (!FRAME_WINDOW_P (it->f))
18670 {
18671 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18672 {
18673 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18674 while (from < end)
18675 *to++ = *from++;
18676 }
18677 }
18678
18679 if (to > toend)
18680 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18681 }
18682 else
18683 {
18684 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18685
18686 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18687 that back to front. */
18688 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18689 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18690 toend = it->glyph_row->glyphs[TEXT_AREA];
18691 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18692 if (FRAME_WINDOW_P (it->f))
18693 {
18694 int w = 0;
18695 struct glyph *g = to;
18696
18697 while (g >= toend && w < it->truncation_pixel_width)
18698 {
18699 w += g->pixel_width;
18700 --g;
18701 }
18702 if (to - g - tused > 0)
18703 to = g + tused;
18704 if (it->glyph_row->truncated_on_right_p
18705 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18706 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18707 {
18708 int extra = w - it->truncation_pixel_width;
18709
18710 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18711 }
18712 }
18713
18714 while (from >= end && to >= toend)
18715 *to-- = *from--;
18716 if (!FRAME_WINDOW_P (it->f))
18717 {
18718 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18719 {
18720 from =
18721 truncate_it.glyph_row->glyphs[TEXT_AREA]
18722 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18723 while (from >= end && to >= toend)
18724 *to-- = *from--;
18725 }
18726 }
18727 if (from >= end)
18728 {
18729 /* Need to free some room before prepending additional
18730 glyphs. */
18731 int move_by = from - end + 1;
18732 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18733 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18734
18735 for ( ; g >= g0; g--)
18736 g[move_by] = *g;
18737 while (from >= end)
18738 *to-- = *from--;
18739 it->glyph_row->used[TEXT_AREA] += move_by;
18740 }
18741 }
18742 }
18743
18744 /* Compute the hash code for ROW. */
18745 unsigned
18746 row_hash (struct glyph_row *row)
18747 {
18748 int area, k;
18749 unsigned hashval = 0;
18750
18751 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18752 for (k = 0; k < row->used[area]; ++k)
18753 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18754 + row->glyphs[area][k].u.val
18755 + row->glyphs[area][k].face_id
18756 + row->glyphs[area][k].padding_p
18757 + (row->glyphs[area][k].type << 2));
18758
18759 return hashval;
18760 }
18761
18762 /* Compute the pixel height and width of IT->glyph_row.
18763
18764 Most of the time, ascent and height of a display line will be equal
18765 to the max_ascent and max_height values of the display iterator
18766 structure. This is not the case if
18767
18768 1. We hit ZV without displaying anything. In this case, max_ascent
18769 and max_height will be zero.
18770
18771 2. We have some glyphs that don't contribute to the line height.
18772 (The glyph row flag contributes_to_line_height_p is for future
18773 pixmap extensions).
18774
18775 The first case is easily covered by using default values because in
18776 these cases, the line height does not really matter, except that it
18777 must not be zero. */
18778
18779 static void
18780 compute_line_metrics (struct it *it)
18781 {
18782 struct glyph_row *row = it->glyph_row;
18783
18784 if (FRAME_WINDOW_P (it->f))
18785 {
18786 int i, min_y, max_y;
18787
18788 /* The line may consist of one space only, that was added to
18789 place the cursor on it. If so, the row's height hasn't been
18790 computed yet. */
18791 if (row->height == 0)
18792 {
18793 if (it->max_ascent + it->max_descent == 0)
18794 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18795 row->ascent = it->max_ascent;
18796 row->height = it->max_ascent + it->max_descent;
18797 row->phys_ascent = it->max_phys_ascent;
18798 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18799 row->extra_line_spacing = it->max_extra_line_spacing;
18800 }
18801
18802 /* Compute the width of this line. */
18803 row->pixel_width = row->x;
18804 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18805 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18806
18807 eassert (row->pixel_width >= 0);
18808 eassert (row->ascent >= 0 && row->height > 0);
18809
18810 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18811 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18812
18813 /* If first line's physical ascent is larger than its logical
18814 ascent, use the physical ascent, and make the row taller.
18815 This makes accented characters fully visible. */
18816 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18817 && row->phys_ascent > row->ascent)
18818 {
18819 row->height += row->phys_ascent - row->ascent;
18820 row->ascent = row->phys_ascent;
18821 }
18822
18823 /* Compute how much of the line is visible. */
18824 row->visible_height = row->height;
18825
18826 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18827 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18828
18829 if (row->y < min_y)
18830 row->visible_height -= min_y - row->y;
18831 if (row->y + row->height > max_y)
18832 row->visible_height -= row->y + row->height - max_y;
18833 }
18834 else
18835 {
18836 row->pixel_width = row->used[TEXT_AREA];
18837 if (row->continued_p)
18838 row->pixel_width -= it->continuation_pixel_width;
18839 else if (row->truncated_on_right_p)
18840 row->pixel_width -= it->truncation_pixel_width;
18841 row->ascent = row->phys_ascent = 0;
18842 row->height = row->phys_height = row->visible_height = 1;
18843 row->extra_line_spacing = 0;
18844 }
18845
18846 /* Compute a hash code for this row. */
18847 row->hash = row_hash (row);
18848
18849 it->max_ascent = it->max_descent = 0;
18850 it->max_phys_ascent = it->max_phys_descent = 0;
18851 }
18852
18853
18854 /* Append one space to the glyph row of iterator IT if doing a
18855 window-based redisplay. The space has the same face as
18856 IT->face_id. Value is non-zero if a space was added.
18857
18858 This function is called to make sure that there is always one glyph
18859 at the end of a glyph row that the cursor can be set on under
18860 window-systems. (If there weren't such a glyph we would not know
18861 how wide and tall a box cursor should be displayed).
18862
18863 At the same time this space let's a nicely handle clearing to the
18864 end of the line if the row ends in italic text. */
18865
18866 static int
18867 append_space_for_newline (struct it *it, int default_face_p)
18868 {
18869 if (FRAME_WINDOW_P (it->f))
18870 {
18871 int n = it->glyph_row->used[TEXT_AREA];
18872
18873 if (it->glyph_row->glyphs[TEXT_AREA] + n
18874 < it->glyph_row->glyphs[1 + TEXT_AREA])
18875 {
18876 /* Save some values that must not be changed.
18877 Must save IT->c and IT->len because otherwise
18878 ITERATOR_AT_END_P wouldn't work anymore after
18879 append_space_for_newline has been called. */
18880 enum display_element_type saved_what = it->what;
18881 int saved_c = it->c, saved_len = it->len;
18882 int saved_char_to_display = it->char_to_display;
18883 int saved_x = it->current_x;
18884 int saved_face_id = it->face_id;
18885 int saved_box_end = it->end_of_box_run_p;
18886 struct text_pos saved_pos;
18887 Lisp_Object saved_object;
18888 struct face *face;
18889
18890 saved_object = it->object;
18891 saved_pos = it->position;
18892
18893 it->what = IT_CHARACTER;
18894 memset (&it->position, 0, sizeof it->position);
18895 it->object = make_number (0);
18896 it->c = it->char_to_display = ' ';
18897 it->len = 1;
18898
18899 /* If the default face was remapped, be sure to use the
18900 remapped face for the appended newline. */
18901 if (default_face_p)
18902 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18903 else if (it->face_before_selective_p)
18904 it->face_id = it->saved_face_id;
18905 face = FACE_FROM_ID (it->f, it->face_id);
18906 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18907 /* In R2L rows, we will prepend a stretch glyph that will
18908 have the end_of_box_run_p flag set for it, so there's no
18909 need for the appended newline glyph to have that flag
18910 set. */
18911 if (it->glyph_row->reversed_p
18912 /* But if the appended newline glyph goes all the way to
18913 the end of the row, there will be no stretch glyph,
18914 so leave the box flag set. */
18915 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18916 it->end_of_box_run_p = 0;
18917
18918 PRODUCE_GLYPHS (it);
18919
18920 it->override_ascent = -1;
18921 it->constrain_row_ascent_descent_p = 0;
18922 it->current_x = saved_x;
18923 it->object = saved_object;
18924 it->position = saved_pos;
18925 it->what = saved_what;
18926 it->face_id = saved_face_id;
18927 it->len = saved_len;
18928 it->c = saved_c;
18929 it->char_to_display = saved_char_to_display;
18930 it->end_of_box_run_p = saved_box_end;
18931 return 1;
18932 }
18933 }
18934
18935 return 0;
18936 }
18937
18938
18939 /* Extend the face of the last glyph in the text area of IT->glyph_row
18940 to the end of the display line. Called from display_line. If the
18941 glyph row is empty, add a space glyph to it so that we know the
18942 face to draw. Set the glyph row flag fill_line_p. If the glyph
18943 row is R2L, prepend a stretch glyph to cover the empty space to the
18944 left of the leftmost glyph. */
18945
18946 static void
18947 extend_face_to_end_of_line (struct it *it)
18948 {
18949 struct face *face, *default_face;
18950 struct frame *f = it->f;
18951
18952 /* If line is already filled, do nothing. Non window-system frames
18953 get a grace of one more ``pixel'' because their characters are
18954 1-``pixel'' wide, so they hit the equality too early. This grace
18955 is needed only for R2L rows that are not continued, to produce
18956 one extra blank where we could display the cursor. */
18957 if ((it->current_x >= it->last_visible_x
18958 + (!FRAME_WINDOW_P (f)
18959 && it->glyph_row->reversed_p
18960 && !it->glyph_row->continued_p))
18961 /* If the window has display margins, we will need to extend
18962 their face even if the text area is filled. */
18963 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18964 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18965 return;
18966
18967 /* The default face, possibly remapped. */
18968 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18969
18970 /* Face extension extends the background and box of IT->face_id
18971 to the end of the line. If the background equals the background
18972 of the frame, we don't have to do anything. */
18973 if (it->face_before_selective_p)
18974 face = FACE_FROM_ID (f, it->saved_face_id);
18975 else
18976 face = FACE_FROM_ID (f, it->face_id);
18977
18978 if (FRAME_WINDOW_P (f)
18979 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18980 && face->box == FACE_NO_BOX
18981 && face->background == FRAME_BACKGROUND_PIXEL (f)
18982 #ifdef HAVE_WINDOW_SYSTEM
18983 && !face->stipple
18984 #endif
18985 && !it->glyph_row->reversed_p)
18986 return;
18987
18988 /* Set the glyph row flag indicating that the face of the last glyph
18989 in the text area has to be drawn to the end of the text area. */
18990 it->glyph_row->fill_line_p = 1;
18991
18992 /* If current character of IT is not ASCII, make sure we have the
18993 ASCII face. This will be automatically undone the next time
18994 get_next_display_element returns a multibyte character. Note
18995 that the character will always be single byte in unibyte
18996 text. */
18997 if (!ASCII_CHAR_P (it->c))
18998 {
18999 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19000 }
19001
19002 if (FRAME_WINDOW_P (f))
19003 {
19004 /* If the row is empty, add a space with the current face of IT,
19005 so that we know which face to draw. */
19006 if (it->glyph_row->used[TEXT_AREA] == 0)
19007 {
19008 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19009 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19010 it->glyph_row->used[TEXT_AREA] = 1;
19011 }
19012 /* Mode line and the header line don't have margins, and
19013 likewise the frame's tool-bar window, if there is any. */
19014 if (!(it->glyph_row->mode_line_p
19015 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19016 || (WINDOWP (f->tool_bar_window)
19017 && it->w == XWINDOW (f->tool_bar_window))
19018 #endif
19019 ))
19020 {
19021 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19022 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19023 {
19024 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19025 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19026 default_face->id;
19027 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19028 }
19029 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19030 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19031 {
19032 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19033 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19034 default_face->id;
19035 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19036 }
19037 }
19038 #ifdef HAVE_WINDOW_SYSTEM
19039 if (it->glyph_row->reversed_p)
19040 {
19041 /* Prepend a stretch glyph to the row, such that the
19042 rightmost glyph will be drawn flushed all the way to the
19043 right margin of the window. The stretch glyph that will
19044 occupy the empty space, if any, to the left of the
19045 glyphs. */
19046 struct font *font = face->font ? face->font : FRAME_FONT (f);
19047 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19048 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19049 struct glyph *g;
19050 int row_width, stretch_ascent, stretch_width;
19051 struct text_pos saved_pos;
19052 int saved_face_id, saved_avoid_cursor, saved_box_start;
19053
19054 for (row_width = 0, g = row_start; g < row_end; g++)
19055 row_width += g->pixel_width;
19056 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19057 if (stretch_width > 0)
19058 {
19059 stretch_ascent =
19060 (((it->ascent + it->descent)
19061 * FONT_BASE (font)) / FONT_HEIGHT (font));
19062 saved_pos = it->position;
19063 memset (&it->position, 0, sizeof it->position);
19064 saved_avoid_cursor = it->avoid_cursor_p;
19065 it->avoid_cursor_p = 1;
19066 saved_face_id = it->face_id;
19067 saved_box_start = it->start_of_box_run_p;
19068 /* The last row's stretch glyph should get the default
19069 face, to avoid painting the rest of the window with
19070 the region face, if the region ends at ZV. */
19071 if (it->glyph_row->ends_at_zv_p)
19072 it->face_id = default_face->id;
19073 else
19074 it->face_id = face->id;
19075 it->start_of_box_run_p = 0;
19076 append_stretch_glyph (it, make_number (0), stretch_width,
19077 it->ascent + it->descent, stretch_ascent);
19078 it->position = saved_pos;
19079 it->avoid_cursor_p = saved_avoid_cursor;
19080 it->face_id = saved_face_id;
19081 it->start_of_box_run_p = saved_box_start;
19082 }
19083 }
19084 #endif /* HAVE_WINDOW_SYSTEM */
19085 }
19086 else
19087 {
19088 /* Save some values that must not be changed. */
19089 int saved_x = it->current_x;
19090 struct text_pos saved_pos;
19091 Lisp_Object saved_object;
19092 enum display_element_type saved_what = it->what;
19093 int saved_face_id = it->face_id;
19094
19095 saved_object = it->object;
19096 saved_pos = it->position;
19097
19098 it->what = IT_CHARACTER;
19099 memset (&it->position, 0, sizeof it->position);
19100 it->object = make_number (0);
19101 it->c = it->char_to_display = ' ';
19102 it->len = 1;
19103
19104 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19105 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19106 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19107 && !it->glyph_row->mode_line_p
19108 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19109 {
19110 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19111 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19112
19113 for (it->current_x = 0; g < e; g++)
19114 it->current_x += g->pixel_width;
19115
19116 it->area = LEFT_MARGIN_AREA;
19117 it->face_id = default_face->id;
19118 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19119 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19120 {
19121 PRODUCE_GLYPHS (it);
19122 /* term.c:produce_glyphs advances it->current_x only for
19123 TEXT_AREA. */
19124 it->current_x += it->pixel_width;
19125 }
19126
19127 it->current_x = saved_x;
19128 it->area = TEXT_AREA;
19129 }
19130
19131 /* The last row's blank glyphs should get the default face, to
19132 avoid painting the rest of the window with the region face,
19133 if the region ends at ZV. */
19134 if (it->glyph_row->ends_at_zv_p)
19135 it->face_id = default_face->id;
19136 else
19137 it->face_id = face->id;
19138 PRODUCE_GLYPHS (it);
19139
19140 while (it->current_x <= it->last_visible_x)
19141 PRODUCE_GLYPHS (it);
19142
19143 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19144 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19145 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19146 && !it->glyph_row->mode_line_p
19147 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19148 {
19149 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19150 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19151
19152 for ( ; g < e; g++)
19153 it->current_x += g->pixel_width;
19154
19155 it->area = RIGHT_MARGIN_AREA;
19156 it->face_id = default_face->id;
19157 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19158 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19159 {
19160 PRODUCE_GLYPHS (it);
19161 it->current_x += it->pixel_width;
19162 }
19163
19164 it->area = TEXT_AREA;
19165 }
19166
19167 /* Don't count these blanks really. It would let us insert a left
19168 truncation glyph below and make us set the cursor on them, maybe. */
19169 it->current_x = saved_x;
19170 it->object = saved_object;
19171 it->position = saved_pos;
19172 it->what = saved_what;
19173 it->face_id = saved_face_id;
19174 }
19175 }
19176
19177
19178 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19179 trailing whitespace. */
19180
19181 static int
19182 trailing_whitespace_p (ptrdiff_t charpos)
19183 {
19184 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19185 int c = 0;
19186
19187 while (bytepos < ZV_BYTE
19188 && (c = FETCH_CHAR (bytepos),
19189 c == ' ' || c == '\t'))
19190 ++bytepos;
19191
19192 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19193 {
19194 if (bytepos != PT_BYTE)
19195 return 1;
19196 }
19197 return 0;
19198 }
19199
19200
19201 /* Highlight trailing whitespace, if any, in ROW. */
19202
19203 static void
19204 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19205 {
19206 int used = row->used[TEXT_AREA];
19207
19208 if (used)
19209 {
19210 struct glyph *start = row->glyphs[TEXT_AREA];
19211 struct glyph *glyph = start + used - 1;
19212
19213 if (row->reversed_p)
19214 {
19215 /* Right-to-left rows need to be processed in the opposite
19216 direction, so swap the edge pointers. */
19217 glyph = start;
19218 start = row->glyphs[TEXT_AREA] + used - 1;
19219 }
19220
19221 /* Skip over glyphs inserted to display the cursor at the
19222 end of a line, for extending the face of the last glyph
19223 to the end of the line on terminals, and for truncation
19224 and continuation glyphs. */
19225 if (!row->reversed_p)
19226 {
19227 while (glyph >= start
19228 && glyph->type == CHAR_GLYPH
19229 && INTEGERP (glyph->object))
19230 --glyph;
19231 }
19232 else
19233 {
19234 while (glyph <= start
19235 && glyph->type == CHAR_GLYPH
19236 && INTEGERP (glyph->object))
19237 ++glyph;
19238 }
19239
19240 /* If last glyph is a space or stretch, and it's trailing
19241 whitespace, set the face of all trailing whitespace glyphs in
19242 IT->glyph_row to `trailing-whitespace'. */
19243 if ((row->reversed_p ? glyph <= start : glyph >= start)
19244 && BUFFERP (glyph->object)
19245 && (glyph->type == STRETCH_GLYPH
19246 || (glyph->type == CHAR_GLYPH
19247 && glyph->u.ch == ' '))
19248 && trailing_whitespace_p (glyph->charpos))
19249 {
19250 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19251 if (face_id < 0)
19252 return;
19253
19254 if (!row->reversed_p)
19255 {
19256 while (glyph >= start
19257 && BUFFERP (glyph->object)
19258 && (glyph->type == STRETCH_GLYPH
19259 || (glyph->type == CHAR_GLYPH
19260 && glyph->u.ch == ' ')))
19261 (glyph--)->face_id = face_id;
19262 }
19263 else
19264 {
19265 while (glyph <= start
19266 && BUFFERP (glyph->object)
19267 && (glyph->type == STRETCH_GLYPH
19268 || (glyph->type == CHAR_GLYPH
19269 && glyph->u.ch == ' ')))
19270 (glyph++)->face_id = face_id;
19271 }
19272 }
19273 }
19274 }
19275
19276
19277 /* Value is non-zero if glyph row ROW should be
19278 considered to hold the buffer position CHARPOS. */
19279
19280 static int
19281 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19282 {
19283 int result = 1;
19284
19285 if (charpos == CHARPOS (row->end.pos)
19286 || charpos == MATRIX_ROW_END_CHARPOS (row))
19287 {
19288 /* Suppose the row ends on a string.
19289 Unless the row is continued, that means it ends on a newline
19290 in the string. If it's anything other than a display string
19291 (e.g., a before-string from an overlay), we don't want the
19292 cursor there. (This heuristic seems to give the optimal
19293 behavior for the various types of multi-line strings.)
19294 One exception: if the string has `cursor' property on one of
19295 its characters, we _do_ want the cursor there. */
19296 if (CHARPOS (row->end.string_pos) >= 0)
19297 {
19298 if (row->continued_p)
19299 result = 1;
19300 else
19301 {
19302 /* Check for `display' property. */
19303 struct glyph *beg = row->glyphs[TEXT_AREA];
19304 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19305 struct glyph *glyph;
19306
19307 result = 0;
19308 for (glyph = end; glyph >= beg; --glyph)
19309 if (STRINGP (glyph->object))
19310 {
19311 Lisp_Object prop
19312 = Fget_char_property (make_number (charpos),
19313 Qdisplay, Qnil);
19314 result =
19315 (!NILP (prop)
19316 && display_prop_string_p (prop, glyph->object));
19317 /* If there's a `cursor' property on one of the
19318 string's characters, this row is a cursor row,
19319 even though this is not a display string. */
19320 if (!result)
19321 {
19322 Lisp_Object s = glyph->object;
19323
19324 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19325 {
19326 ptrdiff_t gpos = glyph->charpos;
19327
19328 if (!NILP (Fget_char_property (make_number (gpos),
19329 Qcursor, s)))
19330 {
19331 result = 1;
19332 break;
19333 }
19334 }
19335 }
19336 break;
19337 }
19338 }
19339 }
19340 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19341 {
19342 /* If the row ends in middle of a real character,
19343 and the line is continued, we want the cursor here.
19344 That's because CHARPOS (ROW->end.pos) would equal
19345 PT if PT is before the character. */
19346 if (!row->ends_in_ellipsis_p)
19347 result = row->continued_p;
19348 else
19349 /* If the row ends in an ellipsis, then
19350 CHARPOS (ROW->end.pos) will equal point after the
19351 invisible text. We want that position to be displayed
19352 after the ellipsis. */
19353 result = 0;
19354 }
19355 /* If the row ends at ZV, display the cursor at the end of that
19356 row instead of at the start of the row below. */
19357 else if (row->ends_at_zv_p)
19358 result = 1;
19359 else
19360 result = 0;
19361 }
19362
19363 return result;
19364 }
19365
19366 /* Value is non-zero if glyph row ROW should be
19367 used to hold the cursor. */
19368
19369 static int
19370 cursor_row_p (struct glyph_row *row)
19371 {
19372 return row_for_charpos_p (row, PT);
19373 }
19374
19375 \f
19376
19377 /* Push the property PROP so that it will be rendered at the current
19378 position in IT. Return 1 if PROP was successfully pushed, 0
19379 otherwise. Called from handle_line_prefix to handle the
19380 `line-prefix' and `wrap-prefix' properties. */
19381
19382 static int
19383 push_prefix_prop (struct it *it, Lisp_Object prop)
19384 {
19385 struct text_pos pos =
19386 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19387
19388 eassert (it->method == GET_FROM_BUFFER
19389 || it->method == GET_FROM_DISPLAY_VECTOR
19390 || it->method == GET_FROM_STRING);
19391
19392 /* We need to save the current buffer/string position, so it will be
19393 restored by pop_it, because iterate_out_of_display_property
19394 depends on that being set correctly, but some situations leave
19395 it->position not yet set when this function is called. */
19396 push_it (it, &pos);
19397
19398 if (STRINGP (prop))
19399 {
19400 if (SCHARS (prop) == 0)
19401 {
19402 pop_it (it);
19403 return 0;
19404 }
19405
19406 it->string = prop;
19407 it->string_from_prefix_prop_p = 1;
19408 it->multibyte_p = STRING_MULTIBYTE (it->string);
19409 it->current.overlay_string_index = -1;
19410 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19411 it->end_charpos = it->string_nchars = SCHARS (it->string);
19412 it->method = GET_FROM_STRING;
19413 it->stop_charpos = 0;
19414 it->prev_stop = 0;
19415 it->base_level_stop = 0;
19416
19417 /* Force paragraph direction to be that of the parent
19418 buffer/string. */
19419 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19420 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19421 else
19422 it->paragraph_embedding = L2R;
19423
19424 /* Set up the bidi iterator for this display string. */
19425 if (it->bidi_p)
19426 {
19427 it->bidi_it.string.lstring = it->string;
19428 it->bidi_it.string.s = NULL;
19429 it->bidi_it.string.schars = it->end_charpos;
19430 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19431 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19432 it->bidi_it.string.unibyte = !it->multibyte_p;
19433 it->bidi_it.w = it->w;
19434 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19435 }
19436 }
19437 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19438 {
19439 it->method = GET_FROM_STRETCH;
19440 it->object = prop;
19441 }
19442 #ifdef HAVE_WINDOW_SYSTEM
19443 else if (IMAGEP (prop))
19444 {
19445 it->what = IT_IMAGE;
19446 it->image_id = lookup_image (it->f, prop);
19447 it->method = GET_FROM_IMAGE;
19448 }
19449 #endif /* HAVE_WINDOW_SYSTEM */
19450 else
19451 {
19452 pop_it (it); /* bogus display property, give up */
19453 return 0;
19454 }
19455
19456 return 1;
19457 }
19458
19459 /* Return the character-property PROP at the current position in IT. */
19460
19461 static Lisp_Object
19462 get_it_property (struct it *it, Lisp_Object prop)
19463 {
19464 Lisp_Object position, object = it->object;
19465
19466 if (STRINGP (object))
19467 position = make_number (IT_STRING_CHARPOS (*it));
19468 else if (BUFFERP (object))
19469 {
19470 position = make_number (IT_CHARPOS (*it));
19471 object = it->window;
19472 }
19473 else
19474 return Qnil;
19475
19476 return Fget_char_property (position, prop, object);
19477 }
19478
19479 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19480
19481 static void
19482 handle_line_prefix (struct it *it)
19483 {
19484 Lisp_Object prefix;
19485
19486 if (it->continuation_lines_width > 0)
19487 {
19488 prefix = get_it_property (it, Qwrap_prefix);
19489 if (NILP (prefix))
19490 prefix = Vwrap_prefix;
19491 }
19492 else
19493 {
19494 prefix = get_it_property (it, Qline_prefix);
19495 if (NILP (prefix))
19496 prefix = Vline_prefix;
19497 }
19498 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19499 {
19500 /* If the prefix is wider than the window, and we try to wrap
19501 it, it would acquire its own wrap prefix, and so on till the
19502 iterator stack overflows. So, don't wrap the prefix. */
19503 it->line_wrap = TRUNCATE;
19504 it->avoid_cursor_p = 1;
19505 }
19506 }
19507
19508 \f
19509
19510 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19511 only for R2L lines from display_line and display_string, when they
19512 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19513 the line/string needs to be continued on the next glyph row. */
19514 static void
19515 unproduce_glyphs (struct it *it, int n)
19516 {
19517 struct glyph *glyph, *end;
19518
19519 eassert (it->glyph_row);
19520 eassert (it->glyph_row->reversed_p);
19521 eassert (it->area == TEXT_AREA);
19522 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19523
19524 if (n > it->glyph_row->used[TEXT_AREA])
19525 n = it->glyph_row->used[TEXT_AREA];
19526 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19527 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19528 for ( ; glyph < end; glyph++)
19529 glyph[-n] = *glyph;
19530 }
19531
19532 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19533 and ROW->maxpos. */
19534 static void
19535 find_row_edges (struct it *it, struct glyph_row *row,
19536 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19537 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19538 {
19539 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19540 lines' rows is implemented for bidi-reordered rows. */
19541
19542 /* ROW->minpos is the value of min_pos, the minimal buffer position
19543 we have in ROW, or ROW->start.pos if that is smaller. */
19544 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19545 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19546 else
19547 /* We didn't find buffer positions smaller than ROW->start, or
19548 didn't find _any_ valid buffer positions in any of the glyphs,
19549 so we must trust the iterator's computed positions. */
19550 row->minpos = row->start.pos;
19551 if (max_pos <= 0)
19552 {
19553 max_pos = CHARPOS (it->current.pos);
19554 max_bpos = BYTEPOS (it->current.pos);
19555 }
19556
19557 /* Here are the various use-cases for ending the row, and the
19558 corresponding values for ROW->maxpos:
19559
19560 Line ends in a newline from buffer eol_pos + 1
19561 Line is continued from buffer max_pos + 1
19562 Line is truncated on right it->current.pos
19563 Line ends in a newline from string max_pos + 1(*)
19564 (*) + 1 only when line ends in a forward scan
19565 Line is continued from string max_pos
19566 Line is continued from display vector max_pos
19567 Line is entirely from a string min_pos == max_pos
19568 Line is entirely from a display vector min_pos == max_pos
19569 Line that ends at ZV ZV
19570
19571 If you discover other use-cases, please add them here as
19572 appropriate. */
19573 if (row->ends_at_zv_p)
19574 row->maxpos = it->current.pos;
19575 else if (row->used[TEXT_AREA])
19576 {
19577 int seen_this_string = 0;
19578 struct glyph_row *r1 = row - 1;
19579
19580 /* Did we see the same display string on the previous row? */
19581 if (STRINGP (it->object)
19582 /* this is not the first row */
19583 && row > it->w->desired_matrix->rows
19584 /* previous row is not the header line */
19585 && !r1->mode_line_p
19586 /* previous row also ends in a newline from a string */
19587 && r1->ends_in_newline_from_string_p)
19588 {
19589 struct glyph *start, *end;
19590
19591 /* Search for the last glyph of the previous row that came
19592 from buffer or string. Depending on whether the row is
19593 L2R or R2L, we need to process it front to back or the
19594 other way round. */
19595 if (!r1->reversed_p)
19596 {
19597 start = r1->glyphs[TEXT_AREA];
19598 end = start + r1->used[TEXT_AREA];
19599 /* Glyphs inserted by redisplay have an integer (zero)
19600 as their object. */
19601 while (end > start
19602 && INTEGERP ((end - 1)->object)
19603 && (end - 1)->charpos <= 0)
19604 --end;
19605 if (end > start)
19606 {
19607 if (EQ ((end - 1)->object, it->object))
19608 seen_this_string = 1;
19609 }
19610 else
19611 /* If all the glyphs of the previous row were inserted
19612 by redisplay, it means the previous row was
19613 produced from a single newline, which is only
19614 possible if that newline came from the same string
19615 as the one which produced this ROW. */
19616 seen_this_string = 1;
19617 }
19618 else
19619 {
19620 end = r1->glyphs[TEXT_AREA] - 1;
19621 start = end + r1->used[TEXT_AREA];
19622 while (end < start
19623 && INTEGERP ((end + 1)->object)
19624 && (end + 1)->charpos <= 0)
19625 ++end;
19626 if (end < start)
19627 {
19628 if (EQ ((end + 1)->object, it->object))
19629 seen_this_string = 1;
19630 }
19631 else
19632 seen_this_string = 1;
19633 }
19634 }
19635 /* Take note of each display string that covers a newline only
19636 once, the first time we see it. This is for when a display
19637 string includes more than one newline in it. */
19638 if (row->ends_in_newline_from_string_p && !seen_this_string)
19639 {
19640 /* If we were scanning the buffer forward when we displayed
19641 the string, we want to account for at least one buffer
19642 position that belongs to this row (position covered by
19643 the display string), so that cursor positioning will
19644 consider this row as a candidate when point is at the end
19645 of the visual line represented by this row. This is not
19646 required when scanning back, because max_pos will already
19647 have a much larger value. */
19648 if (CHARPOS (row->end.pos) > max_pos)
19649 INC_BOTH (max_pos, max_bpos);
19650 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19651 }
19652 else if (CHARPOS (it->eol_pos) > 0)
19653 SET_TEXT_POS (row->maxpos,
19654 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19655 else if (row->continued_p)
19656 {
19657 /* If max_pos is different from IT's current position, it
19658 means IT->method does not belong to the display element
19659 at max_pos. However, it also means that the display
19660 element at max_pos was displayed in its entirety on this
19661 line, which is equivalent to saying that the next line
19662 starts at the next buffer position. */
19663 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19664 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19665 else
19666 {
19667 INC_BOTH (max_pos, max_bpos);
19668 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19669 }
19670 }
19671 else if (row->truncated_on_right_p)
19672 /* display_line already called reseat_at_next_visible_line_start,
19673 which puts the iterator at the beginning of the next line, in
19674 the logical order. */
19675 row->maxpos = it->current.pos;
19676 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19677 /* A line that is entirely from a string/image/stretch... */
19678 row->maxpos = row->minpos;
19679 else
19680 emacs_abort ();
19681 }
19682 else
19683 row->maxpos = it->current.pos;
19684 }
19685
19686 /* Construct the glyph row IT->glyph_row in the desired matrix of
19687 IT->w from text at the current position of IT. See dispextern.h
19688 for an overview of struct it. Value is non-zero if
19689 IT->glyph_row displays text, as opposed to a line displaying ZV
19690 only. */
19691
19692 static int
19693 display_line (struct it *it)
19694 {
19695 struct glyph_row *row = it->glyph_row;
19696 Lisp_Object overlay_arrow_string;
19697 struct it wrap_it;
19698 void *wrap_data = NULL;
19699 int may_wrap = 0, wrap_x IF_LINT (= 0);
19700 int wrap_row_used = -1;
19701 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19702 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19703 int wrap_row_extra_line_spacing IF_LINT (= 0);
19704 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19705 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19706 int cvpos;
19707 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19708 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19709
19710 /* We always start displaying at hpos zero even if hscrolled. */
19711 eassert (it->hpos == 0 && it->current_x == 0);
19712
19713 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19714 >= it->w->desired_matrix->nrows)
19715 {
19716 it->w->nrows_scale_factor++;
19717 it->f->fonts_changed = 1;
19718 return 0;
19719 }
19720
19721 /* Clear the result glyph row and enable it. */
19722 prepare_desired_row (row);
19723
19724 row->y = it->current_y;
19725 row->start = it->start;
19726 row->continuation_lines_width = it->continuation_lines_width;
19727 row->displays_text_p = 1;
19728 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19729 it->starts_in_middle_of_char_p = 0;
19730
19731 /* Arrange the overlays nicely for our purposes. Usually, we call
19732 display_line on only one line at a time, in which case this
19733 can't really hurt too much, or we call it on lines which appear
19734 one after another in the buffer, in which case all calls to
19735 recenter_overlay_lists but the first will be pretty cheap. */
19736 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19737
19738 /* Move over display elements that are not visible because we are
19739 hscrolled. This may stop at an x-position < IT->first_visible_x
19740 if the first glyph is partially visible or if we hit a line end. */
19741 if (it->current_x < it->first_visible_x)
19742 {
19743 enum move_it_result move_result;
19744
19745 this_line_min_pos = row->start.pos;
19746 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19747 MOVE_TO_POS | MOVE_TO_X);
19748 /* If we are under a large hscroll, move_it_in_display_line_to
19749 could hit the end of the line without reaching
19750 it->first_visible_x. Pretend that we did reach it. This is
19751 especially important on a TTY, where we will call
19752 extend_face_to_end_of_line, which needs to know how many
19753 blank glyphs to produce. */
19754 if (it->current_x < it->first_visible_x
19755 && (move_result == MOVE_NEWLINE_OR_CR
19756 || move_result == MOVE_POS_MATCH_OR_ZV))
19757 it->current_x = it->first_visible_x;
19758
19759 /* Record the smallest positions seen while we moved over
19760 display elements that are not visible. This is needed by
19761 redisplay_internal for optimizing the case where the cursor
19762 stays inside the same line. The rest of this function only
19763 considers positions that are actually displayed, so
19764 RECORD_MAX_MIN_POS will not otherwise record positions that
19765 are hscrolled to the left of the left edge of the window. */
19766 min_pos = CHARPOS (this_line_min_pos);
19767 min_bpos = BYTEPOS (this_line_min_pos);
19768 }
19769 else
19770 {
19771 /* We only do this when not calling `move_it_in_display_line_to'
19772 above, because move_it_in_display_line_to calls
19773 handle_line_prefix itself. */
19774 handle_line_prefix (it);
19775 }
19776
19777 /* Get the initial row height. This is either the height of the
19778 text hscrolled, if there is any, or zero. */
19779 row->ascent = it->max_ascent;
19780 row->height = it->max_ascent + it->max_descent;
19781 row->phys_ascent = it->max_phys_ascent;
19782 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19783 row->extra_line_spacing = it->max_extra_line_spacing;
19784
19785 /* Utility macro to record max and min buffer positions seen until now. */
19786 #define RECORD_MAX_MIN_POS(IT) \
19787 do \
19788 { \
19789 int composition_p = !STRINGP ((IT)->string) \
19790 && ((IT)->what == IT_COMPOSITION); \
19791 ptrdiff_t current_pos = \
19792 composition_p ? (IT)->cmp_it.charpos \
19793 : IT_CHARPOS (*(IT)); \
19794 ptrdiff_t current_bpos = \
19795 composition_p ? CHAR_TO_BYTE (current_pos) \
19796 : IT_BYTEPOS (*(IT)); \
19797 if (current_pos < min_pos) \
19798 { \
19799 min_pos = current_pos; \
19800 min_bpos = current_bpos; \
19801 } \
19802 if (IT_CHARPOS (*it) > max_pos) \
19803 { \
19804 max_pos = IT_CHARPOS (*it); \
19805 max_bpos = IT_BYTEPOS (*it); \
19806 } \
19807 } \
19808 while (0)
19809
19810 /* Loop generating characters. The loop is left with IT on the next
19811 character to display. */
19812 while (1)
19813 {
19814 int n_glyphs_before, hpos_before, x_before;
19815 int x, nglyphs;
19816 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19817
19818 /* Retrieve the next thing to display. Value is zero if end of
19819 buffer reached. */
19820 if (!get_next_display_element (it))
19821 {
19822 /* Maybe add a space at the end of this line that is used to
19823 display the cursor there under X. Set the charpos of the
19824 first glyph of blank lines not corresponding to any text
19825 to -1. */
19826 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19827 row->exact_window_width_line_p = 1;
19828 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19829 || row->used[TEXT_AREA] == 0)
19830 {
19831 row->glyphs[TEXT_AREA]->charpos = -1;
19832 row->displays_text_p = 0;
19833
19834 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19835 && (!MINI_WINDOW_P (it->w)
19836 || (minibuf_level && EQ (it->window, minibuf_window))))
19837 row->indicate_empty_line_p = 1;
19838 }
19839
19840 it->continuation_lines_width = 0;
19841 row->ends_at_zv_p = 1;
19842 /* A row that displays right-to-left text must always have
19843 its last face extended all the way to the end of line,
19844 even if this row ends in ZV, because we still write to
19845 the screen left to right. We also need to extend the
19846 last face if the default face is remapped to some
19847 different face, otherwise the functions that clear
19848 portions of the screen will clear with the default face's
19849 background color. */
19850 if (row->reversed_p
19851 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19852 extend_face_to_end_of_line (it);
19853 break;
19854 }
19855
19856 /* Now, get the metrics of what we want to display. This also
19857 generates glyphs in `row' (which is IT->glyph_row). */
19858 n_glyphs_before = row->used[TEXT_AREA];
19859 x = it->current_x;
19860
19861 /* Remember the line height so far in case the next element doesn't
19862 fit on the line. */
19863 if (it->line_wrap != TRUNCATE)
19864 {
19865 ascent = it->max_ascent;
19866 descent = it->max_descent;
19867 phys_ascent = it->max_phys_ascent;
19868 phys_descent = it->max_phys_descent;
19869
19870 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19871 {
19872 if (IT_DISPLAYING_WHITESPACE (it))
19873 may_wrap = 1;
19874 else if (may_wrap)
19875 {
19876 SAVE_IT (wrap_it, *it, wrap_data);
19877 wrap_x = x;
19878 wrap_row_used = row->used[TEXT_AREA];
19879 wrap_row_ascent = row->ascent;
19880 wrap_row_height = row->height;
19881 wrap_row_phys_ascent = row->phys_ascent;
19882 wrap_row_phys_height = row->phys_height;
19883 wrap_row_extra_line_spacing = row->extra_line_spacing;
19884 wrap_row_min_pos = min_pos;
19885 wrap_row_min_bpos = min_bpos;
19886 wrap_row_max_pos = max_pos;
19887 wrap_row_max_bpos = max_bpos;
19888 may_wrap = 0;
19889 }
19890 }
19891 }
19892
19893 PRODUCE_GLYPHS (it);
19894
19895 /* If this display element was in marginal areas, continue with
19896 the next one. */
19897 if (it->area != TEXT_AREA)
19898 {
19899 row->ascent = max (row->ascent, it->max_ascent);
19900 row->height = max (row->height, it->max_ascent + it->max_descent);
19901 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19902 row->phys_height = max (row->phys_height,
19903 it->max_phys_ascent + it->max_phys_descent);
19904 row->extra_line_spacing = max (row->extra_line_spacing,
19905 it->max_extra_line_spacing);
19906 set_iterator_to_next (it, 1);
19907 continue;
19908 }
19909
19910 /* Does the display element fit on the line? If we truncate
19911 lines, we should draw past the right edge of the window. If
19912 we don't truncate, we want to stop so that we can display the
19913 continuation glyph before the right margin. If lines are
19914 continued, there are two possible strategies for characters
19915 resulting in more than 1 glyph (e.g. tabs): Display as many
19916 glyphs as possible in this line and leave the rest for the
19917 continuation line, or display the whole element in the next
19918 line. Original redisplay did the former, so we do it also. */
19919 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19920 hpos_before = it->hpos;
19921 x_before = x;
19922
19923 if (/* Not a newline. */
19924 nglyphs > 0
19925 /* Glyphs produced fit entirely in the line. */
19926 && it->current_x < it->last_visible_x)
19927 {
19928 it->hpos += nglyphs;
19929 row->ascent = max (row->ascent, it->max_ascent);
19930 row->height = max (row->height, it->max_ascent + it->max_descent);
19931 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19932 row->phys_height = max (row->phys_height,
19933 it->max_phys_ascent + it->max_phys_descent);
19934 row->extra_line_spacing = max (row->extra_line_spacing,
19935 it->max_extra_line_spacing);
19936 if (it->current_x - it->pixel_width < it->first_visible_x)
19937 row->x = x - it->first_visible_x;
19938 /* Record the maximum and minimum buffer positions seen so
19939 far in glyphs that will be displayed by this row. */
19940 if (it->bidi_p)
19941 RECORD_MAX_MIN_POS (it);
19942 }
19943 else
19944 {
19945 int i, new_x;
19946 struct glyph *glyph;
19947
19948 for (i = 0; i < nglyphs; ++i, x = new_x)
19949 {
19950 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19951 new_x = x + glyph->pixel_width;
19952
19953 if (/* Lines are continued. */
19954 it->line_wrap != TRUNCATE
19955 && (/* Glyph doesn't fit on the line. */
19956 new_x > it->last_visible_x
19957 /* Or it fits exactly on a window system frame. */
19958 || (new_x == it->last_visible_x
19959 && FRAME_WINDOW_P (it->f)
19960 && (row->reversed_p
19961 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19962 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19963 {
19964 /* End of a continued line. */
19965
19966 if (it->hpos == 0
19967 || (new_x == it->last_visible_x
19968 && FRAME_WINDOW_P (it->f)
19969 && (row->reversed_p
19970 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19971 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19972 {
19973 /* Current glyph is the only one on the line or
19974 fits exactly on the line. We must continue
19975 the line because we can't draw the cursor
19976 after the glyph. */
19977 row->continued_p = 1;
19978 it->current_x = new_x;
19979 it->continuation_lines_width += new_x;
19980 ++it->hpos;
19981 if (i == nglyphs - 1)
19982 {
19983 /* If line-wrap is on, check if a previous
19984 wrap point was found. */
19985 if (wrap_row_used > 0
19986 /* Even if there is a previous wrap
19987 point, continue the line here as
19988 usual, if (i) the previous character
19989 was a space or tab AND (ii) the
19990 current character is not. */
19991 && (!may_wrap
19992 || IT_DISPLAYING_WHITESPACE (it)))
19993 goto back_to_wrap;
19994
19995 /* Record the maximum and minimum buffer
19996 positions seen so far in glyphs that will be
19997 displayed by this row. */
19998 if (it->bidi_p)
19999 RECORD_MAX_MIN_POS (it);
20000 set_iterator_to_next (it, 1);
20001 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20002 {
20003 if (!get_next_display_element (it))
20004 {
20005 row->exact_window_width_line_p = 1;
20006 it->continuation_lines_width = 0;
20007 row->continued_p = 0;
20008 row->ends_at_zv_p = 1;
20009 }
20010 else if (ITERATOR_AT_END_OF_LINE_P (it))
20011 {
20012 row->continued_p = 0;
20013 row->exact_window_width_line_p = 1;
20014 }
20015 }
20016 }
20017 else if (it->bidi_p)
20018 RECORD_MAX_MIN_POS (it);
20019 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20020 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20021 extend_face_to_end_of_line (it);
20022 }
20023 else if (CHAR_GLYPH_PADDING_P (*glyph)
20024 && !FRAME_WINDOW_P (it->f))
20025 {
20026 /* A padding glyph that doesn't fit on this line.
20027 This means the whole character doesn't fit
20028 on the line. */
20029 if (row->reversed_p)
20030 unproduce_glyphs (it, row->used[TEXT_AREA]
20031 - n_glyphs_before);
20032 row->used[TEXT_AREA] = n_glyphs_before;
20033
20034 /* Fill the rest of the row with continuation
20035 glyphs like in 20.x. */
20036 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20037 < row->glyphs[1 + TEXT_AREA])
20038 produce_special_glyphs (it, IT_CONTINUATION);
20039
20040 row->continued_p = 1;
20041 it->current_x = x_before;
20042 it->continuation_lines_width += x_before;
20043
20044 /* Restore the height to what it was before the
20045 element not fitting on the line. */
20046 it->max_ascent = ascent;
20047 it->max_descent = descent;
20048 it->max_phys_ascent = phys_ascent;
20049 it->max_phys_descent = phys_descent;
20050 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20051 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20052 extend_face_to_end_of_line (it);
20053 }
20054 else if (wrap_row_used > 0)
20055 {
20056 back_to_wrap:
20057 if (row->reversed_p)
20058 unproduce_glyphs (it,
20059 row->used[TEXT_AREA] - wrap_row_used);
20060 RESTORE_IT (it, &wrap_it, wrap_data);
20061 it->continuation_lines_width += wrap_x;
20062 row->used[TEXT_AREA] = wrap_row_used;
20063 row->ascent = wrap_row_ascent;
20064 row->height = wrap_row_height;
20065 row->phys_ascent = wrap_row_phys_ascent;
20066 row->phys_height = wrap_row_phys_height;
20067 row->extra_line_spacing = wrap_row_extra_line_spacing;
20068 min_pos = wrap_row_min_pos;
20069 min_bpos = wrap_row_min_bpos;
20070 max_pos = wrap_row_max_pos;
20071 max_bpos = wrap_row_max_bpos;
20072 row->continued_p = 1;
20073 row->ends_at_zv_p = 0;
20074 row->exact_window_width_line_p = 0;
20075 it->continuation_lines_width += x;
20076
20077 /* Make sure that a non-default face is extended
20078 up to the right margin of the window. */
20079 extend_face_to_end_of_line (it);
20080 }
20081 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20082 {
20083 /* A TAB that extends past the right edge of the
20084 window. This produces a single glyph on
20085 window system frames. We leave the glyph in
20086 this row and let it fill the row, but don't
20087 consume the TAB. */
20088 if ((row->reversed_p
20089 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20090 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20091 produce_special_glyphs (it, IT_CONTINUATION);
20092 it->continuation_lines_width += it->last_visible_x;
20093 row->ends_in_middle_of_char_p = 1;
20094 row->continued_p = 1;
20095 glyph->pixel_width = it->last_visible_x - x;
20096 it->starts_in_middle_of_char_p = 1;
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
20102 {
20103 /* Something other than a TAB that draws past
20104 the right edge of the window. Restore
20105 positions to values before the element. */
20106 if (row->reversed_p)
20107 unproduce_glyphs (it, row->used[TEXT_AREA]
20108 - (n_glyphs_before + i));
20109 row->used[TEXT_AREA] = n_glyphs_before + i;
20110
20111 /* Display continuation glyphs. */
20112 it->current_x = x_before;
20113 it->continuation_lines_width += x;
20114 if (!FRAME_WINDOW_P (it->f)
20115 || (row->reversed_p
20116 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20117 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20118 produce_special_glyphs (it, IT_CONTINUATION);
20119 row->continued_p = 1;
20120
20121 extend_face_to_end_of_line (it);
20122
20123 if (nglyphs > 1 && i > 0)
20124 {
20125 row->ends_in_middle_of_char_p = 1;
20126 it->starts_in_middle_of_char_p = 1;
20127 }
20128
20129 /* Restore the height to what it was before the
20130 element not fitting on the line. */
20131 it->max_ascent = ascent;
20132 it->max_descent = descent;
20133 it->max_phys_ascent = phys_ascent;
20134 it->max_phys_descent = phys_descent;
20135 }
20136
20137 break;
20138 }
20139 else if (new_x > it->first_visible_x)
20140 {
20141 /* Increment number of glyphs actually displayed. */
20142 ++it->hpos;
20143
20144 /* Record the maximum and minimum buffer positions
20145 seen so far in glyphs that will be displayed by
20146 this row. */
20147 if (it->bidi_p)
20148 RECORD_MAX_MIN_POS (it);
20149
20150 if (x < it->first_visible_x)
20151 /* Glyph is partially visible, i.e. row starts at
20152 negative X position. */
20153 row->x = x - it->first_visible_x;
20154 }
20155 else
20156 {
20157 /* Glyph is completely off the left margin of the
20158 window. This should not happen because of the
20159 move_it_in_display_line at the start of this
20160 function, unless the text display area of the
20161 window is empty. */
20162 eassert (it->first_visible_x <= it->last_visible_x);
20163 }
20164 }
20165 /* Even if this display element produced no glyphs at all,
20166 we want to record its position. */
20167 if (it->bidi_p && nglyphs == 0)
20168 RECORD_MAX_MIN_POS (it);
20169
20170 row->ascent = max (row->ascent, it->max_ascent);
20171 row->height = max (row->height, it->max_ascent + it->max_descent);
20172 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20173 row->phys_height = max (row->phys_height,
20174 it->max_phys_ascent + it->max_phys_descent);
20175 row->extra_line_spacing = max (row->extra_line_spacing,
20176 it->max_extra_line_spacing);
20177
20178 /* End of this display line if row is continued. */
20179 if (row->continued_p || row->ends_at_zv_p)
20180 break;
20181 }
20182
20183 at_end_of_line:
20184 /* Is this a line end? If yes, we're also done, after making
20185 sure that a non-default face is extended up to the right
20186 margin of the window. */
20187 if (ITERATOR_AT_END_OF_LINE_P (it))
20188 {
20189 int used_before = row->used[TEXT_AREA];
20190
20191 row->ends_in_newline_from_string_p = STRINGP (it->object);
20192
20193 /* Add a space at the end of the line that is used to
20194 display the cursor there. */
20195 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20196 append_space_for_newline (it, 0);
20197
20198 /* Extend the face to the end of the line. */
20199 extend_face_to_end_of_line (it);
20200
20201 /* Make sure we have the position. */
20202 if (used_before == 0)
20203 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20204
20205 /* Record the position of the newline, for use in
20206 find_row_edges. */
20207 it->eol_pos = it->current.pos;
20208
20209 /* Consume the line end. This skips over invisible lines. */
20210 set_iterator_to_next (it, 1);
20211 it->continuation_lines_width = 0;
20212 break;
20213 }
20214
20215 /* Proceed with next display element. Note that this skips
20216 over lines invisible because of selective display. */
20217 set_iterator_to_next (it, 1);
20218
20219 /* If we truncate lines, we are done when the last displayed
20220 glyphs reach past the right margin of the window. */
20221 if (it->line_wrap == TRUNCATE
20222 && ((FRAME_WINDOW_P (it->f)
20223 /* Images are preprocessed in produce_image_glyph such
20224 that they are cropped at the right edge of the
20225 window, so an image glyph will always end exactly at
20226 last_visible_x, even if there's no right fringe. */
20227 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20228 ? (it->current_x >= it->last_visible_x)
20229 : (it->current_x > it->last_visible_x)))
20230 {
20231 /* Maybe add truncation glyphs. */
20232 if (!FRAME_WINDOW_P (it->f)
20233 || (row->reversed_p
20234 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20235 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20236 {
20237 int i, n;
20238
20239 if (!row->reversed_p)
20240 {
20241 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20242 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20243 break;
20244 }
20245 else
20246 {
20247 for (i = 0; i < row->used[TEXT_AREA]; i++)
20248 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20249 break;
20250 /* Remove any padding glyphs at the front of ROW, to
20251 make room for the truncation glyphs we will be
20252 adding below. The loop below always inserts at
20253 least one truncation glyph, so also remove the
20254 last glyph added to ROW. */
20255 unproduce_glyphs (it, i + 1);
20256 /* Adjust i for the loop below. */
20257 i = row->used[TEXT_AREA] - (i + 1);
20258 }
20259
20260 /* produce_special_glyphs overwrites the last glyph, so
20261 we don't want that if we want to keep that last
20262 glyph, which means it's an image. */
20263 if (it->current_x > it->last_visible_x)
20264 {
20265 it->current_x = x_before;
20266 if (!FRAME_WINDOW_P (it->f))
20267 {
20268 for (n = row->used[TEXT_AREA]; i < n; ++i)
20269 {
20270 row->used[TEXT_AREA] = i;
20271 produce_special_glyphs (it, IT_TRUNCATION);
20272 }
20273 }
20274 else
20275 {
20276 row->used[TEXT_AREA] = i;
20277 produce_special_glyphs (it, IT_TRUNCATION);
20278 }
20279 it->hpos = hpos_before;
20280 }
20281 }
20282 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20283 {
20284 /* Don't truncate if we can overflow newline into fringe. */
20285 if (!get_next_display_element (it))
20286 {
20287 it->continuation_lines_width = 0;
20288 row->ends_at_zv_p = 1;
20289 row->exact_window_width_line_p = 1;
20290 break;
20291 }
20292 if (ITERATOR_AT_END_OF_LINE_P (it))
20293 {
20294 row->exact_window_width_line_p = 1;
20295 goto at_end_of_line;
20296 }
20297 it->current_x = x_before;
20298 it->hpos = hpos_before;
20299 }
20300
20301 row->truncated_on_right_p = 1;
20302 it->continuation_lines_width = 0;
20303 reseat_at_next_visible_line_start (it, 0);
20304 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20305 break;
20306 }
20307 }
20308
20309 if (wrap_data)
20310 bidi_unshelve_cache (wrap_data, 1);
20311
20312 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20313 at the left window margin. */
20314 if (it->first_visible_x
20315 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20316 {
20317 if (!FRAME_WINDOW_P (it->f)
20318 || (((row->reversed_p
20319 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20320 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20321 /* Don't let insert_left_trunc_glyphs overwrite the
20322 first glyph of the row if it is an image. */
20323 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20324 insert_left_trunc_glyphs (it);
20325 row->truncated_on_left_p = 1;
20326 }
20327
20328 /* Remember the position at which this line ends.
20329
20330 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20331 cannot be before the call to find_row_edges below, since that is
20332 where these positions are determined. */
20333 row->end = it->current;
20334 if (!it->bidi_p)
20335 {
20336 row->minpos = row->start.pos;
20337 row->maxpos = row->end.pos;
20338 }
20339 else
20340 {
20341 /* ROW->minpos and ROW->maxpos must be the smallest and
20342 `1 + the largest' buffer positions in ROW. But if ROW was
20343 bidi-reordered, these two positions can be anywhere in the
20344 row, so we must determine them now. */
20345 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20346 }
20347
20348 /* If the start of this line is the overlay arrow-position, then
20349 mark this glyph row as the one containing the overlay arrow.
20350 This is clearly a mess with variable size fonts. It would be
20351 better to let it be displayed like cursors under X. */
20352 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20353 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20354 !NILP (overlay_arrow_string)))
20355 {
20356 /* Overlay arrow in window redisplay is a fringe bitmap. */
20357 if (STRINGP (overlay_arrow_string))
20358 {
20359 struct glyph_row *arrow_row
20360 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20361 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20362 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20363 struct glyph *p = row->glyphs[TEXT_AREA];
20364 struct glyph *p2, *end;
20365
20366 /* Copy the arrow glyphs. */
20367 while (glyph < arrow_end)
20368 *p++ = *glyph++;
20369
20370 /* Throw away padding glyphs. */
20371 p2 = p;
20372 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20373 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20374 ++p2;
20375 if (p2 > p)
20376 {
20377 while (p2 < end)
20378 *p++ = *p2++;
20379 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20380 }
20381 }
20382 else
20383 {
20384 eassert (INTEGERP (overlay_arrow_string));
20385 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20386 }
20387 overlay_arrow_seen = 1;
20388 }
20389
20390 /* Highlight trailing whitespace. */
20391 if (!NILP (Vshow_trailing_whitespace))
20392 highlight_trailing_whitespace (it->f, it->glyph_row);
20393
20394 /* Compute pixel dimensions of this line. */
20395 compute_line_metrics (it);
20396
20397 /* Implementation note: No changes in the glyphs of ROW or in their
20398 faces can be done past this point, because compute_line_metrics
20399 computes ROW's hash value and stores it within the glyph_row
20400 structure. */
20401
20402 /* Record whether this row ends inside an ellipsis. */
20403 row->ends_in_ellipsis_p
20404 = (it->method == GET_FROM_DISPLAY_VECTOR
20405 && it->ellipsis_p);
20406
20407 /* Save fringe bitmaps in this row. */
20408 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20409 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20410 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20411 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20412
20413 it->left_user_fringe_bitmap = 0;
20414 it->left_user_fringe_face_id = 0;
20415 it->right_user_fringe_bitmap = 0;
20416 it->right_user_fringe_face_id = 0;
20417
20418 /* Maybe set the cursor. */
20419 cvpos = it->w->cursor.vpos;
20420 if ((cvpos < 0
20421 /* In bidi-reordered rows, keep checking for proper cursor
20422 position even if one has been found already, because buffer
20423 positions in such rows change non-linearly with ROW->VPOS,
20424 when a line is continued. One exception: when we are at ZV,
20425 display cursor on the first suitable glyph row, since all
20426 the empty rows after that also have their position set to ZV. */
20427 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20428 lines' rows is implemented for bidi-reordered rows. */
20429 || (it->bidi_p
20430 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20431 && PT >= MATRIX_ROW_START_CHARPOS (row)
20432 && PT <= MATRIX_ROW_END_CHARPOS (row)
20433 && cursor_row_p (row))
20434 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20435
20436 /* Prepare for the next line. This line starts horizontally at (X
20437 HPOS) = (0 0). Vertical positions are incremented. As a
20438 convenience for the caller, IT->glyph_row is set to the next
20439 row to be used. */
20440 it->current_x = it->hpos = 0;
20441 it->current_y += row->height;
20442 SET_TEXT_POS (it->eol_pos, 0, 0);
20443 ++it->vpos;
20444 ++it->glyph_row;
20445 /* The next row should by default use the same value of the
20446 reversed_p flag as this one. set_iterator_to_next decides when
20447 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20448 the flag accordingly. */
20449 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20450 it->glyph_row->reversed_p = row->reversed_p;
20451 it->start = row->end;
20452 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20453
20454 #undef RECORD_MAX_MIN_POS
20455 }
20456
20457 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20458 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20459 doc: /* Return paragraph direction at point in BUFFER.
20460 Value is either `left-to-right' or `right-to-left'.
20461 If BUFFER is omitted or nil, it defaults to the current buffer.
20462
20463 Paragraph direction determines how the text in the paragraph is displayed.
20464 In left-to-right paragraphs, text begins at the left margin of the window
20465 and the reading direction is generally left to right. In right-to-left
20466 paragraphs, text begins at the right margin and is read from right to left.
20467
20468 See also `bidi-paragraph-direction'. */)
20469 (Lisp_Object buffer)
20470 {
20471 struct buffer *buf = current_buffer;
20472 struct buffer *old = buf;
20473
20474 if (! NILP (buffer))
20475 {
20476 CHECK_BUFFER (buffer);
20477 buf = XBUFFER (buffer);
20478 }
20479
20480 if (NILP (BVAR (buf, bidi_display_reordering))
20481 || NILP (BVAR (buf, enable_multibyte_characters))
20482 /* When we are loading loadup.el, the character property tables
20483 needed for bidi iteration are not yet available. */
20484 || !NILP (Vpurify_flag))
20485 return Qleft_to_right;
20486 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20487 return BVAR (buf, bidi_paragraph_direction);
20488 else
20489 {
20490 /* Determine the direction from buffer text. We could try to
20491 use current_matrix if it is up to date, but this seems fast
20492 enough as it is. */
20493 struct bidi_it itb;
20494 ptrdiff_t pos = BUF_PT (buf);
20495 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20496 int c;
20497 void *itb_data = bidi_shelve_cache ();
20498
20499 set_buffer_temp (buf);
20500 /* bidi_paragraph_init finds the base direction of the paragraph
20501 by searching forward from paragraph start. We need the base
20502 direction of the current or _previous_ paragraph, so we need
20503 to make sure we are within that paragraph. To that end, find
20504 the previous non-empty line. */
20505 if (pos >= ZV && pos > BEGV)
20506 DEC_BOTH (pos, bytepos);
20507 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20508 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20509 {
20510 while ((c = FETCH_BYTE (bytepos)) == '\n'
20511 || c == ' ' || c == '\t' || c == '\f')
20512 {
20513 if (bytepos <= BEGV_BYTE)
20514 break;
20515 bytepos--;
20516 pos--;
20517 }
20518 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20519 bytepos--;
20520 }
20521 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20522 itb.paragraph_dir = NEUTRAL_DIR;
20523 itb.string.s = NULL;
20524 itb.string.lstring = Qnil;
20525 itb.string.bufpos = 0;
20526 itb.string.from_disp_str = 0;
20527 itb.string.unibyte = 0;
20528 /* We have no window to use here for ignoring window-specific
20529 overlays. Using NULL for window pointer will cause
20530 compute_display_string_pos to use the current buffer. */
20531 itb.w = NULL;
20532 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20533 bidi_unshelve_cache (itb_data, 0);
20534 set_buffer_temp (old);
20535 switch (itb.paragraph_dir)
20536 {
20537 case L2R:
20538 return Qleft_to_right;
20539 break;
20540 case R2L:
20541 return Qright_to_left;
20542 break;
20543 default:
20544 emacs_abort ();
20545 }
20546 }
20547 }
20548
20549 DEFUN ("move-point-visually", Fmove_point_visually,
20550 Smove_point_visually, 1, 1, 0,
20551 doc: /* Move point in the visual order in the specified DIRECTION.
20552 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20553 left.
20554
20555 Value is the new character position of point. */)
20556 (Lisp_Object direction)
20557 {
20558 struct window *w = XWINDOW (selected_window);
20559 struct buffer *b = XBUFFER (w->contents);
20560 struct glyph_row *row;
20561 int dir;
20562 Lisp_Object paragraph_dir;
20563
20564 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20565 (!(ROW)->continued_p \
20566 && INTEGERP ((GLYPH)->object) \
20567 && (GLYPH)->type == CHAR_GLYPH \
20568 && (GLYPH)->u.ch == ' ' \
20569 && (GLYPH)->charpos >= 0 \
20570 && !(GLYPH)->avoid_cursor_p)
20571
20572 CHECK_NUMBER (direction);
20573 dir = XINT (direction);
20574 if (dir > 0)
20575 dir = 1;
20576 else
20577 dir = -1;
20578
20579 /* If current matrix is up-to-date, we can use the information
20580 recorded in the glyphs, at least as long as the goal is on the
20581 screen. */
20582 if (w->window_end_valid
20583 && !windows_or_buffers_changed
20584 && b
20585 && !b->clip_changed
20586 && !b->prevent_redisplay_optimizations_p
20587 && !window_outdated (w)
20588 && w->cursor.vpos >= 0
20589 && w->cursor.vpos < w->current_matrix->nrows
20590 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20591 {
20592 struct glyph *g = row->glyphs[TEXT_AREA];
20593 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20594 struct glyph *gpt = g + w->cursor.hpos;
20595
20596 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20597 {
20598 if (BUFFERP (g->object) && g->charpos != PT)
20599 {
20600 SET_PT (g->charpos);
20601 w->cursor.vpos = -1;
20602 return make_number (PT);
20603 }
20604 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20605 {
20606 ptrdiff_t new_pos;
20607
20608 if (BUFFERP (gpt->object))
20609 {
20610 new_pos = PT;
20611 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20612 new_pos += (row->reversed_p ? -dir : dir);
20613 else
20614 new_pos -= (row->reversed_p ? -dir : dir);;
20615 }
20616 else if (BUFFERP (g->object))
20617 new_pos = g->charpos;
20618 else
20619 break;
20620 SET_PT (new_pos);
20621 w->cursor.vpos = -1;
20622 return make_number (PT);
20623 }
20624 else if (ROW_GLYPH_NEWLINE_P (row, g))
20625 {
20626 /* Glyphs inserted at the end of a non-empty line for
20627 positioning the cursor have zero charpos, so we must
20628 deduce the value of point by other means. */
20629 if (g->charpos > 0)
20630 SET_PT (g->charpos);
20631 else if (row->ends_at_zv_p && PT != ZV)
20632 SET_PT (ZV);
20633 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20634 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20635 else
20636 break;
20637 w->cursor.vpos = -1;
20638 return make_number (PT);
20639 }
20640 }
20641 if (g == e || INTEGERP (g->object))
20642 {
20643 if (row->truncated_on_left_p || row->truncated_on_right_p)
20644 goto simulate_display;
20645 if (!row->reversed_p)
20646 row += dir;
20647 else
20648 row -= dir;
20649 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20650 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20651 goto simulate_display;
20652
20653 if (dir > 0)
20654 {
20655 if (row->reversed_p && !row->continued_p)
20656 {
20657 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20658 w->cursor.vpos = -1;
20659 return make_number (PT);
20660 }
20661 g = row->glyphs[TEXT_AREA];
20662 e = g + row->used[TEXT_AREA];
20663 for ( ; g < e; g++)
20664 {
20665 if (BUFFERP (g->object)
20666 /* Empty lines have only one glyph, which stands
20667 for the newline, and whose charpos is the
20668 buffer position of the newline. */
20669 || ROW_GLYPH_NEWLINE_P (row, g)
20670 /* When the buffer ends in a newline, the line at
20671 EOB also has one glyph, but its charpos is -1. */
20672 || (row->ends_at_zv_p
20673 && !row->reversed_p
20674 && INTEGERP (g->object)
20675 && g->type == CHAR_GLYPH
20676 && g->u.ch == ' '))
20677 {
20678 if (g->charpos > 0)
20679 SET_PT (g->charpos);
20680 else if (!row->reversed_p
20681 && row->ends_at_zv_p
20682 && PT != ZV)
20683 SET_PT (ZV);
20684 else
20685 continue;
20686 w->cursor.vpos = -1;
20687 return make_number (PT);
20688 }
20689 }
20690 }
20691 else
20692 {
20693 if (!row->reversed_p && !row->continued_p)
20694 {
20695 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20696 w->cursor.vpos = -1;
20697 return make_number (PT);
20698 }
20699 e = row->glyphs[TEXT_AREA];
20700 g = e + row->used[TEXT_AREA] - 1;
20701 for ( ; g >= e; g--)
20702 {
20703 if (BUFFERP (g->object)
20704 || (ROW_GLYPH_NEWLINE_P (row, g)
20705 && g->charpos > 0)
20706 /* Empty R2L lines on GUI frames have the buffer
20707 position of the newline stored in the stretch
20708 glyph. */
20709 || g->type == STRETCH_GLYPH
20710 || (row->ends_at_zv_p
20711 && row->reversed_p
20712 && INTEGERP (g->object)
20713 && g->type == CHAR_GLYPH
20714 && g->u.ch == ' '))
20715 {
20716 if (g->charpos > 0)
20717 SET_PT (g->charpos);
20718 else if (row->reversed_p
20719 && row->ends_at_zv_p
20720 && PT != ZV)
20721 SET_PT (ZV);
20722 else
20723 continue;
20724 w->cursor.vpos = -1;
20725 return make_number (PT);
20726 }
20727 }
20728 }
20729 }
20730 }
20731
20732 simulate_display:
20733
20734 /* If we wind up here, we failed to move by using the glyphs, so we
20735 need to simulate display instead. */
20736
20737 if (b)
20738 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20739 else
20740 paragraph_dir = Qleft_to_right;
20741 if (EQ (paragraph_dir, Qright_to_left))
20742 dir = -dir;
20743 if (PT <= BEGV && dir < 0)
20744 xsignal0 (Qbeginning_of_buffer);
20745 else if (PT >= ZV && dir > 0)
20746 xsignal0 (Qend_of_buffer);
20747 else
20748 {
20749 struct text_pos pt;
20750 struct it it;
20751 int pt_x, target_x, pixel_width, pt_vpos;
20752 bool at_eol_p;
20753 bool overshoot_expected = false;
20754 bool target_is_eol_p = false;
20755
20756 /* Setup the arena. */
20757 SET_TEXT_POS (pt, PT, PT_BYTE);
20758 start_display (&it, w, pt);
20759
20760 if (it.cmp_it.id < 0
20761 && it.method == GET_FROM_STRING
20762 && it.area == TEXT_AREA
20763 && it.string_from_display_prop_p
20764 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20765 overshoot_expected = true;
20766
20767 /* Find the X coordinate of point. We start from the beginning
20768 of this or previous line to make sure we are before point in
20769 the logical order (since the move_it_* functions can only
20770 move forward). */
20771 reseat:
20772 reseat_at_previous_visible_line_start (&it);
20773 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20774 if (IT_CHARPOS (it) != PT)
20775 {
20776 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20777 -1, -1, -1, MOVE_TO_POS);
20778 /* If we missed point because the character there is
20779 displayed out of a display vector that has more than one
20780 glyph, retry expecting overshoot. */
20781 if (it.method == GET_FROM_DISPLAY_VECTOR
20782 && it.current.dpvec_index > 0
20783 && !overshoot_expected)
20784 {
20785 overshoot_expected = true;
20786 goto reseat;
20787 }
20788 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20789 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20790 }
20791 pt_x = it.current_x;
20792 pt_vpos = it.vpos;
20793 if (dir > 0 || overshoot_expected)
20794 {
20795 struct glyph_row *row = it.glyph_row;
20796
20797 /* When point is at beginning of line, we don't have
20798 information about the glyph there loaded into struct
20799 it. Calling get_next_display_element fixes that. */
20800 if (pt_x == 0)
20801 get_next_display_element (&it);
20802 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20803 it.glyph_row = NULL;
20804 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20805 it.glyph_row = row;
20806 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20807 it, lest it will become out of sync with it's buffer
20808 position. */
20809 it.current_x = pt_x;
20810 }
20811 else
20812 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20813 pixel_width = it.pixel_width;
20814 if (overshoot_expected && at_eol_p)
20815 pixel_width = 0;
20816 else if (pixel_width <= 0)
20817 pixel_width = 1;
20818
20819 /* If there's a display string (or something similar) at point,
20820 we are actually at the glyph to the left of point, so we need
20821 to correct the X coordinate. */
20822 if (overshoot_expected)
20823 {
20824 if (it.bidi_p)
20825 pt_x += pixel_width * it.bidi_it.scan_dir;
20826 else
20827 pt_x += pixel_width;
20828 }
20829
20830 /* Compute target X coordinate, either to the left or to the
20831 right of point. On TTY frames, all characters have the same
20832 pixel width of 1, so we can use that. On GUI frames we don't
20833 have an easy way of getting at the pixel width of the
20834 character to the left of point, so we use a different method
20835 of getting to that place. */
20836 if (dir > 0)
20837 target_x = pt_x + pixel_width;
20838 else
20839 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20840
20841 /* Target X coordinate could be one line above or below the line
20842 of point, in which case we need to adjust the target X
20843 coordinate. Also, if moving to the left, we need to begin at
20844 the left edge of the point's screen line. */
20845 if (dir < 0)
20846 {
20847 if (pt_x > 0)
20848 {
20849 start_display (&it, w, pt);
20850 reseat_at_previous_visible_line_start (&it);
20851 it.current_x = it.current_y = it.hpos = 0;
20852 if (pt_vpos != 0)
20853 move_it_by_lines (&it, pt_vpos);
20854 }
20855 else
20856 {
20857 move_it_by_lines (&it, -1);
20858 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20859 target_is_eol_p = true;
20860 }
20861 }
20862 else
20863 {
20864 if (at_eol_p
20865 || (target_x >= it.last_visible_x
20866 && it.line_wrap != TRUNCATE))
20867 {
20868 if (pt_x > 0)
20869 move_it_by_lines (&it, 0);
20870 move_it_by_lines (&it, 1);
20871 target_x = 0;
20872 }
20873 }
20874
20875 /* Move to the target X coordinate. */
20876 #ifdef HAVE_WINDOW_SYSTEM
20877 /* On GUI frames, as we don't know the X coordinate of the
20878 character to the left of point, moving point to the left
20879 requires walking, one grapheme cluster at a time, until we
20880 find ourself at a place immediately to the left of the
20881 character at point. */
20882 if (FRAME_WINDOW_P (it.f) && dir < 0)
20883 {
20884 struct text_pos new_pos;
20885 enum move_it_result rc = MOVE_X_REACHED;
20886
20887 if (it.current_x == 0)
20888 get_next_display_element (&it);
20889 if (it.what == IT_COMPOSITION)
20890 {
20891 new_pos.charpos = it.cmp_it.charpos;
20892 new_pos.bytepos = -1;
20893 }
20894 else
20895 new_pos = it.current.pos;
20896
20897 while (it.current_x + it.pixel_width <= target_x
20898 && rc == MOVE_X_REACHED)
20899 {
20900 int new_x = it.current_x + it.pixel_width;
20901
20902 /* For composed characters, we want the position of the
20903 first character in the grapheme cluster (usually, the
20904 composition's base character), whereas it.current
20905 might give us the position of the _last_ one, e.g. if
20906 the composition is rendered in reverse due to bidi
20907 reordering. */
20908 if (it.what == IT_COMPOSITION)
20909 {
20910 new_pos.charpos = it.cmp_it.charpos;
20911 new_pos.bytepos = -1;
20912 }
20913 else
20914 new_pos = it.current.pos;
20915 if (new_x == it.current_x)
20916 new_x++;
20917 rc = move_it_in_display_line_to (&it, ZV, new_x,
20918 MOVE_TO_POS | MOVE_TO_X);
20919 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20920 break;
20921 }
20922 /* The previous position we saw in the loop is the one we
20923 want. */
20924 if (new_pos.bytepos == -1)
20925 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20926 it.current.pos = new_pos;
20927 }
20928 else
20929 #endif
20930 if (it.current_x != target_x)
20931 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20932
20933 /* When lines are truncated, the above loop will stop at the
20934 window edge. But we want to get to the end of line, even if
20935 it is beyond the window edge; automatic hscroll will then
20936 scroll the window to show point as appropriate. */
20937 if (target_is_eol_p && it.line_wrap == TRUNCATE
20938 && get_next_display_element (&it))
20939 {
20940 struct text_pos new_pos = it.current.pos;
20941
20942 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20943 {
20944 set_iterator_to_next (&it, 0);
20945 if (it.method == GET_FROM_BUFFER)
20946 new_pos = it.current.pos;
20947 if (!get_next_display_element (&it))
20948 break;
20949 }
20950
20951 it.current.pos = new_pos;
20952 }
20953
20954 /* If we ended up in a display string that covers point, move to
20955 buffer position to the right in the visual order. */
20956 if (dir > 0)
20957 {
20958 while (IT_CHARPOS (it) == PT)
20959 {
20960 set_iterator_to_next (&it, 0);
20961 if (!get_next_display_element (&it))
20962 break;
20963 }
20964 }
20965
20966 /* Move point to that position. */
20967 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20968 }
20969
20970 return make_number (PT);
20971
20972 #undef ROW_GLYPH_NEWLINE_P
20973 }
20974
20975 \f
20976 /***********************************************************************
20977 Menu Bar
20978 ***********************************************************************/
20979
20980 /* Redisplay the menu bar in the frame for window W.
20981
20982 The menu bar of X frames that don't have X toolkit support is
20983 displayed in a special window W->frame->menu_bar_window.
20984
20985 The menu bar of terminal frames is treated specially as far as
20986 glyph matrices are concerned. Menu bar lines are not part of
20987 windows, so the update is done directly on the frame matrix rows
20988 for the menu bar. */
20989
20990 static void
20991 display_menu_bar (struct window *w)
20992 {
20993 struct frame *f = XFRAME (WINDOW_FRAME (w));
20994 struct it it;
20995 Lisp_Object items;
20996 int i;
20997
20998 /* Don't do all this for graphical frames. */
20999 #ifdef HAVE_NTGUI
21000 if (FRAME_W32_P (f))
21001 return;
21002 #endif
21003 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21004 if (FRAME_X_P (f))
21005 return;
21006 #endif
21007
21008 #ifdef HAVE_NS
21009 if (FRAME_NS_P (f))
21010 return;
21011 #endif /* HAVE_NS */
21012
21013 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21014 eassert (!FRAME_WINDOW_P (f));
21015 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21016 it.first_visible_x = 0;
21017 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21018 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21019 if (FRAME_WINDOW_P (f))
21020 {
21021 /* Menu bar lines are displayed in the desired matrix of the
21022 dummy window menu_bar_window. */
21023 struct window *menu_w;
21024 menu_w = XWINDOW (f->menu_bar_window);
21025 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21026 MENU_FACE_ID);
21027 it.first_visible_x = 0;
21028 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21029 }
21030 else
21031 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21032 {
21033 /* This is a TTY frame, i.e. character hpos/vpos are used as
21034 pixel x/y. */
21035 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21036 MENU_FACE_ID);
21037 it.first_visible_x = 0;
21038 it.last_visible_x = FRAME_COLS (f);
21039 }
21040
21041 /* FIXME: This should be controlled by a user option. See the
21042 comments in redisplay_tool_bar and display_mode_line about
21043 this. */
21044 it.paragraph_embedding = L2R;
21045
21046 /* Clear all rows of the menu bar. */
21047 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21048 {
21049 struct glyph_row *row = it.glyph_row + i;
21050 clear_glyph_row (row);
21051 row->enabled_p = true;
21052 row->full_width_p = 1;
21053 }
21054
21055 /* Display all items of the menu bar. */
21056 items = FRAME_MENU_BAR_ITEMS (it.f);
21057 for (i = 0; i < ASIZE (items); i += 4)
21058 {
21059 Lisp_Object string;
21060
21061 /* Stop at nil string. */
21062 string = AREF (items, i + 1);
21063 if (NILP (string))
21064 break;
21065
21066 /* Remember where item was displayed. */
21067 ASET (items, i + 3, make_number (it.hpos));
21068
21069 /* Display the item, pad with one space. */
21070 if (it.current_x < it.last_visible_x)
21071 display_string (NULL, string, Qnil, 0, 0, &it,
21072 SCHARS (string) + 1, 0, 0, -1);
21073 }
21074
21075 /* Fill out the line with spaces. */
21076 if (it.current_x < it.last_visible_x)
21077 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21078
21079 /* Compute the total height of the lines. */
21080 compute_line_metrics (&it);
21081 }
21082
21083 /* Deep copy of a glyph row, including the glyphs. */
21084 static void
21085 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21086 {
21087 struct glyph *pointers[1 + LAST_AREA];
21088 int to_used = to->used[TEXT_AREA];
21089
21090 /* Save glyph pointers of TO. */
21091 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21092
21093 /* Do a structure assignment. */
21094 *to = *from;
21095
21096 /* Restore original glyph pointers of TO. */
21097 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21098
21099 /* Copy the glyphs. */
21100 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21101 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21102
21103 /* If we filled only part of the TO row, fill the rest with
21104 space_glyph (which will display as empty space). */
21105 if (to_used > from->used[TEXT_AREA])
21106 fill_up_frame_row_with_spaces (to, to_used);
21107 }
21108
21109 /* Display one menu item on a TTY, by overwriting the glyphs in the
21110 frame F's desired glyph matrix with glyphs produced from the menu
21111 item text. Called from term.c to display TTY drop-down menus one
21112 item at a time.
21113
21114 ITEM_TEXT is the menu item text as a C string.
21115
21116 FACE_ID is the face ID to be used for this menu item. FACE_ID
21117 could specify one of 3 faces: a face for an enabled item, a face
21118 for a disabled item, or a face for a selected item.
21119
21120 X and Y are coordinates of the first glyph in the frame's desired
21121 matrix to be overwritten by the menu item. Since this is a TTY, Y
21122 is the zero-based number of the glyph row and X is the zero-based
21123 glyph number in the row, starting from left, where to start
21124 displaying the item.
21125
21126 SUBMENU non-zero means this menu item drops down a submenu, which
21127 should be indicated by displaying a proper visual cue after the
21128 item text. */
21129
21130 void
21131 display_tty_menu_item (const char *item_text, int width, int face_id,
21132 int x, int y, int submenu)
21133 {
21134 struct it it;
21135 struct frame *f = SELECTED_FRAME ();
21136 struct window *w = XWINDOW (f->selected_window);
21137 int saved_used, saved_truncated, saved_width, saved_reversed;
21138 struct glyph_row *row;
21139 size_t item_len = strlen (item_text);
21140
21141 eassert (FRAME_TERMCAP_P (f));
21142
21143 /* Don't write beyond the matrix's last row. This can happen for
21144 TTY screens that are not high enough to show the entire menu.
21145 (This is actually a bit of defensive programming, as
21146 tty_menu_display already limits the number of menu items to one
21147 less than the number of screen lines.) */
21148 if (y >= f->desired_matrix->nrows)
21149 return;
21150
21151 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21152 it.first_visible_x = 0;
21153 it.last_visible_x = FRAME_COLS (f) - 1;
21154 row = it.glyph_row;
21155 /* Start with the row contents from the current matrix. */
21156 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21157 saved_width = row->full_width_p;
21158 row->full_width_p = 1;
21159 saved_reversed = row->reversed_p;
21160 row->reversed_p = 0;
21161 row->enabled_p = true;
21162
21163 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21164 desired face. */
21165 eassert (x < f->desired_matrix->matrix_w);
21166 it.current_x = it.hpos = x;
21167 it.current_y = it.vpos = y;
21168 saved_used = row->used[TEXT_AREA];
21169 saved_truncated = row->truncated_on_right_p;
21170 row->used[TEXT_AREA] = x;
21171 it.face_id = face_id;
21172 it.line_wrap = TRUNCATE;
21173
21174 /* FIXME: This should be controlled by a user option. See the
21175 comments in redisplay_tool_bar and display_mode_line about this.
21176 Also, if paragraph_embedding could ever be R2L, changes will be
21177 needed to avoid shifting to the right the row characters in
21178 term.c:append_glyph. */
21179 it.paragraph_embedding = L2R;
21180
21181 /* Pad with a space on the left. */
21182 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21183 width--;
21184 /* Display the menu item, pad with spaces to WIDTH. */
21185 if (submenu)
21186 {
21187 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21188 item_len, 0, FRAME_COLS (f) - 1, -1);
21189 width -= item_len;
21190 /* Indicate with " >" that there's a submenu. */
21191 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21192 FRAME_COLS (f) - 1, -1);
21193 }
21194 else
21195 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21196 width, 0, FRAME_COLS (f) - 1, -1);
21197
21198 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21199 row->truncated_on_right_p = saved_truncated;
21200 row->hash = row_hash (row);
21201 row->full_width_p = saved_width;
21202 row->reversed_p = saved_reversed;
21203 }
21204 \f
21205 /***********************************************************************
21206 Mode Line
21207 ***********************************************************************/
21208
21209 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21210 FORCE is non-zero, redisplay mode lines unconditionally.
21211 Otherwise, redisplay only mode lines that are garbaged. Value is
21212 the number of windows whose mode lines were redisplayed. */
21213
21214 static int
21215 redisplay_mode_lines (Lisp_Object window, bool force)
21216 {
21217 int nwindows = 0;
21218
21219 while (!NILP (window))
21220 {
21221 struct window *w = XWINDOW (window);
21222
21223 if (WINDOWP (w->contents))
21224 nwindows += redisplay_mode_lines (w->contents, force);
21225 else if (force
21226 || FRAME_GARBAGED_P (XFRAME (w->frame))
21227 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21228 {
21229 struct text_pos lpoint;
21230 struct buffer *old = current_buffer;
21231
21232 /* Set the window's buffer for the mode line display. */
21233 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21234 set_buffer_internal_1 (XBUFFER (w->contents));
21235
21236 /* Point refers normally to the selected window. For any
21237 other window, set up appropriate value. */
21238 if (!EQ (window, selected_window))
21239 {
21240 struct text_pos pt;
21241
21242 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21243 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21244 }
21245
21246 /* Display mode lines. */
21247 clear_glyph_matrix (w->desired_matrix);
21248 if (display_mode_lines (w))
21249 ++nwindows;
21250
21251 /* Restore old settings. */
21252 set_buffer_internal_1 (old);
21253 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21254 }
21255
21256 window = w->next;
21257 }
21258
21259 return nwindows;
21260 }
21261
21262
21263 /* Display the mode and/or header line of window W. Value is the
21264 sum number of mode lines and header lines displayed. */
21265
21266 static int
21267 display_mode_lines (struct window *w)
21268 {
21269 Lisp_Object old_selected_window = selected_window;
21270 Lisp_Object old_selected_frame = selected_frame;
21271 Lisp_Object new_frame = w->frame;
21272 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21273 int n = 0;
21274
21275 selected_frame = new_frame;
21276 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21277 or window's point, then we'd need select_window_1 here as well. */
21278 XSETWINDOW (selected_window, w);
21279 XFRAME (new_frame)->selected_window = selected_window;
21280
21281 /* These will be set while the mode line specs are processed. */
21282 line_number_displayed = 0;
21283 w->column_number_displayed = -1;
21284
21285 if (WINDOW_WANTS_MODELINE_P (w))
21286 {
21287 struct window *sel_w = XWINDOW (old_selected_window);
21288
21289 /* Select mode line face based on the real selected window. */
21290 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21291 BVAR (current_buffer, mode_line_format));
21292 ++n;
21293 }
21294
21295 if (WINDOW_WANTS_HEADER_LINE_P (w))
21296 {
21297 display_mode_line (w, HEADER_LINE_FACE_ID,
21298 BVAR (current_buffer, header_line_format));
21299 ++n;
21300 }
21301
21302 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21303 selected_frame = old_selected_frame;
21304 selected_window = old_selected_window;
21305 if (n > 0)
21306 w->must_be_updated_p = true;
21307 return n;
21308 }
21309
21310
21311 /* Display mode or header line of window W. FACE_ID specifies which
21312 line to display; it is either MODE_LINE_FACE_ID or
21313 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21314 display. Value is the pixel height of the mode/header line
21315 displayed. */
21316
21317 static int
21318 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21319 {
21320 struct it it;
21321 struct face *face;
21322 ptrdiff_t count = SPECPDL_INDEX ();
21323
21324 init_iterator (&it, w, -1, -1, NULL, face_id);
21325 /* Don't extend on a previously drawn mode-line.
21326 This may happen if called from pos_visible_p. */
21327 it.glyph_row->enabled_p = false;
21328 prepare_desired_row (it.glyph_row);
21329
21330 it.glyph_row->mode_line_p = 1;
21331
21332 /* FIXME: This should be controlled by a user option. But
21333 supporting such an option is not trivial, since the mode line is
21334 made up of many separate strings. */
21335 it.paragraph_embedding = L2R;
21336
21337 record_unwind_protect (unwind_format_mode_line,
21338 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21339
21340 mode_line_target = MODE_LINE_DISPLAY;
21341
21342 /* Temporarily make frame's keyboard the current kboard so that
21343 kboard-local variables in the mode_line_format will get the right
21344 values. */
21345 push_kboard (FRAME_KBOARD (it.f));
21346 record_unwind_save_match_data ();
21347 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21348 pop_kboard ();
21349
21350 unbind_to (count, Qnil);
21351
21352 /* Fill up with spaces. */
21353 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21354
21355 compute_line_metrics (&it);
21356 it.glyph_row->full_width_p = 1;
21357 it.glyph_row->continued_p = 0;
21358 it.glyph_row->truncated_on_left_p = 0;
21359 it.glyph_row->truncated_on_right_p = 0;
21360
21361 /* Make a 3D mode-line have a shadow at its right end. */
21362 face = FACE_FROM_ID (it.f, face_id);
21363 extend_face_to_end_of_line (&it);
21364 if (face->box != FACE_NO_BOX)
21365 {
21366 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21367 + it.glyph_row->used[TEXT_AREA] - 1);
21368 last->right_box_line_p = 1;
21369 }
21370
21371 return it.glyph_row->height;
21372 }
21373
21374 /* Move element ELT in LIST to the front of LIST.
21375 Return the updated list. */
21376
21377 static Lisp_Object
21378 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21379 {
21380 register Lisp_Object tail, prev;
21381 register Lisp_Object tem;
21382
21383 tail = list;
21384 prev = Qnil;
21385 while (CONSP (tail))
21386 {
21387 tem = XCAR (tail);
21388
21389 if (EQ (elt, tem))
21390 {
21391 /* Splice out the link TAIL. */
21392 if (NILP (prev))
21393 list = XCDR (tail);
21394 else
21395 Fsetcdr (prev, XCDR (tail));
21396
21397 /* Now make it the first. */
21398 Fsetcdr (tail, list);
21399 return tail;
21400 }
21401 else
21402 prev = tail;
21403 tail = XCDR (tail);
21404 QUIT;
21405 }
21406
21407 /* Not found--return unchanged LIST. */
21408 return list;
21409 }
21410
21411 /* Contribute ELT to the mode line for window IT->w. How it
21412 translates into text depends on its data type.
21413
21414 IT describes the display environment in which we display, as usual.
21415
21416 DEPTH is the depth in recursion. It is used to prevent
21417 infinite recursion here.
21418
21419 FIELD_WIDTH is the number of characters the display of ELT should
21420 occupy in the mode line, and PRECISION is the maximum number of
21421 characters to display from ELT's representation. See
21422 display_string for details.
21423
21424 Returns the hpos of the end of the text generated by ELT.
21425
21426 PROPS is a property list to add to any string we encounter.
21427
21428 If RISKY is nonzero, remove (disregard) any properties in any string
21429 we encounter, and ignore :eval and :propertize.
21430
21431 The global variable `mode_line_target' determines whether the
21432 output is passed to `store_mode_line_noprop',
21433 `store_mode_line_string', or `display_string'. */
21434
21435 static int
21436 display_mode_element (struct it *it, int depth, int field_width, int precision,
21437 Lisp_Object elt, Lisp_Object props, int risky)
21438 {
21439 int n = 0, field, prec;
21440 int literal = 0;
21441
21442 tail_recurse:
21443 if (depth > 100)
21444 elt = build_string ("*too-deep*");
21445
21446 depth++;
21447
21448 switch (XTYPE (elt))
21449 {
21450 case Lisp_String:
21451 {
21452 /* A string: output it and check for %-constructs within it. */
21453 unsigned char c;
21454 ptrdiff_t offset = 0;
21455
21456 if (SCHARS (elt) > 0
21457 && (!NILP (props) || risky))
21458 {
21459 Lisp_Object oprops, aelt;
21460 oprops = Ftext_properties_at (make_number (0), elt);
21461
21462 /* If the starting string's properties are not what
21463 we want, translate the string. Also, if the string
21464 is risky, do that anyway. */
21465
21466 if (NILP (Fequal (props, oprops)) || risky)
21467 {
21468 /* If the starting string has properties,
21469 merge the specified ones onto the existing ones. */
21470 if (! NILP (oprops) && !risky)
21471 {
21472 Lisp_Object tem;
21473
21474 oprops = Fcopy_sequence (oprops);
21475 tem = props;
21476 while (CONSP (tem))
21477 {
21478 oprops = Fplist_put (oprops, XCAR (tem),
21479 XCAR (XCDR (tem)));
21480 tem = XCDR (XCDR (tem));
21481 }
21482 props = oprops;
21483 }
21484
21485 aelt = Fassoc (elt, mode_line_proptrans_alist);
21486 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21487 {
21488 /* AELT is what we want. Move it to the front
21489 without consing. */
21490 elt = XCAR (aelt);
21491 mode_line_proptrans_alist
21492 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21493 }
21494 else
21495 {
21496 Lisp_Object tem;
21497
21498 /* If AELT has the wrong props, it is useless.
21499 so get rid of it. */
21500 if (! NILP (aelt))
21501 mode_line_proptrans_alist
21502 = Fdelq (aelt, mode_line_proptrans_alist);
21503
21504 elt = Fcopy_sequence (elt);
21505 Fset_text_properties (make_number (0), Flength (elt),
21506 props, elt);
21507 /* Add this item to mode_line_proptrans_alist. */
21508 mode_line_proptrans_alist
21509 = Fcons (Fcons (elt, props),
21510 mode_line_proptrans_alist);
21511 /* Truncate mode_line_proptrans_alist
21512 to at most 50 elements. */
21513 tem = Fnthcdr (make_number (50),
21514 mode_line_proptrans_alist);
21515 if (! NILP (tem))
21516 XSETCDR (tem, Qnil);
21517 }
21518 }
21519 }
21520
21521 offset = 0;
21522
21523 if (literal)
21524 {
21525 prec = precision - n;
21526 switch (mode_line_target)
21527 {
21528 case MODE_LINE_NOPROP:
21529 case MODE_LINE_TITLE:
21530 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21531 break;
21532 case MODE_LINE_STRING:
21533 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21534 break;
21535 case MODE_LINE_DISPLAY:
21536 n += display_string (NULL, elt, Qnil, 0, 0, it,
21537 0, prec, 0, STRING_MULTIBYTE (elt));
21538 break;
21539 }
21540
21541 break;
21542 }
21543
21544 /* Handle the non-literal case. */
21545
21546 while ((precision <= 0 || n < precision)
21547 && SREF (elt, offset) != 0
21548 && (mode_line_target != MODE_LINE_DISPLAY
21549 || it->current_x < it->last_visible_x))
21550 {
21551 ptrdiff_t last_offset = offset;
21552
21553 /* Advance to end of string or next format specifier. */
21554 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21555 ;
21556
21557 if (offset - 1 != last_offset)
21558 {
21559 ptrdiff_t nchars, nbytes;
21560
21561 /* Output to end of string or up to '%'. Field width
21562 is length of string. Don't output more than
21563 PRECISION allows us. */
21564 offset--;
21565
21566 prec = c_string_width (SDATA (elt) + last_offset,
21567 offset - last_offset, precision - n,
21568 &nchars, &nbytes);
21569
21570 switch (mode_line_target)
21571 {
21572 case MODE_LINE_NOPROP:
21573 case MODE_LINE_TITLE:
21574 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21575 break;
21576 case MODE_LINE_STRING:
21577 {
21578 ptrdiff_t bytepos = last_offset;
21579 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21580 ptrdiff_t endpos = (precision <= 0
21581 ? string_byte_to_char (elt, offset)
21582 : charpos + nchars);
21583
21584 n += store_mode_line_string (NULL,
21585 Fsubstring (elt, make_number (charpos),
21586 make_number (endpos)),
21587 0, 0, 0, Qnil);
21588 }
21589 break;
21590 case MODE_LINE_DISPLAY:
21591 {
21592 ptrdiff_t bytepos = last_offset;
21593 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21594
21595 if (precision <= 0)
21596 nchars = string_byte_to_char (elt, offset) - charpos;
21597 n += display_string (NULL, elt, Qnil, 0, charpos,
21598 it, 0, nchars, 0,
21599 STRING_MULTIBYTE (elt));
21600 }
21601 break;
21602 }
21603 }
21604 else /* c == '%' */
21605 {
21606 ptrdiff_t percent_position = offset;
21607
21608 /* Get the specified minimum width. Zero means
21609 don't pad. */
21610 field = 0;
21611 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21612 field = field * 10 + c - '0';
21613
21614 /* Don't pad beyond the total padding allowed. */
21615 if (field_width - n > 0 && field > field_width - n)
21616 field = field_width - n;
21617
21618 /* Note that either PRECISION <= 0 or N < PRECISION. */
21619 prec = precision - n;
21620
21621 if (c == 'M')
21622 n += display_mode_element (it, depth, field, prec,
21623 Vglobal_mode_string, props,
21624 risky);
21625 else if (c != 0)
21626 {
21627 bool multibyte;
21628 ptrdiff_t bytepos, charpos;
21629 const char *spec;
21630 Lisp_Object string;
21631
21632 bytepos = percent_position;
21633 charpos = (STRING_MULTIBYTE (elt)
21634 ? string_byte_to_char (elt, bytepos)
21635 : bytepos);
21636 spec = decode_mode_spec (it->w, c, field, &string);
21637 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21638
21639 switch (mode_line_target)
21640 {
21641 case MODE_LINE_NOPROP:
21642 case MODE_LINE_TITLE:
21643 n += store_mode_line_noprop (spec, field, prec);
21644 break;
21645 case MODE_LINE_STRING:
21646 {
21647 Lisp_Object tem = build_string (spec);
21648 props = Ftext_properties_at (make_number (charpos), elt);
21649 /* Should only keep face property in props */
21650 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21651 }
21652 break;
21653 case MODE_LINE_DISPLAY:
21654 {
21655 int nglyphs_before, nwritten;
21656
21657 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21658 nwritten = display_string (spec, string, elt,
21659 charpos, 0, it,
21660 field, prec, 0,
21661 multibyte);
21662
21663 /* Assign to the glyphs written above the
21664 string where the `%x' came from, position
21665 of the `%'. */
21666 if (nwritten > 0)
21667 {
21668 struct glyph *glyph
21669 = (it->glyph_row->glyphs[TEXT_AREA]
21670 + nglyphs_before);
21671 int i;
21672
21673 for (i = 0; i < nwritten; ++i)
21674 {
21675 glyph[i].object = elt;
21676 glyph[i].charpos = charpos;
21677 }
21678
21679 n += nwritten;
21680 }
21681 }
21682 break;
21683 }
21684 }
21685 else /* c == 0 */
21686 break;
21687 }
21688 }
21689 }
21690 break;
21691
21692 case Lisp_Symbol:
21693 /* A symbol: process the value of the symbol recursively
21694 as if it appeared here directly. Avoid error if symbol void.
21695 Special case: if value of symbol is a string, output the string
21696 literally. */
21697 {
21698 register Lisp_Object tem;
21699
21700 /* If the variable is not marked as risky to set
21701 then its contents are risky to use. */
21702 if (NILP (Fget (elt, Qrisky_local_variable)))
21703 risky = 1;
21704
21705 tem = Fboundp (elt);
21706 if (!NILP (tem))
21707 {
21708 tem = Fsymbol_value (elt);
21709 /* If value is a string, output that string literally:
21710 don't check for % within it. */
21711 if (STRINGP (tem))
21712 literal = 1;
21713
21714 if (!EQ (tem, elt))
21715 {
21716 /* Give up right away for nil or t. */
21717 elt = tem;
21718 goto tail_recurse;
21719 }
21720 }
21721 }
21722 break;
21723
21724 case Lisp_Cons:
21725 {
21726 register Lisp_Object car, tem;
21727
21728 /* A cons cell: five distinct cases.
21729 If first element is :eval or :propertize, do something special.
21730 If first element is a string or a cons, process all the elements
21731 and effectively concatenate them.
21732 If first element is a negative number, truncate displaying cdr to
21733 at most that many characters. If positive, pad (with spaces)
21734 to at least that many characters.
21735 If first element is a symbol, process the cadr or caddr recursively
21736 according to whether the symbol's value is non-nil or nil. */
21737 car = XCAR (elt);
21738 if (EQ (car, QCeval))
21739 {
21740 /* An element of the form (:eval FORM) means evaluate FORM
21741 and use the result as mode line elements. */
21742
21743 if (risky)
21744 break;
21745
21746 if (CONSP (XCDR (elt)))
21747 {
21748 Lisp_Object spec;
21749 spec = safe_eval (XCAR (XCDR (elt)));
21750 n += display_mode_element (it, depth, field_width - n,
21751 precision - n, spec, props,
21752 risky);
21753 }
21754 }
21755 else if (EQ (car, QCpropertize))
21756 {
21757 /* An element of the form (:propertize ELT PROPS...)
21758 means display ELT but applying properties PROPS. */
21759
21760 if (risky)
21761 break;
21762
21763 if (CONSP (XCDR (elt)))
21764 n += display_mode_element (it, depth, field_width - n,
21765 precision - n, XCAR (XCDR (elt)),
21766 XCDR (XCDR (elt)), risky);
21767 }
21768 else if (SYMBOLP (car))
21769 {
21770 tem = Fboundp (car);
21771 elt = XCDR (elt);
21772 if (!CONSP (elt))
21773 goto invalid;
21774 /* elt is now the cdr, and we know it is a cons cell.
21775 Use its car if CAR has a non-nil value. */
21776 if (!NILP (tem))
21777 {
21778 tem = Fsymbol_value (car);
21779 if (!NILP (tem))
21780 {
21781 elt = XCAR (elt);
21782 goto tail_recurse;
21783 }
21784 }
21785 /* Symbol's value is nil (or symbol is unbound)
21786 Get the cddr of the original list
21787 and if possible find the caddr and use that. */
21788 elt = XCDR (elt);
21789 if (NILP (elt))
21790 break;
21791 else if (!CONSP (elt))
21792 goto invalid;
21793 elt = XCAR (elt);
21794 goto tail_recurse;
21795 }
21796 else if (INTEGERP (car))
21797 {
21798 register int lim = XINT (car);
21799 elt = XCDR (elt);
21800 if (lim < 0)
21801 {
21802 /* Negative int means reduce maximum width. */
21803 if (precision <= 0)
21804 precision = -lim;
21805 else
21806 precision = min (precision, -lim);
21807 }
21808 else if (lim > 0)
21809 {
21810 /* Padding specified. Don't let it be more than
21811 current maximum. */
21812 if (precision > 0)
21813 lim = min (precision, lim);
21814
21815 /* If that's more padding than already wanted, queue it.
21816 But don't reduce padding already specified even if
21817 that is beyond the current truncation point. */
21818 field_width = max (lim, field_width);
21819 }
21820 goto tail_recurse;
21821 }
21822 else if (STRINGP (car) || CONSP (car))
21823 {
21824 Lisp_Object halftail = elt;
21825 int len = 0;
21826
21827 while (CONSP (elt)
21828 && (precision <= 0 || n < precision))
21829 {
21830 n += display_mode_element (it, depth,
21831 /* Do padding only after the last
21832 element in the list. */
21833 (! CONSP (XCDR (elt))
21834 ? field_width - n
21835 : 0),
21836 precision - n, XCAR (elt),
21837 props, risky);
21838 elt = XCDR (elt);
21839 len++;
21840 if ((len & 1) == 0)
21841 halftail = XCDR (halftail);
21842 /* Check for cycle. */
21843 if (EQ (halftail, elt))
21844 break;
21845 }
21846 }
21847 }
21848 break;
21849
21850 default:
21851 invalid:
21852 elt = build_string ("*invalid*");
21853 goto tail_recurse;
21854 }
21855
21856 /* Pad to FIELD_WIDTH. */
21857 if (field_width > 0 && n < field_width)
21858 {
21859 switch (mode_line_target)
21860 {
21861 case MODE_LINE_NOPROP:
21862 case MODE_LINE_TITLE:
21863 n += store_mode_line_noprop ("", field_width - n, 0);
21864 break;
21865 case MODE_LINE_STRING:
21866 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21867 break;
21868 case MODE_LINE_DISPLAY:
21869 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21870 0, 0, 0);
21871 break;
21872 }
21873 }
21874
21875 return n;
21876 }
21877
21878 /* Store a mode-line string element in mode_line_string_list.
21879
21880 If STRING is non-null, display that C string. Otherwise, the Lisp
21881 string LISP_STRING is displayed.
21882
21883 FIELD_WIDTH is the minimum number of output glyphs to produce.
21884 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21885 with spaces. FIELD_WIDTH <= 0 means don't pad.
21886
21887 PRECISION is the maximum number of characters to output from
21888 STRING. PRECISION <= 0 means don't truncate the string.
21889
21890 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21891 properties to the string.
21892
21893 PROPS are the properties to add to the string.
21894 The mode_line_string_face face property is always added to the string.
21895 */
21896
21897 static int
21898 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21899 int field_width, int precision, Lisp_Object props)
21900 {
21901 ptrdiff_t len;
21902 int n = 0;
21903
21904 if (string != NULL)
21905 {
21906 len = strlen (string);
21907 if (precision > 0 && len > precision)
21908 len = precision;
21909 lisp_string = make_string (string, len);
21910 if (NILP (props))
21911 props = mode_line_string_face_prop;
21912 else if (!NILP (mode_line_string_face))
21913 {
21914 Lisp_Object face = Fplist_get (props, Qface);
21915 props = Fcopy_sequence (props);
21916 if (NILP (face))
21917 face = mode_line_string_face;
21918 else
21919 face = list2 (face, mode_line_string_face);
21920 props = Fplist_put (props, Qface, face);
21921 }
21922 Fadd_text_properties (make_number (0), make_number (len),
21923 props, lisp_string);
21924 }
21925 else
21926 {
21927 len = XFASTINT (Flength (lisp_string));
21928 if (precision > 0 && len > precision)
21929 {
21930 len = precision;
21931 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21932 precision = -1;
21933 }
21934 if (!NILP (mode_line_string_face))
21935 {
21936 Lisp_Object face;
21937 if (NILP (props))
21938 props = Ftext_properties_at (make_number (0), lisp_string);
21939 face = Fplist_get (props, Qface);
21940 if (NILP (face))
21941 face = mode_line_string_face;
21942 else
21943 face = list2 (face, mode_line_string_face);
21944 props = list2 (Qface, face);
21945 if (copy_string)
21946 lisp_string = Fcopy_sequence (lisp_string);
21947 }
21948 if (!NILP (props))
21949 Fadd_text_properties (make_number (0), make_number (len),
21950 props, lisp_string);
21951 }
21952
21953 if (len > 0)
21954 {
21955 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21956 n += len;
21957 }
21958
21959 if (field_width > len)
21960 {
21961 field_width -= len;
21962 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21963 if (!NILP (props))
21964 Fadd_text_properties (make_number (0), make_number (field_width),
21965 props, lisp_string);
21966 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21967 n += field_width;
21968 }
21969
21970 return n;
21971 }
21972
21973
21974 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21975 1, 4, 0,
21976 doc: /* Format a string out of a mode line format specification.
21977 First arg FORMAT specifies the mode line format (see `mode-line-format'
21978 for details) to use.
21979
21980 By default, the format is evaluated for the currently selected window.
21981
21982 Optional second arg FACE specifies the face property to put on all
21983 characters for which no face is specified. The value nil means the
21984 default face. The value t means whatever face the window's mode line
21985 currently uses (either `mode-line' or `mode-line-inactive',
21986 depending on whether the window is the selected window or not).
21987 An integer value means the value string has no text
21988 properties.
21989
21990 Optional third and fourth args WINDOW and BUFFER specify the window
21991 and buffer to use as the context for the formatting (defaults
21992 are the selected window and the WINDOW's buffer). */)
21993 (Lisp_Object format, Lisp_Object face,
21994 Lisp_Object window, Lisp_Object buffer)
21995 {
21996 struct it it;
21997 int len;
21998 struct window *w;
21999 struct buffer *old_buffer = NULL;
22000 int face_id;
22001 int no_props = INTEGERP (face);
22002 ptrdiff_t count = SPECPDL_INDEX ();
22003 Lisp_Object str;
22004 int string_start = 0;
22005
22006 w = decode_any_window (window);
22007 XSETWINDOW (window, w);
22008
22009 if (NILP (buffer))
22010 buffer = w->contents;
22011 CHECK_BUFFER (buffer);
22012
22013 /* Make formatting the modeline a non-op when noninteractive, otherwise
22014 there will be problems later caused by a partially initialized frame. */
22015 if (NILP (format) || noninteractive)
22016 return empty_unibyte_string;
22017
22018 if (no_props)
22019 face = Qnil;
22020
22021 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22022 : EQ (face, Qt) ? (EQ (window, selected_window)
22023 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22024 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22025 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22026 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22027 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22028 : DEFAULT_FACE_ID;
22029
22030 old_buffer = current_buffer;
22031
22032 /* Save things including mode_line_proptrans_alist,
22033 and set that to nil so that we don't alter the outer value. */
22034 record_unwind_protect (unwind_format_mode_line,
22035 format_mode_line_unwind_data
22036 (XFRAME (WINDOW_FRAME (w)),
22037 old_buffer, selected_window, 1));
22038 mode_line_proptrans_alist = Qnil;
22039
22040 Fselect_window (window, Qt);
22041 set_buffer_internal_1 (XBUFFER (buffer));
22042
22043 init_iterator (&it, w, -1, -1, NULL, face_id);
22044
22045 if (no_props)
22046 {
22047 mode_line_target = MODE_LINE_NOPROP;
22048 mode_line_string_face_prop = Qnil;
22049 mode_line_string_list = Qnil;
22050 string_start = MODE_LINE_NOPROP_LEN (0);
22051 }
22052 else
22053 {
22054 mode_line_target = MODE_LINE_STRING;
22055 mode_line_string_list = Qnil;
22056 mode_line_string_face = face;
22057 mode_line_string_face_prop
22058 = NILP (face) ? Qnil : list2 (Qface, face);
22059 }
22060
22061 push_kboard (FRAME_KBOARD (it.f));
22062 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22063 pop_kboard ();
22064
22065 if (no_props)
22066 {
22067 len = MODE_LINE_NOPROP_LEN (string_start);
22068 str = make_string (mode_line_noprop_buf + string_start, len);
22069 }
22070 else
22071 {
22072 mode_line_string_list = Fnreverse (mode_line_string_list);
22073 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22074 empty_unibyte_string);
22075 }
22076
22077 unbind_to (count, Qnil);
22078 return str;
22079 }
22080
22081 /* Write a null-terminated, right justified decimal representation of
22082 the positive integer D to BUF using a minimal field width WIDTH. */
22083
22084 static void
22085 pint2str (register char *buf, register int width, register ptrdiff_t d)
22086 {
22087 register char *p = buf;
22088
22089 if (d <= 0)
22090 *p++ = '0';
22091 else
22092 {
22093 while (d > 0)
22094 {
22095 *p++ = d % 10 + '0';
22096 d /= 10;
22097 }
22098 }
22099
22100 for (width -= (int) (p - buf); width > 0; --width)
22101 *p++ = ' ';
22102 *p-- = '\0';
22103 while (p > buf)
22104 {
22105 d = *buf;
22106 *buf++ = *p;
22107 *p-- = d;
22108 }
22109 }
22110
22111 /* Write a null-terminated, right justified decimal and "human
22112 readable" representation of the nonnegative integer D to BUF using
22113 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22114
22115 static const char power_letter[] =
22116 {
22117 0, /* no letter */
22118 'k', /* kilo */
22119 'M', /* mega */
22120 'G', /* giga */
22121 'T', /* tera */
22122 'P', /* peta */
22123 'E', /* exa */
22124 'Z', /* zetta */
22125 'Y' /* yotta */
22126 };
22127
22128 static void
22129 pint2hrstr (char *buf, int width, ptrdiff_t d)
22130 {
22131 /* We aim to represent the nonnegative integer D as
22132 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22133 ptrdiff_t quotient = d;
22134 int remainder = 0;
22135 /* -1 means: do not use TENTHS. */
22136 int tenths = -1;
22137 int exponent = 0;
22138
22139 /* Length of QUOTIENT.TENTHS as a string. */
22140 int length;
22141
22142 char * psuffix;
22143 char * p;
22144
22145 if (quotient >= 1000)
22146 {
22147 /* Scale to the appropriate EXPONENT. */
22148 do
22149 {
22150 remainder = quotient % 1000;
22151 quotient /= 1000;
22152 exponent++;
22153 }
22154 while (quotient >= 1000);
22155
22156 /* Round to nearest and decide whether to use TENTHS or not. */
22157 if (quotient <= 9)
22158 {
22159 tenths = remainder / 100;
22160 if (remainder % 100 >= 50)
22161 {
22162 if (tenths < 9)
22163 tenths++;
22164 else
22165 {
22166 quotient++;
22167 if (quotient == 10)
22168 tenths = -1;
22169 else
22170 tenths = 0;
22171 }
22172 }
22173 }
22174 else
22175 if (remainder >= 500)
22176 {
22177 if (quotient < 999)
22178 quotient++;
22179 else
22180 {
22181 quotient = 1;
22182 exponent++;
22183 tenths = 0;
22184 }
22185 }
22186 }
22187
22188 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22189 if (tenths == -1 && quotient <= 99)
22190 if (quotient <= 9)
22191 length = 1;
22192 else
22193 length = 2;
22194 else
22195 length = 3;
22196 p = psuffix = buf + max (width, length);
22197
22198 /* Print EXPONENT. */
22199 *psuffix++ = power_letter[exponent];
22200 *psuffix = '\0';
22201
22202 /* Print TENTHS. */
22203 if (tenths >= 0)
22204 {
22205 *--p = '0' + tenths;
22206 *--p = '.';
22207 }
22208
22209 /* Print QUOTIENT. */
22210 do
22211 {
22212 int digit = quotient % 10;
22213 *--p = '0' + digit;
22214 }
22215 while ((quotient /= 10) != 0);
22216
22217 /* Print leading spaces. */
22218 while (buf < p)
22219 *--p = ' ';
22220 }
22221
22222 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22223 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22224 type of CODING_SYSTEM. Return updated pointer into BUF. */
22225
22226 static unsigned char invalid_eol_type[] = "(*invalid*)";
22227
22228 static char *
22229 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22230 {
22231 Lisp_Object val;
22232 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22233 const unsigned char *eol_str;
22234 int eol_str_len;
22235 /* The EOL conversion we are using. */
22236 Lisp_Object eoltype;
22237
22238 val = CODING_SYSTEM_SPEC (coding_system);
22239 eoltype = Qnil;
22240
22241 if (!VECTORP (val)) /* Not yet decided. */
22242 {
22243 *buf++ = multibyte ? '-' : ' ';
22244 if (eol_flag)
22245 eoltype = eol_mnemonic_undecided;
22246 /* Don't mention EOL conversion if it isn't decided. */
22247 }
22248 else
22249 {
22250 Lisp_Object attrs;
22251 Lisp_Object eolvalue;
22252
22253 attrs = AREF (val, 0);
22254 eolvalue = AREF (val, 2);
22255
22256 *buf++ = multibyte
22257 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22258 : ' ';
22259
22260 if (eol_flag)
22261 {
22262 /* The EOL conversion that is normal on this system. */
22263
22264 if (NILP (eolvalue)) /* Not yet decided. */
22265 eoltype = eol_mnemonic_undecided;
22266 else if (VECTORP (eolvalue)) /* Not yet decided. */
22267 eoltype = eol_mnemonic_undecided;
22268 else /* eolvalue is Qunix, Qdos, or Qmac. */
22269 eoltype = (EQ (eolvalue, Qunix)
22270 ? eol_mnemonic_unix
22271 : (EQ (eolvalue, Qdos) == 1
22272 ? eol_mnemonic_dos : eol_mnemonic_mac));
22273 }
22274 }
22275
22276 if (eol_flag)
22277 {
22278 /* Mention the EOL conversion if it is not the usual one. */
22279 if (STRINGP (eoltype))
22280 {
22281 eol_str = SDATA (eoltype);
22282 eol_str_len = SBYTES (eoltype);
22283 }
22284 else if (CHARACTERP (eoltype))
22285 {
22286 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22287 int c = XFASTINT (eoltype);
22288 eol_str_len = CHAR_STRING (c, tmp);
22289 eol_str = tmp;
22290 }
22291 else
22292 {
22293 eol_str = invalid_eol_type;
22294 eol_str_len = sizeof (invalid_eol_type) - 1;
22295 }
22296 memcpy (buf, eol_str, eol_str_len);
22297 buf += eol_str_len;
22298 }
22299
22300 return buf;
22301 }
22302
22303 /* Return a string for the output of a mode line %-spec for window W,
22304 generated by character C. FIELD_WIDTH > 0 means pad the string
22305 returned with spaces to that value. Return a Lisp string in
22306 *STRING if the resulting string is taken from that Lisp string.
22307
22308 Note we operate on the current buffer for most purposes. */
22309
22310 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22311
22312 static const char *
22313 decode_mode_spec (struct window *w, register int c, int field_width,
22314 Lisp_Object *string)
22315 {
22316 Lisp_Object obj;
22317 struct frame *f = XFRAME (WINDOW_FRAME (w));
22318 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22319 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22320 produce strings from numerical values, so limit preposterously
22321 large values of FIELD_WIDTH to avoid overrunning the buffer's
22322 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22323 bytes plus the terminating null. */
22324 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22325 struct buffer *b = current_buffer;
22326
22327 obj = Qnil;
22328 *string = Qnil;
22329
22330 switch (c)
22331 {
22332 case '*':
22333 if (!NILP (BVAR (b, read_only)))
22334 return "%";
22335 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22336 return "*";
22337 return "-";
22338
22339 case '+':
22340 /* This differs from %* only for a modified read-only buffer. */
22341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22342 return "*";
22343 if (!NILP (BVAR (b, read_only)))
22344 return "%";
22345 return "-";
22346
22347 case '&':
22348 /* This differs from %* in ignoring read-only-ness. */
22349 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22350 return "*";
22351 return "-";
22352
22353 case '%':
22354 return "%";
22355
22356 case '[':
22357 {
22358 int i;
22359 char *p;
22360
22361 if (command_loop_level > 5)
22362 return "[[[... ";
22363 p = decode_mode_spec_buf;
22364 for (i = 0; i < command_loop_level; i++)
22365 *p++ = '[';
22366 *p = 0;
22367 return decode_mode_spec_buf;
22368 }
22369
22370 case ']':
22371 {
22372 int i;
22373 char *p;
22374
22375 if (command_loop_level > 5)
22376 return " ...]]]";
22377 p = decode_mode_spec_buf;
22378 for (i = 0; i < command_loop_level; i++)
22379 *p++ = ']';
22380 *p = 0;
22381 return decode_mode_spec_buf;
22382 }
22383
22384 case '-':
22385 {
22386 register int i;
22387
22388 /* Let lots_of_dashes be a string of infinite length. */
22389 if (mode_line_target == MODE_LINE_NOPROP
22390 || mode_line_target == MODE_LINE_STRING)
22391 return "--";
22392 if (field_width <= 0
22393 || field_width > sizeof (lots_of_dashes))
22394 {
22395 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22396 decode_mode_spec_buf[i] = '-';
22397 decode_mode_spec_buf[i] = '\0';
22398 return decode_mode_spec_buf;
22399 }
22400 else
22401 return lots_of_dashes;
22402 }
22403
22404 case 'b':
22405 obj = BVAR (b, name);
22406 break;
22407
22408 case 'c':
22409 /* %c and %l are ignored in `frame-title-format'.
22410 (In redisplay_internal, the frame title is drawn _before_ the
22411 windows are updated, so the stuff which depends on actual
22412 window contents (such as %l) may fail to render properly, or
22413 even crash emacs.) */
22414 if (mode_line_target == MODE_LINE_TITLE)
22415 return "";
22416 else
22417 {
22418 ptrdiff_t col = current_column ();
22419 w->column_number_displayed = col;
22420 pint2str (decode_mode_spec_buf, width, col);
22421 return decode_mode_spec_buf;
22422 }
22423
22424 case 'e':
22425 #ifndef SYSTEM_MALLOC
22426 {
22427 if (NILP (Vmemory_full))
22428 return "";
22429 else
22430 return "!MEM FULL! ";
22431 }
22432 #else
22433 return "";
22434 #endif
22435
22436 case 'F':
22437 /* %F displays the frame name. */
22438 if (!NILP (f->title))
22439 return SSDATA (f->title);
22440 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22441 return SSDATA (f->name);
22442 return "Emacs";
22443
22444 case 'f':
22445 obj = BVAR (b, filename);
22446 break;
22447
22448 case 'i':
22449 {
22450 ptrdiff_t size = ZV - BEGV;
22451 pint2str (decode_mode_spec_buf, width, size);
22452 return decode_mode_spec_buf;
22453 }
22454
22455 case 'I':
22456 {
22457 ptrdiff_t size = ZV - BEGV;
22458 pint2hrstr (decode_mode_spec_buf, width, size);
22459 return decode_mode_spec_buf;
22460 }
22461
22462 case 'l':
22463 {
22464 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22465 ptrdiff_t topline, nlines, height;
22466 ptrdiff_t junk;
22467
22468 /* %c and %l are ignored in `frame-title-format'. */
22469 if (mode_line_target == MODE_LINE_TITLE)
22470 return "";
22471
22472 startpos = marker_position (w->start);
22473 startpos_byte = marker_byte_position (w->start);
22474 height = WINDOW_TOTAL_LINES (w);
22475
22476 /* If we decided that this buffer isn't suitable for line numbers,
22477 don't forget that too fast. */
22478 if (w->base_line_pos == -1)
22479 goto no_value;
22480
22481 /* If the buffer is very big, don't waste time. */
22482 if (INTEGERP (Vline_number_display_limit)
22483 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22484 {
22485 w->base_line_pos = 0;
22486 w->base_line_number = 0;
22487 goto no_value;
22488 }
22489
22490 if (w->base_line_number > 0
22491 && w->base_line_pos > 0
22492 && w->base_line_pos <= startpos)
22493 {
22494 line = w->base_line_number;
22495 linepos = w->base_line_pos;
22496 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22497 }
22498 else
22499 {
22500 line = 1;
22501 linepos = BUF_BEGV (b);
22502 linepos_byte = BUF_BEGV_BYTE (b);
22503 }
22504
22505 /* Count lines from base line to window start position. */
22506 nlines = display_count_lines (linepos_byte,
22507 startpos_byte,
22508 startpos, &junk);
22509
22510 topline = nlines + line;
22511
22512 /* Determine a new base line, if the old one is too close
22513 or too far away, or if we did not have one.
22514 "Too close" means it's plausible a scroll-down would
22515 go back past it. */
22516 if (startpos == BUF_BEGV (b))
22517 {
22518 w->base_line_number = topline;
22519 w->base_line_pos = BUF_BEGV (b);
22520 }
22521 else if (nlines < height + 25 || nlines > height * 3 + 50
22522 || linepos == BUF_BEGV (b))
22523 {
22524 ptrdiff_t limit = BUF_BEGV (b);
22525 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22526 ptrdiff_t position;
22527 ptrdiff_t distance =
22528 (height * 2 + 30) * line_number_display_limit_width;
22529
22530 if (startpos - distance > limit)
22531 {
22532 limit = startpos - distance;
22533 limit_byte = CHAR_TO_BYTE (limit);
22534 }
22535
22536 nlines = display_count_lines (startpos_byte,
22537 limit_byte,
22538 - (height * 2 + 30),
22539 &position);
22540 /* If we couldn't find the lines we wanted within
22541 line_number_display_limit_width chars per line,
22542 give up on line numbers for this window. */
22543 if (position == limit_byte && limit == startpos - distance)
22544 {
22545 w->base_line_pos = -1;
22546 w->base_line_number = 0;
22547 goto no_value;
22548 }
22549
22550 w->base_line_number = topline - nlines;
22551 w->base_line_pos = BYTE_TO_CHAR (position);
22552 }
22553
22554 /* Now count lines from the start pos to point. */
22555 nlines = display_count_lines (startpos_byte,
22556 PT_BYTE, PT, &junk);
22557
22558 /* Record that we did display the line number. */
22559 line_number_displayed = 1;
22560
22561 /* Make the string to show. */
22562 pint2str (decode_mode_spec_buf, width, topline + nlines);
22563 return decode_mode_spec_buf;
22564 no_value:
22565 {
22566 char* p = decode_mode_spec_buf;
22567 int pad = width - 2;
22568 while (pad-- > 0)
22569 *p++ = ' ';
22570 *p++ = '?';
22571 *p++ = '?';
22572 *p = '\0';
22573 return decode_mode_spec_buf;
22574 }
22575 }
22576 break;
22577
22578 case 'm':
22579 obj = BVAR (b, mode_name);
22580 break;
22581
22582 case 'n':
22583 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22584 return " Narrow";
22585 break;
22586
22587 case 'p':
22588 {
22589 ptrdiff_t pos = marker_position (w->start);
22590 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22591
22592 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22593 {
22594 if (pos <= BUF_BEGV (b))
22595 return "All";
22596 else
22597 return "Bottom";
22598 }
22599 else if (pos <= BUF_BEGV (b))
22600 return "Top";
22601 else
22602 {
22603 if (total > 1000000)
22604 /* Do it differently for a large value, to avoid overflow. */
22605 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22606 else
22607 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22608 /* We can't normally display a 3-digit number,
22609 so get us a 2-digit number that is close. */
22610 if (total == 100)
22611 total = 99;
22612 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22613 return decode_mode_spec_buf;
22614 }
22615 }
22616
22617 /* Display percentage of size above the bottom of the screen. */
22618 case 'P':
22619 {
22620 ptrdiff_t toppos = marker_position (w->start);
22621 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22622 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22623
22624 if (botpos >= BUF_ZV (b))
22625 {
22626 if (toppos <= BUF_BEGV (b))
22627 return "All";
22628 else
22629 return "Bottom";
22630 }
22631 else
22632 {
22633 if (total > 1000000)
22634 /* Do it differently for a large value, to avoid overflow. */
22635 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22636 else
22637 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22638 /* We can't normally display a 3-digit number,
22639 so get us a 2-digit number that is close. */
22640 if (total == 100)
22641 total = 99;
22642 if (toppos <= BUF_BEGV (b))
22643 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22644 else
22645 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22646 return decode_mode_spec_buf;
22647 }
22648 }
22649
22650 case 's':
22651 /* status of process */
22652 obj = Fget_buffer_process (Fcurrent_buffer ());
22653 if (NILP (obj))
22654 return "no process";
22655 #ifndef MSDOS
22656 obj = Fsymbol_name (Fprocess_status (obj));
22657 #endif
22658 break;
22659
22660 case '@':
22661 {
22662 ptrdiff_t count = inhibit_garbage_collection ();
22663 Lisp_Object val = call1 (intern ("file-remote-p"),
22664 BVAR (current_buffer, directory));
22665 unbind_to (count, Qnil);
22666
22667 if (NILP (val))
22668 return "-";
22669 else
22670 return "@";
22671 }
22672
22673 case 'z':
22674 /* coding-system (not including end-of-line format) */
22675 case 'Z':
22676 /* coding-system (including end-of-line type) */
22677 {
22678 int eol_flag = (c == 'Z');
22679 char *p = decode_mode_spec_buf;
22680
22681 if (! FRAME_WINDOW_P (f))
22682 {
22683 /* No need to mention EOL here--the terminal never needs
22684 to do EOL conversion. */
22685 p = decode_mode_spec_coding (CODING_ID_NAME
22686 (FRAME_KEYBOARD_CODING (f)->id),
22687 p, 0);
22688 p = decode_mode_spec_coding (CODING_ID_NAME
22689 (FRAME_TERMINAL_CODING (f)->id),
22690 p, 0);
22691 }
22692 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22693 p, eol_flag);
22694
22695 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22696 #ifdef subprocesses
22697 obj = Fget_buffer_process (Fcurrent_buffer ());
22698 if (PROCESSP (obj))
22699 {
22700 p = decode_mode_spec_coding
22701 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22702 p = decode_mode_spec_coding
22703 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22704 }
22705 #endif /* subprocesses */
22706 #endif /* 0 */
22707 *p = 0;
22708 return decode_mode_spec_buf;
22709 }
22710 }
22711
22712 if (STRINGP (obj))
22713 {
22714 *string = obj;
22715 return SSDATA (obj);
22716 }
22717 else
22718 return "";
22719 }
22720
22721
22722 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22723 means count lines back from START_BYTE. But don't go beyond
22724 LIMIT_BYTE. Return the number of lines thus found (always
22725 nonnegative).
22726
22727 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22728 either the position COUNT lines after/before START_BYTE, if we
22729 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22730 COUNT lines. */
22731
22732 static ptrdiff_t
22733 display_count_lines (ptrdiff_t start_byte,
22734 ptrdiff_t limit_byte, ptrdiff_t count,
22735 ptrdiff_t *byte_pos_ptr)
22736 {
22737 register unsigned char *cursor;
22738 unsigned char *base;
22739
22740 register ptrdiff_t ceiling;
22741 register unsigned char *ceiling_addr;
22742 ptrdiff_t orig_count = count;
22743
22744 /* If we are not in selective display mode,
22745 check only for newlines. */
22746 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22747 && !INTEGERP (BVAR (current_buffer, selective_display)));
22748
22749 if (count > 0)
22750 {
22751 while (start_byte < limit_byte)
22752 {
22753 ceiling = BUFFER_CEILING_OF (start_byte);
22754 ceiling = min (limit_byte - 1, ceiling);
22755 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22756 base = (cursor = BYTE_POS_ADDR (start_byte));
22757
22758 do
22759 {
22760 if (selective_display)
22761 {
22762 while (*cursor != '\n' && *cursor != 015
22763 && ++cursor != ceiling_addr)
22764 continue;
22765 if (cursor == ceiling_addr)
22766 break;
22767 }
22768 else
22769 {
22770 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22771 if (! cursor)
22772 break;
22773 }
22774
22775 cursor++;
22776
22777 if (--count == 0)
22778 {
22779 start_byte += cursor - base;
22780 *byte_pos_ptr = start_byte;
22781 return orig_count;
22782 }
22783 }
22784 while (cursor < ceiling_addr);
22785
22786 start_byte += ceiling_addr - base;
22787 }
22788 }
22789 else
22790 {
22791 while (start_byte > limit_byte)
22792 {
22793 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22794 ceiling = max (limit_byte, ceiling);
22795 ceiling_addr = BYTE_POS_ADDR (ceiling);
22796 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22797 while (1)
22798 {
22799 if (selective_display)
22800 {
22801 while (--cursor >= ceiling_addr
22802 && *cursor != '\n' && *cursor != 015)
22803 continue;
22804 if (cursor < ceiling_addr)
22805 break;
22806 }
22807 else
22808 {
22809 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22810 if (! cursor)
22811 break;
22812 }
22813
22814 if (++count == 0)
22815 {
22816 start_byte += cursor - base + 1;
22817 *byte_pos_ptr = start_byte;
22818 /* When scanning backwards, we should
22819 not count the newline posterior to which we stop. */
22820 return - orig_count - 1;
22821 }
22822 }
22823 start_byte += ceiling_addr - base;
22824 }
22825 }
22826
22827 *byte_pos_ptr = limit_byte;
22828
22829 if (count < 0)
22830 return - orig_count + count;
22831 return orig_count - count;
22832
22833 }
22834
22835
22836 \f
22837 /***********************************************************************
22838 Displaying strings
22839 ***********************************************************************/
22840
22841 /* Display a NUL-terminated string, starting with index START.
22842
22843 If STRING is non-null, display that C string. Otherwise, the Lisp
22844 string LISP_STRING is displayed. There's a case that STRING is
22845 non-null and LISP_STRING is not nil. It means STRING is a string
22846 data of LISP_STRING. In that case, we display LISP_STRING while
22847 ignoring its text properties.
22848
22849 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22850 FACE_STRING. Display STRING or LISP_STRING with the face at
22851 FACE_STRING_POS in FACE_STRING:
22852
22853 Display the string in the environment given by IT, but use the
22854 standard display table, temporarily.
22855
22856 FIELD_WIDTH is the minimum number of output glyphs to produce.
22857 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22858 with spaces. If STRING has more characters, more than FIELD_WIDTH
22859 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22860
22861 PRECISION is the maximum number of characters to output from
22862 STRING. PRECISION < 0 means don't truncate the string.
22863
22864 This is roughly equivalent to printf format specifiers:
22865
22866 FIELD_WIDTH PRECISION PRINTF
22867 ----------------------------------------
22868 -1 -1 %s
22869 -1 10 %.10s
22870 10 -1 %10s
22871 20 10 %20.10s
22872
22873 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22874 display them, and < 0 means obey the current buffer's value of
22875 enable_multibyte_characters.
22876
22877 Value is the number of columns displayed. */
22878
22879 static int
22880 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22881 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22882 int field_width, int precision, int max_x, int multibyte)
22883 {
22884 int hpos_at_start = it->hpos;
22885 int saved_face_id = it->face_id;
22886 struct glyph_row *row = it->glyph_row;
22887 ptrdiff_t it_charpos;
22888
22889 /* Initialize the iterator IT for iteration over STRING beginning
22890 with index START. */
22891 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22892 precision, field_width, multibyte);
22893 if (string && STRINGP (lisp_string))
22894 /* LISP_STRING is the one returned by decode_mode_spec. We should
22895 ignore its text properties. */
22896 it->stop_charpos = it->end_charpos;
22897
22898 /* If displaying STRING, set up the face of the iterator from
22899 FACE_STRING, if that's given. */
22900 if (STRINGP (face_string))
22901 {
22902 ptrdiff_t endptr;
22903 struct face *face;
22904
22905 it->face_id
22906 = face_at_string_position (it->w, face_string, face_string_pos,
22907 0, &endptr, it->base_face_id, 0);
22908 face = FACE_FROM_ID (it->f, it->face_id);
22909 it->face_box_p = face->box != FACE_NO_BOX;
22910 }
22911
22912 /* Set max_x to the maximum allowed X position. Don't let it go
22913 beyond the right edge of the window. */
22914 if (max_x <= 0)
22915 max_x = it->last_visible_x;
22916 else
22917 max_x = min (max_x, it->last_visible_x);
22918
22919 /* Skip over display elements that are not visible. because IT->w is
22920 hscrolled. */
22921 if (it->current_x < it->first_visible_x)
22922 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22923 MOVE_TO_POS | MOVE_TO_X);
22924
22925 row->ascent = it->max_ascent;
22926 row->height = it->max_ascent + it->max_descent;
22927 row->phys_ascent = it->max_phys_ascent;
22928 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22929 row->extra_line_spacing = it->max_extra_line_spacing;
22930
22931 if (STRINGP (it->string))
22932 it_charpos = IT_STRING_CHARPOS (*it);
22933 else
22934 it_charpos = IT_CHARPOS (*it);
22935
22936 /* This condition is for the case that we are called with current_x
22937 past last_visible_x. */
22938 while (it->current_x < max_x)
22939 {
22940 int x_before, x, n_glyphs_before, i, nglyphs;
22941
22942 /* Get the next display element. */
22943 if (!get_next_display_element (it))
22944 break;
22945
22946 /* Produce glyphs. */
22947 x_before = it->current_x;
22948 n_glyphs_before = row->used[TEXT_AREA];
22949 PRODUCE_GLYPHS (it);
22950
22951 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22952 i = 0;
22953 x = x_before;
22954 while (i < nglyphs)
22955 {
22956 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22957
22958 if (it->line_wrap != TRUNCATE
22959 && x + glyph->pixel_width > max_x)
22960 {
22961 /* End of continued line or max_x reached. */
22962 if (CHAR_GLYPH_PADDING_P (*glyph))
22963 {
22964 /* A wide character is unbreakable. */
22965 if (row->reversed_p)
22966 unproduce_glyphs (it, row->used[TEXT_AREA]
22967 - n_glyphs_before);
22968 row->used[TEXT_AREA] = n_glyphs_before;
22969 it->current_x = x_before;
22970 }
22971 else
22972 {
22973 if (row->reversed_p)
22974 unproduce_glyphs (it, row->used[TEXT_AREA]
22975 - (n_glyphs_before + i));
22976 row->used[TEXT_AREA] = n_glyphs_before + i;
22977 it->current_x = x;
22978 }
22979 break;
22980 }
22981 else if (x + glyph->pixel_width >= it->first_visible_x)
22982 {
22983 /* Glyph is at least partially visible. */
22984 ++it->hpos;
22985 if (x < it->first_visible_x)
22986 row->x = x - it->first_visible_x;
22987 }
22988 else
22989 {
22990 /* Glyph is off the left margin of the display area.
22991 Should not happen. */
22992 emacs_abort ();
22993 }
22994
22995 row->ascent = max (row->ascent, it->max_ascent);
22996 row->height = max (row->height, it->max_ascent + it->max_descent);
22997 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22998 row->phys_height = max (row->phys_height,
22999 it->max_phys_ascent + it->max_phys_descent);
23000 row->extra_line_spacing = max (row->extra_line_spacing,
23001 it->max_extra_line_spacing);
23002 x += glyph->pixel_width;
23003 ++i;
23004 }
23005
23006 /* Stop if max_x reached. */
23007 if (i < nglyphs)
23008 break;
23009
23010 /* Stop at line ends. */
23011 if (ITERATOR_AT_END_OF_LINE_P (it))
23012 {
23013 it->continuation_lines_width = 0;
23014 break;
23015 }
23016
23017 set_iterator_to_next (it, 1);
23018 if (STRINGP (it->string))
23019 it_charpos = IT_STRING_CHARPOS (*it);
23020 else
23021 it_charpos = IT_CHARPOS (*it);
23022
23023 /* Stop if truncating at the right edge. */
23024 if (it->line_wrap == TRUNCATE
23025 && it->current_x >= it->last_visible_x)
23026 {
23027 /* Add truncation mark, but don't do it if the line is
23028 truncated at a padding space. */
23029 if (it_charpos < it->string_nchars)
23030 {
23031 if (!FRAME_WINDOW_P (it->f))
23032 {
23033 int ii, n;
23034
23035 if (it->current_x > it->last_visible_x)
23036 {
23037 if (!row->reversed_p)
23038 {
23039 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23040 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23041 break;
23042 }
23043 else
23044 {
23045 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23046 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23047 break;
23048 unproduce_glyphs (it, ii + 1);
23049 ii = row->used[TEXT_AREA] - (ii + 1);
23050 }
23051 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23052 {
23053 row->used[TEXT_AREA] = ii;
23054 produce_special_glyphs (it, IT_TRUNCATION);
23055 }
23056 }
23057 produce_special_glyphs (it, IT_TRUNCATION);
23058 }
23059 row->truncated_on_right_p = 1;
23060 }
23061 break;
23062 }
23063 }
23064
23065 /* Maybe insert a truncation at the left. */
23066 if (it->first_visible_x
23067 && it_charpos > 0)
23068 {
23069 if (!FRAME_WINDOW_P (it->f)
23070 || (row->reversed_p
23071 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23072 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23073 insert_left_trunc_glyphs (it);
23074 row->truncated_on_left_p = 1;
23075 }
23076
23077 it->face_id = saved_face_id;
23078
23079 /* Value is number of columns displayed. */
23080 return it->hpos - hpos_at_start;
23081 }
23082
23083
23084 \f
23085 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23086 appears as an element of LIST or as the car of an element of LIST.
23087 If PROPVAL is a list, compare each element against LIST in that
23088 way, and return 1/2 if any element of PROPVAL is found in LIST.
23089 Otherwise return 0. This function cannot quit.
23090 The return value is 2 if the text is invisible but with an ellipsis
23091 and 1 if it's invisible and without an ellipsis. */
23092
23093 int
23094 invisible_p (register Lisp_Object propval, Lisp_Object list)
23095 {
23096 register Lisp_Object tail, proptail;
23097
23098 for (tail = list; CONSP (tail); tail = XCDR (tail))
23099 {
23100 register Lisp_Object tem;
23101 tem = XCAR (tail);
23102 if (EQ (propval, tem))
23103 return 1;
23104 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23105 return NILP (XCDR (tem)) ? 1 : 2;
23106 }
23107
23108 if (CONSP (propval))
23109 {
23110 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23111 {
23112 Lisp_Object propelt;
23113 propelt = XCAR (proptail);
23114 for (tail = list; CONSP (tail); tail = XCDR (tail))
23115 {
23116 register Lisp_Object tem;
23117 tem = XCAR (tail);
23118 if (EQ (propelt, tem))
23119 return 1;
23120 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23121 return NILP (XCDR (tem)) ? 1 : 2;
23122 }
23123 }
23124 }
23125
23126 return 0;
23127 }
23128
23129 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23130 doc: /* Non-nil if the property makes the text invisible.
23131 POS-OR-PROP can be a marker or number, in which case it is taken to be
23132 a position in the current buffer and the value of the `invisible' property
23133 is checked; or it can be some other value, which is then presumed to be the
23134 value of the `invisible' property of the text of interest.
23135 The non-nil value returned can be t for truly invisible text or something
23136 else if the text is replaced by an ellipsis. */)
23137 (Lisp_Object pos_or_prop)
23138 {
23139 Lisp_Object prop
23140 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23141 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23142 : pos_or_prop);
23143 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23144 return (invis == 0 ? Qnil
23145 : invis == 1 ? Qt
23146 : make_number (invis));
23147 }
23148
23149 /* Calculate a width or height in pixels from a specification using
23150 the following elements:
23151
23152 SPEC ::=
23153 NUM - a (fractional) multiple of the default font width/height
23154 (NUM) - specifies exactly NUM pixels
23155 UNIT - a fixed number of pixels, see below.
23156 ELEMENT - size of a display element in pixels, see below.
23157 (NUM . SPEC) - equals NUM * SPEC
23158 (+ SPEC SPEC ...) - add pixel values
23159 (- SPEC SPEC ...) - subtract pixel values
23160 (- SPEC) - negate pixel value
23161
23162 NUM ::=
23163 INT or FLOAT - a number constant
23164 SYMBOL - use symbol's (buffer local) variable binding.
23165
23166 UNIT ::=
23167 in - pixels per inch *)
23168 mm - pixels per 1/1000 meter *)
23169 cm - pixels per 1/100 meter *)
23170 width - width of current font in pixels.
23171 height - height of current font in pixels.
23172
23173 *) using the ratio(s) defined in display-pixels-per-inch.
23174
23175 ELEMENT ::=
23176
23177 left-fringe - left fringe width in pixels
23178 right-fringe - right fringe width in pixels
23179
23180 left-margin - left margin width in pixels
23181 right-margin - right margin width in pixels
23182
23183 scroll-bar - scroll-bar area width in pixels
23184
23185 Examples:
23186
23187 Pixels corresponding to 5 inches:
23188 (5 . in)
23189
23190 Total width of non-text areas on left side of window (if scroll-bar is on left):
23191 '(space :width (+ left-fringe left-margin scroll-bar))
23192
23193 Align to first text column (in header line):
23194 '(space :align-to 0)
23195
23196 Align to middle of text area minus half the width of variable `my-image'
23197 containing a loaded image:
23198 '(space :align-to (0.5 . (- text my-image)))
23199
23200 Width of left margin minus width of 1 character in the default font:
23201 '(space :width (- left-margin 1))
23202
23203 Width of left margin minus width of 2 characters in the current font:
23204 '(space :width (- left-margin (2 . width)))
23205
23206 Center 1 character over left-margin (in header line):
23207 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23208
23209 Different ways to express width of left fringe plus left margin minus one pixel:
23210 '(space :width (- (+ left-fringe left-margin) (1)))
23211 '(space :width (+ left-fringe left-margin (- (1))))
23212 '(space :width (+ left-fringe left-margin (-1)))
23213
23214 */
23215
23216 static int
23217 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23218 struct font *font, int width_p, int *align_to)
23219 {
23220 double pixels;
23221
23222 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23223 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23224
23225 if (NILP (prop))
23226 return OK_PIXELS (0);
23227
23228 eassert (FRAME_LIVE_P (it->f));
23229
23230 if (SYMBOLP (prop))
23231 {
23232 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23233 {
23234 char *unit = SSDATA (SYMBOL_NAME (prop));
23235
23236 if (unit[0] == 'i' && unit[1] == 'n')
23237 pixels = 1.0;
23238 else if (unit[0] == 'm' && unit[1] == 'm')
23239 pixels = 25.4;
23240 else if (unit[0] == 'c' && unit[1] == 'm')
23241 pixels = 2.54;
23242 else
23243 pixels = 0;
23244 if (pixels > 0)
23245 {
23246 double ppi = (width_p ? FRAME_RES_X (it->f)
23247 : FRAME_RES_Y (it->f));
23248
23249 if (ppi > 0)
23250 return OK_PIXELS (ppi / pixels);
23251 return 0;
23252 }
23253 }
23254
23255 #ifdef HAVE_WINDOW_SYSTEM
23256 if (EQ (prop, Qheight))
23257 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23258 if (EQ (prop, Qwidth))
23259 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23260 #else
23261 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23262 return OK_PIXELS (1);
23263 #endif
23264
23265 if (EQ (prop, Qtext))
23266 return OK_PIXELS (width_p
23267 ? window_box_width (it->w, TEXT_AREA)
23268 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23269
23270 if (align_to && *align_to < 0)
23271 {
23272 *res = 0;
23273 if (EQ (prop, Qleft))
23274 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23275 if (EQ (prop, Qright))
23276 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23277 if (EQ (prop, Qcenter))
23278 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23279 + window_box_width (it->w, TEXT_AREA) / 2);
23280 if (EQ (prop, Qleft_fringe))
23281 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23282 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23283 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23284 if (EQ (prop, Qright_fringe))
23285 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23286 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23287 : window_box_right_offset (it->w, TEXT_AREA));
23288 if (EQ (prop, Qleft_margin))
23289 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23290 if (EQ (prop, Qright_margin))
23291 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23292 if (EQ (prop, Qscroll_bar))
23293 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23294 ? 0
23295 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23296 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23297 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23298 : 0)));
23299 }
23300 else
23301 {
23302 if (EQ (prop, Qleft_fringe))
23303 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23304 if (EQ (prop, Qright_fringe))
23305 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23306 if (EQ (prop, Qleft_margin))
23307 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23308 if (EQ (prop, Qright_margin))
23309 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23310 if (EQ (prop, Qscroll_bar))
23311 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23312 }
23313
23314 prop = buffer_local_value_1 (prop, it->w->contents);
23315 if (EQ (prop, Qunbound))
23316 prop = Qnil;
23317 }
23318
23319 if (INTEGERP (prop) || FLOATP (prop))
23320 {
23321 int base_unit = (width_p
23322 ? FRAME_COLUMN_WIDTH (it->f)
23323 : FRAME_LINE_HEIGHT (it->f));
23324 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23325 }
23326
23327 if (CONSP (prop))
23328 {
23329 Lisp_Object car = XCAR (prop);
23330 Lisp_Object cdr = XCDR (prop);
23331
23332 if (SYMBOLP (car))
23333 {
23334 #ifdef HAVE_WINDOW_SYSTEM
23335 if (FRAME_WINDOW_P (it->f)
23336 && valid_image_p (prop))
23337 {
23338 ptrdiff_t id = lookup_image (it->f, prop);
23339 struct image *img = IMAGE_FROM_ID (it->f, id);
23340
23341 return OK_PIXELS (width_p ? img->width : img->height);
23342 }
23343 #endif
23344 if (EQ (car, Qplus) || EQ (car, Qminus))
23345 {
23346 int first = 1;
23347 double px;
23348
23349 pixels = 0;
23350 while (CONSP (cdr))
23351 {
23352 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23353 font, width_p, align_to))
23354 return 0;
23355 if (first)
23356 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23357 else
23358 pixels += px;
23359 cdr = XCDR (cdr);
23360 }
23361 if (EQ (car, Qminus))
23362 pixels = -pixels;
23363 return OK_PIXELS (pixels);
23364 }
23365
23366 car = buffer_local_value_1 (car, it->w->contents);
23367 if (EQ (car, Qunbound))
23368 car = Qnil;
23369 }
23370
23371 if (INTEGERP (car) || FLOATP (car))
23372 {
23373 double fact;
23374 pixels = XFLOATINT (car);
23375 if (NILP (cdr))
23376 return OK_PIXELS (pixels);
23377 if (calc_pixel_width_or_height (&fact, it, cdr,
23378 font, width_p, align_to))
23379 return OK_PIXELS (pixels * fact);
23380 return 0;
23381 }
23382
23383 return 0;
23384 }
23385
23386 return 0;
23387 }
23388
23389 \f
23390 /***********************************************************************
23391 Glyph Display
23392 ***********************************************************************/
23393
23394 #ifdef HAVE_WINDOW_SYSTEM
23395
23396 #ifdef GLYPH_DEBUG
23397
23398 void
23399 dump_glyph_string (struct glyph_string *s)
23400 {
23401 fprintf (stderr, "glyph string\n");
23402 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23403 s->x, s->y, s->width, s->height);
23404 fprintf (stderr, " ybase = %d\n", s->ybase);
23405 fprintf (stderr, " hl = %d\n", s->hl);
23406 fprintf (stderr, " left overhang = %d, right = %d\n",
23407 s->left_overhang, s->right_overhang);
23408 fprintf (stderr, " nchars = %d\n", s->nchars);
23409 fprintf (stderr, " extends to end of line = %d\n",
23410 s->extends_to_end_of_line_p);
23411 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23412 fprintf (stderr, " bg width = %d\n", s->background_width);
23413 }
23414
23415 #endif /* GLYPH_DEBUG */
23416
23417 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23418 of XChar2b structures for S; it can't be allocated in
23419 init_glyph_string because it must be allocated via `alloca'. W
23420 is the window on which S is drawn. ROW and AREA are the glyph row
23421 and area within the row from which S is constructed. START is the
23422 index of the first glyph structure covered by S. HL is a
23423 face-override for drawing S. */
23424
23425 #ifdef HAVE_NTGUI
23426 #define OPTIONAL_HDC(hdc) HDC hdc,
23427 #define DECLARE_HDC(hdc) HDC hdc;
23428 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23429 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23430 #endif
23431
23432 #ifndef OPTIONAL_HDC
23433 #define OPTIONAL_HDC(hdc)
23434 #define DECLARE_HDC(hdc)
23435 #define ALLOCATE_HDC(hdc, f)
23436 #define RELEASE_HDC(hdc, f)
23437 #endif
23438
23439 static void
23440 init_glyph_string (struct glyph_string *s,
23441 OPTIONAL_HDC (hdc)
23442 XChar2b *char2b, struct window *w, struct glyph_row *row,
23443 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23444 {
23445 memset (s, 0, sizeof *s);
23446 s->w = w;
23447 s->f = XFRAME (w->frame);
23448 #ifdef HAVE_NTGUI
23449 s->hdc = hdc;
23450 #endif
23451 s->display = FRAME_X_DISPLAY (s->f);
23452 s->window = FRAME_X_WINDOW (s->f);
23453 s->char2b = char2b;
23454 s->hl = hl;
23455 s->row = row;
23456 s->area = area;
23457 s->first_glyph = row->glyphs[area] + start;
23458 s->height = row->height;
23459 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23460 s->ybase = s->y + row->ascent;
23461 }
23462
23463
23464 /* Append the list of glyph strings with head H and tail T to the list
23465 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23466
23467 static void
23468 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23469 struct glyph_string *h, struct glyph_string *t)
23470 {
23471 if (h)
23472 {
23473 if (*head)
23474 (*tail)->next = h;
23475 else
23476 *head = h;
23477 h->prev = *tail;
23478 *tail = t;
23479 }
23480 }
23481
23482
23483 /* Prepend the list of glyph strings with head H and tail T to the
23484 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23485 result. */
23486
23487 static void
23488 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23489 struct glyph_string *h, struct glyph_string *t)
23490 {
23491 if (h)
23492 {
23493 if (*head)
23494 (*head)->prev = t;
23495 else
23496 *tail = t;
23497 t->next = *head;
23498 *head = h;
23499 }
23500 }
23501
23502
23503 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23504 Set *HEAD and *TAIL to the resulting list. */
23505
23506 static void
23507 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23508 struct glyph_string *s)
23509 {
23510 s->next = s->prev = NULL;
23511 append_glyph_string_lists (head, tail, s, s);
23512 }
23513
23514
23515 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23516 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23517 make sure that X resources for the face returned are allocated.
23518 Value is a pointer to a realized face that is ready for display if
23519 DISPLAY_P is non-zero. */
23520
23521 static struct face *
23522 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23523 XChar2b *char2b, int display_p)
23524 {
23525 struct face *face = FACE_FROM_ID (f, face_id);
23526 unsigned code = 0;
23527
23528 if (face->font)
23529 {
23530 code = face->font->driver->encode_char (face->font, c);
23531
23532 if (code == FONT_INVALID_CODE)
23533 code = 0;
23534 }
23535 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23536
23537 /* Make sure X resources of the face are allocated. */
23538 #ifdef HAVE_X_WINDOWS
23539 if (display_p)
23540 #endif
23541 {
23542 eassert (face != NULL);
23543 PREPARE_FACE_FOR_DISPLAY (f, face);
23544 }
23545
23546 return face;
23547 }
23548
23549
23550 /* Get face and two-byte form of character glyph GLYPH on frame F.
23551 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23552 a pointer to a realized face that is ready for display. */
23553
23554 static struct face *
23555 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23556 XChar2b *char2b, int *two_byte_p)
23557 {
23558 struct face *face;
23559 unsigned code = 0;
23560
23561 eassert (glyph->type == CHAR_GLYPH);
23562 face = FACE_FROM_ID (f, glyph->face_id);
23563
23564 /* Make sure X resources of the face are allocated. */
23565 eassert (face != NULL);
23566 PREPARE_FACE_FOR_DISPLAY (f, face);
23567
23568 if (two_byte_p)
23569 *two_byte_p = 0;
23570
23571 if (face->font)
23572 {
23573 if (CHAR_BYTE8_P (glyph->u.ch))
23574 code = CHAR_TO_BYTE8 (glyph->u.ch);
23575 else
23576 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23577
23578 if (code == FONT_INVALID_CODE)
23579 code = 0;
23580 }
23581
23582 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23583 return face;
23584 }
23585
23586
23587 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23588 Return 1 if FONT has a glyph for C, otherwise return 0. */
23589
23590 static int
23591 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23592 {
23593 unsigned code;
23594
23595 if (CHAR_BYTE8_P (c))
23596 code = CHAR_TO_BYTE8 (c);
23597 else
23598 code = font->driver->encode_char (font, c);
23599
23600 if (code == FONT_INVALID_CODE)
23601 return 0;
23602 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23603 return 1;
23604 }
23605
23606
23607 /* Fill glyph string S with composition components specified by S->cmp.
23608
23609 BASE_FACE is the base face of the composition.
23610 S->cmp_from is the index of the first component for S.
23611
23612 OVERLAPS non-zero means S should draw the foreground only, and use
23613 its physical height for clipping. See also draw_glyphs.
23614
23615 Value is the index of a component not in S. */
23616
23617 static int
23618 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23619 int overlaps)
23620 {
23621 int i;
23622 /* For all glyphs of this composition, starting at the offset
23623 S->cmp_from, until we reach the end of the definition or encounter a
23624 glyph that requires the different face, add it to S. */
23625 struct face *face;
23626
23627 eassert (s);
23628
23629 s->for_overlaps = overlaps;
23630 s->face = NULL;
23631 s->font = NULL;
23632 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23633 {
23634 int c = COMPOSITION_GLYPH (s->cmp, i);
23635
23636 /* TAB in a composition means display glyphs with padding space
23637 on the left or right. */
23638 if (c != '\t')
23639 {
23640 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23641 -1, Qnil);
23642
23643 face = get_char_face_and_encoding (s->f, c, face_id,
23644 s->char2b + i, 1);
23645 if (face)
23646 {
23647 if (! s->face)
23648 {
23649 s->face = face;
23650 s->font = s->face->font;
23651 }
23652 else if (s->face != face)
23653 break;
23654 }
23655 }
23656 ++s->nchars;
23657 }
23658 s->cmp_to = i;
23659
23660 if (s->face == NULL)
23661 {
23662 s->face = base_face->ascii_face;
23663 s->font = s->face->font;
23664 }
23665
23666 /* All glyph strings for the same composition has the same width,
23667 i.e. the width set for the first component of the composition. */
23668 s->width = s->first_glyph->pixel_width;
23669
23670 /* If the specified font could not be loaded, use the frame's
23671 default font, but record the fact that we couldn't load it in
23672 the glyph string so that we can draw rectangles for the
23673 characters of the glyph string. */
23674 if (s->font == NULL)
23675 {
23676 s->font_not_found_p = 1;
23677 s->font = FRAME_FONT (s->f);
23678 }
23679
23680 /* Adjust base line for subscript/superscript text. */
23681 s->ybase += s->first_glyph->voffset;
23682
23683 /* This glyph string must always be drawn with 16-bit functions. */
23684 s->two_byte_p = 1;
23685
23686 return s->cmp_to;
23687 }
23688
23689 static int
23690 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23691 int start, int end, int overlaps)
23692 {
23693 struct glyph *glyph, *last;
23694 Lisp_Object lgstring;
23695 int i;
23696
23697 s->for_overlaps = overlaps;
23698 glyph = s->row->glyphs[s->area] + start;
23699 last = s->row->glyphs[s->area] + end;
23700 s->cmp_id = glyph->u.cmp.id;
23701 s->cmp_from = glyph->slice.cmp.from;
23702 s->cmp_to = glyph->slice.cmp.to + 1;
23703 s->face = FACE_FROM_ID (s->f, face_id);
23704 lgstring = composition_gstring_from_id (s->cmp_id);
23705 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23706 glyph++;
23707 while (glyph < last
23708 && glyph->u.cmp.automatic
23709 && glyph->u.cmp.id == s->cmp_id
23710 && s->cmp_to == glyph->slice.cmp.from)
23711 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23712
23713 for (i = s->cmp_from; i < s->cmp_to; i++)
23714 {
23715 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23716 unsigned code = LGLYPH_CODE (lglyph);
23717
23718 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23719 }
23720 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23721 return glyph - s->row->glyphs[s->area];
23722 }
23723
23724
23725 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23726 See the comment of fill_glyph_string for arguments.
23727 Value is the index of the first glyph not in S. */
23728
23729
23730 static int
23731 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23732 int start, int end, int overlaps)
23733 {
23734 struct glyph *glyph, *last;
23735 int voffset;
23736
23737 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23738 s->for_overlaps = overlaps;
23739 glyph = s->row->glyphs[s->area] + start;
23740 last = s->row->glyphs[s->area] + end;
23741 voffset = glyph->voffset;
23742 s->face = FACE_FROM_ID (s->f, face_id);
23743 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23744 s->nchars = 1;
23745 s->width = glyph->pixel_width;
23746 glyph++;
23747 while (glyph < last
23748 && glyph->type == GLYPHLESS_GLYPH
23749 && glyph->voffset == voffset
23750 && glyph->face_id == face_id)
23751 {
23752 s->nchars++;
23753 s->width += glyph->pixel_width;
23754 glyph++;
23755 }
23756 s->ybase += voffset;
23757 return glyph - s->row->glyphs[s->area];
23758 }
23759
23760
23761 /* Fill glyph string S from a sequence of character glyphs.
23762
23763 FACE_ID is the face id of the string. START is the index of the
23764 first glyph to consider, END is the index of the last + 1.
23765 OVERLAPS non-zero means S should draw the foreground only, and use
23766 its physical height for clipping. See also draw_glyphs.
23767
23768 Value is the index of the first glyph not in S. */
23769
23770 static int
23771 fill_glyph_string (struct glyph_string *s, int face_id,
23772 int start, int end, int overlaps)
23773 {
23774 struct glyph *glyph, *last;
23775 int voffset;
23776 int glyph_not_available_p;
23777
23778 eassert (s->f == XFRAME (s->w->frame));
23779 eassert (s->nchars == 0);
23780 eassert (start >= 0 && end > start);
23781
23782 s->for_overlaps = overlaps;
23783 glyph = s->row->glyphs[s->area] + start;
23784 last = s->row->glyphs[s->area] + end;
23785 voffset = glyph->voffset;
23786 s->padding_p = glyph->padding_p;
23787 glyph_not_available_p = glyph->glyph_not_available_p;
23788
23789 while (glyph < last
23790 && glyph->type == CHAR_GLYPH
23791 && glyph->voffset == voffset
23792 /* Same face id implies same font, nowadays. */
23793 && glyph->face_id == face_id
23794 && glyph->glyph_not_available_p == glyph_not_available_p)
23795 {
23796 int two_byte_p;
23797
23798 s->face = get_glyph_face_and_encoding (s->f, glyph,
23799 s->char2b + s->nchars,
23800 &two_byte_p);
23801 s->two_byte_p = two_byte_p;
23802 ++s->nchars;
23803 eassert (s->nchars <= end - start);
23804 s->width += glyph->pixel_width;
23805 if (glyph++->padding_p != s->padding_p)
23806 break;
23807 }
23808
23809 s->font = s->face->font;
23810
23811 /* If the specified font could not be loaded, use the frame's font,
23812 but record the fact that we couldn't load it in
23813 S->font_not_found_p so that we can draw rectangles for the
23814 characters of the glyph string. */
23815 if (s->font == NULL || glyph_not_available_p)
23816 {
23817 s->font_not_found_p = 1;
23818 s->font = FRAME_FONT (s->f);
23819 }
23820
23821 /* Adjust base line for subscript/superscript text. */
23822 s->ybase += voffset;
23823
23824 eassert (s->face && s->face->gc);
23825 return glyph - s->row->glyphs[s->area];
23826 }
23827
23828
23829 /* Fill glyph string S from image glyph S->first_glyph. */
23830
23831 static void
23832 fill_image_glyph_string (struct glyph_string *s)
23833 {
23834 eassert (s->first_glyph->type == IMAGE_GLYPH);
23835 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23836 eassert (s->img);
23837 s->slice = s->first_glyph->slice.img;
23838 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23839 s->font = s->face->font;
23840 s->width = s->first_glyph->pixel_width;
23841
23842 /* Adjust base line for subscript/superscript text. */
23843 s->ybase += s->first_glyph->voffset;
23844 }
23845
23846
23847 /* Fill glyph string S from a sequence of stretch glyphs.
23848
23849 START is the index of the first glyph to consider,
23850 END is the index of the last + 1.
23851
23852 Value is the index of the first glyph not in S. */
23853
23854 static int
23855 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23856 {
23857 struct glyph *glyph, *last;
23858 int voffset, face_id;
23859
23860 eassert (s->first_glyph->type == STRETCH_GLYPH);
23861
23862 glyph = s->row->glyphs[s->area] + start;
23863 last = s->row->glyphs[s->area] + end;
23864 face_id = glyph->face_id;
23865 s->face = FACE_FROM_ID (s->f, face_id);
23866 s->font = s->face->font;
23867 s->width = glyph->pixel_width;
23868 s->nchars = 1;
23869 voffset = glyph->voffset;
23870
23871 for (++glyph;
23872 (glyph < last
23873 && glyph->type == STRETCH_GLYPH
23874 && glyph->voffset == voffset
23875 && glyph->face_id == face_id);
23876 ++glyph)
23877 s->width += glyph->pixel_width;
23878
23879 /* Adjust base line for subscript/superscript text. */
23880 s->ybase += voffset;
23881
23882 /* The case that face->gc == 0 is handled when drawing the glyph
23883 string by calling PREPARE_FACE_FOR_DISPLAY. */
23884 eassert (s->face);
23885 return glyph - s->row->glyphs[s->area];
23886 }
23887
23888 static struct font_metrics *
23889 get_per_char_metric (struct font *font, XChar2b *char2b)
23890 {
23891 static struct font_metrics metrics;
23892 unsigned code;
23893
23894 if (! font)
23895 return NULL;
23896 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23897 if (code == FONT_INVALID_CODE)
23898 return NULL;
23899 font->driver->text_extents (font, &code, 1, &metrics);
23900 return &metrics;
23901 }
23902
23903 /* EXPORT for RIF:
23904 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23905 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23906 assumed to be zero. */
23907
23908 void
23909 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23910 {
23911 *left = *right = 0;
23912
23913 if (glyph->type == CHAR_GLYPH)
23914 {
23915 struct face *face;
23916 XChar2b char2b;
23917 struct font_metrics *pcm;
23918
23919 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23920 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23921 {
23922 if (pcm->rbearing > pcm->width)
23923 *right = pcm->rbearing - pcm->width;
23924 if (pcm->lbearing < 0)
23925 *left = -pcm->lbearing;
23926 }
23927 }
23928 else if (glyph->type == COMPOSITE_GLYPH)
23929 {
23930 if (! glyph->u.cmp.automatic)
23931 {
23932 struct composition *cmp = composition_table[glyph->u.cmp.id];
23933
23934 if (cmp->rbearing > cmp->pixel_width)
23935 *right = cmp->rbearing - cmp->pixel_width;
23936 if (cmp->lbearing < 0)
23937 *left = - cmp->lbearing;
23938 }
23939 else
23940 {
23941 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23942 struct font_metrics metrics;
23943
23944 composition_gstring_width (gstring, glyph->slice.cmp.from,
23945 glyph->slice.cmp.to + 1, &metrics);
23946 if (metrics.rbearing > metrics.width)
23947 *right = metrics.rbearing - metrics.width;
23948 if (metrics.lbearing < 0)
23949 *left = - metrics.lbearing;
23950 }
23951 }
23952 }
23953
23954
23955 /* Return the index of the first glyph preceding glyph string S that
23956 is overwritten by S because of S's left overhang. Value is -1
23957 if no glyphs are overwritten. */
23958
23959 static int
23960 left_overwritten (struct glyph_string *s)
23961 {
23962 int k;
23963
23964 if (s->left_overhang)
23965 {
23966 int x = 0, i;
23967 struct glyph *glyphs = s->row->glyphs[s->area];
23968 int first = s->first_glyph - glyphs;
23969
23970 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23971 x -= glyphs[i].pixel_width;
23972
23973 k = i + 1;
23974 }
23975 else
23976 k = -1;
23977
23978 return k;
23979 }
23980
23981
23982 /* Return the index of the first glyph preceding glyph string S that
23983 is overwriting S because of its right overhang. Value is -1 if no
23984 glyph in front of S overwrites S. */
23985
23986 static int
23987 left_overwriting (struct glyph_string *s)
23988 {
23989 int i, k, x;
23990 struct glyph *glyphs = s->row->glyphs[s->area];
23991 int first = s->first_glyph - glyphs;
23992
23993 k = -1;
23994 x = 0;
23995 for (i = first - 1; i >= 0; --i)
23996 {
23997 int left, right;
23998 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23999 if (x + right > 0)
24000 k = i;
24001 x -= glyphs[i].pixel_width;
24002 }
24003
24004 return k;
24005 }
24006
24007
24008 /* Return the index of the last glyph following glyph string S that is
24009 overwritten by S because of S's right overhang. Value is -1 if
24010 no such glyph is found. */
24011
24012 static int
24013 right_overwritten (struct glyph_string *s)
24014 {
24015 int k = -1;
24016
24017 if (s->right_overhang)
24018 {
24019 int x = 0, i;
24020 struct glyph *glyphs = s->row->glyphs[s->area];
24021 int first = (s->first_glyph - glyphs
24022 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24023 int end = s->row->used[s->area];
24024
24025 for (i = first; i < end && s->right_overhang > x; ++i)
24026 x += glyphs[i].pixel_width;
24027
24028 k = i;
24029 }
24030
24031 return k;
24032 }
24033
24034
24035 /* Return the index of the last glyph following glyph string S that
24036 overwrites S because of its left overhang. Value is negative
24037 if no such glyph is found. */
24038
24039 static int
24040 right_overwriting (struct glyph_string *s)
24041 {
24042 int i, k, x;
24043 int end = s->row->used[s->area];
24044 struct glyph *glyphs = s->row->glyphs[s->area];
24045 int first = (s->first_glyph - glyphs
24046 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24047
24048 k = -1;
24049 x = 0;
24050 for (i = first; i < end; ++i)
24051 {
24052 int left, right;
24053 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24054 if (x - left < 0)
24055 k = i;
24056 x += glyphs[i].pixel_width;
24057 }
24058
24059 return k;
24060 }
24061
24062
24063 /* Set background width of glyph string S. START is the index of the
24064 first glyph following S. LAST_X is the right-most x-position + 1
24065 in the drawing area. */
24066
24067 static void
24068 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24069 {
24070 /* If the face of this glyph string has to be drawn to the end of
24071 the drawing area, set S->extends_to_end_of_line_p. */
24072
24073 if (start == s->row->used[s->area]
24074 && ((s->row->fill_line_p
24075 && (s->hl == DRAW_NORMAL_TEXT
24076 || s->hl == DRAW_IMAGE_RAISED
24077 || s->hl == DRAW_IMAGE_SUNKEN))
24078 || s->hl == DRAW_MOUSE_FACE))
24079 s->extends_to_end_of_line_p = 1;
24080
24081 /* If S extends its face to the end of the line, set its
24082 background_width to the distance to the right edge of the drawing
24083 area. */
24084 if (s->extends_to_end_of_line_p)
24085 s->background_width = last_x - s->x + 1;
24086 else
24087 s->background_width = s->width;
24088 }
24089
24090
24091 /* Compute overhangs and x-positions for glyph string S and its
24092 predecessors, or successors. X is the starting x-position for S.
24093 BACKWARD_P non-zero means process predecessors. */
24094
24095 static void
24096 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24097 {
24098 if (backward_p)
24099 {
24100 while (s)
24101 {
24102 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24103 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24104 x -= s->width;
24105 s->x = x;
24106 s = s->prev;
24107 }
24108 }
24109 else
24110 {
24111 while (s)
24112 {
24113 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24114 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24115 s->x = x;
24116 x += s->width;
24117 s = s->next;
24118 }
24119 }
24120 }
24121
24122
24123
24124 /* The following macros are only called from draw_glyphs below.
24125 They reference the following parameters of that function directly:
24126 `w', `row', `area', and `overlap_p'
24127 as well as the following local variables:
24128 `s', `f', and `hdc' (in W32) */
24129
24130 #ifdef HAVE_NTGUI
24131 /* On W32, silently add local `hdc' variable to argument list of
24132 init_glyph_string. */
24133 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24134 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24135 #else
24136 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24137 init_glyph_string (s, char2b, w, row, area, start, hl)
24138 #endif
24139
24140 /* Add a glyph string for a stretch glyph to the list of strings
24141 between HEAD and TAIL. START is the index of the stretch glyph in
24142 row area AREA of glyph row ROW. END is the index of the last glyph
24143 in that glyph row area. X is the current output position assigned
24144 to the new glyph string constructed. HL overrides that face of the
24145 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24146 is the right-most x-position of the drawing area. */
24147
24148 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24149 and below -- keep them on one line. */
24150 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24151 do \
24152 { \
24153 s = alloca (sizeof *s); \
24154 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24155 START = fill_stretch_glyph_string (s, START, END); \
24156 append_glyph_string (&HEAD, &TAIL, s); \
24157 s->x = (X); \
24158 } \
24159 while (0)
24160
24161
24162 /* Add a glyph string for an image glyph to the list of strings
24163 between HEAD and TAIL. START is the index of the image glyph in
24164 row area AREA of glyph row ROW. END is the index of the last glyph
24165 in that glyph row area. X is the current output position assigned
24166 to the new glyph string constructed. HL overrides that face of the
24167 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24168 is the right-most x-position of the drawing area. */
24169
24170 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24171 do \
24172 { \
24173 s = alloca (sizeof *s); \
24174 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24175 fill_image_glyph_string (s); \
24176 append_glyph_string (&HEAD, &TAIL, s); \
24177 ++START; \
24178 s->x = (X); \
24179 } \
24180 while (0)
24181
24182
24183 /* Add a glyph string for a sequence of character glyphs to the list
24184 of strings between HEAD and TAIL. START is the index of the first
24185 glyph in row area AREA of glyph row ROW that is part of the new
24186 glyph string. END is the index of the last glyph in that glyph row
24187 area. X is the current output position assigned to the new glyph
24188 string constructed. HL overrides that face of the glyph; e.g. it
24189 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24190 right-most x-position of the drawing area. */
24191
24192 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24193 do \
24194 { \
24195 int face_id; \
24196 XChar2b *char2b; \
24197 \
24198 face_id = (row)->glyphs[area][START].face_id; \
24199 \
24200 s = alloca (sizeof *s); \
24201 char2b = alloca ((END - START) * sizeof *char2b); \
24202 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24203 append_glyph_string (&HEAD, &TAIL, s); \
24204 s->x = (X); \
24205 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24206 } \
24207 while (0)
24208
24209
24210 /* Add a glyph string for a composite sequence to the list of strings
24211 between HEAD and TAIL. START is the index of the first glyph in
24212 row area AREA of glyph row ROW that is part of the new glyph
24213 string. END is the index of the last glyph in that glyph row area.
24214 X is the current output position assigned to the new glyph string
24215 constructed. HL overrides that face of the glyph; e.g. it is
24216 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24217 x-position of the drawing area. */
24218
24219 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24220 do { \
24221 int face_id = (row)->glyphs[area][START].face_id; \
24222 struct face *base_face = FACE_FROM_ID (f, face_id); \
24223 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24224 struct composition *cmp = composition_table[cmp_id]; \
24225 XChar2b *char2b; \
24226 struct glyph_string *first_s = NULL; \
24227 int n; \
24228 \
24229 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24230 \
24231 /* Make glyph_strings for each glyph sequence that is drawable by \
24232 the same face, and append them to HEAD/TAIL. */ \
24233 for (n = 0; n < cmp->glyph_len;) \
24234 { \
24235 s = alloca (sizeof *s); \
24236 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24237 append_glyph_string (&(HEAD), &(TAIL), s); \
24238 s->cmp = cmp; \
24239 s->cmp_from = n; \
24240 s->x = (X); \
24241 if (n == 0) \
24242 first_s = s; \
24243 n = fill_composite_glyph_string (s, base_face, overlaps); \
24244 } \
24245 \
24246 ++START; \
24247 s = first_s; \
24248 } while (0)
24249
24250
24251 /* Add a glyph string for a glyph-string sequence to the list of strings
24252 between HEAD and TAIL. */
24253
24254 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24255 do { \
24256 int face_id; \
24257 XChar2b *char2b; \
24258 Lisp_Object gstring; \
24259 \
24260 face_id = (row)->glyphs[area][START].face_id; \
24261 gstring = (composition_gstring_from_id \
24262 ((row)->glyphs[area][START].u.cmp.id)); \
24263 s = alloca (sizeof *s); \
24264 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24265 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24266 append_glyph_string (&(HEAD), &(TAIL), s); \
24267 s->x = (X); \
24268 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24269 } while (0)
24270
24271
24272 /* Add a glyph string for a sequence of glyphless character's glyphs
24273 to the list of strings between HEAD and TAIL. The meanings of
24274 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24275
24276 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24277 do \
24278 { \
24279 int face_id; \
24280 \
24281 face_id = (row)->glyphs[area][START].face_id; \
24282 \
24283 s = alloca (sizeof *s); \
24284 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24285 append_glyph_string (&HEAD, &TAIL, s); \
24286 s->x = (X); \
24287 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24288 overlaps); \
24289 } \
24290 while (0)
24291
24292
24293 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24294 of AREA of glyph row ROW on window W between indices START and END.
24295 HL overrides the face for drawing glyph strings, e.g. it is
24296 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24297 x-positions of the drawing area.
24298
24299 This is an ugly monster macro construct because we must use alloca
24300 to allocate glyph strings (because draw_glyphs can be called
24301 asynchronously). */
24302
24303 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24304 do \
24305 { \
24306 HEAD = TAIL = NULL; \
24307 while (START < END) \
24308 { \
24309 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24310 switch (first_glyph->type) \
24311 { \
24312 case CHAR_GLYPH: \
24313 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24314 HL, X, LAST_X); \
24315 break; \
24316 \
24317 case COMPOSITE_GLYPH: \
24318 if (first_glyph->u.cmp.automatic) \
24319 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24320 HL, X, LAST_X); \
24321 else \
24322 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24323 HL, X, LAST_X); \
24324 break; \
24325 \
24326 case STRETCH_GLYPH: \
24327 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24328 HL, X, LAST_X); \
24329 break; \
24330 \
24331 case IMAGE_GLYPH: \
24332 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24333 HL, X, LAST_X); \
24334 break; \
24335 \
24336 case GLYPHLESS_GLYPH: \
24337 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24338 HL, X, LAST_X); \
24339 break; \
24340 \
24341 default: \
24342 emacs_abort (); \
24343 } \
24344 \
24345 if (s) \
24346 { \
24347 set_glyph_string_background_width (s, START, LAST_X); \
24348 (X) += s->width; \
24349 } \
24350 } \
24351 } while (0)
24352
24353
24354 /* Draw glyphs between START and END in AREA of ROW on window W,
24355 starting at x-position X. X is relative to AREA in W. HL is a
24356 face-override with the following meaning:
24357
24358 DRAW_NORMAL_TEXT draw normally
24359 DRAW_CURSOR draw in cursor face
24360 DRAW_MOUSE_FACE draw in mouse face.
24361 DRAW_INVERSE_VIDEO draw in mode line face
24362 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24363 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24364
24365 If OVERLAPS is non-zero, draw only the foreground of characters and
24366 clip to the physical height of ROW. Non-zero value also defines
24367 the overlapping part to be drawn:
24368
24369 OVERLAPS_PRED overlap with preceding rows
24370 OVERLAPS_SUCC overlap with succeeding rows
24371 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24372 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24373
24374 Value is the x-position reached, relative to AREA of W. */
24375
24376 static int
24377 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24378 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24379 enum draw_glyphs_face hl, int overlaps)
24380 {
24381 struct glyph_string *head, *tail;
24382 struct glyph_string *s;
24383 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24384 int i, j, x_reached, last_x, area_left = 0;
24385 struct frame *f = XFRAME (WINDOW_FRAME (w));
24386 DECLARE_HDC (hdc);
24387
24388 ALLOCATE_HDC (hdc, f);
24389
24390 /* Let's rather be paranoid than getting a SEGV. */
24391 end = min (end, row->used[area]);
24392 start = clip_to_bounds (0, start, end);
24393
24394 /* Translate X to frame coordinates. Set last_x to the right
24395 end of the drawing area. */
24396 if (row->full_width_p)
24397 {
24398 /* X is relative to the left edge of W, without scroll bars
24399 or fringes. */
24400 area_left = WINDOW_LEFT_EDGE_X (w);
24401 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24402 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24403 }
24404 else
24405 {
24406 area_left = window_box_left (w, area);
24407 last_x = area_left + window_box_width (w, area);
24408 }
24409 x += area_left;
24410
24411 /* Build a doubly-linked list of glyph_string structures between
24412 head and tail from what we have to draw. Note that the macro
24413 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24414 the reason we use a separate variable `i'. */
24415 i = start;
24416 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24417 if (tail)
24418 x_reached = tail->x + tail->background_width;
24419 else
24420 x_reached = x;
24421
24422 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24423 the row, redraw some glyphs in front or following the glyph
24424 strings built above. */
24425 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24426 {
24427 struct glyph_string *h, *t;
24428 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24429 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24430 int check_mouse_face = 0;
24431 int dummy_x = 0;
24432
24433 /* If mouse highlighting is on, we may need to draw adjacent
24434 glyphs using mouse-face highlighting. */
24435 if (area == TEXT_AREA && row->mouse_face_p
24436 && hlinfo->mouse_face_beg_row >= 0
24437 && hlinfo->mouse_face_end_row >= 0)
24438 {
24439 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24440
24441 if (row_vpos >= hlinfo->mouse_face_beg_row
24442 && row_vpos <= hlinfo->mouse_face_end_row)
24443 {
24444 check_mouse_face = 1;
24445 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24446 ? hlinfo->mouse_face_beg_col : 0;
24447 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24448 ? hlinfo->mouse_face_end_col
24449 : row->used[TEXT_AREA];
24450 }
24451 }
24452
24453 /* Compute overhangs for all glyph strings. */
24454 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24455 for (s = head; s; s = s->next)
24456 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24457
24458 /* Prepend glyph strings for glyphs in front of the first glyph
24459 string that are overwritten because of the first glyph
24460 string's left overhang. The background of all strings
24461 prepended must be drawn because the first glyph string
24462 draws over it. */
24463 i = left_overwritten (head);
24464 if (i >= 0)
24465 {
24466 enum draw_glyphs_face overlap_hl;
24467
24468 /* If this row contains mouse highlighting, attempt to draw
24469 the overlapped glyphs with the correct highlight. This
24470 code fails if the overlap encompasses more than one glyph
24471 and mouse-highlight spans only some of these glyphs.
24472 However, making it work perfectly involves a lot more
24473 code, and I don't know if the pathological case occurs in
24474 practice, so we'll stick to this for now. --- cyd */
24475 if (check_mouse_face
24476 && mouse_beg_col < start && mouse_end_col > i)
24477 overlap_hl = DRAW_MOUSE_FACE;
24478 else
24479 overlap_hl = DRAW_NORMAL_TEXT;
24480
24481 j = i;
24482 BUILD_GLYPH_STRINGS (j, start, h, t,
24483 overlap_hl, dummy_x, last_x);
24484 start = i;
24485 compute_overhangs_and_x (t, head->x, 1);
24486 prepend_glyph_string_lists (&head, &tail, h, t);
24487 clip_head = head;
24488 }
24489
24490 /* Prepend glyph strings for glyphs in front of the first glyph
24491 string that overwrite that glyph string because of their
24492 right overhang. For these strings, only the foreground must
24493 be drawn, because it draws over the glyph string at `head'.
24494 The background must not be drawn because this would overwrite
24495 right overhangs of preceding glyphs for which no glyph
24496 strings exist. */
24497 i = left_overwriting (head);
24498 if (i >= 0)
24499 {
24500 enum draw_glyphs_face overlap_hl;
24501
24502 if (check_mouse_face
24503 && mouse_beg_col < start && mouse_end_col > i)
24504 overlap_hl = DRAW_MOUSE_FACE;
24505 else
24506 overlap_hl = DRAW_NORMAL_TEXT;
24507
24508 clip_head = head;
24509 BUILD_GLYPH_STRINGS (i, start, h, t,
24510 overlap_hl, dummy_x, last_x);
24511 for (s = h; s; s = s->next)
24512 s->background_filled_p = 1;
24513 compute_overhangs_and_x (t, head->x, 1);
24514 prepend_glyph_string_lists (&head, &tail, h, t);
24515 }
24516
24517 /* Append glyphs strings for glyphs following the last glyph
24518 string tail that are overwritten by tail. The background of
24519 these strings has to be drawn because tail's foreground draws
24520 over it. */
24521 i = right_overwritten (tail);
24522 if (i >= 0)
24523 {
24524 enum draw_glyphs_face overlap_hl;
24525
24526 if (check_mouse_face
24527 && mouse_beg_col < i && mouse_end_col > end)
24528 overlap_hl = DRAW_MOUSE_FACE;
24529 else
24530 overlap_hl = DRAW_NORMAL_TEXT;
24531
24532 BUILD_GLYPH_STRINGS (end, i, h, t,
24533 overlap_hl, x, last_x);
24534 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24535 we don't have `end = i;' here. */
24536 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24537 append_glyph_string_lists (&head, &tail, h, t);
24538 clip_tail = tail;
24539 }
24540
24541 /* Append glyph strings for glyphs following the last glyph
24542 string tail that overwrite tail. The foreground of such
24543 glyphs has to be drawn because it writes into the background
24544 of tail. The background must not be drawn because it could
24545 paint over the foreground of following glyphs. */
24546 i = right_overwriting (tail);
24547 if (i >= 0)
24548 {
24549 enum draw_glyphs_face overlap_hl;
24550 if (check_mouse_face
24551 && mouse_beg_col < i && mouse_end_col > end)
24552 overlap_hl = DRAW_MOUSE_FACE;
24553 else
24554 overlap_hl = DRAW_NORMAL_TEXT;
24555
24556 clip_tail = tail;
24557 i++; /* We must include the Ith glyph. */
24558 BUILD_GLYPH_STRINGS (end, i, h, t,
24559 overlap_hl, x, last_x);
24560 for (s = h; s; s = s->next)
24561 s->background_filled_p = 1;
24562 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24563 append_glyph_string_lists (&head, &tail, h, t);
24564 }
24565 if (clip_head || clip_tail)
24566 for (s = head; s; s = s->next)
24567 {
24568 s->clip_head = clip_head;
24569 s->clip_tail = clip_tail;
24570 }
24571 }
24572
24573 /* Draw all strings. */
24574 for (s = head; s; s = s->next)
24575 FRAME_RIF (f)->draw_glyph_string (s);
24576
24577 #ifndef HAVE_NS
24578 /* When focus a sole frame and move horizontally, this sets on_p to 0
24579 causing a failure to erase prev cursor position. */
24580 if (area == TEXT_AREA
24581 && !row->full_width_p
24582 /* When drawing overlapping rows, only the glyph strings'
24583 foreground is drawn, which doesn't erase a cursor
24584 completely. */
24585 && !overlaps)
24586 {
24587 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24588 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24589 : (tail ? tail->x + tail->background_width : x));
24590 x0 -= area_left;
24591 x1 -= area_left;
24592
24593 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24594 row->y, MATRIX_ROW_BOTTOM_Y (row));
24595 }
24596 #endif
24597
24598 /* Value is the x-position up to which drawn, relative to AREA of W.
24599 This doesn't include parts drawn because of overhangs. */
24600 if (row->full_width_p)
24601 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24602 else
24603 x_reached -= area_left;
24604
24605 RELEASE_HDC (hdc, f);
24606
24607 return x_reached;
24608 }
24609
24610 /* Expand row matrix if too narrow. Don't expand if area
24611 is not present. */
24612
24613 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24614 { \
24615 if (!it->f->fonts_changed \
24616 && (it->glyph_row->glyphs[area] \
24617 < it->glyph_row->glyphs[area + 1])) \
24618 { \
24619 it->w->ncols_scale_factor++; \
24620 it->f->fonts_changed = 1; \
24621 } \
24622 }
24623
24624 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24625 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24626
24627 static void
24628 append_glyph (struct it *it)
24629 {
24630 struct glyph *glyph;
24631 enum glyph_row_area area = it->area;
24632
24633 eassert (it->glyph_row);
24634 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24635
24636 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24637 if (glyph < it->glyph_row->glyphs[area + 1])
24638 {
24639 /* If the glyph row is reversed, we need to prepend the glyph
24640 rather than append it. */
24641 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24642 {
24643 struct glyph *g;
24644
24645 /* Make room for the additional glyph. */
24646 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24647 g[1] = *g;
24648 glyph = it->glyph_row->glyphs[area];
24649 }
24650 glyph->charpos = CHARPOS (it->position);
24651 glyph->object = it->object;
24652 if (it->pixel_width > 0)
24653 {
24654 glyph->pixel_width = it->pixel_width;
24655 glyph->padding_p = 0;
24656 }
24657 else
24658 {
24659 /* Assure at least 1-pixel width. Otherwise, cursor can't
24660 be displayed correctly. */
24661 glyph->pixel_width = 1;
24662 glyph->padding_p = 1;
24663 }
24664 glyph->ascent = it->ascent;
24665 glyph->descent = it->descent;
24666 glyph->voffset = it->voffset;
24667 glyph->type = CHAR_GLYPH;
24668 glyph->avoid_cursor_p = it->avoid_cursor_p;
24669 glyph->multibyte_p = it->multibyte_p;
24670 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24671 {
24672 /* In R2L rows, the left and the right box edges need to be
24673 drawn in reverse direction. */
24674 glyph->right_box_line_p = it->start_of_box_run_p;
24675 glyph->left_box_line_p = it->end_of_box_run_p;
24676 }
24677 else
24678 {
24679 glyph->left_box_line_p = it->start_of_box_run_p;
24680 glyph->right_box_line_p = it->end_of_box_run_p;
24681 }
24682 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24683 || it->phys_descent > it->descent);
24684 glyph->glyph_not_available_p = it->glyph_not_available_p;
24685 glyph->face_id = it->face_id;
24686 glyph->u.ch = it->char_to_display;
24687 glyph->slice.img = null_glyph_slice;
24688 glyph->font_type = FONT_TYPE_UNKNOWN;
24689 if (it->bidi_p)
24690 {
24691 glyph->resolved_level = it->bidi_it.resolved_level;
24692 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24693 emacs_abort ();
24694 glyph->bidi_type = it->bidi_it.type;
24695 }
24696 else
24697 {
24698 glyph->resolved_level = 0;
24699 glyph->bidi_type = UNKNOWN_BT;
24700 }
24701 ++it->glyph_row->used[area];
24702 }
24703 else
24704 IT_EXPAND_MATRIX_WIDTH (it, area);
24705 }
24706
24707 /* Store one glyph for the composition IT->cmp_it.id in
24708 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24709 non-null. */
24710
24711 static void
24712 append_composite_glyph (struct it *it)
24713 {
24714 struct glyph *glyph;
24715 enum glyph_row_area area = it->area;
24716
24717 eassert (it->glyph_row);
24718
24719 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24720 if (glyph < it->glyph_row->glyphs[area + 1])
24721 {
24722 /* If the glyph row is reversed, we need to prepend the glyph
24723 rather than append it. */
24724 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24725 {
24726 struct glyph *g;
24727
24728 /* Make room for the new glyph. */
24729 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24730 g[1] = *g;
24731 glyph = it->glyph_row->glyphs[it->area];
24732 }
24733 glyph->charpos = it->cmp_it.charpos;
24734 glyph->object = it->object;
24735 glyph->pixel_width = it->pixel_width;
24736 glyph->ascent = it->ascent;
24737 glyph->descent = it->descent;
24738 glyph->voffset = it->voffset;
24739 glyph->type = COMPOSITE_GLYPH;
24740 if (it->cmp_it.ch < 0)
24741 {
24742 glyph->u.cmp.automatic = 0;
24743 glyph->u.cmp.id = it->cmp_it.id;
24744 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24745 }
24746 else
24747 {
24748 glyph->u.cmp.automatic = 1;
24749 glyph->u.cmp.id = it->cmp_it.id;
24750 glyph->slice.cmp.from = it->cmp_it.from;
24751 glyph->slice.cmp.to = it->cmp_it.to - 1;
24752 }
24753 glyph->avoid_cursor_p = it->avoid_cursor_p;
24754 glyph->multibyte_p = it->multibyte_p;
24755 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24756 {
24757 /* In R2L rows, the left and the right box edges need to be
24758 drawn in reverse direction. */
24759 glyph->right_box_line_p = it->start_of_box_run_p;
24760 glyph->left_box_line_p = it->end_of_box_run_p;
24761 }
24762 else
24763 {
24764 glyph->left_box_line_p = it->start_of_box_run_p;
24765 glyph->right_box_line_p = it->end_of_box_run_p;
24766 }
24767 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24768 || it->phys_descent > it->descent);
24769 glyph->padding_p = 0;
24770 glyph->glyph_not_available_p = 0;
24771 glyph->face_id = it->face_id;
24772 glyph->font_type = FONT_TYPE_UNKNOWN;
24773 if (it->bidi_p)
24774 {
24775 glyph->resolved_level = it->bidi_it.resolved_level;
24776 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24777 emacs_abort ();
24778 glyph->bidi_type = it->bidi_it.type;
24779 }
24780 ++it->glyph_row->used[area];
24781 }
24782 else
24783 IT_EXPAND_MATRIX_WIDTH (it, area);
24784 }
24785
24786
24787 /* Change IT->ascent and IT->height according to the setting of
24788 IT->voffset. */
24789
24790 static void
24791 take_vertical_position_into_account (struct it *it)
24792 {
24793 if (it->voffset)
24794 {
24795 if (it->voffset < 0)
24796 /* Increase the ascent so that we can display the text higher
24797 in the line. */
24798 it->ascent -= it->voffset;
24799 else
24800 /* Increase the descent so that we can display the text lower
24801 in the line. */
24802 it->descent += it->voffset;
24803 }
24804 }
24805
24806
24807 /* Produce glyphs/get display metrics for the image IT is loaded with.
24808 See the description of struct display_iterator in dispextern.h for
24809 an overview of struct display_iterator. */
24810
24811 static void
24812 produce_image_glyph (struct it *it)
24813 {
24814 struct image *img;
24815 struct face *face;
24816 int glyph_ascent, crop;
24817 struct glyph_slice slice;
24818
24819 eassert (it->what == IT_IMAGE);
24820
24821 face = FACE_FROM_ID (it->f, it->face_id);
24822 eassert (face);
24823 /* Make sure X resources of the face is loaded. */
24824 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24825
24826 if (it->image_id < 0)
24827 {
24828 /* Fringe bitmap. */
24829 it->ascent = it->phys_ascent = 0;
24830 it->descent = it->phys_descent = 0;
24831 it->pixel_width = 0;
24832 it->nglyphs = 0;
24833 return;
24834 }
24835
24836 img = IMAGE_FROM_ID (it->f, it->image_id);
24837 eassert (img);
24838 /* Make sure X resources of the image is loaded. */
24839 prepare_image_for_display (it->f, img);
24840
24841 slice.x = slice.y = 0;
24842 slice.width = img->width;
24843 slice.height = img->height;
24844
24845 if (INTEGERP (it->slice.x))
24846 slice.x = XINT (it->slice.x);
24847 else if (FLOATP (it->slice.x))
24848 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24849
24850 if (INTEGERP (it->slice.y))
24851 slice.y = XINT (it->slice.y);
24852 else if (FLOATP (it->slice.y))
24853 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24854
24855 if (INTEGERP (it->slice.width))
24856 slice.width = XINT (it->slice.width);
24857 else if (FLOATP (it->slice.width))
24858 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24859
24860 if (INTEGERP (it->slice.height))
24861 slice.height = XINT (it->slice.height);
24862 else if (FLOATP (it->slice.height))
24863 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24864
24865 if (slice.x >= img->width)
24866 slice.x = img->width;
24867 if (slice.y >= img->height)
24868 slice.y = img->height;
24869 if (slice.x + slice.width >= img->width)
24870 slice.width = img->width - slice.x;
24871 if (slice.y + slice.height > img->height)
24872 slice.height = img->height - slice.y;
24873
24874 if (slice.width == 0 || slice.height == 0)
24875 return;
24876
24877 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24878
24879 it->descent = slice.height - glyph_ascent;
24880 if (slice.y == 0)
24881 it->descent += img->vmargin;
24882 if (slice.y + slice.height == img->height)
24883 it->descent += img->vmargin;
24884 it->phys_descent = it->descent;
24885
24886 it->pixel_width = slice.width;
24887 if (slice.x == 0)
24888 it->pixel_width += img->hmargin;
24889 if (slice.x + slice.width == img->width)
24890 it->pixel_width += img->hmargin;
24891
24892 /* It's quite possible for images to have an ascent greater than
24893 their height, so don't get confused in that case. */
24894 if (it->descent < 0)
24895 it->descent = 0;
24896
24897 it->nglyphs = 1;
24898
24899 if (face->box != FACE_NO_BOX)
24900 {
24901 if (face->box_line_width > 0)
24902 {
24903 if (slice.y == 0)
24904 it->ascent += face->box_line_width;
24905 if (slice.y + slice.height == img->height)
24906 it->descent += face->box_line_width;
24907 }
24908
24909 if (it->start_of_box_run_p && slice.x == 0)
24910 it->pixel_width += eabs (face->box_line_width);
24911 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24912 it->pixel_width += eabs (face->box_line_width);
24913 }
24914
24915 take_vertical_position_into_account (it);
24916
24917 /* Automatically crop wide image glyphs at right edge so we can
24918 draw the cursor on same display row. */
24919 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24920 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24921 {
24922 it->pixel_width -= crop;
24923 slice.width -= crop;
24924 }
24925
24926 if (it->glyph_row)
24927 {
24928 struct glyph *glyph;
24929 enum glyph_row_area area = it->area;
24930
24931 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24932 if (glyph < it->glyph_row->glyphs[area + 1])
24933 {
24934 glyph->charpos = CHARPOS (it->position);
24935 glyph->object = it->object;
24936 glyph->pixel_width = it->pixel_width;
24937 glyph->ascent = glyph_ascent;
24938 glyph->descent = it->descent;
24939 glyph->voffset = it->voffset;
24940 glyph->type = IMAGE_GLYPH;
24941 glyph->avoid_cursor_p = it->avoid_cursor_p;
24942 glyph->multibyte_p = it->multibyte_p;
24943 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24944 {
24945 /* In R2L rows, the left and the right box edges need to be
24946 drawn in reverse direction. */
24947 glyph->right_box_line_p = it->start_of_box_run_p;
24948 glyph->left_box_line_p = it->end_of_box_run_p;
24949 }
24950 else
24951 {
24952 glyph->left_box_line_p = it->start_of_box_run_p;
24953 glyph->right_box_line_p = it->end_of_box_run_p;
24954 }
24955 glyph->overlaps_vertically_p = 0;
24956 glyph->padding_p = 0;
24957 glyph->glyph_not_available_p = 0;
24958 glyph->face_id = it->face_id;
24959 glyph->u.img_id = img->id;
24960 glyph->slice.img = slice;
24961 glyph->font_type = FONT_TYPE_UNKNOWN;
24962 if (it->bidi_p)
24963 {
24964 glyph->resolved_level = it->bidi_it.resolved_level;
24965 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24966 emacs_abort ();
24967 glyph->bidi_type = it->bidi_it.type;
24968 }
24969 ++it->glyph_row->used[area];
24970 }
24971 else
24972 IT_EXPAND_MATRIX_WIDTH (it, area);
24973 }
24974 }
24975
24976
24977 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24978 of the glyph, WIDTH and HEIGHT are the width and height of the
24979 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24980
24981 static void
24982 append_stretch_glyph (struct it *it, Lisp_Object object,
24983 int width, int height, int ascent)
24984 {
24985 struct glyph *glyph;
24986 enum glyph_row_area area = it->area;
24987
24988 eassert (ascent >= 0 && ascent <= height);
24989
24990 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24991 if (glyph < it->glyph_row->glyphs[area + 1])
24992 {
24993 /* If the glyph row is reversed, we need to prepend the glyph
24994 rather than append it. */
24995 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24996 {
24997 struct glyph *g;
24998
24999 /* Make room for the additional glyph. */
25000 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25001 g[1] = *g;
25002 glyph = it->glyph_row->glyphs[area];
25003 }
25004 glyph->charpos = CHARPOS (it->position);
25005 glyph->object = object;
25006 glyph->pixel_width = width;
25007 glyph->ascent = ascent;
25008 glyph->descent = height - ascent;
25009 glyph->voffset = it->voffset;
25010 glyph->type = STRETCH_GLYPH;
25011 glyph->avoid_cursor_p = it->avoid_cursor_p;
25012 glyph->multibyte_p = it->multibyte_p;
25013 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25014 {
25015 /* In R2L rows, the left and the right box edges need to be
25016 drawn in reverse direction. */
25017 glyph->right_box_line_p = it->start_of_box_run_p;
25018 glyph->left_box_line_p = it->end_of_box_run_p;
25019 }
25020 else
25021 {
25022 glyph->left_box_line_p = it->start_of_box_run_p;
25023 glyph->right_box_line_p = it->end_of_box_run_p;
25024 }
25025 glyph->overlaps_vertically_p = 0;
25026 glyph->padding_p = 0;
25027 glyph->glyph_not_available_p = 0;
25028 glyph->face_id = it->face_id;
25029 glyph->u.stretch.ascent = ascent;
25030 glyph->u.stretch.height = height;
25031 glyph->slice.img = null_glyph_slice;
25032 glyph->font_type = FONT_TYPE_UNKNOWN;
25033 if (it->bidi_p)
25034 {
25035 glyph->resolved_level = it->bidi_it.resolved_level;
25036 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25037 emacs_abort ();
25038 glyph->bidi_type = it->bidi_it.type;
25039 }
25040 else
25041 {
25042 glyph->resolved_level = 0;
25043 glyph->bidi_type = UNKNOWN_BT;
25044 }
25045 ++it->glyph_row->used[area];
25046 }
25047 else
25048 IT_EXPAND_MATRIX_WIDTH (it, area);
25049 }
25050
25051 #endif /* HAVE_WINDOW_SYSTEM */
25052
25053 /* Produce a stretch glyph for iterator IT. IT->object is the value
25054 of the glyph property displayed. The value must be a list
25055 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25056 being recognized:
25057
25058 1. `:width WIDTH' specifies that the space should be WIDTH *
25059 canonical char width wide. WIDTH may be an integer or floating
25060 point number.
25061
25062 2. `:relative-width FACTOR' specifies that the width of the stretch
25063 should be computed from the width of the first character having the
25064 `glyph' property, and should be FACTOR times that width.
25065
25066 3. `:align-to HPOS' specifies that the space should be wide enough
25067 to reach HPOS, a value in canonical character units.
25068
25069 Exactly one of the above pairs must be present.
25070
25071 4. `:height HEIGHT' specifies that the height of the stretch produced
25072 should be HEIGHT, measured in canonical character units.
25073
25074 5. `:relative-height FACTOR' specifies that the height of the
25075 stretch should be FACTOR times the height of the characters having
25076 the glyph property.
25077
25078 Either none or exactly one of 4 or 5 must be present.
25079
25080 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25081 of the stretch should be used for the ascent of the stretch.
25082 ASCENT must be in the range 0 <= ASCENT <= 100. */
25083
25084 void
25085 produce_stretch_glyph (struct it *it)
25086 {
25087 /* (space :width WIDTH :height HEIGHT ...) */
25088 Lisp_Object prop, plist;
25089 int width = 0, height = 0, align_to = -1;
25090 int zero_width_ok_p = 0;
25091 double tem;
25092 struct font *font = NULL;
25093
25094 #ifdef HAVE_WINDOW_SYSTEM
25095 int ascent = 0;
25096 int zero_height_ok_p = 0;
25097
25098 if (FRAME_WINDOW_P (it->f))
25099 {
25100 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25101 font = face->font ? face->font : FRAME_FONT (it->f);
25102 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25103 }
25104 #endif
25105
25106 /* List should start with `space'. */
25107 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25108 plist = XCDR (it->object);
25109
25110 /* Compute the width of the stretch. */
25111 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25112 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25113 {
25114 /* Absolute width `:width WIDTH' specified and valid. */
25115 zero_width_ok_p = 1;
25116 width = (int)tem;
25117 }
25118 #ifdef HAVE_WINDOW_SYSTEM
25119 else if (FRAME_WINDOW_P (it->f)
25120 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25121 {
25122 /* Relative width `:relative-width FACTOR' specified and valid.
25123 Compute the width of the characters having the `glyph'
25124 property. */
25125 struct it it2;
25126 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25127
25128 it2 = *it;
25129 if (it->multibyte_p)
25130 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25131 else
25132 {
25133 it2.c = it2.char_to_display = *p, it2.len = 1;
25134 if (! ASCII_CHAR_P (it2.c))
25135 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25136 }
25137
25138 it2.glyph_row = NULL;
25139 it2.what = IT_CHARACTER;
25140 x_produce_glyphs (&it2);
25141 width = NUMVAL (prop) * it2.pixel_width;
25142 }
25143 #endif /* HAVE_WINDOW_SYSTEM */
25144 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25145 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25146 {
25147 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25148 align_to = (align_to < 0
25149 ? 0
25150 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25151 else if (align_to < 0)
25152 align_to = window_box_left_offset (it->w, TEXT_AREA);
25153 width = max (0, (int)tem + align_to - it->current_x);
25154 zero_width_ok_p = 1;
25155 }
25156 else
25157 /* Nothing specified -> width defaults to canonical char width. */
25158 width = FRAME_COLUMN_WIDTH (it->f);
25159
25160 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25161 width = 1;
25162
25163 #ifdef HAVE_WINDOW_SYSTEM
25164 /* Compute height. */
25165 if (FRAME_WINDOW_P (it->f))
25166 {
25167 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25168 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25169 {
25170 height = (int)tem;
25171 zero_height_ok_p = 1;
25172 }
25173 else if (prop = Fplist_get (plist, QCrelative_height),
25174 NUMVAL (prop) > 0)
25175 height = FONT_HEIGHT (font) * NUMVAL (prop);
25176 else
25177 height = FONT_HEIGHT (font);
25178
25179 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25180 height = 1;
25181
25182 /* Compute percentage of height used for ascent. If
25183 `:ascent ASCENT' is present and valid, use that. Otherwise,
25184 derive the ascent from the font in use. */
25185 if (prop = Fplist_get (plist, QCascent),
25186 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25187 ascent = height * NUMVAL (prop) / 100.0;
25188 else if (!NILP (prop)
25189 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25190 ascent = min (max (0, (int)tem), height);
25191 else
25192 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25193 }
25194 else
25195 #endif /* HAVE_WINDOW_SYSTEM */
25196 height = 1;
25197
25198 if (width > 0 && it->line_wrap != TRUNCATE
25199 && it->current_x + width > it->last_visible_x)
25200 {
25201 width = it->last_visible_x - it->current_x;
25202 #ifdef HAVE_WINDOW_SYSTEM
25203 /* Subtract one more pixel from the stretch width, but only on
25204 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25205 width -= FRAME_WINDOW_P (it->f);
25206 #endif
25207 }
25208
25209 if (width > 0 && height > 0 && it->glyph_row)
25210 {
25211 Lisp_Object o_object = it->object;
25212 Lisp_Object object = it->stack[it->sp - 1].string;
25213 int n = width;
25214
25215 if (!STRINGP (object))
25216 object = it->w->contents;
25217 #ifdef HAVE_WINDOW_SYSTEM
25218 if (FRAME_WINDOW_P (it->f))
25219 append_stretch_glyph (it, object, width, height, ascent);
25220 else
25221 #endif
25222 {
25223 it->object = object;
25224 it->char_to_display = ' ';
25225 it->pixel_width = it->len = 1;
25226 while (n--)
25227 tty_append_glyph (it);
25228 it->object = o_object;
25229 }
25230 }
25231
25232 it->pixel_width = width;
25233 #ifdef HAVE_WINDOW_SYSTEM
25234 if (FRAME_WINDOW_P (it->f))
25235 {
25236 it->ascent = it->phys_ascent = ascent;
25237 it->descent = it->phys_descent = height - it->ascent;
25238 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25239 take_vertical_position_into_account (it);
25240 }
25241 else
25242 #endif
25243 it->nglyphs = width;
25244 }
25245
25246 /* Get information about special display element WHAT in an
25247 environment described by IT. WHAT is one of IT_TRUNCATION or
25248 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25249 non-null glyph_row member. This function ensures that fields like
25250 face_id, c, len of IT are left untouched. */
25251
25252 static void
25253 produce_special_glyphs (struct it *it, enum display_element_type what)
25254 {
25255 struct it temp_it;
25256 Lisp_Object gc;
25257 GLYPH glyph;
25258
25259 temp_it = *it;
25260 temp_it.object = make_number (0);
25261 memset (&temp_it.current, 0, sizeof temp_it.current);
25262
25263 if (what == IT_CONTINUATION)
25264 {
25265 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25266 if (it->bidi_it.paragraph_dir == R2L)
25267 SET_GLYPH_FROM_CHAR (glyph, '/');
25268 else
25269 SET_GLYPH_FROM_CHAR (glyph, '\\');
25270 if (it->dp
25271 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25272 {
25273 /* FIXME: Should we mirror GC for R2L lines? */
25274 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25275 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25276 }
25277 }
25278 else if (what == IT_TRUNCATION)
25279 {
25280 /* Truncation glyph. */
25281 SET_GLYPH_FROM_CHAR (glyph, '$');
25282 if (it->dp
25283 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25284 {
25285 /* FIXME: Should we mirror GC for R2L lines? */
25286 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25287 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25288 }
25289 }
25290 else
25291 emacs_abort ();
25292
25293 #ifdef HAVE_WINDOW_SYSTEM
25294 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25295 is turned off, we precede the truncation/continuation glyphs by a
25296 stretch glyph whose width is computed such that these special
25297 glyphs are aligned at the window margin, even when very different
25298 fonts are used in different glyph rows. */
25299 if (FRAME_WINDOW_P (temp_it.f)
25300 /* init_iterator calls this with it->glyph_row == NULL, and it
25301 wants only the pixel width of the truncation/continuation
25302 glyphs. */
25303 && temp_it.glyph_row
25304 /* insert_left_trunc_glyphs calls us at the beginning of the
25305 row, and it has its own calculation of the stretch glyph
25306 width. */
25307 && temp_it.glyph_row->used[TEXT_AREA] > 0
25308 && (temp_it.glyph_row->reversed_p
25309 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25310 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25311 {
25312 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25313
25314 if (stretch_width > 0)
25315 {
25316 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25317 struct font *font =
25318 face->font ? face->font : FRAME_FONT (temp_it.f);
25319 int stretch_ascent =
25320 (((temp_it.ascent + temp_it.descent)
25321 * FONT_BASE (font)) / FONT_HEIGHT (font));
25322
25323 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25324 temp_it.ascent + temp_it.descent,
25325 stretch_ascent);
25326 }
25327 }
25328 #endif
25329
25330 temp_it.dp = NULL;
25331 temp_it.what = IT_CHARACTER;
25332 temp_it.len = 1;
25333 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25334 temp_it.face_id = GLYPH_FACE (glyph);
25335 temp_it.len = CHAR_BYTES (temp_it.c);
25336
25337 PRODUCE_GLYPHS (&temp_it);
25338 it->pixel_width = temp_it.pixel_width;
25339 it->nglyphs = temp_it.pixel_width;
25340 }
25341
25342 #ifdef HAVE_WINDOW_SYSTEM
25343
25344 /* Calculate line-height and line-spacing properties.
25345 An integer value specifies explicit pixel value.
25346 A float value specifies relative value to current face height.
25347 A cons (float . face-name) specifies relative value to
25348 height of specified face font.
25349
25350 Returns height in pixels, or nil. */
25351
25352
25353 static Lisp_Object
25354 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25355 int boff, int override)
25356 {
25357 Lisp_Object face_name = Qnil;
25358 int ascent, descent, height;
25359
25360 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25361 return val;
25362
25363 if (CONSP (val))
25364 {
25365 face_name = XCAR (val);
25366 val = XCDR (val);
25367 if (!NUMBERP (val))
25368 val = make_number (1);
25369 if (NILP (face_name))
25370 {
25371 height = it->ascent + it->descent;
25372 goto scale;
25373 }
25374 }
25375
25376 if (NILP (face_name))
25377 {
25378 font = FRAME_FONT (it->f);
25379 boff = FRAME_BASELINE_OFFSET (it->f);
25380 }
25381 else if (EQ (face_name, Qt))
25382 {
25383 override = 0;
25384 }
25385 else
25386 {
25387 int face_id;
25388 struct face *face;
25389
25390 face_id = lookup_named_face (it->f, face_name, 0);
25391 if (face_id < 0)
25392 return make_number (-1);
25393
25394 face = FACE_FROM_ID (it->f, face_id);
25395 font = face->font;
25396 if (font == NULL)
25397 return make_number (-1);
25398 boff = font->baseline_offset;
25399 if (font->vertical_centering)
25400 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25401 }
25402
25403 ascent = FONT_BASE (font) + boff;
25404 descent = FONT_DESCENT (font) - boff;
25405
25406 if (override)
25407 {
25408 it->override_ascent = ascent;
25409 it->override_descent = descent;
25410 it->override_boff = boff;
25411 }
25412
25413 height = ascent + descent;
25414
25415 scale:
25416 if (FLOATP (val))
25417 height = (int)(XFLOAT_DATA (val) * height);
25418 else if (INTEGERP (val))
25419 height *= XINT (val);
25420
25421 return make_number (height);
25422 }
25423
25424
25425 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25426 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25427 and only if this is for a character for which no font was found.
25428
25429 If the display method (it->glyphless_method) is
25430 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25431 length of the acronym or the hexadecimal string, UPPER_XOFF and
25432 UPPER_YOFF are pixel offsets for the upper part of the string,
25433 LOWER_XOFF and LOWER_YOFF are for the lower part.
25434
25435 For the other display methods, LEN through LOWER_YOFF are zero. */
25436
25437 static void
25438 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25439 short upper_xoff, short upper_yoff,
25440 short lower_xoff, short lower_yoff)
25441 {
25442 struct glyph *glyph;
25443 enum glyph_row_area area = it->area;
25444
25445 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25446 if (glyph < it->glyph_row->glyphs[area + 1])
25447 {
25448 /* If the glyph row is reversed, we need to prepend the glyph
25449 rather than append it. */
25450 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25451 {
25452 struct glyph *g;
25453
25454 /* Make room for the additional glyph. */
25455 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25456 g[1] = *g;
25457 glyph = it->glyph_row->glyphs[area];
25458 }
25459 glyph->charpos = CHARPOS (it->position);
25460 glyph->object = it->object;
25461 glyph->pixel_width = it->pixel_width;
25462 glyph->ascent = it->ascent;
25463 glyph->descent = it->descent;
25464 glyph->voffset = it->voffset;
25465 glyph->type = GLYPHLESS_GLYPH;
25466 glyph->u.glyphless.method = it->glyphless_method;
25467 glyph->u.glyphless.for_no_font = for_no_font;
25468 glyph->u.glyphless.len = len;
25469 glyph->u.glyphless.ch = it->c;
25470 glyph->slice.glyphless.upper_xoff = upper_xoff;
25471 glyph->slice.glyphless.upper_yoff = upper_yoff;
25472 glyph->slice.glyphless.lower_xoff = lower_xoff;
25473 glyph->slice.glyphless.lower_yoff = lower_yoff;
25474 glyph->avoid_cursor_p = it->avoid_cursor_p;
25475 glyph->multibyte_p = it->multibyte_p;
25476 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25477 {
25478 /* In R2L rows, the left and the right box edges need to be
25479 drawn in reverse direction. */
25480 glyph->right_box_line_p = it->start_of_box_run_p;
25481 glyph->left_box_line_p = it->end_of_box_run_p;
25482 }
25483 else
25484 {
25485 glyph->left_box_line_p = it->start_of_box_run_p;
25486 glyph->right_box_line_p = it->end_of_box_run_p;
25487 }
25488 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25489 || it->phys_descent > it->descent);
25490 glyph->padding_p = 0;
25491 glyph->glyph_not_available_p = 0;
25492 glyph->face_id = face_id;
25493 glyph->font_type = FONT_TYPE_UNKNOWN;
25494 if (it->bidi_p)
25495 {
25496 glyph->resolved_level = it->bidi_it.resolved_level;
25497 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25498 emacs_abort ();
25499 glyph->bidi_type = it->bidi_it.type;
25500 }
25501 ++it->glyph_row->used[area];
25502 }
25503 else
25504 IT_EXPAND_MATRIX_WIDTH (it, area);
25505 }
25506
25507
25508 /* Produce a glyph for a glyphless character for iterator IT.
25509 IT->glyphless_method specifies which method to use for displaying
25510 the character. See the description of enum
25511 glyphless_display_method in dispextern.h for the detail.
25512
25513 FOR_NO_FONT is nonzero if and only if this is for a character for
25514 which no font was found. ACRONYM, if non-nil, is an acronym string
25515 for the character. */
25516
25517 static void
25518 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25519 {
25520 int face_id;
25521 struct face *face;
25522 struct font *font;
25523 int base_width, base_height, width, height;
25524 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25525 int len;
25526
25527 /* Get the metrics of the base font. We always refer to the current
25528 ASCII face. */
25529 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25530 font = face->font ? face->font : FRAME_FONT (it->f);
25531 it->ascent = FONT_BASE (font) + font->baseline_offset;
25532 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25533 base_height = it->ascent + it->descent;
25534 base_width = font->average_width;
25535
25536 face_id = merge_glyphless_glyph_face (it);
25537
25538 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25539 {
25540 it->pixel_width = THIN_SPACE_WIDTH;
25541 len = 0;
25542 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25543 }
25544 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25545 {
25546 width = CHAR_WIDTH (it->c);
25547 if (width == 0)
25548 width = 1;
25549 else if (width > 4)
25550 width = 4;
25551 it->pixel_width = base_width * width;
25552 len = 0;
25553 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25554 }
25555 else
25556 {
25557 char buf[7];
25558 const char *str;
25559 unsigned int code[6];
25560 int upper_len;
25561 int ascent, descent;
25562 struct font_metrics metrics_upper, metrics_lower;
25563
25564 face = FACE_FROM_ID (it->f, face_id);
25565 font = face->font ? face->font : FRAME_FONT (it->f);
25566 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25567
25568 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25569 {
25570 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25571 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25572 if (CONSP (acronym))
25573 acronym = XCAR (acronym);
25574 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25575 }
25576 else
25577 {
25578 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25579 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25580 str = buf;
25581 }
25582 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25583 code[len] = font->driver->encode_char (font, str[len]);
25584 upper_len = (len + 1) / 2;
25585 font->driver->text_extents (font, code, upper_len,
25586 &metrics_upper);
25587 font->driver->text_extents (font, code + upper_len, len - upper_len,
25588 &metrics_lower);
25589
25590
25591
25592 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25593 width = max (metrics_upper.width, metrics_lower.width) + 4;
25594 upper_xoff = upper_yoff = 2; /* the typical case */
25595 if (base_width >= width)
25596 {
25597 /* Align the upper to the left, the lower to the right. */
25598 it->pixel_width = base_width;
25599 lower_xoff = base_width - 2 - metrics_lower.width;
25600 }
25601 else
25602 {
25603 /* Center the shorter one. */
25604 it->pixel_width = width;
25605 if (metrics_upper.width >= metrics_lower.width)
25606 lower_xoff = (width - metrics_lower.width) / 2;
25607 else
25608 {
25609 /* FIXME: This code doesn't look right. It formerly was
25610 missing the "lower_xoff = 0;", which couldn't have
25611 been right since it left lower_xoff uninitialized. */
25612 lower_xoff = 0;
25613 upper_xoff = (width - metrics_upper.width) / 2;
25614 }
25615 }
25616
25617 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25618 top, bottom, and between upper and lower strings. */
25619 height = (metrics_upper.ascent + metrics_upper.descent
25620 + metrics_lower.ascent + metrics_lower.descent) + 5;
25621 /* Center vertically.
25622 H:base_height, D:base_descent
25623 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25624
25625 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25626 descent = D - H/2 + h/2;
25627 lower_yoff = descent - 2 - ld;
25628 upper_yoff = lower_yoff - la - 1 - ud; */
25629 ascent = - (it->descent - (base_height + height + 1) / 2);
25630 descent = it->descent - (base_height - height) / 2;
25631 lower_yoff = descent - 2 - metrics_lower.descent;
25632 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25633 - metrics_upper.descent);
25634 /* Don't make the height shorter than the base height. */
25635 if (height > base_height)
25636 {
25637 it->ascent = ascent;
25638 it->descent = descent;
25639 }
25640 }
25641
25642 it->phys_ascent = it->ascent;
25643 it->phys_descent = it->descent;
25644 if (it->glyph_row)
25645 append_glyphless_glyph (it, face_id, for_no_font, len,
25646 upper_xoff, upper_yoff,
25647 lower_xoff, lower_yoff);
25648 it->nglyphs = 1;
25649 take_vertical_position_into_account (it);
25650 }
25651
25652
25653 /* RIF:
25654 Produce glyphs/get display metrics for the display element IT is
25655 loaded with. See the description of struct it in dispextern.h
25656 for an overview of struct it. */
25657
25658 void
25659 x_produce_glyphs (struct it *it)
25660 {
25661 int extra_line_spacing = it->extra_line_spacing;
25662
25663 it->glyph_not_available_p = 0;
25664
25665 if (it->what == IT_CHARACTER)
25666 {
25667 XChar2b char2b;
25668 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25669 struct font *font = face->font;
25670 struct font_metrics *pcm = NULL;
25671 int boff; /* Baseline offset. */
25672
25673 if (font == NULL)
25674 {
25675 /* When no suitable font is found, display this character by
25676 the method specified in the first extra slot of
25677 Vglyphless_char_display. */
25678 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25679
25680 eassert (it->what == IT_GLYPHLESS);
25681 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25682 goto done;
25683 }
25684
25685 boff = font->baseline_offset;
25686 if (font->vertical_centering)
25687 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25688
25689 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25690 {
25691 int stretched_p;
25692
25693 it->nglyphs = 1;
25694
25695 if (it->override_ascent >= 0)
25696 {
25697 it->ascent = it->override_ascent;
25698 it->descent = it->override_descent;
25699 boff = it->override_boff;
25700 }
25701 else
25702 {
25703 it->ascent = FONT_BASE (font) + boff;
25704 it->descent = FONT_DESCENT (font) - boff;
25705 }
25706
25707 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25708 {
25709 pcm = get_per_char_metric (font, &char2b);
25710 if (pcm->width == 0
25711 && pcm->rbearing == 0 && pcm->lbearing == 0)
25712 pcm = NULL;
25713 }
25714
25715 if (pcm)
25716 {
25717 it->phys_ascent = pcm->ascent + boff;
25718 it->phys_descent = pcm->descent - boff;
25719 it->pixel_width = pcm->width;
25720 }
25721 else
25722 {
25723 it->glyph_not_available_p = 1;
25724 it->phys_ascent = it->ascent;
25725 it->phys_descent = it->descent;
25726 it->pixel_width = font->space_width;
25727 }
25728
25729 if (it->constrain_row_ascent_descent_p)
25730 {
25731 if (it->descent > it->max_descent)
25732 {
25733 it->ascent += it->descent - it->max_descent;
25734 it->descent = it->max_descent;
25735 }
25736 if (it->ascent > it->max_ascent)
25737 {
25738 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25739 it->ascent = it->max_ascent;
25740 }
25741 it->phys_ascent = min (it->phys_ascent, it->ascent);
25742 it->phys_descent = min (it->phys_descent, it->descent);
25743 extra_line_spacing = 0;
25744 }
25745
25746 /* If this is a space inside a region of text with
25747 `space-width' property, change its width. */
25748 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25749 if (stretched_p)
25750 it->pixel_width *= XFLOATINT (it->space_width);
25751
25752 /* If face has a box, add the box thickness to the character
25753 height. If character has a box line to the left and/or
25754 right, add the box line width to the character's width. */
25755 if (face->box != FACE_NO_BOX)
25756 {
25757 int thick = face->box_line_width;
25758
25759 if (thick > 0)
25760 {
25761 it->ascent += thick;
25762 it->descent += thick;
25763 }
25764 else
25765 thick = -thick;
25766
25767 if (it->start_of_box_run_p)
25768 it->pixel_width += thick;
25769 if (it->end_of_box_run_p)
25770 it->pixel_width += thick;
25771 }
25772
25773 /* If face has an overline, add the height of the overline
25774 (1 pixel) and a 1 pixel margin to the character height. */
25775 if (face->overline_p)
25776 it->ascent += overline_margin;
25777
25778 if (it->constrain_row_ascent_descent_p)
25779 {
25780 if (it->ascent > it->max_ascent)
25781 it->ascent = it->max_ascent;
25782 if (it->descent > it->max_descent)
25783 it->descent = it->max_descent;
25784 }
25785
25786 take_vertical_position_into_account (it);
25787
25788 /* If we have to actually produce glyphs, do it. */
25789 if (it->glyph_row)
25790 {
25791 if (stretched_p)
25792 {
25793 /* Translate a space with a `space-width' property
25794 into a stretch glyph. */
25795 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25796 / FONT_HEIGHT (font));
25797 append_stretch_glyph (it, it->object, it->pixel_width,
25798 it->ascent + it->descent, ascent);
25799 }
25800 else
25801 append_glyph (it);
25802
25803 /* If characters with lbearing or rbearing are displayed
25804 in this line, record that fact in a flag of the
25805 glyph row. This is used to optimize X output code. */
25806 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25807 it->glyph_row->contains_overlapping_glyphs_p = 1;
25808 }
25809 if (! stretched_p && it->pixel_width == 0)
25810 /* We assure that all visible glyphs have at least 1-pixel
25811 width. */
25812 it->pixel_width = 1;
25813 }
25814 else if (it->char_to_display == '\n')
25815 {
25816 /* A newline has no width, but we need the height of the
25817 line. But if previous part of the line sets a height,
25818 don't increase that height. */
25819
25820 Lisp_Object height;
25821 Lisp_Object total_height = Qnil;
25822
25823 it->override_ascent = -1;
25824 it->pixel_width = 0;
25825 it->nglyphs = 0;
25826
25827 height = get_it_property (it, Qline_height);
25828 /* Split (line-height total-height) list. */
25829 if (CONSP (height)
25830 && CONSP (XCDR (height))
25831 && NILP (XCDR (XCDR (height))))
25832 {
25833 total_height = XCAR (XCDR (height));
25834 height = XCAR (height);
25835 }
25836 height = calc_line_height_property (it, height, font, boff, 1);
25837
25838 if (it->override_ascent >= 0)
25839 {
25840 it->ascent = it->override_ascent;
25841 it->descent = it->override_descent;
25842 boff = it->override_boff;
25843 }
25844 else
25845 {
25846 it->ascent = FONT_BASE (font) + boff;
25847 it->descent = FONT_DESCENT (font) - boff;
25848 }
25849
25850 if (EQ (height, Qt))
25851 {
25852 if (it->descent > it->max_descent)
25853 {
25854 it->ascent += it->descent - it->max_descent;
25855 it->descent = it->max_descent;
25856 }
25857 if (it->ascent > it->max_ascent)
25858 {
25859 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25860 it->ascent = it->max_ascent;
25861 }
25862 it->phys_ascent = min (it->phys_ascent, it->ascent);
25863 it->phys_descent = min (it->phys_descent, it->descent);
25864 it->constrain_row_ascent_descent_p = 1;
25865 extra_line_spacing = 0;
25866 }
25867 else
25868 {
25869 Lisp_Object spacing;
25870
25871 it->phys_ascent = it->ascent;
25872 it->phys_descent = it->descent;
25873
25874 if ((it->max_ascent > 0 || it->max_descent > 0)
25875 && face->box != FACE_NO_BOX
25876 && face->box_line_width > 0)
25877 {
25878 it->ascent += face->box_line_width;
25879 it->descent += face->box_line_width;
25880 }
25881 if (!NILP (height)
25882 && XINT (height) > it->ascent + it->descent)
25883 it->ascent = XINT (height) - it->descent;
25884
25885 if (!NILP (total_height))
25886 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25887 else
25888 {
25889 spacing = get_it_property (it, Qline_spacing);
25890 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25891 }
25892 if (INTEGERP (spacing))
25893 {
25894 extra_line_spacing = XINT (spacing);
25895 if (!NILP (total_height))
25896 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25897 }
25898 }
25899 }
25900 else /* i.e. (it->char_to_display == '\t') */
25901 {
25902 if (font->space_width > 0)
25903 {
25904 int tab_width = it->tab_width * font->space_width;
25905 int x = it->current_x + it->continuation_lines_width;
25906 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25907
25908 /* If the distance from the current position to the next tab
25909 stop is less than a space character width, use the
25910 tab stop after that. */
25911 if (next_tab_x - x < font->space_width)
25912 next_tab_x += tab_width;
25913
25914 it->pixel_width = next_tab_x - x;
25915 it->nglyphs = 1;
25916 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25917 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25918
25919 if (it->glyph_row)
25920 {
25921 append_stretch_glyph (it, it->object, it->pixel_width,
25922 it->ascent + it->descent, it->ascent);
25923 }
25924 }
25925 else
25926 {
25927 it->pixel_width = 0;
25928 it->nglyphs = 1;
25929 }
25930 }
25931 }
25932 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25933 {
25934 /* A static composition.
25935
25936 Note: A composition is represented as one glyph in the
25937 glyph matrix. There are no padding glyphs.
25938
25939 Important note: pixel_width, ascent, and descent are the
25940 values of what is drawn by draw_glyphs (i.e. the values of
25941 the overall glyphs composed). */
25942 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25943 int boff; /* baseline offset */
25944 struct composition *cmp = composition_table[it->cmp_it.id];
25945 int glyph_len = cmp->glyph_len;
25946 struct font *font = face->font;
25947
25948 it->nglyphs = 1;
25949
25950 /* If we have not yet calculated pixel size data of glyphs of
25951 the composition for the current face font, calculate them
25952 now. Theoretically, we have to check all fonts for the
25953 glyphs, but that requires much time and memory space. So,
25954 here we check only the font of the first glyph. This may
25955 lead to incorrect display, but it's very rare, and C-l
25956 (recenter-top-bottom) can correct the display anyway. */
25957 if (! cmp->font || cmp->font != font)
25958 {
25959 /* Ascent and descent of the font of the first character
25960 of this composition (adjusted by baseline offset).
25961 Ascent and descent of overall glyphs should not be less
25962 than these, respectively. */
25963 int font_ascent, font_descent, font_height;
25964 /* Bounding box of the overall glyphs. */
25965 int leftmost, rightmost, lowest, highest;
25966 int lbearing, rbearing;
25967 int i, width, ascent, descent;
25968 int left_padded = 0, right_padded = 0;
25969 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25970 XChar2b char2b;
25971 struct font_metrics *pcm;
25972 int font_not_found_p;
25973 ptrdiff_t pos;
25974
25975 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25976 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25977 break;
25978 if (glyph_len < cmp->glyph_len)
25979 right_padded = 1;
25980 for (i = 0; i < glyph_len; i++)
25981 {
25982 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25983 break;
25984 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25985 }
25986 if (i > 0)
25987 left_padded = 1;
25988
25989 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25990 : IT_CHARPOS (*it));
25991 /* If no suitable font is found, use the default font. */
25992 font_not_found_p = font == NULL;
25993 if (font_not_found_p)
25994 {
25995 face = face->ascii_face;
25996 font = face->font;
25997 }
25998 boff = font->baseline_offset;
25999 if (font->vertical_centering)
26000 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26001 font_ascent = FONT_BASE (font) + boff;
26002 font_descent = FONT_DESCENT (font) - boff;
26003 font_height = FONT_HEIGHT (font);
26004
26005 cmp->font = font;
26006
26007 pcm = NULL;
26008 if (! font_not_found_p)
26009 {
26010 get_char_face_and_encoding (it->f, c, it->face_id,
26011 &char2b, 0);
26012 pcm = get_per_char_metric (font, &char2b);
26013 }
26014
26015 /* Initialize the bounding box. */
26016 if (pcm)
26017 {
26018 width = cmp->glyph_len > 0 ? pcm->width : 0;
26019 ascent = pcm->ascent;
26020 descent = pcm->descent;
26021 lbearing = pcm->lbearing;
26022 rbearing = pcm->rbearing;
26023 }
26024 else
26025 {
26026 width = cmp->glyph_len > 0 ? font->space_width : 0;
26027 ascent = FONT_BASE (font);
26028 descent = FONT_DESCENT (font);
26029 lbearing = 0;
26030 rbearing = width;
26031 }
26032
26033 rightmost = width;
26034 leftmost = 0;
26035 lowest = - descent + boff;
26036 highest = ascent + boff;
26037
26038 if (! font_not_found_p
26039 && font->default_ascent
26040 && CHAR_TABLE_P (Vuse_default_ascent)
26041 && !NILP (Faref (Vuse_default_ascent,
26042 make_number (it->char_to_display))))
26043 highest = font->default_ascent + boff;
26044
26045 /* Draw the first glyph at the normal position. It may be
26046 shifted to right later if some other glyphs are drawn
26047 at the left. */
26048 cmp->offsets[i * 2] = 0;
26049 cmp->offsets[i * 2 + 1] = boff;
26050 cmp->lbearing = lbearing;
26051 cmp->rbearing = rbearing;
26052
26053 /* Set cmp->offsets for the remaining glyphs. */
26054 for (i++; i < glyph_len; i++)
26055 {
26056 int left, right, btm, top;
26057 int ch = COMPOSITION_GLYPH (cmp, i);
26058 int face_id;
26059 struct face *this_face;
26060
26061 if (ch == '\t')
26062 ch = ' ';
26063 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26064 this_face = FACE_FROM_ID (it->f, face_id);
26065 font = this_face->font;
26066
26067 if (font == NULL)
26068 pcm = NULL;
26069 else
26070 {
26071 get_char_face_and_encoding (it->f, ch, face_id,
26072 &char2b, 0);
26073 pcm = get_per_char_metric (font, &char2b);
26074 }
26075 if (! pcm)
26076 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26077 else
26078 {
26079 width = pcm->width;
26080 ascent = pcm->ascent;
26081 descent = pcm->descent;
26082 lbearing = pcm->lbearing;
26083 rbearing = pcm->rbearing;
26084 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26085 {
26086 /* Relative composition with or without
26087 alternate chars. */
26088 left = (leftmost + rightmost - width) / 2;
26089 btm = - descent + boff;
26090 if (font->relative_compose
26091 && (! CHAR_TABLE_P (Vignore_relative_composition)
26092 || NILP (Faref (Vignore_relative_composition,
26093 make_number (ch)))))
26094 {
26095
26096 if (- descent >= font->relative_compose)
26097 /* One extra pixel between two glyphs. */
26098 btm = highest + 1;
26099 else if (ascent <= 0)
26100 /* One extra pixel between two glyphs. */
26101 btm = lowest - 1 - ascent - descent;
26102 }
26103 }
26104 else
26105 {
26106 /* A composition rule is specified by an integer
26107 value that encodes global and new reference
26108 points (GREF and NREF). GREF and NREF are
26109 specified by numbers as below:
26110
26111 0---1---2 -- ascent
26112 | |
26113 | |
26114 | |
26115 9--10--11 -- center
26116 | |
26117 ---3---4---5--- baseline
26118 | |
26119 6---7---8 -- descent
26120 */
26121 int rule = COMPOSITION_RULE (cmp, i);
26122 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26123
26124 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26125 grefx = gref % 3, nrefx = nref % 3;
26126 grefy = gref / 3, nrefy = nref / 3;
26127 if (xoff)
26128 xoff = font_height * (xoff - 128) / 256;
26129 if (yoff)
26130 yoff = font_height * (yoff - 128) / 256;
26131
26132 left = (leftmost
26133 + grefx * (rightmost - leftmost) / 2
26134 - nrefx * width / 2
26135 + xoff);
26136
26137 btm = ((grefy == 0 ? highest
26138 : grefy == 1 ? 0
26139 : grefy == 2 ? lowest
26140 : (highest + lowest) / 2)
26141 - (nrefy == 0 ? ascent + descent
26142 : nrefy == 1 ? descent - boff
26143 : nrefy == 2 ? 0
26144 : (ascent + descent) / 2)
26145 + yoff);
26146 }
26147
26148 cmp->offsets[i * 2] = left;
26149 cmp->offsets[i * 2 + 1] = btm + descent;
26150
26151 /* Update the bounding box of the overall glyphs. */
26152 if (width > 0)
26153 {
26154 right = left + width;
26155 if (left < leftmost)
26156 leftmost = left;
26157 if (right > rightmost)
26158 rightmost = right;
26159 }
26160 top = btm + descent + ascent;
26161 if (top > highest)
26162 highest = top;
26163 if (btm < lowest)
26164 lowest = btm;
26165
26166 if (cmp->lbearing > left + lbearing)
26167 cmp->lbearing = left + lbearing;
26168 if (cmp->rbearing < left + rbearing)
26169 cmp->rbearing = left + rbearing;
26170 }
26171 }
26172
26173 /* If there are glyphs whose x-offsets are negative,
26174 shift all glyphs to the right and make all x-offsets
26175 non-negative. */
26176 if (leftmost < 0)
26177 {
26178 for (i = 0; i < cmp->glyph_len; i++)
26179 cmp->offsets[i * 2] -= leftmost;
26180 rightmost -= leftmost;
26181 cmp->lbearing -= leftmost;
26182 cmp->rbearing -= leftmost;
26183 }
26184
26185 if (left_padded && cmp->lbearing < 0)
26186 {
26187 for (i = 0; i < cmp->glyph_len; i++)
26188 cmp->offsets[i * 2] -= cmp->lbearing;
26189 rightmost -= cmp->lbearing;
26190 cmp->rbearing -= cmp->lbearing;
26191 cmp->lbearing = 0;
26192 }
26193 if (right_padded && rightmost < cmp->rbearing)
26194 {
26195 rightmost = cmp->rbearing;
26196 }
26197
26198 cmp->pixel_width = rightmost;
26199 cmp->ascent = highest;
26200 cmp->descent = - lowest;
26201 if (cmp->ascent < font_ascent)
26202 cmp->ascent = font_ascent;
26203 if (cmp->descent < font_descent)
26204 cmp->descent = font_descent;
26205 }
26206
26207 if (it->glyph_row
26208 && (cmp->lbearing < 0
26209 || cmp->rbearing > cmp->pixel_width))
26210 it->glyph_row->contains_overlapping_glyphs_p = 1;
26211
26212 it->pixel_width = cmp->pixel_width;
26213 it->ascent = it->phys_ascent = cmp->ascent;
26214 it->descent = it->phys_descent = cmp->descent;
26215 if (face->box != FACE_NO_BOX)
26216 {
26217 int thick = face->box_line_width;
26218
26219 if (thick > 0)
26220 {
26221 it->ascent += thick;
26222 it->descent += thick;
26223 }
26224 else
26225 thick = - thick;
26226
26227 if (it->start_of_box_run_p)
26228 it->pixel_width += thick;
26229 if (it->end_of_box_run_p)
26230 it->pixel_width += thick;
26231 }
26232
26233 /* If face has an overline, add the height of the overline
26234 (1 pixel) and a 1 pixel margin to the character height. */
26235 if (face->overline_p)
26236 it->ascent += overline_margin;
26237
26238 take_vertical_position_into_account (it);
26239 if (it->ascent < 0)
26240 it->ascent = 0;
26241 if (it->descent < 0)
26242 it->descent = 0;
26243
26244 if (it->glyph_row && cmp->glyph_len > 0)
26245 append_composite_glyph (it);
26246 }
26247 else if (it->what == IT_COMPOSITION)
26248 {
26249 /* A dynamic (automatic) composition. */
26250 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26251 Lisp_Object gstring;
26252 struct font_metrics metrics;
26253
26254 it->nglyphs = 1;
26255
26256 gstring = composition_gstring_from_id (it->cmp_it.id);
26257 it->pixel_width
26258 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26259 &metrics);
26260 if (it->glyph_row
26261 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26262 it->glyph_row->contains_overlapping_glyphs_p = 1;
26263 it->ascent = it->phys_ascent = metrics.ascent;
26264 it->descent = it->phys_descent = metrics.descent;
26265 if (face->box != FACE_NO_BOX)
26266 {
26267 int thick = face->box_line_width;
26268
26269 if (thick > 0)
26270 {
26271 it->ascent += thick;
26272 it->descent += thick;
26273 }
26274 else
26275 thick = - thick;
26276
26277 if (it->start_of_box_run_p)
26278 it->pixel_width += thick;
26279 if (it->end_of_box_run_p)
26280 it->pixel_width += thick;
26281 }
26282 /* If face has an overline, add the height of the overline
26283 (1 pixel) and a 1 pixel margin to the character height. */
26284 if (face->overline_p)
26285 it->ascent += overline_margin;
26286 take_vertical_position_into_account (it);
26287 if (it->ascent < 0)
26288 it->ascent = 0;
26289 if (it->descent < 0)
26290 it->descent = 0;
26291
26292 if (it->glyph_row)
26293 append_composite_glyph (it);
26294 }
26295 else if (it->what == IT_GLYPHLESS)
26296 produce_glyphless_glyph (it, 0, Qnil);
26297 else if (it->what == IT_IMAGE)
26298 produce_image_glyph (it);
26299 else if (it->what == IT_STRETCH)
26300 produce_stretch_glyph (it);
26301
26302 done:
26303 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26304 because this isn't true for images with `:ascent 100'. */
26305 eassert (it->ascent >= 0 && it->descent >= 0);
26306 if (it->area == TEXT_AREA)
26307 it->current_x += it->pixel_width;
26308
26309 if (extra_line_spacing > 0)
26310 {
26311 it->descent += extra_line_spacing;
26312 if (extra_line_spacing > it->max_extra_line_spacing)
26313 it->max_extra_line_spacing = extra_line_spacing;
26314 }
26315
26316 it->max_ascent = max (it->max_ascent, it->ascent);
26317 it->max_descent = max (it->max_descent, it->descent);
26318 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26319 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26320 }
26321
26322 /* EXPORT for RIF:
26323 Output LEN glyphs starting at START at the nominal cursor position.
26324 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26325 being updated, and UPDATED_AREA is the area of that row being updated. */
26326
26327 void
26328 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26329 struct glyph *start, enum glyph_row_area updated_area, int len)
26330 {
26331 int x, hpos, chpos = w->phys_cursor.hpos;
26332
26333 eassert (updated_row);
26334 /* When the window is hscrolled, cursor hpos can legitimately be out
26335 of bounds, but we draw the cursor at the corresponding window
26336 margin in that case. */
26337 if (!updated_row->reversed_p && chpos < 0)
26338 chpos = 0;
26339 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26340 chpos = updated_row->used[TEXT_AREA] - 1;
26341
26342 block_input ();
26343
26344 /* Write glyphs. */
26345
26346 hpos = start - updated_row->glyphs[updated_area];
26347 x = draw_glyphs (w, w->output_cursor.x,
26348 updated_row, updated_area,
26349 hpos, hpos + len,
26350 DRAW_NORMAL_TEXT, 0);
26351
26352 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26353 if (updated_area == TEXT_AREA
26354 && w->phys_cursor_on_p
26355 && w->phys_cursor.vpos == w->output_cursor.vpos
26356 && chpos >= hpos
26357 && chpos < hpos + len)
26358 w->phys_cursor_on_p = 0;
26359
26360 unblock_input ();
26361
26362 /* Advance the output cursor. */
26363 w->output_cursor.hpos += len;
26364 w->output_cursor.x = x;
26365 }
26366
26367
26368 /* EXPORT for RIF:
26369 Insert LEN glyphs from START at the nominal cursor position. */
26370
26371 void
26372 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26373 struct glyph *start, enum glyph_row_area updated_area, int len)
26374 {
26375 struct frame *f;
26376 int line_height, shift_by_width, shifted_region_width;
26377 struct glyph_row *row;
26378 struct glyph *glyph;
26379 int frame_x, frame_y;
26380 ptrdiff_t hpos;
26381
26382 eassert (updated_row);
26383 block_input ();
26384 f = XFRAME (WINDOW_FRAME (w));
26385
26386 /* Get the height of the line we are in. */
26387 row = updated_row;
26388 line_height = row->height;
26389
26390 /* Get the width of the glyphs to insert. */
26391 shift_by_width = 0;
26392 for (glyph = start; glyph < start + len; ++glyph)
26393 shift_by_width += glyph->pixel_width;
26394
26395 /* Get the width of the region to shift right. */
26396 shifted_region_width = (window_box_width (w, updated_area)
26397 - w->output_cursor.x
26398 - shift_by_width);
26399
26400 /* Shift right. */
26401 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26402 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26403
26404 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26405 line_height, shift_by_width);
26406
26407 /* Write the glyphs. */
26408 hpos = start - row->glyphs[updated_area];
26409 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26410 hpos, hpos + len,
26411 DRAW_NORMAL_TEXT, 0);
26412
26413 /* Advance the output cursor. */
26414 w->output_cursor.hpos += len;
26415 w->output_cursor.x += shift_by_width;
26416 unblock_input ();
26417 }
26418
26419
26420 /* EXPORT for RIF:
26421 Erase the current text line from the nominal cursor position
26422 (inclusive) to pixel column TO_X (exclusive). The idea is that
26423 everything from TO_X onward is already erased.
26424
26425 TO_X is a pixel position relative to UPDATED_AREA of currently
26426 updated window W. TO_X == -1 means clear to the end of this area. */
26427
26428 void
26429 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26430 enum glyph_row_area updated_area, int to_x)
26431 {
26432 struct frame *f;
26433 int max_x, min_y, max_y;
26434 int from_x, from_y, to_y;
26435
26436 eassert (updated_row);
26437 f = XFRAME (w->frame);
26438
26439 if (updated_row->full_width_p)
26440 max_x = (WINDOW_PIXEL_WIDTH (w)
26441 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26442 else
26443 max_x = window_box_width (w, updated_area);
26444 max_y = window_text_bottom_y (w);
26445
26446 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26447 of window. For TO_X > 0, truncate to end of drawing area. */
26448 if (to_x == 0)
26449 return;
26450 else if (to_x < 0)
26451 to_x = max_x;
26452 else
26453 to_x = min (to_x, max_x);
26454
26455 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26456
26457 /* Notice if the cursor will be cleared by this operation. */
26458 if (!updated_row->full_width_p)
26459 notice_overwritten_cursor (w, updated_area,
26460 w->output_cursor.x, -1,
26461 updated_row->y,
26462 MATRIX_ROW_BOTTOM_Y (updated_row));
26463
26464 from_x = w->output_cursor.x;
26465
26466 /* Translate to frame coordinates. */
26467 if (updated_row->full_width_p)
26468 {
26469 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26470 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26471 }
26472 else
26473 {
26474 int area_left = window_box_left (w, updated_area);
26475 from_x += area_left;
26476 to_x += area_left;
26477 }
26478
26479 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26480 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26481 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26482
26483 /* Prevent inadvertently clearing to end of the X window. */
26484 if (to_x > from_x && to_y > from_y)
26485 {
26486 block_input ();
26487 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26488 to_x - from_x, to_y - from_y);
26489 unblock_input ();
26490 }
26491 }
26492
26493 #endif /* HAVE_WINDOW_SYSTEM */
26494
26495
26496 \f
26497 /***********************************************************************
26498 Cursor types
26499 ***********************************************************************/
26500
26501 /* Value is the internal representation of the specified cursor type
26502 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26503 of the bar cursor. */
26504
26505 static enum text_cursor_kinds
26506 get_specified_cursor_type (Lisp_Object arg, int *width)
26507 {
26508 enum text_cursor_kinds type;
26509
26510 if (NILP (arg))
26511 return NO_CURSOR;
26512
26513 if (EQ (arg, Qbox))
26514 return FILLED_BOX_CURSOR;
26515
26516 if (EQ (arg, Qhollow))
26517 return HOLLOW_BOX_CURSOR;
26518
26519 if (EQ (arg, Qbar))
26520 {
26521 *width = 2;
26522 return BAR_CURSOR;
26523 }
26524
26525 if (CONSP (arg)
26526 && EQ (XCAR (arg), Qbar)
26527 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26528 {
26529 *width = XINT (XCDR (arg));
26530 return BAR_CURSOR;
26531 }
26532
26533 if (EQ (arg, Qhbar))
26534 {
26535 *width = 2;
26536 return HBAR_CURSOR;
26537 }
26538
26539 if (CONSP (arg)
26540 && EQ (XCAR (arg), Qhbar)
26541 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26542 {
26543 *width = XINT (XCDR (arg));
26544 return HBAR_CURSOR;
26545 }
26546
26547 /* Treat anything unknown as "hollow box cursor".
26548 It was bad to signal an error; people have trouble fixing
26549 .Xdefaults with Emacs, when it has something bad in it. */
26550 type = HOLLOW_BOX_CURSOR;
26551
26552 return type;
26553 }
26554
26555 /* Set the default cursor types for specified frame. */
26556 void
26557 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26558 {
26559 int width = 1;
26560 Lisp_Object tem;
26561
26562 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26563 FRAME_CURSOR_WIDTH (f) = width;
26564
26565 /* By default, set up the blink-off state depending on the on-state. */
26566
26567 tem = Fassoc (arg, Vblink_cursor_alist);
26568 if (!NILP (tem))
26569 {
26570 FRAME_BLINK_OFF_CURSOR (f)
26571 = get_specified_cursor_type (XCDR (tem), &width);
26572 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26573 }
26574 else
26575 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26576
26577 /* Make sure the cursor gets redrawn. */
26578 f->cursor_type_changed = 1;
26579 }
26580
26581
26582 #ifdef HAVE_WINDOW_SYSTEM
26583
26584 /* Return the cursor we want to be displayed in window W. Return
26585 width of bar/hbar cursor through WIDTH arg. Return with
26586 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26587 (i.e. if the `system caret' should track this cursor).
26588
26589 In a mini-buffer window, we want the cursor only to appear if we
26590 are reading input from this window. For the selected window, we
26591 want the cursor type given by the frame parameter or buffer local
26592 setting of cursor-type. If explicitly marked off, draw no cursor.
26593 In all other cases, we want a hollow box cursor. */
26594
26595 static enum text_cursor_kinds
26596 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26597 int *active_cursor)
26598 {
26599 struct frame *f = XFRAME (w->frame);
26600 struct buffer *b = XBUFFER (w->contents);
26601 int cursor_type = DEFAULT_CURSOR;
26602 Lisp_Object alt_cursor;
26603 int non_selected = 0;
26604
26605 *active_cursor = 1;
26606
26607 /* Echo area */
26608 if (cursor_in_echo_area
26609 && FRAME_HAS_MINIBUF_P (f)
26610 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26611 {
26612 if (w == XWINDOW (echo_area_window))
26613 {
26614 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26615 {
26616 *width = FRAME_CURSOR_WIDTH (f);
26617 return FRAME_DESIRED_CURSOR (f);
26618 }
26619 else
26620 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26621 }
26622
26623 *active_cursor = 0;
26624 non_selected = 1;
26625 }
26626
26627 /* Detect a nonselected window or nonselected frame. */
26628 else if (w != XWINDOW (f->selected_window)
26629 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26630 {
26631 *active_cursor = 0;
26632
26633 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26634 return NO_CURSOR;
26635
26636 non_selected = 1;
26637 }
26638
26639 /* Never display a cursor in a window in which cursor-type is nil. */
26640 if (NILP (BVAR (b, cursor_type)))
26641 return NO_CURSOR;
26642
26643 /* Get the normal cursor type for this window. */
26644 if (EQ (BVAR (b, cursor_type), Qt))
26645 {
26646 cursor_type = FRAME_DESIRED_CURSOR (f);
26647 *width = FRAME_CURSOR_WIDTH (f);
26648 }
26649 else
26650 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26651
26652 /* Use cursor-in-non-selected-windows instead
26653 for non-selected window or frame. */
26654 if (non_selected)
26655 {
26656 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26657 if (!EQ (Qt, alt_cursor))
26658 return get_specified_cursor_type (alt_cursor, width);
26659 /* t means modify the normal cursor type. */
26660 if (cursor_type == FILLED_BOX_CURSOR)
26661 cursor_type = HOLLOW_BOX_CURSOR;
26662 else if (cursor_type == BAR_CURSOR && *width > 1)
26663 --*width;
26664 return cursor_type;
26665 }
26666
26667 /* Use normal cursor if not blinked off. */
26668 if (!w->cursor_off_p)
26669 {
26670 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26671 {
26672 if (cursor_type == FILLED_BOX_CURSOR)
26673 {
26674 /* Using a block cursor on large images can be very annoying.
26675 So use a hollow cursor for "large" images.
26676 If image is not transparent (no mask), also use hollow cursor. */
26677 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26678 if (img != NULL && IMAGEP (img->spec))
26679 {
26680 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26681 where N = size of default frame font size.
26682 This should cover most of the "tiny" icons people may use. */
26683 if (!img->mask
26684 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26685 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26686 cursor_type = HOLLOW_BOX_CURSOR;
26687 }
26688 }
26689 else if (cursor_type != NO_CURSOR)
26690 {
26691 /* Display current only supports BOX and HOLLOW cursors for images.
26692 So for now, unconditionally use a HOLLOW cursor when cursor is
26693 not a solid box cursor. */
26694 cursor_type = HOLLOW_BOX_CURSOR;
26695 }
26696 }
26697 return cursor_type;
26698 }
26699
26700 /* Cursor is blinked off, so determine how to "toggle" it. */
26701
26702 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26703 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26704 return get_specified_cursor_type (XCDR (alt_cursor), width);
26705
26706 /* Then see if frame has specified a specific blink off cursor type. */
26707 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26708 {
26709 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26710 return FRAME_BLINK_OFF_CURSOR (f);
26711 }
26712
26713 #if 0
26714 /* Some people liked having a permanently visible blinking cursor,
26715 while others had very strong opinions against it. So it was
26716 decided to remove it. KFS 2003-09-03 */
26717
26718 /* Finally perform built-in cursor blinking:
26719 filled box <-> hollow box
26720 wide [h]bar <-> narrow [h]bar
26721 narrow [h]bar <-> no cursor
26722 other type <-> no cursor */
26723
26724 if (cursor_type == FILLED_BOX_CURSOR)
26725 return HOLLOW_BOX_CURSOR;
26726
26727 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26728 {
26729 *width = 1;
26730 return cursor_type;
26731 }
26732 #endif
26733
26734 return NO_CURSOR;
26735 }
26736
26737
26738 /* Notice when the text cursor of window W has been completely
26739 overwritten by a drawing operation that outputs glyphs in AREA
26740 starting at X0 and ending at X1 in the line starting at Y0 and
26741 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26742 the rest of the line after X0 has been written. Y coordinates
26743 are window-relative. */
26744
26745 static void
26746 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26747 int x0, int x1, int y0, int y1)
26748 {
26749 int cx0, cx1, cy0, cy1;
26750 struct glyph_row *row;
26751
26752 if (!w->phys_cursor_on_p)
26753 return;
26754 if (area != TEXT_AREA)
26755 return;
26756
26757 if (w->phys_cursor.vpos < 0
26758 || w->phys_cursor.vpos >= w->current_matrix->nrows
26759 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26760 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26761 return;
26762
26763 if (row->cursor_in_fringe_p)
26764 {
26765 row->cursor_in_fringe_p = 0;
26766 draw_fringe_bitmap (w, row, row->reversed_p);
26767 w->phys_cursor_on_p = 0;
26768 return;
26769 }
26770
26771 cx0 = w->phys_cursor.x;
26772 cx1 = cx0 + w->phys_cursor_width;
26773 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26774 return;
26775
26776 /* The cursor image will be completely removed from the
26777 screen if the output area intersects the cursor area in
26778 y-direction. When we draw in [y0 y1[, and some part of
26779 the cursor is at y < y0, that part must have been drawn
26780 before. When scrolling, the cursor is erased before
26781 actually scrolling, so we don't come here. When not
26782 scrolling, the rows above the old cursor row must have
26783 changed, and in this case these rows must have written
26784 over the cursor image.
26785
26786 Likewise if part of the cursor is below y1, with the
26787 exception of the cursor being in the first blank row at
26788 the buffer and window end because update_text_area
26789 doesn't draw that row. (Except when it does, but
26790 that's handled in update_text_area.) */
26791
26792 cy0 = w->phys_cursor.y;
26793 cy1 = cy0 + w->phys_cursor_height;
26794 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26795 return;
26796
26797 w->phys_cursor_on_p = 0;
26798 }
26799
26800 #endif /* HAVE_WINDOW_SYSTEM */
26801
26802 \f
26803 /************************************************************************
26804 Mouse Face
26805 ************************************************************************/
26806
26807 #ifdef HAVE_WINDOW_SYSTEM
26808
26809 /* EXPORT for RIF:
26810 Fix the display of area AREA of overlapping row ROW in window W
26811 with respect to the overlapping part OVERLAPS. */
26812
26813 void
26814 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26815 enum glyph_row_area area, int overlaps)
26816 {
26817 int i, x;
26818
26819 block_input ();
26820
26821 x = 0;
26822 for (i = 0; i < row->used[area];)
26823 {
26824 if (row->glyphs[area][i].overlaps_vertically_p)
26825 {
26826 int start = i, start_x = x;
26827
26828 do
26829 {
26830 x += row->glyphs[area][i].pixel_width;
26831 ++i;
26832 }
26833 while (i < row->used[area]
26834 && row->glyphs[area][i].overlaps_vertically_p);
26835
26836 draw_glyphs (w, start_x, row, area,
26837 start, i,
26838 DRAW_NORMAL_TEXT, overlaps);
26839 }
26840 else
26841 {
26842 x += row->glyphs[area][i].pixel_width;
26843 ++i;
26844 }
26845 }
26846
26847 unblock_input ();
26848 }
26849
26850
26851 /* EXPORT:
26852 Draw the cursor glyph of window W in glyph row ROW. See the
26853 comment of draw_glyphs for the meaning of HL. */
26854
26855 void
26856 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26857 enum draw_glyphs_face hl)
26858 {
26859 /* If cursor hpos is out of bounds, don't draw garbage. This can
26860 happen in mini-buffer windows when switching between echo area
26861 glyphs and mini-buffer. */
26862 if ((row->reversed_p
26863 ? (w->phys_cursor.hpos >= 0)
26864 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26865 {
26866 int on_p = w->phys_cursor_on_p;
26867 int x1;
26868 int hpos = w->phys_cursor.hpos;
26869
26870 /* When the window is hscrolled, cursor hpos can legitimately be
26871 out of bounds, but we draw the cursor at the corresponding
26872 window margin in that case. */
26873 if (!row->reversed_p && hpos < 0)
26874 hpos = 0;
26875 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26876 hpos = row->used[TEXT_AREA] - 1;
26877
26878 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26879 hl, 0);
26880 w->phys_cursor_on_p = on_p;
26881
26882 if (hl == DRAW_CURSOR)
26883 w->phys_cursor_width = x1 - w->phys_cursor.x;
26884 /* When we erase the cursor, and ROW is overlapped by other
26885 rows, make sure that these overlapping parts of other rows
26886 are redrawn. */
26887 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26888 {
26889 w->phys_cursor_width = x1 - w->phys_cursor.x;
26890
26891 if (row > w->current_matrix->rows
26892 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26893 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26894 OVERLAPS_ERASED_CURSOR);
26895
26896 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26897 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26898 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26899 OVERLAPS_ERASED_CURSOR);
26900 }
26901 }
26902 }
26903
26904
26905 /* Erase the image of a cursor of window W from the screen. */
26906
26907 #ifndef HAVE_NTGUI
26908 static
26909 #endif
26910 void
26911 erase_phys_cursor (struct window *w)
26912 {
26913 struct frame *f = XFRAME (w->frame);
26914 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26915 int hpos = w->phys_cursor.hpos;
26916 int vpos = w->phys_cursor.vpos;
26917 int mouse_face_here_p = 0;
26918 struct glyph_matrix *active_glyphs = w->current_matrix;
26919 struct glyph_row *cursor_row;
26920 struct glyph *cursor_glyph;
26921 enum draw_glyphs_face hl;
26922
26923 /* No cursor displayed or row invalidated => nothing to do on the
26924 screen. */
26925 if (w->phys_cursor_type == NO_CURSOR)
26926 goto mark_cursor_off;
26927
26928 /* VPOS >= active_glyphs->nrows means that window has been resized.
26929 Don't bother to erase the cursor. */
26930 if (vpos >= active_glyphs->nrows)
26931 goto mark_cursor_off;
26932
26933 /* If row containing cursor is marked invalid, there is nothing we
26934 can do. */
26935 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26936 if (!cursor_row->enabled_p)
26937 goto mark_cursor_off;
26938
26939 /* If line spacing is > 0, old cursor may only be partially visible in
26940 window after split-window. So adjust visible height. */
26941 cursor_row->visible_height = min (cursor_row->visible_height,
26942 window_text_bottom_y (w) - cursor_row->y);
26943
26944 /* If row is completely invisible, don't attempt to delete a cursor which
26945 isn't there. This can happen if cursor is at top of a window, and
26946 we switch to a buffer with a header line in that window. */
26947 if (cursor_row->visible_height <= 0)
26948 goto mark_cursor_off;
26949
26950 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26951 if (cursor_row->cursor_in_fringe_p)
26952 {
26953 cursor_row->cursor_in_fringe_p = 0;
26954 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26955 goto mark_cursor_off;
26956 }
26957
26958 /* This can happen when the new row is shorter than the old one.
26959 In this case, either draw_glyphs or clear_end_of_line
26960 should have cleared the cursor. Note that we wouldn't be
26961 able to erase the cursor in this case because we don't have a
26962 cursor glyph at hand. */
26963 if ((cursor_row->reversed_p
26964 ? (w->phys_cursor.hpos < 0)
26965 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26966 goto mark_cursor_off;
26967
26968 /* When the window is hscrolled, cursor hpos can legitimately be out
26969 of bounds, but we draw the cursor at the corresponding window
26970 margin in that case. */
26971 if (!cursor_row->reversed_p && hpos < 0)
26972 hpos = 0;
26973 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26974 hpos = cursor_row->used[TEXT_AREA] - 1;
26975
26976 /* If the cursor is in the mouse face area, redisplay that when
26977 we clear the cursor. */
26978 if (! NILP (hlinfo->mouse_face_window)
26979 && coords_in_mouse_face_p (w, hpos, vpos)
26980 /* Don't redraw the cursor's spot in mouse face if it is at the
26981 end of a line (on a newline). The cursor appears there, but
26982 mouse highlighting does not. */
26983 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26984 mouse_face_here_p = 1;
26985
26986 /* Maybe clear the display under the cursor. */
26987 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26988 {
26989 int x, y, left_x;
26990 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26991 int width;
26992
26993 cursor_glyph = get_phys_cursor_glyph (w);
26994 if (cursor_glyph == NULL)
26995 goto mark_cursor_off;
26996
26997 width = cursor_glyph->pixel_width;
26998 left_x = window_box_left_offset (w, TEXT_AREA);
26999 x = w->phys_cursor.x;
27000 if (x < left_x)
27001 width -= left_x - x;
27002 width = min (width, window_box_width (w, TEXT_AREA) - x);
27003 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27004 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27005
27006 if (width > 0)
27007 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27008 }
27009
27010 /* Erase the cursor by redrawing the character underneath it. */
27011 if (mouse_face_here_p)
27012 hl = DRAW_MOUSE_FACE;
27013 else
27014 hl = DRAW_NORMAL_TEXT;
27015 draw_phys_cursor_glyph (w, cursor_row, hl);
27016
27017 mark_cursor_off:
27018 w->phys_cursor_on_p = 0;
27019 w->phys_cursor_type = NO_CURSOR;
27020 }
27021
27022
27023 /* EXPORT:
27024 Display or clear cursor of window W. If ON is zero, clear the
27025 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27026 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27027
27028 void
27029 display_and_set_cursor (struct window *w, bool on,
27030 int hpos, int vpos, int x, int y)
27031 {
27032 struct frame *f = XFRAME (w->frame);
27033 int new_cursor_type;
27034 int new_cursor_width;
27035 int active_cursor;
27036 struct glyph_row *glyph_row;
27037 struct glyph *glyph;
27038
27039 /* This is pointless on invisible frames, and dangerous on garbaged
27040 windows and frames; in the latter case, the frame or window may
27041 be in the midst of changing its size, and x and y may be off the
27042 window. */
27043 if (! FRAME_VISIBLE_P (f)
27044 || FRAME_GARBAGED_P (f)
27045 || vpos >= w->current_matrix->nrows
27046 || hpos >= w->current_matrix->matrix_w)
27047 return;
27048
27049 /* If cursor is off and we want it off, return quickly. */
27050 if (!on && !w->phys_cursor_on_p)
27051 return;
27052
27053 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27054 /* If cursor row is not enabled, we don't really know where to
27055 display the cursor. */
27056 if (!glyph_row->enabled_p)
27057 {
27058 w->phys_cursor_on_p = 0;
27059 return;
27060 }
27061
27062 glyph = NULL;
27063 if (!glyph_row->exact_window_width_line_p
27064 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27065 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27066
27067 eassert (input_blocked_p ());
27068
27069 /* Set new_cursor_type to the cursor we want to be displayed. */
27070 new_cursor_type = get_window_cursor_type (w, glyph,
27071 &new_cursor_width, &active_cursor);
27072
27073 /* If cursor is currently being shown and we don't want it to be or
27074 it is in the wrong place, or the cursor type is not what we want,
27075 erase it. */
27076 if (w->phys_cursor_on_p
27077 && (!on
27078 || w->phys_cursor.x != x
27079 || w->phys_cursor.y != y
27080 || new_cursor_type != w->phys_cursor_type
27081 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27082 && new_cursor_width != w->phys_cursor_width)))
27083 erase_phys_cursor (w);
27084
27085 /* Don't check phys_cursor_on_p here because that flag is only set
27086 to zero in some cases where we know that the cursor has been
27087 completely erased, to avoid the extra work of erasing the cursor
27088 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27089 still not be visible, or it has only been partly erased. */
27090 if (on)
27091 {
27092 w->phys_cursor_ascent = glyph_row->ascent;
27093 w->phys_cursor_height = glyph_row->height;
27094
27095 /* Set phys_cursor_.* before x_draw_.* is called because some
27096 of them may need the information. */
27097 w->phys_cursor.x = x;
27098 w->phys_cursor.y = glyph_row->y;
27099 w->phys_cursor.hpos = hpos;
27100 w->phys_cursor.vpos = vpos;
27101 }
27102
27103 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27104 new_cursor_type, new_cursor_width,
27105 on, active_cursor);
27106 }
27107
27108
27109 /* Switch the display of W's cursor on or off, according to the value
27110 of ON. */
27111
27112 static void
27113 update_window_cursor (struct window *w, bool on)
27114 {
27115 /* Don't update cursor in windows whose frame is in the process
27116 of being deleted. */
27117 if (w->current_matrix)
27118 {
27119 int hpos = w->phys_cursor.hpos;
27120 int vpos = w->phys_cursor.vpos;
27121 struct glyph_row *row;
27122
27123 if (vpos >= w->current_matrix->nrows
27124 || hpos >= w->current_matrix->matrix_w)
27125 return;
27126
27127 row = MATRIX_ROW (w->current_matrix, vpos);
27128
27129 /* When the window is hscrolled, cursor hpos can legitimately be
27130 out of bounds, but we draw the cursor at the corresponding
27131 window margin in that case. */
27132 if (!row->reversed_p && hpos < 0)
27133 hpos = 0;
27134 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27135 hpos = row->used[TEXT_AREA] - 1;
27136
27137 block_input ();
27138 display_and_set_cursor (w, on, hpos, vpos,
27139 w->phys_cursor.x, w->phys_cursor.y);
27140 unblock_input ();
27141 }
27142 }
27143
27144
27145 /* Call update_window_cursor with parameter ON_P on all leaf windows
27146 in the window tree rooted at W. */
27147
27148 static void
27149 update_cursor_in_window_tree (struct window *w, bool on_p)
27150 {
27151 while (w)
27152 {
27153 if (WINDOWP (w->contents))
27154 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27155 else
27156 update_window_cursor (w, on_p);
27157
27158 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27159 }
27160 }
27161
27162
27163 /* EXPORT:
27164 Display the cursor on window W, or clear it, according to ON_P.
27165 Don't change the cursor's position. */
27166
27167 void
27168 x_update_cursor (struct frame *f, bool on_p)
27169 {
27170 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27171 }
27172
27173
27174 /* EXPORT:
27175 Clear the cursor of window W to background color, and mark the
27176 cursor as not shown. This is used when the text where the cursor
27177 is about to be rewritten. */
27178
27179 void
27180 x_clear_cursor (struct window *w)
27181 {
27182 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27183 update_window_cursor (w, 0);
27184 }
27185
27186 #endif /* HAVE_WINDOW_SYSTEM */
27187
27188 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27189 and MSDOS. */
27190 static void
27191 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27192 int start_hpos, int end_hpos,
27193 enum draw_glyphs_face draw)
27194 {
27195 #ifdef HAVE_WINDOW_SYSTEM
27196 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27197 {
27198 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27199 return;
27200 }
27201 #endif
27202 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27203 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27204 #endif
27205 }
27206
27207 /* Display the active region described by mouse_face_* according to DRAW. */
27208
27209 static void
27210 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27211 {
27212 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27213 struct frame *f = XFRAME (WINDOW_FRAME (w));
27214
27215 if (/* If window is in the process of being destroyed, don't bother
27216 to do anything. */
27217 w->current_matrix != NULL
27218 /* Don't update mouse highlight if hidden */
27219 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27220 /* Recognize when we are called to operate on rows that don't exist
27221 anymore. This can happen when a window is split. */
27222 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27223 {
27224 int phys_cursor_on_p = w->phys_cursor_on_p;
27225 struct glyph_row *row, *first, *last;
27226
27227 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27228 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27229
27230 for (row = first; row <= last && row->enabled_p; ++row)
27231 {
27232 int start_hpos, end_hpos, start_x;
27233
27234 /* For all but the first row, the highlight starts at column 0. */
27235 if (row == first)
27236 {
27237 /* R2L rows have BEG and END in reversed order, but the
27238 screen drawing geometry is always left to right. So
27239 we need to mirror the beginning and end of the
27240 highlighted area in R2L rows. */
27241 if (!row->reversed_p)
27242 {
27243 start_hpos = hlinfo->mouse_face_beg_col;
27244 start_x = hlinfo->mouse_face_beg_x;
27245 }
27246 else if (row == last)
27247 {
27248 start_hpos = hlinfo->mouse_face_end_col;
27249 start_x = hlinfo->mouse_face_end_x;
27250 }
27251 else
27252 {
27253 start_hpos = 0;
27254 start_x = 0;
27255 }
27256 }
27257 else if (row->reversed_p && row == last)
27258 {
27259 start_hpos = hlinfo->mouse_face_end_col;
27260 start_x = hlinfo->mouse_face_end_x;
27261 }
27262 else
27263 {
27264 start_hpos = 0;
27265 start_x = 0;
27266 }
27267
27268 if (row == last)
27269 {
27270 if (!row->reversed_p)
27271 end_hpos = hlinfo->mouse_face_end_col;
27272 else if (row == first)
27273 end_hpos = hlinfo->mouse_face_beg_col;
27274 else
27275 {
27276 end_hpos = row->used[TEXT_AREA];
27277 if (draw == DRAW_NORMAL_TEXT)
27278 row->fill_line_p = 1; /* Clear to end of line */
27279 }
27280 }
27281 else if (row->reversed_p && row == first)
27282 end_hpos = hlinfo->mouse_face_beg_col;
27283 else
27284 {
27285 end_hpos = row->used[TEXT_AREA];
27286 if (draw == DRAW_NORMAL_TEXT)
27287 row->fill_line_p = 1; /* Clear to end of line */
27288 }
27289
27290 if (end_hpos > start_hpos)
27291 {
27292 draw_row_with_mouse_face (w, start_x, row,
27293 start_hpos, end_hpos, draw);
27294
27295 row->mouse_face_p
27296 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27297 }
27298 }
27299
27300 #ifdef HAVE_WINDOW_SYSTEM
27301 /* When we've written over the cursor, arrange for it to
27302 be displayed again. */
27303 if (FRAME_WINDOW_P (f)
27304 && phys_cursor_on_p && !w->phys_cursor_on_p)
27305 {
27306 int hpos = w->phys_cursor.hpos;
27307
27308 /* When the window is hscrolled, cursor hpos can legitimately be
27309 out of bounds, but we draw the cursor at the corresponding
27310 window margin in that case. */
27311 if (!row->reversed_p && hpos < 0)
27312 hpos = 0;
27313 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27314 hpos = row->used[TEXT_AREA] - 1;
27315
27316 block_input ();
27317 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27318 w->phys_cursor.x, w->phys_cursor.y);
27319 unblock_input ();
27320 }
27321 #endif /* HAVE_WINDOW_SYSTEM */
27322 }
27323
27324 #ifdef HAVE_WINDOW_SYSTEM
27325 /* Change the mouse cursor. */
27326 if (FRAME_WINDOW_P (f))
27327 {
27328 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27329 if (draw == DRAW_NORMAL_TEXT
27330 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27331 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27332 else
27333 #endif
27334 if (draw == DRAW_MOUSE_FACE)
27335 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27336 else
27337 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27338 }
27339 #endif /* HAVE_WINDOW_SYSTEM */
27340 }
27341
27342 /* EXPORT:
27343 Clear out the mouse-highlighted active region.
27344 Redraw it un-highlighted first. Value is non-zero if mouse
27345 face was actually drawn unhighlighted. */
27346
27347 int
27348 clear_mouse_face (Mouse_HLInfo *hlinfo)
27349 {
27350 int cleared = 0;
27351
27352 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27353 {
27354 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27355 cleared = 1;
27356 }
27357
27358 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27359 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27360 hlinfo->mouse_face_window = Qnil;
27361 hlinfo->mouse_face_overlay = Qnil;
27362 return cleared;
27363 }
27364
27365 /* Return true if the coordinates HPOS and VPOS on windows W are
27366 within the mouse face on that window. */
27367 static bool
27368 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27369 {
27370 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27371
27372 /* Quickly resolve the easy cases. */
27373 if (!(WINDOWP (hlinfo->mouse_face_window)
27374 && XWINDOW (hlinfo->mouse_face_window) == w))
27375 return false;
27376 if (vpos < hlinfo->mouse_face_beg_row
27377 || vpos > hlinfo->mouse_face_end_row)
27378 return false;
27379 if (vpos > hlinfo->mouse_face_beg_row
27380 && vpos < hlinfo->mouse_face_end_row)
27381 return true;
27382
27383 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27384 {
27385 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27386 {
27387 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27388 return true;
27389 }
27390 else if ((vpos == hlinfo->mouse_face_beg_row
27391 && hpos >= hlinfo->mouse_face_beg_col)
27392 || (vpos == hlinfo->mouse_face_end_row
27393 && hpos < hlinfo->mouse_face_end_col))
27394 return true;
27395 }
27396 else
27397 {
27398 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27399 {
27400 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27401 return true;
27402 }
27403 else if ((vpos == hlinfo->mouse_face_beg_row
27404 && hpos <= hlinfo->mouse_face_beg_col)
27405 || (vpos == hlinfo->mouse_face_end_row
27406 && hpos > hlinfo->mouse_face_end_col))
27407 return true;
27408 }
27409 return false;
27410 }
27411
27412
27413 /* EXPORT:
27414 True if physical cursor of window W is within mouse face. */
27415
27416 bool
27417 cursor_in_mouse_face_p (struct window *w)
27418 {
27419 int hpos = w->phys_cursor.hpos;
27420 int vpos = w->phys_cursor.vpos;
27421 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27422
27423 /* When the window is hscrolled, cursor hpos can legitimately be out
27424 of bounds, but we draw the cursor at the corresponding window
27425 margin in that case. */
27426 if (!row->reversed_p && hpos < 0)
27427 hpos = 0;
27428 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27429 hpos = row->used[TEXT_AREA] - 1;
27430
27431 return coords_in_mouse_face_p (w, hpos, vpos);
27432 }
27433
27434
27435 \f
27436 /* Find the glyph rows START_ROW and END_ROW of window W that display
27437 characters between buffer positions START_CHARPOS and END_CHARPOS
27438 (excluding END_CHARPOS). DISP_STRING is a display string that
27439 covers these buffer positions. This is similar to
27440 row_containing_pos, but is more accurate when bidi reordering makes
27441 buffer positions change non-linearly with glyph rows. */
27442 static void
27443 rows_from_pos_range (struct window *w,
27444 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27445 Lisp_Object disp_string,
27446 struct glyph_row **start, struct glyph_row **end)
27447 {
27448 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27449 int last_y = window_text_bottom_y (w);
27450 struct glyph_row *row;
27451
27452 *start = NULL;
27453 *end = NULL;
27454
27455 while (!first->enabled_p
27456 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27457 first++;
27458
27459 /* Find the START row. */
27460 for (row = first;
27461 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27462 row++)
27463 {
27464 /* A row can potentially be the START row if the range of the
27465 characters it displays intersects the range
27466 [START_CHARPOS..END_CHARPOS). */
27467 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27468 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27469 /* See the commentary in row_containing_pos, for the
27470 explanation of the complicated way to check whether
27471 some position is beyond the end of the characters
27472 displayed by a row. */
27473 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27474 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27475 && !row->ends_at_zv_p
27476 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27477 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27478 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27479 && !row->ends_at_zv_p
27480 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27481 {
27482 /* Found a candidate row. Now make sure at least one of the
27483 glyphs it displays has a charpos from the range
27484 [START_CHARPOS..END_CHARPOS).
27485
27486 This is not obvious because bidi reordering could make
27487 buffer positions of a row be 1,2,3,102,101,100, and if we
27488 want to highlight characters in [50..60), we don't want
27489 this row, even though [50..60) does intersect [1..103),
27490 the range of character positions given by the row's start
27491 and end positions. */
27492 struct glyph *g = row->glyphs[TEXT_AREA];
27493 struct glyph *e = g + row->used[TEXT_AREA];
27494
27495 while (g < e)
27496 {
27497 if (((BUFFERP (g->object) || INTEGERP (g->object))
27498 && start_charpos <= g->charpos && g->charpos < end_charpos)
27499 /* A glyph that comes from DISP_STRING is by
27500 definition to be highlighted. */
27501 || EQ (g->object, disp_string))
27502 *start = row;
27503 g++;
27504 }
27505 if (*start)
27506 break;
27507 }
27508 }
27509
27510 /* Find the END row. */
27511 if (!*start
27512 /* If the last row is partially visible, start looking for END
27513 from that row, instead of starting from FIRST. */
27514 && !(row->enabled_p
27515 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27516 row = first;
27517 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27518 {
27519 struct glyph_row *next = row + 1;
27520 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27521
27522 if (!next->enabled_p
27523 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27524 /* The first row >= START whose range of displayed characters
27525 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27526 is the row END + 1. */
27527 || (start_charpos < next_start
27528 && end_charpos < next_start)
27529 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27530 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27531 && !next->ends_at_zv_p
27532 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27533 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27534 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27535 && !next->ends_at_zv_p
27536 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27537 {
27538 *end = row;
27539 break;
27540 }
27541 else
27542 {
27543 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27544 but none of the characters it displays are in the range, it is
27545 also END + 1. */
27546 struct glyph *g = next->glyphs[TEXT_AREA];
27547 struct glyph *s = g;
27548 struct glyph *e = g + next->used[TEXT_AREA];
27549
27550 while (g < e)
27551 {
27552 if (((BUFFERP (g->object) || INTEGERP (g->object))
27553 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27554 /* If the buffer position of the first glyph in
27555 the row is equal to END_CHARPOS, it means
27556 the last character to be highlighted is the
27557 newline of ROW, and we must consider NEXT as
27558 END, not END+1. */
27559 || (((!next->reversed_p && g == s)
27560 || (next->reversed_p && g == e - 1))
27561 && (g->charpos == end_charpos
27562 /* Special case for when NEXT is an
27563 empty line at ZV. */
27564 || (g->charpos == -1
27565 && !row->ends_at_zv_p
27566 && next_start == end_charpos)))))
27567 /* A glyph that comes from DISP_STRING is by
27568 definition to be highlighted. */
27569 || EQ (g->object, disp_string))
27570 break;
27571 g++;
27572 }
27573 if (g == e)
27574 {
27575 *end = row;
27576 break;
27577 }
27578 /* The first row that ends at ZV must be the last to be
27579 highlighted. */
27580 else if (next->ends_at_zv_p)
27581 {
27582 *end = next;
27583 break;
27584 }
27585 }
27586 }
27587 }
27588
27589 /* This function sets the mouse_face_* elements of HLINFO, assuming
27590 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27591 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27592 for the overlay or run of text properties specifying the mouse
27593 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27594 before-string and after-string that must also be highlighted.
27595 DISP_STRING, if non-nil, is a display string that may cover some
27596 or all of the highlighted text. */
27597
27598 static void
27599 mouse_face_from_buffer_pos (Lisp_Object window,
27600 Mouse_HLInfo *hlinfo,
27601 ptrdiff_t mouse_charpos,
27602 ptrdiff_t start_charpos,
27603 ptrdiff_t end_charpos,
27604 Lisp_Object before_string,
27605 Lisp_Object after_string,
27606 Lisp_Object disp_string)
27607 {
27608 struct window *w = XWINDOW (window);
27609 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27610 struct glyph_row *r1, *r2;
27611 struct glyph *glyph, *end;
27612 ptrdiff_t ignore, pos;
27613 int x;
27614
27615 eassert (NILP (disp_string) || STRINGP (disp_string));
27616 eassert (NILP (before_string) || STRINGP (before_string));
27617 eassert (NILP (after_string) || STRINGP (after_string));
27618
27619 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27620 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27621 if (r1 == NULL)
27622 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27623 /* If the before-string or display-string contains newlines,
27624 rows_from_pos_range skips to its last row. Move back. */
27625 if (!NILP (before_string) || !NILP (disp_string))
27626 {
27627 struct glyph_row *prev;
27628 while ((prev = r1 - 1, prev >= first)
27629 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27630 && prev->used[TEXT_AREA] > 0)
27631 {
27632 struct glyph *beg = prev->glyphs[TEXT_AREA];
27633 glyph = beg + prev->used[TEXT_AREA];
27634 while (--glyph >= beg && INTEGERP (glyph->object));
27635 if (glyph < beg
27636 || !(EQ (glyph->object, before_string)
27637 || EQ (glyph->object, disp_string)))
27638 break;
27639 r1 = prev;
27640 }
27641 }
27642 if (r2 == NULL)
27643 {
27644 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27645 hlinfo->mouse_face_past_end = 1;
27646 }
27647 else if (!NILP (after_string))
27648 {
27649 /* If the after-string has newlines, advance to its last row. */
27650 struct glyph_row *next;
27651 struct glyph_row *last
27652 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27653
27654 for (next = r2 + 1;
27655 next <= last
27656 && next->used[TEXT_AREA] > 0
27657 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27658 ++next)
27659 r2 = next;
27660 }
27661 /* The rest of the display engine assumes that mouse_face_beg_row is
27662 either above mouse_face_end_row or identical to it. But with
27663 bidi-reordered continued lines, the row for START_CHARPOS could
27664 be below the row for END_CHARPOS. If so, swap the rows and store
27665 them in correct order. */
27666 if (r1->y > r2->y)
27667 {
27668 struct glyph_row *tem = r2;
27669
27670 r2 = r1;
27671 r1 = tem;
27672 }
27673
27674 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27675 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27676
27677 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27678 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27679 could be anywhere in the row and in any order. The strategy
27680 below is to find the leftmost and the rightmost glyph that
27681 belongs to either of these 3 strings, or whose position is
27682 between START_CHARPOS and END_CHARPOS, and highlight all the
27683 glyphs between those two. This may cover more than just the text
27684 between START_CHARPOS and END_CHARPOS if the range of characters
27685 strides the bidi level boundary, e.g. if the beginning is in R2L
27686 text while the end is in L2R text or vice versa. */
27687 if (!r1->reversed_p)
27688 {
27689 /* This row is in a left to right paragraph. Scan it left to
27690 right. */
27691 glyph = r1->glyphs[TEXT_AREA];
27692 end = glyph + r1->used[TEXT_AREA];
27693 x = r1->x;
27694
27695 /* Skip truncation glyphs at the start of the glyph row. */
27696 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27697 for (; glyph < end
27698 && INTEGERP (glyph->object)
27699 && glyph->charpos < 0;
27700 ++glyph)
27701 x += glyph->pixel_width;
27702
27703 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27704 or DISP_STRING, and the first glyph from buffer whose
27705 position is between START_CHARPOS and END_CHARPOS. */
27706 for (; glyph < end
27707 && !INTEGERP (glyph->object)
27708 && !EQ (glyph->object, disp_string)
27709 && !(BUFFERP (glyph->object)
27710 && (glyph->charpos >= start_charpos
27711 && glyph->charpos < end_charpos));
27712 ++glyph)
27713 {
27714 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27715 are present at buffer positions between START_CHARPOS and
27716 END_CHARPOS, or if they come from an overlay. */
27717 if (EQ (glyph->object, before_string))
27718 {
27719 pos = string_buffer_position (before_string,
27720 start_charpos);
27721 /* If pos == 0, it means before_string came from an
27722 overlay, not from a buffer position. */
27723 if (!pos || (pos >= start_charpos && pos < end_charpos))
27724 break;
27725 }
27726 else if (EQ (glyph->object, after_string))
27727 {
27728 pos = string_buffer_position (after_string, end_charpos);
27729 if (!pos || (pos >= start_charpos && pos < end_charpos))
27730 break;
27731 }
27732 x += glyph->pixel_width;
27733 }
27734 hlinfo->mouse_face_beg_x = x;
27735 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27736 }
27737 else
27738 {
27739 /* This row is in a right to left paragraph. Scan it right to
27740 left. */
27741 struct glyph *g;
27742
27743 end = r1->glyphs[TEXT_AREA] - 1;
27744 glyph = end + r1->used[TEXT_AREA];
27745
27746 /* Skip truncation glyphs at the start of the glyph row. */
27747 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27748 for (; glyph > end
27749 && INTEGERP (glyph->object)
27750 && glyph->charpos < 0;
27751 --glyph)
27752 ;
27753
27754 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27755 or DISP_STRING, and the first glyph from buffer whose
27756 position is between START_CHARPOS and END_CHARPOS. */
27757 for (; glyph > end
27758 && !INTEGERP (glyph->object)
27759 && !EQ (glyph->object, disp_string)
27760 && !(BUFFERP (glyph->object)
27761 && (glyph->charpos >= start_charpos
27762 && glyph->charpos < end_charpos));
27763 --glyph)
27764 {
27765 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27766 are present at buffer positions between START_CHARPOS and
27767 END_CHARPOS, or if they come from an overlay. */
27768 if (EQ (glyph->object, before_string))
27769 {
27770 pos = string_buffer_position (before_string, start_charpos);
27771 /* If pos == 0, it means before_string came from an
27772 overlay, not from a buffer position. */
27773 if (!pos || (pos >= start_charpos && pos < end_charpos))
27774 break;
27775 }
27776 else if (EQ (glyph->object, after_string))
27777 {
27778 pos = string_buffer_position (after_string, end_charpos);
27779 if (!pos || (pos >= start_charpos && pos < end_charpos))
27780 break;
27781 }
27782 }
27783
27784 glyph++; /* first glyph to the right of the highlighted area */
27785 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27786 x += g->pixel_width;
27787 hlinfo->mouse_face_beg_x = x;
27788 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27789 }
27790
27791 /* If the highlight ends in a different row, compute GLYPH and END
27792 for the end row. Otherwise, reuse the values computed above for
27793 the row where the highlight begins. */
27794 if (r2 != r1)
27795 {
27796 if (!r2->reversed_p)
27797 {
27798 glyph = r2->glyphs[TEXT_AREA];
27799 end = glyph + r2->used[TEXT_AREA];
27800 x = r2->x;
27801 }
27802 else
27803 {
27804 end = r2->glyphs[TEXT_AREA] - 1;
27805 glyph = end + r2->used[TEXT_AREA];
27806 }
27807 }
27808
27809 if (!r2->reversed_p)
27810 {
27811 /* Skip truncation and continuation glyphs near the end of the
27812 row, and also blanks and stretch glyphs inserted by
27813 extend_face_to_end_of_line. */
27814 while (end > glyph
27815 && INTEGERP ((end - 1)->object))
27816 --end;
27817 /* Scan the rest of the glyph row from the end, looking for the
27818 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27819 DISP_STRING, or whose position is between START_CHARPOS
27820 and END_CHARPOS */
27821 for (--end;
27822 end > glyph
27823 && !INTEGERP (end->object)
27824 && !EQ (end->object, disp_string)
27825 && !(BUFFERP (end->object)
27826 && (end->charpos >= start_charpos
27827 && end->charpos < end_charpos));
27828 --end)
27829 {
27830 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27831 are present at buffer positions between START_CHARPOS and
27832 END_CHARPOS, or if they come from an overlay. */
27833 if (EQ (end->object, before_string))
27834 {
27835 pos = string_buffer_position (before_string, start_charpos);
27836 if (!pos || (pos >= start_charpos && pos < end_charpos))
27837 break;
27838 }
27839 else if (EQ (end->object, after_string))
27840 {
27841 pos = string_buffer_position (after_string, end_charpos);
27842 if (!pos || (pos >= start_charpos && pos < end_charpos))
27843 break;
27844 }
27845 }
27846 /* Find the X coordinate of the last glyph to be highlighted. */
27847 for (; glyph <= end; ++glyph)
27848 x += glyph->pixel_width;
27849
27850 hlinfo->mouse_face_end_x = x;
27851 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27852 }
27853 else
27854 {
27855 /* Skip truncation and continuation glyphs near the end of the
27856 row, and also blanks and stretch glyphs inserted by
27857 extend_face_to_end_of_line. */
27858 x = r2->x;
27859 end++;
27860 while (end < glyph
27861 && INTEGERP (end->object))
27862 {
27863 x += end->pixel_width;
27864 ++end;
27865 }
27866 /* Scan the rest of the glyph row from the end, looking for the
27867 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27868 DISP_STRING, or whose position is between START_CHARPOS
27869 and END_CHARPOS */
27870 for ( ;
27871 end < glyph
27872 && !INTEGERP (end->object)
27873 && !EQ (end->object, disp_string)
27874 && !(BUFFERP (end->object)
27875 && (end->charpos >= start_charpos
27876 && end->charpos < end_charpos));
27877 ++end)
27878 {
27879 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27880 are present at buffer positions between START_CHARPOS and
27881 END_CHARPOS, or if they come from an overlay. */
27882 if (EQ (end->object, before_string))
27883 {
27884 pos = string_buffer_position (before_string, start_charpos);
27885 if (!pos || (pos >= start_charpos && pos < end_charpos))
27886 break;
27887 }
27888 else if (EQ (end->object, after_string))
27889 {
27890 pos = string_buffer_position (after_string, end_charpos);
27891 if (!pos || (pos >= start_charpos && pos < end_charpos))
27892 break;
27893 }
27894 x += end->pixel_width;
27895 }
27896 /* If we exited the above loop because we arrived at the last
27897 glyph of the row, and its buffer position is still not in
27898 range, it means the last character in range is the preceding
27899 newline. Bump the end column and x values to get past the
27900 last glyph. */
27901 if (end == glyph
27902 && BUFFERP (end->object)
27903 && (end->charpos < start_charpos
27904 || end->charpos >= end_charpos))
27905 {
27906 x += end->pixel_width;
27907 ++end;
27908 }
27909 hlinfo->mouse_face_end_x = x;
27910 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27911 }
27912
27913 hlinfo->mouse_face_window = window;
27914 hlinfo->mouse_face_face_id
27915 = face_at_buffer_position (w, mouse_charpos, &ignore,
27916 mouse_charpos + 1,
27917 !hlinfo->mouse_face_hidden, -1);
27918 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27919 }
27920
27921 /* The following function is not used anymore (replaced with
27922 mouse_face_from_string_pos), but I leave it here for the time
27923 being, in case someone would. */
27924
27925 #if 0 /* not used */
27926
27927 /* Find the position of the glyph for position POS in OBJECT in
27928 window W's current matrix, and return in *X, *Y the pixel
27929 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27930
27931 RIGHT_P non-zero means return the position of the right edge of the
27932 glyph, RIGHT_P zero means return the left edge position.
27933
27934 If no glyph for POS exists in the matrix, return the position of
27935 the glyph with the next smaller position that is in the matrix, if
27936 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27937 exists in the matrix, return the position of the glyph with the
27938 next larger position in OBJECT.
27939
27940 Value is non-zero if a glyph was found. */
27941
27942 static int
27943 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27944 int *hpos, int *vpos, int *x, int *y, int right_p)
27945 {
27946 int yb = window_text_bottom_y (w);
27947 struct glyph_row *r;
27948 struct glyph *best_glyph = NULL;
27949 struct glyph_row *best_row = NULL;
27950 int best_x = 0;
27951
27952 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27953 r->enabled_p && r->y < yb;
27954 ++r)
27955 {
27956 struct glyph *g = r->glyphs[TEXT_AREA];
27957 struct glyph *e = g + r->used[TEXT_AREA];
27958 int gx;
27959
27960 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27961 if (EQ (g->object, object))
27962 {
27963 if (g->charpos == pos)
27964 {
27965 best_glyph = g;
27966 best_x = gx;
27967 best_row = r;
27968 goto found;
27969 }
27970 else if (best_glyph == NULL
27971 || ((eabs (g->charpos - pos)
27972 < eabs (best_glyph->charpos - pos))
27973 && (right_p
27974 ? g->charpos < pos
27975 : g->charpos > pos)))
27976 {
27977 best_glyph = g;
27978 best_x = gx;
27979 best_row = r;
27980 }
27981 }
27982 }
27983
27984 found:
27985
27986 if (best_glyph)
27987 {
27988 *x = best_x;
27989 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27990
27991 if (right_p)
27992 {
27993 *x += best_glyph->pixel_width;
27994 ++*hpos;
27995 }
27996
27997 *y = best_row->y;
27998 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27999 }
28000
28001 return best_glyph != NULL;
28002 }
28003 #endif /* not used */
28004
28005 /* Find the positions of the first and the last glyphs in window W's
28006 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28007 (assumed to be a string), and return in HLINFO's mouse_face_*
28008 members the pixel and column/row coordinates of those glyphs. */
28009
28010 static void
28011 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28012 Lisp_Object object,
28013 ptrdiff_t startpos, ptrdiff_t endpos)
28014 {
28015 int yb = window_text_bottom_y (w);
28016 struct glyph_row *r;
28017 struct glyph *g, *e;
28018 int gx;
28019 int found = 0;
28020
28021 /* Find the glyph row with at least one position in the range
28022 [STARTPOS..ENDPOS), and the first glyph in that row whose
28023 position belongs to that range. */
28024 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28025 r->enabled_p && r->y < yb;
28026 ++r)
28027 {
28028 if (!r->reversed_p)
28029 {
28030 g = r->glyphs[TEXT_AREA];
28031 e = g + r->used[TEXT_AREA];
28032 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28033 if (EQ (g->object, object)
28034 && startpos <= g->charpos && g->charpos < endpos)
28035 {
28036 hlinfo->mouse_face_beg_row
28037 = MATRIX_ROW_VPOS (r, w->current_matrix);
28038 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28039 hlinfo->mouse_face_beg_x = gx;
28040 found = 1;
28041 break;
28042 }
28043 }
28044 else
28045 {
28046 struct glyph *g1;
28047
28048 e = r->glyphs[TEXT_AREA];
28049 g = e + r->used[TEXT_AREA];
28050 for ( ; g > e; --g)
28051 if (EQ ((g-1)->object, object)
28052 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28053 {
28054 hlinfo->mouse_face_beg_row
28055 = MATRIX_ROW_VPOS (r, w->current_matrix);
28056 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28057 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28058 gx += g1->pixel_width;
28059 hlinfo->mouse_face_beg_x = gx;
28060 found = 1;
28061 break;
28062 }
28063 }
28064 if (found)
28065 break;
28066 }
28067
28068 if (!found)
28069 return;
28070
28071 /* Starting with the next row, look for the first row which does NOT
28072 include any glyphs whose positions are in the range. */
28073 for (++r; r->enabled_p && r->y < yb; ++r)
28074 {
28075 g = r->glyphs[TEXT_AREA];
28076 e = g + r->used[TEXT_AREA];
28077 found = 0;
28078 for ( ; g < e; ++g)
28079 if (EQ (g->object, object)
28080 && startpos <= g->charpos && g->charpos < endpos)
28081 {
28082 found = 1;
28083 break;
28084 }
28085 if (!found)
28086 break;
28087 }
28088
28089 /* The highlighted region ends on the previous row. */
28090 r--;
28091
28092 /* Set the end row. */
28093 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28094
28095 /* Compute and set the end column and the end column's horizontal
28096 pixel coordinate. */
28097 if (!r->reversed_p)
28098 {
28099 g = r->glyphs[TEXT_AREA];
28100 e = g + r->used[TEXT_AREA];
28101 for ( ; e > g; --e)
28102 if (EQ ((e-1)->object, object)
28103 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28104 break;
28105 hlinfo->mouse_face_end_col = e - g;
28106
28107 for (gx = r->x; g < e; ++g)
28108 gx += g->pixel_width;
28109 hlinfo->mouse_face_end_x = gx;
28110 }
28111 else
28112 {
28113 e = r->glyphs[TEXT_AREA];
28114 g = e + r->used[TEXT_AREA];
28115 for (gx = r->x ; e < g; ++e)
28116 {
28117 if (EQ (e->object, object)
28118 && startpos <= e->charpos && e->charpos < endpos)
28119 break;
28120 gx += e->pixel_width;
28121 }
28122 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28123 hlinfo->mouse_face_end_x = gx;
28124 }
28125 }
28126
28127 #ifdef HAVE_WINDOW_SYSTEM
28128
28129 /* See if position X, Y is within a hot-spot of an image. */
28130
28131 static int
28132 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28133 {
28134 if (!CONSP (hot_spot))
28135 return 0;
28136
28137 if (EQ (XCAR (hot_spot), Qrect))
28138 {
28139 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28140 Lisp_Object rect = XCDR (hot_spot);
28141 Lisp_Object tem;
28142 if (!CONSP (rect))
28143 return 0;
28144 if (!CONSP (XCAR (rect)))
28145 return 0;
28146 if (!CONSP (XCDR (rect)))
28147 return 0;
28148 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28149 return 0;
28150 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28151 return 0;
28152 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28153 return 0;
28154 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28155 return 0;
28156 return 1;
28157 }
28158 else if (EQ (XCAR (hot_spot), Qcircle))
28159 {
28160 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28161 Lisp_Object circ = XCDR (hot_spot);
28162 Lisp_Object lr, lx0, ly0;
28163 if (CONSP (circ)
28164 && CONSP (XCAR (circ))
28165 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28166 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28167 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28168 {
28169 double r = XFLOATINT (lr);
28170 double dx = XINT (lx0) - x;
28171 double dy = XINT (ly0) - y;
28172 return (dx * dx + dy * dy <= r * r);
28173 }
28174 }
28175 else if (EQ (XCAR (hot_spot), Qpoly))
28176 {
28177 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28178 if (VECTORP (XCDR (hot_spot)))
28179 {
28180 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28181 Lisp_Object *poly = v->contents;
28182 ptrdiff_t n = v->header.size;
28183 ptrdiff_t i;
28184 int inside = 0;
28185 Lisp_Object lx, ly;
28186 int x0, y0;
28187
28188 /* Need an even number of coordinates, and at least 3 edges. */
28189 if (n < 6 || n & 1)
28190 return 0;
28191
28192 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28193 If count is odd, we are inside polygon. Pixels on edges
28194 may or may not be included depending on actual geometry of the
28195 polygon. */
28196 if ((lx = poly[n-2], !INTEGERP (lx))
28197 || (ly = poly[n-1], !INTEGERP (lx)))
28198 return 0;
28199 x0 = XINT (lx), y0 = XINT (ly);
28200 for (i = 0; i < n; i += 2)
28201 {
28202 int x1 = x0, y1 = y0;
28203 if ((lx = poly[i], !INTEGERP (lx))
28204 || (ly = poly[i+1], !INTEGERP (ly)))
28205 return 0;
28206 x0 = XINT (lx), y0 = XINT (ly);
28207
28208 /* Does this segment cross the X line? */
28209 if (x0 >= x)
28210 {
28211 if (x1 >= x)
28212 continue;
28213 }
28214 else if (x1 < x)
28215 continue;
28216 if (y > y0 && y > y1)
28217 continue;
28218 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28219 inside = !inside;
28220 }
28221 return inside;
28222 }
28223 }
28224 return 0;
28225 }
28226
28227 Lisp_Object
28228 find_hot_spot (Lisp_Object map, int x, int y)
28229 {
28230 while (CONSP (map))
28231 {
28232 if (CONSP (XCAR (map))
28233 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28234 return XCAR (map);
28235 map = XCDR (map);
28236 }
28237
28238 return Qnil;
28239 }
28240
28241 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28242 3, 3, 0,
28243 doc: /* Lookup in image map MAP coordinates X and Y.
28244 An image map is an alist where each element has the format (AREA ID PLIST).
28245 An AREA is specified as either a rectangle, a circle, or a polygon:
28246 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28247 pixel coordinates of the upper left and bottom right corners.
28248 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28249 and the radius of the circle; r may be a float or integer.
28250 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28251 vector describes one corner in the polygon.
28252 Returns the alist element for the first matching AREA in MAP. */)
28253 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28254 {
28255 if (NILP (map))
28256 return Qnil;
28257
28258 CHECK_NUMBER (x);
28259 CHECK_NUMBER (y);
28260
28261 return find_hot_spot (map,
28262 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28263 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28264 }
28265
28266
28267 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28268 static void
28269 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28270 {
28271 /* Do not change cursor shape while dragging mouse. */
28272 if (!NILP (do_mouse_tracking))
28273 return;
28274
28275 if (!NILP (pointer))
28276 {
28277 if (EQ (pointer, Qarrow))
28278 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28279 else if (EQ (pointer, Qhand))
28280 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28281 else if (EQ (pointer, Qtext))
28282 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28283 else if (EQ (pointer, intern ("hdrag")))
28284 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28285 else if (EQ (pointer, intern ("nhdrag")))
28286 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28287 #ifdef HAVE_X_WINDOWS
28288 else if (EQ (pointer, intern ("vdrag")))
28289 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28290 #endif
28291 else if (EQ (pointer, intern ("hourglass")))
28292 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28293 else if (EQ (pointer, Qmodeline))
28294 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28295 else
28296 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28297 }
28298
28299 if (cursor != No_Cursor)
28300 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28301 }
28302
28303 #endif /* HAVE_WINDOW_SYSTEM */
28304
28305 /* Take proper action when mouse has moved to the mode or header line
28306 or marginal area AREA of window W, x-position X and y-position Y.
28307 X is relative to the start of the text display area of W, so the
28308 width of bitmap areas and scroll bars must be subtracted to get a
28309 position relative to the start of the mode line. */
28310
28311 static void
28312 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28313 enum window_part area)
28314 {
28315 struct window *w = XWINDOW (window);
28316 struct frame *f = XFRAME (w->frame);
28317 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28318 #ifdef HAVE_WINDOW_SYSTEM
28319 Display_Info *dpyinfo;
28320 #endif
28321 Cursor cursor = No_Cursor;
28322 Lisp_Object pointer = Qnil;
28323 int dx, dy, width, height;
28324 ptrdiff_t charpos;
28325 Lisp_Object string, object = Qnil;
28326 Lisp_Object pos IF_LINT (= Qnil), help;
28327
28328 Lisp_Object mouse_face;
28329 int original_x_pixel = x;
28330 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28331 struct glyph_row *row IF_LINT (= 0);
28332
28333 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28334 {
28335 int x0;
28336 struct glyph *end;
28337
28338 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28339 returns them in row/column units! */
28340 string = mode_line_string (w, area, &x, &y, &charpos,
28341 &object, &dx, &dy, &width, &height);
28342
28343 row = (area == ON_MODE_LINE
28344 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28345 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28346
28347 /* Find the glyph under the mouse pointer. */
28348 if (row->mode_line_p && row->enabled_p)
28349 {
28350 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28351 end = glyph + row->used[TEXT_AREA];
28352
28353 for (x0 = original_x_pixel;
28354 glyph < end && x0 >= glyph->pixel_width;
28355 ++glyph)
28356 x0 -= glyph->pixel_width;
28357
28358 if (glyph >= end)
28359 glyph = NULL;
28360 }
28361 }
28362 else
28363 {
28364 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28365 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28366 returns them in row/column units! */
28367 string = marginal_area_string (w, area, &x, &y, &charpos,
28368 &object, &dx, &dy, &width, &height);
28369 }
28370
28371 help = Qnil;
28372
28373 #ifdef HAVE_WINDOW_SYSTEM
28374 if (IMAGEP (object))
28375 {
28376 Lisp_Object image_map, hotspot;
28377 if ((image_map = Fplist_get (XCDR (object), QCmap),
28378 !NILP (image_map))
28379 && (hotspot = find_hot_spot (image_map, dx, dy),
28380 CONSP (hotspot))
28381 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28382 {
28383 Lisp_Object plist;
28384
28385 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28386 If so, we could look for mouse-enter, mouse-leave
28387 properties in PLIST (and do something...). */
28388 hotspot = XCDR (hotspot);
28389 if (CONSP (hotspot)
28390 && (plist = XCAR (hotspot), CONSP (plist)))
28391 {
28392 pointer = Fplist_get (plist, Qpointer);
28393 if (NILP (pointer))
28394 pointer = Qhand;
28395 help = Fplist_get (plist, Qhelp_echo);
28396 if (!NILP (help))
28397 {
28398 help_echo_string = help;
28399 XSETWINDOW (help_echo_window, w);
28400 help_echo_object = w->contents;
28401 help_echo_pos = charpos;
28402 }
28403 }
28404 }
28405 if (NILP (pointer))
28406 pointer = Fplist_get (XCDR (object), QCpointer);
28407 }
28408 #endif /* HAVE_WINDOW_SYSTEM */
28409
28410 if (STRINGP (string))
28411 pos = make_number (charpos);
28412
28413 /* Set the help text and mouse pointer. If the mouse is on a part
28414 of the mode line without any text (e.g. past the right edge of
28415 the mode line text), use the default help text and pointer. */
28416 if (STRINGP (string) || area == ON_MODE_LINE)
28417 {
28418 /* Arrange to display the help by setting the global variables
28419 help_echo_string, help_echo_object, and help_echo_pos. */
28420 if (NILP (help))
28421 {
28422 if (STRINGP (string))
28423 help = Fget_text_property (pos, Qhelp_echo, string);
28424
28425 if (!NILP (help))
28426 {
28427 help_echo_string = help;
28428 XSETWINDOW (help_echo_window, w);
28429 help_echo_object = string;
28430 help_echo_pos = charpos;
28431 }
28432 else if (area == ON_MODE_LINE)
28433 {
28434 Lisp_Object default_help
28435 = buffer_local_value_1 (Qmode_line_default_help_echo,
28436 w->contents);
28437
28438 if (STRINGP (default_help))
28439 {
28440 help_echo_string = default_help;
28441 XSETWINDOW (help_echo_window, w);
28442 help_echo_object = Qnil;
28443 help_echo_pos = -1;
28444 }
28445 }
28446 }
28447
28448 #ifdef HAVE_WINDOW_SYSTEM
28449 /* Change the mouse pointer according to what is under it. */
28450 if (FRAME_WINDOW_P (f))
28451 {
28452 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28453 || minibuf_level
28454 || NILP (Vresize_mini_windows));
28455
28456 dpyinfo = FRAME_DISPLAY_INFO (f);
28457 if (STRINGP (string))
28458 {
28459 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28460
28461 if (NILP (pointer))
28462 pointer = Fget_text_property (pos, Qpointer, string);
28463
28464 /* Change the mouse pointer according to what is under X/Y. */
28465 if (NILP (pointer)
28466 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28467 {
28468 Lisp_Object map;
28469 map = Fget_text_property (pos, Qlocal_map, string);
28470 if (!KEYMAPP (map))
28471 map = Fget_text_property (pos, Qkeymap, string);
28472 if (!KEYMAPP (map) && draggable)
28473 cursor = dpyinfo->vertical_scroll_bar_cursor;
28474 }
28475 }
28476 else if (draggable)
28477 /* Default mode-line pointer. */
28478 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28479 }
28480 #endif
28481 }
28482
28483 /* Change the mouse face according to what is under X/Y. */
28484 if (STRINGP (string))
28485 {
28486 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28487 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28488 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28489 && glyph)
28490 {
28491 Lisp_Object b, e;
28492
28493 struct glyph * tmp_glyph;
28494
28495 int gpos;
28496 int gseq_length;
28497 int total_pixel_width;
28498 ptrdiff_t begpos, endpos, ignore;
28499
28500 int vpos, hpos;
28501
28502 b = Fprevious_single_property_change (make_number (charpos + 1),
28503 Qmouse_face, string, Qnil);
28504 if (NILP (b))
28505 begpos = 0;
28506 else
28507 begpos = XINT (b);
28508
28509 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28510 if (NILP (e))
28511 endpos = SCHARS (string);
28512 else
28513 endpos = XINT (e);
28514
28515 /* Calculate the glyph position GPOS of GLYPH in the
28516 displayed string, relative to the beginning of the
28517 highlighted part of the string.
28518
28519 Note: GPOS is different from CHARPOS. CHARPOS is the
28520 position of GLYPH in the internal string object. A mode
28521 line string format has structures which are converted to
28522 a flattened string by the Emacs Lisp interpreter. The
28523 internal string is an element of those structures. The
28524 displayed string is the flattened string. */
28525 tmp_glyph = row_start_glyph;
28526 while (tmp_glyph < glyph
28527 && (!(EQ (tmp_glyph->object, glyph->object)
28528 && begpos <= tmp_glyph->charpos
28529 && tmp_glyph->charpos < endpos)))
28530 tmp_glyph++;
28531 gpos = glyph - tmp_glyph;
28532
28533 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28534 the highlighted part of the displayed string to which
28535 GLYPH belongs. Note: GSEQ_LENGTH is different from
28536 SCHARS (STRING), because the latter returns the length of
28537 the internal string. */
28538 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28539 tmp_glyph > glyph
28540 && (!(EQ (tmp_glyph->object, glyph->object)
28541 && begpos <= tmp_glyph->charpos
28542 && tmp_glyph->charpos < endpos));
28543 tmp_glyph--)
28544 ;
28545 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28546
28547 /* Calculate the total pixel width of all the glyphs between
28548 the beginning of the highlighted area and GLYPH. */
28549 total_pixel_width = 0;
28550 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28551 total_pixel_width += tmp_glyph->pixel_width;
28552
28553 /* Pre calculation of re-rendering position. Note: X is in
28554 column units here, after the call to mode_line_string or
28555 marginal_area_string. */
28556 hpos = x - gpos;
28557 vpos = (area == ON_MODE_LINE
28558 ? (w->current_matrix)->nrows - 1
28559 : 0);
28560
28561 /* If GLYPH's position is included in the region that is
28562 already drawn in mouse face, we have nothing to do. */
28563 if ( EQ (window, hlinfo->mouse_face_window)
28564 && (!row->reversed_p
28565 ? (hlinfo->mouse_face_beg_col <= hpos
28566 && hpos < hlinfo->mouse_face_end_col)
28567 /* In R2L rows we swap BEG and END, see below. */
28568 : (hlinfo->mouse_face_end_col <= hpos
28569 && hpos < hlinfo->mouse_face_beg_col))
28570 && hlinfo->mouse_face_beg_row == vpos )
28571 return;
28572
28573 if (clear_mouse_face (hlinfo))
28574 cursor = No_Cursor;
28575
28576 if (!row->reversed_p)
28577 {
28578 hlinfo->mouse_face_beg_col = hpos;
28579 hlinfo->mouse_face_beg_x = original_x_pixel
28580 - (total_pixel_width + dx);
28581 hlinfo->mouse_face_end_col = hpos + gseq_length;
28582 hlinfo->mouse_face_end_x = 0;
28583 }
28584 else
28585 {
28586 /* In R2L rows, show_mouse_face expects BEG and END
28587 coordinates to be swapped. */
28588 hlinfo->mouse_face_end_col = hpos;
28589 hlinfo->mouse_face_end_x = original_x_pixel
28590 - (total_pixel_width + dx);
28591 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28592 hlinfo->mouse_face_beg_x = 0;
28593 }
28594
28595 hlinfo->mouse_face_beg_row = vpos;
28596 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28597 hlinfo->mouse_face_past_end = 0;
28598 hlinfo->mouse_face_window = window;
28599
28600 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28601 charpos,
28602 0, &ignore,
28603 glyph->face_id,
28604 1);
28605 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28606
28607 if (NILP (pointer))
28608 pointer = Qhand;
28609 }
28610 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28611 clear_mouse_face (hlinfo);
28612 }
28613 #ifdef HAVE_WINDOW_SYSTEM
28614 if (FRAME_WINDOW_P (f))
28615 define_frame_cursor1 (f, cursor, pointer);
28616 #endif
28617 }
28618
28619
28620 /* EXPORT:
28621 Take proper action when the mouse has moved to position X, Y on
28622 frame F with regards to highlighting portions of display that have
28623 mouse-face properties. Also de-highlight portions of display where
28624 the mouse was before, set the mouse pointer shape as appropriate
28625 for the mouse coordinates, and activate help echo (tooltips).
28626 X and Y can be negative or out of range. */
28627
28628 void
28629 note_mouse_highlight (struct frame *f, int x, int y)
28630 {
28631 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28632 enum window_part part = ON_NOTHING;
28633 Lisp_Object window;
28634 struct window *w;
28635 Cursor cursor = No_Cursor;
28636 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28637 struct buffer *b;
28638
28639 /* When a menu is active, don't highlight because this looks odd. */
28640 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28641 if (popup_activated ())
28642 return;
28643 #endif
28644
28645 if (!f->glyphs_initialized_p
28646 || f->pointer_invisible)
28647 return;
28648
28649 hlinfo->mouse_face_mouse_x = x;
28650 hlinfo->mouse_face_mouse_y = y;
28651 hlinfo->mouse_face_mouse_frame = f;
28652
28653 if (hlinfo->mouse_face_defer)
28654 return;
28655
28656 /* Which window is that in? */
28657 window = window_from_coordinates (f, x, y, &part, 1);
28658
28659 /* If displaying active text in another window, clear that. */
28660 if (! EQ (window, hlinfo->mouse_face_window)
28661 /* Also clear if we move out of text area in same window. */
28662 || (!NILP (hlinfo->mouse_face_window)
28663 && !NILP (window)
28664 && part != ON_TEXT
28665 && part != ON_MODE_LINE
28666 && part != ON_HEADER_LINE))
28667 clear_mouse_face (hlinfo);
28668
28669 /* Not on a window -> return. */
28670 if (!WINDOWP (window))
28671 return;
28672
28673 /* Reset help_echo_string. It will get recomputed below. */
28674 help_echo_string = Qnil;
28675
28676 /* Convert to window-relative pixel coordinates. */
28677 w = XWINDOW (window);
28678 frame_to_window_pixel_xy (w, &x, &y);
28679
28680 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28681 /* Handle tool-bar window differently since it doesn't display a
28682 buffer. */
28683 if (EQ (window, f->tool_bar_window))
28684 {
28685 note_tool_bar_highlight (f, x, y);
28686 return;
28687 }
28688 #endif
28689
28690 /* Mouse is on the mode, header line or margin? */
28691 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28692 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28693 {
28694 note_mode_line_or_margin_highlight (window, x, y, part);
28695
28696 #ifdef HAVE_WINDOW_SYSTEM
28697 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28698 {
28699 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28700 /* Show non-text cursor (Bug#16647). */
28701 goto set_cursor;
28702 }
28703 else
28704 #endif
28705 return;
28706 }
28707
28708 #ifdef HAVE_WINDOW_SYSTEM
28709 if (part == ON_VERTICAL_BORDER)
28710 {
28711 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28712 help_echo_string = build_string ("drag-mouse-1: resize");
28713 }
28714 else if (part == ON_RIGHT_DIVIDER)
28715 {
28716 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28717 help_echo_string = build_string ("drag-mouse-1: resize");
28718 }
28719 else if (part == ON_BOTTOM_DIVIDER)
28720 if (! WINDOW_BOTTOMMOST_P (w)
28721 || minibuf_level
28722 || NILP (Vresize_mini_windows))
28723 {
28724 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28725 help_echo_string = build_string ("drag-mouse-1: resize");
28726 }
28727 else
28728 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28729 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28730 || part == ON_SCROLL_BAR)
28731 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28732 else
28733 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28734 #endif
28735
28736 /* Are we in a window whose display is up to date?
28737 And verify the buffer's text has not changed. */
28738 b = XBUFFER (w->contents);
28739 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28740 {
28741 int hpos, vpos, dx, dy, area = LAST_AREA;
28742 ptrdiff_t pos;
28743 struct glyph *glyph;
28744 Lisp_Object object;
28745 Lisp_Object mouse_face = Qnil, position;
28746 Lisp_Object *overlay_vec = NULL;
28747 ptrdiff_t i, noverlays;
28748 struct buffer *obuf;
28749 ptrdiff_t obegv, ozv;
28750 int same_region;
28751
28752 /* Find the glyph under X/Y. */
28753 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28754
28755 #ifdef HAVE_WINDOW_SYSTEM
28756 /* Look for :pointer property on image. */
28757 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28758 {
28759 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28760 if (img != NULL && IMAGEP (img->spec))
28761 {
28762 Lisp_Object image_map, hotspot;
28763 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28764 !NILP (image_map))
28765 && (hotspot = find_hot_spot (image_map,
28766 glyph->slice.img.x + dx,
28767 glyph->slice.img.y + dy),
28768 CONSP (hotspot))
28769 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28770 {
28771 Lisp_Object plist;
28772
28773 /* Could check XCAR (hotspot) to see if we enter/leave
28774 this hot-spot.
28775 If so, we could look for mouse-enter, mouse-leave
28776 properties in PLIST (and do something...). */
28777 hotspot = XCDR (hotspot);
28778 if (CONSP (hotspot)
28779 && (plist = XCAR (hotspot), CONSP (plist)))
28780 {
28781 pointer = Fplist_get (plist, Qpointer);
28782 if (NILP (pointer))
28783 pointer = Qhand;
28784 help_echo_string = Fplist_get (plist, Qhelp_echo);
28785 if (!NILP (help_echo_string))
28786 {
28787 help_echo_window = window;
28788 help_echo_object = glyph->object;
28789 help_echo_pos = glyph->charpos;
28790 }
28791 }
28792 }
28793 if (NILP (pointer))
28794 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28795 }
28796 }
28797 #endif /* HAVE_WINDOW_SYSTEM */
28798
28799 /* Clear mouse face if X/Y not over text. */
28800 if (glyph == NULL
28801 || area != TEXT_AREA
28802 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28803 /* Glyph's OBJECT is an integer for glyphs inserted by the
28804 display engine for its internal purposes, like truncation
28805 and continuation glyphs and blanks beyond the end of
28806 line's text on text terminals. If we are over such a
28807 glyph, we are not over any text. */
28808 || INTEGERP (glyph->object)
28809 /* R2L rows have a stretch glyph at their front, which
28810 stands for no text, whereas L2R rows have no glyphs at
28811 all beyond the end of text. Treat such stretch glyphs
28812 like we do with NULL glyphs in L2R rows. */
28813 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28814 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28815 && glyph->type == STRETCH_GLYPH
28816 && glyph->avoid_cursor_p))
28817 {
28818 if (clear_mouse_face (hlinfo))
28819 cursor = No_Cursor;
28820 #ifdef HAVE_WINDOW_SYSTEM
28821 if (FRAME_WINDOW_P (f) && NILP (pointer))
28822 {
28823 if (area != TEXT_AREA)
28824 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28825 else
28826 pointer = Vvoid_text_area_pointer;
28827 }
28828 #endif
28829 goto set_cursor;
28830 }
28831
28832 pos = glyph->charpos;
28833 object = glyph->object;
28834 if (!STRINGP (object) && !BUFFERP (object))
28835 goto set_cursor;
28836
28837 /* If we get an out-of-range value, return now; avoid an error. */
28838 if (BUFFERP (object) && pos > BUF_Z (b))
28839 goto set_cursor;
28840
28841 /* Make the window's buffer temporarily current for
28842 overlays_at and compute_char_face. */
28843 obuf = current_buffer;
28844 current_buffer = b;
28845 obegv = BEGV;
28846 ozv = ZV;
28847 BEGV = BEG;
28848 ZV = Z;
28849
28850 /* Is this char mouse-active or does it have help-echo? */
28851 position = make_number (pos);
28852
28853 if (BUFFERP (object))
28854 {
28855 /* Put all the overlays we want in a vector in overlay_vec. */
28856 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28857 /* Sort overlays into increasing priority order. */
28858 noverlays = sort_overlays (overlay_vec, noverlays, w);
28859 }
28860 else
28861 noverlays = 0;
28862
28863 if (NILP (Vmouse_highlight))
28864 {
28865 clear_mouse_face (hlinfo);
28866 goto check_help_echo;
28867 }
28868
28869 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28870
28871 if (same_region)
28872 cursor = No_Cursor;
28873
28874 /* Check mouse-face highlighting. */
28875 if (! same_region
28876 /* If there exists an overlay with mouse-face overlapping
28877 the one we are currently highlighting, we have to
28878 check if we enter the overlapping overlay, and then
28879 highlight only that. */
28880 || (OVERLAYP (hlinfo->mouse_face_overlay)
28881 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28882 {
28883 /* Find the highest priority overlay with a mouse-face. */
28884 Lisp_Object overlay = Qnil;
28885 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28886 {
28887 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28888 if (!NILP (mouse_face))
28889 overlay = overlay_vec[i];
28890 }
28891
28892 /* If we're highlighting the same overlay as before, there's
28893 no need to do that again. */
28894 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28895 goto check_help_echo;
28896 hlinfo->mouse_face_overlay = overlay;
28897
28898 /* Clear the display of the old active region, if any. */
28899 if (clear_mouse_face (hlinfo))
28900 cursor = No_Cursor;
28901
28902 /* If no overlay applies, get a text property. */
28903 if (NILP (overlay))
28904 mouse_face = Fget_text_property (position, Qmouse_face, object);
28905
28906 /* Next, compute the bounds of the mouse highlighting and
28907 display it. */
28908 if (!NILP (mouse_face) && STRINGP (object))
28909 {
28910 /* The mouse-highlighting comes from a display string
28911 with a mouse-face. */
28912 Lisp_Object s, e;
28913 ptrdiff_t ignore;
28914
28915 s = Fprevious_single_property_change
28916 (make_number (pos + 1), Qmouse_face, object, Qnil);
28917 e = Fnext_single_property_change
28918 (position, Qmouse_face, object, Qnil);
28919 if (NILP (s))
28920 s = make_number (0);
28921 if (NILP (e))
28922 e = make_number (SCHARS (object));
28923 mouse_face_from_string_pos (w, hlinfo, object,
28924 XINT (s), XINT (e));
28925 hlinfo->mouse_face_past_end = 0;
28926 hlinfo->mouse_face_window = window;
28927 hlinfo->mouse_face_face_id
28928 = face_at_string_position (w, object, pos, 0, &ignore,
28929 glyph->face_id, 1);
28930 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28931 cursor = No_Cursor;
28932 }
28933 else
28934 {
28935 /* The mouse-highlighting, if any, comes from an overlay
28936 or text property in the buffer. */
28937 Lisp_Object buffer IF_LINT (= Qnil);
28938 Lisp_Object disp_string IF_LINT (= Qnil);
28939
28940 if (STRINGP (object))
28941 {
28942 /* If we are on a display string with no mouse-face,
28943 check if the text under it has one. */
28944 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28945 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28946 pos = string_buffer_position (object, start);
28947 if (pos > 0)
28948 {
28949 mouse_face = get_char_property_and_overlay
28950 (make_number (pos), Qmouse_face, w->contents, &overlay);
28951 buffer = w->contents;
28952 disp_string = object;
28953 }
28954 }
28955 else
28956 {
28957 buffer = object;
28958 disp_string = Qnil;
28959 }
28960
28961 if (!NILP (mouse_face))
28962 {
28963 Lisp_Object before, after;
28964 Lisp_Object before_string, after_string;
28965 /* To correctly find the limits of mouse highlight
28966 in a bidi-reordered buffer, we must not use the
28967 optimization of limiting the search in
28968 previous-single-property-change and
28969 next-single-property-change, because
28970 rows_from_pos_range needs the real start and end
28971 positions to DTRT in this case. That's because
28972 the first row visible in a window does not
28973 necessarily display the character whose position
28974 is the smallest. */
28975 Lisp_Object lim1
28976 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28977 ? Fmarker_position (w->start)
28978 : Qnil;
28979 Lisp_Object lim2
28980 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28981 ? make_number (BUF_Z (XBUFFER (buffer))
28982 - w->window_end_pos)
28983 : Qnil;
28984
28985 if (NILP (overlay))
28986 {
28987 /* Handle the text property case. */
28988 before = Fprevious_single_property_change
28989 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28990 after = Fnext_single_property_change
28991 (make_number (pos), Qmouse_face, buffer, lim2);
28992 before_string = after_string = Qnil;
28993 }
28994 else
28995 {
28996 /* Handle the overlay case. */
28997 before = Foverlay_start (overlay);
28998 after = Foverlay_end (overlay);
28999 before_string = Foverlay_get (overlay, Qbefore_string);
29000 after_string = Foverlay_get (overlay, Qafter_string);
29001
29002 if (!STRINGP (before_string)) before_string = Qnil;
29003 if (!STRINGP (after_string)) after_string = Qnil;
29004 }
29005
29006 mouse_face_from_buffer_pos (window, hlinfo, pos,
29007 NILP (before)
29008 ? 1
29009 : XFASTINT (before),
29010 NILP (after)
29011 ? BUF_Z (XBUFFER (buffer))
29012 : XFASTINT (after),
29013 before_string, after_string,
29014 disp_string);
29015 cursor = No_Cursor;
29016 }
29017 }
29018 }
29019
29020 check_help_echo:
29021
29022 /* Look for a `help-echo' property. */
29023 if (NILP (help_echo_string)) {
29024 Lisp_Object help, overlay;
29025
29026 /* Check overlays first. */
29027 help = overlay = Qnil;
29028 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29029 {
29030 overlay = overlay_vec[i];
29031 help = Foverlay_get (overlay, Qhelp_echo);
29032 }
29033
29034 if (!NILP (help))
29035 {
29036 help_echo_string = help;
29037 help_echo_window = window;
29038 help_echo_object = overlay;
29039 help_echo_pos = pos;
29040 }
29041 else
29042 {
29043 Lisp_Object obj = glyph->object;
29044 ptrdiff_t charpos = glyph->charpos;
29045
29046 /* Try text properties. */
29047 if (STRINGP (obj)
29048 && charpos >= 0
29049 && charpos < SCHARS (obj))
29050 {
29051 help = Fget_text_property (make_number (charpos),
29052 Qhelp_echo, obj);
29053 if (NILP (help))
29054 {
29055 /* If the string itself doesn't specify a help-echo,
29056 see if the buffer text ``under'' it does. */
29057 struct glyph_row *r
29058 = MATRIX_ROW (w->current_matrix, vpos);
29059 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29060 ptrdiff_t p = string_buffer_position (obj, start);
29061 if (p > 0)
29062 {
29063 help = Fget_char_property (make_number (p),
29064 Qhelp_echo, w->contents);
29065 if (!NILP (help))
29066 {
29067 charpos = p;
29068 obj = w->contents;
29069 }
29070 }
29071 }
29072 }
29073 else if (BUFFERP (obj)
29074 && charpos >= BEGV
29075 && charpos < ZV)
29076 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29077 obj);
29078
29079 if (!NILP (help))
29080 {
29081 help_echo_string = help;
29082 help_echo_window = window;
29083 help_echo_object = obj;
29084 help_echo_pos = charpos;
29085 }
29086 }
29087 }
29088
29089 #ifdef HAVE_WINDOW_SYSTEM
29090 /* Look for a `pointer' property. */
29091 if (FRAME_WINDOW_P (f) && NILP (pointer))
29092 {
29093 /* Check overlays first. */
29094 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29095 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29096
29097 if (NILP (pointer))
29098 {
29099 Lisp_Object obj = glyph->object;
29100 ptrdiff_t charpos = glyph->charpos;
29101
29102 /* Try text properties. */
29103 if (STRINGP (obj)
29104 && charpos >= 0
29105 && charpos < SCHARS (obj))
29106 {
29107 pointer = Fget_text_property (make_number (charpos),
29108 Qpointer, obj);
29109 if (NILP (pointer))
29110 {
29111 /* If the string itself doesn't specify a pointer,
29112 see if the buffer text ``under'' it does. */
29113 struct glyph_row *r
29114 = MATRIX_ROW (w->current_matrix, vpos);
29115 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29116 ptrdiff_t p = string_buffer_position (obj, start);
29117 if (p > 0)
29118 pointer = Fget_char_property (make_number (p),
29119 Qpointer, w->contents);
29120 }
29121 }
29122 else if (BUFFERP (obj)
29123 && charpos >= BEGV
29124 && charpos < ZV)
29125 pointer = Fget_text_property (make_number (charpos),
29126 Qpointer, obj);
29127 }
29128 }
29129 #endif /* HAVE_WINDOW_SYSTEM */
29130
29131 BEGV = obegv;
29132 ZV = ozv;
29133 current_buffer = obuf;
29134 }
29135
29136 set_cursor:
29137
29138 #ifdef HAVE_WINDOW_SYSTEM
29139 if (FRAME_WINDOW_P (f))
29140 define_frame_cursor1 (f, cursor, pointer);
29141 #else
29142 /* This is here to prevent a compiler error, about "label at end of
29143 compound statement". */
29144 return;
29145 #endif
29146 }
29147
29148
29149 /* EXPORT for RIF:
29150 Clear any mouse-face on window W. This function is part of the
29151 redisplay interface, and is called from try_window_id and similar
29152 functions to ensure the mouse-highlight is off. */
29153
29154 void
29155 x_clear_window_mouse_face (struct window *w)
29156 {
29157 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29158 Lisp_Object window;
29159
29160 block_input ();
29161 XSETWINDOW (window, w);
29162 if (EQ (window, hlinfo->mouse_face_window))
29163 clear_mouse_face (hlinfo);
29164 unblock_input ();
29165 }
29166
29167
29168 /* EXPORT:
29169 Just discard the mouse face information for frame F, if any.
29170 This is used when the size of F is changed. */
29171
29172 void
29173 cancel_mouse_face (struct frame *f)
29174 {
29175 Lisp_Object window;
29176 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29177
29178 window = hlinfo->mouse_face_window;
29179 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29180 reset_mouse_highlight (hlinfo);
29181 }
29182
29183
29184 \f
29185 /***********************************************************************
29186 Exposure Events
29187 ***********************************************************************/
29188
29189 #ifdef HAVE_WINDOW_SYSTEM
29190
29191 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29192 which intersects rectangle R. R is in window-relative coordinates. */
29193
29194 static void
29195 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29196 enum glyph_row_area area)
29197 {
29198 struct glyph *first = row->glyphs[area];
29199 struct glyph *end = row->glyphs[area] + row->used[area];
29200 struct glyph *last;
29201 int first_x, start_x, x;
29202
29203 if (area == TEXT_AREA && row->fill_line_p)
29204 /* If row extends face to end of line write the whole line. */
29205 draw_glyphs (w, 0, row, area,
29206 0, row->used[area],
29207 DRAW_NORMAL_TEXT, 0);
29208 else
29209 {
29210 /* Set START_X to the window-relative start position for drawing glyphs of
29211 AREA. The first glyph of the text area can be partially visible.
29212 The first glyphs of other areas cannot. */
29213 start_x = window_box_left_offset (w, area);
29214 x = start_x;
29215 if (area == TEXT_AREA)
29216 x += row->x;
29217
29218 /* Find the first glyph that must be redrawn. */
29219 while (first < end
29220 && x + first->pixel_width < r->x)
29221 {
29222 x += first->pixel_width;
29223 ++first;
29224 }
29225
29226 /* Find the last one. */
29227 last = first;
29228 first_x = x;
29229 while (last < end
29230 && x < r->x + r->width)
29231 {
29232 x += last->pixel_width;
29233 ++last;
29234 }
29235
29236 /* Repaint. */
29237 if (last > first)
29238 draw_glyphs (w, first_x - start_x, row, area,
29239 first - row->glyphs[area], last - row->glyphs[area],
29240 DRAW_NORMAL_TEXT, 0);
29241 }
29242 }
29243
29244
29245 /* Redraw the parts of the glyph row ROW on window W intersecting
29246 rectangle R. R is in window-relative coordinates. Value is
29247 non-zero if mouse-face was overwritten. */
29248
29249 static int
29250 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29251 {
29252 eassert (row->enabled_p);
29253
29254 if (row->mode_line_p || w->pseudo_window_p)
29255 draw_glyphs (w, 0, row, TEXT_AREA,
29256 0, row->used[TEXT_AREA],
29257 DRAW_NORMAL_TEXT, 0);
29258 else
29259 {
29260 if (row->used[LEFT_MARGIN_AREA])
29261 expose_area (w, row, r, LEFT_MARGIN_AREA);
29262 if (row->used[TEXT_AREA])
29263 expose_area (w, row, r, TEXT_AREA);
29264 if (row->used[RIGHT_MARGIN_AREA])
29265 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29266 draw_row_fringe_bitmaps (w, row);
29267 }
29268
29269 return row->mouse_face_p;
29270 }
29271
29272
29273 /* Redraw those parts of glyphs rows during expose event handling that
29274 overlap other rows. Redrawing of an exposed line writes over parts
29275 of lines overlapping that exposed line; this function fixes that.
29276
29277 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29278 row in W's current matrix that is exposed and overlaps other rows.
29279 LAST_OVERLAPPING_ROW is the last such row. */
29280
29281 static void
29282 expose_overlaps (struct window *w,
29283 struct glyph_row *first_overlapping_row,
29284 struct glyph_row *last_overlapping_row,
29285 XRectangle *r)
29286 {
29287 struct glyph_row *row;
29288
29289 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29290 if (row->overlapping_p)
29291 {
29292 eassert (row->enabled_p && !row->mode_line_p);
29293
29294 row->clip = r;
29295 if (row->used[LEFT_MARGIN_AREA])
29296 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29297
29298 if (row->used[TEXT_AREA])
29299 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29300
29301 if (row->used[RIGHT_MARGIN_AREA])
29302 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29303 row->clip = NULL;
29304 }
29305 }
29306
29307
29308 /* Return non-zero if W's cursor intersects rectangle R. */
29309
29310 static int
29311 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29312 {
29313 XRectangle cr, result;
29314 struct glyph *cursor_glyph;
29315 struct glyph_row *row;
29316
29317 if (w->phys_cursor.vpos >= 0
29318 && w->phys_cursor.vpos < w->current_matrix->nrows
29319 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29320 row->enabled_p)
29321 && row->cursor_in_fringe_p)
29322 {
29323 /* Cursor is in the fringe. */
29324 cr.x = window_box_right_offset (w,
29325 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29326 ? RIGHT_MARGIN_AREA
29327 : TEXT_AREA));
29328 cr.y = row->y;
29329 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29330 cr.height = row->height;
29331 return x_intersect_rectangles (&cr, r, &result);
29332 }
29333
29334 cursor_glyph = get_phys_cursor_glyph (w);
29335 if (cursor_glyph)
29336 {
29337 /* r is relative to W's box, but w->phys_cursor.x is relative
29338 to left edge of W's TEXT area. Adjust it. */
29339 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29340 cr.y = w->phys_cursor.y;
29341 cr.width = cursor_glyph->pixel_width;
29342 cr.height = w->phys_cursor_height;
29343 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29344 I assume the effect is the same -- and this is portable. */
29345 return x_intersect_rectangles (&cr, r, &result);
29346 }
29347 /* If we don't understand the format, pretend we're not in the hot-spot. */
29348 return 0;
29349 }
29350
29351
29352 /* EXPORT:
29353 Draw a vertical window border to the right of window W if W doesn't
29354 have vertical scroll bars. */
29355
29356 void
29357 x_draw_vertical_border (struct window *w)
29358 {
29359 struct frame *f = XFRAME (WINDOW_FRAME (w));
29360
29361 /* We could do better, if we knew what type of scroll-bar the adjacent
29362 windows (on either side) have... But we don't :-(
29363 However, I think this works ok. ++KFS 2003-04-25 */
29364
29365 /* Redraw borders between horizontally adjacent windows. Don't
29366 do it for frames with vertical scroll bars because either the
29367 right scroll bar of a window, or the left scroll bar of its
29368 neighbor will suffice as a border. */
29369 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29370 return;
29371
29372 /* Note: It is necessary to redraw both the left and the right
29373 borders, for when only this single window W is being
29374 redisplayed. */
29375 if (!WINDOW_RIGHTMOST_P (w)
29376 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29377 {
29378 int x0, x1, y0, y1;
29379
29380 window_box_edges (w, &x0, &y0, &x1, &y1);
29381 y1 -= 1;
29382
29383 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29384 x1 -= 1;
29385
29386 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29387 }
29388
29389 if (!WINDOW_LEFTMOST_P (w)
29390 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29391 {
29392 int x0, x1, y0, y1;
29393
29394 window_box_edges (w, &x0, &y0, &x1, &y1);
29395 y1 -= 1;
29396
29397 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29398 x0 -= 1;
29399
29400 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29401 }
29402 }
29403
29404
29405 /* Draw window dividers for window W. */
29406
29407 void
29408 x_draw_right_divider (struct window *w)
29409 {
29410 struct frame *f = WINDOW_XFRAME (w);
29411
29412 if (w->mini || w->pseudo_window_p)
29413 return;
29414 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29415 {
29416 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29417 int x1 = WINDOW_RIGHT_EDGE_X (w);
29418 int y0 = WINDOW_TOP_EDGE_Y (w);
29419 /* The bottom divider prevails. */
29420 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29421
29422 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29423 }
29424 }
29425
29426 static void
29427 x_draw_bottom_divider (struct window *w)
29428 {
29429 struct frame *f = XFRAME (WINDOW_FRAME (w));
29430
29431 if (w->mini || w->pseudo_window_p)
29432 return;
29433 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29434 {
29435 int x0 = WINDOW_LEFT_EDGE_X (w);
29436 int x1 = WINDOW_RIGHT_EDGE_X (w);
29437 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29438 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29439
29440 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29441 }
29442 }
29443
29444 /* Redraw the part of window W intersection rectangle FR. Pixel
29445 coordinates in FR are frame-relative. Call this function with
29446 input blocked. Value is non-zero if the exposure overwrites
29447 mouse-face. */
29448
29449 static int
29450 expose_window (struct window *w, XRectangle *fr)
29451 {
29452 struct frame *f = XFRAME (w->frame);
29453 XRectangle wr, r;
29454 int mouse_face_overwritten_p = 0;
29455
29456 /* If window is not yet fully initialized, do nothing. This can
29457 happen when toolkit scroll bars are used and a window is split.
29458 Reconfiguring the scroll bar will generate an expose for a newly
29459 created window. */
29460 if (w->current_matrix == NULL)
29461 return 0;
29462
29463 /* When we're currently updating the window, display and current
29464 matrix usually don't agree. Arrange for a thorough display
29465 later. */
29466 if (w->must_be_updated_p)
29467 {
29468 SET_FRAME_GARBAGED (f);
29469 return 0;
29470 }
29471
29472 /* Frame-relative pixel rectangle of W. */
29473 wr.x = WINDOW_LEFT_EDGE_X (w);
29474 wr.y = WINDOW_TOP_EDGE_Y (w);
29475 wr.width = WINDOW_PIXEL_WIDTH (w);
29476 wr.height = WINDOW_PIXEL_HEIGHT (w);
29477
29478 if (x_intersect_rectangles (fr, &wr, &r))
29479 {
29480 int yb = window_text_bottom_y (w);
29481 struct glyph_row *row;
29482 int cursor_cleared_p, phys_cursor_on_p;
29483 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29484
29485 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29486 r.x, r.y, r.width, r.height));
29487
29488 /* Convert to window coordinates. */
29489 r.x -= WINDOW_LEFT_EDGE_X (w);
29490 r.y -= WINDOW_TOP_EDGE_Y (w);
29491
29492 /* Turn off the cursor. */
29493 if (!w->pseudo_window_p
29494 && phys_cursor_in_rect_p (w, &r))
29495 {
29496 x_clear_cursor (w);
29497 cursor_cleared_p = 1;
29498 }
29499 else
29500 cursor_cleared_p = 0;
29501
29502 /* If the row containing the cursor extends face to end of line,
29503 then expose_area might overwrite the cursor outside the
29504 rectangle and thus notice_overwritten_cursor might clear
29505 w->phys_cursor_on_p. We remember the original value and
29506 check later if it is changed. */
29507 phys_cursor_on_p = w->phys_cursor_on_p;
29508
29509 /* Update lines intersecting rectangle R. */
29510 first_overlapping_row = last_overlapping_row = NULL;
29511 for (row = w->current_matrix->rows;
29512 row->enabled_p;
29513 ++row)
29514 {
29515 int y0 = row->y;
29516 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29517
29518 if ((y0 >= r.y && y0 < r.y + r.height)
29519 || (y1 > r.y && y1 < r.y + r.height)
29520 || (r.y >= y0 && r.y < y1)
29521 || (r.y + r.height > y0 && r.y + r.height < y1))
29522 {
29523 /* A header line may be overlapping, but there is no need
29524 to fix overlapping areas for them. KFS 2005-02-12 */
29525 if (row->overlapping_p && !row->mode_line_p)
29526 {
29527 if (first_overlapping_row == NULL)
29528 first_overlapping_row = row;
29529 last_overlapping_row = row;
29530 }
29531
29532 row->clip = fr;
29533 if (expose_line (w, row, &r))
29534 mouse_face_overwritten_p = 1;
29535 row->clip = NULL;
29536 }
29537 else if (row->overlapping_p)
29538 {
29539 /* We must redraw a row overlapping the exposed area. */
29540 if (y0 < r.y
29541 ? y0 + row->phys_height > r.y
29542 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29543 {
29544 if (first_overlapping_row == NULL)
29545 first_overlapping_row = row;
29546 last_overlapping_row = row;
29547 }
29548 }
29549
29550 if (y1 >= yb)
29551 break;
29552 }
29553
29554 /* Display the mode line if there is one. */
29555 if (WINDOW_WANTS_MODELINE_P (w)
29556 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29557 row->enabled_p)
29558 && row->y < r.y + r.height)
29559 {
29560 if (expose_line (w, row, &r))
29561 mouse_face_overwritten_p = 1;
29562 }
29563
29564 if (!w->pseudo_window_p)
29565 {
29566 /* Fix the display of overlapping rows. */
29567 if (first_overlapping_row)
29568 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29569 fr);
29570
29571 /* Draw border between windows. */
29572 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29573 x_draw_right_divider (w);
29574 else
29575 x_draw_vertical_border (w);
29576
29577 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29578 x_draw_bottom_divider (w);
29579
29580 /* Turn the cursor on again. */
29581 if (cursor_cleared_p
29582 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29583 update_window_cursor (w, 1);
29584 }
29585 }
29586
29587 return mouse_face_overwritten_p;
29588 }
29589
29590
29591
29592 /* Redraw (parts) of all windows in the window tree rooted at W that
29593 intersect R. R contains frame pixel coordinates. Value is
29594 non-zero if the exposure overwrites mouse-face. */
29595
29596 static int
29597 expose_window_tree (struct window *w, XRectangle *r)
29598 {
29599 struct frame *f = XFRAME (w->frame);
29600 int mouse_face_overwritten_p = 0;
29601
29602 while (w && !FRAME_GARBAGED_P (f))
29603 {
29604 if (WINDOWP (w->contents))
29605 mouse_face_overwritten_p
29606 |= expose_window_tree (XWINDOW (w->contents), r);
29607 else
29608 mouse_face_overwritten_p |= expose_window (w, r);
29609
29610 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29611 }
29612
29613 return mouse_face_overwritten_p;
29614 }
29615
29616
29617 /* EXPORT:
29618 Redisplay an exposed area of frame F. X and Y are the upper-left
29619 corner of the exposed rectangle. W and H are width and height of
29620 the exposed area. All are pixel values. W or H zero means redraw
29621 the entire frame. */
29622
29623 void
29624 expose_frame (struct frame *f, int x, int y, int w, int h)
29625 {
29626 XRectangle r;
29627 int mouse_face_overwritten_p = 0;
29628
29629 TRACE ((stderr, "expose_frame "));
29630
29631 /* No need to redraw if frame will be redrawn soon. */
29632 if (FRAME_GARBAGED_P (f))
29633 {
29634 TRACE ((stderr, " garbaged\n"));
29635 return;
29636 }
29637
29638 /* If basic faces haven't been realized yet, there is no point in
29639 trying to redraw anything. This can happen when we get an expose
29640 event while Emacs is starting, e.g. by moving another window. */
29641 if (FRAME_FACE_CACHE (f) == NULL
29642 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29643 {
29644 TRACE ((stderr, " no faces\n"));
29645 return;
29646 }
29647
29648 if (w == 0 || h == 0)
29649 {
29650 r.x = r.y = 0;
29651 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29652 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29653 }
29654 else
29655 {
29656 r.x = x;
29657 r.y = y;
29658 r.width = w;
29659 r.height = h;
29660 }
29661
29662 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29663 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29664
29665 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29666 if (WINDOWP (f->tool_bar_window))
29667 mouse_face_overwritten_p
29668 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29669 #endif
29670
29671 #ifdef HAVE_X_WINDOWS
29672 #ifndef MSDOS
29673 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29674 if (WINDOWP (f->menu_bar_window))
29675 mouse_face_overwritten_p
29676 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29677 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29678 #endif
29679 #endif
29680
29681 /* Some window managers support a focus-follows-mouse style with
29682 delayed raising of frames. Imagine a partially obscured frame,
29683 and moving the mouse into partially obscured mouse-face on that
29684 frame. The visible part of the mouse-face will be highlighted,
29685 then the WM raises the obscured frame. With at least one WM, KDE
29686 2.1, Emacs is not getting any event for the raising of the frame
29687 (even tried with SubstructureRedirectMask), only Expose events.
29688 These expose events will draw text normally, i.e. not
29689 highlighted. Which means we must redo the highlight here.
29690 Subsume it under ``we love X''. --gerd 2001-08-15 */
29691 /* Included in Windows version because Windows most likely does not
29692 do the right thing if any third party tool offers
29693 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29694 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29695 {
29696 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29697 if (f == hlinfo->mouse_face_mouse_frame)
29698 {
29699 int mouse_x = hlinfo->mouse_face_mouse_x;
29700 int mouse_y = hlinfo->mouse_face_mouse_y;
29701 clear_mouse_face (hlinfo);
29702 note_mouse_highlight (f, mouse_x, mouse_y);
29703 }
29704 }
29705 }
29706
29707
29708 /* EXPORT:
29709 Determine the intersection of two rectangles R1 and R2. Return
29710 the intersection in *RESULT. Value is non-zero if RESULT is not
29711 empty. */
29712
29713 int
29714 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29715 {
29716 XRectangle *left, *right;
29717 XRectangle *upper, *lower;
29718 int intersection_p = 0;
29719
29720 /* Rearrange so that R1 is the left-most rectangle. */
29721 if (r1->x < r2->x)
29722 left = r1, right = r2;
29723 else
29724 left = r2, right = r1;
29725
29726 /* X0 of the intersection is right.x0, if this is inside R1,
29727 otherwise there is no intersection. */
29728 if (right->x <= left->x + left->width)
29729 {
29730 result->x = right->x;
29731
29732 /* The right end of the intersection is the minimum of
29733 the right ends of left and right. */
29734 result->width = (min (left->x + left->width, right->x + right->width)
29735 - result->x);
29736
29737 /* Same game for Y. */
29738 if (r1->y < r2->y)
29739 upper = r1, lower = r2;
29740 else
29741 upper = r2, lower = r1;
29742
29743 /* The upper end of the intersection is lower.y0, if this is inside
29744 of upper. Otherwise, there is no intersection. */
29745 if (lower->y <= upper->y + upper->height)
29746 {
29747 result->y = lower->y;
29748
29749 /* The lower end of the intersection is the minimum of the lower
29750 ends of upper and lower. */
29751 result->height = (min (lower->y + lower->height,
29752 upper->y + upper->height)
29753 - result->y);
29754 intersection_p = 1;
29755 }
29756 }
29757
29758 return intersection_p;
29759 }
29760
29761 #endif /* HAVE_WINDOW_SYSTEM */
29762
29763 \f
29764 /***********************************************************************
29765 Initialization
29766 ***********************************************************************/
29767
29768 void
29769 syms_of_xdisp (void)
29770 {
29771 Vwith_echo_area_save_vector = Qnil;
29772 staticpro (&Vwith_echo_area_save_vector);
29773
29774 Vmessage_stack = Qnil;
29775 staticpro (&Vmessage_stack);
29776
29777 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29778 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29779
29780 message_dolog_marker1 = Fmake_marker ();
29781 staticpro (&message_dolog_marker1);
29782 message_dolog_marker2 = Fmake_marker ();
29783 staticpro (&message_dolog_marker2);
29784 message_dolog_marker3 = Fmake_marker ();
29785 staticpro (&message_dolog_marker3);
29786
29787 #ifdef GLYPH_DEBUG
29788 defsubr (&Sdump_frame_glyph_matrix);
29789 defsubr (&Sdump_glyph_matrix);
29790 defsubr (&Sdump_glyph_row);
29791 defsubr (&Sdump_tool_bar_row);
29792 defsubr (&Strace_redisplay);
29793 defsubr (&Strace_to_stderr);
29794 #endif
29795 #ifdef HAVE_WINDOW_SYSTEM
29796 defsubr (&Stool_bar_height);
29797 defsubr (&Slookup_image_map);
29798 #endif
29799 defsubr (&Sline_pixel_height);
29800 defsubr (&Sformat_mode_line);
29801 defsubr (&Sinvisible_p);
29802 defsubr (&Scurrent_bidi_paragraph_direction);
29803 defsubr (&Swindow_text_pixel_size);
29804 defsubr (&Smove_point_visually);
29805
29806 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29807 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29808 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29809 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29810 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29811 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29812 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29813 DEFSYM (Qeval, "eval");
29814 DEFSYM (QCdata, ":data");
29815 DEFSYM (Qdisplay, "display");
29816 DEFSYM (Qspace_width, "space-width");
29817 DEFSYM (Qraise, "raise");
29818 DEFSYM (Qslice, "slice");
29819 DEFSYM (Qspace, "space");
29820 DEFSYM (Qmargin, "margin");
29821 DEFSYM (Qpointer, "pointer");
29822 DEFSYM (Qleft_margin, "left-margin");
29823 DEFSYM (Qright_margin, "right-margin");
29824 DEFSYM (Qcenter, "center");
29825 DEFSYM (Qline_height, "line-height");
29826 DEFSYM (QCalign_to, ":align-to");
29827 DEFSYM (QCrelative_width, ":relative-width");
29828 DEFSYM (QCrelative_height, ":relative-height");
29829 DEFSYM (QCeval, ":eval");
29830 DEFSYM (QCpropertize, ":propertize");
29831 DEFSYM (QCfile, ":file");
29832 DEFSYM (Qfontified, "fontified");
29833 DEFSYM (Qfontification_functions, "fontification-functions");
29834 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29835 DEFSYM (Qescape_glyph, "escape-glyph");
29836 DEFSYM (Qnobreak_space, "nobreak-space");
29837 DEFSYM (Qimage, "image");
29838 DEFSYM (Qtext, "text");
29839 DEFSYM (Qboth, "both");
29840 DEFSYM (Qboth_horiz, "both-horiz");
29841 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29842 DEFSYM (QCmap, ":map");
29843 DEFSYM (QCpointer, ":pointer");
29844 DEFSYM (Qrect, "rect");
29845 DEFSYM (Qcircle, "circle");
29846 DEFSYM (Qpoly, "poly");
29847 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29848 DEFSYM (Qgrow_only, "grow-only");
29849 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29850 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29851 DEFSYM (Qposition, "position");
29852 DEFSYM (Qbuffer_position, "buffer-position");
29853 DEFSYM (Qobject, "object");
29854 DEFSYM (Qbar, "bar");
29855 DEFSYM (Qhbar, "hbar");
29856 DEFSYM (Qbox, "box");
29857 DEFSYM (Qhollow, "hollow");
29858 DEFSYM (Qhand, "hand");
29859 DEFSYM (Qarrow, "arrow");
29860 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29861
29862 list_of_error = list1 (list2 (intern_c_string ("error"),
29863 intern_c_string ("void-variable")));
29864 staticpro (&list_of_error);
29865
29866 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29867 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29868 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29869 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29870
29871 echo_buffer[0] = echo_buffer[1] = Qnil;
29872 staticpro (&echo_buffer[0]);
29873 staticpro (&echo_buffer[1]);
29874
29875 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29876 staticpro (&echo_area_buffer[0]);
29877 staticpro (&echo_area_buffer[1]);
29878
29879 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29880 staticpro (&Vmessages_buffer_name);
29881
29882 mode_line_proptrans_alist = Qnil;
29883 staticpro (&mode_line_proptrans_alist);
29884 mode_line_string_list = Qnil;
29885 staticpro (&mode_line_string_list);
29886 mode_line_string_face = Qnil;
29887 staticpro (&mode_line_string_face);
29888 mode_line_string_face_prop = Qnil;
29889 staticpro (&mode_line_string_face_prop);
29890 Vmode_line_unwind_vector = Qnil;
29891 staticpro (&Vmode_line_unwind_vector);
29892
29893 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29894
29895 help_echo_string = Qnil;
29896 staticpro (&help_echo_string);
29897 help_echo_object = Qnil;
29898 staticpro (&help_echo_object);
29899 help_echo_window = Qnil;
29900 staticpro (&help_echo_window);
29901 previous_help_echo_string = Qnil;
29902 staticpro (&previous_help_echo_string);
29903 help_echo_pos = -1;
29904
29905 DEFSYM (Qright_to_left, "right-to-left");
29906 DEFSYM (Qleft_to_right, "left-to-right");
29907
29908 #ifdef HAVE_WINDOW_SYSTEM
29909 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29910 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29911 For example, if a block cursor is over a tab, it will be drawn as
29912 wide as that tab on the display. */);
29913 x_stretch_cursor_p = 0;
29914 #endif
29915
29916 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29917 doc: /* Non-nil means highlight trailing whitespace.
29918 The face used for trailing whitespace is `trailing-whitespace'. */);
29919 Vshow_trailing_whitespace = Qnil;
29920
29921 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29922 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29923 If the value is t, Emacs highlights non-ASCII chars which have the
29924 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29925 or `escape-glyph' face respectively.
29926
29927 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29928 U+2011 (non-breaking hyphen) are affected.
29929
29930 Any other non-nil value means to display these characters as a escape
29931 glyph followed by an ordinary space or hyphen.
29932
29933 A value of nil means no special handling of these characters. */);
29934 Vnobreak_char_display = Qt;
29935
29936 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29937 doc: /* The pointer shape to show in void text areas.
29938 A value of nil means to show the text pointer. Other options are `arrow',
29939 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29940 Vvoid_text_area_pointer = Qarrow;
29941
29942 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29943 doc: /* Non-nil means don't actually do any redisplay.
29944 This is used for internal purposes. */);
29945 Vinhibit_redisplay = Qnil;
29946
29947 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29948 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29949 Vglobal_mode_string = Qnil;
29950
29951 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29952 doc: /* Marker for where to display an arrow on top of the buffer text.
29953 This must be the beginning of a line in order to work.
29954 See also `overlay-arrow-string'. */);
29955 Voverlay_arrow_position = Qnil;
29956
29957 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29958 doc: /* String to display as an arrow in non-window frames.
29959 See also `overlay-arrow-position'. */);
29960 Voverlay_arrow_string = build_pure_c_string ("=>");
29961
29962 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29963 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29964 The symbols on this list are examined during redisplay to determine
29965 where to display overlay arrows. */);
29966 Voverlay_arrow_variable_list
29967 = list1 (intern_c_string ("overlay-arrow-position"));
29968
29969 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29970 doc: /* The number of lines to try scrolling a window by when point moves out.
29971 If that fails to bring point back on frame, point is centered instead.
29972 If this is zero, point is always centered after it moves off frame.
29973 If you want scrolling to always be a line at a time, you should set
29974 `scroll-conservatively' to a large value rather than set this to 1. */);
29975
29976 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29977 doc: /* Scroll up to this many lines, to bring point back on screen.
29978 If point moves off-screen, redisplay will scroll by up to
29979 `scroll-conservatively' lines in order to bring point just barely
29980 onto the screen again. If that cannot be done, then redisplay
29981 recenters point as usual.
29982
29983 If the value is greater than 100, redisplay will never recenter point,
29984 but will always scroll just enough text to bring point into view, even
29985 if you move far away.
29986
29987 A value of zero means always recenter point if it moves off screen. */);
29988 scroll_conservatively = 0;
29989
29990 DEFVAR_INT ("scroll-margin", scroll_margin,
29991 doc: /* Number of lines of margin at the top and bottom of a window.
29992 Recenter the window whenever point gets within this many lines
29993 of the top or bottom of the window. */);
29994 scroll_margin = 0;
29995
29996 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29997 doc: /* Pixels per inch value for non-window system displays.
29998 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29999 Vdisplay_pixels_per_inch = make_float (72.0);
30000
30001 #ifdef GLYPH_DEBUG
30002 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30003 #endif
30004
30005 DEFVAR_LISP ("truncate-partial-width-windows",
30006 Vtruncate_partial_width_windows,
30007 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30008 For an integer value, truncate lines in each window narrower than the
30009 full frame width, provided the window width is less than that integer;
30010 otherwise, respect the value of `truncate-lines'.
30011
30012 For any other non-nil value, truncate lines in all windows that do
30013 not span the full frame width.
30014
30015 A value of nil means to respect the value of `truncate-lines'.
30016
30017 If `word-wrap' is enabled, you might want to reduce this. */);
30018 Vtruncate_partial_width_windows = make_number (50);
30019
30020 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30021 doc: /* Maximum buffer size for which line number should be displayed.
30022 If the buffer is bigger than this, the line number does not appear
30023 in the mode line. A value of nil means no limit. */);
30024 Vline_number_display_limit = Qnil;
30025
30026 DEFVAR_INT ("line-number-display-limit-width",
30027 line_number_display_limit_width,
30028 doc: /* Maximum line width (in characters) for line number display.
30029 If the average length of the lines near point is bigger than this, then the
30030 line number may be omitted from the mode line. */);
30031 line_number_display_limit_width = 200;
30032
30033 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30034 doc: /* Non-nil means highlight region even in nonselected windows. */);
30035 highlight_nonselected_windows = 0;
30036
30037 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30038 doc: /* Non-nil if more than one frame is visible on this display.
30039 Minibuffer-only frames don't count, but iconified frames do.
30040 This variable is not guaranteed to be accurate except while processing
30041 `frame-title-format' and `icon-title-format'. */);
30042
30043 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30044 doc: /* Template for displaying the title bar of visible frames.
30045 \(Assuming the window manager supports this feature.)
30046
30047 This variable has the same structure as `mode-line-format', except that
30048 the %c and %l constructs are ignored. It is used only on frames for
30049 which no explicit name has been set \(see `modify-frame-parameters'). */);
30050
30051 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30052 doc: /* Template for displaying the title bar of an iconified frame.
30053 \(Assuming the window manager supports this feature.)
30054 This variable has the same structure as `mode-line-format' (which see),
30055 and is used only on frames for which no explicit name has been set
30056 \(see `modify-frame-parameters'). */);
30057 Vicon_title_format
30058 = Vframe_title_format
30059 = listn (CONSTYPE_PURE, 3,
30060 intern_c_string ("multiple-frames"),
30061 build_pure_c_string ("%b"),
30062 listn (CONSTYPE_PURE, 4,
30063 empty_unibyte_string,
30064 intern_c_string ("invocation-name"),
30065 build_pure_c_string ("@"),
30066 intern_c_string ("system-name")));
30067
30068 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30069 doc: /* Maximum number of lines to keep in the message log buffer.
30070 If nil, disable message logging. If t, log messages but don't truncate
30071 the buffer when it becomes large. */);
30072 Vmessage_log_max = make_number (1000);
30073
30074 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30075 doc: /* Functions called before redisplay, if window sizes have changed.
30076 The value should be a list of functions that take one argument.
30077 Just before redisplay, for each frame, if any of its windows have changed
30078 size since the last redisplay, or have been split or deleted,
30079 all the functions in the list are called, with the frame as argument. */);
30080 Vwindow_size_change_functions = Qnil;
30081
30082 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30083 doc: /* List of functions to call before redisplaying a window with scrolling.
30084 Each function is called with two arguments, the window and its new
30085 display-start position. Note that these functions are also called by
30086 `set-window-buffer'. Also note that the value of `window-end' is not
30087 valid when these functions are called.
30088
30089 Warning: Do not use this feature to alter the way the window
30090 is scrolled. It is not designed for that, and such use probably won't
30091 work. */);
30092 Vwindow_scroll_functions = Qnil;
30093
30094 DEFVAR_LISP ("window-text-change-functions",
30095 Vwindow_text_change_functions,
30096 doc: /* Functions to call in redisplay when text in the window might change. */);
30097 Vwindow_text_change_functions = Qnil;
30098
30099 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30100 doc: /* Functions called when redisplay of a window reaches the end trigger.
30101 Each function is called with two arguments, the window and the end trigger value.
30102 See `set-window-redisplay-end-trigger'. */);
30103 Vredisplay_end_trigger_functions = Qnil;
30104
30105 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30106 doc: /* Non-nil means autoselect window with mouse pointer.
30107 If nil, do not autoselect windows.
30108 A positive number means delay autoselection by that many seconds: a
30109 window is autoselected only after the mouse has remained in that
30110 window for the duration of the delay.
30111 A negative number has a similar effect, but causes windows to be
30112 autoselected only after the mouse has stopped moving. \(Because of
30113 the way Emacs compares mouse events, you will occasionally wait twice
30114 that time before the window gets selected.\)
30115 Any other value means to autoselect window instantaneously when the
30116 mouse pointer enters it.
30117
30118 Autoselection selects the minibuffer only if it is active, and never
30119 unselects the minibuffer if it is active.
30120
30121 When customizing this variable make sure that the actual value of
30122 `focus-follows-mouse' matches the behavior of your window manager. */);
30123 Vmouse_autoselect_window = Qnil;
30124
30125 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30126 doc: /* Non-nil means automatically resize tool-bars.
30127 This dynamically changes the tool-bar's height to the minimum height
30128 that is needed to make all tool-bar items visible.
30129 If value is `grow-only', the tool-bar's height is only increased
30130 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30131 Vauto_resize_tool_bars = Qt;
30132
30133 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30134 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30135 auto_raise_tool_bar_buttons_p = 1;
30136
30137 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30138 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30139 make_cursor_line_fully_visible_p = 1;
30140
30141 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30142 doc: /* Border below tool-bar in pixels.
30143 If an integer, use it as the height of the border.
30144 If it is one of `internal-border-width' or `border-width', use the
30145 value of the corresponding frame parameter.
30146 Otherwise, no border is added below the tool-bar. */);
30147 Vtool_bar_border = Qinternal_border_width;
30148
30149 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30150 doc: /* Margin around tool-bar buttons in pixels.
30151 If an integer, use that for both horizontal and vertical margins.
30152 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30153 HORZ specifying the horizontal margin, and VERT specifying the
30154 vertical margin. */);
30155 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30156
30157 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30158 doc: /* Relief thickness of tool-bar buttons. */);
30159 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30160
30161 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30162 doc: /* Tool bar style to use.
30163 It can be one of
30164 image - show images only
30165 text - show text only
30166 both - show both, text below image
30167 both-horiz - show text to the right of the image
30168 text-image-horiz - show text to the left of the image
30169 any other - use system default or image if no system default.
30170
30171 This variable only affects the GTK+ toolkit version of Emacs. */);
30172 Vtool_bar_style = Qnil;
30173
30174 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30175 doc: /* Maximum number of characters a label can have to be shown.
30176 The tool bar style must also show labels for this to have any effect, see
30177 `tool-bar-style'. */);
30178 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30179
30180 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30181 doc: /* List of functions to call to fontify regions of text.
30182 Each function is called with one argument POS. Functions must
30183 fontify a region starting at POS in the current buffer, and give
30184 fontified regions the property `fontified'. */);
30185 Vfontification_functions = Qnil;
30186 Fmake_variable_buffer_local (Qfontification_functions);
30187
30188 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30189 unibyte_display_via_language_environment,
30190 doc: /* Non-nil means display unibyte text according to language environment.
30191 Specifically, this means that raw bytes in the range 160-255 decimal
30192 are displayed by converting them to the equivalent multibyte characters
30193 according to the current language environment. As a result, they are
30194 displayed according to the current fontset.
30195
30196 Note that this variable affects only how these bytes are displayed,
30197 but does not change the fact they are interpreted as raw bytes. */);
30198 unibyte_display_via_language_environment = 0;
30199
30200 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30201 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30202 If a float, it specifies a fraction of the mini-window frame's height.
30203 If an integer, it specifies a number of lines. */);
30204 Vmax_mini_window_height = make_float (0.25);
30205
30206 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30207 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30208 A value of nil means don't automatically resize mini-windows.
30209 A value of t means resize them to fit the text displayed in them.
30210 A value of `grow-only', the default, means let mini-windows grow only;
30211 they return to their normal size when the minibuffer is closed, or the
30212 echo area becomes empty. */);
30213 Vresize_mini_windows = Qgrow_only;
30214
30215 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30216 doc: /* Alist specifying how to blink the cursor off.
30217 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30218 `cursor-type' frame-parameter or variable equals ON-STATE,
30219 comparing using `equal', Emacs uses OFF-STATE to specify
30220 how to blink it off. ON-STATE and OFF-STATE are values for
30221 the `cursor-type' frame parameter.
30222
30223 If a frame's ON-STATE has no entry in this list,
30224 the frame's other specifications determine how to blink the cursor off. */);
30225 Vblink_cursor_alist = Qnil;
30226
30227 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30228 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30229 If non-nil, windows are automatically scrolled horizontally to make
30230 point visible. */);
30231 automatic_hscrolling_p = 1;
30232 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30233
30234 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30235 doc: /* How many columns away from the window edge point is allowed to get
30236 before automatic hscrolling will horizontally scroll the window. */);
30237 hscroll_margin = 5;
30238
30239 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30240 doc: /* How many columns to scroll the window when point gets too close to the edge.
30241 When point is less than `hscroll-margin' columns from the window
30242 edge, automatic hscrolling will scroll the window by the amount of columns
30243 determined by this variable. If its value is a positive integer, scroll that
30244 many columns. If it's a positive floating-point number, it specifies the
30245 fraction of the window's width to scroll. If it's nil or zero, point will be
30246 centered horizontally after the scroll. Any other value, including negative
30247 numbers, are treated as if the value were zero.
30248
30249 Automatic hscrolling always moves point outside the scroll margin, so if
30250 point was more than scroll step columns inside the margin, the window will
30251 scroll more than the value given by the scroll step.
30252
30253 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30254 and `scroll-right' overrides this variable's effect. */);
30255 Vhscroll_step = make_number (0);
30256
30257 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30258 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30259 Bind this around calls to `message' to let it take effect. */);
30260 message_truncate_lines = 0;
30261
30262 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30263 doc: /* Normal hook run to update the menu bar definitions.
30264 Redisplay runs this hook before it redisplays the menu bar.
30265 This is used to update menus such as Buffers, whose contents depend on
30266 various data. */);
30267 Vmenu_bar_update_hook = Qnil;
30268
30269 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30270 doc: /* Frame for which we are updating a menu.
30271 The enable predicate for a menu binding should check this variable. */);
30272 Vmenu_updating_frame = Qnil;
30273
30274 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30275 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30276 inhibit_menubar_update = 0;
30277
30278 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30279 doc: /* Prefix prepended to all continuation lines at display time.
30280 The value may be a string, an image, or a stretch-glyph; it is
30281 interpreted in the same way as the value of a `display' text property.
30282
30283 This variable is overridden by any `wrap-prefix' text or overlay
30284 property.
30285
30286 To add a prefix to non-continuation lines, use `line-prefix'. */);
30287 Vwrap_prefix = Qnil;
30288 DEFSYM (Qwrap_prefix, "wrap-prefix");
30289 Fmake_variable_buffer_local (Qwrap_prefix);
30290
30291 DEFVAR_LISP ("line-prefix", Vline_prefix,
30292 doc: /* Prefix prepended to all non-continuation lines at display time.
30293 The value may be a string, an image, or a stretch-glyph; it is
30294 interpreted in the same way as the value of a `display' text property.
30295
30296 This variable is overridden by any `line-prefix' text or overlay
30297 property.
30298
30299 To add a prefix to continuation lines, use `wrap-prefix'. */);
30300 Vline_prefix = Qnil;
30301 DEFSYM (Qline_prefix, "line-prefix");
30302 Fmake_variable_buffer_local (Qline_prefix);
30303
30304 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30305 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30306 inhibit_eval_during_redisplay = 0;
30307
30308 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30309 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30310 inhibit_free_realized_faces = 0;
30311
30312 #ifdef GLYPH_DEBUG
30313 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30314 doc: /* Inhibit try_window_id display optimization. */);
30315 inhibit_try_window_id = 0;
30316
30317 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30318 doc: /* Inhibit try_window_reusing display optimization. */);
30319 inhibit_try_window_reusing = 0;
30320
30321 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30322 doc: /* Inhibit try_cursor_movement display optimization. */);
30323 inhibit_try_cursor_movement = 0;
30324 #endif /* GLYPH_DEBUG */
30325
30326 DEFVAR_INT ("overline-margin", overline_margin,
30327 doc: /* Space between overline and text, in pixels.
30328 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30329 margin to the character height. */);
30330 overline_margin = 2;
30331
30332 DEFVAR_INT ("underline-minimum-offset",
30333 underline_minimum_offset,
30334 doc: /* Minimum distance between baseline and underline.
30335 This can improve legibility of underlined text at small font sizes,
30336 particularly when using variable `x-use-underline-position-properties'
30337 with fonts that specify an UNDERLINE_POSITION relatively close to the
30338 baseline. The default value is 1. */);
30339 underline_minimum_offset = 1;
30340
30341 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30342 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30343 This feature only works when on a window system that can change
30344 cursor shapes. */);
30345 display_hourglass_p = 1;
30346
30347 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30348 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30349 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30350
30351 #ifdef HAVE_WINDOW_SYSTEM
30352 hourglass_atimer = NULL;
30353 hourglass_shown_p = 0;
30354 #endif /* HAVE_WINDOW_SYSTEM */
30355
30356 DEFSYM (Qglyphless_char, "glyphless-char");
30357 DEFSYM (Qhex_code, "hex-code");
30358 DEFSYM (Qempty_box, "empty-box");
30359 DEFSYM (Qthin_space, "thin-space");
30360 DEFSYM (Qzero_width, "zero-width");
30361
30362 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30363 doc: /* Function run just before redisplay.
30364 It is called with one argument, which is the set of windows that are to
30365 be redisplayed. This set can be nil (meaning, only the selected window),
30366 or t (meaning all windows). */);
30367 Vpre_redisplay_function = intern ("ignore");
30368
30369 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30370 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30371
30372 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30373 doc: /* Char-table defining glyphless characters.
30374 Each element, if non-nil, should be one of the following:
30375 an ASCII acronym string: display this string in a box
30376 `hex-code': display the hexadecimal code of a character in a box
30377 `empty-box': display as an empty box
30378 `thin-space': display as 1-pixel width space
30379 `zero-width': don't display
30380 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30381 display method for graphical terminals and text terminals respectively.
30382 GRAPHICAL and TEXT should each have one of the values listed above.
30383
30384 The char-table has one extra slot to control the display of a character for
30385 which no font is found. This slot only takes effect on graphical terminals.
30386 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30387 `thin-space'. The default is `empty-box'.
30388
30389 If a character has a non-nil entry in an active display table, the
30390 display table takes effect; in this case, Emacs does not consult
30391 `glyphless-char-display' at all. */);
30392 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30393 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30394 Qempty_box);
30395
30396 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30397 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30398 Vdebug_on_message = Qnil;
30399
30400 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30401 doc: /* */);
30402 Vredisplay__all_windows_cause
30403 = Fmake_vector (make_number (100), make_number (0));
30404
30405 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30406 doc: /* */);
30407 Vredisplay__mode_lines_cause
30408 = Fmake_vector (make_number (100), make_number (0));
30409 }
30410
30411
30412 /* Initialize this module when Emacs starts. */
30413
30414 void
30415 init_xdisp (void)
30416 {
30417 CHARPOS (this_line_start_pos) = 0;
30418
30419 if (!noninteractive)
30420 {
30421 struct window *m = XWINDOW (minibuf_window);
30422 Lisp_Object frame = m->frame;
30423 struct frame *f = XFRAME (frame);
30424 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30425 struct window *r = XWINDOW (root);
30426 int i;
30427
30428 echo_area_window = minibuf_window;
30429
30430 r->top_line = FRAME_TOP_MARGIN (f);
30431 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30432 r->total_cols = FRAME_COLS (f);
30433 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30434 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30435 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30436
30437 m->top_line = FRAME_LINES (f) - 1;
30438 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30439 m->total_cols = FRAME_COLS (f);
30440 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30441 m->total_lines = 1;
30442 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30443
30444 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30445 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30446 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30447
30448 /* The default ellipsis glyphs `...'. */
30449 for (i = 0; i < 3; ++i)
30450 default_invis_vector[i] = make_number ('.');
30451 }
30452
30453 {
30454 /* Allocate the buffer for frame titles.
30455 Also used for `format-mode-line'. */
30456 int size = 100;
30457 mode_line_noprop_buf = xmalloc (size);
30458 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30459 mode_line_noprop_ptr = mode_line_noprop_buf;
30460 mode_line_target = MODE_LINE_DISPLAY;
30461 }
30462
30463 help_echo_showing_p = 0;
30464 }
30465
30466 #ifdef HAVE_WINDOW_SYSTEM
30467
30468 /* Platform-independent portion of hourglass implementation. */
30469
30470 /* Cancel a currently active hourglass timer, and start a new one. */
30471 void
30472 start_hourglass (void)
30473 {
30474 struct timespec delay;
30475
30476 cancel_hourglass ();
30477
30478 if (INTEGERP (Vhourglass_delay)
30479 && XINT (Vhourglass_delay) > 0)
30480 delay = make_timespec (min (XINT (Vhourglass_delay),
30481 TYPE_MAXIMUM (time_t)),
30482 0);
30483 else if (FLOATP (Vhourglass_delay)
30484 && XFLOAT_DATA (Vhourglass_delay) > 0)
30485 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30486 else
30487 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30488
30489 #ifdef HAVE_NTGUI
30490 {
30491 extern void w32_note_current_window (void);
30492 w32_note_current_window ();
30493 }
30494 #endif /* HAVE_NTGUI */
30495
30496 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30497 show_hourglass, NULL);
30498 }
30499
30500
30501 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30502 shown. */
30503 void
30504 cancel_hourglass (void)
30505 {
30506 if (hourglass_atimer)
30507 {
30508 cancel_atimer (hourglass_atimer);
30509 hourglass_atimer = NULL;
30510 }
30511
30512 if (hourglass_shown_p)
30513 hide_hourglass ();
30514 }
30515
30516 #endif /* HAVE_WINDOW_SYSTEM */