Fix bug #15437 with mouse highlight on overlay strings.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 int noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static int message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static int overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static int message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay. */
506
507 int update_mode_lines;
508
509 /* Nonzero if window sizes or contents have changed since last
510 redisplay that finished. */
511
512 int windows_or_buffers_changed;
513
514 /* Nonzero after display_mode_line if %l was used and it displayed a
515 line number. */
516
517 static int line_number_displayed;
518
519 /* The name of the *Messages* buffer, a string. */
520
521 static Lisp_Object Vmessages_buffer_name;
522
523 /* Current, index 0, and last displayed echo area message. Either
524 buffers from echo_buffers, or nil to indicate no message. */
525
526 Lisp_Object echo_area_buffer[2];
527
528 /* The buffers referenced from echo_area_buffer. */
529
530 static Lisp_Object echo_buffer[2];
531
532 /* A vector saved used in with_area_buffer to reduce consing. */
533
534 static Lisp_Object Vwith_echo_area_save_vector;
535
536 /* Non-zero means display_echo_area should display the last echo area
537 message again. Set by redisplay_preserve_echo_area. */
538
539 static int display_last_displayed_message_p;
540
541 /* Nonzero if echo area is being used by print; zero if being used by
542 message. */
543
544 static int message_buf_print;
545
546 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
547
548 static Lisp_Object Qinhibit_menubar_update;
549 static Lisp_Object Qmessage_truncate_lines;
550
551 /* Set to 1 in clear_message to make redisplay_internal aware
552 of an emptied echo area. */
553
554 static int message_cleared_p;
555
556 /* A scratch glyph row with contents used for generating truncation
557 glyphs. Also used in direct_output_for_insert. */
558
559 #define MAX_SCRATCH_GLYPHS 100
560 static struct glyph_row scratch_glyph_row;
561 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
562
563 /* Ascent and height of the last line processed by move_it_to. */
564
565 static int last_height;
566
567 /* Non-zero if there's a help-echo in the echo area. */
568
569 int help_echo_showing_p;
570
571 /* The maximum distance to look ahead for text properties. Values
572 that are too small let us call compute_char_face and similar
573 functions too often which is expensive. Values that are too large
574 let us call compute_char_face and alike too often because we
575 might not be interested in text properties that far away. */
576
577 #define TEXT_PROP_DISTANCE_LIMIT 100
578
579 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
580 iterator state and later restore it. This is needed because the
581 bidi iterator on bidi.c keeps a stacked cache of its states, which
582 is really a singleton. When we use scratch iterator objects to
583 move around the buffer, we can cause the bidi cache to be pushed or
584 popped, and therefore we need to restore the cache state when we
585 return to the original iterator. */
586 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
587 do { \
588 if (CACHE) \
589 bidi_unshelve_cache (CACHE, 1); \
590 ITCOPY = ITORIG; \
591 CACHE = bidi_shelve_cache (); \
592 } while (0)
593
594 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
595 do { \
596 if (pITORIG != pITCOPY) \
597 *(pITORIG) = *(pITCOPY); \
598 bidi_unshelve_cache (CACHE, 0); \
599 CACHE = NULL; \
600 } while (0)
601
602 #ifdef GLYPH_DEBUG
603
604 /* Non-zero means print traces of redisplay if compiled with
605 GLYPH_DEBUG defined. */
606
607 int trace_redisplay_p;
608
609 #endif /* GLYPH_DEBUG */
610
611 #ifdef DEBUG_TRACE_MOVE
612 /* Non-zero means trace with TRACE_MOVE to stderr. */
613 int trace_move;
614
615 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
616 #else
617 #define TRACE_MOVE(x) (void) 0
618 #endif
619
620 static Lisp_Object Qauto_hscroll_mode;
621
622 /* Buffer being redisplayed -- for redisplay_window_error. */
623
624 static struct buffer *displayed_buffer;
625
626 /* Value returned from text property handlers (see below). */
627
628 enum prop_handled
629 {
630 HANDLED_NORMALLY,
631 HANDLED_RECOMPUTE_PROPS,
632 HANDLED_OVERLAY_STRING_CONSUMED,
633 HANDLED_RETURN
634 };
635
636 /* A description of text properties that redisplay is interested
637 in. */
638
639 struct props
640 {
641 /* The name of the property. */
642 Lisp_Object *name;
643
644 /* A unique index for the property. */
645 enum prop_idx idx;
646
647 /* A handler function called to set up iterator IT from the property
648 at IT's current position. Value is used to steer handle_stop. */
649 enum prop_handled (*handler) (struct it *it);
650 };
651
652 static enum prop_handled handle_face_prop (struct it *);
653 static enum prop_handled handle_invisible_prop (struct it *);
654 static enum prop_handled handle_display_prop (struct it *);
655 static enum prop_handled handle_composition_prop (struct it *);
656 static enum prop_handled handle_overlay_change (struct it *);
657 static enum prop_handled handle_fontified_prop (struct it *);
658
659 /* Properties handled by iterators. */
660
661 static struct props it_props[] =
662 {
663 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
664 /* Handle `face' before `display' because some sub-properties of
665 `display' need to know the face. */
666 {&Qface, FACE_PROP_IDX, handle_face_prop},
667 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
668 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
669 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
670 {NULL, 0, NULL}
671 };
672
673 /* Value is the position described by X. If X is a marker, value is
674 the marker_position of X. Otherwise, value is X. */
675
676 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
677
678 /* Enumeration returned by some move_it_.* functions internally. */
679
680 enum move_it_result
681 {
682 /* Not used. Undefined value. */
683 MOVE_UNDEFINED,
684
685 /* Move ended at the requested buffer position or ZV. */
686 MOVE_POS_MATCH_OR_ZV,
687
688 /* Move ended at the requested X pixel position. */
689 MOVE_X_REACHED,
690
691 /* Move within a line ended at the end of a line that must be
692 continued. */
693 MOVE_LINE_CONTINUED,
694
695 /* Move within a line ended at the end of a line that would
696 be displayed truncated. */
697 MOVE_LINE_TRUNCATED,
698
699 /* Move within a line ended at a line end. */
700 MOVE_NEWLINE_OR_CR
701 };
702
703 /* This counter is used to clear the face cache every once in a while
704 in redisplay_internal. It is incremented for each redisplay.
705 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
706 cleared. */
707
708 #define CLEAR_FACE_CACHE_COUNT 500
709 static int clear_face_cache_count;
710
711 /* Similarly for the image cache. */
712
713 #ifdef HAVE_WINDOW_SYSTEM
714 #define CLEAR_IMAGE_CACHE_COUNT 101
715 static int clear_image_cache_count;
716
717 /* Null glyph slice */
718 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
719 #endif
720
721 /* True while redisplay_internal is in progress. */
722
723 bool redisplaying_p;
724
725 static Lisp_Object Qinhibit_free_realized_faces;
726 static Lisp_Object Qmode_line_default_help_echo;
727
728 /* If a string, XTread_socket generates an event to display that string.
729 (The display is done in read_char.) */
730
731 Lisp_Object help_echo_string;
732 Lisp_Object help_echo_window;
733 Lisp_Object help_echo_object;
734 ptrdiff_t help_echo_pos;
735
736 /* Temporary variable for XTread_socket. */
737
738 Lisp_Object previous_help_echo_string;
739
740 /* Platform-independent portion of hourglass implementation. */
741
742 #ifdef HAVE_WINDOW_SYSTEM
743
744 /* Non-zero means an hourglass cursor is currently shown. */
745 int hourglass_shown_p;
746
747 /* If non-null, an asynchronous timer that, when it expires, displays
748 an hourglass cursor on all frames. */
749 struct atimer *hourglass_atimer;
750
751 #endif /* HAVE_WINDOW_SYSTEM */
752
753 /* Name of the face used to display glyphless characters. */
754 Lisp_Object Qglyphless_char;
755
756 /* Symbol for the purpose of Vglyphless_char_display. */
757 static Lisp_Object Qglyphless_char_display;
758
759 /* Method symbols for Vglyphless_char_display. */
760 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
761
762 /* Default number of seconds to wait before displaying an hourglass
763 cursor. */
764 #define DEFAULT_HOURGLASS_DELAY 1
765
766 #ifdef HAVE_WINDOW_SYSTEM
767
768 /* Default pixel width of `thin-space' display method. */
769 #define THIN_SPACE_WIDTH 1
770
771 #endif /* HAVE_WINDOW_SYSTEM */
772
773 /* Function prototypes. */
774
775 static void setup_for_ellipsis (struct it *, int);
776 static void set_iterator_to_next (struct it *, int);
777 static void mark_window_display_accurate_1 (struct window *, int);
778 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
779 static int display_prop_string_p (Lisp_Object, Lisp_Object);
780 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
781 static int cursor_row_p (struct glyph_row *);
782 static int redisplay_mode_lines (Lisp_Object, int);
783 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
784
785 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
786
787 static void handle_line_prefix (struct it *);
788
789 static void pint2str (char *, int, ptrdiff_t);
790 static void pint2hrstr (char *, int, ptrdiff_t);
791 static struct text_pos run_window_scroll_functions (Lisp_Object,
792 struct text_pos);
793 static int text_outside_line_unchanged_p (struct window *,
794 ptrdiff_t, ptrdiff_t);
795 static void store_mode_line_noprop_char (char);
796 static int store_mode_line_noprop (const char *, int, int);
797 static void handle_stop (struct it *);
798 static void handle_stop_backwards (struct it *, ptrdiff_t);
799 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
800 static void ensure_echo_area_buffers (void);
801 static void unwind_with_echo_area_buffer (Lisp_Object);
802 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
803 static int with_echo_area_buffer (struct window *, int,
804 int (*) (ptrdiff_t, Lisp_Object),
805 ptrdiff_t, Lisp_Object);
806 static void clear_garbaged_frames (void);
807 static int current_message_1 (ptrdiff_t, Lisp_Object);
808 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static int set_message_1 (ptrdiff_t, Lisp_Object);
811 static int display_echo_area (struct window *);
812 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static int string_char_and_length (const unsigned char *, int *);
816 static struct text_pos display_prop_end (struct it *, Lisp_Object,
817 struct text_pos);
818 static int compute_window_start_on_continuation_line (struct window *);
819 static void insert_left_trunc_glyphs (struct it *);
820 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
821 Lisp_Object);
822 static void extend_face_to_end_of_line (struct it *);
823 static int append_space_for_newline (struct it *, int);
824 static int cursor_row_fully_visible_p (struct window *, int, int);
825 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
826 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
827 static int trailing_whitespace_p (ptrdiff_t);
828 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
829 static void push_it (struct it *, struct text_pos *);
830 static void iterate_out_of_display_property (struct it *);
831 static void pop_it (struct it *);
832 static void sync_frame_with_window_matrix_rows (struct window *);
833 static void redisplay_internal (void);
834 static int echo_area_display (int);
835 static void redisplay_windows (Lisp_Object);
836 static void redisplay_window (Lisp_Object, int);
837 static Lisp_Object redisplay_window_error (Lisp_Object);
838 static Lisp_Object redisplay_window_0 (Lisp_Object);
839 static Lisp_Object redisplay_window_1 (Lisp_Object);
840 static int set_cursor_from_row (struct window *, struct glyph_row *,
841 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
842 int, int);
843 static int update_menu_bar (struct frame *, int, int);
844 static int try_window_reusing_current_matrix (struct window *);
845 static int try_window_id (struct window *);
846 static int display_line (struct it *);
847 static int display_mode_lines (struct window *);
848 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
849 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
850 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
851 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
852 static void display_menu_bar (struct window *);
853 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
854 ptrdiff_t *);
855 static int display_string (const char *, Lisp_Object, Lisp_Object,
856 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
857 static void compute_line_metrics (struct it *);
858 static void run_redisplay_end_trigger_hook (struct it *);
859 static int get_overlay_strings (struct it *, ptrdiff_t);
860 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
861 static void next_overlay_string (struct it *);
862 static void reseat (struct it *, struct text_pos, int);
863 static void reseat_1 (struct it *, struct text_pos, int);
864 static void back_to_previous_visible_line_start (struct it *);
865 static void reseat_at_next_visible_line_start (struct it *, int);
866 static int next_element_from_ellipsis (struct it *);
867 static int next_element_from_display_vector (struct it *);
868 static int next_element_from_string (struct it *);
869 static int next_element_from_c_string (struct it *);
870 static int next_element_from_buffer (struct it *);
871 static int next_element_from_composition (struct it *);
872 static int next_element_from_image (struct it *);
873 static int next_element_from_stretch (struct it *);
874 static void load_overlay_strings (struct it *, ptrdiff_t);
875 static int init_from_display_pos (struct it *, struct window *,
876 struct display_pos *);
877 static void reseat_to_string (struct it *, const char *,
878 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
879 static int get_next_display_element (struct it *);
880 static enum move_it_result
881 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
882 enum move_operation_enum);
883 static void get_visually_first_element (struct it *);
884 static void init_to_row_start (struct it *, struct window *,
885 struct glyph_row *);
886 static int init_to_row_end (struct it *, struct window *,
887 struct glyph_row *);
888 static void back_to_previous_line_start (struct it *);
889 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
890 static struct text_pos string_pos_nchars_ahead (struct text_pos,
891 Lisp_Object, ptrdiff_t);
892 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
893 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
894 static ptrdiff_t number_of_chars (const char *, bool);
895 static void compute_stop_pos (struct it *);
896 static void compute_string_pos (struct text_pos *, struct text_pos,
897 Lisp_Object);
898 static int face_before_or_after_it_pos (struct it *, int);
899 static ptrdiff_t next_overlay_change (ptrdiff_t);
900 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
901 Lisp_Object, struct text_pos *, ptrdiff_t, int);
902 static int handle_single_display_spec (struct it *, Lisp_Object,
903 Lisp_Object, Lisp_Object,
904 struct text_pos *, ptrdiff_t, int, int);
905 static int underlying_face_id (struct it *);
906 static int in_ellipses_for_invisible_text_p (struct display_pos *,
907 struct window *);
908
909 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
910 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
911
912 #ifdef HAVE_WINDOW_SYSTEM
913
914 static void x_consider_frame_title (Lisp_Object);
915 static void update_tool_bar (struct frame *, int);
916 static int redisplay_tool_bar (struct frame *);
917 static void notice_overwritten_cursor (struct window *,
918 enum glyph_row_area,
919 int, int, int, int);
920 static void append_stretch_glyph (struct it *, Lisp_Object,
921 int, int, int);
922
923
924 #endif /* HAVE_WINDOW_SYSTEM */
925
926 static void produce_special_glyphs (struct it *, enum display_element_type);
927 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
928 static int coords_in_mouse_face_p (struct window *, int, int);
929
930
931 \f
932 /***********************************************************************
933 Window display dimensions
934 ***********************************************************************/
935
936 /* Return the bottom boundary y-position for text lines in window W.
937 This is the first y position at which a line cannot start.
938 It is relative to the top of the window.
939
940 This is the height of W minus the height of a mode line, if any. */
941
942 int
943 window_text_bottom_y (struct window *w)
944 {
945 int height = WINDOW_TOTAL_HEIGHT (w);
946
947 if (WINDOW_WANTS_MODELINE_P (w))
948 height -= CURRENT_MODE_LINE_HEIGHT (w);
949 return height;
950 }
951
952 /* Return the pixel width of display area AREA of window W.
953 ANY_AREA means return the total width of W, not including
954 fringes to the left and right of the window. */
955
956 int
957 window_box_width (struct window *w, enum glyph_row_area area)
958 {
959 int cols = w->total_cols;
960 int pixels = 0;
961
962 if (!w->pseudo_window_p)
963 {
964 cols -= WINDOW_SCROLL_BAR_COLS (w);
965
966 if (area == TEXT_AREA)
967 {
968 cols -= max (0, w->left_margin_cols);
969 cols -= max (0, w->right_margin_cols);
970 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
971 }
972 else if (area == LEFT_MARGIN_AREA)
973 {
974 cols = max (0, w->left_margin_cols);
975 pixels = 0;
976 }
977 else if (area == RIGHT_MARGIN_AREA)
978 {
979 cols = max (0, w->right_margin_cols);
980 pixels = 0;
981 }
982 }
983
984 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
985 }
986
987
988 /* Return the pixel height of the display area of window W, not
989 including mode lines of W, if any. */
990
991 int
992 window_box_height (struct window *w)
993 {
994 struct frame *f = XFRAME (w->frame);
995 int height = WINDOW_TOTAL_HEIGHT (w);
996
997 eassert (height >= 0);
998
999 /* Note: the code below that determines the mode-line/header-line
1000 height is essentially the same as that contained in the macro
1001 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1002 the appropriate glyph row has its `mode_line_p' flag set,
1003 and if it doesn't, uses estimate_mode_line_height instead. */
1004
1005 if (WINDOW_WANTS_MODELINE_P (w))
1006 {
1007 struct glyph_row *ml_row
1008 = (w->current_matrix && w->current_matrix->rows
1009 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1010 : 0);
1011 if (ml_row && ml_row->mode_line_p)
1012 height -= ml_row->height;
1013 else
1014 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1015 }
1016
1017 if (WINDOW_WANTS_HEADER_LINE_P (w))
1018 {
1019 struct glyph_row *hl_row
1020 = (w->current_matrix && w->current_matrix->rows
1021 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1022 : 0);
1023 if (hl_row && hl_row->mode_line_p)
1024 height -= hl_row->height;
1025 else
1026 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1027 }
1028
1029 /* With a very small font and a mode-line that's taller than
1030 default, we might end up with a negative height. */
1031 return max (0, height);
1032 }
1033
1034 /* Return the window-relative coordinate of the left edge of display
1035 area AREA of window W. ANY_AREA means return the left edge of the
1036 whole window, to the right of the left fringe of W. */
1037
1038 int
1039 window_box_left_offset (struct window *w, enum glyph_row_area area)
1040 {
1041 int x;
1042
1043 if (w->pseudo_window_p)
1044 return 0;
1045
1046 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1047
1048 if (area == TEXT_AREA)
1049 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1050 + window_box_width (w, LEFT_MARGIN_AREA));
1051 else if (area == RIGHT_MARGIN_AREA)
1052 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1053 + window_box_width (w, LEFT_MARGIN_AREA)
1054 + window_box_width (w, TEXT_AREA)
1055 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1056 ? 0
1057 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1058 else if (area == LEFT_MARGIN_AREA
1059 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1060 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1061
1062 return x;
1063 }
1064
1065
1066 /* Return the window-relative coordinate of the right edge of display
1067 area AREA of window W. ANY_AREA means return the right edge of the
1068 whole window, to the left of the right fringe of W. */
1069
1070 int
1071 window_box_right_offset (struct window *w, enum glyph_row_area area)
1072 {
1073 return window_box_left_offset (w, area) + window_box_width (w, area);
1074 }
1075
1076 /* Return the frame-relative coordinate of the left edge of display
1077 area AREA of window W. ANY_AREA means return the left edge of the
1078 whole window, to the right of the left fringe of W. */
1079
1080 int
1081 window_box_left (struct window *w, enum glyph_row_area area)
1082 {
1083 struct frame *f = XFRAME (w->frame);
1084 int x;
1085
1086 if (w->pseudo_window_p)
1087 return FRAME_INTERNAL_BORDER_WIDTH (f);
1088
1089 x = (WINDOW_LEFT_EDGE_X (w)
1090 + window_box_left_offset (w, area));
1091
1092 return x;
1093 }
1094
1095
1096 /* Return the frame-relative coordinate of the right edge of display
1097 area AREA of window W. ANY_AREA means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1099
1100 int
1101 window_box_right (struct window *w, enum glyph_row_area area)
1102 {
1103 return window_box_left (w, area) + window_box_width (w, area);
1104 }
1105
1106 /* Get the bounding box of the display area AREA of window W, without
1107 mode lines, in frame-relative coordinates. ANY_AREA means the
1108 whole window, not including the left and right fringes of
1109 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1110 coordinates of the upper-left corner of the box. Return in
1111 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1112
1113 void
1114 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1115 int *box_y, int *box_width, int *box_height)
1116 {
1117 if (box_width)
1118 *box_width = window_box_width (w, area);
1119 if (box_height)
1120 *box_height = window_box_height (w);
1121 if (box_x)
1122 *box_x = window_box_left (w, area);
1123 if (box_y)
1124 {
1125 *box_y = WINDOW_TOP_EDGE_Y (w);
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1128 }
1129 }
1130
1131 #ifdef HAVE_WINDOW_SYSTEM
1132
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1135 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1136 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1137 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1138 box. */
1139
1140 static void
1141 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1142 int *bottom_right_x, int *bottom_right_y)
1143 {
1144 window_box (w, ANY_AREA, top_left_x, top_left_y,
1145 bottom_right_x, bottom_right_y);
1146 *bottom_right_x += *top_left_x;
1147 *bottom_right_y += *top_left_y;
1148 }
1149
1150 #endif /* HAVE_WINDOW_SYSTEM */
1151
1152 /***********************************************************************
1153 Utilities
1154 ***********************************************************************/
1155
1156 /* Return the bottom y-position of the line the iterator IT is in.
1157 This can modify IT's settings. */
1158
1159 int
1160 line_bottom_y (struct it *it)
1161 {
1162 int line_height = it->max_ascent + it->max_descent;
1163 int line_top_y = it->current_y;
1164
1165 if (line_height == 0)
1166 {
1167 if (last_height)
1168 line_height = last_height;
1169 else if (IT_CHARPOS (*it) < ZV)
1170 {
1171 move_it_by_lines (it, 1);
1172 line_height = (it->max_ascent || it->max_descent
1173 ? it->max_ascent + it->max_descent
1174 : last_height);
1175 }
1176 else
1177 {
1178 struct glyph_row *row = it->glyph_row;
1179
1180 /* Use the default character height. */
1181 it->glyph_row = NULL;
1182 it->what = IT_CHARACTER;
1183 it->c = ' ';
1184 it->len = 1;
1185 PRODUCE_GLYPHS (it);
1186 line_height = it->ascent + it->descent;
1187 it->glyph_row = row;
1188 }
1189 }
1190
1191 return line_top_y + line_height;
1192 }
1193
1194 DEFUN ("line-pixel-height", Fline_pixel_height,
1195 Sline_pixel_height, 0, 0, 0,
1196 doc: /* Return height in pixels of text line in the selected window.
1197
1198 Value is the height in pixels of the line at point. */)
1199 (void)
1200 {
1201 struct it it;
1202 struct text_pos pt;
1203 struct window *w = XWINDOW (selected_window);
1204
1205 SET_TEXT_POS (pt, PT, PT_BYTE);
1206 start_display (&it, w, pt);
1207 it.vpos = it.current_y = 0;
1208 last_height = 0;
1209 return make_number (line_bottom_y (&it));
1210 }
1211
1212 /* Return the default pixel height of text lines in window W. The
1213 value is the canonical height of the W frame's default font, plus
1214 any extra space required by the line-spacing variable or frame
1215 parameter.
1216
1217 Implementation note: this ignores any line-spacing text properties
1218 put on the newline characters. This is because those properties
1219 only affect the _screen_ line ending in the newline (i.e., in a
1220 continued line, only the last screen line will be affected), which
1221 means only a small number of lines in a buffer can ever use this
1222 feature. Since this function is used to compute the default pixel
1223 equivalent of text lines in a window, we can safely ignore those
1224 few lines. For the same reasons, we ignore the line-height
1225 properties. */
1226 int
1227 default_line_pixel_height (struct window *w)
1228 {
1229 struct frame *f = WINDOW_XFRAME (w);
1230 int height = FRAME_LINE_HEIGHT (f);
1231
1232 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1233 {
1234 struct buffer *b = XBUFFER (w->contents);
1235 Lisp_Object val = BVAR (b, extra_line_spacing);
1236
1237 if (NILP (val))
1238 val = BVAR (&buffer_defaults, extra_line_spacing);
1239 if (!NILP (val))
1240 {
1241 if (RANGED_INTEGERP (0, val, INT_MAX))
1242 height += XFASTINT (val);
1243 else if (FLOATP (val))
1244 {
1245 int addon = XFLOAT_DATA (val) * height + 0.5;
1246
1247 if (addon >= 0)
1248 height += addon;
1249 }
1250 }
1251 else
1252 height += f->extra_line_spacing;
1253 }
1254
1255 return height;
1256 }
1257
1258 /* Subroutine of pos_visible_p below. Extracts a display string, if
1259 any, from the display spec given as its argument. */
1260 static Lisp_Object
1261 string_from_display_spec (Lisp_Object spec)
1262 {
1263 if (CONSP (spec))
1264 {
1265 while (CONSP (spec))
1266 {
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1269 spec = XCDR (spec);
1270 }
1271 }
1272 else if (VECTORP (spec))
1273 {
1274 ptrdiff_t i;
1275
1276 for (i = 0; i < ASIZE (spec); i++)
1277 {
1278 if (STRINGP (AREF (spec, i)))
1279 return AREF (spec, i);
1280 }
1281 return Qnil;
1282 }
1283
1284 return spec;
1285 }
1286
1287
1288 /* Limit insanely large values of W->hscroll on frame F to the largest
1289 value that will still prevent first_visible_x and last_visible_x of
1290 'struct it' from overflowing an int. */
1291 static int
1292 window_hscroll_limited (struct window *w, struct frame *f)
1293 {
1294 ptrdiff_t window_hscroll = w->hscroll;
1295 int window_text_width = window_box_width (w, TEXT_AREA);
1296 int colwidth = FRAME_COLUMN_WIDTH (f);
1297
1298 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1299 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1300
1301 return window_hscroll;
1302 }
1303
1304 /* Return 1 if position CHARPOS is visible in window W.
1305 CHARPOS < 0 means return info about WINDOW_END position.
1306 If visible, set *X and *Y to pixel coordinates of top left corner.
1307 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1308 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1309
1310 int
1311 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1312 int *rtop, int *rbot, int *rowh, int *vpos)
1313 {
1314 struct it it;
1315 void *itdata = bidi_shelve_cache ();
1316 struct text_pos top;
1317 int visible_p = 0;
1318 struct buffer *old_buffer = NULL;
1319
1320 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1321 return visible_p;
1322
1323 if (XBUFFER (w->contents) != current_buffer)
1324 {
1325 old_buffer = current_buffer;
1326 set_buffer_internal_1 (XBUFFER (w->contents));
1327 }
1328
1329 SET_TEXT_POS_FROM_MARKER (top, w->start);
1330 /* Scrolling a minibuffer window via scroll bar when the echo area
1331 shows long text sometimes resets the minibuffer contents behind
1332 our backs. */
1333 if (CHARPOS (top) > ZV)
1334 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1335
1336 /* Compute exact mode line heights. */
1337 if (WINDOW_WANTS_MODELINE_P (w))
1338 w->mode_line_height
1339 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1340 BVAR (current_buffer, mode_line_format));
1341
1342 if (WINDOW_WANTS_HEADER_LINE_P (w))
1343 w->header_line_height
1344 = display_mode_line (w, HEADER_LINE_FACE_ID,
1345 BVAR (current_buffer, header_line_format));
1346
1347 start_display (&it, w, top);
1348 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1349 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1350
1351 if (charpos >= 0
1352 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1353 && IT_CHARPOS (it) >= charpos)
1354 /* When scanning backwards under bidi iteration, move_it_to
1355 stops at or _before_ CHARPOS, because it stops at or to
1356 the _right_ of the character at CHARPOS. */
1357 || (it.bidi_p && it.bidi_it.scan_dir == -1
1358 && IT_CHARPOS (it) <= charpos)))
1359 {
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1362 or covered by a display property, move_it_to stops at the end
1363 of the invisible text, to the right of CHARPOS. (ii) If
1364 CHARPOS is in a display vector, move_it_to stops on its last
1365 glyph. */
1366 int top_x = it.current_x;
1367 int top_y = it.current_y;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 enum it_method it_method = it.method;
1370 int bottom_y = (last_height = 0, line_bottom_y (&it));
1371 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1372
1373 if (top_y < window_top_y)
1374 visible_p = bottom_y > window_top_y;
1375 else if (top_y < it.last_visible_y)
1376 visible_p = 1;
1377 if (bottom_y >= it.last_visible_y
1378 && it.bidi_p && it.bidi_it.scan_dir == -1
1379 && IT_CHARPOS (it) < charpos)
1380 {
1381 /* When the last line of the window is scanned backwards
1382 under bidi iteration, we could be duped into thinking
1383 that we have passed CHARPOS, when in fact move_it_to
1384 simply stopped short of CHARPOS because it reached
1385 last_visible_y. To see if that's what happened, we call
1386 move_it_to again with a slightly larger vertical limit,
1387 and see if it actually moved vertically; if it did, we
1388 didn't really reach CHARPOS, which is beyond window end. */
1389 struct it save_it = it;
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1393
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = 0;
1398
1399 it = save_it;
1400 }
1401 if (visible_p)
1402 {
1403 if (it_method == GET_FROM_DISPLAY_VECTOR)
1404 {
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1410 {
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1427 {
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, 1);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1438 }
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1443 {
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1446 }
1447 }
1448 }
1449 else if (IT_CHARPOS (it) != charpos)
1450 {
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 int replacing_spec_p;
1456 bool newline_in_string
1457 = (STRINGP (string)
1458 && memchr (SDATA (string), '\n', SBYTES (string)));
1459
1460 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1461 replacing_spec_p
1462 = (!NILP (spec)
1463 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1464 charpos, FRAME_WINDOW_P (it.f)));
1465 /* The tricky code below is needed because there's a
1466 discrepancy between move_it_to and how we set cursor
1467 when PT is at the beginning of a portion of text
1468 covered by a display property or an overlay with a
1469 display property, or the display line ends in a
1470 newline from a display string. move_it_to will stop
1471 _after_ such display strings, whereas
1472 set_cursor_from_row conspires with cursor_row_p to
1473 place the cursor on the first glyph produced from the
1474 display string. */
1475
1476 /* We have overshoot PT because it is covered by a
1477 display property that replaces the text it covers.
1478 If the string includes embedded newlines, we are also
1479 in the wrong display line. Backtrack to the correct
1480 line, where the display property begins. */
1481 if (replacing_spec_p)
1482 {
1483 Lisp_Object startpos, endpos;
1484 EMACS_INT start, end;
1485 struct it it3;
1486 int it3_moved;
1487
1488 /* Find the first and the last buffer positions
1489 covered by the display string. */
1490 endpos =
1491 Fnext_single_char_property_change (cpos, Qdisplay,
1492 Qnil, Qnil);
1493 startpos =
1494 Fprevious_single_char_property_change (endpos, Qdisplay,
1495 Qnil, Qnil);
1496 start = XFASTINT (startpos);
1497 end = XFASTINT (endpos);
1498 /* Move to the last buffer position before the
1499 display property. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1502 /* Move forward one more line if the position before
1503 the display string is a newline or if it is the
1504 rightmost character on a line that is
1505 continued or word-wrapped. */
1506 if (it3.method == GET_FROM_BUFFER
1507 && (it3.c == '\n'
1508 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1509 move_it_by_lines (&it3, 1);
1510 else if (move_it_in_display_line_to (&it3, -1,
1511 it3.current_x
1512 + it3.pixel_width,
1513 MOVE_TO_X)
1514 == MOVE_LINE_CONTINUED)
1515 {
1516 move_it_by_lines (&it3, 1);
1517 /* When we are under word-wrap, the #$@%!
1518 move_it_by_lines moves 2 lines, so we need to
1519 fix that up. */
1520 if (it3.line_wrap == WORD_WRAP)
1521 move_it_by_lines (&it3, -1);
1522 }
1523
1524 /* Record the vertical coordinate of the display
1525 line where we wound up. */
1526 top_y = it3.current_y;
1527 if (it3.bidi_p)
1528 {
1529 /* When characters are reordered for display,
1530 the character displayed to the left of the
1531 display string could be _after_ the display
1532 property in the logical order. Use the
1533 smallest vertical position of these two. */
1534 start_display (&it3, w, top);
1535 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1536 if (it3.current_y < top_y)
1537 top_y = it3.current_y;
1538 }
1539 /* Move from the top of the window to the beginning
1540 of the display line where the display string
1541 begins. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1544 /* If it3_moved stays zero after the 'while' loop
1545 below, that means we already were at a newline
1546 before the loop (e.g., the display string begins
1547 with a newline), so we don't need to (and cannot)
1548 inspect the glyphs of it3.glyph_row, because
1549 PRODUCE_GLYPHS will not produce anything for a
1550 newline, and thus it3.glyph_row stays at its
1551 stale content it got at top of the window. */
1552 it3_moved = 0;
1553 /* Finally, advance the iterator until we hit the
1554 first display element whose character position is
1555 CHARPOS, or until the first newline from the
1556 display string, which signals the end of the
1557 display line. */
1558 while (get_next_display_element (&it3))
1559 {
1560 PRODUCE_GLYPHS (&it3);
1561 if (IT_CHARPOS (it3) == charpos
1562 || ITERATOR_AT_END_OF_LINE_P (&it3))
1563 break;
1564 it3_moved = 1;
1565 set_iterator_to_next (&it3, 0);
1566 }
1567 top_x = it3.current_x - it3.pixel_width;
1568 /* Normally, we would exit the above loop because we
1569 found the display element whose character
1570 position is CHARPOS. For the contingency that we
1571 didn't, and stopped at the first newline from the
1572 display string, move back over the glyphs
1573 produced from the string, until we find the
1574 rightmost glyph not from the string. */
1575 if (it3_moved
1576 && newline_in_string
1577 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1578 {
1579 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1580 + it3.glyph_row->used[TEXT_AREA];
1581
1582 while (EQ ((g - 1)->object, string))
1583 {
1584 --g;
1585 top_x -= g->pixel_width;
1586 }
1587 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA]);
1589 }
1590 }
1591 }
1592
1593 *x = top_x;
1594 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1595 *rtop = max (0, window_top_y - top_y);
1596 *rbot = max (0, bottom_y - it.last_visible_y);
1597 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1598 - max (top_y, window_top_y)));
1599 *vpos = it.vpos;
1600 }
1601 }
1602 else
1603 {
1604 /* We were asked to provide info about WINDOW_END. */
1605 struct it it2;
1606 void *it2data = NULL;
1607
1608 SAVE_IT (it2, it, it2data);
1609 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1610 move_it_by_lines (&it, 1);
1611 if (charpos < IT_CHARPOS (it)
1612 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1613 {
1614 visible_p = 1;
1615 RESTORE_IT (&it2, &it2, it2data);
1616 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1617 *x = it2.current_x;
1618 *y = it2.current_y + it2.max_ascent - it2.ascent;
1619 *rtop = max (0, -it2.current_y);
1620 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1621 - it.last_visible_y));
1622 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1623 it.last_visible_y)
1624 - max (it2.current_y,
1625 WINDOW_HEADER_LINE_HEIGHT (w))));
1626 *vpos = it2.vpos;
1627 }
1628 else
1629 bidi_unshelve_cache (it2data, 1);
1630 }
1631 bidi_unshelve_cache (itdata, 0);
1632
1633 if (old_buffer)
1634 set_buffer_internal_1 (old_buffer);
1635
1636 if (visible_p && w->hscroll > 0)
1637 *x -=
1638 window_hscroll_limited (w, WINDOW_XFRAME (w))
1639 * WINDOW_FRAME_COLUMN_WIDTH (w);
1640
1641 #if 0
1642 /* Debugging code. */
1643 if (visible_p)
1644 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1645 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1646 else
1647 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1648 #endif
1649
1650 return visible_p;
1651 }
1652
1653
1654 /* Return the next character from STR. Return in *LEN the length of
1655 the character. This is like STRING_CHAR_AND_LENGTH but never
1656 returns an invalid character. If we find one, we return a `?', but
1657 with the length of the invalid character. */
1658
1659 static int
1660 string_char_and_length (const unsigned char *str, int *len)
1661 {
1662 int c;
1663
1664 c = STRING_CHAR_AND_LENGTH (str, *len);
1665 if (!CHAR_VALID_P (c))
1666 /* We may not change the length here because other places in Emacs
1667 don't use this function, i.e. they silently accept invalid
1668 characters. */
1669 c = '?';
1670
1671 return c;
1672 }
1673
1674
1675
1676 /* Given a position POS containing a valid character and byte position
1677 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1678
1679 static struct text_pos
1680 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1681 {
1682 eassert (STRINGP (string) && nchars >= 0);
1683
1684 if (STRING_MULTIBYTE (string))
1685 {
1686 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1687 int len;
1688
1689 while (nchars--)
1690 {
1691 string_char_and_length (p, &len);
1692 p += len;
1693 CHARPOS (pos) += 1;
1694 BYTEPOS (pos) += len;
1695 }
1696 }
1697 else
1698 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1699
1700 return pos;
1701 }
1702
1703
1704 /* Value is the text position, i.e. character and byte position,
1705 for character position CHARPOS in STRING. */
1706
1707 static struct text_pos
1708 string_pos (ptrdiff_t charpos, Lisp_Object string)
1709 {
1710 struct text_pos pos;
1711 eassert (STRINGP (string));
1712 eassert (charpos >= 0);
1713 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1714 return pos;
1715 }
1716
1717
1718 /* Value is a text position, i.e. character and byte position, for
1719 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1720 means recognize multibyte characters. */
1721
1722 static struct text_pos
1723 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1724 {
1725 struct text_pos pos;
1726
1727 eassert (s != NULL);
1728 eassert (charpos >= 0);
1729
1730 if (multibyte_p)
1731 {
1732 int len;
1733
1734 SET_TEXT_POS (pos, 0, 0);
1735 while (charpos--)
1736 {
1737 string_char_and_length ((const unsigned char *) s, &len);
1738 s += len;
1739 CHARPOS (pos) += 1;
1740 BYTEPOS (pos) += len;
1741 }
1742 }
1743 else
1744 SET_TEXT_POS (pos, charpos, charpos);
1745
1746 return pos;
1747 }
1748
1749
1750 /* Value is the number of characters in C string S. MULTIBYTE_P
1751 non-zero means recognize multibyte characters. */
1752
1753 static ptrdiff_t
1754 number_of_chars (const char *s, bool multibyte_p)
1755 {
1756 ptrdiff_t nchars;
1757
1758 if (multibyte_p)
1759 {
1760 ptrdiff_t rest = strlen (s);
1761 int len;
1762 const unsigned char *p = (const unsigned char *) s;
1763
1764 for (nchars = 0; rest > 0; ++nchars)
1765 {
1766 string_char_and_length (p, &len);
1767 rest -= len, p += len;
1768 }
1769 }
1770 else
1771 nchars = strlen (s);
1772
1773 return nchars;
1774 }
1775
1776
1777 /* Compute byte position NEWPOS->bytepos corresponding to
1778 NEWPOS->charpos. POS is a known position in string STRING.
1779 NEWPOS->charpos must be >= POS.charpos. */
1780
1781 static void
1782 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1783 {
1784 eassert (STRINGP (string));
1785 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1786
1787 if (STRING_MULTIBYTE (string))
1788 *newpos = string_pos_nchars_ahead (pos, string,
1789 CHARPOS (*newpos) - CHARPOS (pos));
1790 else
1791 BYTEPOS (*newpos) = CHARPOS (*newpos);
1792 }
1793
1794 /* EXPORT:
1795 Return an estimation of the pixel height of mode or header lines on
1796 frame F. FACE_ID specifies what line's height to estimate. */
1797
1798 int
1799 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1800 {
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1803 {
1804 int height = FONT_HEIGHT (FRAME_FONT (f));
1805
1806 /* This function is called so early when Emacs starts that the face
1807 cache and mode line face are not yet initialized. */
1808 if (FRAME_FACE_CACHE (f))
1809 {
1810 struct face *face = FACE_FROM_ID (f, face_id);
1811 if (face)
1812 {
1813 if (face->font)
1814 height = FONT_HEIGHT (face->font);
1815 if (face->box_line_width > 0)
1816 height += 2 * face->box_line_width;
1817 }
1818 }
1819
1820 return height;
1821 }
1822 #endif
1823
1824 return 1;
1825 }
1826
1827 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1828 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1829 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1830 not force the value into range. */
1831
1832 void
1833 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1834 int *x, int *y, NativeRectangle *bounds, int noclip)
1835 {
1836
1837 #ifdef HAVE_WINDOW_SYSTEM
1838 if (FRAME_WINDOW_P (f))
1839 {
1840 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1841 even for negative values. */
1842 if (pix_x < 0)
1843 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1844 if (pix_y < 0)
1845 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1846
1847 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1848 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1849
1850 if (bounds)
1851 STORE_NATIVE_RECT (*bounds,
1852 FRAME_COL_TO_PIXEL_X (f, pix_x),
1853 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1854 FRAME_COLUMN_WIDTH (f) - 1,
1855 FRAME_LINE_HEIGHT (f) - 1);
1856
1857 if (!noclip)
1858 {
1859 if (pix_x < 0)
1860 pix_x = 0;
1861 else if (pix_x > FRAME_TOTAL_COLS (f))
1862 pix_x = FRAME_TOTAL_COLS (f);
1863
1864 if (pix_y < 0)
1865 pix_y = 0;
1866 else if (pix_y > FRAME_LINES (f))
1867 pix_y = FRAME_LINES (f);
1868 }
1869 }
1870 #endif
1871
1872 *x = pix_x;
1873 *y = pix_y;
1874 }
1875
1876
1877 /* Find the glyph under window-relative coordinates X/Y in window W.
1878 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1879 strings. Return in *HPOS and *VPOS the row and column number of
1880 the glyph found. Return in *AREA the glyph area containing X.
1881 Value is a pointer to the glyph found or null if X/Y is not on
1882 text, or we can't tell because W's current matrix is not up to
1883 date. */
1884
1885 static
1886 struct glyph *
1887 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1888 int *dx, int *dy, int *area)
1889 {
1890 struct glyph *glyph, *end;
1891 struct glyph_row *row = NULL;
1892 int x0, i;
1893
1894 /* Find row containing Y. Give up if some row is not enabled. */
1895 for (i = 0; i < w->current_matrix->nrows; ++i)
1896 {
1897 row = MATRIX_ROW (w->current_matrix, i);
1898 if (!row->enabled_p)
1899 return NULL;
1900 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1901 break;
1902 }
1903
1904 *vpos = i;
1905 *hpos = 0;
1906
1907 /* Give up if Y is not in the window. */
1908 if (i == w->current_matrix->nrows)
1909 return NULL;
1910
1911 /* Get the glyph area containing X. */
1912 if (w->pseudo_window_p)
1913 {
1914 *area = TEXT_AREA;
1915 x0 = 0;
1916 }
1917 else
1918 {
1919 if (x < window_box_left_offset (w, TEXT_AREA))
1920 {
1921 *area = LEFT_MARGIN_AREA;
1922 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1923 }
1924 else if (x < window_box_right_offset (w, TEXT_AREA))
1925 {
1926 *area = TEXT_AREA;
1927 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1928 }
1929 else
1930 {
1931 *area = RIGHT_MARGIN_AREA;
1932 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1933 }
1934 }
1935
1936 /* Find glyph containing X. */
1937 glyph = row->glyphs[*area];
1938 end = glyph + row->used[*area];
1939 x -= x0;
1940 while (glyph < end && x >= glyph->pixel_width)
1941 {
1942 x -= glyph->pixel_width;
1943 ++glyph;
1944 }
1945
1946 if (glyph == end)
1947 return NULL;
1948
1949 if (dx)
1950 {
1951 *dx = x;
1952 *dy = y - (row->y + row->ascent - glyph->ascent);
1953 }
1954
1955 *hpos = glyph - row->glyphs[*area];
1956 return glyph;
1957 }
1958
1959 /* Convert frame-relative x/y to coordinates relative to window W.
1960 Takes pseudo-windows into account. */
1961
1962 static void
1963 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1964 {
1965 if (w->pseudo_window_p)
1966 {
1967 /* A pseudo-window is always full-width, and starts at the
1968 left edge of the frame, plus a frame border. */
1969 struct frame *f = XFRAME (w->frame);
1970 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1971 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1972 }
1973 else
1974 {
1975 *x -= WINDOW_LEFT_EDGE_X (w);
1976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1977 }
1978 }
1979
1980 #ifdef HAVE_WINDOW_SYSTEM
1981
1982 /* EXPORT:
1983 Return in RECTS[] at most N clipping rectangles for glyph string S.
1984 Return the number of stored rectangles. */
1985
1986 int
1987 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1988 {
1989 XRectangle r;
1990
1991 if (n <= 0)
1992 return 0;
1993
1994 if (s->row->full_width_p)
1995 {
1996 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1997 r.x = WINDOW_LEFT_EDGE_X (s->w);
1998 r.width = WINDOW_TOTAL_WIDTH (s->w);
1999
2000 /* Unless displaying a mode or menu bar line, which are always
2001 fully visible, clip to the visible part of the row. */
2002 if (s->w->pseudo_window_p)
2003 r.height = s->row->visible_height;
2004 else
2005 r.height = s->height;
2006 }
2007 else
2008 {
2009 /* This is a text line that may be partially visible. */
2010 r.x = window_box_left (s->w, s->area);
2011 r.width = window_box_width (s->w, s->area);
2012 r.height = s->row->visible_height;
2013 }
2014
2015 if (s->clip_head)
2016 if (r.x < s->clip_head->x)
2017 {
2018 if (r.width >= s->clip_head->x - r.x)
2019 r.width -= s->clip_head->x - r.x;
2020 else
2021 r.width = 0;
2022 r.x = s->clip_head->x;
2023 }
2024 if (s->clip_tail)
2025 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2026 {
2027 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2028 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2029 else
2030 r.width = 0;
2031 }
2032
2033 /* If S draws overlapping rows, it's sufficient to use the top and
2034 bottom of the window for clipping because this glyph string
2035 intentionally draws over other lines. */
2036 if (s->for_overlaps)
2037 {
2038 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2039 r.height = window_text_bottom_y (s->w) - r.y;
2040
2041 /* Alas, the above simple strategy does not work for the
2042 environments with anti-aliased text: if the same text is
2043 drawn onto the same place multiple times, it gets thicker.
2044 If the overlap we are processing is for the erased cursor, we
2045 take the intersection with the rectangle of the cursor. */
2046 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2047 {
2048 XRectangle rc, r_save = r;
2049
2050 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2051 rc.y = s->w->phys_cursor.y;
2052 rc.width = s->w->phys_cursor_width;
2053 rc.height = s->w->phys_cursor_height;
2054
2055 x_intersect_rectangles (&r_save, &rc, &r);
2056 }
2057 }
2058 else
2059 {
2060 /* Don't use S->y for clipping because it doesn't take partially
2061 visible lines into account. For example, it can be negative for
2062 partially visible lines at the top of a window. */
2063 if (!s->row->full_width_p
2064 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2065 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2066 else
2067 r.y = max (0, s->row->y);
2068 }
2069
2070 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2071
2072 /* If drawing the cursor, don't let glyph draw outside its
2073 advertised boundaries. Cleartype does this under some circumstances. */
2074 if (s->hl == DRAW_CURSOR)
2075 {
2076 struct glyph *glyph = s->first_glyph;
2077 int height, max_y;
2078
2079 if (s->x > r.x)
2080 {
2081 r.width -= s->x - r.x;
2082 r.x = s->x;
2083 }
2084 r.width = min (r.width, glyph->pixel_width);
2085
2086 /* If r.y is below window bottom, ensure that we still see a cursor. */
2087 height = min (glyph->ascent + glyph->descent,
2088 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2089 max_y = window_text_bottom_y (s->w) - height;
2090 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2091 if (s->ybase - glyph->ascent > max_y)
2092 {
2093 r.y = max_y;
2094 r.height = height;
2095 }
2096 else
2097 {
2098 /* Don't draw cursor glyph taller than our actual glyph. */
2099 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2100 if (height < r.height)
2101 {
2102 max_y = r.y + r.height;
2103 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2104 r.height = min (max_y - r.y, height);
2105 }
2106 }
2107 }
2108
2109 if (s->row->clip)
2110 {
2111 XRectangle r_save = r;
2112
2113 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2114 r.width = 0;
2115 }
2116
2117 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2118 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2119 {
2120 #ifdef CONVERT_FROM_XRECT
2121 CONVERT_FROM_XRECT (r, *rects);
2122 #else
2123 *rects = r;
2124 #endif
2125 return 1;
2126 }
2127 else
2128 {
2129 /* If we are processing overlapping and allowed to return
2130 multiple clipping rectangles, we exclude the row of the glyph
2131 string from the clipping rectangle. This is to avoid drawing
2132 the same text on the environment with anti-aliasing. */
2133 #ifdef CONVERT_FROM_XRECT
2134 XRectangle rs[2];
2135 #else
2136 XRectangle *rs = rects;
2137 #endif
2138 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2139
2140 if (s->for_overlaps & OVERLAPS_PRED)
2141 {
2142 rs[i] = r;
2143 if (r.y + r.height > row_y)
2144 {
2145 if (r.y < row_y)
2146 rs[i].height = row_y - r.y;
2147 else
2148 rs[i].height = 0;
2149 }
2150 i++;
2151 }
2152 if (s->for_overlaps & OVERLAPS_SUCC)
2153 {
2154 rs[i] = r;
2155 if (r.y < row_y + s->row->visible_height)
2156 {
2157 if (r.y + r.height > row_y + s->row->visible_height)
2158 {
2159 rs[i].y = row_y + s->row->visible_height;
2160 rs[i].height = r.y + r.height - rs[i].y;
2161 }
2162 else
2163 rs[i].height = 0;
2164 }
2165 i++;
2166 }
2167
2168 n = i;
2169 #ifdef CONVERT_FROM_XRECT
2170 for (i = 0; i < n; i++)
2171 CONVERT_FROM_XRECT (rs[i], rects[i]);
2172 #endif
2173 return n;
2174 }
2175 }
2176
2177 /* EXPORT:
2178 Return in *NR the clipping rectangle for glyph string S. */
2179
2180 void
2181 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2182 {
2183 get_glyph_string_clip_rects (s, nr, 1);
2184 }
2185
2186
2187 /* EXPORT:
2188 Return the position and height of the phys cursor in window W.
2189 Set w->phys_cursor_width to width of phys cursor.
2190 */
2191
2192 void
2193 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2194 struct glyph *glyph, int *xp, int *yp, int *heightp)
2195 {
2196 struct frame *f = XFRAME (WINDOW_FRAME (w));
2197 int x, y, wd, h, h0, y0;
2198
2199 /* Compute the width of the rectangle to draw. If on a stretch
2200 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2201 rectangle as wide as the glyph, but use a canonical character
2202 width instead. */
2203 wd = glyph->pixel_width - 1;
2204 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2205 wd++; /* Why? */
2206 #endif
2207
2208 x = w->phys_cursor.x;
2209 if (x < 0)
2210 {
2211 wd += x;
2212 x = 0;
2213 }
2214
2215 if (glyph->type == STRETCH_GLYPH
2216 && !x_stretch_cursor_p)
2217 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2218 w->phys_cursor_width = wd;
2219
2220 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2221
2222 /* If y is below window bottom, ensure that we still see a cursor. */
2223 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2224
2225 h = max (h0, glyph->ascent + glyph->descent);
2226 h0 = min (h0, glyph->ascent + glyph->descent);
2227
2228 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2229 if (y < y0)
2230 {
2231 h = max (h - (y0 - y) + 1, h0);
2232 y = y0 - 1;
2233 }
2234 else
2235 {
2236 y0 = window_text_bottom_y (w) - h0;
2237 if (y > y0)
2238 {
2239 h += y - y0;
2240 y = y0;
2241 }
2242 }
2243
2244 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2245 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2246 *heightp = h;
2247 }
2248
2249 /*
2250 * Remember which glyph the mouse is over.
2251 */
2252
2253 void
2254 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2255 {
2256 Lisp_Object window;
2257 struct window *w;
2258 struct glyph_row *r, *gr, *end_row;
2259 enum window_part part;
2260 enum glyph_row_area area;
2261 int x, y, width, height;
2262
2263 /* Try to determine frame pixel position and size of the glyph under
2264 frame pixel coordinates X/Y on frame F. */
2265
2266 if (!f->glyphs_initialized_p
2267 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2268 NILP (window)))
2269 {
2270 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2271 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2272 goto virtual_glyph;
2273 }
2274
2275 w = XWINDOW (window);
2276 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2277 height = WINDOW_FRAME_LINE_HEIGHT (w);
2278
2279 x = window_relative_x_coord (w, part, gx);
2280 y = gy - WINDOW_TOP_EDGE_Y (w);
2281
2282 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2283 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2284
2285 if (w->pseudo_window_p)
2286 {
2287 area = TEXT_AREA;
2288 part = ON_MODE_LINE; /* Don't adjust margin. */
2289 goto text_glyph;
2290 }
2291
2292 switch (part)
2293 {
2294 case ON_LEFT_MARGIN:
2295 area = LEFT_MARGIN_AREA;
2296 goto text_glyph;
2297
2298 case ON_RIGHT_MARGIN:
2299 area = RIGHT_MARGIN_AREA;
2300 goto text_glyph;
2301
2302 case ON_HEADER_LINE:
2303 case ON_MODE_LINE:
2304 gr = (part == ON_HEADER_LINE
2305 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2306 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2307 gy = gr->y;
2308 area = TEXT_AREA;
2309 goto text_glyph_row_found;
2310
2311 case ON_TEXT:
2312 area = TEXT_AREA;
2313
2314 text_glyph:
2315 gr = 0; gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2318 {
2319 gr = r; gy = r->y;
2320 break;
2321 }
2322
2323 text_glyph_row_found:
2324 if (gr && gy <= y)
2325 {
2326 struct glyph *g = gr->glyphs[area];
2327 struct glyph *end = g + gr->used[area];
2328
2329 height = gr->height;
2330 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2331 if (gx + g->pixel_width > x)
2332 break;
2333
2334 if (g < end)
2335 {
2336 if (g->type == IMAGE_GLYPH)
2337 {
2338 /* Don't remember when mouse is over image, as
2339 image may have hot-spots. */
2340 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2341 return;
2342 }
2343 width = g->pixel_width;
2344 }
2345 else
2346 {
2347 /* Use nominal char spacing at end of line. */
2348 x -= gx;
2349 gx += (x / width) * width;
2350 }
2351
2352 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2353 gx += window_box_left_offset (w, area);
2354 }
2355 else
2356 {
2357 /* Use nominal line height at end of window. */
2358 gx = (x / width) * width;
2359 y -= gy;
2360 gy += (y / height) * height;
2361 }
2362 break;
2363
2364 case ON_LEFT_FRINGE:
2365 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2366 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2367 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2368 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2369 goto row_glyph;
2370
2371 case ON_RIGHT_FRINGE:
2372 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2373 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2374 : window_box_right_offset (w, TEXT_AREA));
2375 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2376 goto row_glyph;
2377
2378 case ON_SCROLL_BAR:
2379 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2380 ? 0
2381 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2382 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2383 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2384 : 0)));
2385 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2386
2387 row_glyph:
2388 gr = 0, gy = 0;
2389 for (; r <= end_row && r->enabled_p; ++r)
2390 if (r->y + r->height > y)
2391 {
2392 gr = r; gy = r->y;
2393 break;
2394 }
2395
2396 if (gr && gy <= y)
2397 height = gr->height;
2398 else
2399 {
2400 /* Use nominal line height at end of window. */
2401 y -= gy;
2402 gy += (y / height) * height;
2403 }
2404 break;
2405
2406 default:
2407 ;
2408 virtual_glyph:
2409 /* If there is no glyph under the mouse, then we divide the screen
2410 into a grid of the smallest glyph in the frame, and use that
2411 as our "glyph". */
2412
2413 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2414 round down even for negative values. */
2415 if (gx < 0)
2416 gx -= width - 1;
2417 if (gy < 0)
2418 gy -= height - 1;
2419
2420 gx = (gx / width) * width;
2421 gy = (gy / height) * height;
2422
2423 goto store_rect;
2424 }
2425
2426 gx += WINDOW_LEFT_EDGE_X (w);
2427 gy += WINDOW_TOP_EDGE_Y (w);
2428
2429 store_rect:
2430 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2431
2432 /* Visible feedback for debugging. */
2433 #if 0
2434 #if HAVE_X_WINDOWS
2435 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2436 f->output_data.x->normal_gc,
2437 gx, gy, width, height);
2438 #endif
2439 #endif
2440 }
2441
2442
2443 #endif /* HAVE_WINDOW_SYSTEM */
2444
2445 static void
2446 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2447 {
2448 eassert (w);
2449 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2450 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2451 w->window_end_vpos
2452 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2453 }
2454
2455 /***********************************************************************
2456 Lisp form evaluation
2457 ***********************************************************************/
2458
2459 /* Error handler for safe_eval and safe_call. */
2460
2461 static Lisp_Object
2462 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2463 {
2464 add_to_log ("Error during redisplay: %S signaled %S",
2465 Flist (nargs, args), arg);
2466 return Qnil;
2467 }
2468
2469 /* Call function FUNC with the rest of NARGS - 1 arguments
2470 following. Return the result, or nil if something went
2471 wrong. Prevent redisplay during the evaluation. */
2472
2473 Lisp_Object
2474 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2475 {
2476 Lisp_Object val;
2477
2478 if (inhibit_eval_during_redisplay)
2479 val = Qnil;
2480 else
2481 {
2482 va_list ap;
2483 ptrdiff_t i;
2484 ptrdiff_t count = SPECPDL_INDEX ();
2485 struct gcpro gcpro1;
2486 Lisp_Object *args = alloca (nargs * word_size);
2487
2488 args[0] = func;
2489 va_start (ap, func);
2490 for (i = 1; i < nargs; i++)
2491 args[i] = va_arg (ap, Lisp_Object);
2492 va_end (ap);
2493
2494 GCPRO1 (args[0]);
2495 gcpro1.nvars = nargs;
2496 specbind (Qinhibit_redisplay, Qt);
2497 /* Use Qt to ensure debugger does not run,
2498 so there is no possibility of wanting to redisplay. */
2499 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2500 safe_eval_handler);
2501 UNGCPRO;
2502 val = unbind_to (count, val);
2503 }
2504
2505 return val;
2506 }
2507
2508
2509 /* Call function FN with one argument ARG.
2510 Return the result, or nil if something went wrong. */
2511
2512 Lisp_Object
2513 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2514 {
2515 return safe_call (2, fn, arg);
2516 }
2517
2518 static Lisp_Object Qeval;
2519
2520 Lisp_Object
2521 safe_eval (Lisp_Object sexpr)
2522 {
2523 return safe_call1 (Qeval, sexpr);
2524 }
2525
2526 /* Call function FN with two arguments ARG1 and ARG2.
2527 Return the result, or nil if something went wrong. */
2528
2529 Lisp_Object
2530 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2531 {
2532 return safe_call (3, fn, arg1, arg2);
2533 }
2534
2535
2536 \f
2537 /***********************************************************************
2538 Debugging
2539 ***********************************************************************/
2540
2541 #if 0
2542
2543 /* Define CHECK_IT to perform sanity checks on iterators.
2544 This is for debugging. It is too slow to do unconditionally. */
2545
2546 static void
2547 check_it (struct it *it)
2548 {
2549 if (it->method == GET_FROM_STRING)
2550 {
2551 eassert (STRINGP (it->string));
2552 eassert (IT_STRING_CHARPOS (*it) >= 0);
2553 }
2554 else
2555 {
2556 eassert (IT_STRING_CHARPOS (*it) < 0);
2557 if (it->method == GET_FROM_BUFFER)
2558 {
2559 /* Check that character and byte positions agree. */
2560 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2561 }
2562 }
2563
2564 if (it->dpvec)
2565 eassert (it->current.dpvec_index >= 0);
2566 else
2567 eassert (it->current.dpvec_index < 0);
2568 }
2569
2570 #define CHECK_IT(IT) check_it ((IT))
2571
2572 #else /* not 0 */
2573
2574 #define CHECK_IT(IT) (void) 0
2575
2576 #endif /* not 0 */
2577
2578
2579 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2580
2581 /* Check that the window end of window W is what we expect it
2582 to be---the last row in the current matrix displaying text. */
2583
2584 static void
2585 check_window_end (struct window *w)
2586 {
2587 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2588 {
2589 struct glyph_row *row;
2590 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2591 !row->enabled_p
2592 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2593 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2594 }
2595 }
2596
2597 #define CHECK_WINDOW_END(W) check_window_end ((W))
2598
2599 #else
2600
2601 #define CHECK_WINDOW_END(W) (void) 0
2602
2603 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2604
2605 /* Return mark position if current buffer has the region of non-zero length,
2606 or -1 otherwise. */
2607
2608 static ptrdiff_t
2609 markpos_of_region (void)
2610 {
2611 if (!NILP (Vtransient_mark_mode)
2612 && !NILP (BVAR (current_buffer, mark_active))
2613 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2614 {
2615 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2616
2617 if (markpos != PT)
2618 return markpos;
2619 }
2620 return -1;
2621 }
2622
2623 /***********************************************************************
2624 Iterator initialization
2625 ***********************************************************************/
2626
2627 /* Initialize IT for displaying current_buffer in window W, starting
2628 at character position CHARPOS. CHARPOS < 0 means that no buffer
2629 position is specified which is useful when the iterator is assigned
2630 a position later. BYTEPOS is the byte position corresponding to
2631 CHARPOS.
2632
2633 If ROW is not null, calls to produce_glyphs with IT as parameter
2634 will produce glyphs in that row.
2635
2636 BASE_FACE_ID is the id of a base face to use. It must be one of
2637 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2638 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2639 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2640
2641 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2642 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2643 will be initialized to use the corresponding mode line glyph row of
2644 the desired matrix of W. */
2645
2646 void
2647 init_iterator (struct it *it, struct window *w,
2648 ptrdiff_t charpos, ptrdiff_t bytepos,
2649 struct glyph_row *row, enum face_id base_face_id)
2650 {
2651 ptrdiff_t markpos;
2652 enum face_id remapped_base_face_id = base_face_id;
2653
2654 /* Some precondition checks. */
2655 eassert (w != NULL && it != NULL);
2656 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2657 && charpos <= ZV));
2658
2659 /* If face attributes have been changed since the last redisplay,
2660 free realized faces now because they depend on face definitions
2661 that might have changed. Don't free faces while there might be
2662 desired matrices pending which reference these faces. */
2663 if (face_change_count && !inhibit_free_realized_faces)
2664 {
2665 face_change_count = 0;
2666 free_all_realized_faces (Qnil);
2667 }
2668
2669 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2670 if (! NILP (Vface_remapping_alist))
2671 remapped_base_face_id
2672 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2673
2674 /* Use one of the mode line rows of W's desired matrix if
2675 appropriate. */
2676 if (row == NULL)
2677 {
2678 if (base_face_id == MODE_LINE_FACE_ID
2679 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2680 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2681 else if (base_face_id == HEADER_LINE_FACE_ID)
2682 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2683 }
2684
2685 /* Clear IT. */
2686 memset (it, 0, sizeof *it);
2687 it->current.overlay_string_index = -1;
2688 it->current.dpvec_index = -1;
2689 it->base_face_id = remapped_base_face_id;
2690 it->string = Qnil;
2691 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2692 it->paragraph_embedding = L2R;
2693 it->bidi_it.string.lstring = Qnil;
2694 it->bidi_it.string.s = NULL;
2695 it->bidi_it.string.bufpos = 0;
2696 it->bidi_it.w = w;
2697
2698 /* The window in which we iterate over current_buffer: */
2699 XSETWINDOW (it->window, w);
2700 it->w = w;
2701 it->f = XFRAME (w->frame);
2702
2703 it->cmp_it.id = -1;
2704
2705 /* Extra space between lines (on window systems only). */
2706 if (base_face_id == DEFAULT_FACE_ID
2707 && FRAME_WINDOW_P (it->f))
2708 {
2709 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2710 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2711 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2712 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2713 * FRAME_LINE_HEIGHT (it->f));
2714 else if (it->f->extra_line_spacing > 0)
2715 it->extra_line_spacing = it->f->extra_line_spacing;
2716 it->max_extra_line_spacing = 0;
2717 }
2718
2719 /* If realized faces have been removed, e.g. because of face
2720 attribute changes of named faces, recompute them. When running
2721 in batch mode, the face cache of the initial frame is null. If
2722 we happen to get called, make a dummy face cache. */
2723 if (FRAME_FACE_CACHE (it->f) == NULL)
2724 init_frame_faces (it->f);
2725 if (FRAME_FACE_CACHE (it->f)->used == 0)
2726 recompute_basic_faces (it->f);
2727
2728 /* Current value of the `slice', `space-width', and 'height' properties. */
2729 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2730 it->space_width = Qnil;
2731 it->font_height = Qnil;
2732 it->override_ascent = -1;
2733
2734 /* Are control characters displayed as `^C'? */
2735 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2736
2737 /* -1 means everything between a CR and the following line end
2738 is invisible. >0 means lines indented more than this value are
2739 invisible. */
2740 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2741 ? (clip_to_bounds
2742 (-1, XINT (BVAR (current_buffer, selective_display)),
2743 PTRDIFF_MAX))
2744 : (!NILP (BVAR (current_buffer, selective_display))
2745 ? -1 : 0));
2746 it->selective_display_ellipsis_p
2747 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2748
2749 /* Display table to use. */
2750 it->dp = window_display_table (w);
2751
2752 /* Are multibyte characters enabled in current_buffer? */
2753 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2754
2755 /* If visible region is of non-zero length, set IT->region_beg_charpos
2756 and IT->region_end_charpos to the start and end of a visible region
2757 in window IT->w. Set both to -1 to indicate no region. */
2758 markpos = markpos_of_region ();
2759 if (markpos >= 0
2760 /* Maybe highlight only in selected window. */
2761 && (/* Either show region everywhere. */
2762 highlight_nonselected_windows
2763 /* Or show region in the selected window. */
2764 || w == XWINDOW (selected_window)
2765 /* Or show the region if we are in the mini-buffer and W is
2766 the window the mini-buffer refers to. */
2767 || (MINI_WINDOW_P (XWINDOW (selected_window))
2768 && WINDOWP (minibuf_selected_window)
2769 && w == XWINDOW (minibuf_selected_window))))
2770 {
2771 it->region_beg_charpos = min (PT, markpos);
2772 it->region_end_charpos = max (PT, markpos);
2773 }
2774 else
2775 it->region_beg_charpos = it->region_end_charpos = -1;
2776
2777 /* Get the position at which the redisplay_end_trigger hook should
2778 be run, if it is to be run at all. */
2779 if (MARKERP (w->redisplay_end_trigger)
2780 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2781 it->redisplay_end_trigger_charpos
2782 = marker_position (w->redisplay_end_trigger);
2783 else if (INTEGERP (w->redisplay_end_trigger))
2784 it->redisplay_end_trigger_charpos =
2785 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2786
2787 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2788
2789 /* Are lines in the display truncated? */
2790 if (base_face_id != DEFAULT_FACE_ID
2791 || it->w->hscroll
2792 || (! WINDOW_FULL_WIDTH_P (it->w)
2793 && ((!NILP (Vtruncate_partial_width_windows)
2794 && !INTEGERP (Vtruncate_partial_width_windows))
2795 || (INTEGERP (Vtruncate_partial_width_windows)
2796 && (WINDOW_TOTAL_COLS (it->w)
2797 < XINT (Vtruncate_partial_width_windows))))))
2798 it->line_wrap = TRUNCATE;
2799 else if (NILP (BVAR (current_buffer, truncate_lines)))
2800 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2801 ? WINDOW_WRAP : WORD_WRAP;
2802 else
2803 it->line_wrap = TRUNCATE;
2804
2805 /* Get dimensions of truncation and continuation glyphs. These are
2806 displayed as fringe bitmaps under X, but we need them for such
2807 frames when the fringes are turned off. But leave the dimensions
2808 zero for tooltip frames, as these glyphs look ugly there and also
2809 sabotage calculations of tooltip dimensions in x-show-tip. */
2810 #ifdef HAVE_WINDOW_SYSTEM
2811 if (!(FRAME_WINDOW_P (it->f)
2812 && FRAMEP (tip_frame)
2813 && it->f == XFRAME (tip_frame)))
2814 #endif
2815 {
2816 if (it->line_wrap == TRUNCATE)
2817 {
2818 /* We will need the truncation glyph. */
2819 eassert (it->glyph_row == NULL);
2820 produce_special_glyphs (it, IT_TRUNCATION);
2821 it->truncation_pixel_width = it->pixel_width;
2822 }
2823 else
2824 {
2825 /* We will need the continuation glyph. */
2826 eassert (it->glyph_row == NULL);
2827 produce_special_glyphs (it, IT_CONTINUATION);
2828 it->continuation_pixel_width = it->pixel_width;
2829 }
2830 }
2831
2832 /* Reset these values to zero because the produce_special_glyphs
2833 above has changed them. */
2834 it->pixel_width = it->ascent = it->descent = 0;
2835 it->phys_ascent = it->phys_descent = 0;
2836
2837 /* Set this after getting the dimensions of truncation and
2838 continuation glyphs, so that we don't produce glyphs when calling
2839 produce_special_glyphs, above. */
2840 it->glyph_row = row;
2841 it->area = TEXT_AREA;
2842
2843 /* Forget any previous info about this row being reversed. */
2844 if (it->glyph_row)
2845 it->glyph_row->reversed_p = 0;
2846
2847 /* Get the dimensions of the display area. The display area
2848 consists of the visible window area plus a horizontally scrolled
2849 part to the left of the window. All x-values are relative to the
2850 start of this total display area. */
2851 if (base_face_id != DEFAULT_FACE_ID)
2852 {
2853 /* Mode lines, menu bar in terminal frames. */
2854 it->first_visible_x = 0;
2855 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2856 }
2857 else
2858 {
2859 it->first_visible_x =
2860 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2861 it->last_visible_x = (it->first_visible_x
2862 + window_box_width (w, TEXT_AREA));
2863
2864 /* If we truncate lines, leave room for the truncation glyph(s) at
2865 the right margin. Otherwise, leave room for the continuation
2866 glyph(s). Done only if the window has no fringes. Since we
2867 don't know at this point whether there will be any R2L lines in
2868 the window, we reserve space for truncation/continuation glyphs
2869 even if only one of the fringes is absent. */
2870 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2871 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2872 {
2873 if (it->line_wrap == TRUNCATE)
2874 it->last_visible_x -= it->truncation_pixel_width;
2875 else
2876 it->last_visible_x -= it->continuation_pixel_width;
2877 }
2878
2879 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2880 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2881 }
2882
2883 /* Leave room for a border glyph. */
2884 if (!FRAME_WINDOW_P (it->f)
2885 && !WINDOW_RIGHTMOST_P (it->w))
2886 it->last_visible_x -= 1;
2887
2888 it->last_visible_y = window_text_bottom_y (w);
2889
2890 /* For mode lines and alike, arrange for the first glyph having a
2891 left box line if the face specifies a box. */
2892 if (base_face_id != DEFAULT_FACE_ID)
2893 {
2894 struct face *face;
2895
2896 it->face_id = remapped_base_face_id;
2897
2898 /* If we have a boxed mode line, make the first character appear
2899 with a left box line. */
2900 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2901 if (face->box != FACE_NO_BOX)
2902 it->start_of_box_run_p = 1;
2903 }
2904
2905 /* If a buffer position was specified, set the iterator there,
2906 getting overlays and face properties from that position. */
2907 if (charpos >= BUF_BEG (current_buffer))
2908 {
2909 it->end_charpos = ZV;
2910 eassert (charpos == BYTE_TO_CHAR (bytepos));
2911 IT_CHARPOS (*it) = charpos;
2912 IT_BYTEPOS (*it) = bytepos;
2913
2914 /* We will rely on `reseat' to set this up properly, via
2915 handle_face_prop. */
2916 it->face_id = it->base_face_id;
2917
2918 it->start = it->current;
2919 /* Do we need to reorder bidirectional text? Not if this is a
2920 unibyte buffer: by definition, none of the single-byte
2921 characters are strong R2L, so no reordering is needed. And
2922 bidi.c doesn't support unibyte buffers anyway. Also, don't
2923 reorder while we are loading loadup.el, since the tables of
2924 character properties needed for reordering are not yet
2925 available. */
2926 it->bidi_p =
2927 NILP (Vpurify_flag)
2928 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2929 && it->multibyte_p;
2930
2931 /* If we are to reorder bidirectional text, init the bidi
2932 iterator. */
2933 if (it->bidi_p)
2934 {
2935 /* Note the paragraph direction that this buffer wants to
2936 use. */
2937 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2938 Qleft_to_right))
2939 it->paragraph_embedding = L2R;
2940 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2941 Qright_to_left))
2942 it->paragraph_embedding = R2L;
2943 else
2944 it->paragraph_embedding = NEUTRAL_DIR;
2945 bidi_unshelve_cache (NULL, 0);
2946 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2947 &it->bidi_it);
2948 }
2949
2950 /* Compute faces etc. */
2951 reseat (it, it->current.pos, 1);
2952 }
2953
2954 CHECK_IT (it);
2955 }
2956
2957
2958 /* Initialize IT for the display of window W with window start POS. */
2959
2960 void
2961 start_display (struct it *it, struct window *w, struct text_pos pos)
2962 {
2963 struct glyph_row *row;
2964 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2965
2966 row = w->desired_matrix->rows + first_vpos;
2967 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2968 it->first_vpos = first_vpos;
2969
2970 /* Don't reseat to previous visible line start if current start
2971 position is in a string or image. */
2972 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2973 {
2974 int start_at_line_beg_p;
2975 int first_y = it->current_y;
2976
2977 /* If window start is not at a line start, skip forward to POS to
2978 get the correct continuation lines width. */
2979 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2980 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2981 if (!start_at_line_beg_p)
2982 {
2983 int new_x;
2984
2985 reseat_at_previous_visible_line_start (it);
2986 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2987
2988 new_x = it->current_x + it->pixel_width;
2989
2990 /* If lines are continued, this line may end in the middle
2991 of a multi-glyph character (e.g. a control character
2992 displayed as \003, or in the middle of an overlay
2993 string). In this case move_it_to above will not have
2994 taken us to the start of the continuation line but to the
2995 end of the continued line. */
2996 if (it->current_x > 0
2997 && it->line_wrap != TRUNCATE /* Lines are continued. */
2998 && (/* And glyph doesn't fit on the line. */
2999 new_x > it->last_visible_x
3000 /* Or it fits exactly and we're on a window
3001 system frame. */
3002 || (new_x == it->last_visible_x
3003 && FRAME_WINDOW_P (it->f)
3004 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3005 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3006 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3007 {
3008 if ((it->current.dpvec_index >= 0
3009 || it->current.overlay_string_index >= 0)
3010 /* If we are on a newline from a display vector or
3011 overlay string, then we are already at the end of
3012 a screen line; no need to go to the next line in
3013 that case, as this line is not really continued.
3014 (If we do go to the next line, C-e will not DTRT.) */
3015 && it->c != '\n')
3016 {
3017 set_iterator_to_next (it, 1);
3018 move_it_in_display_line_to (it, -1, -1, 0);
3019 }
3020
3021 it->continuation_lines_width += it->current_x;
3022 }
3023 /* If the character at POS is displayed via a display
3024 vector, move_it_to above stops at the final glyph of
3025 IT->dpvec. To make the caller redisplay that character
3026 again (a.k.a. start at POS), we need to reset the
3027 dpvec_index to the beginning of IT->dpvec. */
3028 else if (it->current.dpvec_index >= 0)
3029 it->current.dpvec_index = 0;
3030
3031 /* We're starting a new display line, not affected by the
3032 height of the continued line, so clear the appropriate
3033 fields in the iterator structure. */
3034 it->max_ascent = it->max_descent = 0;
3035 it->max_phys_ascent = it->max_phys_descent = 0;
3036
3037 it->current_y = first_y;
3038 it->vpos = 0;
3039 it->current_x = it->hpos = 0;
3040 }
3041 }
3042 }
3043
3044
3045 /* Return 1 if POS is a position in ellipses displayed for invisible
3046 text. W is the window we display, for text property lookup. */
3047
3048 static int
3049 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3050 {
3051 Lisp_Object prop, window;
3052 int ellipses_p = 0;
3053 ptrdiff_t charpos = CHARPOS (pos->pos);
3054
3055 /* If POS specifies a position in a display vector, this might
3056 be for an ellipsis displayed for invisible text. We won't
3057 get the iterator set up for delivering that ellipsis unless
3058 we make sure that it gets aware of the invisible text. */
3059 if (pos->dpvec_index >= 0
3060 && pos->overlay_string_index < 0
3061 && CHARPOS (pos->string_pos) < 0
3062 && charpos > BEGV
3063 && (XSETWINDOW (window, w),
3064 prop = Fget_char_property (make_number (charpos),
3065 Qinvisible, window),
3066 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3067 {
3068 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3069 window);
3070 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3071 }
3072
3073 return ellipses_p;
3074 }
3075
3076
3077 /* Initialize IT for stepping through current_buffer in window W,
3078 starting at position POS that includes overlay string and display
3079 vector/ control character translation position information. Value
3080 is zero if there are overlay strings with newlines at POS. */
3081
3082 static int
3083 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3084 {
3085 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3086 int i, overlay_strings_with_newlines = 0;
3087
3088 /* If POS specifies a position in a display vector, this might
3089 be for an ellipsis displayed for invisible text. We won't
3090 get the iterator set up for delivering that ellipsis unless
3091 we make sure that it gets aware of the invisible text. */
3092 if (in_ellipses_for_invisible_text_p (pos, w))
3093 {
3094 --charpos;
3095 bytepos = 0;
3096 }
3097
3098 /* Keep in mind: the call to reseat in init_iterator skips invisible
3099 text, so we might end up at a position different from POS. This
3100 is only a problem when POS is a row start after a newline and an
3101 overlay starts there with an after-string, and the overlay has an
3102 invisible property. Since we don't skip invisible text in
3103 display_line and elsewhere immediately after consuming the
3104 newline before the row start, such a POS will not be in a string,
3105 but the call to init_iterator below will move us to the
3106 after-string. */
3107 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3108
3109 /* This only scans the current chunk -- it should scan all chunks.
3110 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3111 to 16 in 22.1 to make this a lesser problem. */
3112 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3113 {
3114 const char *s = SSDATA (it->overlay_strings[i]);
3115 const char *e = s + SBYTES (it->overlay_strings[i]);
3116
3117 while (s < e && *s != '\n')
3118 ++s;
3119
3120 if (s < e)
3121 {
3122 overlay_strings_with_newlines = 1;
3123 break;
3124 }
3125 }
3126
3127 /* If position is within an overlay string, set up IT to the right
3128 overlay string. */
3129 if (pos->overlay_string_index >= 0)
3130 {
3131 int relative_index;
3132
3133 /* If the first overlay string happens to have a `display'
3134 property for an image, the iterator will be set up for that
3135 image, and we have to undo that setup first before we can
3136 correct the overlay string index. */
3137 if (it->method == GET_FROM_IMAGE)
3138 pop_it (it);
3139
3140 /* We already have the first chunk of overlay strings in
3141 IT->overlay_strings. Load more until the one for
3142 pos->overlay_string_index is in IT->overlay_strings. */
3143 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3144 {
3145 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3146 it->current.overlay_string_index = 0;
3147 while (n--)
3148 {
3149 load_overlay_strings (it, 0);
3150 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3151 }
3152 }
3153
3154 it->current.overlay_string_index = pos->overlay_string_index;
3155 relative_index = (it->current.overlay_string_index
3156 % OVERLAY_STRING_CHUNK_SIZE);
3157 it->string = it->overlay_strings[relative_index];
3158 eassert (STRINGP (it->string));
3159 it->current.string_pos = pos->string_pos;
3160 it->method = GET_FROM_STRING;
3161 it->end_charpos = SCHARS (it->string);
3162 /* Set up the bidi iterator for this overlay string. */
3163 if (it->bidi_p)
3164 {
3165 it->bidi_it.string.lstring = it->string;
3166 it->bidi_it.string.s = NULL;
3167 it->bidi_it.string.schars = SCHARS (it->string);
3168 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3169 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3170 it->bidi_it.string.unibyte = !it->multibyte_p;
3171 it->bidi_it.w = it->w;
3172 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3173 FRAME_WINDOW_P (it->f), &it->bidi_it);
3174
3175 /* Synchronize the state of the bidi iterator with
3176 pos->string_pos. For any string position other than
3177 zero, this will be done automagically when we resume
3178 iteration over the string and get_visually_first_element
3179 is called. But if string_pos is zero, and the string is
3180 to be reordered for display, we need to resync manually,
3181 since it could be that the iteration state recorded in
3182 pos ended at string_pos of 0 moving backwards in string. */
3183 if (CHARPOS (pos->string_pos) == 0)
3184 {
3185 get_visually_first_element (it);
3186 if (IT_STRING_CHARPOS (*it) != 0)
3187 do {
3188 /* Paranoia. */
3189 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3190 bidi_move_to_visually_next (&it->bidi_it);
3191 } while (it->bidi_it.charpos != 0);
3192 }
3193 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3194 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3195 }
3196 }
3197
3198 if (CHARPOS (pos->string_pos) >= 0)
3199 {
3200 /* Recorded position is not in an overlay string, but in another
3201 string. This can only be a string from a `display' property.
3202 IT should already be filled with that string. */
3203 it->current.string_pos = pos->string_pos;
3204 eassert (STRINGP (it->string));
3205 if (it->bidi_p)
3206 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3207 FRAME_WINDOW_P (it->f), &it->bidi_it);
3208 }
3209
3210 /* Restore position in display vector translations, control
3211 character translations or ellipses. */
3212 if (pos->dpvec_index >= 0)
3213 {
3214 if (it->dpvec == NULL)
3215 get_next_display_element (it);
3216 eassert (it->dpvec && it->current.dpvec_index == 0);
3217 it->current.dpvec_index = pos->dpvec_index;
3218 }
3219
3220 CHECK_IT (it);
3221 return !overlay_strings_with_newlines;
3222 }
3223
3224
3225 /* Initialize IT for stepping through current_buffer in window W
3226 starting at ROW->start. */
3227
3228 static void
3229 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3230 {
3231 init_from_display_pos (it, w, &row->start);
3232 it->start = row->start;
3233 it->continuation_lines_width = row->continuation_lines_width;
3234 CHECK_IT (it);
3235 }
3236
3237
3238 /* Initialize IT for stepping through current_buffer in window W
3239 starting in the line following ROW, i.e. starting at ROW->end.
3240 Value is zero if there are overlay strings with newlines at ROW's
3241 end position. */
3242
3243 static int
3244 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3245 {
3246 int success = 0;
3247
3248 if (init_from_display_pos (it, w, &row->end))
3249 {
3250 if (row->continued_p)
3251 it->continuation_lines_width
3252 = row->continuation_lines_width + row->pixel_width;
3253 CHECK_IT (it);
3254 success = 1;
3255 }
3256
3257 return success;
3258 }
3259
3260
3261
3262 \f
3263 /***********************************************************************
3264 Text properties
3265 ***********************************************************************/
3266
3267 /* Called when IT reaches IT->stop_charpos. Handle text property and
3268 overlay changes. Set IT->stop_charpos to the next position where
3269 to stop. */
3270
3271 static void
3272 handle_stop (struct it *it)
3273 {
3274 enum prop_handled handled;
3275 int handle_overlay_change_p;
3276 struct props *p;
3277
3278 it->dpvec = NULL;
3279 it->current.dpvec_index = -1;
3280 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3281 it->ignore_overlay_strings_at_pos_p = 0;
3282 it->ellipsis_p = 0;
3283
3284 /* Use face of preceding text for ellipsis (if invisible) */
3285 if (it->selective_display_ellipsis_p)
3286 it->saved_face_id = it->face_id;
3287
3288 do
3289 {
3290 handled = HANDLED_NORMALLY;
3291
3292 /* Call text property handlers. */
3293 for (p = it_props; p->handler; ++p)
3294 {
3295 handled = p->handler (it);
3296
3297 if (handled == HANDLED_RECOMPUTE_PROPS)
3298 break;
3299 else if (handled == HANDLED_RETURN)
3300 {
3301 /* We still want to show before and after strings from
3302 overlays even if the actual buffer text is replaced. */
3303 if (!handle_overlay_change_p
3304 || it->sp > 1
3305 /* Don't call get_overlay_strings_1 if we already
3306 have overlay strings loaded, because doing so
3307 will load them again and push the iterator state
3308 onto the stack one more time, which is not
3309 expected by the rest of the code that processes
3310 overlay strings. */
3311 || (it->current.overlay_string_index < 0
3312 ? !get_overlay_strings_1 (it, 0, 0)
3313 : 0))
3314 {
3315 if (it->ellipsis_p)
3316 setup_for_ellipsis (it, 0);
3317 /* When handling a display spec, we might load an
3318 empty string. In that case, discard it here. We
3319 used to discard it in handle_single_display_spec,
3320 but that causes get_overlay_strings_1, above, to
3321 ignore overlay strings that we must check. */
3322 if (STRINGP (it->string) && !SCHARS (it->string))
3323 pop_it (it);
3324 return;
3325 }
3326 else if (STRINGP (it->string) && !SCHARS (it->string))
3327 pop_it (it);
3328 else
3329 {
3330 it->ignore_overlay_strings_at_pos_p = 1;
3331 it->string_from_display_prop_p = 0;
3332 it->from_disp_prop_p = 0;
3333 handle_overlay_change_p = 0;
3334 }
3335 handled = HANDLED_RECOMPUTE_PROPS;
3336 break;
3337 }
3338 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3339 handle_overlay_change_p = 0;
3340 }
3341
3342 if (handled != HANDLED_RECOMPUTE_PROPS)
3343 {
3344 /* Don't check for overlay strings below when set to deliver
3345 characters from a display vector. */
3346 if (it->method == GET_FROM_DISPLAY_VECTOR)
3347 handle_overlay_change_p = 0;
3348
3349 /* Handle overlay changes.
3350 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3351 if it finds overlays. */
3352 if (handle_overlay_change_p)
3353 handled = handle_overlay_change (it);
3354 }
3355
3356 if (it->ellipsis_p)
3357 {
3358 setup_for_ellipsis (it, 0);
3359 break;
3360 }
3361 }
3362 while (handled == HANDLED_RECOMPUTE_PROPS);
3363
3364 /* Determine where to stop next. */
3365 if (handled == HANDLED_NORMALLY)
3366 compute_stop_pos (it);
3367 }
3368
3369
3370 /* Compute IT->stop_charpos from text property and overlay change
3371 information for IT's current position. */
3372
3373 static void
3374 compute_stop_pos (struct it *it)
3375 {
3376 register INTERVAL iv, next_iv;
3377 Lisp_Object object, limit, position;
3378 ptrdiff_t charpos, bytepos;
3379
3380 if (STRINGP (it->string))
3381 {
3382 /* Strings are usually short, so don't limit the search for
3383 properties. */
3384 it->stop_charpos = it->end_charpos;
3385 object = it->string;
3386 limit = Qnil;
3387 charpos = IT_STRING_CHARPOS (*it);
3388 bytepos = IT_STRING_BYTEPOS (*it);
3389 }
3390 else
3391 {
3392 ptrdiff_t pos;
3393
3394 /* If end_charpos is out of range for some reason, such as a
3395 misbehaving display function, rationalize it (Bug#5984). */
3396 if (it->end_charpos > ZV)
3397 it->end_charpos = ZV;
3398 it->stop_charpos = it->end_charpos;
3399
3400 /* If next overlay change is in front of the current stop pos
3401 (which is IT->end_charpos), stop there. Note: value of
3402 next_overlay_change is point-max if no overlay change
3403 follows. */
3404 charpos = IT_CHARPOS (*it);
3405 bytepos = IT_BYTEPOS (*it);
3406 pos = next_overlay_change (charpos);
3407 if (pos < it->stop_charpos)
3408 it->stop_charpos = pos;
3409
3410 /* If showing the region, we have to stop at the region
3411 start or end because the face might change there. */
3412 if (it->region_beg_charpos > 0)
3413 {
3414 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3415 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3416 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3417 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3418 }
3419
3420 /* Set up variables for computing the stop position from text
3421 property changes. */
3422 XSETBUFFER (object, current_buffer);
3423 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3424 }
3425
3426 /* Get the interval containing IT's position. Value is a null
3427 interval if there isn't such an interval. */
3428 position = make_number (charpos);
3429 iv = validate_interval_range (object, &position, &position, 0);
3430 if (iv)
3431 {
3432 Lisp_Object values_here[LAST_PROP_IDX];
3433 struct props *p;
3434
3435 /* Get properties here. */
3436 for (p = it_props; p->handler; ++p)
3437 values_here[p->idx] = textget (iv->plist, *p->name);
3438
3439 /* Look for an interval following iv that has different
3440 properties. */
3441 for (next_iv = next_interval (iv);
3442 (next_iv
3443 && (NILP (limit)
3444 || XFASTINT (limit) > next_iv->position));
3445 next_iv = next_interval (next_iv))
3446 {
3447 for (p = it_props; p->handler; ++p)
3448 {
3449 Lisp_Object new_value;
3450
3451 new_value = textget (next_iv->plist, *p->name);
3452 if (!EQ (values_here[p->idx], new_value))
3453 break;
3454 }
3455
3456 if (p->handler)
3457 break;
3458 }
3459
3460 if (next_iv)
3461 {
3462 if (INTEGERP (limit)
3463 && next_iv->position >= XFASTINT (limit))
3464 /* No text property change up to limit. */
3465 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3466 else
3467 /* Text properties change in next_iv. */
3468 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3469 }
3470 }
3471
3472 if (it->cmp_it.id < 0)
3473 {
3474 ptrdiff_t stoppos = it->end_charpos;
3475
3476 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3477 stoppos = -1;
3478 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3479 stoppos, it->string);
3480 }
3481
3482 eassert (STRINGP (it->string)
3483 || (it->stop_charpos >= BEGV
3484 && it->stop_charpos >= IT_CHARPOS (*it)));
3485 }
3486
3487
3488 /* Return the position of the next overlay change after POS in
3489 current_buffer. Value is point-max if no overlay change
3490 follows. This is like `next-overlay-change' but doesn't use
3491 xmalloc. */
3492
3493 static ptrdiff_t
3494 next_overlay_change (ptrdiff_t pos)
3495 {
3496 ptrdiff_t i, noverlays;
3497 ptrdiff_t endpos;
3498 Lisp_Object *overlays;
3499
3500 /* Get all overlays at the given position. */
3501 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3502
3503 /* If any of these overlays ends before endpos,
3504 use its ending point instead. */
3505 for (i = 0; i < noverlays; ++i)
3506 {
3507 Lisp_Object oend;
3508 ptrdiff_t oendpos;
3509
3510 oend = OVERLAY_END (overlays[i]);
3511 oendpos = OVERLAY_POSITION (oend);
3512 endpos = min (endpos, oendpos);
3513 }
3514
3515 return endpos;
3516 }
3517
3518 /* How many characters forward to search for a display property or
3519 display string. Searching too far forward makes the bidi display
3520 sluggish, especially in small windows. */
3521 #define MAX_DISP_SCAN 250
3522
3523 /* Return the character position of a display string at or after
3524 position specified by POSITION. If no display string exists at or
3525 after POSITION, return ZV. A display string is either an overlay
3526 with `display' property whose value is a string, or a `display'
3527 text property whose value is a string. STRING is data about the
3528 string to iterate; if STRING->lstring is nil, we are iterating a
3529 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3530 on a GUI frame. DISP_PROP is set to zero if we searched
3531 MAX_DISP_SCAN characters forward without finding any display
3532 strings, non-zero otherwise. It is set to 2 if the display string
3533 uses any kind of `(space ...)' spec that will produce a stretch of
3534 white space in the text area. */
3535 ptrdiff_t
3536 compute_display_string_pos (struct text_pos *position,
3537 struct bidi_string_data *string,
3538 struct window *w,
3539 int frame_window_p, int *disp_prop)
3540 {
3541 /* OBJECT = nil means current buffer. */
3542 Lisp_Object object, object1;
3543 Lisp_Object pos, spec, limpos;
3544 int string_p = (string && (STRINGP (string->lstring) || string->s));
3545 ptrdiff_t eob = string_p ? string->schars : ZV;
3546 ptrdiff_t begb = string_p ? 0 : BEGV;
3547 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3548 ptrdiff_t lim =
3549 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3550 struct text_pos tpos;
3551 int rv = 0;
3552
3553 if (string && STRINGP (string->lstring))
3554 object1 = object = string->lstring;
3555 else if (w && !string_p)
3556 {
3557 XSETWINDOW (object, w);
3558 object1 = Qnil;
3559 }
3560 else
3561 object1 = object = Qnil;
3562
3563 *disp_prop = 1;
3564
3565 if (charpos >= eob
3566 /* We don't support display properties whose values are strings
3567 that have display string properties. */
3568 || string->from_disp_str
3569 /* C strings cannot have display properties. */
3570 || (string->s && !STRINGP (object)))
3571 {
3572 *disp_prop = 0;
3573 return eob;
3574 }
3575
3576 /* If the character at CHARPOS is where the display string begins,
3577 return CHARPOS. */
3578 pos = make_number (charpos);
3579 if (STRINGP (object))
3580 bufpos = string->bufpos;
3581 else
3582 bufpos = charpos;
3583 tpos = *position;
3584 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3585 && (charpos <= begb
3586 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3587 object),
3588 spec))
3589 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3590 frame_window_p)))
3591 {
3592 if (rv == 2)
3593 *disp_prop = 2;
3594 return charpos;
3595 }
3596
3597 /* Look forward for the first character with a `display' property
3598 that will replace the underlying text when displayed. */
3599 limpos = make_number (lim);
3600 do {
3601 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3602 CHARPOS (tpos) = XFASTINT (pos);
3603 if (CHARPOS (tpos) >= lim)
3604 {
3605 *disp_prop = 0;
3606 break;
3607 }
3608 if (STRINGP (object))
3609 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3610 else
3611 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3612 spec = Fget_char_property (pos, Qdisplay, object);
3613 if (!STRINGP (object))
3614 bufpos = CHARPOS (tpos);
3615 } while (NILP (spec)
3616 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3617 bufpos, frame_window_p)));
3618 if (rv == 2)
3619 *disp_prop = 2;
3620
3621 return CHARPOS (tpos);
3622 }
3623
3624 /* Return the character position of the end of the display string that
3625 started at CHARPOS. If there's no display string at CHARPOS,
3626 return -1. A display string is either an overlay with `display'
3627 property whose value is a string or a `display' text property whose
3628 value is a string. */
3629 ptrdiff_t
3630 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3631 {
3632 /* OBJECT = nil means current buffer. */
3633 Lisp_Object object =
3634 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3635 Lisp_Object pos = make_number (charpos);
3636 ptrdiff_t eob =
3637 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3638
3639 if (charpos >= eob || (string->s && !STRINGP (object)))
3640 return eob;
3641
3642 /* It could happen that the display property or overlay was removed
3643 since we found it in compute_display_string_pos above. One way
3644 this can happen is if JIT font-lock was called (through
3645 handle_fontified_prop), and jit-lock-functions remove text
3646 properties or overlays from the portion of buffer that includes
3647 CHARPOS. Muse mode is known to do that, for example. In this
3648 case, we return -1 to the caller, to signal that no display
3649 string is actually present at CHARPOS. See bidi_fetch_char for
3650 how this is handled.
3651
3652 An alternative would be to never look for display properties past
3653 it->stop_charpos. But neither compute_display_string_pos nor
3654 bidi_fetch_char that calls it know or care where the next
3655 stop_charpos is. */
3656 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3657 return -1;
3658
3659 /* Look forward for the first character where the `display' property
3660 changes. */
3661 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3662
3663 return XFASTINT (pos);
3664 }
3665
3666
3667 \f
3668 /***********************************************************************
3669 Fontification
3670 ***********************************************************************/
3671
3672 /* Handle changes in the `fontified' property of the current buffer by
3673 calling hook functions from Qfontification_functions to fontify
3674 regions of text. */
3675
3676 static enum prop_handled
3677 handle_fontified_prop (struct it *it)
3678 {
3679 Lisp_Object prop, pos;
3680 enum prop_handled handled = HANDLED_NORMALLY;
3681
3682 if (!NILP (Vmemory_full))
3683 return handled;
3684
3685 /* Get the value of the `fontified' property at IT's current buffer
3686 position. (The `fontified' property doesn't have a special
3687 meaning in strings.) If the value is nil, call functions from
3688 Qfontification_functions. */
3689 if (!STRINGP (it->string)
3690 && it->s == NULL
3691 && !NILP (Vfontification_functions)
3692 && !NILP (Vrun_hooks)
3693 && (pos = make_number (IT_CHARPOS (*it)),
3694 prop = Fget_char_property (pos, Qfontified, Qnil),
3695 /* Ignore the special cased nil value always present at EOB since
3696 no amount of fontifying will be able to change it. */
3697 NILP (prop) && IT_CHARPOS (*it) < Z))
3698 {
3699 ptrdiff_t count = SPECPDL_INDEX ();
3700 Lisp_Object val;
3701 struct buffer *obuf = current_buffer;
3702 int begv = BEGV, zv = ZV;
3703 int old_clip_changed = current_buffer->clip_changed;
3704
3705 val = Vfontification_functions;
3706 specbind (Qfontification_functions, Qnil);
3707
3708 eassert (it->end_charpos == ZV);
3709
3710 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3711 safe_call1 (val, pos);
3712 else
3713 {
3714 Lisp_Object fns, fn;
3715 struct gcpro gcpro1, gcpro2;
3716
3717 fns = Qnil;
3718 GCPRO2 (val, fns);
3719
3720 for (; CONSP (val); val = XCDR (val))
3721 {
3722 fn = XCAR (val);
3723
3724 if (EQ (fn, Qt))
3725 {
3726 /* A value of t indicates this hook has a local
3727 binding; it means to run the global binding too.
3728 In a global value, t should not occur. If it
3729 does, we must ignore it to avoid an endless
3730 loop. */
3731 for (fns = Fdefault_value (Qfontification_functions);
3732 CONSP (fns);
3733 fns = XCDR (fns))
3734 {
3735 fn = XCAR (fns);
3736 if (!EQ (fn, Qt))
3737 safe_call1 (fn, pos);
3738 }
3739 }
3740 else
3741 safe_call1 (fn, pos);
3742 }
3743
3744 UNGCPRO;
3745 }
3746
3747 unbind_to (count, Qnil);
3748
3749 /* Fontification functions routinely call `save-restriction'.
3750 Normally, this tags clip_changed, which can confuse redisplay
3751 (see discussion in Bug#6671). Since we don't perform any
3752 special handling of fontification changes in the case where
3753 `save-restriction' isn't called, there's no point doing so in
3754 this case either. So, if the buffer's restrictions are
3755 actually left unchanged, reset clip_changed. */
3756 if (obuf == current_buffer)
3757 {
3758 if (begv == BEGV && zv == ZV)
3759 current_buffer->clip_changed = old_clip_changed;
3760 }
3761 /* There isn't much we can reasonably do to protect against
3762 misbehaving fontification, but here's a fig leaf. */
3763 else if (BUFFER_LIVE_P (obuf))
3764 set_buffer_internal_1 (obuf);
3765
3766 /* The fontification code may have added/removed text.
3767 It could do even a lot worse, but let's at least protect against
3768 the most obvious case where only the text past `pos' gets changed',
3769 as is/was done in grep.el where some escapes sequences are turned
3770 into face properties (bug#7876). */
3771 it->end_charpos = ZV;
3772
3773 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3774 something. This avoids an endless loop if they failed to
3775 fontify the text for which reason ever. */
3776 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3777 handled = HANDLED_RECOMPUTE_PROPS;
3778 }
3779
3780 return handled;
3781 }
3782
3783
3784 \f
3785 /***********************************************************************
3786 Faces
3787 ***********************************************************************/
3788
3789 /* Set up iterator IT from face properties at its current position.
3790 Called from handle_stop. */
3791
3792 static enum prop_handled
3793 handle_face_prop (struct it *it)
3794 {
3795 int new_face_id;
3796 ptrdiff_t next_stop;
3797
3798 if (!STRINGP (it->string))
3799 {
3800 new_face_id
3801 = face_at_buffer_position (it->w,
3802 IT_CHARPOS (*it),
3803 it->region_beg_charpos,
3804 it->region_end_charpos,
3805 &next_stop,
3806 (IT_CHARPOS (*it)
3807 + TEXT_PROP_DISTANCE_LIMIT),
3808 0, it->base_face_id);
3809
3810 /* Is this a start of a run of characters with box face?
3811 Caveat: this can be called for a freshly initialized
3812 iterator; face_id is -1 in this case. We know that the new
3813 face will not change until limit, i.e. if the new face has a
3814 box, all characters up to limit will have one. But, as
3815 usual, we don't know whether limit is really the end. */
3816 if (new_face_id != it->face_id)
3817 {
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 /* If it->face_id is -1, old_face below will be NULL, see
3820 the definition of FACE_FROM_ID. This will happen if this
3821 is the initial call that gets the face. */
3822 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3823
3824 /* If the value of face_id of the iterator is -1, we have to
3825 look in front of IT's position and see whether there is a
3826 face there that's different from new_face_id. */
3827 if (!old_face && IT_CHARPOS (*it) > BEG)
3828 {
3829 int prev_face_id = face_before_it_pos (it);
3830
3831 old_face = FACE_FROM_ID (it->f, prev_face_id);
3832 }
3833
3834 /* If the new face has a box, but the old face does not,
3835 this is the start of a run of characters with box face,
3836 i.e. this character has a shadow on the left side. */
3837 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3838 && (old_face == NULL || !old_face->box));
3839 it->face_box_p = new_face->box != FACE_NO_BOX;
3840 }
3841 }
3842 else
3843 {
3844 int base_face_id;
3845 ptrdiff_t bufpos;
3846 int i;
3847 Lisp_Object from_overlay
3848 = (it->current.overlay_string_index >= 0
3849 ? it->string_overlays[it->current.overlay_string_index
3850 % OVERLAY_STRING_CHUNK_SIZE]
3851 : Qnil);
3852
3853 /* See if we got to this string directly or indirectly from
3854 an overlay property. That includes the before-string or
3855 after-string of an overlay, strings in display properties
3856 provided by an overlay, their text properties, etc.
3857
3858 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3859 if (! NILP (from_overlay))
3860 for (i = it->sp - 1; i >= 0; i--)
3861 {
3862 if (it->stack[i].current.overlay_string_index >= 0)
3863 from_overlay
3864 = it->string_overlays[it->stack[i].current.overlay_string_index
3865 % OVERLAY_STRING_CHUNK_SIZE];
3866 else if (! NILP (it->stack[i].from_overlay))
3867 from_overlay = it->stack[i].from_overlay;
3868
3869 if (!NILP (from_overlay))
3870 break;
3871 }
3872
3873 if (! NILP (from_overlay))
3874 {
3875 bufpos = IT_CHARPOS (*it);
3876 /* For a string from an overlay, the base face depends
3877 only on text properties and ignores overlays. */
3878 base_face_id
3879 = face_for_overlay_string (it->w,
3880 IT_CHARPOS (*it),
3881 it->region_beg_charpos,
3882 it->region_end_charpos,
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3886 0,
3887 from_overlay);
3888 }
3889 else
3890 {
3891 bufpos = 0;
3892
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3905 }
3906
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 it->region_beg_charpos,
3912 it->region_end_charpos,
3913 &next_stop,
3914 base_face_id, 0);
3915
3916 /* Is this a start of a run of characters with box? Caveat:
3917 this can be called for a freshly allocated iterator; face_id
3918 is -1 is this case. We know that the new face will not
3919 change until the next check pos, i.e. if the new face has a
3920 box, all characters up to that position will have a
3921 box. But, as usual, we don't know whether that position
3922 is really the end. */
3923 if (new_face_id != it->face_id)
3924 {
3925 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3926 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3927
3928 /* If new face has a box but old face hasn't, this is the
3929 start of a run of characters with box, i.e. it has a
3930 shadow on the left side. */
3931 it->start_of_box_run_p
3932 = new_face->box && (old_face == NULL || !old_face->box);
3933 it->face_box_p = new_face->box != FACE_NO_BOX;
3934 }
3935 }
3936
3937 it->face_id = new_face_id;
3938 return HANDLED_NORMALLY;
3939 }
3940
3941
3942 /* Return the ID of the face ``underlying'' IT's current position,
3943 which is in a string. If the iterator is associated with a
3944 buffer, return the face at IT's current buffer position.
3945 Otherwise, use the iterator's base_face_id. */
3946
3947 static int
3948 underlying_face_id (struct it *it)
3949 {
3950 int face_id = it->base_face_id, i;
3951
3952 eassert (STRINGP (it->string));
3953
3954 for (i = it->sp - 1; i >= 0; --i)
3955 if (NILP (it->stack[i].string))
3956 face_id = it->stack[i].face_id;
3957
3958 return face_id;
3959 }
3960
3961
3962 /* Compute the face one character before or after the current position
3963 of IT, in the visual order. BEFORE_P non-zero means get the face
3964 in front (to the left in L2R paragraphs, to the right in R2L
3965 paragraphs) of IT's screen position. Value is the ID of the face. */
3966
3967 static int
3968 face_before_or_after_it_pos (struct it *it, int before_p)
3969 {
3970 int face_id, limit;
3971 ptrdiff_t next_check_charpos;
3972 struct it it_copy;
3973 void *it_copy_data = NULL;
3974
3975 eassert (it->s == NULL);
3976
3977 if (STRINGP (it->string))
3978 {
3979 ptrdiff_t bufpos, charpos;
3980 int base_face_id;
3981
3982 /* No face change past the end of the string (for the case
3983 we are padding with spaces). No face change before the
3984 string start. */
3985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3986 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3987 return it->face_id;
3988
3989 if (!it->bidi_p)
3990 {
3991 /* Set charpos to the position before or after IT's current
3992 position, in the logical order, which in the non-bidi
3993 case is the same as the visual order. */
3994 if (before_p)
3995 charpos = IT_STRING_CHARPOS (*it) - 1;
3996 else if (it->what == IT_COMPOSITION)
3997 /* For composition, we must check the character after the
3998 composition. */
3999 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4000 else
4001 charpos = IT_STRING_CHARPOS (*it) + 1;
4002 }
4003 else
4004 {
4005 if (before_p)
4006 {
4007 /* With bidi iteration, the character before the current
4008 in the visual order cannot be found by simple
4009 iteration, because "reverse" reordering is not
4010 supported. Instead, we need to use the move_it_*
4011 family of functions. */
4012 /* Ignore face changes before the first visible
4013 character on this display line. */
4014 if (it->current_x <= it->first_visible_x)
4015 return it->face_id;
4016 SAVE_IT (it_copy, *it, it_copy_data);
4017 /* Implementation note: Since move_it_in_display_line
4018 works in the iterator geometry, and thinks the first
4019 character is always the leftmost, even in R2L lines,
4020 we don't need to distinguish between the R2L and L2R
4021 cases here. */
4022 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4023 it_copy.current_x - 1, MOVE_TO_X);
4024 charpos = IT_STRING_CHARPOS (it_copy);
4025 RESTORE_IT (it, it, it_copy_data);
4026 }
4027 else
4028 {
4029 /* Set charpos to the string position of the character
4030 that comes after IT's current position in the visual
4031 order. */
4032 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4033
4034 it_copy = *it;
4035 while (n--)
4036 bidi_move_to_visually_next (&it_copy.bidi_it);
4037
4038 charpos = it_copy.bidi_it.charpos;
4039 }
4040 }
4041 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4042
4043 if (it->current.overlay_string_index >= 0)
4044 bufpos = IT_CHARPOS (*it);
4045 else
4046 bufpos = 0;
4047
4048 base_face_id = underlying_face_id (it);
4049
4050 /* Get the face for ASCII, or unibyte. */
4051 face_id = face_at_string_position (it->w,
4052 it->string,
4053 charpos,
4054 bufpos,
4055 it->region_beg_charpos,
4056 it->region_end_charpos,
4057 &next_check_charpos,
4058 base_face_id, 0);
4059
4060 /* Correct the face for charsets different from ASCII. Do it
4061 for the multibyte case only. The face returned above is
4062 suitable for unibyte text if IT->string is unibyte. */
4063 if (STRING_MULTIBYTE (it->string))
4064 {
4065 struct text_pos pos1 = string_pos (charpos, it->string);
4066 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4067 int c, len;
4068 struct face *face = FACE_FROM_ID (it->f, face_id);
4069
4070 c = string_char_and_length (p, &len);
4071 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4072 }
4073 }
4074 else
4075 {
4076 struct text_pos pos;
4077
4078 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4079 || (IT_CHARPOS (*it) <= BEGV && before_p))
4080 return it->face_id;
4081
4082 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4083 pos = it->current.pos;
4084
4085 if (!it->bidi_p)
4086 {
4087 if (before_p)
4088 DEC_TEXT_POS (pos, it->multibyte_p);
4089 else
4090 {
4091 if (it->what == IT_COMPOSITION)
4092 {
4093 /* For composition, we must check the position after
4094 the composition. */
4095 pos.charpos += it->cmp_it.nchars;
4096 pos.bytepos += it->len;
4097 }
4098 else
4099 INC_TEXT_POS (pos, it->multibyte_p);
4100 }
4101 }
4102 else
4103 {
4104 if (before_p)
4105 {
4106 /* With bidi iteration, the character before the current
4107 in the visual order cannot be found by simple
4108 iteration, because "reverse" reordering is not
4109 supported. Instead, we need to use the move_it_*
4110 family of functions. */
4111 /* Ignore face changes before the first visible
4112 character on this display line. */
4113 if (it->current_x <= it->first_visible_x)
4114 return it->face_id;
4115 SAVE_IT (it_copy, *it, it_copy_data);
4116 /* Implementation note: Since move_it_in_display_line
4117 works in the iterator geometry, and thinks the first
4118 character is always the leftmost, even in R2L lines,
4119 we don't need to distinguish between the R2L and L2R
4120 cases here. */
4121 move_it_in_display_line (&it_copy, ZV,
4122 it_copy.current_x - 1, MOVE_TO_X);
4123 pos = it_copy.current.pos;
4124 RESTORE_IT (it, it, it_copy_data);
4125 }
4126 else
4127 {
4128 /* Set charpos to the buffer position of the character
4129 that comes after IT's current position in the visual
4130 order. */
4131 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4132
4133 it_copy = *it;
4134 while (n--)
4135 bidi_move_to_visually_next (&it_copy.bidi_it);
4136
4137 SET_TEXT_POS (pos,
4138 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4139 }
4140 }
4141 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4142
4143 /* Determine face for CHARSET_ASCII, or unibyte. */
4144 face_id = face_at_buffer_position (it->w,
4145 CHARPOS (pos),
4146 it->region_beg_charpos,
4147 it->region_end_charpos,
4148 &next_check_charpos,
4149 limit, 0, -1);
4150
4151 /* Correct the face for charsets different from ASCII. Do it
4152 for the multibyte case only. The face returned above is
4153 suitable for unibyte text if current_buffer is unibyte. */
4154 if (it->multibyte_p)
4155 {
4156 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4157 struct face *face = FACE_FROM_ID (it->f, face_id);
4158 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4159 }
4160 }
4161
4162 return face_id;
4163 }
4164
4165
4166 \f
4167 /***********************************************************************
4168 Invisible text
4169 ***********************************************************************/
4170
4171 /* Set up iterator IT from invisible properties at its current
4172 position. Called from handle_stop. */
4173
4174 static enum prop_handled
4175 handle_invisible_prop (struct it *it)
4176 {
4177 enum prop_handled handled = HANDLED_NORMALLY;
4178 int invis_p;
4179 Lisp_Object prop;
4180
4181 if (STRINGP (it->string))
4182 {
4183 Lisp_Object end_charpos, limit, charpos;
4184
4185 /* Get the value of the invisible text property at the
4186 current position. Value will be nil if there is no such
4187 property. */
4188 charpos = make_number (IT_STRING_CHARPOS (*it));
4189 prop = Fget_text_property (charpos, Qinvisible, it->string);
4190 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4191
4192 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4193 {
4194 /* Record whether we have to display an ellipsis for the
4195 invisible text. */
4196 int display_ellipsis_p = (invis_p == 2);
4197 ptrdiff_t len, endpos;
4198
4199 handled = HANDLED_RECOMPUTE_PROPS;
4200
4201 /* Get the position at which the next visible text can be
4202 found in IT->string, if any. */
4203 endpos = len = SCHARS (it->string);
4204 XSETINT (limit, len);
4205 do
4206 {
4207 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4208 it->string, limit);
4209 if (INTEGERP (end_charpos))
4210 {
4211 endpos = XFASTINT (end_charpos);
4212 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4213 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4214 if (invis_p == 2)
4215 display_ellipsis_p = 1;
4216 }
4217 }
4218 while (invis_p && endpos < len);
4219
4220 if (display_ellipsis_p)
4221 it->ellipsis_p = 1;
4222
4223 if (endpos < len)
4224 {
4225 /* Text at END_CHARPOS is visible. Move IT there. */
4226 struct text_pos old;
4227 ptrdiff_t oldpos;
4228
4229 old = it->current.string_pos;
4230 oldpos = CHARPOS (old);
4231 if (it->bidi_p)
4232 {
4233 if (it->bidi_it.first_elt
4234 && it->bidi_it.charpos < SCHARS (it->string))
4235 bidi_paragraph_init (it->paragraph_embedding,
4236 &it->bidi_it, 1);
4237 /* Bidi-iterate out of the invisible text. */
4238 do
4239 {
4240 bidi_move_to_visually_next (&it->bidi_it);
4241 }
4242 while (oldpos <= it->bidi_it.charpos
4243 && it->bidi_it.charpos < endpos);
4244
4245 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4246 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4247 if (IT_CHARPOS (*it) >= endpos)
4248 it->prev_stop = endpos;
4249 }
4250 else
4251 {
4252 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4253 compute_string_pos (&it->current.string_pos, old, it->string);
4254 }
4255 }
4256 else
4257 {
4258 /* The rest of the string is invisible. If this is an
4259 overlay string, proceed with the next overlay string
4260 or whatever comes and return a character from there. */
4261 if (it->current.overlay_string_index >= 0
4262 && !display_ellipsis_p)
4263 {
4264 next_overlay_string (it);
4265 /* Don't check for overlay strings when we just
4266 finished processing them. */
4267 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4268 }
4269 else
4270 {
4271 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4272 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4273 }
4274 }
4275 }
4276 }
4277 else
4278 {
4279 ptrdiff_t newpos, next_stop, start_charpos, tem;
4280 Lisp_Object pos, overlay;
4281
4282 /* First of all, is there invisible text at this position? */
4283 tem = start_charpos = IT_CHARPOS (*it);
4284 pos = make_number (tem);
4285 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4286 &overlay);
4287 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4288
4289 /* If we are on invisible text, skip over it. */
4290 if (invis_p && start_charpos < it->end_charpos)
4291 {
4292 /* Record whether we have to display an ellipsis for the
4293 invisible text. */
4294 int display_ellipsis_p = invis_p == 2;
4295
4296 handled = HANDLED_RECOMPUTE_PROPS;
4297
4298 /* Loop skipping over invisible text. The loop is left at
4299 ZV or with IT on the first char being visible again. */
4300 do
4301 {
4302 /* Try to skip some invisible text. Return value is the
4303 position reached which can be equal to where we start
4304 if there is nothing invisible there. This skips both
4305 over invisible text properties and overlays with
4306 invisible property. */
4307 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4308
4309 /* If we skipped nothing at all we weren't at invisible
4310 text in the first place. If everything to the end of
4311 the buffer was skipped, end the loop. */
4312 if (newpos == tem || newpos >= ZV)
4313 invis_p = 0;
4314 else
4315 {
4316 /* We skipped some characters but not necessarily
4317 all there are. Check if we ended up on visible
4318 text. Fget_char_property returns the property of
4319 the char before the given position, i.e. if we
4320 get invis_p = 0, this means that the char at
4321 newpos is visible. */
4322 pos = make_number (newpos);
4323 prop = Fget_char_property (pos, Qinvisible, it->window);
4324 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4325 }
4326
4327 /* If we ended up on invisible text, proceed to
4328 skip starting with next_stop. */
4329 if (invis_p)
4330 tem = next_stop;
4331
4332 /* If there are adjacent invisible texts, don't lose the
4333 second one's ellipsis. */
4334 if (invis_p == 2)
4335 display_ellipsis_p = 1;
4336 }
4337 while (invis_p);
4338
4339 /* The position newpos is now either ZV or on visible text. */
4340 if (it->bidi_p)
4341 {
4342 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4343 int on_newline =
4344 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4345 int after_newline =
4346 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4347
4348 /* If the invisible text ends on a newline or on a
4349 character after a newline, we can avoid the costly,
4350 character by character, bidi iteration to NEWPOS, and
4351 instead simply reseat the iterator there. That's
4352 because all bidi reordering information is tossed at
4353 the newline. This is a big win for modes that hide
4354 complete lines, like Outline, Org, etc. */
4355 if (on_newline || after_newline)
4356 {
4357 struct text_pos tpos;
4358 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4359
4360 SET_TEXT_POS (tpos, newpos, bpos);
4361 reseat_1 (it, tpos, 0);
4362 /* If we reseat on a newline/ZV, we need to prep the
4363 bidi iterator for advancing to the next character
4364 after the newline/EOB, keeping the current paragraph
4365 direction (so that PRODUCE_GLYPHS does TRT wrt
4366 prepending/appending glyphs to a glyph row). */
4367 if (on_newline)
4368 {
4369 it->bidi_it.first_elt = 0;
4370 it->bidi_it.paragraph_dir = pdir;
4371 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4372 it->bidi_it.nchars = 1;
4373 it->bidi_it.ch_len = 1;
4374 }
4375 }
4376 else /* Must use the slow method. */
4377 {
4378 /* With bidi iteration, the region of invisible text
4379 could start and/or end in the middle of a
4380 non-base embedding level. Therefore, we need to
4381 skip invisible text using the bidi iterator,
4382 starting at IT's current position, until we find
4383 ourselves outside of the invisible text.
4384 Skipping invisible text _after_ bidi iteration
4385 avoids affecting the visual order of the
4386 displayed text when invisible properties are
4387 added or removed. */
4388 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4389 {
4390 /* If we were `reseat'ed to a new paragraph,
4391 determine the paragraph base direction. We
4392 need to do it now because
4393 next_element_from_buffer may not have a
4394 chance to do it, if we are going to skip any
4395 text at the beginning, which resets the
4396 FIRST_ELT flag. */
4397 bidi_paragraph_init (it->paragraph_embedding,
4398 &it->bidi_it, 1);
4399 }
4400 do
4401 {
4402 bidi_move_to_visually_next (&it->bidi_it);
4403 }
4404 while (it->stop_charpos <= it->bidi_it.charpos
4405 && it->bidi_it.charpos < newpos);
4406 IT_CHARPOS (*it) = it->bidi_it.charpos;
4407 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4408 /* If we overstepped NEWPOS, record its position in
4409 the iterator, so that we skip invisible text if
4410 later the bidi iteration lands us in the
4411 invisible region again. */
4412 if (IT_CHARPOS (*it) >= newpos)
4413 it->prev_stop = newpos;
4414 }
4415 }
4416 else
4417 {
4418 IT_CHARPOS (*it) = newpos;
4419 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4420 }
4421
4422 /* If there are before-strings at the start of invisible
4423 text, and the text is invisible because of a text
4424 property, arrange to show before-strings because 20.x did
4425 it that way. (If the text is invisible because of an
4426 overlay property instead of a text property, this is
4427 already handled in the overlay code.) */
4428 if (NILP (overlay)
4429 && get_overlay_strings (it, it->stop_charpos))
4430 {
4431 handled = HANDLED_RECOMPUTE_PROPS;
4432 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4433 }
4434 else if (display_ellipsis_p)
4435 {
4436 /* Make sure that the glyphs of the ellipsis will get
4437 correct `charpos' values. If we would not update
4438 it->position here, the glyphs would belong to the
4439 last visible character _before_ the invisible
4440 text, which confuses `set_cursor_from_row'.
4441
4442 We use the last invisible position instead of the
4443 first because this way the cursor is always drawn on
4444 the first "." of the ellipsis, whenever PT is inside
4445 the invisible text. Otherwise the cursor would be
4446 placed _after_ the ellipsis when the point is after the
4447 first invisible character. */
4448 if (!STRINGP (it->object))
4449 {
4450 it->position.charpos = newpos - 1;
4451 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4452 }
4453 it->ellipsis_p = 1;
4454 /* Let the ellipsis display before
4455 considering any properties of the following char.
4456 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4457 handled = HANDLED_RETURN;
4458 }
4459 }
4460 }
4461
4462 return handled;
4463 }
4464
4465
4466 /* Make iterator IT return `...' next.
4467 Replaces LEN characters from buffer. */
4468
4469 static void
4470 setup_for_ellipsis (struct it *it, int len)
4471 {
4472 /* Use the display table definition for `...'. Invalid glyphs
4473 will be handled by the method returning elements from dpvec. */
4474 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4475 {
4476 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4477 it->dpvec = v->contents;
4478 it->dpend = v->contents + v->header.size;
4479 }
4480 else
4481 {
4482 /* Default `...'. */
4483 it->dpvec = default_invis_vector;
4484 it->dpend = default_invis_vector + 3;
4485 }
4486
4487 it->dpvec_char_len = len;
4488 it->current.dpvec_index = 0;
4489 it->dpvec_face_id = -1;
4490
4491 /* Remember the current face id in case glyphs specify faces.
4492 IT's face is restored in set_iterator_to_next.
4493 saved_face_id was set to preceding char's face in handle_stop. */
4494 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4495 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4496
4497 it->method = GET_FROM_DISPLAY_VECTOR;
4498 it->ellipsis_p = 1;
4499 }
4500
4501
4502 \f
4503 /***********************************************************************
4504 'display' property
4505 ***********************************************************************/
4506
4507 /* Set up iterator IT from `display' property at its current position.
4508 Called from handle_stop.
4509 We return HANDLED_RETURN if some part of the display property
4510 overrides the display of the buffer text itself.
4511 Otherwise we return HANDLED_NORMALLY. */
4512
4513 static enum prop_handled
4514 handle_display_prop (struct it *it)
4515 {
4516 Lisp_Object propval, object, overlay;
4517 struct text_pos *position;
4518 ptrdiff_t bufpos;
4519 /* Nonzero if some property replaces the display of the text itself. */
4520 int display_replaced_p = 0;
4521
4522 if (STRINGP (it->string))
4523 {
4524 object = it->string;
4525 position = &it->current.string_pos;
4526 bufpos = CHARPOS (it->current.pos);
4527 }
4528 else
4529 {
4530 XSETWINDOW (object, it->w);
4531 position = &it->current.pos;
4532 bufpos = CHARPOS (*position);
4533 }
4534
4535 /* Reset those iterator values set from display property values. */
4536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4537 it->space_width = Qnil;
4538 it->font_height = Qnil;
4539 it->voffset = 0;
4540
4541 /* We don't support recursive `display' properties, i.e. string
4542 values that have a string `display' property, that have a string
4543 `display' property etc. */
4544 if (!it->string_from_display_prop_p)
4545 it->area = TEXT_AREA;
4546
4547 propval = get_char_property_and_overlay (make_number (position->charpos),
4548 Qdisplay, object, &overlay);
4549 if (NILP (propval))
4550 return HANDLED_NORMALLY;
4551 /* Now OVERLAY is the overlay that gave us this property, or nil
4552 if it was a text property. */
4553
4554 if (!STRINGP (it->string))
4555 object = it->w->contents;
4556
4557 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4558 position, bufpos,
4559 FRAME_WINDOW_P (it->f));
4560
4561 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4562 }
4563
4564 /* Subroutine of handle_display_prop. Returns non-zero if the display
4565 specification in SPEC is a replacing specification, i.e. it would
4566 replace the text covered by `display' property with something else,
4567 such as an image or a display string. If SPEC includes any kind or
4568 `(space ...) specification, the value is 2; this is used by
4569 compute_display_string_pos, which see.
4570
4571 See handle_single_display_spec for documentation of arguments.
4572 frame_window_p is non-zero if the window being redisplayed is on a
4573 GUI frame; this argument is used only if IT is NULL, see below.
4574
4575 IT can be NULL, if this is called by the bidi reordering code
4576 through compute_display_string_pos, which see. In that case, this
4577 function only examines SPEC, but does not otherwise "handle" it, in
4578 the sense that it doesn't set up members of IT from the display
4579 spec. */
4580 static int
4581 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4582 Lisp_Object overlay, struct text_pos *position,
4583 ptrdiff_t bufpos, int frame_window_p)
4584 {
4585 int replacing_p = 0;
4586 int rv;
4587
4588 if (CONSP (spec)
4589 /* Simple specifications. */
4590 && !EQ (XCAR (spec), Qimage)
4591 && !EQ (XCAR (spec), Qspace)
4592 && !EQ (XCAR (spec), Qwhen)
4593 && !EQ (XCAR (spec), Qslice)
4594 && !EQ (XCAR (spec), Qspace_width)
4595 && !EQ (XCAR (spec), Qheight)
4596 && !EQ (XCAR (spec), Qraise)
4597 /* Marginal area specifications. */
4598 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4599 && !EQ (XCAR (spec), Qleft_fringe)
4600 && !EQ (XCAR (spec), Qright_fringe)
4601 && !NILP (XCAR (spec)))
4602 {
4603 for (; CONSP (spec); spec = XCDR (spec))
4604 {
4605 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4606 overlay, position, bufpos,
4607 replacing_p, frame_window_p)))
4608 {
4609 replacing_p = rv;
4610 /* If some text in a string is replaced, `position' no
4611 longer points to the position of `object'. */
4612 if (!it || STRINGP (object))
4613 break;
4614 }
4615 }
4616 }
4617 else if (VECTORP (spec))
4618 {
4619 ptrdiff_t i;
4620 for (i = 0; i < ASIZE (spec); ++i)
4621 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4622 overlay, position, bufpos,
4623 replacing_p, frame_window_p)))
4624 {
4625 replacing_p = rv;
4626 /* If some text in a string is replaced, `position' no
4627 longer points to the position of `object'. */
4628 if (!it || STRINGP (object))
4629 break;
4630 }
4631 }
4632 else
4633 {
4634 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4635 position, bufpos, 0,
4636 frame_window_p)))
4637 replacing_p = rv;
4638 }
4639
4640 return replacing_p;
4641 }
4642
4643 /* Value is the position of the end of the `display' property starting
4644 at START_POS in OBJECT. */
4645
4646 static struct text_pos
4647 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4648 {
4649 Lisp_Object end;
4650 struct text_pos end_pos;
4651
4652 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4653 Qdisplay, object, Qnil);
4654 CHARPOS (end_pos) = XFASTINT (end);
4655 if (STRINGP (object))
4656 compute_string_pos (&end_pos, start_pos, it->string);
4657 else
4658 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4659
4660 return end_pos;
4661 }
4662
4663
4664 /* Set up IT from a single `display' property specification SPEC. OBJECT
4665 is the object in which the `display' property was found. *POSITION
4666 is the position in OBJECT at which the `display' property was found.
4667 BUFPOS is the buffer position of OBJECT (different from POSITION if
4668 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4669 previously saw a display specification which already replaced text
4670 display with something else, for example an image; we ignore such
4671 properties after the first one has been processed.
4672
4673 OVERLAY is the overlay this `display' property came from,
4674 or nil if it was a text property.
4675
4676 If SPEC is a `space' or `image' specification, and in some other
4677 cases too, set *POSITION to the position where the `display'
4678 property ends.
4679
4680 If IT is NULL, only examine the property specification in SPEC, but
4681 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4682 is intended to be displayed in a window on a GUI frame.
4683
4684 Value is non-zero if something was found which replaces the display
4685 of buffer or string text. */
4686
4687 static int
4688 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4689 Lisp_Object overlay, struct text_pos *position,
4690 ptrdiff_t bufpos, int display_replaced_p,
4691 int frame_window_p)
4692 {
4693 Lisp_Object form;
4694 Lisp_Object location, value;
4695 struct text_pos start_pos = *position;
4696 int valid_p;
4697
4698 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4699 If the result is non-nil, use VALUE instead of SPEC. */
4700 form = Qt;
4701 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4702 {
4703 spec = XCDR (spec);
4704 if (!CONSP (spec))
4705 return 0;
4706 form = XCAR (spec);
4707 spec = XCDR (spec);
4708 }
4709
4710 if (!NILP (form) && !EQ (form, Qt))
4711 {
4712 ptrdiff_t count = SPECPDL_INDEX ();
4713 struct gcpro gcpro1;
4714
4715 /* Bind `object' to the object having the `display' property, a
4716 buffer or string. Bind `position' to the position in the
4717 object where the property was found, and `buffer-position'
4718 to the current position in the buffer. */
4719
4720 if (NILP (object))
4721 XSETBUFFER (object, current_buffer);
4722 specbind (Qobject, object);
4723 specbind (Qposition, make_number (CHARPOS (*position)));
4724 specbind (Qbuffer_position, make_number (bufpos));
4725 GCPRO1 (form);
4726 form = safe_eval (form);
4727 UNGCPRO;
4728 unbind_to (count, Qnil);
4729 }
4730
4731 if (NILP (form))
4732 return 0;
4733
4734 /* Handle `(height HEIGHT)' specifications. */
4735 if (CONSP (spec)
4736 && EQ (XCAR (spec), Qheight)
4737 && CONSP (XCDR (spec)))
4738 {
4739 if (it)
4740 {
4741 if (!FRAME_WINDOW_P (it->f))
4742 return 0;
4743
4744 it->font_height = XCAR (XCDR (spec));
4745 if (!NILP (it->font_height))
4746 {
4747 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4748 int new_height = -1;
4749
4750 if (CONSP (it->font_height)
4751 && (EQ (XCAR (it->font_height), Qplus)
4752 || EQ (XCAR (it->font_height), Qminus))
4753 && CONSP (XCDR (it->font_height))
4754 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4755 {
4756 /* `(+ N)' or `(- N)' where N is an integer. */
4757 int steps = XINT (XCAR (XCDR (it->font_height)));
4758 if (EQ (XCAR (it->font_height), Qplus))
4759 steps = - steps;
4760 it->face_id = smaller_face (it->f, it->face_id, steps);
4761 }
4762 else if (FUNCTIONP (it->font_height))
4763 {
4764 /* Call function with current height as argument.
4765 Value is the new height. */
4766 Lisp_Object height;
4767 height = safe_call1 (it->font_height,
4768 face->lface[LFACE_HEIGHT_INDEX]);
4769 if (NUMBERP (height))
4770 new_height = XFLOATINT (height);
4771 }
4772 else if (NUMBERP (it->font_height))
4773 {
4774 /* Value is a multiple of the canonical char height. */
4775 struct face *f;
4776
4777 f = FACE_FROM_ID (it->f,
4778 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4779 new_height = (XFLOATINT (it->font_height)
4780 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4781 }
4782 else
4783 {
4784 /* Evaluate IT->font_height with `height' bound to the
4785 current specified height to get the new height. */
4786 ptrdiff_t count = SPECPDL_INDEX ();
4787
4788 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4789 value = safe_eval (it->font_height);
4790 unbind_to (count, Qnil);
4791
4792 if (NUMBERP (value))
4793 new_height = XFLOATINT (value);
4794 }
4795
4796 if (new_height > 0)
4797 it->face_id = face_with_height (it->f, it->face_id, new_height);
4798 }
4799 }
4800
4801 return 0;
4802 }
4803
4804 /* Handle `(space-width WIDTH)'. */
4805 if (CONSP (spec)
4806 && EQ (XCAR (spec), Qspace_width)
4807 && CONSP (XCDR (spec)))
4808 {
4809 if (it)
4810 {
4811 if (!FRAME_WINDOW_P (it->f))
4812 return 0;
4813
4814 value = XCAR (XCDR (spec));
4815 if (NUMBERP (value) && XFLOATINT (value) > 0)
4816 it->space_width = value;
4817 }
4818
4819 return 0;
4820 }
4821
4822 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4823 if (CONSP (spec)
4824 && EQ (XCAR (spec), Qslice))
4825 {
4826 Lisp_Object tem;
4827
4828 if (it)
4829 {
4830 if (!FRAME_WINDOW_P (it->f))
4831 return 0;
4832
4833 if (tem = XCDR (spec), CONSP (tem))
4834 {
4835 it->slice.x = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4837 {
4838 it->slice.y = XCAR (tem);
4839 if (tem = XCDR (tem), CONSP (tem))
4840 {
4841 it->slice.width = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 it->slice.height = XCAR (tem);
4844 }
4845 }
4846 }
4847 }
4848
4849 return 0;
4850 }
4851
4852 /* Handle `(raise FACTOR)'. */
4853 if (CONSP (spec)
4854 && EQ (XCAR (spec), Qraise)
4855 && CONSP (XCDR (spec)))
4856 {
4857 if (it)
4858 {
4859 if (!FRAME_WINDOW_P (it->f))
4860 return 0;
4861
4862 #ifdef HAVE_WINDOW_SYSTEM
4863 value = XCAR (XCDR (spec));
4864 if (NUMBERP (value))
4865 {
4866 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4867 it->voffset = - (XFLOATINT (value)
4868 * (FONT_HEIGHT (face->font)));
4869 }
4870 #endif /* HAVE_WINDOW_SYSTEM */
4871 }
4872
4873 return 0;
4874 }
4875
4876 /* Don't handle the other kinds of display specifications
4877 inside a string that we got from a `display' property. */
4878 if (it && it->string_from_display_prop_p)
4879 return 0;
4880
4881 /* Characters having this form of property are not displayed, so
4882 we have to find the end of the property. */
4883 if (it)
4884 {
4885 start_pos = *position;
4886 *position = display_prop_end (it, object, start_pos);
4887 }
4888 value = Qnil;
4889
4890 /* Stop the scan at that end position--we assume that all
4891 text properties change there. */
4892 if (it)
4893 it->stop_charpos = position->charpos;
4894
4895 /* Handle `(left-fringe BITMAP [FACE])'
4896 and `(right-fringe BITMAP [FACE])'. */
4897 if (CONSP (spec)
4898 && (EQ (XCAR (spec), Qleft_fringe)
4899 || EQ (XCAR (spec), Qright_fringe))
4900 && CONSP (XCDR (spec)))
4901 {
4902 int fringe_bitmap;
4903
4904 if (it)
4905 {
4906 if (!FRAME_WINDOW_P (it->f))
4907 /* If we return here, POSITION has been advanced
4908 across the text with this property. */
4909 {
4910 /* Synchronize the bidi iterator with POSITION. This is
4911 needed because we are not going to push the iterator
4912 on behalf of this display property, so there will be
4913 no pop_it call to do this synchronization for us. */
4914 if (it->bidi_p)
4915 {
4916 it->position = *position;
4917 iterate_out_of_display_property (it);
4918 *position = it->position;
4919 }
4920 return 1;
4921 }
4922 }
4923 else if (!frame_window_p)
4924 return 1;
4925
4926 #ifdef HAVE_WINDOW_SYSTEM
4927 value = XCAR (XCDR (spec));
4928 if (!SYMBOLP (value)
4929 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4930 /* If we return here, POSITION has been advanced
4931 across the text with this property. */
4932 {
4933 if (it && it->bidi_p)
4934 {
4935 it->position = *position;
4936 iterate_out_of_display_property (it);
4937 *position = it->position;
4938 }
4939 return 1;
4940 }
4941
4942 if (it)
4943 {
4944 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4945
4946 if (CONSP (XCDR (XCDR (spec))))
4947 {
4948 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4949 int face_id2 = lookup_derived_face (it->f, face_name,
4950 FRINGE_FACE_ID, 0);
4951 if (face_id2 >= 0)
4952 face_id = face_id2;
4953 }
4954
4955 /* Save current settings of IT so that we can restore them
4956 when we are finished with the glyph property value. */
4957 push_it (it, position);
4958
4959 it->area = TEXT_AREA;
4960 it->what = IT_IMAGE;
4961 it->image_id = -1; /* no image */
4962 it->position = start_pos;
4963 it->object = NILP (object) ? it->w->contents : object;
4964 it->method = GET_FROM_IMAGE;
4965 it->from_overlay = Qnil;
4966 it->face_id = face_id;
4967 it->from_disp_prop_p = 1;
4968
4969 /* Say that we haven't consumed the characters with
4970 `display' property yet. The call to pop_it in
4971 set_iterator_to_next will clean this up. */
4972 *position = start_pos;
4973
4974 if (EQ (XCAR (spec), Qleft_fringe))
4975 {
4976 it->left_user_fringe_bitmap = fringe_bitmap;
4977 it->left_user_fringe_face_id = face_id;
4978 }
4979 else
4980 {
4981 it->right_user_fringe_bitmap = fringe_bitmap;
4982 it->right_user_fringe_face_id = face_id;
4983 }
4984 }
4985 #endif /* HAVE_WINDOW_SYSTEM */
4986 return 1;
4987 }
4988
4989 /* Prepare to handle `((margin left-margin) ...)',
4990 `((margin right-margin) ...)' and `((margin nil) ...)'
4991 prefixes for display specifications. */
4992 location = Qunbound;
4993 if (CONSP (spec) && CONSP (XCAR (spec)))
4994 {
4995 Lisp_Object tem;
4996
4997 value = XCDR (spec);
4998 if (CONSP (value))
4999 value = XCAR (value);
5000
5001 tem = XCAR (spec);
5002 if (EQ (XCAR (tem), Qmargin)
5003 && (tem = XCDR (tem),
5004 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5005 (NILP (tem)
5006 || EQ (tem, Qleft_margin)
5007 || EQ (tem, Qright_margin))))
5008 location = tem;
5009 }
5010
5011 if (EQ (location, Qunbound))
5012 {
5013 location = Qnil;
5014 value = spec;
5015 }
5016
5017 /* After this point, VALUE is the property after any
5018 margin prefix has been stripped. It must be a string,
5019 an image specification, or `(space ...)'.
5020
5021 LOCATION specifies where to display: `left-margin',
5022 `right-margin' or nil. */
5023
5024 valid_p = (STRINGP (value)
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5027 && valid_image_p (value))
5028 #endif /* not HAVE_WINDOW_SYSTEM */
5029 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5030
5031 if (valid_p && !display_replaced_p)
5032 {
5033 int retval = 1;
5034
5035 if (!it)
5036 {
5037 /* Callers need to know whether the display spec is any kind
5038 of `(space ...)' spec that is about to affect text-area
5039 display. */
5040 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5041 retval = 2;
5042 return retval;
5043 }
5044
5045 /* Save current settings of IT so that we can restore them
5046 when we are finished with the glyph property value. */
5047 push_it (it, position);
5048 it->from_overlay = overlay;
5049 it->from_disp_prop_p = 1;
5050
5051 if (NILP (location))
5052 it->area = TEXT_AREA;
5053 else if (EQ (location, Qleft_margin))
5054 it->area = LEFT_MARGIN_AREA;
5055 else
5056 it->area = RIGHT_MARGIN_AREA;
5057
5058 if (STRINGP (value))
5059 {
5060 it->string = value;
5061 it->multibyte_p = STRING_MULTIBYTE (it->string);
5062 it->current.overlay_string_index = -1;
5063 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5064 it->end_charpos = it->string_nchars = SCHARS (it->string);
5065 it->method = GET_FROM_STRING;
5066 it->stop_charpos = 0;
5067 it->prev_stop = 0;
5068 it->base_level_stop = 0;
5069 it->string_from_display_prop_p = 1;
5070 /* Say that we haven't consumed the characters with
5071 `display' property yet. The call to pop_it in
5072 set_iterator_to_next will clean this up. */
5073 if (BUFFERP (object))
5074 *position = start_pos;
5075
5076 /* Force paragraph direction to be that of the parent
5077 object. If the parent object's paragraph direction is
5078 not yet determined, default to L2R. */
5079 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5080 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5081 else
5082 it->paragraph_embedding = L2R;
5083
5084 /* Set up the bidi iterator for this display string. */
5085 if (it->bidi_p)
5086 {
5087 it->bidi_it.string.lstring = it->string;
5088 it->bidi_it.string.s = NULL;
5089 it->bidi_it.string.schars = it->end_charpos;
5090 it->bidi_it.string.bufpos = bufpos;
5091 it->bidi_it.string.from_disp_str = 1;
5092 it->bidi_it.string.unibyte = !it->multibyte_p;
5093 it->bidi_it.w = it->w;
5094 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5095 }
5096 }
5097 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5098 {
5099 it->method = GET_FROM_STRETCH;
5100 it->object = value;
5101 *position = it->position = start_pos;
5102 retval = 1 + (it->area == TEXT_AREA);
5103 }
5104 #ifdef HAVE_WINDOW_SYSTEM
5105 else
5106 {
5107 it->what = IT_IMAGE;
5108 it->image_id = lookup_image (it->f, value);
5109 it->position = start_pos;
5110 it->object = NILP (object) ? it->w->contents : object;
5111 it->method = GET_FROM_IMAGE;
5112
5113 /* Say that we haven't consumed the characters with
5114 `display' property yet. The call to pop_it in
5115 set_iterator_to_next will clean this up. */
5116 *position = start_pos;
5117 }
5118 #endif /* HAVE_WINDOW_SYSTEM */
5119
5120 return retval;
5121 }
5122
5123 /* Invalid property or property not supported. Restore
5124 POSITION to what it was before. */
5125 *position = start_pos;
5126 return 0;
5127 }
5128
5129 /* Check if PROP is a display property value whose text should be
5130 treated as intangible. OVERLAY is the overlay from which PROP
5131 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5132 specify the buffer position covered by PROP. */
5133
5134 int
5135 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5136 ptrdiff_t charpos, ptrdiff_t bytepos)
5137 {
5138 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5139 struct text_pos position;
5140
5141 SET_TEXT_POS (position, charpos, bytepos);
5142 return handle_display_spec (NULL, prop, Qnil, overlay,
5143 &position, charpos, frame_window_p);
5144 }
5145
5146
5147 /* Return 1 if PROP is a display sub-property value containing STRING.
5148
5149 Implementation note: this and the following function are really
5150 special cases of handle_display_spec and
5151 handle_single_display_spec, and should ideally use the same code.
5152 Until they do, these two pairs must be consistent and must be
5153 modified in sync. */
5154
5155 static int
5156 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5157 {
5158 if (EQ (string, prop))
5159 return 1;
5160
5161 /* Skip over `when FORM'. */
5162 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5163 {
5164 prop = XCDR (prop);
5165 if (!CONSP (prop))
5166 return 0;
5167 /* Actually, the condition following `when' should be eval'ed,
5168 like handle_single_display_spec does, and we should return
5169 zero if it evaluates to nil. However, this function is
5170 called only when the buffer was already displayed and some
5171 glyph in the glyph matrix was found to come from a display
5172 string. Therefore, the condition was already evaluated, and
5173 the result was non-nil, otherwise the display string wouldn't
5174 have been displayed and we would have never been called for
5175 this property. Thus, we can skip the evaluation and assume
5176 its result is non-nil. */
5177 prop = XCDR (prop);
5178 }
5179
5180 if (CONSP (prop))
5181 /* Skip over `margin LOCATION'. */
5182 if (EQ (XCAR (prop), Qmargin))
5183 {
5184 prop = XCDR (prop);
5185 if (!CONSP (prop))
5186 return 0;
5187
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5191 }
5192
5193 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5194 }
5195
5196
5197 /* Return 1 if STRING appears in the `display' property PROP. */
5198
5199 static int
5200 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5201 {
5202 if (CONSP (prop)
5203 && !EQ (XCAR (prop), Qwhen)
5204 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5205 {
5206 /* A list of sub-properties. */
5207 while (CONSP (prop))
5208 {
5209 if (single_display_spec_string_p (XCAR (prop), string))
5210 return 1;
5211 prop = XCDR (prop);
5212 }
5213 }
5214 else if (VECTORP (prop))
5215 {
5216 /* A vector of sub-properties. */
5217 ptrdiff_t i;
5218 for (i = 0; i < ASIZE (prop); ++i)
5219 if (single_display_spec_string_p (AREF (prop, i), string))
5220 return 1;
5221 }
5222 else
5223 return single_display_spec_string_p (prop, string);
5224
5225 return 0;
5226 }
5227
5228 /* Look for STRING in overlays and text properties in the current
5229 buffer, between character positions FROM and TO (excluding TO).
5230 BACK_P non-zero means look back (in this case, TO is supposed to be
5231 less than FROM).
5232 Value is the first character position where STRING was found, or
5233 zero if it wasn't found before hitting TO.
5234
5235 This function may only use code that doesn't eval because it is
5236 called asynchronously from note_mouse_highlight. */
5237
5238 static ptrdiff_t
5239 string_buffer_position_lim (Lisp_Object string,
5240 ptrdiff_t from, ptrdiff_t to, int back_p)
5241 {
5242 Lisp_Object limit, prop, pos;
5243 int found = 0;
5244
5245 pos = make_number (max (from, BEGV));
5246
5247 if (!back_p) /* looking forward */
5248 {
5249 limit = make_number (min (to, ZV));
5250 while (!found && !EQ (pos, limit))
5251 {
5252 prop = Fget_char_property (pos, Qdisplay, Qnil);
5253 if (!NILP (prop) && display_prop_string_p (prop, string))
5254 found = 1;
5255 else
5256 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5257 limit);
5258 }
5259 }
5260 else /* looking back */
5261 {
5262 limit = make_number (max (to, BEGV));
5263 while (!found && !EQ (pos, limit))
5264 {
5265 prop = Fget_char_property (pos, Qdisplay, Qnil);
5266 if (!NILP (prop) && display_prop_string_p (prop, string))
5267 found = 1;
5268 else
5269 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5270 limit);
5271 }
5272 }
5273
5274 return found ? XINT (pos) : 0;
5275 }
5276
5277 /* Determine which buffer position in current buffer STRING comes from.
5278 AROUND_CHARPOS is an approximate position where it could come from.
5279 Value is the buffer position or 0 if it couldn't be determined.
5280
5281 This function is necessary because we don't record buffer positions
5282 in glyphs generated from strings (to keep struct glyph small).
5283 This function may only use code that doesn't eval because it is
5284 called asynchronously from note_mouse_highlight. */
5285
5286 static ptrdiff_t
5287 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5288 {
5289 const int MAX_DISTANCE = 1000;
5290 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5291 around_charpos + MAX_DISTANCE,
5292 0);
5293
5294 if (!found)
5295 found = string_buffer_position_lim (string, around_charpos,
5296 around_charpos - MAX_DISTANCE, 1);
5297 return found;
5298 }
5299
5300
5301 \f
5302 /***********************************************************************
5303 `composition' property
5304 ***********************************************************************/
5305
5306 /* Set up iterator IT from `composition' property at its current
5307 position. Called from handle_stop. */
5308
5309 static enum prop_handled
5310 handle_composition_prop (struct it *it)
5311 {
5312 Lisp_Object prop, string;
5313 ptrdiff_t pos, pos_byte, start, end;
5314
5315 if (STRINGP (it->string))
5316 {
5317 unsigned char *s;
5318
5319 pos = IT_STRING_CHARPOS (*it);
5320 pos_byte = IT_STRING_BYTEPOS (*it);
5321 string = it->string;
5322 s = SDATA (string) + pos_byte;
5323 it->c = STRING_CHAR (s);
5324 }
5325 else
5326 {
5327 pos = IT_CHARPOS (*it);
5328 pos_byte = IT_BYTEPOS (*it);
5329 string = Qnil;
5330 it->c = FETCH_CHAR (pos_byte);
5331 }
5332
5333 /* If there's a valid composition and point is not inside of the
5334 composition (in the case that the composition is from the current
5335 buffer), draw a glyph composed from the composition components. */
5336 if (find_composition (pos, -1, &start, &end, &prop, string)
5337 && composition_valid_p (start, end, prop)
5338 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5339 {
5340 if (start < pos)
5341 /* As we can't handle this situation (perhaps font-lock added
5342 a new composition), we just return here hoping that next
5343 redisplay will detect this composition much earlier. */
5344 return HANDLED_NORMALLY;
5345 if (start != pos)
5346 {
5347 if (STRINGP (it->string))
5348 pos_byte = string_char_to_byte (it->string, start);
5349 else
5350 pos_byte = CHAR_TO_BYTE (start);
5351 }
5352 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5353 prop, string);
5354
5355 if (it->cmp_it.id >= 0)
5356 {
5357 it->cmp_it.ch = -1;
5358 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5359 it->cmp_it.nglyphs = -1;
5360 }
5361 }
5362
5363 return HANDLED_NORMALLY;
5364 }
5365
5366
5367 \f
5368 /***********************************************************************
5369 Overlay strings
5370 ***********************************************************************/
5371
5372 /* The following structure is used to record overlay strings for
5373 later sorting in load_overlay_strings. */
5374
5375 struct overlay_entry
5376 {
5377 Lisp_Object overlay;
5378 Lisp_Object string;
5379 EMACS_INT priority;
5380 int after_string_p;
5381 };
5382
5383
5384 /* Set up iterator IT from overlay strings at its current position.
5385 Called from handle_stop. */
5386
5387 static enum prop_handled
5388 handle_overlay_change (struct it *it)
5389 {
5390 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5391 return HANDLED_RECOMPUTE_PROPS;
5392 else
5393 return HANDLED_NORMALLY;
5394 }
5395
5396
5397 /* Set up the next overlay string for delivery by IT, if there is an
5398 overlay string to deliver. Called by set_iterator_to_next when the
5399 end of the current overlay string is reached. If there are more
5400 overlay strings to display, IT->string and
5401 IT->current.overlay_string_index are set appropriately here.
5402 Otherwise IT->string is set to nil. */
5403
5404 static void
5405 next_overlay_string (struct it *it)
5406 {
5407 ++it->current.overlay_string_index;
5408 if (it->current.overlay_string_index == it->n_overlay_strings)
5409 {
5410 /* No more overlay strings. Restore IT's settings to what
5411 they were before overlay strings were processed, and
5412 continue to deliver from current_buffer. */
5413
5414 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5415 pop_it (it);
5416 eassert (it->sp > 0
5417 || (NILP (it->string)
5418 && it->method == GET_FROM_BUFFER
5419 && it->stop_charpos >= BEGV
5420 && it->stop_charpos <= it->end_charpos));
5421 it->current.overlay_string_index = -1;
5422 it->n_overlay_strings = 0;
5423 it->overlay_strings_charpos = -1;
5424 /* If there's an empty display string on the stack, pop the
5425 stack, to resync the bidi iterator with IT's position. Such
5426 empty strings are pushed onto the stack in
5427 get_overlay_strings_1. */
5428 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5429 pop_it (it);
5430
5431 /* If we're at the end of the buffer, record that we have
5432 processed the overlay strings there already, so that
5433 next_element_from_buffer doesn't try it again. */
5434 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5435 it->overlay_strings_at_end_processed_p = 1;
5436 }
5437 else
5438 {
5439 /* There are more overlay strings to process. If
5440 IT->current.overlay_string_index has advanced to a position
5441 where we must load IT->overlay_strings with more strings, do
5442 it. We must load at the IT->overlay_strings_charpos where
5443 IT->n_overlay_strings was originally computed; when invisible
5444 text is present, this might not be IT_CHARPOS (Bug#7016). */
5445 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5446
5447 if (it->current.overlay_string_index && i == 0)
5448 load_overlay_strings (it, it->overlay_strings_charpos);
5449
5450 /* Initialize IT to deliver display elements from the overlay
5451 string. */
5452 it->string = it->overlay_strings[i];
5453 it->multibyte_p = STRING_MULTIBYTE (it->string);
5454 SET_TEXT_POS (it->current.string_pos, 0, 0);
5455 it->method = GET_FROM_STRING;
5456 it->stop_charpos = 0;
5457 it->end_charpos = SCHARS (it->string);
5458 if (it->cmp_it.stop_pos >= 0)
5459 it->cmp_it.stop_pos = 0;
5460 it->prev_stop = 0;
5461 it->base_level_stop = 0;
5462
5463 /* Set up the bidi iterator for this overlay string. */
5464 if (it->bidi_p)
5465 {
5466 it->bidi_it.string.lstring = it->string;
5467 it->bidi_it.string.s = NULL;
5468 it->bidi_it.string.schars = SCHARS (it->string);
5469 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5470 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5471 it->bidi_it.string.unibyte = !it->multibyte_p;
5472 it->bidi_it.w = it->w;
5473 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5474 }
5475 }
5476
5477 CHECK_IT (it);
5478 }
5479
5480
5481 /* Compare two overlay_entry structures E1 and E2. Used as a
5482 comparison function for qsort in load_overlay_strings. Overlay
5483 strings for the same position are sorted so that
5484
5485 1. All after-strings come in front of before-strings, except
5486 when they come from the same overlay.
5487
5488 2. Within after-strings, strings are sorted so that overlay strings
5489 from overlays with higher priorities come first.
5490
5491 2. Within before-strings, strings are sorted so that overlay
5492 strings from overlays with higher priorities come last.
5493
5494 Value is analogous to strcmp. */
5495
5496
5497 static int
5498 compare_overlay_entries (const void *e1, const void *e2)
5499 {
5500 struct overlay_entry const *entry1 = e1;
5501 struct overlay_entry const *entry2 = e2;
5502 int result;
5503
5504 if (entry1->after_string_p != entry2->after_string_p)
5505 {
5506 /* Let after-strings appear in front of before-strings if
5507 they come from different overlays. */
5508 if (EQ (entry1->overlay, entry2->overlay))
5509 result = entry1->after_string_p ? 1 : -1;
5510 else
5511 result = entry1->after_string_p ? -1 : 1;
5512 }
5513 else if (entry1->priority != entry2->priority)
5514 {
5515 if (entry1->after_string_p)
5516 /* After-strings sorted in order of decreasing priority. */
5517 result = entry2->priority < entry1->priority ? -1 : 1;
5518 else
5519 /* Before-strings sorted in order of increasing priority. */
5520 result = entry1->priority < entry2->priority ? -1 : 1;
5521 }
5522 else
5523 result = 0;
5524
5525 return result;
5526 }
5527
5528
5529 /* Load the vector IT->overlay_strings with overlay strings from IT's
5530 current buffer position, or from CHARPOS if that is > 0. Set
5531 IT->n_overlays to the total number of overlay strings found.
5532
5533 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5534 a time. On entry into load_overlay_strings,
5535 IT->current.overlay_string_index gives the number of overlay
5536 strings that have already been loaded by previous calls to this
5537 function.
5538
5539 IT->add_overlay_start contains an additional overlay start
5540 position to consider for taking overlay strings from, if non-zero.
5541 This position comes into play when the overlay has an `invisible'
5542 property, and both before and after-strings. When we've skipped to
5543 the end of the overlay, because of its `invisible' property, we
5544 nevertheless want its before-string to appear.
5545 IT->add_overlay_start will contain the overlay start position
5546 in this case.
5547
5548 Overlay strings are sorted so that after-string strings come in
5549 front of before-string strings. Within before and after-strings,
5550 strings are sorted by overlay priority. See also function
5551 compare_overlay_entries. */
5552
5553 static void
5554 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5555 {
5556 Lisp_Object overlay, window, str, invisible;
5557 struct Lisp_Overlay *ov;
5558 ptrdiff_t start, end;
5559 ptrdiff_t size = 20;
5560 ptrdiff_t n = 0, i, j;
5561 int invis_p;
5562 struct overlay_entry *entries = alloca (size * sizeof *entries);
5563 USE_SAFE_ALLOCA;
5564
5565 if (charpos <= 0)
5566 charpos = IT_CHARPOS (*it);
5567
5568 /* Append the overlay string STRING of overlay OVERLAY to vector
5569 `entries' which has size `size' and currently contains `n'
5570 elements. AFTER_P non-zero means STRING is an after-string of
5571 OVERLAY. */
5572 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5573 do \
5574 { \
5575 Lisp_Object priority; \
5576 \
5577 if (n == size) \
5578 { \
5579 struct overlay_entry *old = entries; \
5580 SAFE_NALLOCA (entries, 2, size); \
5581 memcpy (entries, old, size * sizeof *entries); \
5582 size *= 2; \
5583 } \
5584 \
5585 entries[n].string = (STRING); \
5586 entries[n].overlay = (OVERLAY); \
5587 priority = Foverlay_get ((OVERLAY), Qpriority); \
5588 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5589 entries[n].after_string_p = (AFTER_P); \
5590 ++n; \
5591 } \
5592 while (0)
5593
5594 /* Process overlay before the overlay center. */
5595 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5596 {
5597 XSETMISC (overlay, ov);
5598 eassert (OVERLAYP (overlay));
5599 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5600 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5601
5602 if (end < charpos)
5603 break;
5604
5605 /* Skip this overlay if it doesn't start or end at IT's current
5606 position. */
5607 if (end != charpos && start != charpos)
5608 continue;
5609
5610 /* Skip this overlay if it doesn't apply to IT->w. */
5611 window = Foverlay_get (overlay, Qwindow);
5612 if (WINDOWP (window) && XWINDOW (window) != it->w)
5613 continue;
5614
5615 /* If the text ``under'' the overlay is invisible, both before-
5616 and after-strings from this overlay are visible; start and
5617 end position are indistinguishable. */
5618 invisible = Foverlay_get (overlay, Qinvisible);
5619 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5620
5621 /* If overlay has a non-empty before-string, record it. */
5622 if ((start == charpos || (end == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 0);
5626
5627 /* If overlay has a non-empty after-string, record it. */
5628 if ((end == charpos || (start == charpos && invis_p))
5629 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5630 && SCHARS (str))
5631 RECORD_OVERLAY_STRING (overlay, str, 1);
5632 }
5633
5634 /* Process overlays after the overlay center. */
5635 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5636 {
5637 XSETMISC (overlay, ov);
5638 eassert (OVERLAYP (overlay));
5639 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5640 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5641
5642 if (start > charpos)
5643 break;
5644
5645 /* Skip this overlay if it doesn't start or end at IT's current
5646 position. */
5647 if (end != charpos && start != charpos)
5648 continue;
5649
5650 /* Skip this overlay if it doesn't apply to IT->w. */
5651 window = Foverlay_get (overlay, Qwindow);
5652 if (WINDOWP (window) && XWINDOW (window) != it->w)
5653 continue;
5654
5655 /* If the text ``under'' the overlay is invisible, it has a zero
5656 dimension, and both before- and after-strings apply. */
5657 invisible = Foverlay_get (overlay, Qinvisible);
5658 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5659
5660 /* If overlay has a non-empty before-string, record it. */
5661 if ((start == charpos || (end == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 0);
5665
5666 /* If overlay has a non-empty after-string, record it. */
5667 if ((end == charpos || (start == charpos && invis_p))
5668 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5669 && SCHARS (str))
5670 RECORD_OVERLAY_STRING (overlay, str, 1);
5671 }
5672
5673 #undef RECORD_OVERLAY_STRING
5674
5675 /* Sort entries. */
5676 if (n > 1)
5677 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5678
5679 /* Record number of overlay strings, and where we computed it. */
5680 it->n_overlay_strings = n;
5681 it->overlay_strings_charpos = charpos;
5682
5683 /* IT->current.overlay_string_index is the number of overlay strings
5684 that have already been consumed by IT. Copy some of the
5685 remaining overlay strings to IT->overlay_strings. */
5686 i = 0;
5687 j = it->current.overlay_string_index;
5688 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5689 {
5690 it->overlay_strings[i] = entries[j].string;
5691 it->string_overlays[i++] = entries[j++].overlay;
5692 }
5693
5694 CHECK_IT (it);
5695 SAFE_FREE ();
5696 }
5697
5698
5699 /* Get the first chunk of overlay strings at IT's current buffer
5700 position, or at CHARPOS if that is > 0. Value is non-zero if at
5701 least one overlay string was found. */
5702
5703 static int
5704 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5705 {
5706 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5707 process. This fills IT->overlay_strings with strings, and sets
5708 IT->n_overlay_strings to the total number of strings to process.
5709 IT->pos.overlay_string_index has to be set temporarily to zero
5710 because load_overlay_strings needs this; it must be set to -1
5711 when no overlay strings are found because a zero value would
5712 indicate a position in the first overlay string. */
5713 it->current.overlay_string_index = 0;
5714 load_overlay_strings (it, charpos);
5715
5716 /* If we found overlay strings, set up IT to deliver display
5717 elements from the first one. Otherwise set up IT to deliver
5718 from current_buffer. */
5719 if (it->n_overlay_strings)
5720 {
5721 /* Make sure we know settings in current_buffer, so that we can
5722 restore meaningful values when we're done with the overlay
5723 strings. */
5724 if (compute_stop_p)
5725 compute_stop_pos (it);
5726 eassert (it->face_id >= 0);
5727
5728 /* Save IT's settings. They are restored after all overlay
5729 strings have been processed. */
5730 eassert (!compute_stop_p || it->sp == 0);
5731
5732 /* When called from handle_stop, there might be an empty display
5733 string loaded. In that case, don't bother saving it. But
5734 don't use this optimization with the bidi iterator, since we
5735 need the corresponding pop_it call to resync the bidi
5736 iterator's position with IT's position, after we are done
5737 with the overlay strings. (The corresponding call to pop_it
5738 in case of an empty display string is in
5739 next_overlay_string.) */
5740 if (!(!it->bidi_p
5741 && STRINGP (it->string) && !SCHARS (it->string)))
5742 push_it (it, NULL);
5743
5744 /* Set up IT to deliver display elements from the first overlay
5745 string. */
5746 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5747 it->string = it->overlay_strings[0];
5748 it->from_overlay = Qnil;
5749 it->stop_charpos = 0;
5750 eassert (STRINGP (it->string));
5751 it->end_charpos = SCHARS (it->string);
5752 it->prev_stop = 0;
5753 it->base_level_stop = 0;
5754 it->multibyte_p = STRING_MULTIBYTE (it->string);
5755 it->method = GET_FROM_STRING;
5756 it->from_disp_prop_p = 0;
5757
5758 /* Force paragraph direction to be that of the parent
5759 buffer. */
5760 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5761 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5762 else
5763 it->paragraph_embedding = L2R;
5764
5765 /* Set up the bidi iterator for this overlay string. */
5766 if (it->bidi_p)
5767 {
5768 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5769
5770 it->bidi_it.string.lstring = it->string;
5771 it->bidi_it.string.s = NULL;
5772 it->bidi_it.string.schars = SCHARS (it->string);
5773 it->bidi_it.string.bufpos = pos;
5774 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5775 it->bidi_it.string.unibyte = !it->multibyte_p;
5776 it->bidi_it.w = it->w;
5777 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5778 }
5779 return 1;
5780 }
5781
5782 it->current.overlay_string_index = -1;
5783 return 0;
5784 }
5785
5786 static int
5787 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5788 {
5789 it->string = Qnil;
5790 it->method = GET_FROM_BUFFER;
5791
5792 (void) get_overlay_strings_1 (it, charpos, 1);
5793
5794 CHECK_IT (it);
5795
5796 /* Value is non-zero if we found at least one overlay string. */
5797 return STRINGP (it->string);
5798 }
5799
5800
5801 \f
5802 /***********************************************************************
5803 Saving and restoring state
5804 ***********************************************************************/
5805
5806 /* Save current settings of IT on IT->stack. Called, for example,
5807 before setting up IT for an overlay string, to be able to restore
5808 IT's settings to what they were after the overlay string has been
5809 processed. If POSITION is non-NULL, it is the position to save on
5810 the stack instead of IT->position. */
5811
5812 static void
5813 push_it (struct it *it, struct text_pos *position)
5814 {
5815 struct iterator_stack_entry *p;
5816
5817 eassert (it->sp < IT_STACK_SIZE);
5818 p = it->stack + it->sp;
5819
5820 p->stop_charpos = it->stop_charpos;
5821 p->prev_stop = it->prev_stop;
5822 p->base_level_stop = it->base_level_stop;
5823 p->cmp_it = it->cmp_it;
5824 eassert (it->face_id >= 0);
5825 p->face_id = it->face_id;
5826 p->string = it->string;
5827 p->method = it->method;
5828 p->from_overlay = it->from_overlay;
5829 switch (p->method)
5830 {
5831 case GET_FROM_IMAGE:
5832 p->u.image.object = it->object;
5833 p->u.image.image_id = it->image_id;
5834 p->u.image.slice = it->slice;
5835 break;
5836 case GET_FROM_STRETCH:
5837 p->u.stretch.object = it->object;
5838 break;
5839 }
5840 p->position = position ? *position : it->position;
5841 p->current = it->current;
5842 p->end_charpos = it->end_charpos;
5843 p->string_nchars = it->string_nchars;
5844 p->area = it->area;
5845 p->multibyte_p = it->multibyte_p;
5846 p->avoid_cursor_p = it->avoid_cursor_p;
5847 p->space_width = it->space_width;
5848 p->font_height = it->font_height;
5849 p->voffset = it->voffset;
5850 p->string_from_display_prop_p = it->string_from_display_prop_p;
5851 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5852 p->display_ellipsis_p = 0;
5853 p->line_wrap = it->line_wrap;
5854 p->bidi_p = it->bidi_p;
5855 p->paragraph_embedding = it->paragraph_embedding;
5856 p->from_disp_prop_p = it->from_disp_prop_p;
5857 ++it->sp;
5858
5859 /* Save the state of the bidi iterator as well. */
5860 if (it->bidi_p)
5861 bidi_push_it (&it->bidi_it);
5862 }
5863
5864 static void
5865 iterate_out_of_display_property (struct it *it)
5866 {
5867 int buffer_p = !STRINGP (it->string);
5868 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5869 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5870
5871 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5872
5873 /* Maybe initialize paragraph direction. If we are at the beginning
5874 of a new paragraph, next_element_from_buffer may not have a
5875 chance to do that. */
5876 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5877 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5878 /* prev_stop can be zero, so check against BEGV as well. */
5879 while (it->bidi_it.charpos >= bob
5880 && it->prev_stop <= it->bidi_it.charpos
5881 && it->bidi_it.charpos < CHARPOS (it->position)
5882 && it->bidi_it.charpos < eob)
5883 bidi_move_to_visually_next (&it->bidi_it);
5884 /* Record the stop_pos we just crossed, for when we cross it
5885 back, maybe. */
5886 if (it->bidi_it.charpos > CHARPOS (it->position))
5887 it->prev_stop = CHARPOS (it->position);
5888 /* If we ended up not where pop_it put us, resync IT's
5889 positional members with the bidi iterator. */
5890 if (it->bidi_it.charpos != CHARPOS (it->position))
5891 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5892 if (buffer_p)
5893 it->current.pos = it->position;
5894 else
5895 it->current.string_pos = it->position;
5896 }
5897
5898 /* Restore IT's settings from IT->stack. Called, for example, when no
5899 more overlay strings must be processed, and we return to delivering
5900 display elements from a buffer, or when the end of a string from a
5901 `display' property is reached and we return to delivering display
5902 elements from an overlay string, or from a buffer. */
5903
5904 static void
5905 pop_it (struct it *it)
5906 {
5907 struct iterator_stack_entry *p;
5908 int from_display_prop = it->from_disp_prop_p;
5909
5910 eassert (it->sp > 0);
5911 --it->sp;
5912 p = it->stack + it->sp;
5913 it->stop_charpos = p->stop_charpos;
5914 it->prev_stop = p->prev_stop;
5915 it->base_level_stop = p->base_level_stop;
5916 it->cmp_it = p->cmp_it;
5917 it->face_id = p->face_id;
5918 it->current = p->current;
5919 it->position = p->position;
5920 it->string = p->string;
5921 it->from_overlay = p->from_overlay;
5922 if (NILP (it->string))
5923 SET_TEXT_POS (it->current.string_pos, -1, -1);
5924 it->method = p->method;
5925 switch (it->method)
5926 {
5927 case GET_FROM_IMAGE:
5928 it->image_id = p->u.image.image_id;
5929 it->object = p->u.image.object;
5930 it->slice = p->u.image.slice;
5931 break;
5932 case GET_FROM_STRETCH:
5933 it->object = p->u.stretch.object;
5934 break;
5935 case GET_FROM_BUFFER:
5936 it->object = it->w->contents;
5937 break;
5938 case GET_FROM_STRING:
5939 it->object = it->string;
5940 break;
5941 case GET_FROM_DISPLAY_VECTOR:
5942 if (it->s)
5943 it->method = GET_FROM_C_STRING;
5944 else if (STRINGP (it->string))
5945 it->method = GET_FROM_STRING;
5946 else
5947 {
5948 it->method = GET_FROM_BUFFER;
5949 it->object = it->w->contents;
5950 }
5951 }
5952 it->end_charpos = p->end_charpos;
5953 it->string_nchars = p->string_nchars;
5954 it->area = p->area;
5955 it->multibyte_p = p->multibyte_p;
5956 it->avoid_cursor_p = p->avoid_cursor_p;
5957 it->space_width = p->space_width;
5958 it->font_height = p->font_height;
5959 it->voffset = p->voffset;
5960 it->string_from_display_prop_p = p->string_from_display_prop_p;
5961 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5962 it->line_wrap = p->line_wrap;
5963 it->bidi_p = p->bidi_p;
5964 it->paragraph_embedding = p->paragraph_embedding;
5965 it->from_disp_prop_p = p->from_disp_prop_p;
5966 if (it->bidi_p)
5967 {
5968 bidi_pop_it (&it->bidi_it);
5969 /* Bidi-iterate until we get out of the portion of text, if any,
5970 covered by a `display' text property or by an overlay with
5971 `display' property. (We cannot just jump there, because the
5972 internal coherency of the bidi iterator state can not be
5973 preserved across such jumps.) We also must determine the
5974 paragraph base direction if the overlay we just processed is
5975 at the beginning of a new paragraph. */
5976 if (from_display_prop
5977 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5978 iterate_out_of_display_property (it);
5979
5980 eassert ((BUFFERP (it->object)
5981 && IT_CHARPOS (*it) == it->bidi_it.charpos
5982 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5983 || (STRINGP (it->object)
5984 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5985 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5986 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5987 }
5988 }
5989
5990
5991 \f
5992 /***********************************************************************
5993 Moving over lines
5994 ***********************************************************************/
5995
5996 /* Set IT's current position to the previous line start. */
5997
5998 static void
5999 back_to_previous_line_start (struct it *it)
6000 {
6001 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6002
6003 DEC_BOTH (cp, bp);
6004 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6005 }
6006
6007
6008 /* Move IT to the next line start.
6009
6010 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6011 we skipped over part of the text (as opposed to moving the iterator
6012 continuously over the text). Otherwise, don't change the value
6013 of *SKIPPED_P.
6014
6015 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6016 iterator on the newline, if it was found.
6017
6018 Newlines may come from buffer text, overlay strings, or strings
6019 displayed via the `display' property. That's the reason we can't
6020 simply use find_newline_no_quit.
6021
6022 Note that this function may not skip over invisible text that is so
6023 because of text properties and immediately follows a newline. If
6024 it would, function reseat_at_next_visible_line_start, when called
6025 from set_iterator_to_next, would effectively make invisible
6026 characters following a newline part of the wrong glyph row, which
6027 leads to wrong cursor motion. */
6028
6029 static int
6030 forward_to_next_line_start (struct it *it, int *skipped_p,
6031 struct bidi_it *bidi_it_prev)
6032 {
6033 ptrdiff_t old_selective;
6034 int newline_found_p, n;
6035 const int MAX_NEWLINE_DISTANCE = 500;
6036
6037 /* If already on a newline, just consume it to avoid unintended
6038 skipping over invisible text below. */
6039 if (it->what == IT_CHARACTER
6040 && it->c == '\n'
6041 && CHARPOS (it->position) == IT_CHARPOS (*it))
6042 {
6043 if (it->bidi_p && bidi_it_prev)
6044 *bidi_it_prev = it->bidi_it;
6045 set_iterator_to_next (it, 0);
6046 it->c = 0;
6047 return 1;
6048 }
6049
6050 /* Don't handle selective display in the following. It's (a)
6051 unnecessary because it's done by the caller, and (b) leads to an
6052 infinite recursion because next_element_from_ellipsis indirectly
6053 calls this function. */
6054 old_selective = it->selective;
6055 it->selective = 0;
6056
6057 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6058 from buffer text. */
6059 for (n = newline_found_p = 0;
6060 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6061 n += STRINGP (it->string) ? 0 : 1)
6062 {
6063 if (!get_next_display_element (it))
6064 return 0;
6065 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6066 if (newline_found_p && it->bidi_p && bidi_it_prev)
6067 *bidi_it_prev = it->bidi_it;
6068 set_iterator_to_next (it, 0);
6069 }
6070
6071 /* If we didn't find a newline near enough, see if we can use a
6072 short-cut. */
6073 if (!newline_found_p)
6074 {
6075 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6076 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6077 1, &bytepos);
6078 Lisp_Object pos;
6079
6080 eassert (!STRINGP (it->string));
6081
6082 /* If there isn't any `display' property in sight, and no
6083 overlays, we can just use the position of the newline in
6084 buffer text. */
6085 if (it->stop_charpos >= limit
6086 || ((pos = Fnext_single_property_change (make_number (start),
6087 Qdisplay, Qnil,
6088 make_number (limit)),
6089 NILP (pos))
6090 && next_overlay_change (start) == ZV))
6091 {
6092 if (!it->bidi_p)
6093 {
6094 IT_CHARPOS (*it) = limit;
6095 IT_BYTEPOS (*it) = bytepos;
6096 }
6097 else
6098 {
6099 struct bidi_it bprev;
6100
6101 /* Help bidi.c avoid expensive searches for display
6102 properties and overlays, by telling it that there are
6103 none up to `limit'. */
6104 if (it->bidi_it.disp_pos < limit)
6105 {
6106 it->bidi_it.disp_pos = limit;
6107 it->bidi_it.disp_prop = 0;
6108 }
6109 do {
6110 bprev = it->bidi_it;
6111 bidi_move_to_visually_next (&it->bidi_it);
6112 } while (it->bidi_it.charpos != limit);
6113 IT_CHARPOS (*it) = limit;
6114 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6115 if (bidi_it_prev)
6116 *bidi_it_prev = bprev;
6117 }
6118 *skipped_p = newline_found_p = 1;
6119 }
6120 else
6121 {
6122 while (get_next_display_element (it)
6123 && !newline_found_p)
6124 {
6125 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6126 if (newline_found_p && it->bidi_p && bidi_it_prev)
6127 *bidi_it_prev = it->bidi_it;
6128 set_iterator_to_next (it, 0);
6129 }
6130 }
6131 }
6132
6133 it->selective = old_selective;
6134 return newline_found_p;
6135 }
6136
6137
6138 /* Set IT's current position to the previous visible line start. Skip
6139 invisible text that is so either due to text properties or due to
6140 selective display. Caution: this does not change IT->current_x and
6141 IT->hpos. */
6142
6143 static void
6144 back_to_previous_visible_line_start (struct it *it)
6145 {
6146 while (IT_CHARPOS (*it) > BEGV)
6147 {
6148 back_to_previous_line_start (it);
6149
6150 if (IT_CHARPOS (*it) <= BEGV)
6151 break;
6152
6153 /* If selective > 0, then lines indented more than its value are
6154 invisible. */
6155 if (it->selective > 0
6156 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6157 it->selective))
6158 continue;
6159
6160 /* Check the newline before point for invisibility. */
6161 {
6162 Lisp_Object prop;
6163 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6164 Qinvisible, it->window);
6165 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6166 continue;
6167 }
6168
6169 if (IT_CHARPOS (*it) <= BEGV)
6170 break;
6171
6172 {
6173 struct it it2;
6174 void *it2data = NULL;
6175 ptrdiff_t pos;
6176 ptrdiff_t beg, end;
6177 Lisp_Object val, overlay;
6178
6179 SAVE_IT (it2, *it, it2data);
6180
6181 /* If newline is part of a composition, continue from start of composition */
6182 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6183 && beg < IT_CHARPOS (*it))
6184 goto replaced;
6185
6186 /* If newline is replaced by a display property, find start of overlay
6187 or interval and continue search from that point. */
6188 pos = --IT_CHARPOS (it2);
6189 --IT_BYTEPOS (it2);
6190 it2.sp = 0;
6191 bidi_unshelve_cache (NULL, 0);
6192 it2.string_from_display_prop_p = 0;
6193 it2.from_disp_prop_p = 0;
6194 if (handle_display_prop (&it2) == HANDLED_RETURN
6195 && !NILP (val = get_char_property_and_overlay
6196 (make_number (pos), Qdisplay, Qnil, &overlay))
6197 && (OVERLAYP (overlay)
6198 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6199 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6200 {
6201 RESTORE_IT (it, it, it2data);
6202 goto replaced;
6203 }
6204
6205 /* Newline is not replaced by anything -- so we are done. */
6206 RESTORE_IT (it, it, it2data);
6207 break;
6208
6209 replaced:
6210 if (beg < BEGV)
6211 beg = BEGV;
6212 IT_CHARPOS (*it) = beg;
6213 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6214 }
6215 }
6216
6217 it->continuation_lines_width = 0;
6218
6219 eassert (IT_CHARPOS (*it) >= BEGV);
6220 eassert (IT_CHARPOS (*it) == BEGV
6221 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6222 CHECK_IT (it);
6223 }
6224
6225
6226 /* Reseat iterator IT at the previous visible line start. Skip
6227 invisible text that is so either due to text properties or due to
6228 selective display. At the end, update IT's overlay information,
6229 face information etc. */
6230
6231 void
6232 reseat_at_previous_visible_line_start (struct it *it)
6233 {
6234 back_to_previous_visible_line_start (it);
6235 reseat (it, it->current.pos, 1);
6236 CHECK_IT (it);
6237 }
6238
6239
6240 /* Reseat iterator IT on the next visible line start in the current
6241 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6242 preceding the line start. Skip over invisible text that is so
6243 because of selective display. Compute faces, overlays etc at the
6244 new position. Note that this function does not skip over text that
6245 is invisible because of text properties. */
6246
6247 static void
6248 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6249 {
6250 int newline_found_p, skipped_p = 0;
6251 struct bidi_it bidi_it_prev;
6252
6253 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6254
6255 /* Skip over lines that are invisible because they are indented
6256 more than the value of IT->selective. */
6257 if (it->selective > 0)
6258 while (IT_CHARPOS (*it) < ZV
6259 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6260 it->selective))
6261 {
6262 eassert (IT_BYTEPOS (*it) == BEGV
6263 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6264 newline_found_p =
6265 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6266 }
6267
6268 /* Position on the newline if that's what's requested. */
6269 if (on_newline_p && newline_found_p)
6270 {
6271 if (STRINGP (it->string))
6272 {
6273 if (IT_STRING_CHARPOS (*it) > 0)
6274 {
6275 if (!it->bidi_p)
6276 {
6277 --IT_STRING_CHARPOS (*it);
6278 --IT_STRING_BYTEPOS (*it);
6279 }
6280 else
6281 {
6282 /* We need to restore the bidi iterator to the state
6283 it had on the newline, and resync the IT's
6284 position with that. */
6285 it->bidi_it = bidi_it_prev;
6286 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6287 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6288 }
6289 }
6290 }
6291 else if (IT_CHARPOS (*it) > BEGV)
6292 {
6293 if (!it->bidi_p)
6294 {
6295 --IT_CHARPOS (*it);
6296 --IT_BYTEPOS (*it);
6297 }
6298 else
6299 {
6300 /* We need to restore the bidi iterator to the state it
6301 had on the newline and resync IT with that. */
6302 it->bidi_it = bidi_it_prev;
6303 IT_CHARPOS (*it) = it->bidi_it.charpos;
6304 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6305 }
6306 reseat (it, it->current.pos, 0);
6307 }
6308 }
6309 else if (skipped_p)
6310 reseat (it, it->current.pos, 0);
6311
6312 CHECK_IT (it);
6313 }
6314
6315
6316 \f
6317 /***********************************************************************
6318 Changing an iterator's position
6319 ***********************************************************************/
6320
6321 /* Change IT's current position to POS in current_buffer. If FORCE_P
6322 is non-zero, always check for text properties at the new position.
6323 Otherwise, text properties are only looked up if POS >=
6324 IT->check_charpos of a property. */
6325
6326 static void
6327 reseat (struct it *it, struct text_pos pos, int force_p)
6328 {
6329 ptrdiff_t original_pos = IT_CHARPOS (*it);
6330
6331 reseat_1 (it, pos, 0);
6332
6333 /* Determine where to check text properties. Avoid doing it
6334 where possible because text property lookup is very expensive. */
6335 if (force_p
6336 || CHARPOS (pos) > it->stop_charpos
6337 || CHARPOS (pos) < original_pos)
6338 {
6339 if (it->bidi_p)
6340 {
6341 /* For bidi iteration, we need to prime prev_stop and
6342 base_level_stop with our best estimations. */
6343 /* Implementation note: Of course, POS is not necessarily a
6344 stop position, so assigning prev_pos to it is a lie; we
6345 should have called compute_stop_backwards. However, if
6346 the current buffer does not include any R2L characters,
6347 that call would be a waste of cycles, because the
6348 iterator will never move back, and thus never cross this
6349 "fake" stop position. So we delay that backward search
6350 until the time we really need it, in next_element_from_buffer. */
6351 if (CHARPOS (pos) != it->prev_stop)
6352 it->prev_stop = CHARPOS (pos);
6353 if (CHARPOS (pos) < it->base_level_stop)
6354 it->base_level_stop = 0; /* meaning it's unknown */
6355 handle_stop (it);
6356 }
6357 else
6358 {
6359 handle_stop (it);
6360 it->prev_stop = it->base_level_stop = 0;
6361 }
6362
6363 }
6364
6365 CHECK_IT (it);
6366 }
6367
6368
6369 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6370 IT->stop_pos to POS, also. */
6371
6372 static void
6373 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6374 {
6375 /* Don't call this function when scanning a C string. */
6376 eassert (it->s == NULL);
6377
6378 /* POS must be a reasonable value. */
6379 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6380
6381 it->current.pos = it->position = pos;
6382 it->end_charpos = ZV;
6383 it->dpvec = NULL;
6384 it->current.dpvec_index = -1;
6385 it->current.overlay_string_index = -1;
6386 IT_STRING_CHARPOS (*it) = -1;
6387 IT_STRING_BYTEPOS (*it) = -1;
6388 it->string = Qnil;
6389 it->method = GET_FROM_BUFFER;
6390 it->object = it->w->contents;
6391 it->area = TEXT_AREA;
6392 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6393 it->sp = 0;
6394 it->string_from_display_prop_p = 0;
6395 it->string_from_prefix_prop_p = 0;
6396
6397 it->from_disp_prop_p = 0;
6398 it->face_before_selective_p = 0;
6399 if (it->bidi_p)
6400 {
6401 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6402 &it->bidi_it);
6403 bidi_unshelve_cache (NULL, 0);
6404 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6405 it->bidi_it.string.s = NULL;
6406 it->bidi_it.string.lstring = Qnil;
6407 it->bidi_it.string.bufpos = 0;
6408 it->bidi_it.string.unibyte = 0;
6409 it->bidi_it.w = it->w;
6410 }
6411
6412 if (set_stop_p)
6413 {
6414 it->stop_charpos = CHARPOS (pos);
6415 it->base_level_stop = CHARPOS (pos);
6416 }
6417 /* This make the information stored in it->cmp_it invalidate. */
6418 it->cmp_it.id = -1;
6419 }
6420
6421
6422 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6423 If S is non-null, it is a C string to iterate over. Otherwise,
6424 STRING gives a Lisp string to iterate over.
6425
6426 If PRECISION > 0, don't return more then PRECISION number of
6427 characters from the string.
6428
6429 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6430 characters have been returned. FIELD_WIDTH < 0 means an infinite
6431 field width.
6432
6433 MULTIBYTE = 0 means disable processing of multibyte characters,
6434 MULTIBYTE > 0 means enable it,
6435 MULTIBYTE < 0 means use IT->multibyte_p.
6436
6437 IT must be initialized via a prior call to init_iterator before
6438 calling this function. */
6439
6440 static void
6441 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6442 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6443 int multibyte)
6444 {
6445 /* No region in strings. */
6446 it->region_beg_charpos = it->region_end_charpos = -1;
6447
6448 /* No text property checks performed by default, but see below. */
6449 it->stop_charpos = -1;
6450
6451 /* Set iterator position and end position. */
6452 memset (&it->current, 0, sizeof it->current);
6453 it->current.overlay_string_index = -1;
6454 it->current.dpvec_index = -1;
6455 eassert (charpos >= 0);
6456
6457 /* If STRING is specified, use its multibyteness, otherwise use the
6458 setting of MULTIBYTE, if specified. */
6459 if (multibyte >= 0)
6460 it->multibyte_p = multibyte > 0;
6461
6462 /* Bidirectional reordering of strings is controlled by the default
6463 value of bidi-display-reordering. Don't try to reorder while
6464 loading loadup.el, as the necessary character property tables are
6465 not yet available. */
6466 it->bidi_p =
6467 NILP (Vpurify_flag)
6468 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6469
6470 if (s == NULL)
6471 {
6472 eassert (STRINGP (string));
6473 it->string = string;
6474 it->s = NULL;
6475 it->end_charpos = it->string_nchars = SCHARS (string);
6476 it->method = GET_FROM_STRING;
6477 it->current.string_pos = string_pos (charpos, string);
6478
6479 if (it->bidi_p)
6480 {
6481 it->bidi_it.string.lstring = string;
6482 it->bidi_it.string.s = NULL;
6483 it->bidi_it.string.schars = it->end_charpos;
6484 it->bidi_it.string.bufpos = 0;
6485 it->bidi_it.string.from_disp_str = 0;
6486 it->bidi_it.string.unibyte = !it->multibyte_p;
6487 it->bidi_it.w = it->w;
6488 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6489 FRAME_WINDOW_P (it->f), &it->bidi_it);
6490 }
6491 }
6492 else
6493 {
6494 it->s = (const unsigned char *) s;
6495 it->string = Qnil;
6496
6497 /* Note that we use IT->current.pos, not it->current.string_pos,
6498 for displaying C strings. */
6499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6500 if (it->multibyte_p)
6501 {
6502 it->current.pos = c_string_pos (charpos, s, 1);
6503 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6504 }
6505 else
6506 {
6507 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6508 it->end_charpos = it->string_nchars = strlen (s);
6509 }
6510
6511 if (it->bidi_p)
6512 {
6513 it->bidi_it.string.lstring = Qnil;
6514 it->bidi_it.string.s = (const unsigned char *) s;
6515 it->bidi_it.string.schars = it->end_charpos;
6516 it->bidi_it.string.bufpos = 0;
6517 it->bidi_it.string.from_disp_str = 0;
6518 it->bidi_it.string.unibyte = !it->multibyte_p;
6519 it->bidi_it.w = it->w;
6520 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6521 &it->bidi_it);
6522 }
6523 it->method = GET_FROM_C_STRING;
6524 }
6525
6526 /* PRECISION > 0 means don't return more than PRECISION characters
6527 from the string. */
6528 if (precision > 0 && it->end_charpos - charpos > precision)
6529 {
6530 it->end_charpos = it->string_nchars = charpos + precision;
6531 if (it->bidi_p)
6532 it->bidi_it.string.schars = it->end_charpos;
6533 }
6534
6535 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6536 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6537 FIELD_WIDTH < 0 means infinite field width. This is useful for
6538 padding with `-' at the end of a mode line. */
6539 if (field_width < 0)
6540 field_width = INFINITY;
6541 /* Implementation note: We deliberately don't enlarge
6542 it->bidi_it.string.schars here to fit it->end_charpos, because
6543 the bidi iterator cannot produce characters out of thin air. */
6544 if (field_width > it->end_charpos - charpos)
6545 it->end_charpos = charpos + field_width;
6546
6547 /* Use the standard display table for displaying strings. */
6548 if (DISP_TABLE_P (Vstandard_display_table))
6549 it->dp = XCHAR_TABLE (Vstandard_display_table);
6550
6551 it->stop_charpos = charpos;
6552 it->prev_stop = charpos;
6553 it->base_level_stop = 0;
6554 if (it->bidi_p)
6555 {
6556 it->bidi_it.first_elt = 1;
6557 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6558 it->bidi_it.disp_pos = -1;
6559 }
6560 if (s == NULL && it->multibyte_p)
6561 {
6562 ptrdiff_t endpos = SCHARS (it->string);
6563 if (endpos > it->end_charpos)
6564 endpos = it->end_charpos;
6565 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6566 it->string);
6567 }
6568 CHECK_IT (it);
6569 }
6570
6571
6572 \f
6573 /***********************************************************************
6574 Iteration
6575 ***********************************************************************/
6576
6577 /* Map enum it_method value to corresponding next_element_from_* function. */
6578
6579 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6580 {
6581 next_element_from_buffer,
6582 next_element_from_display_vector,
6583 next_element_from_string,
6584 next_element_from_c_string,
6585 next_element_from_image,
6586 next_element_from_stretch
6587 };
6588
6589 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6590
6591
6592 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6593 (possibly with the following characters). */
6594
6595 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6596 ((IT)->cmp_it.id >= 0 \
6597 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6598 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6599 END_CHARPOS, (IT)->w, \
6600 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6601 (IT)->string)))
6602
6603
6604 /* Lookup the char-table Vglyphless_char_display for character C (-1
6605 if we want information for no-font case), and return the display
6606 method symbol. By side-effect, update it->what and
6607 it->glyphless_method. This function is called from
6608 get_next_display_element for each character element, and from
6609 x_produce_glyphs when no suitable font was found. */
6610
6611 Lisp_Object
6612 lookup_glyphless_char_display (int c, struct it *it)
6613 {
6614 Lisp_Object glyphless_method = Qnil;
6615
6616 if (CHAR_TABLE_P (Vglyphless_char_display)
6617 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6618 {
6619 if (c >= 0)
6620 {
6621 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6622 if (CONSP (glyphless_method))
6623 glyphless_method = FRAME_WINDOW_P (it->f)
6624 ? XCAR (glyphless_method)
6625 : XCDR (glyphless_method);
6626 }
6627 else
6628 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6629 }
6630
6631 retry:
6632 if (NILP (glyphless_method))
6633 {
6634 if (c >= 0)
6635 /* The default is to display the character by a proper font. */
6636 return Qnil;
6637 /* The default for the no-font case is to display an empty box. */
6638 glyphless_method = Qempty_box;
6639 }
6640 if (EQ (glyphless_method, Qzero_width))
6641 {
6642 if (c >= 0)
6643 return glyphless_method;
6644 /* This method can't be used for the no-font case. */
6645 glyphless_method = Qempty_box;
6646 }
6647 if (EQ (glyphless_method, Qthin_space))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6649 else if (EQ (glyphless_method, Qempty_box))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6651 else if (EQ (glyphless_method, Qhex_code))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6653 else if (STRINGP (glyphless_method))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6655 else
6656 {
6657 /* Invalid value. We use the default method. */
6658 glyphless_method = Qnil;
6659 goto retry;
6660 }
6661 it->what = IT_GLYPHLESS;
6662 return glyphless_method;
6663 }
6664
6665 /* Merge escape glyph face and cache the result. */
6666
6667 static struct frame *last_escape_glyph_frame = NULL;
6668 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6669 static int last_escape_glyph_merged_face_id = 0;
6670
6671 static int
6672 merge_escape_glyph_face (struct it *it)
6673 {
6674 int face_id;
6675
6676 if (it->f == last_escape_glyph_frame
6677 && it->face_id == last_escape_glyph_face_id)
6678 face_id = last_escape_glyph_merged_face_id;
6679 else
6680 {
6681 /* Merge the `escape-glyph' face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6683 last_escape_glyph_frame = it->f;
6684 last_escape_glyph_face_id = it->face_id;
6685 last_escape_glyph_merged_face_id = face_id;
6686 }
6687 return face_id;
6688 }
6689
6690 /* Likewise for glyphless glyph face. */
6691
6692 static struct frame *last_glyphless_glyph_frame = NULL;
6693 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 static int last_glyphless_glyph_merged_face_id = 0;
6695
6696 int
6697 merge_glyphless_glyph_face (struct it *it)
6698 {
6699 int face_id;
6700
6701 if (it->f == last_glyphless_glyph_frame
6702 && it->face_id == last_glyphless_glyph_face_id)
6703 face_id = last_glyphless_glyph_merged_face_id;
6704 else
6705 {
6706 /* Merge the `glyphless-char' face into the current face. */
6707 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6708 last_glyphless_glyph_frame = it->f;
6709 last_glyphless_glyph_face_id = it->face_id;
6710 last_glyphless_glyph_merged_face_id = face_id;
6711 }
6712 return face_id;
6713 }
6714
6715 /* Load IT's display element fields with information about the next
6716 display element from the current position of IT. Value is zero if
6717 end of buffer (or C string) is reached. */
6718
6719 static int
6720 get_next_display_element (struct it *it)
6721 {
6722 /* Non-zero means that we found a display element. Zero means that
6723 we hit the end of what we iterate over. Performance note: the
6724 function pointer `method' used here turns out to be faster than
6725 using a sequence of if-statements. */
6726 int success_p;
6727
6728 get_next:
6729 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6730
6731 if (it->what == IT_CHARACTER)
6732 {
6733 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6734 and only if (a) the resolved directionality of that character
6735 is R..." */
6736 /* FIXME: Do we need an exception for characters from display
6737 tables? */
6738 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6739 it->c = bidi_mirror_char (it->c);
6740 /* Map via display table or translate control characters.
6741 IT->c, IT->len etc. have been set to the next character by
6742 the function call above. If we have a display table, and it
6743 contains an entry for IT->c, translate it. Don't do this if
6744 IT->c itself comes from a display table, otherwise we could
6745 end up in an infinite recursion. (An alternative could be to
6746 count the recursion depth of this function and signal an
6747 error when a certain maximum depth is reached.) Is it worth
6748 it? */
6749 if (success_p && it->dpvec == NULL)
6750 {
6751 Lisp_Object dv;
6752 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6753 int nonascii_space_p = 0;
6754 int nonascii_hyphen_p = 0;
6755 int c = it->c; /* This is the character to display. */
6756
6757 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6758 {
6759 eassert (SINGLE_BYTE_CHAR_P (c));
6760 if (unibyte_display_via_language_environment)
6761 {
6762 c = DECODE_CHAR (unibyte, c);
6763 if (c < 0)
6764 c = BYTE8_TO_CHAR (it->c);
6765 }
6766 else
6767 c = BYTE8_TO_CHAR (it->c);
6768 }
6769
6770 if (it->dp
6771 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6772 VECTORP (dv)))
6773 {
6774 struct Lisp_Vector *v = XVECTOR (dv);
6775
6776 /* Return the first character from the display table
6777 entry, if not empty. If empty, don't display the
6778 current character. */
6779 if (v->header.size)
6780 {
6781 it->dpvec_char_len = it->len;
6782 it->dpvec = v->contents;
6783 it->dpend = v->contents + v->header.size;
6784 it->current.dpvec_index = 0;
6785 it->dpvec_face_id = -1;
6786 it->saved_face_id = it->face_id;
6787 it->method = GET_FROM_DISPLAY_VECTOR;
6788 it->ellipsis_p = 0;
6789 }
6790 else
6791 {
6792 set_iterator_to_next (it, 0);
6793 }
6794 goto get_next;
6795 }
6796
6797 if (! NILP (lookup_glyphless_char_display (c, it)))
6798 {
6799 if (it->what == IT_GLYPHLESS)
6800 goto done;
6801 /* Don't display this character. */
6802 set_iterator_to_next (it, 0);
6803 goto get_next;
6804 }
6805
6806 /* If `nobreak-char-display' is non-nil, we display
6807 non-ASCII spaces and hyphens specially. */
6808 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6809 {
6810 if (c == 0xA0)
6811 nonascii_space_p = 1;
6812 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6813 nonascii_hyphen_p = 1;
6814 }
6815
6816 /* Translate control characters into `\003' or `^C' form.
6817 Control characters coming from a display table entry are
6818 currently not translated because we use IT->dpvec to hold
6819 the translation. This could easily be changed but I
6820 don't believe that it is worth doing.
6821
6822 The characters handled by `nobreak-char-display' must be
6823 translated too.
6824
6825 Non-printable characters and raw-byte characters are also
6826 translated to octal form. */
6827 if (((c < ' ' || c == 127) /* ASCII control chars */
6828 ? (it->area != TEXT_AREA
6829 /* In mode line, treat \n, \t like other crl chars. */
6830 || (c != '\t'
6831 && it->glyph_row
6832 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6833 || (c != '\n' && c != '\t'))
6834 : (nonascii_space_p
6835 || nonascii_hyphen_p
6836 || CHAR_BYTE8_P (c)
6837 || ! CHAR_PRINTABLE_P (c))))
6838 {
6839 /* C is a control character, non-ASCII space/hyphen,
6840 raw-byte, or a non-printable character which must be
6841 displayed either as '\003' or as `^C' where the '\\'
6842 and '^' can be defined in the display table. Fill
6843 IT->ctl_chars with glyphs for what we have to
6844 display. Then, set IT->dpvec to these glyphs. */
6845 Lisp_Object gc;
6846 int ctl_len;
6847 int face_id;
6848 int lface_id = 0;
6849 int escape_glyph;
6850
6851 /* Handle control characters with ^. */
6852
6853 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6854 {
6855 int g;
6856
6857 g = '^'; /* default glyph for Control */
6858 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6859 if (it->dp
6860 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6861 {
6862 g = GLYPH_CODE_CHAR (gc);
6863 lface_id = GLYPH_CODE_FACE (gc);
6864 }
6865
6866 face_id = (lface_id
6867 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6868 : merge_escape_glyph_face (it));
6869
6870 XSETINT (it->ctl_chars[0], g);
6871 XSETINT (it->ctl_chars[1], c ^ 0100);
6872 ctl_len = 2;
6873 goto display_control;
6874 }
6875
6876 /* Handle non-ascii space in the mode where it only gets
6877 highlighting. */
6878
6879 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6880 {
6881 /* Merge `nobreak-space' into the current face. */
6882 face_id = merge_faces (it->f, Qnobreak_space, 0,
6883 it->face_id);
6884 XSETINT (it->ctl_chars[0], ' ');
6885 ctl_len = 1;
6886 goto display_control;
6887 }
6888
6889 /* Handle sequences that start with the "escape glyph". */
6890
6891 /* the default escape glyph is \. */
6892 escape_glyph = '\\';
6893
6894 if (it->dp
6895 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6896 {
6897 escape_glyph = GLYPH_CODE_CHAR (gc);
6898 lface_id = GLYPH_CODE_FACE (gc);
6899 }
6900
6901 face_id = (lface_id
6902 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6903 : merge_escape_glyph_face (it));
6904
6905 /* Draw non-ASCII hyphen with just highlighting: */
6906
6907 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6908 {
6909 XSETINT (it->ctl_chars[0], '-');
6910 ctl_len = 1;
6911 goto display_control;
6912 }
6913
6914 /* Draw non-ASCII space/hyphen with escape glyph: */
6915
6916 if (nonascii_space_p || nonascii_hyphen_p)
6917 {
6918 XSETINT (it->ctl_chars[0], escape_glyph);
6919 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6920 ctl_len = 2;
6921 goto display_control;
6922 }
6923
6924 {
6925 char str[10];
6926 int len, i;
6927
6928 if (CHAR_BYTE8_P (c))
6929 /* Display \200 instead of \17777600. */
6930 c = CHAR_TO_BYTE8 (c);
6931 len = sprintf (str, "%03o", c);
6932
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 for (i = 0; i < len; i++)
6935 XSETINT (it->ctl_chars[i + 1], str[i]);
6936 ctl_len = len + 1;
6937 }
6938
6939 display_control:
6940 /* Set up IT->dpvec and return first character from it. */
6941 it->dpvec_char_len = it->len;
6942 it->dpvec = it->ctl_chars;
6943 it->dpend = it->dpvec + ctl_len;
6944 it->current.dpvec_index = 0;
6945 it->dpvec_face_id = face_id;
6946 it->saved_face_id = it->face_id;
6947 it->method = GET_FROM_DISPLAY_VECTOR;
6948 it->ellipsis_p = 0;
6949 goto get_next;
6950 }
6951 it->char_to_display = c;
6952 }
6953 else if (success_p)
6954 {
6955 it->char_to_display = it->c;
6956 }
6957 }
6958
6959 /* Adjust face id for a multibyte character. There are no multibyte
6960 character in unibyte text. */
6961 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6962 && it->multibyte_p
6963 && success_p
6964 && FRAME_WINDOW_P (it->f))
6965 {
6966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6967
6968 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6969 {
6970 /* Automatic composition with glyph-string. */
6971 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6972
6973 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6974 }
6975 else
6976 {
6977 ptrdiff_t pos = (it->s ? -1
6978 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6979 : IT_CHARPOS (*it));
6980 int c;
6981
6982 if (it->what == IT_CHARACTER)
6983 c = it->char_to_display;
6984 else
6985 {
6986 struct composition *cmp = composition_table[it->cmp_it.id];
6987 int i;
6988
6989 c = ' ';
6990 for (i = 0; i < cmp->glyph_len; i++)
6991 /* TAB in a composition means display glyphs with
6992 padding space on the left or right. */
6993 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6994 break;
6995 }
6996 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6997 }
6998 }
6999
7000 done:
7001 /* Is this character the last one of a run of characters with
7002 box? If yes, set IT->end_of_box_run_p to 1. */
7003 if (it->face_box_p
7004 && it->s == NULL)
7005 {
7006 if (it->method == GET_FROM_STRING && it->sp)
7007 {
7008 int face_id = underlying_face_id (it);
7009 struct face *face = FACE_FROM_ID (it->f, face_id);
7010
7011 if (face)
7012 {
7013 if (face->box == FACE_NO_BOX)
7014 {
7015 /* If the box comes from face properties in a
7016 display string, check faces in that string. */
7017 int string_face_id = face_after_it_pos (it);
7018 it->end_of_box_run_p
7019 = (FACE_FROM_ID (it->f, string_face_id)->box
7020 == FACE_NO_BOX);
7021 }
7022 /* Otherwise, the box comes from the underlying face.
7023 If this is the last string character displayed, check
7024 the next buffer location. */
7025 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7026 && (it->current.overlay_string_index
7027 == it->n_overlay_strings - 1))
7028 {
7029 ptrdiff_t ignore;
7030 int next_face_id;
7031 struct text_pos pos = it->current.pos;
7032 INC_TEXT_POS (pos, it->multibyte_p);
7033
7034 next_face_id = face_at_buffer_position
7035 (it->w, CHARPOS (pos), it->region_beg_charpos,
7036 it->region_end_charpos, &ignore,
7037 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7038 -1);
7039 it->end_of_box_run_p
7040 = (FACE_FROM_ID (it->f, next_face_id)->box
7041 == FACE_NO_BOX);
7042 }
7043 }
7044 }
7045 /* next_element_from_display_vector sets this flag according to
7046 faces of the display vector glyphs, see there. */
7047 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7048 {
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7053 }
7054 }
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7061 {
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7064 }
7065
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7068 }
7069
7070
7071 /* Move IT to the next display element.
7072
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7075
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7085
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7088 {
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7093
7094 switch (it->method)
7095 {
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7103 {
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7106
7107 if (! it->bidi_p)
7108 {
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7112 {
7113 it->cmp_it.from = it->cmp_it.to;
7114 }
7115 else
7116 {
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7121 }
7122 }
7123 else if (! it->cmp_it.reversed_p)
7124 {
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 {
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7138 }
7139 else
7140 {
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7150 }
7151 }
7152 else
7153 {
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7163 {
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7166 }
7167 else
7168 {
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7178 }
7179 }
7180 }
7181 else
7182 {
7183 eassert (it->len != 0);
7184
7185 if (!it->bidi_p)
7186 {
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7189 }
7190 else
7191 {
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7201 {
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7209 }
7210 }
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7212 }
7213 break;
7214
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7223 {
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7226 }
7227 else
7228 {
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7232 }
7233 break;
7234
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7241
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7245
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7247 {
7248 int recheck_faces = it->ellipsis_p;
7249
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7255 {
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7258 }
7259
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7262
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7267 {
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = 1;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7274 }
7275
7276 /* Maybe recheck faces after display vector */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7279 }
7280 break;
7281
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7291 {
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7297 }
7298 else
7299 {
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos . */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7304 }
7305 if (it->cmp_it.id >= 0)
7306 {
7307 int i;
7308
7309 if (! it->bidi_p)
7310 {
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7316 {
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7322 }
7323 }
7324 else if (! it->cmp_it.reversed_p)
7325 {
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7330
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7334 {
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7342 }
7343 }
7344 else
7345 {
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7353 {
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7361 }
7362 }
7363 }
7364 else
7365 {
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 {
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7380
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7385 {
7386 ptrdiff_t stop = it->end_charpos;
7387
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7394 }
7395 }
7396 }
7397
7398 consider_string_end:
7399
7400 if (it->current.overlay_string_index >= 0)
7401 {
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7405 {
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7410 }
7411 }
7412 else
7413 {
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7420 {
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7424 }
7425 }
7426 break;
7427
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7438
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7442 }
7443
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7447 }
7448
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7453
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7457
7458 static int
7459 next_element_from_display_vector (struct it *it)
7460 {
7461 Lisp_Object gc;
7462 int prev_face_id = it->face_id;
7463 int next_face_id;
7464
7465 /* Precondition. */
7466 eassert (it->dpvec && it->current.dpvec_index >= 0);
7467
7468 it->face_id = it->saved_face_id;
7469
7470 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7471 That seemed totally bogus - so I changed it... */
7472 gc = it->dpvec[it->current.dpvec_index];
7473
7474 if (GLYPH_CODE_P (gc))
7475 {
7476 struct face *this_face, *prev_face, *next_face;
7477
7478 it->c = GLYPH_CODE_CHAR (gc);
7479 it->len = CHAR_BYTES (it->c);
7480
7481 /* The entry may contain a face id to use. Such a face id is
7482 the id of a Lisp face, not a realized face. A face id of
7483 zero means no face is specified. */
7484 if (it->dpvec_face_id >= 0)
7485 it->face_id = it->dpvec_face_id;
7486 else
7487 {
7488 int lface_id = GLYPH_CODE_FACE (gc);
7489 if (lface_id > 0)
7490 it->face_id = merge_faces (it->f, Qt, lface_id,
7491 it->saved_face_id);
7492 }
7493
7494 /* Glyphs in the display vector could have the box face, so we
7495 need to set the related flags in the iterator, as
7496 appropriate. */
7497 this_face = FACE_FROM_ID (it->f, it->face_id);
7498 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7499
7500 /* Is this character the first character of a box-face run? */
7501 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7502 && (!prev_face
7503 || prev_face->box == FACE_NO_BOX));
7504
7505 /* For the last character of the box-face run, we need to look
7506 either at the next glyph from the display vector, or at the
7507 face we saw before the display vector. */
7508 next_face_id = it->saved_face_id;
7509 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7510 {
7511 if (it->dpvec_face_id >= 0)
7512 next_face_id = it->dpvec_face_id;
7513 else
7514 {
7515 int lface_id =
7516 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7517
7518 if (lface_id > 0)
7519 next_face_id = merge_faces (it->f, Qt, lface_id,
7520 it->saved_face_id);
7521 }
7522 }
7523 next_face = FACE_FROM_ID (it->f, next_face_id);
7524 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7525 && (!next_face
7526 || next_face->box == FACE_NO_BOX));
7527 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7528 }
7529 else
7530 /* Display table entry is invalid. Return a space. */
7531 it->c = ' ', it->len = 1;
7532
7533 /* Don't change position and object of the iterator here. They are
7534 still the values of the character that had this display table
7535 entry or was translated, and that's what we want. */
7536 it->what = IT_CHARACTER;
7537 return 1;
7538 }
7539
7540 /* Get the first element of string/buffer in the visual order, after
7541 being reseated to a new position in a string or a buffer. */
7542 static void
7543 get_visually_first_element (struct it *it)
7544 {
7545 int string_p = STRINGP (it->string) || it->s;
7546 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7547 ptrdiff_t bob = (string_p ? 0 : BEGV);
7548
7549 if (STRINGP (it->string))
7550 {
7551 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7552 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7553 }
7554 else
7555 {
7556 it->bidi_it.charpos = IT_CHARPOS (*it);
7557 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7558 }
7559
7560 if (it->bidi_it.charpos == eob)
7561 {
7562 /* Nothing to do, but reset the FIRST_ELT flag, like
7563 bidi_paragraph_init does, because we are not going to
7564 call it. */
7565 it->bidi_it.first_elt = 0;
7566 }
7567 else if (it->bidi_it.charpos == bob
7568 || (!string_p
7569 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7570 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7571 {
7572 /* If we are at the beginning of a line/string, we can produce
7573 the next element right away. */
7574 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7575 bidi_move_to_visually_next (&it->bidi_it);
7576 }
7577 else
7578 {
7579 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7580
7581 /* We need to prime the bidi iterator starting at the line's or
7582 string's beginning, before we will be able to produce the
7583 next element. */
7584 if (string_p)
7585 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7586 else
7587 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7588 IT_BYTEPOS (*it), -1,
7589 &it->bidi_it.bytepos);
7590 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7591 do
7592 {
7593 /* Now return to buffer/string position where we were asked
7594 to get the next display element, and produce that. */
7595 bidi_move_to_visually_next (&it->bidi_it);
7596 }
7597 while (it->bidi_it.bytepos != orig_bytepos
7598 && it->bidi_it.charpos < eob);
7599 }
7600
7601 /* Adjust IT's position information to where we ended up. */
7602 if (STRINGP (it->string))
7603 {
7604 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7605 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7606 }
7607 else
7608 {
7609 IT_CHARPOS (*it) = it->bidi_it.charpos;
7610 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7611 }
7612
7613 if (STRINGP (it->string) || !it->s)
7614 {
7615 ptrdiff_t stop, charpos, bytepos;
7616
7617 if (STRINGP (it->string))
7618 {
7619 eassert (!it->s);
7620 stop = SCHARS (it->string);
7621 if (stop > it->end_charpos)
7622 stop = it->end_charpos;
7623 charpos = IT_STRING_CHARPOS (*it);
7624 bytepos = IT_STRING_BYTEPOS (*it);
7625 }
7626 else
7627 {
7628 stop = it->end_charpos;
7629 charpos = IT_CHARPOS (*it);
7630 bytepos = IT_BYTEPOS (*it);
7631 }
7632 if (it->bidi_it.scan_dir < 0)
7633 stop = -1;
7634 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7635 it->string);
7636 }
7637 }
7638
7639 /* Load IT with the next display element from Lisp string IT->string.
7640 IT->current.string_pos is the current position within the string.
7641 If IT->current.overlay_string_index >= 0, the Lisp string is an
7642 overlay string. */
7643
7644 static int
7645 next_element_from_string (struct it *it)
7646 {
7647 struct text_pos position;
7648
7649 eassert (STRINGP (it->string));
7650 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7651 eassert (IT_STRING_CHARPOS (*it) >= 0);
7652 position = it->current.string_pos;
7653
7654 /* With bidi reordering, the character to display might not be the
7655 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7656 that we were reseat()ed to a new string, whose paragraph
7657 direction is not known. */
7658 if (it->bidi_p && it->bidi_it.first_elt)
7659 {
7660 get_visually_first_element (it);
7661 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7662 }
7663
7664 /* Time to check for invisible text? */
7665 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7666 {
7667 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7668 {
7669 if (!(!it->bidi_p
7670 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7671 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7672 {
7673 /* With bidi non-linear iteration, we could find
7674 ourselves far beyond the last computed stop_charpos,
7675 with several other stop positions in between that we
7676 missed. Scan them all now, in buffer's logical
7677 order, until we find and handle the last stop_charpos
7678 that precedes our current position. */
7679 handle_stop_backwards (it, it->stop_charpos);
7680 return GET_NEXT_DISPLAY_ELEMENT (it);
7681 }
7682 else
7683 {
7684 if (it->bidi_p)
7685 {
7686 /* Take note of the stop position we just moved
7687 across, for when we will move back across it. */
7688 it->prev_stop = it->stop_charpos;
7689 /* If we are at base paragraph embedding level, take
7690 note of the last stop position seen at this
7691 level. */
7692 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7693 it->base_level_stop = it->stop_charpos;
7694 }
7695 handle_stop (it);
7696
7697 /* Since a handler may have changed IT->method, we must
7698 recurse here. */
7699 return GET_NEXT_DISPLAY_ELEMENT (it);
7700 }
7701 }
7702 else if (it->bidi_p
7703 /* If we are before prev_stop, we may have overstepped
7704 on our way backwards a stop_pos, and if so, we need
7705 to handle that stop_pos. */
7706 && IT_STRING_CHARPOS (*it) < it->prev_stop
7707 /* We can sometimes back up for reasons that have nothing
7708 to do with bidi reordering. E.g., compositions. The
7709 code below is only needed when we are above the base
7710 embedding level, so test for that explicitly. */
7711 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7712 {
7713 /* If we lost track of base_level_stop, we have no better
7714 place for handle_stop_backwards to start from than string
7715 beginning. This happens, e.g., when we were reseated to
7716 the previous screenful of text by vertical-motion. */
7717 if (it->base_level_stop <= 0
7718 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7719 it->base_level_stop = 0;
7720 handle_stop_backwards (it, it->base_level_stop);
7721 return GET_NEXT_DISPLAY_ELEMENT (it);
7722 }
7723 }
7724
7725 if (it->current.overlay_string_index >= 0)
7726 {
7727 /* Get the next character from an overlay string. In overlay
7728 strings, there is no field width or padding with spaces to
7729 do. */
7730 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7731 {
7732 it->what = IT_EOB;
7733 return 0;
7734 }
7735 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7736 IT_STRING_BYTEPOS (*it),
7737 it->bidi_it.scan_dir < 0
7738 ? -1
7739 : SCHARS (it->string))
7740 && next_element_from_composition (it))
7741 {
7742 return 1;
7743 }
7744 else if (STRING_MULTIBYTE (it->string))
7745 {
7746 const unsigned char *s = (SDATA (it->string)
7747 + IT_STRING_BYTEPOS (*it));
7748 it->c = string_char_and_length (s, &it->len);
7749 }
7750 else
7751 {
7752 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7753 it->len = 1;
7754 }
7755 }
7756 else
7757 {
7758 /* Get the next character from a Lisp string that is not an
7759 overlay string. Such strings come from the mode line, for
7760 example. We may have to pad with spaces, or truncate the
7761 string. See also next_element_from_c_string. */
7762 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7763 {
7764 it->what = IT_EOB;
7765 return 0;
7766 }
7767 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7768 {
7769 /* Pad with spaces. */
7770 it->c = ' ', it->len = 1;
7771 CHARPOS (position) = BYTEPOS (position) = -1;
7772 }
7773 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7774 IT_STRING_BYTEPOS (*it),
7775 it->bidi_it.scan_dir < 0
7776 ? -1
7777 : it->string_nchars)
7778 && next_element_from_composition (it))
7779 {
7780 return 1;
7781 }
7782 else if (STRING_MULTIBYTE (it->string))
7783 {
7784 const unsigned char *s = (SDATA (it->string)
7785 + IT_STRING_BYTEPOS (*it));
7786 it->c = string_char_and_length (s, &it->len);
7787 }
7788 else
7789 {
7790 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7791 it->len = 1;
7792 }
7793 }
7794
7795 /* Record what we have and where it came from. */
7796 it->what = IT_CHARACTER;
7797 it->object = it->string;
7798 it->position = position;
7799 return 1;
7800 }
7801
7802
7803 /* Load IT with next display element from C string IT->s.
7804 IT->string_nchars is the maximum number of characters to return
7805 from the string. IT->end_charpos may be greater than
7806 IT->string_nchars when this function is called, in which case we
7807 may have to return padding spaces. Value is zero if end of string
7808 reached, including padding spaces. */
7809
7810 static int
7811 next_element_from_c_string (struct it *it)
7812 {
7813 int success_p = 1;
7814
7815 eassert (it->s);
7816 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7817 it->what = IT_CHARACTER;
7818 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7819 it->object = Qnil;
7820
7821 /* With bidi reordering, the character to display might not be the
7822 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7823 we were reseated to a new string, whose paragraph direction is
7824 not known. */
7825 if (it->bidi_p && it->bidi_it.first_elt)
7826 get_visually_first_element (it);
7827
7828 /* IT's position can be greater than IT->string_nchars in case a
7829 field width or precision has been specified when the iterator was
7830 initialized. */
7831 if (IT_CHARPOS (*it) >= it->end_charpos)
7832 {
7833 /* End of the game. */
7834 it->what = IT_EOB;
7835 success_p = 0;
7836 }
7837 else if (IT_CHARPOS (*it) >= it->string_nchars)
7838 {
7839 /* Pad with spaces. */
7840 it->c = ' ', it->len = 1;
7841 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7842 }
7843 else if (it->multibyte_p)
7844 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7845 else
7846 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7847
7848 return success_p;
7849 }
7850
7851
7852 /* Set up IT to return characters from an ellipsis, if appropriate.
7853 The definition of the ellipsis glyphs may come from a display table
7854 entry. This function fills IT with the first glyph from the
7855 ellipsis if an ellipsis is to be displayed. */
7856
7857 static int
7858 next_element_from_ellipsis (struct it *it)
7859 {
7860 if (it->selective_display_ellipsis_p)
7861 setup_for_ellipsis (it, it->len);
7862 else
7863 {
7864 /* The face at the current position may be different from the
7865 face we find after the invisible text. Remember what it
7866 was in IT->saved_face_id, and signal that it's there by
7867 setting face_before_selective_p. */
7868 it->saved_face_id = it->face_id;
7869 it->method = GET_FROM_BUFFER;
7870 it->object = it->w->contents;
7871 reseat_at_next_visible_line_start (it, 1);
7872 it->face_before_selective_p = 1;
7873 }
7874
7875 return GET_NEXT_DISPLAY_ELEMENT (it);
7876 }
7877
7878
7879 /* Deliver an image display element. The iterator IT is already
7880 filled with image information (done in handle_display_prop). Value
7881 is always 1. */
7882
7883
7884 static int
7885 next_element_from_image (struct it *it)
7886 {
7887 it->what = IT_IMAGE;
7888 it->ignore_overlay_strings_at_pos_p = 0;
7889 return 1;
7890 }
7891
7892
7893 /* Fill iterator IT with next display element from a stretch glyph
7894 property. IT->object is the value of the text property. Value is
7895 always 1. */
7896
7897 static int
7898 next_element_from_stretch (struct it *it)
7899 {
7900 it->what = IT_STRETCH;
7901 return 1;
7902 }
7903
7904 /* Scan backwards from IT's current position until we find a stop
7905 position, or until BEGV. This is called when we find ourself
7906 before both the last known prev_stop and base_level_stop while
7907 reordering bidirectional text. */
7908
7909 static void
7910 compute_stop_pos_backwards (struct it *it)
7911 {
7912 const int SCAN_BACK_LIMIT = 1000;
7913 struct text_pos pos;
7914 struct display_pos save_current = it->current;
7915 struct text_pos save_position = it->position;
7916 ptrdiff_t charpos = IT_CHARPOS (*it);
7917 ptrdiff_t where_we_are = charpos;
7918 ptrdiff_t save_stop_pos = it->stop_charpos;
7919 ptrdiff_t save_end_pos = it->end_charpos;
7920
7921 eassert (NILP (it->string) && !it->s);
7922 eassert (it->bidi_p);
7923 it->bidi_p = 0;
7924 do
7925 {
7926 it->end_charpos = min (charpos + 1, ZV);
7927 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7928 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7929 reseat_1 (it, pos, 0);
7930 compute_stop_pos (it);
7931 /* We must advance forward, right? */
7932 if (it->stop_charpos <= charpos)
7933 emacs_abort ();
7934 }
7935 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7936
7937 if (it->stop_charpos <= where_we_are)
7938 it->prev_stop = it->stop_charpos;
7939 else
7940 it->prev_stop = BEGV;
7941 it->bidi_p = 1;
7942 it->current = save_current;
7943 it->position = save_position;
7944 it->stop_charpos = save_stop_pos;
7945 it->end_charpos = save_end_pos;
7946 }
7947
7948 /* Scan forward from CHARPOS in the current buffer/string, until we
7949 find a stop position > current IT's position. Then handle the stop
7950 position before that. This is called when we bump into a stop
7951 position while reordering bidirectional text. CHARPOS should be
7952 the last previously processed stop_pos (or BEGV/0, if none were
7953 processed yet) whose position is less that IT's current
7954 position. */
7955
7956 static void
7957 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7958 {
7959 int bufp = !STRINGP (it->string);
7960 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7961 struct display_pos save_current = it->current;
7962 struct text_pos save_position = it->position;
7963 struct text_pos pos1;
7964 ptrdiff_t next_stop;
7965
7966 /* Scan in strict logical order. */
7967 eassert (it->bidi_p);
7968 it->bidi_p = 0;
7969 do
7970 {
7971 it->prev_stop = charpos;
7972 if (bufp)
7973 {
7974 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7975 reseat_1 (it, pos1, 0);
7976 }
7977 else
7978 it->current.string_pos = string_pos (charpos, it->string);
7979 compute_stop_pos (it);
7980 /* We must advance forward, right? */
7981 if (it->stop_charpos <= it->prev_stop)
7982 emacs_abort ();
7983 charpos = it->stop_charpos;
7984 }
7985 while (charpos <= where_we_are);
7986
7987 it->bidi_p = 1;
7988 it->current = save_current;
7989 it->position = save_position;
7990 next_stop = it->stop_charpos;
7991 it->stop_charpos = it->prev_stop;
7992 handle_stop (it);
7993 it->stop_charpos = next_stop;
7994 }
7995
7996 /* Load IT with the next display element from current_buffer. Value
7997 is zero if end of buffer reached. IT->stop_charpos is the next
7998 position at which to stop and check for text properties or buffer
7999 end. */
8000
8001 static int
8002 next_element_from_buffer (struct it *it)
8003 {
8004 int success_p = 1;
8005
8006 eassert (IT_CHARPOS (*it) >= BEGV);
8007 eassert (NILP (it->string) && !it->s);
8008 eassert (!it->bidi_p
8009 || (EQ (it->bidi_it.string.lstring, Qnil)
8010 && it->bidi_it.string.s == NULL));
8011
8012 /* With bidi reordering, the character to display might not be the
8013 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8014 we were reseat()ed to a new buffer position, which is potentially
8015 a different paragraph. */
8016 if (it->bidi_p && it->bidi_it.first_elt)
8017 {
8018 get_visually_first_element (it);
8019 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8020 }
8021
8022 if (IT_CHARPOS (*it) >= it->stop_charpos)
8023 {
8024 if (IT_CHARPOS (*it) >= it->end_charpos)
8025 {
8026 int overlay_strings_follow_p;
8027
8028 /* End of the game, except when overlay strings follow that
8029 haven't been returned yet. */
8030 if (it->overlay_strings_at_end_processed_p)
8031 overlay_strings_follow_p = 0;
8032 else
8033 {
8034 it->overlay_strings_at_end_processed_p = 1;
8035 overlay_strings_follow_p = get_overlay_strings (it, 0);
8036 }
8037
8038 if (overlay_strings_follow_p)
8039 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8040 else
8041 {
8042 it->what = IT_EOB;
8043 it->position = it->current.pos;
8044 success_p = 0;
8045 }
8046 }
8047 else if (!(!it->bidi_p
8048 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8049 || IT_CHARPOS (*it) == it->stop_charpos))
8050 {
8051 /* With bidi non-linear iteration, we could find ourselves
8052 far beyond the last computed stop_charpos, with several
8053 other stop positions in between that we missed. Scan
8054 them all now, in buffer's logical order, until we find
8055 and handle the last stop_charpos that precedes our
8056 current position. */
8057 handle_stop_backwards (it, it->stop_charpos);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8059 }
8060 else
8061 {
8062 if (it->bidi_p)
8063 {
8064 /* Take note of the stop position we just moved across,
8065 for when we will move back across it. */
8066 it->prev_stop = it->stop_charpos;
8067 /* If we are at base paragraph embedding level, take
8068 note of the last stop position seen at this
8069 level. */
8070 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8071 it->base_level_stop = it->stop_charpos;
8072 }
8073 handle_stop (it);
8074 return GET_NEXT_DISPLAY_ELEMENT (it);
8075 }
8076 }
8077 else if (it->bidi_p
8078 /* If we are before prev_stop, we may have overstepped on
8079 our way backwards a stop_pos, and if so, we need to
8080 handle that stop_pos. */
8081 && IT_CHARPOS (*it) < it->prev_stop
8082 /* We can sometimes back up for reasons that have nothing
8083 to do with bidi reordering. E.g., compositions. The
8084 code below is only needed when we are above the base
8085 embedding level, so test for that explicitly. */
8086 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8087 {
8088 if (it->base_level_stop <= 0
8089 || IT_CHARPOS (*it) < it->base_level_stop)
8090 {
8091 /* If we lost track of base_level_stop, we need to find
8092 prev_stop by looking backwards. This happens, e.g., when
8093 we were reseated to the previous screenful of text by
8094 vertical-motion. */
8095 it->base_level_stop = BEGV;
8096 compute_stop_pos_backwards (it);
8097 handle_stop_backwards (it, it->prev_stop);
8098 }
8099 else
8100 handle_stop_backwards (it, it->base_level_stop);
8101 return GET_NEXT_DISPLAY_ELEMENT (it);
8102 }
8103 else
8104 {
8105 /* No face changes, overlays etc. in sight, so just return a
8106 character from current_buffer. */
8107 unsigned char *p;
8108 ptrdiff_t stop;
8109
8110 /* Maybe run the redisplay end trigger hook. Performance note:
8111 This doesn't seem to cost measurable time. */
8112 if (it->redisplay_end_trigger_charpos
8113 && it->glyph_row
8114 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8115 run_redisplay_end_trigger_hook (it);
8116
8117 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8118 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8119 stop)
8120 && next_element_from_composition (it))
8121 {
8122 return 1;
8123 }
8124
8125 /* Get the next character, maybe multibyte. */
8126 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8127 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8128 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8129 else
8130 it->c = *p, it->len = 1;
8131
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->w->contents;
8135 it->position = it->current.pos;
8136
8137 /* Normally we return the character found above, except when we
8138 really want to return an ellipsis for selective display. */
8139 if (it->selective)
8140 {
8141 if (it->c == '\n')
8142 {
8143 /* A value of selective > 0 means hide lines indented more
8144 than that number of columns. */
8145 if (it->selective > 0
8146 && IT_CHARPOS (*it) + 1 < ZV
8147 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8148 IT_BYTEPOS (*it) + 1,
8149 it->selective))
8150 {
8151 success_p = next_element_from_ellipsis (it);
8152 it->dpvec_char_len = -1;
8153 }
8154 }
8155 else if (it->c == '\r' && it->selective == -1)
8156 {
8157 /* A value of selective == -1 means that everything from the
8158 CR to the end of the line is invisible, with maybe an
8159 ellipsis displayed for it. */
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8162 }
8163 }
8164 }
8165
8166 /* Value is zero if end of buffer reached. */
8167 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8168 return success_p;
8169 }
8170
8171
8172 /* Run the redisplay end trigger hook for IT. */
8173
8174 static void
8175 run_redisplay_end_trigger_hook (struct it *it)
8176 {
8177 Lisp_Object args[3];
8178
8179 /* IT->glyph_row should be non-null, i.e. we should be actually
8180 displaying something, or otherwise we should not run the hook. */
8181 eassert (it->glyph_row);
8182
8183 /* Set up hook arguments. */
8184 args[0] = Qredisplay_end_trigger_functions;
8185 args[1] = it->window;
8186 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8187 it->redisplay_end_trigger_charpos = 0;
8188
8189 /* Since we are *trying* to run these functions, don't try to run
8190 them again, even if they get an error. */
8191 wset_redisplay_end_trigger (it->w, Qnil);
8192 Frun_hook_with_args (3, args);
8193
8194 /* Notice if it changed the face of the character we are on. */
8195 handle_face_prop (it);
8196 }
8197
8198
8199 /* Deliver a composition display element. Unlike the other
8200 next_element_from_XXX, this function is not registered in the array
8201 get_next_element[]. It is called from next_element_from_buffer and
8202 next_element_from_string when necessary. */
8203
8204 static int
8205 next_element_from_composition (struct it *it)
8206 {
8207 it->what = IT_COMPOSITION;
8208 it->len = it->cmp_it.nbytes;
8209 if (STRINGP (it->string))
8210 {
8211 if (it->c < 0)
8212 {
8213 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8214 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8215 return 0;
8216 }
8217 it->position = it->current.string_pos;
8218 it->object = it->string;
8219 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8220 IT_STRING_BYTEPOS (*it), it->string);
8221 }
8222 else
8223 {
8224 if (it->c < 0)
8225 {
8226 IT_CHARPOS (*it) += it->cmp_it.nchars;
8227 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8228 if (it->bidi_p)
8229 {
8230 if (it->bidi_it.new_paragraph)
8231 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8232 /* Resync the bidi iterator with IT's new position.
8233 FIXME: this doesn't support bidirectional text. */
8234 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8235 bidi_move_to_visually_next (&it->bidi_it);
8236 }
8237 return 0;
8238 }
8239 it->position = it->current.pos;
8240 it->object = it->w->contents;
8241 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8242 IT_BYTEPOS (*it), Qnil);
8243 }
8244 return 1;
8245 }
8246
8247
8248 \f
8249 /***********************************************************************
8250 Moving an iterator without producing glyphs
8251 ***********************************************************************/
8252
8253 /* Check if iterator is at a position corresponding to a valid buffer
8254 position after some move_it_ call. */
8255
8256 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8257 ((it)->method == GET_FROM_STRING \
8258 ? IT_STRING_CHARPOS (*it) == 0 \
8259 : 1)
8260
8261
8262 /* Move iterator IT to a specified buffer or X position within one
8263 line on the display without producing glyphs.
8264
8265 OP should be a bit mask including some or all of these bits:
8266 MOVE_TO_X: Stop upon reaching x-position TO_X.
8267 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8268 Regardless of OP's value, stop upon reaching the end of the display line.
8269
8270 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8271 This means, in particular, that TO_X includes window's horizontal
8272 scroll amount.
8273
8274 The return value has several possible values that
8275 say what condition caused the scan to stop:
8276
8277 MOVE_POS_MATCH_OR_ZV
8278 - when TO_POS or ZV was reached.
8279
8280 MOVE_X_REACHED
8281 -when TO_X was reached before TO_POS or ZV were reached.
8282
8283 MOVE_LINE_CONTINUED
8284 - when we reached the end of the display area and the line must
8285 be continued.
8286
8287 MOVE_LINE_TRUNCATED
8288 - when we reached the end of the display area and the line is
8289 truncated.
8290
8291 MOVE_NEWLINE_OR_CR
8292 - when we stopped at a line end, i.e. a newline or a CR and selective
8293 display is on. */
8294
8295 static enum move_it_result
8296 move_it_in_display_line_to (struct it *it,
8297 ptrdiff_t to_charpos, int to_x,
8298 enum move_operation_enum op)
8299 {
8300 enum move_it_result result = MOVE_UNDEFINED;
8301 struct glyph_row *saved_glyph_row;
8302 struct it wrap_it, atpos_it, atx_it, ppos_it;
8303 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8304 void *ppos_data = NULL;
8305 int may_wrap = 0;
8306 enum it_method prev_method = it->method;
8307 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8308 int saw_smaller_pos = prev_pos < to_charpos;
8309
8310 /* Don't produce glyphs in produce_glyphs. */
8311 saved_glyph_row = it->glyph_row;
8312 it->glyph_row = NULL;
8313
8314 /* Use wrap_it to save a copy of IT wherever a word wrap could
8315 occur. Use atpos_it to save a copy of IT at the desired buffer
8316 position, if found, so that we can scan ahead and check if the
8317 word later overshoots the window edge. Use atx_it similarly, for
8318 pixel positions. */
8319 wrap_it.sp = -1;
8320 atpos_it.sp = -1;
8321 atx_it.sp = -1;
8322
8323 /* Use ppos_it under bidi reordering to save a copy of IT for the
8324 position > CHARPOS that is the closest to CHARPOS. We restore
8325 that position in IT when we have scanned the entire display line
8326 without finding a match for CHARPOS and all the character
8327 positions are greater than CHARPOS. */
8328 if (it->bidi_p)
8329 {
8330 SAVE_IT (ppos_it, *it, ppos_data);
8331 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8332 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8333 SAVE_IT (ppos_it, *it, ppos_data);
8334 }
8335
8336 #define BUFFER_POS_REACHED_P() \
8337 ((op & MOVE_TO_POS) != 0 \
8338 && BUFFERP (it->object) \
8339 && (IT_CHARPOS (*it) == to_charpos \
8340 || ((!it->bidi_p \
8341 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8342 && IT_CHARPOS (*it) > to_charpos) \
8343 || (it->what == IT_COMPOSITION \
8344 && ((IT_CHARPOS (*it) > to_charpos \
8345 && to_charpos >= it->cmp_it.charpos) \
8346 || (IT_CHARPOS (*it) < to_charpos \
8347 && to_charpos <= it->cmp_it.charpos)))) \
8348 && (it->method == GET_FROM_BUFFER \
8349 || (it->method == GET_FROM_DISPLAY_VECTOR \
8350 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8351
8352 /* If there's a line-/wrap-prefix, handle it. */
8353 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8354 && it->current_y < it->last_visible_y)
8355 handle_line_prefix (it);
8356
8357 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8358 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8359
8360 while (1)
8361 {
8362 int x, i, ascent = 0, descent = 0;
8363
8364 /* Utility macro to reset an iterator with x, ascent, and descent. */
8365 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8366 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8367 (IT)->max_descent = descent)
8368
8369 /* Stop if we move beyond TO_CHARPOS (after an image or a
8370 display string or stretch glyph). */
8371 if ((op & MOVE_TO_POS) != 0
8372 && BUFFERP (it->object)
8373 && it->method == GET_FROM_BUFFER
8374 && (((!it->bidi_p
8375 /* When the iterator is at base embedding level, we
8376 are guaranteed that characters are delivered for
8377 display in strictly increasing order of their
8378 buffer positions. */
8379 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8380 && IT_CHARPOS (*it) > to_charpos)
8381 || (it->bidi_p
8382 && (prev_method == GET_FROM_IMAGE
8383 || prev_method == GET_FROM_STRETCH
8384 || prev_method == GET_FROM_STRING)
8385 /* Passed TO_CHARPOS from left to right. */
8386 && ((prev_pos < to_charpos
8387 && IT_CHARPOS (*it) > to_charpos)
8388 /* Passed TO_CHARPOS from right to left. */
8389 || (prev_pos > to_charpos
8390 && IT_CHARPOS (*it) < to_charpos)))))
8391 {
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8393 {
8394 result = MOVE_POS_MATCH_OR_ZV;
8395 break;
8396 }
8397 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8398 /* If wrap_it is valid, the current position might be in a
8399 word that is wrapped. So, save the iterator in
8400 atpos_it and continue to see if wrapping happens. */
8401 SAVE_IT (atpos_it, *it, atpos_data);
8402 }
8403
8404 /* Stop when ZV reached.
8405 We used to stop here when TO_CHARPOS reached as well, but that is
8406 too soon if this glyph does not fit on this line. So we handle it
8407 explicitly below. */
8408 if (!get_next_display_element (it))
8409 {
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8412 }
8413
8414 if (it->line_wrap == TRUNCATE)
8415 {
8416 if (BUFFER_POS_REACHED_P ())
8417 {
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8420 }
8421 }
8422 else
8423 {
8424 if (it->line_wrap == WORD_WRAP)
8425 {
8426 if (IT_DISPLAYING_WHITESPACE (it))
8427 may_wrap = 1;
8428 else if (may_wrap)
8429 {
8430 /* We have reached a glyph that follows one or more
8431 whitespace characters. If the position is
8432 already found, we are done. */
8433 if (atpos_it.sp >= 0)
8434 {
8435 RESTORE_IT (it, &atpos_it, atpos_data);
8436 result = MOVE_POS_MATCH_OR_ZV;
8437 goto done;
8438 }
8439 if (atx_it.sp >= 0)
8440 {
8441 RESTORE_IT (it, &atx_it, atx_data);
8442 result = MOVE_X_REACHED;
8443 goto done;
8444 }
8445 /* Otherwise, we can wrap here. */
8446 SAVE_IT (wrap_it, *it, wrap_data);
8447 may_wrap = 0;
8448 }
8449 }
8450 }
8451
8452 /* Remember the line height for the current line, in case
8453 the next element doesn't fit on the line. */
8454 ascent = it->max_ascent;
8455 descent = it->max_descent;
8456
8457 /* The call to produce_glyphs will get the metrics of the
8458 display element IT is loaded with. Record the x-position
8459 before this display element, in case it doesn't fit on the
8460 line. */
8461 x = it->current_x;
8462
8463 PRODUCE_GLYPHS (it);
8464
8465 if (it->area != TEXT_AREA)
8466 {
8467 prev_method = it->method;
8468 if (it->method == GET_FROM_BUFFER)
8469 prev_pos = IT_CHARPOS (*it);
8470 set_iterator_to_next (it, 1);
8471 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8472 SET_TEXT_POS (this_line_min_pos,
8473 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8474 if (it->bidi_p
8475 && (op & MOVE_TO_POS)
8476 && IT_CHARPOS (*it) > to_charpos
8477 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8478 SAVE_IT (ppos_it, *it, ppos_data);
8479 continue;
8480 }
8481
8482 /* The number of glyphs we get back in IT->nglyphs will normally
8483 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8484 character on a terminal frame, or (iii) a line end. For the
8485 second case, IT->nglyphs - 1 padding glyphs will be present.
8486 (On X frames, there is only one glyph produced for a
8487 composite character.)
8488
8489 The behavior implemented below means, for continuation lines,
8490 that as many spaces of a TAB as fit on the current line are
8491 displayed there. For terminal frames, as many glyphs of a
8492 multi-glyph character are displayed in the current line, too.
8493 This is what the old redisplay code did, and we keep it that
8494 way. Under X, the whole shape of a complex character must
8495 fit on the line or it will be completely displayed in the
8496 next line.
8497
8498 Note that both for tabs and padding glyphs, all glyphs have
8499 the same width. */
8500 if (it->nglyphs)
8501 {
8502 /* More than one glyph or glyph doesn't fit on line. All
8503 glyphs have the same width. */
8504 int single_glyph_width = it->pixel_width / it->nglyphs;
8505 int new_x;
8506 int x_before_this_char = x;
8507 int hpos_before_this_char = it->hpos;
8508
8509 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8510 {
8511 new_x = x + single_glyph_width;
8512
8513 /* We want to leave anything reaching TO_X to the caller. */
8514 if ((op & MOVE_TO_X) && new_x > to_x)
8515 {
8516 if (BUFFER_POS_REACHED_P ())
8517 {
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8519 goto buffer_pos_reached;
8520 if (atpos_it.sp < 0)
8521 {
8522 SAVE_IT (atpos_it, *it, atpos_data);
8523 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8524 }
8525 }
8526 else
8527 {
8528 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8529 {
8530 it->current_x = x;
8531 result = MOVE_X_REACHED;
8532 break;
8533 }
8534 if (atx_it.sp < 0)
8535 {
8536 SAVE_IT (atx_it, *it, atx_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8538 }
8539 }
8540 }
8541
8542 if (/* Lines are continued. */
8543 it->line_wrap != TRUNCATE
8544 && (/* And glyph doesn't fit on the line. */
8545 new_x > it->last_visible_x
8546 /* Or it fits exactly and we're on a window
8547 system frame. */
8548 || (new_x == it->last_visible_x
8549 && FRAME_WINDOW_P (it->f)
8550 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8551 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8552 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8553 {
8554 if (/* IT->hpos == 0 means the very first glyph
8555 doesn't fit on the line, e.g. a wide image. */
8556 it->hpos == 0
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)))
8559 {
8560 ++it->hpos;
8561 it->current_x = new_x;
8562
8563 /* The character's last glyph just barely fits
8564 in this row. */
8565 if (i == it->nglyphs - 1)
8566 {
8567 /* If this is the destination position,
8568 return a position *before* it in this row,
8569 now that we know it fits in this row. */
8570 if (BUFFER_POS_REACHED_P ())
8571 {
8572 if (it->line_wrap != WORD_WRAP
8573 || wrap_it.sp < 0)
8574 {
8575 it->hpos = hpos_before_this_char;
8576 it->current_x = x_before_this_char;
8577 result = MOVE_POS_MATCH_OR_ZV;
8578 break;
8579 }
8580 if (it->line_wrap == WORD_WRAP
8581 && atpos_it.sp < 0)
8582 {
8583 SAVE_IT (atpos_it, *it, atpos_data);
8584 atpos_it.current_x = x_before_this_char;
8585 atpos_it.hpos = hpos_before_this_char;
8586 }
8587 }
8588
8589 prev_method = it->method;
8590 if (it->method == GET_FROM_BUFFER)
8591 prev_pos = IT_CHARPOS (*it);
8592 set_iterator_to_next (it, 1);
8593 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8594 SET_TEXT_POS (this_line_min_pos,
8595 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8596 /* On graphical terminals, newlines may
8597 "overflow" into the fringe if
8598 overflow-newline-into-fringe is non-nil.
8599 On text terminals, and on graphical
8600 terminals with no right margin, newlines
8601 may overflow into the last glyph on the
8602 display line.*/
8603 if (!FRAME_WINDOW_P (it->f)
8604 || ((it->bidi_p
8605 && it->bidi_it.paragraph_dir == R2L)
8606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8608 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8609 {
8610 if (!get_next_display_element (it))
8611 {
8612 result = MOVE_POS_MATCH_OR_ZV;
8613 break;
8614 }
8615 if (BUFFER_POS_REACHED_P ())
8616 {
8617 if (ITERATOR_AT_END_OF_LINE_P (it))
8618 result = MOVE_POS_MATCH_OR_ZV;
8619 else
8620 result = MOVE_LINE_CONTINUED;
8621 break;
8622 }
8623 if (ITERATOR_AT_END_OF_LINE_P (it)
8624 && (it->line_wrap != WORD_WRAP
8625 || wrap_it.sp < 0))
8626 {
8627 result = MOVE_NEWLINE_OR_CR;
8628 break;
8629 }
8630 }
8631 }
8632 }
8633 else
8634 IT_RESET_X_ASCENT_DESCENT (it);
8635
8636 if (wrap_it.sp >= 0)
8637 {
8638 RESTORE_IT (it, &wrap_it, wrap_data);
8639 atpos_it.sp = -1;
8640 atx_it.sp = -1;
8641 }
8642
8643 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8644 IT_CHARPOS (*it)));
8645 result = MOVE_LINE_CONTINUED;
8646 break;
8647 }
8648
8649 if (BUFFER_POS_REACHED_P ())
8650 {
8651 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8652 goto buffer_pos_reached;
8653 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8654 {
8655 SAVE_IT (atpos_it, *it, atpos_data);
8656 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8657 }
8658 }
8659
8660 if (new_x > it->first_visible_x)
8661 {
8662 /* Glyph is visible. Increment number of glyphs that
8663 would be displayed. */
8664 ++it->hpos;
8665 }
8666 }
8667
8668 if (result != MOVE_UNDEFINED)
8669 break;
8670 }
8671 else if (BUFFER_POS_REACHED_P ())
8672 {
8673 buffer_pos_reached:
8674 IT_RESET_X_ASCENT_DESCENT (it);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8677 }
8678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8679 {
8680 /* Stop when TO_X specified and reached. This check is
8681 necessary here because of lines consisting of a line end,
8682 only. The line end will not produce any glyphs and we
8683 would never get MOVE_X_REACHED. */
8684 eassert (it->nglyphs == 0);
8685 result = MOVE_X_REACHED;
8686 break;
8687 }
8688
8689 /* Is this a line end? If yes, we're done. */
8690 if (ITERATOR_AT_END_OF_LINE_P (it))
8691 {
8692 /* If we are past TO_CHARPOS, but never saw any character
8693 positions smaller than TO_CHARPOS, return
8694 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8695 did. */
8696 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8697 {
8698 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8699 {
8700 if (IT_CHARPOS (ppos_it) < ZV)
8701 {
8702 RESTORE_IT (it, &ppos_it, ppos_data);
8703 result = MOVE_POS_MATCH_OR_ZV;
8704 }
8705 else
8706 goto buffer_pos_reached;
8707 }
8708 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8709 && IT_CHARPOS (*it) > to_charpos)
8710 goto buffer_pos_reached;
8711 else
8712 result = MOVE_NEWLINE_OR_CR;
8713 }
8714 else
8715 result = MOVE_NEWLINE_OR_CR;
8716 break;
8717 }
8718
8719 prev_method = it->method;
8720 if (it->method == GET_FROM_BUFFER)
8721 prev_pos = IT_CHARPOS (*it);
8722 /* The current display element has been consumed. Advance
8723 to the next. */
8724 set_iterator_to_next (it, 1);
8725 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8726 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8727 if (IT_CHARPOS (*it) < to_charpos)
8728 saw_smaller_pos = 1;
8729 if (it->bidi_p
8730 && (op & MOVE_TO_POS)
8731 && IT_CHARPOS (*it) >= to_charpos
8732 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8733 SAVE_IT (ppos_it, *it, ppos_data);
8734
8735 /* Stop if lines are truncated and IT's current x-position is
8736 past the right edge of the window now. */
8737 if (it->line_wrap == TRUNCATE
8738 && it->current_x >= it->last_visible_x)
8739 {
8740 if (!FRAME_WINDOW_P (it->f)
8741 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8742 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8743 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8744 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8745 {
8746 int at_eob_p = 0;
8747
8748 if ((at_eob_p = !get_next_display_element (it))
8749 || BUFFER_POS_REACHED_P ()
8750 /* If we are past TO_CHARPOS, but never saw any
8751 character positions smaller than TO_CHARPOS,
8752 return MOVE_POS_MATCH_OR_ZV, like the
8753 unidirectional display did. */
8754 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8755 && !saw_smaller_pos
8756 && IT_CHARPOS (*it) > to_charpos))
8757 {
8758 if (it->bidi_p
8759 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8760 RESTORE_IT (it, &ppos_it, ppos_data);
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8763 }
8764 if (ITERATOR_AT_END_OF_LINE_P (it))
8765 {
8766 result = MOVE_NEWLINE_OR_CR;
8767 break;
8768 }
8769 }
8770 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8771 && !saw_smaller_pos
8772 && IT_CHARPOS (*it) > to_charpos)
8773 {
8774 if (IT_CHARPOS (ppos_it) < ZV)
8775 RESTORE_IT (it, &ppos_it, ppos_data);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8778 }
8779 result = MOVE_LINE_TRUNCATED;
8780 break;
8781 }
8782 #undef IT_RESET_X_ASCENT_DESCENT
8783 }
8784
8785 #undef BUFFER_POS_REACHED_P
8786
8787 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8788 restore the saved iterator. */
8789 if (atpos_it.sp >= 0)
8790 RESTORE_IT (it, &atpos_it, atpos_data);
8791 else if (atx_it.sp >= 0)
8792 RESTORE_IT (it, &atx_it, atx_data);
8793
8794 done:
8795
8796 if (atpos_data)
8797 bidi_unshelve_cache (atpos_data, 1);
8798 if (atx_data)
8799 bidi_unshelve_cache (atx_data, 1);
8800 if (wrap_data)
8801 bidi_unshelve_cache (wrap_data, 1);
8802 if (ppos_data)
8803 bidi_unshelve_cache (ppos_data, 1);
8804
8805 /* Restore the iterator settings altered at the beginning of this
8806 function. */
8807 it->glyph_row = saved_glyph_row;
8808 return result;
8809 }
8810
8811 /* For external use. */
8812 void
8813 move_it_in_display_line (struct it *it,
8814 ptrdiff_t to_charpos, int to_x,
8815 enum move_operation_enum op)
8816 {
8817 if (it->line_wrap == WORD_WRAP
8818 && (op & MOVE_TO_X))
8819 {
8820 struct it save_it;
8821 void *save_data = NULL;
8822 int skip;
8823
8824 SAVE_IT (save_it, *it, save_data);
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8826 /* When word-wrap is on, TO_X may lie past the end
8827 of a wrapped line. Then it->current is the
8828 character on the next line, so backtrack to the
8829 space before the wrap point. */
8830 if (skip == MOVE_LINE_CONTINUED)
8831 {
8832 int prev_x = max (it->current_x - 1, 0);
8833 RESTORE_IT (it, &save_it, save_data);
8834 move_it_in_display_line_to
8835 (it, -1, prev_x, MOVE_TO_X);
8836 }
8837 else
8838 bidi_unshelve_cache (save_data, 1);
8839 }
8840 else
8841 move_it_in_display_line_to (it, to_charpos, to_x, op);
8842 }
8843
8844
8845 /* Move IT forward until it satisfies one or more of the criteria in
8846 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8847
8848 OP is a bit-mask that specifies where to stop, and in particular,
8849 which of those four position arguments makes a difference. See the
8850 description of enum move_operation_enum.
8851
8852 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8853 screen line, this function will set IT to the next position that is
8854 displayed to the right of TO_CHARPOS on the screen. */
8855
8856 void
8857 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8858 {
8859 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8860 int line_height, line_start_x = 0, reached = 0;
8861 void *backup_data = NULL;
8862
8863 for (;;)
8864 {
8865 if (op & MOVE_TO_VPOS)
8866 {
8867 /* If no TO_CHARPOS and no TO_X specified, stop at the
8868 start of the line TO_VPOS. */
8869 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8870 {
8871 if (it->vpos == to_vpos)
8872 {
8873 reached = 1;
8874 break;
8875 }
8876 else
8877 skip = move_it_in_display_line_to (it, -1, -1, 0);
8878 }
8879 else
8880 {
8881 /* TO_VPOS >= 0 means stop at TO_X in the line at
8882 TO_VPOS, or at TO_POS, whichever comes first. */
8883 if (it->vpos == to_vpos)
8884 {
8885 reached = 2;
8886 break;
8887 }
8888
8889 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8890
8891 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8892 {
8893 reached = 3;
8894 break;
8895 }
8896 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8897 {
8898 /* We have reached TO_X but not in the line we want. */
8899 skip = move_it_in_display_line_to (it, to_charpos,
8900 -1, MOVE_TO_POS);
8901 if (skip == MOVE_POS_MATCH_OR_ZV)
8902 {
8903 reached = 4;
8904 break;
8905 }
8906 }
8907 }
8908 }
8909 else if (op & MOVE_TO_Y)
8910 {
8911 struct it it_backup;
8912
8913 if (it->line_wrap == WORD_WRAP)
8914 SAVE_IT (it_backup, *it, backup_data);
8915
8916 /* TO_Y specified means stop at TO_X in the line containing
8917 TO_Y---or at TO_CHARPOS if this is reached first. The
8918 problem is that we can't really tell whether the line
8919 contains TO_Y before we have completely scanned it, and
8920 this may skip past TO_X. What we do is to first scan to
8921 TO_X.
8922
8923 If TO_X is not specified, use a TO_X of zero. The reason
8924 is to make the outcome of this function more predictable.
8925 If we didn't use TO_X == 0, we would stop at the end of
8926 the line which is probably not what a caller would expect
8927 to happen. */
8928 skip = move_it_in_display_line_to
8929 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8930 (MOVE_TO_X | (op & MOVE_TO_POS)));
8931
8932 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8933 if (skip == MOVE_POS_MATCH_OR_ZV)
8934 reached = 5;
8935 else if (skip == MOVE_X_REACHED)
8936 {
8937 /* If TO_X was reached, we want to know whether TO_Y is
8938 in the line. We know this is the case if the already
8939 scanned glyphs make the line tall enough. Otherwise,
8940 we must check by scanning the rest of the line. */
8941 line_height = it->max_ascent + it->max_descent;
8942 if (to_y >= it->current_y
8943 && to_y < it->current_y + line_height)
8944 {
8945 reached = 6;
8946 break;
8947 }
8948 SAVE_IT (it_backup, *it, backup_data);
8949 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8950 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8951 op & MOVE_TO_POS);
8952 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8953 line_height = it->max_ascent + it->max_descent;
8954 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8955
8956 if (to_y >= it->current_y
8957 && to_y < it->current_y + line_height)
8958 {
8959 /* If TO_Y is in this line and TO_X was reached
8960 above, we scanned too far. We have to restore
8961 IT's settings to the ones before skipping. But
8962 keep the more accurate values of max_ascent and
8963 max_descent we've found while skipping the rest
8964 of the line, for the sake of callers, such as
8965 pos_visible_p, that need to know the line
8966 height. */
8967 int max_ascent = it->max_ascent;
8968 int max_descent = it->max_descent;
8969
8970 RESTORE_IT (it, &it_backup, backup_data);
8971 it->max_ascent = max_ascent;
8972 it->max_descent = max_descent;
8973 reached = 6;
8974 }
8975 else
8976 {
8977 skip = skip2;
8978 if (skip == MOVE_POS_MATCH_OR_ZV)
8979 reached = 7;
8980 }
8981 }
8982 else
8983 {
8984 /* Check whether TO_Y is in this line. */
8985 line_height = it->max_ascent + it->max_descent;
8986 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8987
8988 if (to_y >= it->current_y
8989 && to_y < it->current_y + line_height)
8990 {
8991 /* When word-wrap is on, TO_X may lie past the end
8992 of a wrapped line. Then it->current is the
8993 character on the next line, so backtrack to the
8994 space before the wrap point. */
8995 if (skip == MOVE_LINE_CONTINUED
8996 && it->line_wrap == WORD_WRAP)
8997 {
8998 int prev_x = max (it->current_x - 1, 0);
8999 RESTORE_IT (it, &it_backup, backup_data);
9000 skip = move_it_in_display_line_to
9001 (it, -1, prev_x, MOVE_TO_X);
9002 }
9003 reached = 6;
9004 }
9005 }
9006
9007 if (reached)
9008 break;
9009 }
9010 else if (BUFFERP (it->object)
9011 && (it->method == GET_FROM_BUFFER
9012 || it->method == GET_FROM_STRETCH)
9013 && IT_CHARPOS (*it) >= to_charpos
9014 /* Under bidi iteration, a call to set_iterator_to_next
9015 can scan far beyond to_charpos if the initial
9016 portion of the next line needs to be reordered. In
9017 that case, give move_it_in_display_line_to another
9018 chance below. */
9019 && !(it->bidi_p
9020 && it->bidi_it.scan_dir == -1))
9021 skip = MOVE_POS_MATCH_OR_ZV;
9022 else
9023 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9024
9025 switch (skip)
9026 {
9027 case MOVE_POS_MATCH_OR_ZV:
9028 reached = 8;
9029 goto out;
9030
9031 case MOVE_NEWLINE_OR_CR:
9032 set_iterator_to_next (it, 1);
9033 it->continuation_lines_width = 0;
9034 break;
9035
9036 case MOVE_LINE_TRUNCATED:
9037 it->continuation_lines_width = 0;
9038 reseat_at_next_visible_line_start (it, 0);
9039 if ((op & MOVE_TO_POS) != 0
9040 && IT_CHARPOS (*it) > to_charpos)
9041 {
9042 reached = 9;
9043 goto out;
9044 }
9045 break;
9046
9047 case MOVE_LINE_CONTINUED:
9048 /* For continued lines ending in a tab, some of the glyphs
9049 associated with the tab are displayed on the current
9050 line. Since it->current_x does not include these glyphs,
9051 we use it->last_visible_x instead. */
9052 if (it->c == '\t')
9053 {
9054 it->continuation_lines_width += it->last_visible_x;
9055 /* When moving by vpos, ensure that the iterator really
9056 advances to the next line (bug#847, bug#969). Fixme:
9057 do we need to do this in other circumstances? */
9058 if (it->current_x != it->last_visible_x
9059 && (op & MOVE_TO_VPOS)
9060 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9061 {
9062 line_start_x = it->current_x + it->pixel_width
9063 - it->last_visible_x;
9064 set_iterator_to_next (it, 0);
9065 }
9066 }
9067 else
9068 it->continuation_lines_width += it->current_x;
9069 break;
9070
9071 default:
9072 emacs_abort ();
9073 }
9074
9075 /* Reset/increment for the next run. */
9076 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9077 it->current_x = line_start_x;
9078 line_start_x = 0;
9079 it->hpos = 0;
9080 it->current_y += it->max_ascent + it->max_descent;
9081 ++it->vpos;
9082 last_height = it->max_ascent + it->max_descent;
9083 it->max_ascent = it->max_descent = 0;
9084 }
9085
9086 out:
9087
9088 /* On text terminals, we may stop at the end of a line in the middle
9089 of a multi-character glyph. If the glyph itself is continued,
9090 i.e. it is actually displayed on the next line, don't treat this
9091 stopping point as valid; move to the next line instead (unless
9092 that brings us offscreen). */
9093 if (!FRAME_WINDOW_P (it->f)
9094 && op & MOVE_TO_POS
9095 && IT_CHARPOS (*it) == to_charpos
9096 && it->what == IT_CHARACTER
9097 && it->nglyphs > 1
9098 && it->line_wrap == WINDOW_WRAP
9099 && it->current_x == it->last_visible_x - 1
9100 && it->c != '\n'
9101 && it->c != '\t'
9102 && it->vpos < it->w->window_end_vpos)
9103 {
9104 it->continuation_lines_width += it->current_x;
9105 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9106 it->current_y += it->max_ascent + it->max_descent;
9107 ++it->vpos;
9108 last_height = it->max_ascent + it->max_descent;
9109 }
9110
9111 if (backup_data)
9112 bidi_unshelve_cache (backup_data, 1);
9113
9114 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9115 }
9116
9117
9118 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9119
9120 If DY > 0, move IT backward at least that many pixels. DY = 0
9121 means move IT backward to the preceding line start or BEGV. This
9122 function may move over more than DY pixels if IT->current_y - DY
9123 ends up in the middle of a line; in this case IT->current_y will be
9124 set to the top of the line moved to. */
9125
9126 void
9127 move_it_vertically_backward (struct it *it, int dy)
9128 {
9129 int nlines, h;
9130 struct it it2, it3;
9131 void *it2data = NULL, *it3data = NULL;
9132 ptrdiff_t start_pos;
9133 int nchars_per_row
9134 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9135 ptrdiff_t pos_limit;
9136
9137 move_further_back:
9138 eassert (dy >= 0);
9139
9140 start_pos = IT_CHARPOS (*it);
9141
9142 /* Estimate how many newlines we must move back. */
9143 nlines = max (1, dy / default_line_pixel_height (it->w));
9144 if (it->line_wrap == TRUNCATE)
9145 pos_limit = BEGV;
9146 else
9147 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9148
9149 /* Set the iterator's position that many lines back. But don't go
9150 back more than NLINES full screen lines -- this wins a day with
9151 buffers which have very long lines. */
9152 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9153 back_to_previous_visible_line_start (it);
9154
9155 /* Reseat the iterator here. When moving backward, we don't want
9156 reseat to skip forward over invisible text, set up the iterator
9157 to deliver from overlay strings at the new position etc. So,
9158 use reseat_1 here. */
9159 reseat_1 (it, it->current.pos, 1);
9160
9161 /* We are now surely at a line start. */
9162 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9163 reordering is in effect. */
9164 it->continuation_lines_width = 0;
9165
9166 /* Move forward and see what y-distance we moved. First move to the
9167 start of the next line so that we get its height. We need this
9168 height to be able to tell whether we reached the specified
9169 y-distance. */
9170 SAVE_IT (it2, *it, it2data);
9171 it2.max_ascent = it2.max_descent = 0;
9172 do
9173 {
9174 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9175 MOVE_TO_POS | MOVE_TO_VPOS);
9176 }
9177 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9178 /* If we are in a display string which starts at START_POS,
9179 and that display string includes a newline, and we are
9180 right after that newline (i.e. at the beginning of a
9181 display line), exit the loop, because otherwise we will
9182 infloop, since move_it_to will see that it is already at
9183 START_POS and will not move. */
9184 || (it2.method == GET_FROM_STRING
9185 && IT_CHARPOS (it2) == start_pos
9186 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9187 eassert (IT_CHARPOS (*it) >= BEGV);
9188 SAVE_IT (it3, it2, it3data);
9189
9190 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9191 eassert (IT_CHARPOS (*it) >= BEGV);
9192 /* H is the actual vertical distance from the position in *IT
9193 and the starting position. */
9194 h = it2.current_y - it->current_y;
9195 /* NLINES is the distance in number of lines. */
9196 nlines = it2.vpos - it->vpos;
9197
9198 /* Correct IT's y and vpos position
9199 so that they are relative to the starting point. */
9200 it->vpos -= nlines;
9201 it->current_y -= h;
9202
9203 if (dy == 0)
9204 {
9205 /* DY == 0 means move to the start of the screen line. The
9206 value of nlines is > 0 if continuation lines were involved,
9207 or if the original IT position was at start of a line. */
9208 RESTORE_IT (it, it, it2data);
9209 if (nlines > 0)
9210 move_it_by_lines (it, nlines);
9211 /* The above code moves us to some position NLINES down,
9212 usually to its first glyph (leftmost in an L2R line), but
9213 that's not necessarily the start of the line, under bidi
9214 reordering. We want to get to the character position
9215 that is immediately after the newline of the previous
9216 line. */
9217 if (it->bidi_p
9218 && !it->continuation_lines_width
9219 && !STRINGP (it->string)
9220 && IT_CHARPOS (*it) > BEGV
9221 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9222 {
9223 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9224
9225 DEC_BOTH (cp, bp);
9226 cp = find_newline_no_quit (cp, bp, -1, NULL);
9227 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9228 }
9229 bidi_unshelve_cache (it3data, 1);
9230 }
9231 else
9232 {
9233 /* The y-position we try to reach, relative to *IT.
9234 Note that H has been subtracted in front of the if-statement. */
9235 int target_y = it->current_y + h - dy;
9236 int y0 = it3.current_y;
9237 int y1;
9238 int line_height;
9239
9240 RESTORE_IT (&it3, &it3, it3data);
9241 y1 = line_bottom_y (&it3);
9242 line_height = y1 - y0;
9243 RESTORE_IT (it, it, it2data);
9244 /* If we did not reach target_y, try to move further backward if
9245 we can. If we moved too far backward, try to move forward. */
9246 if (target_y < it->current_y
9247 /* This is heuristic. In a window that's 3 lines high, with
9248 a line height of 13 pixels each, recentering with point
9249 on the bottom line will try to move -39/2 = 19 pixels
9250 backward. Try to avoid moving into the first line. */
9251 && (it->current_y - target_y
9252 > min (window_box_height (it->w), line_height * 2 / 3))
9253 && IT_CHARPOS (*it) > BEGV)
9254 {
9255 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9256 target_y - it->current_y));
9257 dy = it->current_y - target_y;
9258 goto move_further_back;
9259 }
9260 else if (target_y >= it->current_y + line_height
9261 && IT_CHARPOS (*it) < ZV)
9262 {
9263 /* Should move forward by at least one line, maybe more.
9264
9265 Note: Calling move_it_by_lines can be expensive on
9266 terminal frames, where compute_motion is used (via
9267 vmotion) to do the job, when there are very long lines
9268 and truncate-lines is nil. That's the reason for
9269 treating terminal frames specially here. */
9270
9271 if (!FRAME_WINDOW_P (it->f))
9272 move_it_vertically (it, target_y - (it->current_y + line_height));
9273 else
9274 {
9275 do
9276 {
9277 move_it_by_lines (it, 1);
9278 }
9279 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9280 }
9281 }
9282 }
9283 }
9284
9285
9286 /* Move IT by a specified amount of pixel lines DY. DY negative means
9287 move backwards. DY = 0 means move to start of screen line. At the
9288 end, IT will be on the start of a screen line. */
9289
9290 void
9291 move_it_vertically (struct it *it, int dy)
9292 {
9293 if (dy <= 0)
9294 move_it_vertically_backward (it, -dy);
9295 else
9296 {
9297 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9298 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9299 MOVE_TO_POS | MOVE_TO_Y);
9300 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9301
9302 /* If buffer ends in ZV without a newline, move to the start of
9303 the line to satisfy the post-condition. */
9304 if (IT_CHARPOS (*it) == ZV
9305 && ZV > BEGV
9306 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9307 move_it_by_lines (it, 0);
9308 }
9309 }
9310
9311
9312 /* Move iterator IT past the end of the text line it is in. */
9313
9314 void
9315 move_it_past_eol (struct it *it)
9316 {
9317 enum move_it_result rc;
9318
9319 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9320 if (rc == MOVE_NEWLINE_OR_CR)
9321 set_iterator_to_next (it, 0);
9322 }
9323
9324
9325 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9326 negative means move up. DVPOS == 0 means move to the start of the
9327 screen line.
9328
9329 Optimization idea: If we would know that IT->f doesn't use
9330 a face with proportional font, we could be faster for
9331 truncate-lines nil. */
9332
9333 void
9334 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9335 {
9336
9337 /* The commented-out optimization uses vmotion on terminals. This
9338 gives bad results, because elements like it->what, on which
9339 callers such as pos_visible_p rely, aren't updated. */
9340 /* struct position pos;
9341 if (!FRAME_WINDOW_P (it->f))
9342 {
9343 struct text_pos textpos;
9344
9345 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9346 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9347 reseat (it, textpos, 1);
9348 it->vpos += pos.vpos;
9349 it->current_y += pos.vpos;
9350 }
9351 else */
9352
9353 if (dvpos == 0)
9354 {
9355 /* DVPOS == 0 means move to the start of the screen line. */
9356 move_it_vertically_backward (it, 0);
9357 /* Let next call to line_bottom_y calculate real line height */
9358 last_height = 0;
9359 }
9360 else if (dvpos > 0)
9361 {
9362 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9363 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9364 {
9365 /* Only move to the next buffer position if we ended up in a
9366 string from display property, not in an overlay string
9367 (before-string or after-string). That is because the
9368 latter don't conceal the underlying buffer position, so
9369 we can ask to move the iterator to the exact position we
9370 are interested in. Note that, even if we are already at
9371 IT_CHARPOS (*it), the call below is not a no-op, as it
9372 will detect that we are at the end of the string, pop the
9373 iterator, and compute it->current_x and it->hpos
9374 correctly. */
9375 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9376 -1, -1, -1, MOVE_TO_POS);
9377 }
9378 }
9379 else
9380 {
9381 struct it it2;
9382 void *it2data = NULL;
9383 ptrdiff_t start_charpos, i;
9384 int nchars_per_row
9385 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9386 ptrdiff_t pos_limit;
9387
9388 /* Start at the beginning of the screen line containing IT's
9389 position. This may actually move vertically backwards,
9390 in case of overlays, so adjust dvpos accordingly. */
9391 dvpos += it->vpos;
9392 move_it_vertically_backward (it, 0);
9393 dvpos -= it->vpos;
9394
9395 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9396 screen lines, and reseat the iterator there. */
9397 start_charpos = IT_CHARPOS (*it);
9398 if (it->line_wrap == TRUNCATE)
9399 pos_limit = BEGV;
9400 else
9401 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9402 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9403 back_to_previous_visible_line_start (it);
9404 reseat (it, it->current.pos, 1);
9405
9406 /* Move further back if we end up in a string or an image. */
9407 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9408 {
9409 /* First try to move to start of display line. */
9410 dvpos += it->vpos;
9411 move_it_vertically_backward (it, 0);
9412 dvpos -= it->vpos;
9413 if (IT_POS_VALID_AFTER_MOVE_P (it))
9414 break;
9415 /* If start of line is still in string or image,
9416 move further back. */
9417 back_to_previous_visible_line_start (it);
9418 reseat (it, it->current.pos, 1);
9419 dvpos--;
9420 }
9421
9422 it->current_x = it->hpos = 0;
9423
9424 /* Above call may have moved too far if continuation lines
9425 are involved. Scan forward and see if it did. */
9426 SAVE_IT (it2, *it, it2data);
9427 it2.vpos = it2.current_y = 0;
9428 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9429 it->vpos -= it2.vpos;
9430 it->current_y -= it2.current_y;
9431 it->current_x = it->hpos = 0;
9432
9433 /* If we moved too far back, move IT some lines forward. */
9434 if (it2.vpos > -dvpos)
9435 {
9436 int delta = it2.vpos + dvpos;
9437
9438 RESTORE_IT (&it2, &it2, it2data);
9439 SAVE_IT (it2, *it, it2data);
9440 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9441 /* Move back again if we got too far ahead. */
9442 if (IT_CHARPOS (*it) >= start_charpos)
9443 RESTORE_IT (it, &it2, it2data);
9444 else
9445 bidi_unshelve_cache (it2data, 1);
9446 }
9447 else
9448 RESTORE_IT (it, it, it2data);
9449 }
9450 }
9451
9452 /* Return 1 if IT points into the middle of a display vector. */
9453
9454 int
9455 in_display_vector_p (struct it *it)
9456 {
9457 return (it->method == GET_FROM_DISPLAY_VECTOR
9458 && it->current.dpvec_index > 0
9459 && it->dpvec + it->current.dpvec_index != it->dpend);
9460 }
9461
9462 \f
9463 /***********************************************************************
9464 Messages
9465 ***********************************************************************/
9466
9467
9468 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9469 to *Messages*. */
9470
9471 void
9472 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9473 {
9474 Lisp_Object args[3];
9475 Lisp_Object msg, fmt;
9476 char *buffer;
9477 ptrdiff_t len;
9478 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9479 USE_SAFE_ALLOCA;
9480
9481 fmt = msg = Qnil;
9482 GCPRO4 (fmt, msg, arg1, arg2);
9483
9484 args[0] = fmt = build_string (format);
9485 args[1] = arg1;
9486 args[2] = arg2;
9487 msg = Fformat (3, args);
9488
9489 len = SBYTES (msg) + 1;
9490 buffer = SAFE_ALLOCA (len);
9491 memcpy (buffer, SDATA (msg), len);
9492
9493 message_dolog (buffer, len - 1, 1, 0);
9494 SAFE_FREE ();
9495
9496 UNGCPRO;
9497 }
9498
9499
9500 /* Output a newline in the *Messages* buffer if "needs" one. */
9501
9502 void
9503 message_log_maybe_newline (void)
9504 {
9505 if (message_log_need_newline)
9506 message_dolog ("", 0, 1, 0);
9507 }
9508
9509
9510 /* Add a string M of length NBYTES to the message log, optionally
9511 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9512 true, means interpret the contents of M as multibyte. This
9513 function calls low-level routines in order to bypass text property
9514 hooks, etc. which might not be safe to run.
9515
9516 This may GC (insert may run before/after change hooks),
9517 so the buffer M must NOT point to a Lisp string. */
9518
9519 void
9520 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9521 {
9522 const unsigned char *msg = (const unsigned char *) m;
9523
9524 if (!NILP (Vmemory_full))
9525 return;
9526
9527 if (!NILP (Vmessage_log_max))
9528 {
9529 struct buffer *oldbuf;
9530 Lisp_Object oldpoint, oldbegv, oldzv;
9531 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9532 ptrdiff_t point_at_end = 0;
9533 ptrdiff_t zv_at_end = 0;
9534 Lisp_Object old_deactivate_mark;
9535 bool shown;
9536 struct gcpro gcpro1;
9537
9538 old_deactivate_mark = Vdeactivate_mark;
9539 oldbuf = current_buffer;
9540
9541 /* Ensure the Messages buffer exists, and switch to it.
9542 If we created it, set the major-mode. */
9543 {
9544 int newbuffer = 0;
9545 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9546
9547 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9548
9549 if (newbuffer &&
9550 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9551 call0 (intern ("messages-buffer-mode"));
9552 }
9553
9554 bset_undo_list (current_buffer, Qt);
9555
9556 oldpoint = message_dolog_marker1;
9557 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9558 oldbegv = message_dolog_marker2;
9559 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9560 oldzv = message_dolog_marker3;
9561 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9562 GCPRO1 (old_deactivate_mark);
9563
9564 if (PT == Z)
9565 point_at_end = 1;
9566 if (ZV == Z)
9567 zv_at_end = 1;
9568
9569 BEGV = BEG;
9570 BEGV_BYTE = BEG_BYTE;
9571 ZV = Z;
9572 ZV_BYTE = Z_BYTE;
9573 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9574
9575 /* Insert the string--maybe converting multibyte to single byte
9576 or vice versa, so that all the text fits the buffer. */
9577 if (multibyte
9578 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9579 {
9580 ptrdiff_t i;
9581 int c, char_bytes;
9582 char work[1];
9583
9584 /* Convert a multibyte string to single-byte
9585 for the *Message* buffer. */
9586 for (i = 0; i < nbytes; i += char_bytes)
9587 {
9588 c = string_char_and_length (msg + i, &char_bytes);
9589 work[0] = (ASCII_CHAR_P (c)
9590 ? c
9591 : multibyte_char_to_unibyte (c));
9592 insert_1_both (work, 1, 1, 1, 0, 0);
9593 }
9594 }
9595 else if (! multibyte
9596 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9597 {
9598 ptrdiff_t i;
9599 int c, char_bytes;
9600 unsigned char str[MAX_MULTIBYTE_LENGTH];
9601 /* Convert a single-byte string to multibyte
9602 for the *Message* buffer. */
9603 for (i = 0; i < nbytes; i++)
9604 {
9605 c = msg[i];
9606 MAKE_CHAR_MULTIBYTE (c);
9607 char_bytes = CHAR_STRING (c, str);
9608 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9609 }
9610 }
9611 else if (nbytes)
9612 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9613
9614 if (nlflag)
9615 {
9616 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9617 printmax_t dups;
9618
9619 insert_1_both ("\n", 1, 1, 1, 0, 0);
9620
9621 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9622 this_bol = PT;
9623 this_bol_byte = PT_BYTE;
9624
9625 /* See if this line duplicates the previous one.
9626 If so, combine duplicates. */
9627 if (this_bol > BEG)
9628 {
9629 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9630 prev_bol = PT;
9631 prev_bol_byte = PT_BYTE;
9632
9633 dups = message_log_check_duplicate (prev_bol_byte,
9634 this_bol_byte);
9635 if (dups)
9636 {
9637 del_range_both (prev_bol, prev_bol_byte,
9638 this_bol, this_bol_byte, 0);
9639 if (dups > 1)
9640 {
9641 char dupstr[sizeof " [ times]"
9642 + INT_STRLEN_BOUND (printmax_t)];
9643
9644 /* If you change this format, don't forget to also
9645 change message_log_check_duplicate. */
9646 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9647 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9648 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9649 }
9650 }
9651 }
9652
9653 /* If we have more than the desired maximum number of lines
9654 in the *Messages* buffer now, delete the oldest ones.
9655 This is safe because we don't have undo in this buffer. */
9656
9657 if (NATNUMP (Vmessage_log_max))
9658 {
9659 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9660 -XFASTINT (Vmessage_log_max) - 1, 0);
9661 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9662 }
9663 }
9664 BEGV = marker_position (oldbegv);
9665 BEGV_BYTE = marker_byte_position (oldbegv);
9666
9667 if (zv_at_end)
9668 {
9669 ZV = Z;
9670 ZV_BYTE = Z_BYTE;
9671 }
9672 else
9673 {
9674 ZV = marker_position (oldzv);
9675 ZV_BYTE = marker_byte_position (oldzv);
9676 }
9677
9678 if (point_at_end)
9679 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9680 else
9681 /* We can't do Fgoto_char (oldpoint) because it will run some
9682 Lisp code. */
9683 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9684 marker_byte_position (oldpoint));
9685
9686 UNGCPRO;
9687 unchain_marker (XMARKER (oldpoint));
9688 unchain_marker (XMARKER (oldbegv));
9689 unchain_marker (XMARKER (oldzv));
9690
9691 shown = buffer_window_count (current_buffer) > 0;
9692 set_buffer_internal (oldbuf);
9693 /* We called insert_1_both above with its 5th argument (PREPARE)
9694 zero, which prevents insert_1_both from calling
9695 prepare_to_modify_buffer, which in turns prevents us from
9696 incrementing windows_or_buffers_changed even if *Messages* is
9697 shown in some window. So we must manually incrementing
9698 windows_or_buffers_changed here to make up for that. */
9699 if (shown)
9700 windows_or_buffers_changed++;
9701 else
9702 windows_or_buffers_changed = old_windows_or_buffers_changed;
9703 message_log_need_newline = !nlflag;
9704 Vdeactivate_mark = old_deactivate_mark;
9705 }
9706 }
9707
9708
9709 /* We are at the end of the buffer after just having inserted a newline.
9710 (Note: We depend on the fact we won't be crossing the gap.)
9711 Check to see if the most recent message looks a lot like the previous one.
9712 Return 0 if different, 1 if the new one should just replace it, or a
9713 value N > 1 if we should also append " [N times]". */
9714
9715 static intmax_t
9716 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9717 {
9718 ptrdiff_t i;
9719 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9720 int seen_dots = 0;
9721 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9722 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9723
9724 for (i = 0; i < len; i++)
9725 {
9726 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9727 seen_dots = 1;
9728 if (p1[i] != p2[i])
9729 return seen_dots;
9730 }
9731 p1 += len;
9732 if (*p1 == '\n')
9733 return 2;
9734 if (*p1++ == ' ' && *p1++ == '[')
9735 {
9736 char *pend;
9737 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9738 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9739 return n + 1;
9740 }
9741 return 0;
9742 }
9743 \f
9744
9745 /* Display an echo area message M with a specified length of NBYTES
9746 bytes. The string may include null characters. If M is not a
9747 string, clear out any existing message, and let the mini-buffer
9748 text show through.
9749
9750 This function cancels echoing. */
9751
9752 void
9753 message3 (Lisp_Object m)
9754 {
9755 struct gcpro gcpro1;
9756
9757 GCPRO1 (m);
9758 clear_message (1,1);
9759 cancel_echoing ();
9760
9761 /* First flush out any partial line written with print. */
9762 message_log_maybe_newline ();
9763 if (STRINGP (m))
9764 {
9765 ptrdiff_t nbytes = SBYTES (m);
9766 bool multibyte = STRING_MULTIBYTE (m);
9767 USE_SAFE_ALLOCA;
9768 char *buffer = SAFE_ALLOCA (nbytes);
9769 memcpy (buffer, SDATA (m), nbytes);
9770 message_dolog (buffer, nbytes, 1, multibyte);
9771 SAFE_FREE ();
9772 }
9773 message3_nolog (m);
9774
9775 UNGCPRO;
9776 }
9777
9778
9779 /* The non-logging version of message3.
9780 This does not cancel echoing, because it is used for echoing.
9781 Perhaps we need to make a separate function for echoing
9782 and make this cancel echoing. */
9783
9784 void
9785 message3_nolog (Lisp_Object m)
9786 {
9787 struct frame *sf = SELECTED_FRAME ();
9788
9789 if (FRAME_INITIAL_P (sf))
9790 {
9791 if (noninteractive_need_newline)
9792 putc ('\n', stderr);
9793 noninteractive_need_newline = 0;
9794 if (STRINGP (m))
9795 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9796 if (cursor_in_echo_area == 0)
9797 fprintf (stderr, "\n");
9798 fflush (stderr);
9799 }
9800 /* Error messages get reported properly by cmd_error, so this must be just an
9801 informative message; if the frame hasn't really been initialized yet, just
9802 toss it. */
9803 else if (INTERACTIVE && sf->glyphs_initialized_p)
9804 {
9805 /* Get the frame containing the mini-buffer
9806 that the selected frame is using. */
9807 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9808 Lisp_Object frame = XWINDOW (mini_window)->frame;
9809 struct frame *f = XFRAME (frame);
9810
9811 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9812 Fmake_frame_visible (frame);
9813
9814 if (STRINGP (m) && SCHARS (m) > 0)
9815 {
9816 set_message (m);
9817 if (minibuffer_auto_raise)
9818 Fraise_frame (frame);
9819 /* Assume we are not echoing.
9820 (If we are, echo_now will override this.) */
9821 echo_message_buffer = Qnil;
9822 }
9823 else
9824 clear_message (1, 1);
9825
9826 do_pending_window_change (0);
9827 echo_area_display (1);
9828 do_pending_window_change (0);
9829 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9830 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9831 }
9832 }
9833
9834
9835 /* Display a null-terminated echo area message M. If M is 0, clear
9836 out any existing message, and let the mini-buffer text show through.
9837
9838 The buffer M must continue to exist until after the echo area gets
9839 cleared or some other message gets displayed there. Do not pass
9840 text that is stored in a Lisp string. Do not pass text in a buffer
9841 that was alloca'd. */
9842
9843 void
9844 message1 (const char *m)
9845 {
9846 message3 (m ? build_unibyte_string (m) : Qnil);
9847 }
9848
9849
9850 /* The non-logging counterpart of message1. */
9851
9852 void
9853 message1_nolog (const char *m)
9854 {
9855 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9856 }
9857
9858 /* Display a message M which contains a single %s
9859 which gets replaced with STRING. */
9860
9861 void
9862 message_with_string (const char *m, Lisp_Object string, int log)
9863 {
9864 CHECK_STRING (string);
9865
9866 if (noninteractive)
9867 {
9868 if (m)
9869 {
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, m, SDATA (string));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9877 }
9878 }
9879 else if (INTERACTIVE)
9880 {
9881 /* The frame whose minibuffer we're going to display the message on.
9882 It may be larger than the selected frame, so we need
9883 to use its buffer, not the selected frame's buffer. */
9884 Lisp_Object mini_window;
9885 struct frame *f, *sf = SELECTED_FRAME ();
9886
9887 /* Get the frame containing the minibuffer
9888 that the selected frame is using. */
9889 mini_window = FRAME_MINIBUF_WINDOW (sf);
9890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9891
9892 /* Error messages get reported properly by cmd_error, so this must be
9893 just an informative message; if the frame hasn't really been
9894 initialized yet, just toss it. */
9895 if (f->glyphs_initialized_p)
9896 {
9897 Lisp_Object args[2], msg;
9898 struct gcpro gcpro1, gcpro2;
9899
9900 args[0] = build_string (m);
9901 args[1] = msg = string;
9902 GCPRO2 (args[0], msg);
9903 gcpro1.nvars = 2;
9904
9905 msg = Fformat (2, args);
9906
9907 if (log)
9908 message3 (msg);
9909 else
9910 message3_nolog (msg);
9911
9912 UNGCPRO;
9913
9914 /* Print should start at the beginning of the message
9915 buffer next time. */
9916 message_buf_print = 0;
9917 }
9918 }
9919 }
9920
9921
9922 /* Dump an informative message to the minibuf. If M is 0, clear out
9923 any existing message, and let the mini-buffer text show through. */
9924
9925 static void
9926 vmessage (const char *m, va_list ap)
9927 {
9928 if (noninteractive)
9929 {
9930 if (m)
9931 {
9932 if (noninteractive_need_newline)
9933 putc ('\n', stderr);
9934 noninteractive_need_newline = 0;
9935 vfprintf (stderr, m, ap);
9936 if (cursor_in_echo_area == 0)
9937 fprintf (stderr, "\n");
9938 fflush (stderr);
9939 }
9940 }
9941 else if (INTERACTIVE)
9942 {
9943 /* The frame whose mini-buffer we're going to display the message
9944 on. It may be larger than the selected frame, so we need to
9945 use its buffer, not the selected frame's buffer. */
9946 Lisp_Object mini_window;
9947 struct frame *f, *sf = SELECTED_FRAME ();
9948
9949 /* Get the frame containing the mini-buffer
9950 that the selected frame is using. */
9951 mini_window = FRAME_MINIBUF_WINDOW (sf);
9952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9953
9954 /* Error messages get reported properly by cmd_error, so this must be
9955 just an informative message; if the frame hasn't really been
9956 initialized yet, just toss it. */
9957 if (f->glyphs_initialized_p)
9958 {
9959 if (m)
9960 {
9961 ptrdiff_t len;
9962 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9963 char *message_buf = alloca (maxsize + 1);
9964
9965 len = doprnt (message_buf, maxsize, m, 0, ap);
9966
9967 message3 (make_string (message_buf, len));
9968 }
9969 else
9970 message1 (0);
9971
9972 /* Print should start at the beginning of the message
9973 buffer next time. */
9974 message_buf_print = 0;
9975 }
9976 }
9977 }
9978
9979 void
9980 message (const char *m, ...)
9981 {
9982 va_list ap;
9983 va_start (ap, m);
9984 vmessage (m, ap);
9985 va_end (ap);
9986 }
9987
9988
9989 #if 0
9990 /* The non-logging version of message. */
9991
9992 void
9993 message_nolog (const char *m, ...)
9994 {
9995 Lisp_Object old_log_max;
9996 va_list ap;
9997 va_start (ap, m);
9998 old_log_max = Vmessage_log_max;
9999 Vmessage_log_max = Qnil;
10000 vmessage (m, ap);
10001 Vmessage_log_max = old_log_max;
10002 va_end (ap);
10003 }
10004 #endif
10005
10006
10007 /* Display the current message in the current mini-buffer. This is
10008 only called from error handlers in process.c, and is not time
10009 critical. */
10010
10011 void
10012 update_echo_area (void)
10013 {
10014 if (!NILP (echo_area_buffer[0]))
10015 {
10016 Lisp_Object string;
10017 string = Fcurrent_message ();
10018 message3 (string);
10019 }
10020 }
10021
10022
10023 /* Make sure echo area buffers in `echo_buffers' are live.
10024 If they aren't, make new ones. */
10025
10026 static void
10027 ensure_echo_area_buffers (void)
10028 {
10029 int i;
10030
10031 for (i = 0; i < 2; ++i)
10032 if (!BUFFERP (echo_buffer[i])
10033 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10034 {
10035 char name[30];
10036 Lisp_Object old_buffer;
10037 int j;
10038
10039 old_buffer = echo_buffer[i];
10040 echo_buffer[i] = Fget_buffer_create
10041 (make_formatted_string (name, " *Echo Area %d*", i));
10042 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10043 /* to force word wrap in echo area -
10044 it was decided to postpone this*/
10045 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10046
10047 for (j = 0; j < 2; ++j)
10048 if (EQ (old_buffer, echo_area_buffer[j]))
10049 echo_area_buffer[j] = echo_buffer[i];
10050 }
10051 }
10052
10053
10054 /* Call FN with args A1..A2 with either the current or last displayed
10055 echo_area_buffer as current buffer.
10056
10057 WHICH zero means use the current message buffer
10058 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10059 from echo_buffer[] and clear it.
10060
10061 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10062 suitable buffer from echo_buffer[] and clear it.
10063
10064 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10065 that the current message becomes the last displayed one, make
10066 choose a suitable buffer for echo_area_buffer[0], and clear it.
10067
10068 Value is what FN returns. */
10069
10070 static int
10071 with_echo_area_buffer (struct window *w, int which,
10072 int (*fn) (ptrdiff_t, Lisp_Object),
10073 ptrdiff_t a1, Lisp_Object a2)
10074 {
10075 Lisp_Object buffer;
10076 int this_one, the_other, clear_buffer_p, rc;
10077 ptrdiff_t count = SPECPDL_INDEX ();
10078
10079 /* If buffers aren't live, make new ones. */
10080 ensure_echo_area_buffers ();
10081
10082 clear_buffer_p = 0;
10083
10084 if (which == 0)
10085 this_one = 0, the_other = 1;
10086 else if (which > 0)
10087 this_one = 1, the_other = 0;
10088 else
10089 {
10090 this_one = 0, the_other = 1;
10091 clear_buffer_p = 1;
10092
10093 /* We need a fresh one in case the current echo buffer equals
10094 the one containing the last displayed echo area message. */
10095 if (!NILP (echo_area_buffer[this_one])
10096 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10097 echo_area_buffer[this_one] = Qnil;
10098 }
10099
10100 /* Choose a suitable buffer from echo_buffer[] is we don't
10101 have one. */
10102 if (NILP (echo_area_buffer[this_one]))
10103 {
10104 echo_area_buffer[this_one]
10105 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10106 ? echo_buffer[the_other]
10107 : echo_buffer[this_one]);
10108 clear_buffer_p = 1;
10109 }
10110
10111 buffer = echo_area_buffer[this_one];
10112
10113 /* Don't get confused by reusing the buffer used for echoing
10114 for a different purpose. */
10115 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10116 cancel_echoing ();
10117
10118 record_unwind_protect (unwind_with_echo_area_buffer,
10119 with_echo_area_buffer_unwind_data (w));
10120
10121 /* Make the echo area buffer current. Note that for display
10122 purposes, it is not necessary that the displayed window's buffer
10123 == current_buffer, except for text property lookup. So, let's
10124 only set that buffer temporarily here without doing a full
10125 Fset_window_buffer. We must also change w->pointm, though,
10126 because otherwise an assertions in unshow_buffer fails, and Emacs
10127 aborts. */
10128 set_buffer_internal_1 (XBUFFER (buffer));
10129 if (w)
10130 {
10131 wset_buffer (w, buffer);
10132 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10133 }
10134
10135 bset_undo_list (current_buffer, Qt);
10136 bset_read_only (current_buffer, Qnil);
10137 specbind (Qinhibit_read_only, Qt);
10138 specbind (Qinhibit_modification_hooks, Qt);
10139
10140 if (clear_buffer_p && Z > BEG)
10141 del_range (BEG, Z);
10142
10143 eassert (BEGV >= BEG);
10144 eassert (ZV <= Z && ZV >= BEGV);
10145
10146 rc = fn (a1, a2);
10147
10148 eassert (BEGV >= BEG);
10149 eassert (ZV <= Z && ZV >= BEGV);
10150
10151 unbind_to (count, Qnil);
10152 return rc;
10153 }
10154
10155
10156 /* Save state that should be preserved around the call to the function
10157 FN called in with_echo_area_buffer. */
10158
10159 static Lisp_Object
10160 with_echo_area_buffer_unwind_data (struct window *w)
10161 {
10162 int i = 0;
10163 Lisp_Object vector, tmp;
10164
10165 /* Reduce consing by keeping one vector in
10166 Vwith_echo_area_save_vector. */
10167 vector = Vwith_echo_area_save_vector;
10168 Vwith_echo_area_save_vector = Qnil;
10169
10170 if (NILP (vector))
10171 vector = Fmake_vector (make_number (9), Qnil);
10172
10173 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10174 ASET (vector, i, Vdeactivate_mark); ++i;
10175 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10176
10177 if (w)
10178 {
10179 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10180 ASET (vector, i, w->contents); ++i;
10181 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10182 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10184 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10185 }
10186 else
10187 {
10188 int end = i + 6;
10189 for (; i < end; ++i)
10190 ASET (vector, i, Qnil);
10191 }
10192
10193 eassert (i == ASIZE (vector));
10194 return vector;
10195 }
10196
10197
10198 /* Restore global state from VECTOR which was created by
10199 with_echo_area_buffer_unwind_data. */
10200
10201 static void
10202 unwind_with_echo_area_buffer (Lisp_Object vector)
10203 {
10204 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10205 Vdeactivate_mark = AREF (vector, 1);
10206 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10207
10208 if (WINDOWP (AREF (vector, 3)))
10209 {
10210 struct window *w;
10211 Lisp_Object buffer;
10212
10213 w = XWINDOW (AREF (vector, 3));
10214 buffer = AREF (vector, 4);
10215
10216 wset_buffer (w, buffer);
10217 set_marker_both (w->pointm, buffer,
10218 XFASTINT (AREF (vector, 5)),
10219 XFASTINT (AREF (vector, 6)));
10220 set_marker_both (w->start, buffer,
10221 XFASTINT (AREF (vector, 7)),
10222 XFASTINT (AREF (vector, 8)));
10223 }
10224
10225 Vwith_echo_area_save_vector = vector;
10226 }
10227
10228
10229 /* Set up the echo area for use by print functions. MULTIBYTE_P
10230 non-zero means we will print multibyte. */
10231
10232 void
10233 setup_echo_area_for_printing (int multibyte_p)
10234 {
10235 /* If we can't find an echo area any more, exit. */
10236 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10237 Fkill_emacs (Qnil);
10238
10239 ensure_echo_area_buffers ();
10240
10241 if (!message_buf_print)
10242 {
10243 /* A message has been output since the last time we printed.
10244 Choose a fresh echo area buffer. */
10245 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10246 echo_area_buffer[0] = echo_buffer[1];
10247 else
10248 echo_area_buffer[0] = echo_buffer[0];
10249
10250 /* Switch to that buffer and clear it. */
10251 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10252 bset_truncate_lines (current_buffer, Qnil);
10253
10254 if (Z > BEG)
10255 {
10256 ptrdiff_t count = SPECPDL_INDEX ();
10257 specbind (Qinhibit_read_only, Qt);
10258 /* Note that undo recording is always disabled. */
10259 del_range (BEG, Z);
10260 unbind_to (count, Qnil);
10261 }
10262 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10263
10264 /* Set up the buffer for the multibyteness we need. */
10265 if (multibyte_p
10266 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10267 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10268
10269 /* Raise the frame containing the echo area. */
10270 if (minibuffer_auto_raise)
10271 {
10272 struct frame *sf = SELECTED_FRAME ();
10273 Lisp_Object mini_window;
10274 mini_window = FRAME_MINIBUF_WINDOW (sf);
10275 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10276 }
10277
10278 message_log_maybe_newline ();
10279 message_buf_print = 1;
10280 }
10281 else
10282 {
10283 if (NILP (echo_area_buffer[0]))
10284 {
10285 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10286 echo_area_buffer[0] = echo_buffer[1];
10287 else
10288 echo_area_buffer[0] = echo_buffer[0];
10289 }
10290
10291 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10292 {
10293 /* Someone switched buffers between print requests. */
10294 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10295 bset_truncate_lines (current_buffer, Qnil);
10296 }
10297 }
10298 }
10299
10300
10301 /* Display an echo area message in window W. Value is non-zero if W's
10302 height is changed. If display_last_displayed_message_p is
10303 non-zero, display the message that was last displayed, otherwise
10304 display the current message. */
10305
10306 static int
10307 display_echo_area (struct window *w)
10308 {
10309 int i, no_message_p, window_height_changed_p;
10310
10311 /* Temporarily disable garbage collections while displaying the echo
10312 area. This is done because a GC can print a message itself.
10313 That message would modify the echo area buffer's contents while a
10314 redisplay of the buffer is going on, and seriously confuse
10315 redisplay. */
10316 ptrdiff_t count = inhibit_garbage_collection ();
10317
10318 /* If there is no message, we must call display_echo_area_1
10319 nevertheless because it resizes the window. But we will have to
10320 reset the echo_area_buffer in question to nil at the end because
10321 with_echo_area_buffer will sets it to an empty buffer. */
10322 i = display_last_displayed_message_p ? 1 : 0;
10323 no_message_p = NILP (echo_area_buffer[i]);
10324
10325 window_height_changed_p
10326 = with_echo_area_buffer (w, display_last_displayed_message_p,
10327 display_echo_area_1,
10328 (intptr_t) w, Qnil);
10329
10330 if (no_message_p)
10331 echo_area_buffer[i] = Qnil;
10332
10333 unbind_to (count, Qnil);
10334 return window_height_changed_p;
10335 }
10336
10337
10338 /* Helper for display_echo_area. Display the current buffer which
10339 contains the current echo area message in window W, a mini-window,
10340 a pointer to which is passed in A1. A2..A4 are currently not used.
10341 Change the height of W so that all of the message is displayed.
10342 Value is non-zero if height of W was changed. */
10343
10344 static int
10345 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10346 {
10347 intptr_t i1 = a1;
10348 struct window *w = (struct window *) i1;
10349 Lisp_Object window;
10350 struct text_pos start;
10351 int window_height_changed_p = 0;
10352
10353 /* Do this before displaying, so that we have a large enough glyph
10354 matrix for the display. If we can't get enough space for the
10355 whole text, display the last N lines. That works by setting w->start. */
10356 window_height_changed_p = resize_mini_window (w, 0);
10357
10358 /* Use the starting position chosen by resize_mini_window. */
10359 SET_TEXT_POS_FROM_MARKER (start, w->start);
10360
10361 /* Display. */
10362 clear_glyph_matrix (w->desired_matrix);
10363 XSETWINDOW (window, w);
10364 try_window (window, start, 0);
10365
10366 return window_height_changed_p;
10367 }
10368
10369
10370 /* Resize the echo area window to exactly the size needed for the
10371 currently displayed message, if there is one. If a mini-buffer
10372 is active, don't shrink it. */
10373
10374 void
10375 resize_echo_area_exactly (void)
10376 {
10377 if (BUFFERP (echo_area_buffer[0])
10378 && WINDOWP (echo_area_window))
10379 {
10380 struct window *w = XWINDOW (echo_area_window);
10381 int resized_p;
10382 Lisp_Object resize_exactly;
10383
10384 if (minibuf_level == 0)
10385 resize_exactly = Qt;
10386 else
10387 resize_exactly = Qnil;
10388
10389 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10390 (intptr_t) w, resize_exactly);
10391 if (resized_p)
10392 {
10393 ++windows_or_buffers_changed;
10394 ++update_mode_lines;
10395 redisplay_internal ();
10396 }
10397 }
10398 }
10399
10400
10401 /* Callback function for with_echo_area_buffer, when used from
10402 resize_echo_area_exactly. A1 contains a pointer to the window to
10403 resize, EXACTLY non-nil means resize the mini-window exactly to the
10404 size of the text displayed. A3 and A4 are not used. Value is what
10405 resize_mini_window returns. */
10406
10407 static int
10408 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10409 {
10410 intptr_t i1 = a1;
10411 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10412 }
10413
10414
10415 /* Resize mini-window W to fit the size of its contents. EXACT_P
10416 means size the window exactly to the size needed. Otherwise, it's
10417 only enlarged until W's buffer is empty.
10418
10419 Set W->start to the right place to begin display. If the whole
10420 contents fit, start at the beginning. Otherwise, start so as
10421 to make the end of the contents appear. This is particularly
10422 important for y-or-n-p, but seems desirable generally.
10423
10424 Value is non-zero if the window height has been changed. */
10425
10426 int
10427 resize_mini_window (struct window *w, int exact_p)
10428 {
10429 struct frame *f = XFRAME (w->frame);
10430 int window_height_changed_p = 0;
10431
10432 eassert (MINI_WINDOW_P (w));
10433
10434 /* By default, start display at the beginning. */
10435 set_marker_both (w->start, w->contents,
10436 BUF_BEGV (XBUFFER (w->contents)),
10437 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10438
10439 /* Don't resize windows while redisplaying a window; it would
10440 confuse redisplay functions when the size of the window they are
10441 displaying changes from under them. Such a resizing can happen,
10442 for instance, when which-func prints a long message while
10443 we are running fontification-functions. We're running these
10444 functions with safe_call which binds inhibit-redisplay to t. */
10445 if (!NILP (Vinhibit_redisplay))
10446 return 0;
10447
10448 /* Nil means don't try to resize. */
10449 if (NILP (Vresize_mini_windows)
10450 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10451 return 0;
10452
10453 if (!FRAME_MINIBUF_ONLY_P (f))
10454 {
10455 struct it it;
10456 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10457 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10458 int height;
10459 EMACS_INT max_height;
10460 int unit = FRAME_LINE_HEIGHT (f);
10461 struct text_pos start;
10462 struct buffer *old_current_buffer = NULL;
10463
10464 if (current_buffer != XBUFFER (w->contents))
10465 {
10466 old_current_buffer = current_buffer;
10467 set_buffer_internal (XBUFFER (w->contents));
10468 }
10469
10470 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10471
10472 /* Compute the max. number of lines specified by the user. */
10473 if (FLOATP (Vmax_mini_window_height))
10474 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10475 else if (INTEGERP (Vmax_mini_window_height))
10476 max_height = XINT (Vmax_mini_window_height);
10477 else
10478 max_height = total_height / 4;
10479
10480 /* Correct that max. height if it's bogus. */
10481 max_height = clip_to_bounds (1, max_height, total_height);
10482
10483 /* Find out the height of the text in the window. */
10484 if (it.line_wrap == TRUNCATE)
10485 height = 1;
10486 else
10487 {
10488 last_height = 0;
10489 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10490 if (it.max_ascent == 0 && it.max_descent == 0)
10491 height = it.current_y + last_height;
10492 else
10493 height = it.current_y + it.max_ascent + it.max_descent;
10494 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10495 height = (height + unit - 1) / unit;
10496 }
10497
10498 /* Compute a suitable window start. */
10499 if (height > max_height)
10500 {
10501 height = max_height;
10502 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10503 move_it_vertically_backward (&it, (height - 1) * unit);
10504 start = it.current.pos;
10505 }
10506 else
10507 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10508 SET_MARKER_FROM_TEXT_POS (w->start, start);
10509
10510 if (EQ (Vresize_mini_windows, Qgrow_only))
10511 {
10512 /* Let it grow only, until we display an empty message, in which
10513 case the window shrinks again. */
10514 if (height > WINDOW_TOTAL_LINES (w))
10515 {
10516 int old_height = WINDOW_TOTAL_LINES (w);
10517
10518 FRAME_WINDOWS_FROZEN (f) = 1;
10519 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10521 }
10522 else if (height < WINDOW_TOTAL_LINES (w)
10523 && (exact_p || BEGV == ZV))
10524 {
10525 int old_height = WINDOW_TOTAL_LINES (w);
10526
10527 FRAME_WINDOWS_FROZEN (f) = 0;
10528 shrink_mini_window (w);
10529 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10530 }
10531 }
10532 else
10533 {
10534 /* Always resize to exact size needed. */
10535 if (height > WINDOW_TOTAL_LINES (w))
10536 {
10537 int old_height = WINDOW_TOTAL_LINES (w);
10538
10539 FRAME_WINDOWS_FROZEN (f) = 1;
10540 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10541 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10542 }
10543 else if (height < WINDOW_TOTAL_LINES (w))
10544 {
10545 int old_height = WINDOW_TOTAL_LINES (w);
10546
10547 FRAME_WINDOWS_FROZEN (f) = 0;
10548 shrink_mini_window (w);
10549
10550 if (height)
10551 {
10552 FRAME_WINDOWS_FROZEN (f) = 1;
10553 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10554 }
10555
10556 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10557 }
10558 }
10559
10560 if (old_current_buffer)
10561 set_buffer_internal (old_current_buffer);
10562 }
10563
10564 return window_height_changed_p;
10565 }
10566
10567
10568 /* Value is the current message, a string, or nil if there is no
10569 current message. */
10570
10571 Lisp_Object
10572 current_message (void)
10573 {
10574 Lisp_Object msg;
10575
10576 if (!BUFFERP (echo_area_buffer[0]))
10577 msg = Qnil;
10578 else
10579 {
10580 with_echo_area_buffer (0, 0, current_message_1,
10581 (intptr_t) &msg, Qnil);
10582 if (NILP (msg))
10583 echo_area_buffer[0] = Qnil;
10584 }
10585
10586 return msg;
10587 }
10588
10589
10590 static int
10591 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10592 {
10593 intptr_t i1 = a1;
10594 Lisp_Object *msg = (Lisp_Object *) i1;
10595
10596 if (Z > BEG)
10597 *msg = make_buffer_string (BEG, Z, 1);
10598 else
10599 *msg = Qnil;
10600 return 0;
10601 }
10602
10603
10604 /* Push the current message on Vmessage_stack for later restoration
10605 by restore_message. Value is non-zero if the current message isn't
10606 empty. This is a relatively infrequent operation, so it's not
10607 worth optimizing. */
10608
10609 bool
10610 push_message (void)
10611 {
10612 Lisp_Object msg = current_message ();
10613 Vmessage_stack = Fcons (msg, Vmessage_stack);
10614 return STRINGP (msg);
10615 }
10616
10617
10618 /* Restore message display from the top of Vmessage_stack. */
10619
10620 void
10621 restore_message (void)
10622 {
10623 eassert (CONSP (Vmessage_stack));
10624 message3_nolog (XCAR (Vmessage_stack));
10625 }
10626
10627
10628 /* Handler for unwind-protect calling pop_message. */
10629
10630 void
10631 pop_message_unwind (void)
10632 {
10633 /* Pop the top-most entry off Vmessage_stack. */
10634 eassert (CONSP (Vmessage_stack));
10635 Vmessage_stack = XCDR (Vmessage_stack);
10636 }
10637
10638
10639 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10640 exits. If the stack is not empty, we have a missing pop_message
10641 somewhere. */
10642
10643 void
10644 check_message_stack (void)
10645 {
10646 if (!NILP (Vmessage_stack))
10647 emacs_abort ();
10648 }
10649
10650
10651 /* Truncate to NCHARS what will be displayed in the echo area the next
10652 time we display it---but don't redisplay it now. */
10653
10654 void
10655 truncate_echo_area (ptrdiff_t nchars)
10656 {
10657 if (nchars == 0)
10658 echo_area_buffer[0] = Qnil;
10659 else if (!noninteractive
10660 && INTERACTIVE
10661 && !NILP (echo_area_buffer[0]))
10662 {
10663 struct frame *sf = SELECTED_FRAME ();
10664 /* Error messages get reported properly by cmd_error, so this must be
10665 just an informative message; if the frame hasn't really been
10666 initialized yet, just toss it. */
10667 if (sf->glyphs_initialized_p)
10668 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10669 }
10670 }
10671
10672
10673 /* Helper function for truncate_echo_area. Truncate the current
10674 message to at most NCHARS characters. */
10675
10676 static int
10677 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10678 {
10679 if (BEG + nchars < Z)
10680 del_range (BEG + nchars, Z);
10681 if (Z == BEG)
10682 echo_area_buffer[0] = Qnil;
10683 return 0;
10684 }
10685
10686 /* Set the current message to STRING. */
10687
10688 static void
10689 set_message (Lisp_Object string)
10690 {
10691 eassert (STRINGP (string));
10692
10693 message_enable_multibyte = STRING_MULTIBYTE (string);
10694
10695 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10696 message_buf_print = 0;
10697 help_echo_showing_p = 0;
10698
10699 if (STRINGP (Vdebug_on_message)
10700 && STRINGP (string)
10701 && fast_string_match (Vdebug_on_message, string) >= 0)
10702 call_debugger (list2 (Qerror, string));
10703 }
10704
10705
10706 /* Helper function for set_message. First argument is ignored and second
10707 argument has the same meaning as for set_message.
10708 This function is called with the echo area buffer being current. */
10709
10710 static int
10711 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10712 {
10713 eassert (STRINGP (string));
10714
10715 /* Change multibyteness of the echo buffer appropriately. */
10716 if (message_enable_multibyte
10717 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10718 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10719
10720 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10721 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10722 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10723
10724 /* Insert new message at BEG. */
10725 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10726
10727 /* This function takes care of single/multibyte conversion.
10728 We just have to ensure that the echo area buffer has the right
10729 setting of enable_multibyte_characters. */
10730 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10731
10732 return 0;
10733 }
10734
10735
10736 /* Clear messages. CURRENT_P non-zero means clear the current
10737 message. LAST_DISPLAYED_P non-zero means clear the message
10738 last displayed. */
10739
10740 void
10741 clear_message (int current_p, int last_displayed_p)
10742 {
10743 if (current_p)
10744 {
10745 echo_area_buffer[0] = Qnil;
10746 message_cleared_p = 1;
10747 }
10748
10749 if (last_displayed_p)
10750 echo_area_buffer[1] = Qnil;
10751
10752 message_buf_print = 0;
10753 }
10754
10755 /* Clear garbaged frames.
10756
10757 This function is used where the old redisplay called
10758 redraw_garbaged_frames which in turn called redraw_frame which in
10759 turn called clear_frame. The call to clear_frame was a source of
10760 flickering. I believe a clear_frame is not necessary. It should
10761 suffice in the new redisplay to invalidate all current matrices,
10762 and ensure a complete redisplay of all windows. */
10763
10764 static void
10765 clear_garbaged_frames (void)
10766 {
10767 if (frame_garbaged)
10768 {
10769 Lisp_Object tail, frame;
10770 int changed_count = 0;
10771
10772 FOR_EACH_FRAME (tail, frame)
10773 {
10774 struct frame *f = XFRAME (frame);
10775
10776 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10777 {
10778 if (f->resized_p)
10779 redraw_frame (f);
10780 else
10781 clear_current_matrices (f);
10782 changed_count++;
10783 f->garbaged = 0;
10784 f->resized_p = 0;
10785 }
10786 }
10787
10788 frame_garbaged = 0;
10789 if (changed_count)
10790 ++windows_or_buffers_changed;
10791 }
10792 }
10793
10794
10795 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10796 is non-zero update selected_frame. Value is non-zero if the
10797 mini-windows height has been changed. */
10798
10799 static int
10800 echo_area_display (int update_frame_p)
10801 {
10802 Lisp_Object mini_window;
10803 struct window *w;
10804 struct frame *f;
10805 int window_height_changed_p = 0;
10806 struct frame *sf = SELECTED_FRAME ();
10807
10808 mini_window = FRAME_MINIBUF_WINDOW (sf);
10809 w = XWINDOW (mini_window);
10810 f = XFRAME (WINDOW_FRAME (w));
10811
10812 /* Don't display if frame is invisible or not yet initialized. */
10813 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10814 return 0;
10815
10816 #ifdef HAVE_WINDOW_SYSTEM
10817 /* When Emacs starts, selected_frame may be the initial terminal
10818 frame. If we let this through, a message would be displayed on
10819 the terminal. */
10820 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10821 return 0;
10822 #endif /* HAVE_WINDOW_SYSTEM */
10823
10824 /* Redraw garbaged frames. */
10825 clear_garbaged_frames ();
10826
10827 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10828 {
10829 echo_area_window = mini_window;
10830 window_height_changed_p = display_echo_area (w);
10831 w->must_be_updated_p = 1;
10832
10833 /* Update the display, unless called from redisplay_internal.
10834 Also don't update the screen during redisplay itself. The
10835 update will happen at the end of redisplay, and an update
10836 here could cause confusion. */
10837 if (update_frame_p && !redisplaying_p)
10838 {
10839 int n = 0;
10840
10841 /* If the display update has been interrupted by pending
10842 input, update mode lines in the frame. Due to the
10843 pending input, it might have been that redisplay hasn't
10844 been called, so that mode lines above the echo area are
10845 garbaged. This looks odd, so we prevent it here. */
10846 if (!display_completed)
10847 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10848
10849 if (window_height_changed_p
10850 /* Don't do this if Emacs is shutting down. Redisplay
10851 needs to run hooks. */
10852 && !NILP (Vrun_hooks))
10853 {
10854 /* Must update other windows. Likewise as in other
10855 cases, don't let this update be interrupted by
10856 pending input. */
10857 ptrdiff_t count = SPECPDL_INDEX ();
10858 specbind (Qredisplay_dont_pause, Qt);
10859 windows_or_buffers_changed = 1;
10860 redisplay_internal ();
10861 unbind_to (count, Qnil);
10862 }
10863 else if (FRAME_WINDOW_P (f) && n == 0)
10864 {
10865 /* Window configuration is the same as before.
10866 Can do with a display update of the echo area,
10867 unless we displayed some mode lines. */
10868 update_single_window (w, 1);
10869 flush_frame (f);
10870 }
10871 else
10872 update_frame (f, 1, 1);
10873
10874 /* If cursor is in the echo area, make sure that the next
10875 redisplay displays the minibuffer, so that the cursor will
10876 be replaced with what the minibuffer wants. */
10877 if (cursor_in_echo_area)
10878 ++windows_or_buffers_changed;
10879 }
10880 }
10881 else if (!EQ (mini_window, selected_window))
10882 windows_or_buffers_changed++;
10883
10884 /* Last displayed message is now the current message. */
10885 echo_area_buffer[1] = echo_area_buffer[0];
10886 /* Inform read_char that we're not echoing. */
10887 echo_message_buffer = Qnil;
10888
10889 /* Prevent redisplay optimization in redisplay_internal by resetting
10890 this_line_start_pos. This is done because the mini-buffer now
10891 displays the message instead of its buffer text. */
10892 if (EQ (mini_window, selected_window))
10893 CHARPOS (this_line_start_pos) = 0;
10894
10895 return window_height_changed_p;
10896 }
10897
10898 /* Nonzero if the current window's buffer is shown in more than one
10899 window and was modified since last redisplay. */
10900
10901 static int
10902 buffer_shared_and_changed (void)
10903 {
10904 return (buffer_window_count (current_buffer) > 1
10905 && UNCHANGED_MODIFIED < MODIFF);
10906 }
10907
10908 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10909 is enabled and mark of W's buffer was changed since last W's update. */
10910
10911 static int
10912 window_buffer_changed (struct window *w)
10913 {
10914 struct buffer *b = XBUFFER (w->contents);
10915
10916 eassert (BUFFER_LIVE_P (b));
10917
10918 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10919 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10920 != (w->region_showing != 0)));
10921 }
10922
10923 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10924
10925 static int
10926 mode_line_update_needed (struct window *w)
10927 {
10928 return (w->column_number_displayed != -1
10929 && !(PT == w->last_point && !window_outdated (w))
10930 && (w->column_number_displayed != current_column ()));
10931 }
10932
10933 /* Nonzero if window start of W is frozen and may not be changed during
10934 redisplay. */
10935
10936 static bool
10937 window_frozen_p (struct window *w)
10938 {
10939 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10940 {
10941 Lisp_Object window;
10942
10943 XSETWINDOW (window, w);
10944 if (MINI_WINDOW_P (w))
10945 return 0;
10946 else if (EQ (window, selected_window))
10947 return 0;
10948 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10949 && EQ (window, Vminibuf_scroll_window))
10950 /* This special window can't be frozen too. */
10951 return 0;
10952 else
10953 return 1;
10954 }
10955 return 0;
10956 }
10957
10958 /***********************************************************************
10959 Mode Lines and Frame Titles
10960 ***********************************************************************/
10961
10962 /* A buffer for constructing non-propertized mode-line strings and
10963 frame titles in it; allocated from the heap in init_xdisp and
10964 resized as needed in store_mode_line_noprop_char. */
10965
10966 static char *mode_line_noprop_buf;
10967
10968 /* The buffer's end, and a current output position in it. */
10969
10970 static char *mode_line_noprop_buf_end;
10971 static char *mode_line_noprop_ptr;
10972
10973 #define MODE_LINE_NOPROP_LEN(start) \
10974 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10975
10976 static enum {
10977 MODE_LINE_DISPLAY = 0,
10978 MODE_LINE_TITLE,
10979 MODE_LINE_NOPROP,
10980 MODE_LINE_STRING
10981 } mode_line_target;
10982
10983 /* Alist that caches the results of :propertize.
10984 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10985 static Lisp_Object mode_line_proptrans_alist;
10986
10987 /* List of strings making up the mode-line. */
10988 static Lisp_Object mode_line_string_list;
10989
10990 /* Base face property when building propertized mode line string. */
10991 static Lisp_Object mode_line_string_face;
10992 static Lisp_Object mode_line_string_face_prop;
10993
10994
10995 /* Unwind data for mode line strings */
10996
10997 static Lisp_Object Vmode_line_unwind_vector;
10998
10999 static Lisp_Object
11000 format_mode_line_unwind_data (struct frame *target_frame,
11001 struct buffer *obuf,
11002 Lisp_Object owin,
11003 int save_proptrans)
11004 {
11005 Lisp_Object vector, tmp;
11006
11007 /* Reduce consing by keeping one vector in
11008 Vwith_echo_area_save_vector. */
11009 vector = Vmode_line_unwind_vector;
11010 Vmode_line_unwind_vector = Qnil;
11011
11012 if (NILP (vector))
11013 vector = Fmake_vector (make_number (10), Qnil);
11014
11015 ASET (vector, 0, make_number (mode_line_target));
11016 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11017 ASET (vector, 2, mode_line_string_list);
11018 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11019 ASET (vector, 4, mode_line_string_face);
11020 ASET (vector, 5, mode_line_string_face_prop);
11021
11022 if (obuf)
11023 XSETBUFFER (tmp, obuf);
11024 else
11025 tmp = Qnil;
11026 ASET (vector, 6, tmp);
11027 ASET (vector, 7, owin);
11028 if (target_frame)
11029 {
11030 /* Similarly to `with-selected-window', if the operation selects
11031 a window on another frame, we must restore that frame's
11032 selected window, and (for a tty) the top-frame. */
11033 ASET (vector, 8, target_frame->selected_window);
11034 if (FRAME_TERMCAP_P (target_frame))
11035 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11036 }
11037
11038 return vector;
11039 }
11040
11041 static void
11042 unwind_format_mode_line (Lisp_Object vector)
11043 {
11044 Lisp_Object old_window = AREF (vector, 7);
11045 Lisp_Object target_frame_window = AREF (vector, 8);
11046 Lisp_Object old_top_frame = AREF (vector, 9);
11047
11048 mode_line_target = XINT (AREF (vector, 0));
11049 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11050 mode_line_string_list = AREF (vector, 2);
11051 if (! EQ (AREF (vector, 3), Qt))
11052 mode_line_proptrans_alist = AREF (vector, 3);
11053 mode_line_string_face = AREF (vector, 4);
11054 mode_line_string_face_prop = AREF (vector, 5);
11055
11056 /* Select window before buffer, since it may change the buffer. */
11057 if (!NILP (old_window))
11058 {
11059 /* If the operation that we are unwinding had selected a window
11060 on a different frame, reset its frame-selected-window. For a
11061 text terminal, reset its top-frame if necessary. */
11062 if (!NILP (target_frame_window))
11063 {
11064 Lisp_Object frame
11065 = WINDOW_FRAME (XWINDOW (target_frame_window));
11066
11067 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11068 Fselect_window (target_frame_window, Qt);
11069
11070 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11071 Fselect_frame (old_top_frame, Qt);
11072 }
11073
11074 Fselect_window (old_window, Qt);
11075 }
11076
11077 if (!NILP (AREF (vector, 6)))
11078 {
11079 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11080 ASET (vector, 6, Qnil);
11081 }
11082
11083 Vmode_line_unwind_vector = vector;
11084 }
11085
11086
11087 /* Store a single character C for the frame title in mode_line_noprop_buf.
11088 Re-allocate mode_line_noprop_buf if necessary. */
11089
11090 static void
11091 store_mode_line_noprop_char (char c)
11092 {
11093 /* If output position has reached the end of the allocated buffer,
11094 increase the buffer's size. */
11095 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11096 {
11097 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11098 ptrdiff_t size = len;
11099 mode_line_noprop_buf =
11100 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11101 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11102 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11103 }
11104
11105 *mode_line_noprop_ptr++ = c;
11106 }
11107
11108
11109 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11110 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11111 characters that yield more columns than PRECISION; PRECISION <= 0
11112 means copy the whole string. Pad with spaces until FIELD_WIDTH
11113 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11114 pad. Called from display_mode_element when it is used to build a
11115 frame title. */
11116
11117 static int
11118 store_mode_line_noprop (const char *string, int field_width, int precision)
11119 {
11120 const unsigned char *str = (const unsigned char *) string;
11121 int n = 0;
11122 ptrdiff_t dummy, nbytes;
11123
11124 /* Copy at most PRECISION chars from STR. */
11125 nbytes = strlen (string);
11126 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11127 while (nbytes--)
11128 store_mode_line_noprop_char (*str++);
11129
11130 /* Fill up with spaces until FIELD_WIDTH reached. */
11131 while (field_width > 0
11132 && n < field_width)
11133 {
11134 store_mode_line_noprop_char (' ');
11135 ++n;
11136 }
11137
11138 return n;
11139 }
11140
11141 /***********************************************************************
11142 Frame Titles
11143 ***********************************************************************/
11144
11145 #ifdef HAVE_WINDOW_SYSTEM
11146
11147 /* Set the title of FRAME, if it has changed. The title format is
11148 Vicon_title_format if FRAME is iconified, otherwise it is
11149 frame_title_format. */
11150
11151 static void
11152 x_consider_frame_title (Lisp_Object frame)
11153 {
11154 struct frame *f = XFRAME (frame);
11155
11156 if (FRAME_WINDOW_P (f)
11157 || FRAME_MINIBUF_ONLY_P (f)
11158 || f->explicit_name)
11159 {
11160 /* Do we have more than one visible frame on this X display? */
11161 Lisp_Object tail, other_frame, fmt;
11162 ptrdiff_t title_start;
11163 char *title;
11164 ptrdiff_t len;
11165 struct it it;
11166 ptrdiff_t count = SPECPDL_INDEX ();
11167
11168 FOR_EACH_FRAME (tail, other_frame)
11169 {
11170 struct frame *tf = XFRAME (other_frame);
11171
11172 if (tf != f
11173 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11174 && !FRAME_MINIBUF_ONLY_P (tf)
11175 && !EQ (other_frame, tip_frame)
11176 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11177 break;
11178 }
11179
11180 /* Set global variable indicating that multiple frames exist. */
11181 multiple_frames = CONSP (tail);
11182
11183 /* Switch to the buffer of selected window of the frame. Set up
11184 mode_line_target so that display_mode_element will output into
11185 mode_line_noprop_buf; then display the title. */
11186 record_unwind_protect (unwind_format_mode_line,
11187 format_mode_line_unwind_data
11188 (f, current_buffer, selected_window, 0));
11189
11190 Fselect_window (f->selected_window, Qt);
11191 set_buffer_internal_1
11192 (XBUFFER (XWINDOW (f->selected_window)->contents));
11193 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11194
11195 mode_line_target = MODE_LINE_TITLE;
11196 title_start = MODE_LINE_NOPROP_LEN (0);
11197 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11198 NULL, DEFAULT_FACE_ID);
11199 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11200 len = MODE_LINE_NOPROP_LEN (title_start);
11201 title = mode_line_noprop_buf + title_start;
11202 unbind_to (count, Qnil);
11203
11204 /* Set the title only if it's changed. This avoids consing in
11205 the common case where it hasn't. (If it turns out that we've
11206 already wasted too much time by walking through the list with
11207 display_mode_element, then we might need to optimize at a
11208 higher level than this.) */
11209 if (! STRINGP (f->name)
11210 || SBYTES (f->name) != len
11211 || memcmp (title, SDATA (f->name), len) != 0)
11212 x_implicitly_set_name (f, make_string (title, len), Qnil);
11213 }
11214 }
11215
11216 #endif /* not HAVE_WINDOW_SYSTEM */
11217
11218 \f
11219 /***********************************************************************
11220 Menu Bars
11221 ***********************************************************************/
11222
11223
11224 /* Prepare for redisplay by updating menu-bar item lists when
11225 appropriate. This can call eval. */
11226
11227 void
11228 prepare_menu_bars (void)
11229 {
11230 int all_windows;
11231 struct gcpro gcpro1, gcpro2;
11232 struct frame *f;
11233 Lisp_Object tooltip_frame;
11234
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 tooltip_frame = tip_frame;
11237 #else
11238 tooltip_frame = Qnil;
11239 #endif
11240
11241 /* Update all frame titles based on their buffer names, etc. We do
11242 this before the menu bars so that the buffer-menu will show the
11243 up-to-date frame titles. */
11244 #ifdef HAVE_WINDOW_SYSTEM
11245 if (windows_or_buffers_changed || update_mode_lines)
11246 {
11247 Lisp_Object tail, frame;
11248
11249 FOR_EACH_FRAME (tail, frame)
11250 {
11251 f = XFRAME (frame);
11252 if (!EQ (frame, tooltip_frame)
11253 && (FRAME_ICONIFIED_P (f)
11254 || FRAME_VISIBLE_P (f) == 1
11255 /* Exclude TTY frames that are obscured because they
11256 are not the top frame on their console. This is
11257 because x_consider_frame_title actually switches
11258 to the frame, which for TTY frames means it is
11259 marked as garbaged, and will be completely
11260 redrawn on the next redisplay cycle. This causes
11261 TTY frames to be completely redrawn, when there
11262 are more than one of them, even though nothing
11263 should be changed on display. */
11264 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11265 x_consider_frame_title (frame);
11266 }
11267 }
11268 #endif /* HAVE_WINDOW_SYSTEM */
11269
11270 /* Update the menu bar item lists, if appropriate. This has to be
11271 done before any actual redisplay or generation of display lines. */
11272 all_windows = (update_mode_lines
11273 || buffer_shared_and_changed ()
11274 || windows_or_buffers_changed);
11275 if (all_windows)
11276 {
11277 Lisp_Object tail, frame;
11278 ptrdiff_t count = SPECPDL_INDEX ();
11279 /* 1 means that update_menu_bar has run its hooks
11280 so any further calls to update_menu_bar shouldn't do so again. */
11281 int menu_bar_hooks_run = 0;
11282
11283 record_unwind_save_match_data ();
11284
11285 FOR_EACH_FRAME (tail, frame)
11286 {
11287 f = XFRAME (frame);
11288
11289 /* Ignore tooltip frame. */
11290 if (EQ (frame, tooltip_frame))
11291 continue;
11292
11293 /* If a window on this frame changed size, report that to
11294 the user and clear the size-change flag. */
11295 if (FRAME_WINDOW_SIZES_CHANGED (f))
11296 {
11297 Lisp_Object functions;
11298
11299 /* Clear flag first in case we get an error below. */
11300 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11301 functions = Vwindow_size_change_functions;
11302 GCPRO2 (tail, functions);
11303
11304 while (CONSP (functions))
11305 {
11306 if (!EQ (XCAR (functions), Qt))
11307 call1 (XCAR (functions), frame);
11308 functions = XCDR (functions);
11309 }
11310 UNGCPRO;
11311 }
11312
11313 GCPRO1 (tail);
11314 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11315 #ifdef HAVE_WINDOW_SYSTEM
11316 update_tool_bar (f, 0);
11317 #endif
11318 #ifdef HAVE_NS
11319 if (windows_or_buffers_changed
11320 && FRAME_NS_P (f))
11321 ns_set_doc_edited
11322 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11323 #endif
11324 UNGCPRO;
11325 }
11326
11327 unbind_to (count, Qnil);
11328 }
11329 else
11330 {
11331 struct frame *sf = SELECTED_FRAME ();
11332 update_menu_bar (sf, 1, 0);
11333 #ifdef HAVE_WINDOW_SYSTEM
11334 update_tool_bar (sf, 1);
11335 #endif
11336 }
11337 }
11338
11339
11340 /* Update the menu bar item list for frame F. This has to be done
11341 before we start to fill in any display lines, because it can call
11342 eval.
11343
11344 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11345
11346 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11347 already ran the menu bar hooks for this redisplay, so there
11348 is no need to run them again. The return value is the
11349 updated value of this flag, to pass to the next call. */
11350
11351 static int
11352 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11353 {
11354 Lisp_Object window;
11355 register struct window *w;
11356
11357 /* If called recursively during a menu update, do nothing. This can
11358 happen when, for instance, an activate-menubar-hook causes a
11359 redisplay. */
11360 if (inhibit_menubar_update)
11361 return hooks_run;
11362
11363 window = FRAME_SELECTED_WINDOW (f);
11364 w = XWINDOW (window);
11365
11366 if (FRAME_WINDOW_P (f)
11367 ?
11368 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11369 || defined (HAVE_NS) || defined (USE_GTK)
11370 FRAME_EXTERNAL_MENU_BAR (f)
11371 #else
11372 FRAME_MENU_BAR_LINES (f) > 0
11373 #endif
11374 : FRAME_MENU_BAR_LINES (f) > 0)
11375 {
11376 /* If the user has switched buffers or windows, we need to
11377 recompute to reflect the new bindings. But we'll
11378 recompute when update_mode_lines is set too; that means
11379 that people can use force-mode-line-update to request
11380 that the menu bar be recomputed. The adverse effect on
11381 the rest of the redisplay algorithm is about the same as
11382 windows_or_buffers_changed anyway. */
11383 if (windows_or_buffers_changed
11384 /* This used to test w->update_mode_line, but we believe
11385 there is no need to recompute the menu in that case. */
11386 || update_mode_lines
11387 || window_buffer_changed (w))
11388 {
11389 struct buffer *prev = current_buffer;
11390 ptrdiff_t count = SPECPDL_INDEX ();
11391
11392 specbind (Qinhibit_menubar_update, Qt);
11393
11394 set_buffer_internal_1 (XBUFFER (w->contents));
11395 if (save_match_data)
11396 record_unwind_save_match_data ();
11397 if (NILP (Voverriding_local_map_menu_flag))
11398 {
11399 specbind (Qoverriding_terminal_local_map, Qnil);
11400 specbind (Qoverriding_local_map, Qnil);
11401 }
11402
11403 if (!hooks_run)
11404 {
11405 /* Run the Lucid hook. */
11406 safe_run_hooks (Qactivate_menubar_hook);
11407
11408 /* If it has changed current-menubar from previous value,
11409 really recompute the menu-bar from the value. */
11410 if (! NILP (Vlucid_menu_bar_dirty_flag))
11411 call0 (Qrecompute_lucid_menubar);
11412
11413 safe_run_hooks (Qmenu_bar_update_hook);
11414
11415 hooks_run = 1;
11416 }
11417
11418 XSETFRAME (Vmenu_updating_frame, f);
11419 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11420
11421 /* Redisplay the menu bar in case we changed it. */
11422 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11423 || defined (HAVE_NS) || defined (USE_GTK)
11424 if (FRAME_WINDOW_P (f))
11425 {
11426 #if defined (HAVE_NS)
11427 /* All frames on Mac OS share the same menubar. So only
11428 the selected frame should be allowed to set it. */
11429 if (f == SELECTED_FRAME ())
11430 #endif
11431 set_frame_menubar (f, 0, 0);
11432 }
11433 else
11434 /* On a terminal screen, the menu bar is an ordinary screen
11435 line, and this makes it get updated. */
11436 w->update_mode_line = 1;
11437 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11438 /* In the non-toolkit version, the menu bar is an ordinary screen
11439 line, and this makes it get updated. */
11440 w->update_mode_line = 1;
11441 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11442
11443 unbind_to (count, Qnil);
11444 set_buffer_internal_1 (prev);
11445 }
11446 }
11447
11448 return hooks_run;
11449 }
11450
11451 /***********************************************************************
11452 Tool-bars
11453 ***********************************************************************/
11454
11455 #ifdef HAVE_WINDOW_SYSTEM
11456
11457 /* Tool-bar item index of the item on which a mouse button was pressed
11458 or -1. */
11459
11460 int last_tool_bar_item;
11461
11462 /* Select `frame' temporarily without running all the code in
11463 do_switch_frame.
11464 FIXME: Maybe do_switch_frame should be trimmed down similarly
11465 when `norecord' is set. */
11466 static void
11467 fast_set_selected_frame (Lisp_Object frame)
11468 {
11469 if (!EQ (selected_frame, frame))
11470 {
11471 selected_frame = frame;
11472 selected_window = XFRAME (frame)->selected_window;
11473 }
11474 }
11475
11476 /* Update the tool-bar item list for frame F. This has to be done
11477 before we start to fill in any display lines. Called from
11478 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11479 and restore it here. */
11480
11481 static void
11482 update_tool_bar (struct frame *f, int save_match_data)
11483 {
11484 #if defined (USE_GTK) || defined (HAVE_NS)
11485 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11486 #else
11487 int do_update = WINDOWP (f->tool_bar_window)
11488 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11489 #endif
11490
11491 if (do_update)
11492 {
11493 Lisp_Object window;
11494 struct window *w;
11495
11496 window = FRAME_SELECTED_WINDOW (f);
11497 w = XWINDOW (window);
11498
11499 /* If the user has switched buffers or windows, we need to
11500 recompute to reflect the new bindings. But we'll
11501 recompute when update_mode_lines is set too; that means
11502 that people can use force-mode-line-update to request
11503 that the menu bar be recomputed. The adverse effect on
11504 the rest of the redisplay algorithm is about the same as
11505 windows_or_buffers_changed anyway. */
11506 if (windows_or_buffers_changed
11507 || w->update_mode_line
11508 || update_mode_lines
11509 || window_buffer_changed (w))
11510 {
11511 struct buffer *prev = current_buffer;
11512 ptrdiff_t count = SPECPDL_INDEX ();
11513 Lisp_Object frame, new_tool_bar;
11514 int new_n_tool_bar;
11515 struct gcpro gcpro1;
11516
11517 /* Set current_buffer to the buffer of the selected
11518 window of the frame, so that we get the right local
11519 keymaps. */
11520 set_buffer_internal_1 (XBUFFER (w->contents));
11521
11522 /* Save match data, if we must. */
11523 if (save_match_data)
11524 record_unwind_save_match_data ();
11525
11526 /* Make sure that we don't accidentally use bogus keymaps. */
11527 if (NILP (Voverriding_local_map_menu_flag))
11528 {
11529 specbind (Qoverriding_terminal_local_map, Qnil);
11530 specbind (Qoverriding_local_map, Qnil);
11531 }
11532
11533 GCPRO1 (new_tool_bar);
11534
11535 /* We must temporarily set the selected frame to this frame
11536 before calling tool_bar_items, because the calculation of
11537 the tool-bar keymap uses the selected frame (see
11538 `tool-bar-make-keymap' in tool-bar.el). */
11539 eassert (EQ (selected_window,
11540 /* Since we only explicitly preserve selected_frame,
11541 check that selected_window would be redundant. */
11542 XFRAME (selected_frame)->selected_window));
11543 record_unwind_protect (fast_set_selected_frame, selected_frame);
11544 XSETFRAME (frame, f);
11545 fast_set_selected_frame (frame);
11546
11547 /* Build desired tool-bar items from keymaps. */
11548 new_tool_bar
11549 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11550 &new_n_tool_bar);
11551
11552 /* Redisplay the tool-bar if we changed it. */
11553 if (new_n_tool_bar != f->n_tool_bar_items
11554 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11555 {
11556 /* Redisplay that happens asynchronously due to an expose event
11557 may access f->tool_bar_items. Make sure we update both
11558 variables within BLOCK_INPUT so no such event interrupts. */
11559 block_input ();
11560 fset_tool_bar_items (f, new_tool_bar);
11561 f->n_tool_bar_items = new_n_tool_bar;
11562 w->update_mode_line = 1;
11563 unblock_input ();
11564 }
11565
11566 UNGCPRO;
11567
11568 unbind_to (count, Qnil);
11569 set_buffer_internal_1 (prev);
11570 }
11571 }
11572 }
11573
11574 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11575
11576 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11577 F's desired tool-bar contents. F->tool_bar_items must have
11578 been set up previously by calling prepare_menu_bars. */
11579
11580 static void
11581 build_desired_tool_bar_string (struct frame *f)
11582 {
11583 int i, size, size_needed;
11584 struct gcpro gcpro1, gcpro2, gcpro3;
11585 Lisp_Object image, plist, props;
11586
11587 image = plist = props = Qnil;
11588 GCPRO3 (image, plist, props);
11589
11590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11591 Otherwise, make a new string. */
11592
11593 /* The size of the string we might be able to reuse. */
11594 size = (STRINGP (f->desired_tool_bar_string)
11595 ? SCHARS (f->desired_tool_bar_string)
11596 : 0);
11597
11598 /* We need one space in the string for each image. */
11599 size_needed = f->n_tool_bar_items;
11600
11601 /* Reuse f->desired_tool_bar_string, if possible. */
11602 if (size < size_needed || NILP (f->desired_tool_bar_string))
11603 fset_desired_tool_bar_string
11604 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11605 else
11606 {
11607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11608 Fremove_text_properties (make_number (0), make_number (size),
11609 props, f->desired_tool_bar_string);
11610 }
11611
11612 /* Put a `display' property on the string for the images to display,
11613 put a `menu_item' property on tool-bar items with a value that
11614 is the index of the item in F's tool-bar item vector. */
11615 for (i = 0; i < f->n_tool_bar_items; ++i)
11616 {
11617 #define PROP(IDX) \
11618 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11619
11620 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11621 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11622 int hmargin, vmargin, relief, idx, end;
11623
11624 /* If image is a vector, choose the image according to the
11625 button state. */
11626 image = PROP (TOOL_BAR_ITEM_IMAGES);
11627 if (VECTORP (image))
11628 {
11629 if (enabled_p)
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11633 else
11634 idx = (selected_p
11635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11637
11638 eassert (ASIZE (image) >= idx);
11639 image = AREF (image, idx);
11640 }
11641 else
11642 idx = -1;
11643
11644 /* Ignore invalid image specifications. */
11645 if (!valid_image_p (image))
11646 continue;
11647
11648 /* Display the tool-bar button pressed, or depressed. */
11649 plist = Fcopy_sequence (XCDR (image));
11650
11651 /* Compute margin and relief to draw. */
11652 relief = (tool_bar_button_relief >= 0
11653 ? tool_bar_button_relief
11654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11655 hmargin = vmargin = relief;
11656
11657 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11658 INT_MAX - max (hmargin, vmargin)))
11659 {
11660 hmargin += XFASTINT (Vtool_bar_button_margin);
11661 vmargin += XFASTINT (Vtool_bar_button_margin);
11662 }
11663 else if (CONSP (Vtool_bar_button_margin))
11664 {
11665 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11666 INT_MAX - hmargin))
11667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11668
11669 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11670 INT_MAX - vmargin))
11671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11672 }
11673
11674 if (auto_raise_tool_bar_buttons_p)
11675 {
11676 /* Add a `:relief' property to the image spec if the item is
11677 selected. */
11678 if (selected_p)
11679 {
11680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11681 hmargin -= relief;
11682 vmargin -= relief;
11683 }
11684 }
11685 else
11686 {
11687 /* If image is selected, display it pressed, i.e. with a
11688 negative relief. If it's not selected, display it with a
11689 raised relief. */
11690 plist = Fplist_put (plist, QCrelief,
11691 (selected_p
11692 ? make_number (-relief)
11693 : make_number (relief)));
11694 hmargin -= relief;
11695 vmargin -= relief;
11696 }
11697
11698 /* Put a margin around the image. */
11699 if (hmargin || vmargin)
11700 {
11701 if (hmargin == vmargin)
11702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11703 else
11704 plist = Fplist_put (plist, QCmargin,
11705 Fcons (make_number (hmargin),
11706 make_number (vmargin)));
11707 }
11708
11709 /* If button is not enabled, and we don't have special images
11710 for the disabled state, make the image appear disabled by
11711 applying an appropriate algorithm to it. */
11712 if (!enabled_p && idx < 0)
11713 plist = Fplist_put (plist, QCconversion, Qdisabled);
11714
11715 /* Put a `display' text property on the string for the image to
11716 display. Put a `menu-item' property on the string that gives
11717 the start of this item's properties in the tool-bar items
11718 vector. */
11719 image = Fcons (Qimage, plist);
11720 props = list4 (Qdisplay, image,
11721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11722
11723 /* Let the last image hide all remaining spaces in the tool bar
11724 string. The string can be longer than needed when we reuse a
11725 previous string. */
11726 if (i + 1 == f->n_tool_bar_items)
11727 end = SCHARS (f->desired_tool_bar_string);
11728 else
11729 end = i + 1;
11730 Fadd_text_properties (make_number (i), make_number (end),
11731 props, f->desired_tool_bar_string);
11732 #undef PROP
11733 }
11734
11735 UNGCPRO;
11736 }
11737
11738
11739 /* Display one line of the tool-bar of frame IT->f.
11740
11741 HEIGHT specifies the desired height of the tool-bar line.
11742 If the actual height of the glyph row is less than HEIGHT, the
11743 row's height is increased to HEIGHT, and the icons are centered
11744 vertically in the new height.
11745
11746 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11747 count a final empty row in case the tool-bar width exactly matches
11748 the window width.
11749 */
11750
11751 static void
11752 display_tool_bar_line (struct it *it, int height)
11753 {
11754 struct glyph_row *row = it->glyph_row;
11755 int max_x = it->last_visible_x;
11756 struct glyph *last;
11757
11758 prepare_desired_row (row);
11759 row->y = it->current_y;
11760
11761 /* Note that this isn't made use of if the face hasn't a box,
11762 so there's no need to check the face here. */
11763 it->start_of_box_run_p = 1;
11764
11765 while (it->current_x < max_x)
11766 {
11767 int x, n_glyphs_before, i, nglyphs;
11768 struct it it_before;
11769
11770 /* Get the next display element. */
11771 if (!get_next_display_element (it))
11772 {
11773 /* Don't count empty row if we are counting needed tool-bar lines. */
11774 if (height < 0 && !it->hpos)
11775 return;
11776 break;
11777 }
11778
11779 /* Produce glyphs. */
11780 n_glyphs_before = row->used[TEXT_AREA];
11781 it_before = *it;
11782
11783 PRODUCE_GLYPHS (it);
11784
11785 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11786 i = 0;
11787 x = it_before.current_x;
11788 while (i < nglyphs)
11789 {
11790 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11791
11792 if (x + glyph->pixel_width > max_x)
11793 {
11794 /* Glyph doesn't fit on line. Backtrack. */
11795 row->used[TEXT_AREA] = n_glyphs_before;
11796 *it = it_before;
11797 /* If this is the only glyph on this line, it will never fit on the
11798 tool-bar, so skip it. But ensure there is at least one glyph,
11799 so we don't accidentally disable the tool-bar. */
11800 if (n_glyphs_before == 0
11801 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11802 break;
11803 goto out;
11804 }
11805
11806 ++it->hpos;
11807 x += glyph->pixel_width;
11808 ++i;
11809 }
11810
11811 /* Stop at line end. */
11812 if (ITERATOR_AT_END_OF_LINE_P (it))
11813 break;
11814
11815 set_iterator_to_next (it, 1);
11816 }
11817
11818 out:;
11819
11820 row->displays_text_p = row->used[TEXT_AREA] != 0;
11821
11822 /* Use default face for the border below the tool bar.
11823
11824 FIXME: When auto-resize-tool-bars is grow-only, there is
11825 no additional border below the possibly empty tool-bar lines.
11826 So to make the extra empty lines look "normal", we have to
11827 use the tool-bar face for the border too. */
11828 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11829 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11830 it->face_id = DEFAULT_FACE_ID;
11831
11832 extend_face_to_end_of_line (it);
11833 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11834 last->right_box_line_p = 1;
11835 if (last == row->glyphs[TEXT_AREA])
11836 last->left_box_line_p = 1;
11837
11838 /* Make line the desired height and center it vertically. */
11839 if ((height -= it->max_ascent + it->max_descent) > 0)
11840 {
11841 /* Don't add more than one line height. */
11842 height %= FRAME_LINE_HEIGHT (it->f);
11843 it->max_ascent += height / 2;
11844 it->max_descent += (height + 1) / 2;
11845 }
11846
11847 compute_line_metrics (it);
11848
11849 /* If line is empty, make it occupy the rest of the tool-bar. */
11850 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11851 {
11852 row->height = row->phys_height = it->last_visible_y - row->y;
11853 row->visible_height = row->height;
11854 row->ascent = row->phys_ascent = 0;
11855 row->extra_line_spacing = 0;
11856 }
11857
11858 row->full_width_p = 1;
11859 row->continued_p = 0;
11860 row->truncated_on_left_p = 0;
11861 row->truncated_on_right_p = 0;
11862
11863 it->current_x = it->hpos = 0;
11864 it->current_y += row->height;
11865 ++it->vpos;
11866 ++it->glyph_row;
11867 }
11868
11869
11870 /* Max tool-bar height. */
11871
11872 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11873 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11874
11875 /* Value is the number of screen lines needed to make all tool-bar
11876 items of frame F visible. The number of actual rows needed is
11877 returned in *N_ROWS if non-NULL. */
11878
11879 static int
11880 tool_bar_lines_needed (struct frame *f, int *n_rows)
11881 {
11882 struct window *w = XWINDOW (f->tool_bar_window);
11883 struct it it;
11884 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11885 the desired matrix, so use (unused) mode-line row as temporary row to
11886 avoid destroying the first tool-bar row. */
11887 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11888
11889 /* Initialize an iterator for iteration over
11890 F->desired_tool_bar_string in the tool-bar window of frame F. */
11891 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11892 it.first_visible_x = 0;
11893 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11894 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11895 it.paragraph_embedding = L2R;
11896
11897 while (!ITERATOR_AT_END_P (&it))
11898 {
11899 clear_glyph_row (temp_row);
11900 it.glyph_row = temp_row;
11901 display_tool_bar_line (&it, -1);
11902 }
11903 clear_glyph_row (temp_row);
11904
11905 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11906 if (n_rows)
11907 *n_rows = it.vpos > 0 ? it.vpos : -1;
11908
11909 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11910 }
11911
11912 #endif /* !USE_GTK && !HAVE_NS */
11913
11914 #if defined USE_GTK || defined HAVE_NS
11915 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11916 #endif
11917
11918 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11919 0, 1, 0,
11920 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11921 If FRAME is nil or omitted, use the selected frame. */)
11922 (Lisp_Object frame)
11923 {
11924 int nlines = 0;
11925 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11926 struct frame *f = decode_any_frame (frame);
11927 struct window *w;
11928
11929 if (WINDOWP (f->tool_bar_window)
11930 && (w = XWINDOW (f->tool_bar_window),
11931 WINDOW_TOTAL_LINES (w) > 0))
11932 {
11933 update_tool_bar (f, 1);
11934 if (f->n_tool_bar_items)
11935 {
11936 build_desired_tool_bar_string (f);
11937 nlines = tool_bar_lines_needed (f, NULL);
11938 }
11939 }
11940 #endif
11941 return make_number (nlines);
11942 }
11943
11944
11945 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11946 height should be changed. */
11947
11948 static int
11949 redisplay_tool_bar (struct frame *f)
11950 {
11951 #if defined (USE_GTK) || defined (HAVE_NS)
11952
11953 if (FRAME_EXTERNAL_TOOL_BAR (f))
11954 update_frame_tool_bar (f);
11955 return 0;
11956
11957 #else /* !USE_GTK && !HAVE_NS */
11958
11959 struct window *w;
11960 struct it it;
11961 struct glyph_row *row;
11962
11963 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11964 do anything. This means you must start with tool-bar-lines
11965 non-zero to get the auto-sizing effect. Or in other words, you
11966 can turn off tool-bars by specifying tool-bar-lines zero. */
11967 if (!WINDOWP (f->tool_bar_window)
11968 || (w = XWINDOW (f->tool_bar_window),
11969 WINDOW_TOTAL_LINES (w) == 0))
11970 return 0;
11971
11972 /* Set up an iterator for the tool-bar window. */
11973 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11974 it.first_visible_x = 0;
11975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11976 row = it.glyph_row;
11977
11978 /* Build a string that represents the contents of the tool-bar. */
11979 build_desired_tool_bar_string (f);
11980 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11981 /* FIXME: This should be controlled by a user option. But it
11982 doesn't make sense to have an R2L tool bar if the menu bar cannot
11983 be drawn also R2L, and making the menu bar R2L is tricky due
11984 toolkit-specific code that implements it. If an R2L tool bar is
11985 ever supported, display_tool_bar_line should also be augmented to
11986 call unproduce_glyphs like display_line and display_string
11987 do. */
11988 it.paragraph_embedding = L2R;
11989
11990 if (f->n_tool_bar_rows == 0)
11991 {
11992 int nlines;
11993
11994 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11995 nlines != WINDOW_TOTAL_LINES (w)))
11996 {
11997 Lisp_Object frame;
11998 int old_height = WINDOW_TOTAL_LINES (w);
11999
12000 XSETFRAME (frame, f);
12001 Fmodify_frame_parameters (frame,
12002 list1 (Fcons (Qtool_bar_lines,
12003 make_number (nlines))));
12004 if (WINDOW_TOTAL_LINES (w) != old_height)
12005 {
12006 clear_glyph_matrix (w->desired_matrix);
12007 f->fonts_changed = 1;
12008 return 1;
12009 }
12010 }
12011 }
12012
12013 /* Display as many lines as needed to display all tool-bar items. */
12014
12015 if (f->n_tool_bar_rows > 0)
12016 {
12017 int border, rows, height, extra;
12018
12019 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12020 border = XINT (Vtool_bar_border);
12021 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12022 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12023 else if (EQ (Vtool_bar_border, Qborder_width))
12024 border = f->border_width;
12025 else
12026 border = 0;
12027 if (border < 0)
12028 border = 0;
12029
12030 rows = f->n_tool_bar_rows;
12031 height = max (1, (it.last_visible_y - border) / rows);
12032 extra = it.last_visible_y - border - height * rows;
12033
12034 while (it.current_y < it.last_visible_y)
12035 {
12036 int h = 0;
12037 if (extra > 0 && rows-- > 0)
12038 {
12039 h = (extra + rows - 1) / rows;
12040 extra -= h;
12041 }
12042 display_tool_bar_line (&it, height + h);
12043 }
12044 }
12045 else
12046 {
12047 while (it.current_y < it.last_visible_y)
12048 display_tool_bar_line (&it, 0);
12049 }
12050
12051 /* It doesn't make much sense to try scrolling in the tool-bar
12052 window, so don't do it. */
12053 w->desired_matrix->no_scrolling_p = 1;
12054 w->must_be_updated_p = 1;
12055
12056 if (!NILP (Vauto_resize_tool_bars))
12057 {
12058 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12059 int change_height_p = 0;
12060
12061 /* If we couldn't display everything, change the tool-bar's
12062 height if there is room for more. */
12063 if (IT_STRING_CHARPOS (it) < it.end_charpos
12064 && it.current_y < max_tool_bar_height)
12065 change_height_p = 1;
12066
12067 row = it.glyph_row - 1;
12068
12069 /* If there are blank lines at the end, except for a partially
12070 visible blank line at the end that is smaller than
12071 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12072 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12073 && row->height >= FRAME_LINE_HEIGHT (f))
12074 change_height_p = 1;
12075
12076 /* If row displays tool-bar items, but is partially visible,
12077 change the tool-bar's height. */
12078 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12079 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12080 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12081 change_height_p = 1;
12082
12083 /* Resize windows as needed by changing the `tool-bar-lines'
12084 frame parameter. */
12085 if (change_height_p)
12086 {
12087 Lisp_Object frame;
12088 int old_height = WINDOW_TOTAL_LINES (w);
12089 int nrows;
12090 int nlines = tool_bar_lines_needed (f, &nrows);
12091
12092 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12093 && !f->minimize_tool_bar_window_p)
12094 ? (nlines > old_height)
12095 : (nlines != old_height));
12096 f->minimize_tool_bar_window_p = 0;
12097
12098 if (change_height_p)
12099 {
12100 XSETFRAME (frame, f);
12101 Fmodify_frame_parameters (frame,
12102 list1 (Fcons (Qtool_bar_lines,
12103 make_number (nlines))));
12104 if (WINDOW_TOTAL_LINES (w) != old_height)
12105 {
12106 clear_glyph_matrix (w->desired_matrix);
12107 f->n_tool_bar_rows = nrows;
12108 f->fonts_changed = 1;
12109 return 1;
12110 }
12111 }
12112 }
12113 }
12114
12115 f->minimize_tool_bar_window_p = 0;
12116 return 0;
12117
12118 #endif /* USE_GTK || HAVE_NS */
12119 }
12120
12121 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12122
12123 /* Get information about the tool-bar item which is displayed in GLYPH
12124 on frame F. Return in *PROP_IDX the index where tool-bar item
12125 properties start in F->tool_bar_items. Value is zero if
12126 GLYPH doesn't display a tool-bar item. */
12127
12128 static int
12129 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12130 {
12131 Lisp_Object prop;
12132 int success_p;
12133 int charpos;
12134
12135 /* This function can be called asynchronously, which means we must
12136 exclude any possibility that Fget_text_property signals an
12137 error. */
12138 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12139 charpos = max (0, charpos);
12140
12141 /* Get the text property `menu-item' at pos. The value of that
12142 property is the start index of this item's properties in
12143 F->tool_bar_items. */
12144 prop = Fget_text_property (make_number (charpos),
12145 Qmenu_item, f->current_tool_bar_string);
12146 if (INTEGERP (prop))
12147 {
12148 *prop_idx = XINT (prop);
12149 success_p = 1;
12150 }
12151 else
12152 success_p = 0;
12153
12154 return success_p;
12155 }
12156
12157 \f
12158 /* Get information about the tool-bar item at position X/Y on frame F.
12159 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12160 the current matrix of the tool-bar window of F, or NULL if not
12161 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12162 item in F->tool_bar_items. Value is
12163
12164 -1 if X/Y is not on a tool-bar item
12165 0 if X/Y is on the same item that was highlighted before.
12166 1 otherwise. */
12167
12168 static int
12169 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12170 int *hpos, int *vpos, int *prop_idx)
12171 {
12172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12173 struct window *w = XWINDOW (f->tool_bar_window);
12174 int area;
12175
12176 /* Find the glyph under X/Y. */
12177 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12178 if (*glyph == NULL)
12179 return -1;
12180
12181 /* Get the start of this tool-bar item's properties in
12182 f->tool_bar_items. */
12183 if (!tool_bar_item_info (f, *glyph, prop_idx))
12184 return -1;
12185
12186 /* Is mouse on the highlighted item? */
12187 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12188 && *vpos >= hlinfo->mouse_face_beg_row
12189 && *vpos <= hlinfo->mouse_face_end_row
12190 && (*vpos > hlinfo->mouse_face_beg_row
12191 || *hpos >= hlinfo->mouse_face_beg_col)
12192 && (*vpos < hlinfo->mouse_face_end_row
12193 || *hpos < hlinfo->mouse_face_end_col
12194 || hlinfo->mouse_face_past_end))
12195 return 0;
12196
12197 return 1;
12198 }
12199
12200
12201 /* EXPORT:
12202 Handle mouse button event on the tool-bar of frame F, at
12203 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12204 0 for button release. MODIFIERS is event modifiers for button
12205 release. */
12206
12207 void
12208 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12209 int modifiers)
12210 {
12211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12212 struct window *w = XWINDOW (f->tool_bar_window);
12213 int hpos, vpos, prop_idx;
12214 struct glyph *glyph;
12215 Lisp_Object enabled_p;
12216 int ts;
12217
12218 /* If not on the highlighted tool-bar item, and mouse-highlight is
12219 non-nil, return. This is so we generate the tool-bar button
12220 click only when the mouse button is released on the same item as
12221 where it was pressed. However, when mouse-highlight is disabled,
12222 generate the click when the button is released regardless of the
12223 highlight, since tool-bar items are not highlighted in that
12224 case. */
12225 frame_to_window_pixel_xy (w, &x, &y);
12226 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12227 if (ts == -1
12228 || (ts != 0 && !NILP (Vmouse_highlight)))
12229 return;
12230
12231 /* When mouse-highlight is off, generate the click for the item
12232 where the button was pressed, disregarding where it was
12233 released. */
12234 if (NILP (Vmouse_highlight) && !down_p)
12235 prop_idx = last_tool_bar_item;
12236
12237 /* If item is disabled, do nothing. */
12238 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12239 if (NILP (enabled_p))
12240 return;
12241
12242 if (down_p)
12243 {
12244 /* Show item in pressed state. */
12245 if (!NILP (Vmouse_highlight))
12246 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12247 last_tool_bar_item = prop_idx;
12248 }
12249 else
12250 {
12251 Lisp_Object key, frame;
12252 struct input_event event;
12253 EVENT_INIT (event);
12254
12255 /* Show item in released state. */
12256 if (!NILP (Vmouse_highlight))
12257 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12258
12259 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12260
12261 XSETFRAME (frame, f);
12262 event.kind = TOOL_BAR_EVENT;
12263 event.frame_or_window = frame;
12264 event.arg = frame;
12265 kbd_buffer_store_event (&event);
12266
12267 event.kind = TOOL_BAR_EVENT;
12268 event.frame_or_window = frame;
12269 event.arg = key;
12270 event.modifiers = modifiers;
12271 kbd_buffer_store_event (&event);
12272 last_tool_bar_item = -1;
12273 }
12274 }
12275
12276
12277 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12278 tool-bar window-relative coordinates X/Y. Called from
12279 note_mouse_highlight. */
12280
12281 static void
12282 note_tool_bar_highlight (struct frame *f, int x, int y)
12283 {
12284 Lisp_Object window = f->tool_bar_window;
12285 struct window *w = XWINDOW (window);
12286 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12288 int hpos, vpos;
12289 struct glyph *glyph;
12290 struct glyph_row *row;
12291 int i;
12292 Lisp_Object enabled_p;
12293 int prop_idx;
12294 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12295 int mouse_down_p, rc;
12296
12297 /* Function note_mouse_highlight is called with negative X/Y
12298 values when mouse moves outside of the frame. */
12299 if (x <= 0 || y <= 0)
12300 {
12301 clear_mouse_face (hlinfo);
12302 return;
12303 }
12304
12305 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12306 if (rc < 0)
12307 {
12308 /* Not on tool-bar item. */
12309 clear_mouse_face (hlinfo);
12310 return;
12311 }
12312 else if (rc == 0)
12313 /* On same tool-bar item as before. */
12314 goto set_help_echo;
12315
12316 clear_mouse_face (hlinfo);
12317
12318 /* Mouse is down, but on different tool-bar item? */
12319 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12320 && f == dpyinfo->last_mouse_frame);
12321
12322 if (mouse_down_p
12323 && last_tool_bar_item != prop_idx)
12324 return;
12325
12326 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12327
12328 /* If tool-bar item is not enabled, don't highlight it. */
12329 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12330 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12331 {
12332 /* Compute the x-position of the glyph. In front and past the
12333 image is a space. We include this in the highlighted area. */
12334 row = MATRIX_ROW (w->current_matrix, vpos);
12335 for (i = x = 0; i < hpos; ++i)
12336 x += row->glyphs[TEXT_AREA][i].pixel_width;
12337
12338 /* Record this as the current active region. */
12339 hlinfo->mouse_face_beg_col = hpos;
12340 hlinfo->mouse_face_beg_row = vpos;
12341 hlinfo->mouse_face_beg_x = x;
12342 hlinfo->mouse_face_past_end = 0;
12343
12344 hlinfo->mouse_face_end_col = hpos + 1;
12345 hlinfo->mouse_face_end_row = vpos;
12346 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12347 hlinfo->mouse_face_window = window;
12348 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12349
12350 /* Display it as active. */
12351 show_mouse_face (hlinfo, draw);
12352 }
12353
12354 set_help_echo:
12355
12356 /* Set help_echo_string to a help string to display for this tool-bar item.
12357 XTread_socket does the rest. */
12358 help_echo_object = help_echo_window = Qnil;
12359 help_echo_pos = -1;
12360 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12361 if (NILP (help_echo_string))
12362 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12363 }
12364
12365 #endif /* !USE_GTK && !HAVE_NS */
12366
12367 #endif /* HAVE_WINDOW_SYSTEM */
12368
12369
12370 \f
12371 /************************************************************************
12372 Horizontal scrolling
12373 ************************************************************************/
12374
12375 static int hscroll_window_tree (Lisp_Object);
12376 static int hscroll_windows (Lisp_Object);
12377
12378 /* For all leaf windows in the window tree rooted at WINDOW, set their
12379 hscroll value so that PT is (i) visible in the window, and (ii) so
12380 that it is not within a certain margin at the window's left and
12381 right border. Value is non-zero if any window's hscroll has been
12382 changed. */
12383
12384 static int
12385 hscroll_window_tree (Lisp_Object window)
12386 {
12387 int hscrolled_p = 0;
12388 int hscroll_relative_p = FLOATP (Vhscroll_step);
12389 int hscroll_step_abs = 0;
12390 double hscroll_step_rel = 0;
12391
12392 if (hscroll_relative_p)
12393 {
12394 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12395 if (hscroll_step_rel < 0)
12396 {
12397 hscroll_relative_p = 0;
12398 hscroll_step_abs = 0;
12399 }
12400 }
12401 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12402 {
12403 hscroll_step_abs = XINT (Vhscroll_step);
12404 if (hscroll_step_abs < 0)
12405 hscroll_step_abs = 0;
12406 }
12407 else
12408 hscroll_step_abs = 0;
12409
12410 while (WINDOWP (window))
12411 {
12412 struct window *w = XWINDOW (window);
12413
12414 if (WINDOWP (w->contents))
12415 hscrolled_p |= hscroll_window_tree (w->contents);
12416 else if (w->cursor.vpos >= 0)
12417 {
12418 int h_margin;
12419 int text_area_width;
12420 struct glyph_row *current_cursor_row
12421 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12422 struct glyph_row *desired_cursor_row
12423 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12424 struct glyph_row *cursor_row
12425 = (desired_cursor_row->enabled_p
12426 ? desired_cursor_row
12427 : current_cursor_row);
12428 int row_r2l_p = cursor_row->reversed_p;
12429
12430 text_area_width = window_box_width (w, TEXT_AREA);
12431
12432 /* Scroll when cursor is inside this scroll margin. */
12433 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12434
12435 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12436 /* For left-to-right rows, hscroll when cursor is either
12437 (i) inside the right hscroll margin, or (ii) if it is
12438 inside the left margin and the window is already
12439 hscrolled. */
12440 && ((!row_r2l_p
12441 && ((w->hscroll
12442 && w->cursor.x <= h_margin)
12443 || (cursor_row->enabled_p
12444 && cursor_row->truncated_on_right_p
12445 && (w->cursor.x >= text_area_width - h_margin))))
12446 /* For right-to-left rows, the logic is similar,
12447 except that rules for scrolling to left and right
12448 are reversed. E.g., if cursor.x <= h_margin, we
12449 need to hscroll "to the right" unconditionally,
12450 and that will scroll the screen to the left so as
12451 to reveal the next portion of the row. */
12452 || (row_r2l_p
12453 && ((cursor_row->enabled_p
12454 /* FIXME: It is confusing to set the
12455 truncated_on_right_p flag when R2L rows
12456 are actually truncated on the left. */
12457 && cursor_row->truncated_on_right_p
12458 && w->cursor.x <= h_margin)
12459 || (w->hscroll
12460 && (w->cursor.x >= text_area_width - h_margin))))))
12461 {
12462 struct it it;
12463 ptrdiff_t hscroll;
12464 struct buffer *saved_current_buffer;
12465 ptrdiff_t pt;
12466 int wanted_x;
12467
12468 /* Find point in a display of infinite width. */
12469 saved_current_buffer = current_buffer;
12470 current_buffer = XBUFFER (w->contents);
12471
12472 if (w == XWINDOW (selected_window))
12473 pt = PT;
12474 else
12475 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12476
12477 /* Move iterator to pt starting at cursor_row->start in
12478 a line with infinite width. */
12479 init_to_row_start (&it, w, cursor_row);
12480 it.last_visible_x = INFINITY;
12481 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12482 current_buffer = saved_current_buffer;
12483
12484 /* Position cursor in window. */
12485 if (!hscroll_relative_p && hscroll_step_abs == 0)
12486 hscroll = max (0, (it.current_x
12487 - (ITERATOR_AT_END_OF_LINE_P (&it)
12488 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12489 : (text_area_width / 2))))
12490 / FRAME_COLUMN_WIDTH (it.f);
12491 else if ((!row_r2l_p
12492 && w->cursor.x >= text_area_width - h_margin)
12493 || (row_r2l_p && w->cursor.x <= h_margin))
12494 {
12495 if (hscroll_relative_p)
12496 wanted_x = text_area_width * (1 - hscroll_step_rel)
12497 - h_margin;
12498 else
12499 wanted_x = text_area_width
12500 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12501 - h_margin;
12502 hscroll
12503 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12504 }
12505 else
12506 {
12507 if (hscroll_relative_p)
12508 wanted_x = text_area_width * hscroll_step_rel
12509 + h_margin;
12510 else
12511 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12512 + h_margin;
12513 hscroll
12514 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12515 }
12516 hscroll = max (hscroll, w->min_hscroll);
12517
12518 /* Don't prevent redisplay optimizations if hscroll
12519 hasn't changed, as it will unnecessarily slow down
12520 redisplay. */
12521 if (w->hscroll != hscroll)
12522 {
12523 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12524 w->hscroll = hscroll;
12525 hscrolled_p = 1;
12526 }
12527 }
12528 }
12529
12530 window = w->next;
12531 }
12532
12533 /* Value is non-zero if hscroll of any leaf window has been changed. */
12534 return hscrolled_p;
12535 }
12536
12537
12538 /* Set hscroll so that cursor is visible and not inside horizontal
12539 scroll margins for all windows in the tree rooted at WINDOW. See
12540 also hscroll_window_tree above. Value is non-zero if any window's
12541 hscroll has been changed. If it has, desired matrices on the frame
12542 of WINDOW are cleared. */
12543
12544 static int
12545 hscroll_windows (Lisp_Object window)
12546 {
12547 int hscrolled_p = hscroll_window_tree (window);
12548 if (hscrolled_p)
12549 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12550 return hscrolled_p;
12551 }
12552
12553
12554 \f
12555 /************************************************************************
12556 Redisplay
12557 ************************************************************************/
12558
12559 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12560 to a non-zero value. This is sometimes handy to have in a debugger
12561 session. */
12562
12563 #ifdef GLYPH_DEBUG
12564
12565 /* First and last unchanged row for try_window_id. */
12566
12567 static int debug_first_unchanged_at_end_vpos;
12568 static int debug_last_unchanged_at_beg_vpos;
12569
12570 /* Delta vpos and y. */
12571
12572 static int debug_dvpos, debug_dy;
12573
12574 /* Delta in characters and bytes for try_window_id. */
12575
12576 static ptrdiff_t debug_delta, debug_delta_bytes;
12577
12578 /* Values of window_end_pos and window_end_vpos at the end of
12579 try_window_id. */
12580
12581 static ptrdiff_t debug_end_vpos;
12582
12583 /* Append a string to W->desired_matrix->method. FMT is a printf
12584 format string. If trace_redisplay_p is non-zero also printf the
12585 resulting string to stderr. */
12586
12587 static void debug_method_add (struct window *, char const *, ...)
12588 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12589
12590 static void
12591 debug_method_add (struct window *w, char const *fmt, ...)
12592 {
12593 void *ptr = w;
12594 char *method = w->desired_matrix->method;
12595 int len = strlen (method);
12596 int size = sizeof w->desired_matrix->method;
12597 int remaining = size - len - 1;
12598 va_list ap;
12599
12600 if (len && remaining)
12601 {
12602 method[len] = '|';
12603 --remaining, ++len;
12604 }
12605
12606 va_start (ap, fmt);
12607 vsnprintf (method + len, remaining + 1, fmt, ap);
12608 va_end (ap);
12609
12610 if (trace_redisplay_p)
12611 fprintf (stderr, "%p (%s): %s\n",
12612 ptr,
12613 ((BUFFERP (w->contents)
12614 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12615 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12616 : "no buffer"),
12617 method + len);
12618 }
12619
12620 #endif /* GLYPH_DEBUG */
12621
12622
12623 /* Value is non-zero if all changes in window W, which displays
12624 current_buffer, are in the text between START and END. START is a
12625 buffer position, END is given as a distance from Z. Used in
12626 redisplay_internal for display optimization. */
12627
12628 static int
12629 text_outside_line_unchanged_p (struct window *w,
12630 ptrdiff_t start, ptrdiff_t end)
12631 {
12632 int unchanged_p = 1;
12633
12634 /* If text or overlays have changed, see where. */
12635 if (window_outdated (w))
12636 {
12637 /* Gap in the line? */
12638 if (GPT < start || Z - GPT < end)
12639 unchanged_p = 0;
12640
12641 /* Changes start in front of the line, or end after it? */
12642 if (unchanged_p
12643 && (BEG_UNCHANGED < start - 1
12644 || END_UNCHANGED < end))
12645 unchanged_p = 0;
12646
12647 /* If selective display, can't optimize if changes start at the
12648 beginning of the line. */
12649 if (unchanged_p
12650 && INTEGERP (BVAR (current_buffer, selective_display))
12651 && XINT (BVAR (current_buffer, selective_display)) > 0
12652 && (BEG_UNCHANGED < start || GPT <= start))
12653 unchanged_p = 0;
12654
12655 /* If there are overlays at the start or end of the line, these
12656 may have overlay strings with newlines in them. A change at
12657 START, for instance, may actually concern the display of such
12658 overlay strings as well, and they are displayed on different
12659 lines. So, quickly rule out this case. (For the future, it
12660 might be desirable to implement something more telling than
12661 just BEG/END_UNCHANGED.) */
12662 if (unchanged_p)
12663 {
12664 if (BEG + BEG_UNCHANGED == start
12665 && overlay_touches_p (start))
12666 unchanged_p = 0;
12667 if (END_UNCHANGED == end
12668 && overlay_touches_p (Z - end))
12669 unchanged_p = 0;
12670 }
12671
12672 /* Under bidi reordering, adding or deleting a character in the
12673 beginning of a paragraph, before the first strong directional
12674 character, can change the base direction of the paragraph (unless
12675 the buffer specifies a fixed paragraph direction), which will
12676 require to redisplay the whole paragraph. It might be worthwhile
12677 to find the paragraph limits and widen the range of redisplayed
12678 lines to that, but for now just give up this optimization. */
12679 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12680 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12681 unchanged_p = 0;
12682 }
12683
12684 return unchanged_p;
12685 }
12686
12687
12688 /* Do a frame update, taking possible shortcuts into account. This is
12689 the main external entry point for redisplay.
12690
12691 If the last redisplay displayed an echo area message and that message
12692 is no longer requested, we clear the echo area or bring back the
12693 mini-buffer if that is in use. */
12694
12695 void
12696 redisplay (void)
12697 {
12698 redisplay_internal ();
12699 }
12700
12701
12702 static Lisp_Object
12703 overlay_arrow_string_or_property (Lisp_Object var)
12704 {
12705 Lisp_Object val;
12706
12707 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12708 return val;
12709
12710 return Voverlay_arrow_string;
12711 }
12712
12713 /* Return 1 if there are any overlay-arrows in current_buffer. */
12714 static int
12715 overlay_arrow_in_current_buffer_p (void)
12716 {
12717 Lisp_Object vlist;
12718
12719 for (vlist = Voverlay_arrow_variable_list;
12720 CONSP (vlist);
12721 vlist = XCDR (vlist))
12722 {
12723 Lisp_Object var = XCAR (vlist);
12724 Lisp_Object val;
12725
12726 if (!SYMBOLP (var))
12727 continue;
12728 val = find_symbol_value (var);
12729 if (MARKERP (val)
12730 && current_buffer == XMARKER (val)->buffer)
12731 return 1;
12732 }
12733 return 0;
12734 }
12735
12736
12737 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12738 has changed. */
12739
12740 static int
12741 overlay_arrows_changed_p (void)
12742 {
12743 Lisp_Object vlist;
12744
12745 for (vlist = Voverlay_arrow_variable_list;
12746 CONSP (vlist);
12747 vlist = XCDR (vlist))
12748 {
12749 Lisp_Object var = XCAR (vlist);
12750 Lisp_Object val, pstr;
12751
12752 if (!SYMBOLP (var))
12753 continue;
12754 val = find_symbol_value (var);
12755 if (!MARKERP (val))
12756 continue;
12757 if (! EQ (COERCE_MARKER (val),
12758 Fget (var, Qlast_arrow_position))
12759 || ! (pstr = overlay_arrow_string_or_property (var),
12760 EQ (pstr, Fget (var, Qlast_arrow_string))))
12761 return 1;
12762 }
12763 return 0;
12764 }
12765
12766 /* Mark overlay arrows to be updated on next redisplay. */
12767
12768 static void
12769 update_overlay_arrows (int up_to_date)
12770 {
12771 Lisp_Object vlist;
12772
12773 for (vlist = Voverlay_arrow_variable_list;
12774 CONSP (vlist);
12775 vlist = XCDR (vlist))
12776 {
12777 Lisp_Object var = XCAR (vlist);
12778
12779 if (!SYMBOLP (var))
12780 continue;
12781
12782 if (up_to_date > 0)
12783 {
12784 Lisp_Object val = find_symbol_value (var);
12785 Fput (var, Qlast_arrow_position,
12786 COERCE_MARKER (val));
12787 Fput (var, Qlast_arrow_string,
12788 overlay_arrow_string_or_property (var));
12789 }
12790 else if (up_to_date < 0
12791 || !NILP (Fget (var, Qlast_arrow_position)))
12792 {
12793 Fput (var, Qlast_arrow_position, Qt);
12794 Fput (var, Qlast_arrow_string, Qt);
12795 }
12796 }
12797 }
12798
12799
12800 /* Return overlay arrow string to display at row.
12801 Return integer (bitmap number) for arrow bitmap in left fringe.
12802 Return nil if no overlay arrow. */
12803
12804 static Lisp_Object
12805 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12806 {
12807 Lisp_Object vlist;
12808
12809 for (vlist = Voverlay_arrow_variable_list;
12810 CONSP (vlist);
12811 vlist = XCDR (vlist))
12812 {
12813 Lisp_Object var = XCAR (vlist);
12814 Lisp_Object val;
12815
12816 if (!SYMBOLP (var))
12817 continue;
12818
12819 val = find_symbol_value (var);
12820
12821 if (MARKERP (val)
12822 && current_buffer == XMARKER (val)->buffer
12823 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12824 {
12825 if (FRAME_WINDOW_P (it->f)
12826 /* FIXME: if ROW->reversed_p is set, this should test
12827 the right fringe, not the left one. */
12828 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12829 {
12830 #ifdef HAVE_WINDOW_SYSTEM
12831 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12832 {
12833 int fringe_bitmap;
12834 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12835 return make_number (fringe_bitmap);
12836 }
12837 #endif
12838 return make_number (-1); /* Use default arrow bitmap. */
12839 }
12840 return overlay_arrow_string_or_property (var);
12841 }
12842 }
12843
12844 return Qnil;
12845 }
12846
12847 /* Return 1 if point moved out of or into a composition. Otherwise
12848 return 0. PREV_BUF and PREV_PT are the last point buffer and
12849 position. BUF and PT are the current point buffer and position. */
12850
12851 static int
12852 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12853 struct buffer *buf, ptrdiff_t pt)
12854 {
12855 ptrdiff_t start, end;
12856 Lisp_Object prop;
12857 Lisp_Object buffer;
12858
12859 XSETBUFFER (buffer, buf);
12860 /* Check a composition at the last point if point moved within the
12861 same buffer. */
12862 if (prev_buf == buf)
12863 {
12864 if (prev_pt == pt)
12865 /* Point didn't move. */
12866 return 0;
12867
12868 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12869 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12870 && composition_valid_p (start, end, prop)
12871 && start < prev_pt && end > prev_pt)
12872 /* The last point was within the composition. Return 1 iff
12873 point moved out of the composition. */
12874 return (pt <= start || pt >= end);
12875 }
12876
12877 /* Check a composition at the current point. */
12878 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12879 && find_composition (pt, -1, &start, &end, &prop, buffer)
12880 && composition_valid_p (start, end, prop)
12881 && start < pt && end > pt);
12882 }
12883
12884 /* Reconsider the clip changes of buffer which is displayed in W. */
12885
12886 static void
12887 reconsider_clip_changes (struct window *w)
12888 {
12889 struct buffer *b = XBUFFER (w->contents);
12890
12891 if (b->clip_changed
12892 && w->window_end_valid
12893 && w->current_matrix->buffer == b
12894 && w->current_matrix->zv == BUF_ZV (b)
12895 && w->current_matrix->begv == BUF_BEGV (b))
12896 b->clip_changed = 0;
12897
12898 /* If display wasn't paused, and W is not a tool bar window, see if
12899 point has been moved into or out of a composition. In that case,
12900 we set b->clip_changed to 1 to force updating the screen. If
12901 b->clip_changed has already been set to 1, we can skip this
12902 check. */
12903 if (!b->clip_changed && w->window_end_valid)
12904 {
12905 ptrdiff_t pt = (w == XWINDOW (selected_window)
12906 ? PT : marker_position (w->pointm));
12907
12908 if ((w->current_matrix->buffer != b || pt != w->last_point)
12909 && check_point_in_composition (w->current_matrix->buffer,
12910 w->last_point, b, pt))
12911 b->clip_changed = 1;
12912 }
12913 }
12914
12915 #define STOP_POLLING \
12916 do { if (! polling_stopped_here) stop_polling (); \
12917 polling_stopped_here = 1; } while (0)
12918
12919 #define RESUME_POLLING \
12920 do { if (polling_stopped_here) start_polling (); \
12921 polling_stopped_here = 0; } while (0)
12922
12923
12924 /* Perhaps in the future avoid recentering windows if it
12925 is not necessary; currently that causes some problems. */
12926
12927 static void
12928 redisplay_internal (void)
12929 {
12930 struct window *w = XWINDOW (selected_window);
12931 struct window *sw;
12932 struct frame *fr;
12933 int pending;
12934 bool must_finish = 0, match_p;
12935 struct text_pos tlbufpos, tlendpos;
12936 int number_of_visible_frames;
12937 ptrdiff_t count;
12938 struct frame *sf;
12939 int polling_stopped_here = 0;
12940 Lisp_Object tail, frame;
12941
12942 /* Non-zero means redisplay has to consider all windows on all
12943 frames. Zero means, only selected_window is considered. */
12944 int consider_all_windows_p;
12945
12946 /* Non-zero means redisplay has to redisplay the miniwindow. */
12947 int update_miniwindow_p = 0;
12948
12949 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12950
12951 /* No redisplay if running in batch mode or frame is not yet fully
12952 initialized, or redisplay is explicitly turned off by setting
12953 Vinhibit_redisplay. */
12954 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12955 || !NILP (Vinhibit_redisplay))
12956 return;
12957
12958 /* Don't examine these until after testing Vinhibit_redisplay.
12959 When Emacs is shutting down, perhaps because its connection to
12960 X has dropped, we should not look at them at all. */
12961 fr = XFRAME (w->frame);
12962 sf = SELECTED_FRAME ();
12963
12964 if (!fr->glyphs_initialized_p)
12965 return;
12966
12967 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12968 if (popup_activated ())
12969 return;
12970 #endif
12971
12972 /* I don't think this happens but let's be paranoid. */
12973 if (redisplaying_p)
12974 return;
12975
12976 /* Record a function that clears redisplaying_p
12977 when we leave this function. */
12978 count = SPECPDL_INDEX ();
12979 record_unwind_protect_void (unwind_redisplay);
12980 redisplaying_p = 1;
12981 specbind (Qinhibit_free_realized_faces, Qnil);
12982
12983 /* Record this function, so it appears on the profiler's backtraces. */
12984 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12985
12986 FOR_EACH_FRAME (tail, frame)
12987 XFRAME (frame)->already_hscrolled_p = 0;
12988
12989 retry:
12990 /* Remember the currently selected window. */
12991 sw = w;
12992
12993 pending = 0;
12994 last_escape_glyph_frame = NULL;
12995 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12996 last_glyphless_glyph_frame = NULL;
12997 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12998
12999 /* If face_change_count is non-zero, init_iterator will free all
13000 realized faces, which includes the faces referenced from current
13001 matrices. So, we can't reuse current matrices in this case. */
13002 if (face_change_count)
13003 ++windows_or_buffers_changed;
13004
13005 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13006 && FRAME_TTY (sf)->previous_frame != sf)
13007 {
13008 /* Since frames on a single ASCII terminal share the same
13009 display area, displaying a different frame means redisplay
13010 the whole thing. */
13011 windows_or_buffers_changed++;
13012 SET_FRAME_GARBAGED (sf);
13013 #ifndef DOS_NT
13014 set_tty_color_mode (FRAME_TTY (sf), sf);
13015 #endif
13016 FRAME_TTY (sf)->previous_frame = sf;
13017 }
13018
13019 /* Set the visible flags for all frames. Do this before checking for
13020 resized or garbaged frames; they want to know if their frames are
13021 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13022 number_of_visible_frames = 0;
13023
13024 FOR_EACH_FRAME (tail, frame)
13025 {
13026 struct frame *f = XFRAME (frame);
13027
13028 if (FRAME_VISIBLE_P (f))
13029 {
13030 ++number_of_visible_frames;
13031 /* Adjust matrices for visible frames only. */
13032 if (f->fonts_changed)
13033 {
13034 adjust_frame_glyphs (f);
13035 f->fonts_changed = 0;
13036 }
13037 /* If cursor type has been changed on the frame
13038 other than selected, consider all frames. */
13039 if (f != sf && f->cursor_type_changed)
13040 update_mode_lines++;
13041 }
13042 clear_desired_matrices (f);
13043 }
13044
13045 /* Notice any pending interrupt request to change frame size. */
13046 do_pending_window_change (1);
13047
13048 /* do_pending_window_change could change the selected_window due to
13049 frame resizing which makes the selected window too small. */
13050 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13051 sw = w;
13052
13053 /* Clear frames marked as garbaged. */
13054 clear_garbaged_frames ();
13055
13056 /* Build menubar and tool-bar items. */
13057 if (NILP (Vmemory_full))
13058 prepare_menu_bars ();
13059
13060 if (windows_or_buffers_changed)
13061 update_mode_lines++;
13062
13063 reconsider_clip_changes (w);
13064
13065 /* In most cases selected window displays current buffer. */
13066 match_p = XBUFFER (w->contents) == current_buffer;
13067 if (match_p)
13068 {
13069 ptrdiff_t count1;
13070
13071 /* Detect case that we need to write or remove a star in the mode line. */
13072 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13073 {
13074 w->update_mode_line = 1;
13075 if (buffer_shared_and_changed ())
13076 update_mode_lines++;
13077 }
13078
13079 /* Avoid invocation of point motion hooks by `current_column' below. */
13080 count1 = SPECPDL_INDEX ();
13081 specbind (Qinhibit_point_motion_hooks, Qt);
13082
13083 if (mode_line_update_needed (w))
13084 w->update_mode_line = 1;
13085
13086 unbind_to (count1, Qnil);
13087 }
13088
13089 consider_all_windows_p = (update_mode_lines
13090 || buffer_shared_and_changed ());
13091
13092 /* If specs for an arrow have changed, do thorough redisplay
13093 to ensure we remove any arrow that should no longer exist. */
13094 if (overlay_arrows_changed_p ())
13095 consider_all_windows_p = windows_or_buffers_changed = 1;
13096
13097 /* Normally the message* functions will have already displayed and
13098 updated the echo area, but the frame may have been trashed, or
13099 the update may have been preempted, so display the echo area
13100 again here. Checking message_cleared_p captures the case that
13101 the echo area should be cleared. */
13102 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13103 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13104 || (message_cleared_p
13105 && minibuf_level == 0
13106 /* If the mini-window is currently selected, this means the
13107 echo-area doesn't show through. */
13108 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13109 {
13110 int window_height_changed_p = echo_area_display (0);
13111
13112 if (message_cleared_p)
13113 update_miniwindow_p = 1;
13114
13115 must_finish = 1;
13116
13117 /* If we don't display the current message, don't clear the
13118 message_cleared_p flag, because, if we did, we wouldn't clear
13119 the echo area in the next redisplay which doesn't preserve
13120 the echo area. */
13121 if (!display_last_displayed_message_p)
13122 message_cleared_p = 0;
13123
13124 if (window_height_changed_p)
13125 {
13126 consider_all_windows_p = 1;
13127 ++update_mode_lines;
13128 ++windows_or_buffers_changed;
13129
13130 /* If window configuration was changed, frames may have been
13131 marked garbaged. Clear them or we will experience
13132 surprises wrt scrolling. */
13133 clear_garbaged_frames ();
13134 }
13135 }
13136 else if (EQ (selected_window, minibuf_window)
13137 && (current_buffer->clip_changed || window_outdated (w))
13138 && resize_mini_window (w, 0))
13139 {
13140 /* Resized active mini-window to fit the size of what it is
13141 showing if its contents might have changed. */
13142 must_finish = 1;
13143 /* FIXME: this causes all frames to be updated, which seems unnecessary
13144 since only the current frame needs to be considered. This function
13145 needs to be rewritten with two variables, consider_all_windows and
13146 consider_all_frames. */
13147 consider_all_windows_p = 1;
13148 ++windows_or_buffers_changed;
13149 ++update_mode_lines;
13150
13151 /* If window configuration was changed, frames may have been
13152 marked garbaged. Clear them or we will experience
13153 surprises wrt scrolling. */
13154 clear_garbaged_frames ();
13155 }
13156
13157 /* If showing the region, and mark has changed, we must redisplay
13158 the whole window. The assignment to this_line_start_pos prevents
13159 the optimization directly below this if-statement. */
13160 if (((!NILP (Vtransient_mark_mode)
13161 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13162 != (w->region_showing > 0))
13163 || (w->region_showing
13164 && w->region_showing
13165 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13166 CHARPOS (this_line_start_pos) = 0;
13167
13168 /* Optimize the case that only the line containing the cursor in the
13169 selected window has changed. Variables starting with this_ are
13170 set in display_line and record information about the line
13171 containing the cursor. */
13172 tlbufpos = this_line_start_pos;
13173 tlendpos = this_line_end_pos;
13174 if (!consider_all_windows_p
13175 && CHARPOS (tlbufpos) > 0
13176 && !w->update_mode_line
13177 && !current_buffer->clip_changed
13178 && !current_buffer->prevent_redisplay_optimizations_p
13179 && FRAME_VISIBLE_P (XFRAME (w->frame))
13180 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13181 && !XFRAME (w->frame)->cursor_type_changed
13182 /* Make sure recorded data applies to current buffer, etc. */
13183 && this_line_buffer == current_buffer
13184 && match_p
13185 && !w->force_start
13186 && !w->optional_new_start
13187 /* Point must be on the line that we have info recorded about. */
13188 && PT >= CHARPOS (tlbufpos)
13189 && PT <= Z - CHARPOS (tlendpos)
13190 /* All text outside that line, including its final newline,
13191 must be unchanged. */
13192 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13193 CHARPOS (tlendpos)))
13194 {
13195 if (CHARPOS (tlbufpos) > BEGV
13196 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13197 && (CHARPOS (tlbufpos) == ZV
13198 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13199 /* Former continuation line has disappeared by becoming empty. */
13200 goto cancel;
13201 else if (window_outdated (w) || MINI_WINDOW_P (w))
13202 {
13203 /* We have to handle the case of continuation around a
13204 wide-column character (see the comment in indent.c around
13205 line 1340).
13206
13207 For instance, in the following case:
13208
13209 -------- Insert --------
13210 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13211 J_I_ ==> J_I_ `^^' are cursors.
13212 ^^ ^^
13213 -------- --------
13214
13215 As we have to redraw the line above, we cannot use this
13216 optimization. */
13217
13218 struct it it;
13219 int line_height_before = this_line_pixel_height;
13220
13221 /* Note that start_display will handle the case that the
13222 line starting at tlbufpos is a continuation line. */
13223 start_display (&it, w, tlbufpos);
13224
13225 /* Implementation note: It this still necessary? */
13226 if (it.current_x != this_line_start_x)
13227 goto cancel;
13228
13229 TRACE ((stderr, "trying display optimization 1\n"));
13230 w->cursor.vpos = -1;
13231 overlay_arrow_seen = 0;
13232 it.vpos = this_line_vpos;
13233 it.current_y = this_line_y;
13234 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13235 display_line (&it);
13236
13237 /* If line contains point, is not continued,
13238 and ends at same distance from eob as before, we win. */
13239 if (w->cursor.vpos >= 0
13240 /* Line is not continued, otherwise this_line_start_pos
13241 would have been set to 0 in display_line. */
13242 && CHARPOS (this_line_start_pos)
13243 /* Line ends as before. */
13244 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13245 /* Line has same height as before. Otherwise other lines
13246 would have to be shifted up or down. */
13247 && this_line_pixel_height == line_height_before)
13248 {
13249 /* If this is not the window's last line, we must adjust
13250 the charstarts of the lines below. */
13251 if (it.current_y < it.last_visible_y)
13252 {
13253 struct glyph_row *row
13254 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13255 ptrdiff_t delta, delta_bytes;
13256
13257 /* We used to distinguish between two cases here,
13258 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13259 when the line ends in a newline or the end of the
13260 buffer's accessible portion. But both cases did
13261 the same, so they were collapsed. */
13262 delta = (Z
13263 - CHARPOS (tlendpos)
13264 - MATRIX_ROW_START_CHARPOS (row));
13265 delta_bytes = (Z_BYTE
13266 - BYTEPOS (tlendpos)
13267 - MATRIX_ROW_START_BYTEPOS (row));
13268
13269 increment_matrix_positions (w->current_matrix,
13270 this_line_vpos + 1,
13271 w->current_matrix->nrows,
13272 delta, delta_bytes);
13273 }
13274
13275 /* If this row displays text now but previously didn't,
13276 or vice versa, w->window_end_vpos may have to be
13277 adjusted. */
13278 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13279 {
13280 if (w->window_end_vpos < this_line_vpos)
13281 w->window_end_vpos = this_line_vpos;
13282 }
13283 else if (w->window_end_vpos == this_line_vpos
13284 && this_line_vpos > 0)
13285 w->window_end_vpos = this_line_vpos - 1;
13286 w->window_end_valid = 0;
13287
13288 /* Update hint: No need to try to scroll in update_window. */
13289 w->desired_matrix->no_scrolling_p = 1;
13290
13291 #ifdef GLYPH_DEBUG
13292 *w->desired_matrix->method = 0;
13293 debug_method_add (w, "optimization 1");
13294 #endif
13295 #ifdef HAVE_WINDOW_SYSTEM
13296 update_window_fringes (w, 0);
13297 #endif
13298 goto update;
13299 }
13300 else
13301 goto cancel;
13302 }
13303 else if (/* Cursor position hasn't changed. */
13304 PT == w->last_point
13305 /* Make sure the cursor was last displayed
13306 in this window. Otherwise we have to reposition it. */
13307 && 0 <= w->cursor.vpos
13308 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13309 {
13310 if (!must_finish)
13311 {
13312 do_pending_window_change (1);
13313 /* If selected_window changed, redisplay again. */
13314 if (WINDOWP (selected_window)
13315 && (w = XWINDOW (selected_window)) != sw)
13316 goto retry;
13317
13318 /* We used to always goto end_of_redisplay here, but this
13319 isn't enough if we have a blinking cursor. */
13320 if (w->cursor_off_p == w->last_cursor_off_p)
13321 goto end_of_redisplay;
13322 }
13323 goto update;
13324 }
13325 /* If highlighting the region, or if the cursor is in the echo area,
13326 then we can't just move the cursor. */
13327 else if (! (!NILP (Vtransient_mark_mode)
13328 && !NILP (BVAR (current_buffer, mark_active)))
13329 && (EQ (selected_window,
13330 BVAR (current_buffer, last_selected_window))
13331 || highlight_nonselected_windows)
13332 && !w->region_showing
13333 && NILP (Vshow_trailing_whitespace)
13334 && !cursor_in_echo_area)
13335 {
13336 struct it it;
13337 struct glyph_row *row;
13338
13339 /* Skip from tlbufpos to PT and see where it is. Note that
13340 PT may be in invisible text. If so, we will end at the
13341 next visible position. */
13342 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13343 NULL, DEFAULT_FACE_ID);
13344 it.current_x = this_line_start_x;
13345 it.current_y = this_line_y;
13346 it.vpos = this_line_vpos;
13347
13348 /* The call to move_it_to stops in front of PT, but
13349 moves over before-strings. */
13350 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13351
13352 if (it.vpos == this_line_vpos
13353 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13354 row->enabled_p))
13355 {
13356 eassert (this_line_vpos == it.vpos);
13357 eassert (this_line_y == it.current_y);
13358 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13359 #ifdef GLYPH_DEBUG
13360 *w->desired_matrix->method = 0;
13361 debug_method_add (w, "optimization 3");
13362 #endif
13363 goto update;
13364 }
13365 else
13366 goto cancel;
13367 }
13368
13369 cancel:
13370 /* Text changed drastically or point moved off of line. */
13371 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13372 }
13373
13374 CHARPOS (this_line_start_pos) = 0;
13375 consider_all_windows_p |= buffer_shared_and_changed ();
13376 ++clear_face_cache_count;
13377 #ifdef HAVE_WINDOW_SYSTEM
13378 ++clear_image_cache_count;
13379 #endif
13380
13381 /* Build desired matrices, and update the display. If
13382 consider_all_windows_p is non-zero, do it for all windows on all
13383 frames. Otherwise do it for selected_window, only. */
13384
13385 if (consider_all_windows_p)
13386 {
13387 FOR_EACH_FRAME (tail, frame)
13388 XFRAME (frame)->updated_p = 0;
13389
13390 FOR_EACH_FRAME (tail, frame)
13391 {
13392 struct frame *f = XFRAME (frame);
13393
13394 /* We don't have to do anything for unselected terminal
13395 frames. */
13396 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13397 && !EQ (FRAME_TTY (f)->top_frame, frame))
13398 continue;
13399
13400 retry_frame:
13401
13402 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13403 {
13404 /* Mark all the scroll bars to be removed; we'll redeem
13405 the ones we want when we redisplay their windows. */
13406 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13407 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13408
13409 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13410 redisplay_windows (FRAME_ROOT_WINDOW (f));
13411
13412 /* The X error handler may have deleted that frame. */
13413 if (!FRAME_LIVE_P (f))
13414 continue;
13415
13416 /* Any scroll bars which redisplay_windows should have
13417 nuked should now go away. */
13418 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13419 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13420
13421 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13422 {
13423 /* If fonts changed on visible frame, display again. */
13424 if (f->fonts_changed)
13425 {
13426 adjust_frame_glyphs (f);
13427 f->fonts_changed = 0;
13428 goto retry_frame;
13429 }
13430
13431 /* See if we have to hscroll. */
13432 if (!f->already_hscrolled_p)
13433 {
13434 f->already_hscrolled_p = 1;
13435 if (hscroll_windows (f->root_window))
13436 goto retry_frame;
13437 }
13438
13439 /* Prevent various kinds of signals during display
13440 update. stdio is not robust about handling
13441 signals, which can cause an apparent I/O
13442 error. */
13443 if (interrupt_input)
13444 unrequest_sigio ();
13445 STOP_POLLING;
13446
13447 /* Update the display. */
13448 set_window_update_flags (XWINDOW (f->root_window), 1);
13449 pending |= update_frame (f, 0, 0);
13450 f->cursor_type_changed = 0;
13451 f->updated_p = 1;
13452 }
13453 }
13454 }
13455
13456 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13457
13458 if (!pending)
13459 {
13460 /* Do the mark_window_display_accurate after all windows have
13461 been redisplayed because this call resets flags in buffers
13462 which are needed for proper redisplay. */
13463 FOR_EACH_FRAME (tail, frame)
13464 {
13465 struct frame *f = XFRAME (frame);
13466 if (f->updated_p)
13467 {
13468 mark_window_display_accurate (f->root_window, 1);
13469 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13470 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13471 }
13472 }
13473 }
13474 }
13475 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13476 {
13477 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13478 struct frame *mini_frame;
13479
13480 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13481 /* Use list_of_error, not Qerror, so that
13482 we catch only errors and don't run the debugger. */
13483 internal_condition_case_1 (redisplay_window_1, selected_window,
13484 list_of_error,
13485 redisplay_window_error);
13486 if (update_miniwindow_p)
13487 internal_condition_case_1 (redisplay_window_1, mini_window,
13488 list_of_error,
13489 redisplay_window_error);
13490
13491 /* Compare desired and current matrices, perform output. */
13492
13493 update:
13494 /* If fonts changed, display again. */
13495 if (sf->fonts_changed)
13496 goto retry;
13497
13498 /* Prevent various kinds of signals during display update.
13499 stdio is not robust about handling signals,
13500 which can cause an apparent I/O error. */
13501 if (interrupt_input)
13502 unrequest_sigio ();
13503 STOP_POLLING;
13504
13505 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13506 {
13507 if (hscroll_windows (selected_window))
13508 goto retry;
13509
13510 XWINDOW (selected_window)->must_be_updated_p = 1;
13511 pending = update_frame (sf, 0, 0);
13512 sf->cursor_type_changed = 0;
13513 }
13514
13515 /* We may have called echo_area_display at the top of this
13516 function. If the echo area is on another frame, that may
13517 have put text on a frame other than the selected one, so the
13518 above call to update_frame would not have caught it. Catch
13519 it here. */
13520 mini_window = FRAME_MINIBUF_WINDOW (sf);
13521 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13522
13523 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13524 {
13525 XWINDOW (mini_window)->must_be_updated_p = 1;
13526 pending |= update_frame (mini_frame, 0, 0);
13527 mini_frame->cursor_type_changed = 0;
13528 if (!pending && hscroll_windows (mini_window))
13529 goto retry;
13530 }
13531 }
13532
13533 /* If display was paused because of pending input, make sure we do a
13534 thorough update the next time. */
13535 if (pending)
13536 {
13537 /* Prevent the optimization at the beginning of
13538 redisplay_internal that tries a single-line update of the
13539 line containing the cursor in the selected window. */
13540 CHARPOS (this_line_start_pos) = 0;
13541
13542 /* Let the overlay arrow be updated the next time. */
13543 update_overlay_arrows (0);
13544
13545 /* If we pause after scrolling, some rows in the current
13546 matrices of some windows are not valid. */
13547 if (!WINDOW_FULL_WIDTH_P (w)
13548 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13549 update_mode_lines = 1;
13550 }
13551 else
13552 {
13553 if (!consider_all_windows_p)
13554 {
13555 /* This has already been done above if
13556 consider_all_windows_p is set. */
13557 mark_window_display_accurate_1 (w, 1);
13558
13559 /* Say overlay arrows are up to date. */
13560 update_overlay_arrows (1);
13561
13562 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13563 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13564 }
13565
13566 update_mode_lines = 0;
13567 windows_or_buffers_changed = 0;
13568 }
13569
13570 /* Start SIGIO interrupts coming again. Having them off during the
13571 code above makes it less likely one will discard output, but not
13572 impossible, since there might be stuff in the system buffer here.
13573 But it is much hairier to try to do anything about that. */
13574 if (interrupt_input)
13575 request_sigio ();
13576 RESUME_POLLING;
13577
13578 /* If a frame has become visible which was not before, redisplay
13579 again, so that we display it. Expose events for such a frame
13580 (which it gets when becoming visible) don't call the parts of
13581 redisplay constructing glyphs, so simply exposing a frame won't
13582 display anything in this case. So, we have to display these
13583 frames here explicitly. */
13584 if (!pending)
13585 {
13586 int new_count = 0;
13587
13588 FOR_EACH_FRAME (tail, frame)
13589 {
13590 int this_is_visible = 0;
13591
13592 if (XFRAME (frame)->visible)
13593 this_is_visible = 1;
13594
13595 if (this_is_visible)
13596 new_count++;
13597 }
13598
13599 if (new_count != number_of_visible_frames)
13600 windows_or_buffers_changed++;
13601 }
13602
13603 /* Change frame size now if a change is pending. */
13604 do_pending_window_change (1);
13605
13606 /* If we just did a pending size change, or have additional
13607 visible frames, or selected_window changed, redisplay again. */
13608 if ((windows_or_buffers_changed && !pending)
13609 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13610 goto retry;
13611
13612 /* Clear the face and image caches.
13613
13614 We used to do this only if consider_all_windows_p. But the cache
13615 needs to be cleared if a timer creates images in the current
13616 buffer (e.g. the test case in Bug#6230). */
13617
13618 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13619 {
13620 clear_face_cache (0);
13621 clear_face_cache_count = 0;
13622 }
13623
13624 #ifdef HAVE_WINDOW_SYSTEM
13625 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13626 {
13627 clear_image_caches (Qnil);
13628 clear_image_cache_count = 0;
13629 }
13630 #endif /* HAVE_WINDOW_SYSTEM */
13631
13632 end_of_redisplay:
13633 unbind_to (count, Qnil);
13634 RESUME_POLLING;
13635 }
13636
13637
13638 /* Redisplay, but leave alone any recent echo area message unless
13639 another message has been requested in its place.
13640
13641 This is useful in situations where you need to redisplay but no
13642 user action has occurred, making it inappropriate for the message
13643 area to be cleared. See tracking_off and
13644 wait_reading_process_output for examples of these situations.
13645
13646 FROM_WHERE is an integer saying from where this function was
13647 called. This is useful for debugging. */
13648
13649 void
13650 redisplay_preserve_echo_area (int from_where)
13651 {
13652 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13653
13654 if (!NILP (echo_area_buffer[1]))
13655 {
13656 /* We have a previously displayed message, but no current
13657 message. Redisplay the previous message. */
13658 display_last_displayed_message_p = 1;
13659 redisplay_internal ();
13660 display_last_displayed_message_p = 0;
13661 }
13662 else
13663 redisplay_internal ();
13664
13665 flush_frame (SELECTED_FRAME ());
13666 }
13667
13668
13669 /* Function registered with record_unwind_protect in redisplay_internal. */
13670
13671 static void
13672 unwind_redisplay (void)
13673 {
13674 redisplaying_p = 0;
13675 }
13676
13677
13678 /* Mark the display of leaf window W as accurate or inaccurate.
13679 If ACCURATE_P is non-zero mark display of W as accurate. If
13680 ACCURATE_P is zero, arrange for W to be redisplayed the next
13681 time redisplay_internal is called. */
13682
13683 static void
13684 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13685 {
13686 struct buffer *b = XBUFFER (w->contents);
13687
13688 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13689 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13690 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13691
13692 if (accurate_p)
13693 {
13694 b->clip_changed = 0;
13695 b->prevent_redisplay_optimizations_p = 0;
13696
13697 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13698 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13699 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13700 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13701
13702 w->current_matrix->buffer = b;
13703 w->current_matrix->begv = BUF_BEGV (b);
13704 w->current_matrix->zv = BUF_ZV (b);
13705
13706 w->last_cursor_vpos = w->cursor.vpos;
13707 w->last_cursor_off_p = w->cursor_off_p;
13708
13709 if (w == XWINDOW (selected_window))
13710 w->last_point = BUF_PT (b);
13711 else
13712 w->last_point = marker_position (w->pointm);
13713
13714 w->window_end_valid = 1;
13715 w->update_mode_line = 0;
13716 }
13717 }
13718
13719
13720 /* Mark the display of windows in the window tree rooted at WINDOW as
13721 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13722 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13723 be redisplayed the next time redisplay_internal is called. */
13724
13725 void
13726 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13727 {
13728 struct window *w;
13729
13730 for (; !NILP (window); window = w->next)
13731 {
13732 w = XWINDOW (window);
13733 if (WINDOWP (w->contents))
13734 mark_window_display_accurate (w->contents, accurate_p);
13735 else
13736 mark_window_display_accurate_1 (w, accurate_p);
13737 }
13738
13739 if (accurate_p)
13740 update_overlay_arrows (1);
13741 else
13742 /* Force a thorough redisplay the next time by setting
13743 last_arrow_position and last_arrow_string to t, which is
13744 unequal to any useful value of Voverlay_arrow_... */
13745 update_overlay_arrows (-1);
13746 }
13747
13748
13749 /* Return value in display table DP (Lisp_Char_Table *) for character
13750 C. Since a display table doesn't have any parent, we don't have to
13751 follow parent. Do not call this function directly but use the
13752 macro DISP_CHAR_VECTOR. */
13753
13754 Lisp_Object
13755 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13756 {
13757 Lisp_Object val;
13758
13759 if (ASCII_CHAR_P (c))
13760 {
13761 val = dp->ascii;
13762 if (SUB_CHAR_TABLE_P (val))
13763 val = XSUB_CHAR_TABLE (val)->contents[c];
13764 }
13765 else
13766 {
13767 Lisp_Object table;
13768
13769 XSETCHAR_TABLE (table, dp);
13770 val = char_table_ref (table, c);
13771 }
13772 if (NILP (val))
13773 val = dp->defalt;
13774 return val;
13775 }
13776
13777
13778 \f
13779 /***********************************************************************
13780 Window Redisplay
13781 ***********************************************************************/
13782
13783 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13784
13785 static void
13786 redisplay_windows (Lisp_Object window)
13787 {
13788 while (!NILP (window))
13789 {
13790 struct window *w = XWINDOW (window);
13791
13792 if (WINDOWP (w->contents))
13793 redisplay_windows (w->contents);
13794 else if (BUFFERP (w->contents))
13795 {
13796 displayed_buffer = XBUFFER (w->contents);
13797 /* Use list_of_error, not Qerror, so that
13798 we catch only errors and don't run the debugger. */
13799 internal_condition_case_1 (redisplay_window_0, window,
13800 list_of_error,
13801 redisplay_window_error);
13802 }
13803
13804 window = w->next;
13805 }
13806 }
13807
13808 static Lisp_Object
13809 redisplay_window_error (Lisp_Object ignore)
13810 {
13811 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13812 return Qnil;
13813 }
13814
13815 static Lisp_Object
13816 redisplay_window_0 (Lisp_Object window)
13817 {
13818 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13819 redisplay_window (window, 0);
13820 return Qnil;
13821 }
13822
13823 static Lisp_Object
13824 redisplay_window_1 (Lisp_Object window)
13825 {
13826 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13827 redisplay_window (window, 1);
13828 return Qnil;
13829 }
13830 \f
13831
13832 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13833 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13834 which positions recorded in ROW differ from current buffer
13835 positions.
13836
13837 Return 0 if cursor is not on this row, 1 otherwise. */
13838
13839 static int
13840 set_cursor_from_row (struct window *w, struct glyph_row *row,
13841 struct glyph_matrix *matrix,
13842 ptrdiff_t delta, ptrdiff_t delta_bytes,
13843 int dy, int dvpos)
13844 {
13845 struct glyph *glyph = row->glyphs[TEXT_AREA];
13846 struct glyph *end = glyph + row->used[TEXT_AREA];
13847 struct glyph *cursor = NULL;
13848 /* The last known character position in row. */
13849 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13850 int x = row->x;
13851 ptrdiff_t pt_old = PT - delta;
13852 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13853 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13854 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13855 /* A glyph beyond the edge of TEXT_AREA which we should never
13856 touch. */
13857 struct glyph *glyphs_end = end;
13858 /* Non-zero means we've found a match for cursor position, but that
13859 glyph has the avoid_cursor_p flag set. */
13860 int match_with_avoid_cursor = 0;
13861 /* Non-zero means we've seen at least one glyph that came from a
13862 display string. */
13863 int string_seen = 0;
13864 /* Largest and smallest buffer positions seen so far during scan of
13865 glyph row. */
13866 ptrdiff_t bpos_max = pos_before;
13867 ptrdiff_t bpos_min = pos_after;
13868 /* Last buffer position covered by an overlay string with an integer
13869 `cursor' property. */
13870 ptrdiff_t bpos_covered = 0;
13871 /* Non-zero means the display string on which to display the cursor
13872 comes from a text property, not from an overlay. */
13873 int string_from_text_prop = 0;
13874
13875 /* Don't even try doing anything if called for a mode-line or
13876 header-line row, since the rest of the code isn't prepared to
13877 deal with such calamities. */
13878 eassert (!row->mode_line_p);
13879 if (row->mode_line_p)
13880 return 0;
13881
13882 /* Skip over glyphs not having an object at the start and the end of
13883 the row. These are special glyphs like truncation marks on
13884 terminal frames. */
13885 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13886 {
13887 if (!row->reversed_p)
13888 {
13889 while (glyph < end
13890 && INTEGERP (glyph->object)
13891 && glyph->charpos < 0)
13892 {
13893 x += glyph->pixel_width;
13894 ++glyph;
13895 }
13896 while (end > glyph
13897 && INTEGERP ((end - 1)->object)
13898 /* CHARPOS is zero for blanks and stretch glyphs
13899 inserted by extend_face_to_end_of_line. */
13900 && (end - 1)->charpos <= 0)
13901 --end;
13902 glyph_before = glyph - 1;
13903 glyph_after = end;
13904 }
13905 else
13906 {
13907 struct glyph *g;
13908
13909 /* If the glyph row is reversed, we need to process it from back
13910 to front, so swap the edge pointers. */
13911 glyphs_end = end = glyph - 1;
13912 glyph += row->used[TEXT_AREA] - 1;
13913
13914 while (glyph > end + 1
13915 && INTEGERP (glyph->object)
13916 && glyph->charpos < 0)
13917 {
13918 --glyph;
13919 x -= glyph->pixel_width;
13920 }
13921 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13922 --glyph;
13923 /* By default, in reversed rows we put the cursor on the
13924 rightmost (first in the reading order) glyph. */
13925 for (g = end + 1; g < glyph; g++)
13926 x += g->pixel_width;
13927 while (end < glyph
13928 && INTEGERP ((end + 1)->object)
13929 && (end + 1)->charpos <= 0)
13930 ++end;
13931 glyph_before = glyph + 1;
13932 glyph_after = end;
13933 }
13934 }
13935 else if (row->reversed_p)
13936 {
13937 /* In R2L rows that don't display text, put the cursor on the
13938 rightmost glyph. Case in point: an empty last line that is
13939 part of an R2L paragraph. */
13940 cursor = end - 1;
13941 /* Avoid placing the cursor on the last glyph of the row, where
13942 on terminal frames we hold the vertical border between
13943 adjacent windows. */
13944 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13945 && !WINDOW_RIGHTMOST_P (w)
13946 && cursor == row->glyphs[LAST_AREA] - 1)
13947 cursor--;
13948 x = -1; /* will be computed below, at label compute_x */
13949 }
13950
13951 /* Step 1: Try to find the glyph whose character position
13952 corresponds to point. If that's not possible, find 2 glyphs
13953 whose character positions are the closest to point, one before
13954 point, the other after it. */
13955 if (!row->reversed_p)
13956 while (/* not marched to end of glyph row */
13957 glyph < end
13958 /* glyph was not inserted by redisplay for internal purposes */
13959 && !INTEGERP (glyph->object))
13960 {
13961 if (BUFFERP (glyph->object))
13962 {
13963 ptrdiff_t dpos = glyph->charpos - pt_old;
13964
13965 if (glyph->charpos > bpos_max)
13966 bpos_max = glyph->charpos;
13967 if (glyph->charpos < bpos_min)
13968 bpos_min = glyph->charpos;
13969 if (!glyph->avoid_cursor_p)
13970 {
13971 /* If we hit point, we've found the glyph on which to
13972 display the cursor. */
13973 if (dpos == 0)
13974 {
13975 match_with_avoid_cursor = 0;
13976 break;
13977 }
13978 /* See if we've found a better approximation to
13979 POS_BEFORE or to POS_AFTER. */
13980 if (0 > dpos && dpos > pos_before - pt_old)
13981 {
13982 pos_before = glyph->charpos;
13983 glyph_before = glyph;
13984 }
13985 else if (0 < dpos && dpos < pos_after - pt_old)
13986 {
13987 pos_after = glyph->charpos;
13988 glyph_after = glyph;
13989 }
13990 }
13991 else if (dpos == 0)
13992 match_with_avoid_cursor = 1;
13993 }
13994 else if (STRINGP (glyph->object))
13995 {
13996 Lisp_Object chprop;
13997 ptrdiff_t glyph_pos = glyph->charpos;
13998
13999 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14000 glyph->object);
14001 if (!NILP (chprop))
14002 {
14003 /* If the string came from a `display' text property,
14004 look up the buffer position of that property and
14005 use that position to update bpos_max, as if we
14006 actually saw such a position in one of the row's
14007 glyphs. This helps with supporting integer values
14008 of `cursor' property on the display string in
14009 situations where most or all of the row's buffer
14010 text is completely covered by display properties,
14011 so that no glyph with valid buffer positions is
14012 ever seen in the row. */
14013 ptrdiff_t prop_pos =
14014 string_buffer_position_lim (glyph->object, pos_before,
14015 pos_after, 0);
14016
14017 if (prop_pos >= pos_before)
14018 bpos_max = prop_pos - 1;
14019 }
14020 if (INTEGERP (chprop))
14021 {
14022 bpos_covered = bpos_max + XINT (chprop);
14023 /* If the `cursor' property covers buffer positions up
14024 to and including point, we should display cursor on
14025 this glyph. Note that, if a `cursor' property on one
14026 of the string's characters has an integer value, we
14027 will break out of the loop below _before_ we get to
14028 the position match above. IOW, integer values of
14029 the `cursor' property override the "exact match for
14030 point" strategy of positioning the cursor. */
14031 /* Implementation note: bpos_max == pt_old when, e.g.,
14032 we are in an empty line, where bpos_max is set to
14033 MATRIX_ROW_START_CHARPOS, see above. */
14034 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14035 {
14036 cursor = glyph;
14037 break;
14038 }
14039 }
14040
14041 string_seen = 1;
14042 }
14043 x += glyph->pixel_width;
14044 ++glyph;
14045 }
14046 else if (glyph > end) /* row is reversed */
14047 while (!INTEGERP (glyph->object))
14048 {
14049 if (BUFFERP (glyph->object))
14050 {
14051 ptrdiff_t dpos = glyph->charpos - pt_old;
14052
14053 if (glyph->charpos > bpos_max)
14054 bpos_max = glyph->charpos;
14055 if (glyph->charpos < bpos_min)
14056 bpos_min = glyph->charpos;
14057 if (!glyph->avoid_cursor_p)
14058 {
14059 if (dpos == 0)
14060 {
14061 match_with_avoid_cursor = 0;
14062 break;
14063 }
14064 if (0 > dpos && dpos > pos_before - pt_old)
14065 {
14066 pos_before = glyph->charpos;
14067 glyph_before = glyph;
14068 }
14069 else if (0 < dpos && dpos < pos_after - pt_old)
14070 {
14071 pos_after = glyph->charpos;
14072 glyph_after = glyph;
14073 }
14074 }
14075 else if (dpos == 0)
14076 match_with_avoid_cursor = 1;
14077 }
14078 else if (STRINGP (glyph->object))
14079 {
14080 Lisp_Object chprop;
14081 ptrdiff_t glyph_pos = glyph->charpos;
14082
14083 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14084 glyph->object);
14085 if (!NILP (chprop))
14086 {
14087 ptrdiff_t prop_pos =
14088 string_buffer_position_lim (glyph->object, pos_before,
14089 pos_after, 0);
14090
14091 if (prop_pos >= pos_before)
14092 bpos_max = prop_pos - 1;
14093 }
14094 if (INTEGERP (chprop))
14095 {
14096 bpos_covered = bpos_max + XINT (chprop);
14097 /* If the `cursor' property covers buffer positions up
14098 to and including point, we should display cursor on
14099 this glyph. */
14100 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14101 {
14102 cursor = glyph;
14103 break;
14104 }
14105 }
14106 string_seen = 1;
14107 }
14108 --glyph;
14109 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14110 {
14111 x--; /* can't use any pixel_width */
14112 break;
14113 }
14114 x -= glyph->pixel_width;
14115 }
14116
14117 /* Step 2: If we didn't find an exact match for point, we need to
14118 look for a proper place to put the cursor among glyphs between
14119 GLYPH_BEFORE and GLYPH_AFTER. */
14120 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14121 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14122 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14123 {
14124 /* An empty line has a single glyph whose OBJECT is zero and
14125 whose CHARPOS is the position of a newline on that line.
14126 Note that on a TTY, there are more glyphs after that, which
14127 were produced by extend_face_to_end_of_line, but their
14128 CHARPOS is zero or negative. */
14129 int empty_line_p =
14130 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14131 && INTEGERP (glyph->object) && glyph->charpos > 0
14132 /* On a TTY, continued and truncated rows also have a glyph at
14133 their end whose OBJECT is zero and whose CHARPOS is
14134 positive (the continuation and truncation glyphs), but such
14135 rows are obviously not "empty". */
14136 && !(row->continued_p || row->truncated_on_right_p);
14137
14138 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14139 {
14140 ptrdiff_t ellipsis_pos;
14141
14142 /* Scan back over the ellipsis glyphs. */
14143 if (!row->reversed_p)
14144 {
14145 ellipsis_pos = (glyph - 1)->charpos;
14146 while (glyph > row->glyphs[TEXT_AREA]
14147 && (glyph - 1)->charpos == ellipsis_pos)
14148 glyph--, x -= glyph->pixel_width;
14149 /* That loop always goes one position too far, including
14150 the glyph before the ellipsis. So scan forward over
14151 that one. */
14152 x += glyph->pixel_width;
14153 glyph++;
14154 }
14155 else /* row is reversed */
14156 {
14157 ellipsis_pos = (glyph + 1)->charpos;
14158 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14159 && (glyph + 1)->charpos == ellipsis_pos)
14160 glyph++, x += glyph->pixel_width;
14161 x -= glyph->pixel_width;
14162 glyph--;
14163 }
14164 }
14165 else if (match_with_avoid_cursor)
14166 {
14167 cursor = glyph_after;
14168 x = -1;
14169 }
14170 else if (string_seen)
14171 {
14172 int incr = row->reversed_p ? -1 : +1;
14173
14174 /* Need to find the glyph that came out of a string which is
14175 present at point. That glyph is somewhere between
14176 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14177 positioned between POS_BEFORE and POS_AFTER in the
14178 buffer. */
14179 struct glyph *start, *stop;
14180 ptrdiff_t pos = pos_before;
14181
14182 x = -1;
14183
14184 /* If the row ends in a newline from a display string,
14185 reordering could have moved the glyphs belonging to the
14186 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14187 in this case we extend the search to the last glyph in
14188 the row that was not inserted by redisplay. */
14189 if (row->ends_in_newline_from_string_p)
14190 {
14191 glyph_after = end;
14192 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14193 }
14194
14195 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14196 correspond to POS_BEFORE and POS_AFTER, respectively. We
14197 need START and STOP in the order that corresponds to the
14198 row's direction as given by its reversed_p flag. If the
14199 directionality of characters between POS_BEFORE and
14200 POS_AFTER is the opposite of the row's base direction,
14201 these characters will have been reordered for display,
14202 and we need to reverse START and STOP. */
14203 if (!row->reversed_p)
14204 {
14205 start = min (glyph_before, glyph_after);
14206 stop = max (glyph_before, glyph_after);
14207 }
14208 else
14209 {
14210 start = max (glyph_before, glyph_after);
14211 stop = min (glyph_before, glyph_after);
14212 }
14213 for (glyph = start + incr;
14214 row->reversed_p ? glyph > stop : glyph < stop; )
14215 {
14216
14217 /* Any glyphs that come from the buffer are here because
14218 of bidi reordering. Skip them, and only pay
14219 attention to glyphs that came from some string. */
14220 if (STRINGP (glyph->object))
14221 {
14222 Lisp_Object str;
14223 ptrdiff_t tem;
14224 /* If the display property covers the newline, we
14225 need to search for it one position farther. */
14226 ptrdiff_t lim = pos_after
14227 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14228
14229 string_from_text_prop = 0;
14230 str = glyph->object;
14231 tem = string_buffer_position_lim (str, pos, lim, 0);
14232 if (tem == 0 /* from overlay */
14233 || pos <= tem)
14234 {
14235 /* If the string from which this glyph came is
14236 found in the buffer at point, or at position
14237 that is closer to point than pos_after, then
14238 we've found the glyph we've been looking for.
14239 If it comes from an overlay (tem == 0), and
14240 it has the `cursor' property on one of its
14241 glyphs, record that glyph as a candidate for
14242 displaying the cursor. (As in the
14243 unidirectional version, we will display the
14244 cursor on the last candidate we find.) */
14245 if (tem == 0
14246 || tem == pt_old
14247 || (tem - pt_old > 0 && tem < pos_after))
14248 {
14249 /* The glyphs from this string could have
14250 been reordered. Find the one with the
14251 smallest string position. Or there could
14252 be a character in the string with the
14253 `cursor' property, which means display
14254 cursor on that character's glyph. */
14255 ptrdiff_t strpos = glyph->charpos;
14256
14257 if (tem)
14258 {
14259 cursor = glyph;
14260 string_from_text_prop = 1;
14261 }
14262 for ( ;
14263 (row->reversed_p ? glyph > stop : glyph < stop)
14264 && EQ (glyph->object, str);
14265 glyph += incr)
14266 {
14267 Lisp_Object cprop;
14268 ptrdiff_t gpos = glyph->charpos;
14269
14270 cprop = Fget_char_property (make_number (gpos),
14271 Qcursor,
14272 glyph->object);
14273 if (!NILP (cprop))
14274 {
14275 cursor = glyph;
14276 break;
14277 }
14278 if (tem && glyph->charpos < strpos)
14279 {
14280 strpos = glyph->charpos;
14281 cursor = glyph;
14282 }
14283 }
14284
14285 if (tem == pt_old
14286 || (tem - pt_old > 0 && tem < pos_after))
14287 goto compute_x;
14288 }
14289 if (tem)
14290 pos = tem + 1; /* don't find previous instances */
14291 }
14292 /* This string is not what we want; skip all of the
14293 glyphs that came from it. */
14294 while ((row->reversed_p ? glyph > stop : glyph < stop)
14295 && EQ (glyph->object, str))
14296 glyph += incr;
14297 }
14298 else
14299 glyph += incr;
14300 }
14301
14302 /* If we reached the end of the line, and END was from a string,
14303 the cursor is not on this line. */
14304 if (cursor == NULL
14305 && (row->reversed_p ? glyph <= end : glyph >= end)
14306 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14307 && STRINGP (end->object)
14308 && row->continued_p)
14309 return 0;
14310 }
14311 /* A truncated row may not include PT among its character positions.
14312 Setting the cursor inside the scroll margin will trigger
14313 recalculation of hscroll in hscroll_window_tree. But if a
14314 display string covers point, defer to the string-handling
14315 code below to figure this out. */
14316 else if (row->truncated_on_left_p && pt_old < bpos_min)
14317 {
14318 cursor = glyph_before;
14319 x = -1;
14320 }
14321 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14322 /* Zero-width characters produce no glyphs. */
14323 || (!empty_line_p
14324 && (row->reversed_p
14325 ? glyph_after > glyphs_end
14326 : glyph_after < glyphs_end)))
14327 {
14328 cursor = glyph_after;
14329 x = -1;
14330 }
14331 }
14332
14333 compute_x:
14334 if (cursor != NULL)
14335 glyph = cursor;
14336 else if (glyph == glyphs_end
14337 && pos_before == pos_after
14338 && STRINGP ((row->reversed_p
14339 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14340 : row->glyphs[TEXT_AREA])->object))
14341 {
14342 /* If all the glyphs of this row came from strings, put the
14343 cursor on the first glyph of the row. This avoids having the
14344 cursor outside of the text area in this very rare and hard
14345 use case. */
14346 glyph =
14347 row->reversed_p
14348 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14349 : row->glyphs[TEXT_AREA];
14350 }
14351 if (x < 0)
14352 {
14353 struct glyph *g;
14354
14355 /* Need to compute x that corresponds to GLYPH. */
14356 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14357 {
14358 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14359 emacs_abort ();
14360 x += g->pixel_width;
14361 }
14362 }
14363
14364 /* ROW could be part of a continued line, which, under bidi
14365 reordering, might have other rows whose start and end charpos
14366 occlude point. Only set w->cursor if we found a better
14367 approximation to the cursor position than we have from previously
14368 examined candidate rows belonging to the same continued line. */
14369 if (/* we already have a candidate row */
14370 w->cursor.vpos >= 0
14371 /* that candidate is not the row we are processing */
14372 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14373 /* Make sure cursor.vpos specifies a row whose start and end
14374 charpos occlude point, and it is valid candidate for being a
14375 cursor-row. This is because some callers of this function
14376 leave cursor.vpos at the row where the cursor was displayed
14377 during the last redisplay cycle. */
14378 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14379 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14380 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14381 {
14382 struct glyph *g1 =
14383 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14384
14385 /* Don't consider glyphs that are outside TEXT_AREA. */
14386 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14387 return 0;
14388 /* Keep the candidate whose buffer position is the closest to
14389 point or has the `cursor' property. */
14390 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14391 w->cursor.hpos >= 0
14392 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14393 && ((BUFFERP (g1->object)
14394 && (g1->charpos == pt_old /* an exact match always wins */
14395 || (BUFFERP (glyph->object)
14396 && eabs (g1->charpos - pt_old)
14397 < eabs (glyph->charpos - pt_old))))
14398 /* previous candidate is a glyph from a string that has
14399 a non-nil `cursor' property */
14400 || (STRINGP (g1->object)
14401 && (!NILP (Fget_char_property (make_number (g1->charpos),
14402 Qcursor, g1->object))
14403 /* previous candidate is from the same display
14404 string as this one, and the display string
14405 came from a text property */
14406 || (EQ (g1->object, glyph->object)
14407 && string_from_text_prop)
14408 /* this candidate is from newline and its
14409 position is not an exact match */
14410 || (INTEGERP (glyph->object)
14411 && glyph->charpos != pt_old)))))
14412 return 0;
14413 /* If this candidate gives an exact match, use that. */
14414 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14415 /* If this candidate is a glyph created for the
14416 terminating newline of a line, and point is on that
14417 newline, it wins because it's an exact match. */
14418 || (!row->continued_p
14419 && INTEGERP (glyph->object)
14420 && glyph->charpos == 0
14421 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14422 /* Otherwise, keep the candidate that comes from a row
14423 spanning less buffer positions. This may win when one or
14424 both candidate positions are on glyphs that came from
14425 display strings, for which we cannot compare buffer
14426 positions. */
14427 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14428 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14429 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14430 return 0;
14431 }
14432 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14433 w->cursor.x = x;
14434 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14435 w->cursor.y = row->y + dy;
14436
14437 if (w == XWINDOW (selected_window))
14438 {
14439 if (!row->continued_p
14440 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14441 && row->x == 0)
14442 {
14443 this_line_buffer = XBUFFER (w->contents);
14444
14445 CHARPOS (this_line_start_pos)
14446 = MATRIX_ROW_START_CHARPOS (row) + delta;
14447 BYTEPOS (this_line_start_pos)
14448 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14449
14450 CHARPOS (this_line_end_pos)
14451 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14452 BYTEPOS (this_line_end_pos)
14453 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14454
14455 this_line_y = w->cursor.y;
14456 this_line_pixel_height = row->height;
14457 this_line_vpos = w->cursor.vpos;
14458 this_line_start_x = row->x;
14459 }
14460 else
14461 CHARPOS (this_line_start_pos) = 0;
14462 }
14463
14464 return 1;
14465 }
14466
14467
14468 /* Run window scroll functions, if any, for WINDOW with new window
14469 start STARTP. Sets the window start of WINDOW to that position.
14470
14471 We assume that the window's buffer is really current. */
14472
14473 static struct text_pos
14474 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14475 {
14476 struct window *w = XWINDOW (window);
14477 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14478
14479 eassert (current_buffer == XBUFFER (w->contents));
14480
14481 if (!NILP (Vwindow_scroll_functions))
14482 {
14483 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14484 make_number (CHARPOS (startp)));
14485 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14486 /* In case the hook functions switch buffers. */
14487 set_buffer_internal (XBUFFER (w->contents));
14488 }
14489
14490 return startp;
14491 }
14492
14493
14494 /* Make sure the line containing the cursor is fully visible.
14495 A value of 1 means there is nothing to be done.
14496 (Either the line is fully visible, or it cannot be made so,
14497 or we cannot tell.)
14498
14499 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14500 is higher than window.
14501
14502 A value of 0 means the caller should do scrolling
14503 as if point had gone off the screen. */
14504
14505 static int
14506 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14507 {
14508 struct glyph_matrix *matrix;
14509 struct glyph_row *row;
14510 int window_height;
14511
14512 if (!make_cursor_line_fully_visible_p)
14513 return 1;
14514
14515 /* It's not always possible to find the cursor, e.g, when a window
14516 is full of overlay strings. Don't do anything in that case. */
14517 if (w->cursor.vpos < 0)
14518 return 1;
14519
14520 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14521 row = MATRIX_ROW (matrix, w->cursor.vpos);
14522
14523 /* If the cursor row is not partially visible, there's nothing to do. */
14524 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14525 return 1;
14526
14527 /* If the row the cursor is in is taller than the window's height,
14528 it's not clear what to do, so do nothing. */
14529 window_height = window_box_height (w);
14530 if (row->height >= window_height)
14531 {
14532 if (!force_p || MINI_WINDOW_P (w)
14533 || w->vscroll || w->cursor.vpos == 0)
14534 return 1;
14535 }
14536 return 0;
14537 }
14538
14539
14540 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14541 non-zero means only WINDOW is redisplayed in redisplay_internal.
14542 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14543 in redisplay_window to bring a partially visible line into view in
14544 the case that only the cursor has moved.
14545
14546 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14547 last screen line's vertical height extends past the end of the screen.
14548
14549 Value is
14550
14551 1 if scrolling succeeded
14552
14553 0 if scrolling didn't find point.
14554
14555 -1 if new fonts have been loaded so that we must interrupt
14556 redisplay, adjust glyph matrices, and try again. */
14557
14558 enum
14559 {
14560 SCROLLING_SUCCESS,
14561 SCROLLING_FAILED,
14562 SCROLLING_NEED_LARGER_MATRICES
14563 };
14564
14565 /* If scroll-conservatively is more than this, never recenter.
14566
14567 If you change this, don't forget to update the doc string of
14568 `scroll-conservatively' and the Emacs manual. */
14569 #define SCROLL_LIMIT 100
14570
14571 static int
14572 try_scrolling (Lisp_Object window, int just_this_one_p,
14573 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14574 int temp_scroll_step, int last_line_misfit)
14575 {
14576 struct window *w = XWINDOW (window);
14577 struct frame *f = XFRAME (w->frame);
14578 struct text_pos pos, startp;
14579 struct it it;
14580 int this_scroll_margin, scroll_max, rc, height;
14581 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14582 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14583 Lisp_Object aggressive;
14584 /* We will never try scrolling more than this number of lines. */
14585 int scroll_limit = SCROLL_LIMIT;
14586 int frame_line_height = default_line_pixel_height (w);
14587 int window_total_lines
14588 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14589
14590 #ifdef GLYPH_DEBUG
14591 debug_method_add (w, "try_scrolling");
14592 #endif
14593
14594 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14595
14596 /* Compute scroll margin height in pixels. We scroll when point is
14597 within this distance from the top or bottom of the window. */
14598 if (scroll_margin > 0)
14599 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14600 * frame_line_height;
14601 else
14602 this_scroll_margin = 0;
14603
14604 /* Force arg_scroll_conservatively to have a reasonable value, to
14605 avoid scrolling too far away with slow move_it_* functions. Note
14606 that the user can supply scroll-conservatively equal to
14607 `most-positive-fixnum', which can be larger than INT_MAX. */
14608 if (arg_scroll_conservatively > scroll_limit)
14609 {
14610 arg_scroll_conservatively = scroll_limit + 1;
14611 scroll_max = scroll_limit * frame_line_height;
14612 }
14613 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14614 /* Compute how much we should try to scroll maximally to bring
14615 point into view. */
14616 scroll_max = (max (scroll_step,
14617 max (arg_scroll_conservatively, temp_scroll_step))
14618 * frame_line_height);
14619 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14620 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14621 /* We're trying to scroll because of aggressive scrolling but no
14622 scroll_step is set. Choose an arbitrary one. */
14623 scroll_max = 10 * frame_line_height;
14624 else
14625 scroll_max = 0;
14626
14627 too_near_end:
14628
14629 /* Decide whether to scroll down. */
14630 if (PT > CHARPOS (startp))
14631 {
14632 int scroll_margin_y;
14633
14634 /* Compute the pixel ypos of the scroll margin, then move IT to
14635 either that ypos or PT, whichever comes first. */
14636 start_display (&it, w, startp);
14637 scroll_margin_y = it.last_visible_y - this_scroll_margin
14638 - frame_line_height * extra_scroll_margin_lines;
14639 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14640 (MOVE_TO_POS | MOVE_TO_Y));
14641
14642 if (PT > CHARPOS (it.current.pos))
14643 {
14644 int y0 = line_bottom_y (&it);
14645 /* Compute how many pixels below window bottom to stop searching
14646 for PT. This avoids costly search for PT that is far away if
14647 the user limited scrolling by a small number of lines, but
14648 always finds PT if scroll_conservatively is set to a large
14649 number, such as most-positive-fixnum. */
14650 int slack = max (scroll_max, 10 * frame_line_height);
14651 int y_to_move = it.last_visible_y + slack;
14652
14653 /* Compute the distance from the scroll margin to PT or to
14654 the scroll limit, whichever comes first. This should
14655 include the height of the cursor line, to make that line
14656 fully visible. */
14657 move_it_to (&it, PT, -1, y_to_move,
14658 -1, MOVE_TO_POS | MOVE_TO_Y);
14659 dy = line_bottom_y (&it) - y0;
14660
14661 if (dy > scroll_max)
14662 return SCROLLING_FAILED;
14663
14664 if (dy > 0)
14665 scroll_down_p = 1;
14666 }
14667 }
14668
14669 if (scroll_down_p)
14670 {
14671 /* Point is in or below the bottom scroll margin, so move the
14672 window start down. If scrolling conservatively, move it just
14673 enough down to make point visible. If scroll_step is set,
14674 move it down by scroll_step. */
14675 if (arg_scroll_conservatively)
14676 amount_to_scroll
14677 = min (max (dy, frame_line_height),
14678 frame_line_height * arg_scroll_conservatively);
14679 else if (scroll_step || temp_scroll_step)
14680 amount_to_scroll = scroll_max;
14681 else
14682 {
14683 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14684 height = WINDOW_BOX_TEXT_HEIGHT (w);
14685 if (NUMBERP (aggressive))
14686 {
14687 double float_amount = XFLOATINT (aggressive) * height;
14688 int aggressive_scroll = float_amount;
14689 if (aggressive_scroll == 0 && float_amount > 0)
14690 aggressive_scroll = 1;
14691 /* Don't let point enter the scroll margin near top of
14692 the window. This could happen if the value of
14693 scroll_up_aggressively is too large and there are
14694 non-zero margins, because scroll_up_aggressively
14695 means put point that fraction of window height
14696 _from_the_bottom_margin_. */
14697 if (aggressive_scroll + 2*this_scroll_margin > height)
14698 aggressive_scroll = height - 2*this_scroll_margin;
14699 amount_to_scroll = dy + aggressive_scroll;
14700 }
14701 }
14702
14703 if (amount_to_scroll <= 0)
14704 return SCROLLING_FAILED;
14705
14706 start_display (&it, w, startp);
14707 if (arg_scroll_conservatively <= scroll_limit)
14708 move_it_vertically (&it, amount_to_scroll);
14709 else
14710 {
14711 /* Extra precision for users who set scroll-conservatively
14712 to a large number: make sure the amount we scroll
14713 the window start is never less than amount_to_scroll,
14714 which was computed as distance from window bottom to
14715 point. This matters when lines at window top and lines
14716 below window bottom have different height. */
14717 struct it it1;
14718 void *it1data = NULL;
14719 /* We use a temporary it1 because line_bottom_y can modify
14720 its argument, if it moves one line down; see there. */
14721 int start_y;
14722
14723 SAVE_IT (it1, it, it1data);
14724 start_y = line_bottom_y (&it1);
14725 do {
14726 RESTORE_IT (&it, &it, it1data);
14727 move_it_by_lines (&it, 1);
14728 SAVE_IT (it1, it, it1data);
14729 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14730 }
14731
14732 /* If STARTP is unchanged, move it down another screen line. */
14733 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14734 move_it_by_lines (&it, 1);
14735 startp = it.current.pos;
14736 }
14737 else
14738 {
14739 struct text_pos scroll_margin_pos = startp;
14740 int y_offset = 0;
14741
14742 /* See if point is inside the scroll margin at the top of the
14743 window. */
14744 if (this_scroll_margin)
14745 {
14746 int y_start;
14747
14748 start_display (&it, w, startp);
14749 y_start = it.current_y;
14750 move_it_vertically (&it, this_scroll_margin);
14751 scroll_margin_pos = it.current.pos;
14752 /* If we didn't move enough before hitting ZV, request
14753 additional amount of scroll, to move point out of the
14754 scroll margin. */
14755 if (IT_CHARPOS (it) == ZV
14756 && it.current_y - y_start < this_scroll_margin)
14757 y_offset = this_scroll_margin - (it.current_y - y_start);
14758 }
14759
14760 if (PT < CHARPOS (scroll_margin_pos))
14761 {
14762 /* Point is in the scroll margin at the top of the window or
14763 above what is displayed in the window. */
14764 int y0, y_to_move;
14765
14766 /* Compute the vertical distance from PT to the scroll
14767 margin position. Move as far as scroll_max allows, or
14768 one screenful, or 10 screen lines, whichever is largest.
14769 Give up if distance is greater than scroll_max or if we
14770 didn't reach the scroll margin position. */
14771 SET_TEXT_POS (pos, PT, PT_BYTE);
14772 start_display (&it, w, pos);
14773 y0 = it.current_y;
14774 y_to_move = max (it.last_visible_y,
14775 max (scroll_max, 10 * frame_line_height));
14776 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14777 y_to_move, -1,
14778 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14779 dy = it.current_y - y0;
14780 if (dy > scroll_max
14781 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14782 return SCROLLING_FAILED;
14783
14784 /* Additional scroll for when ZV was too close to point. */
14785 dy += y_offset;
14786
14787 /* Compute new window start. */
14788 start_display (&it, w, startp);
14789
14790 if (arg_scroll_conservatively)
14791 amount_to_scroll = max (dy, frame_line_height *
14792 max (scroll_step, temp_scroll_step));
14793 else if (scroll_step || temp_scroll_step)
14794 amount_to_scroll = scroll_max;
14795 else
14796 {
14797 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14798 height = WINDOW_BOX_TEXT_HEIGHT (w);
14799 if (NUMBERP (aggressive))
14800 {
14801 double float_amount = XFLOATINT (aggressive) * height;
14802 int aggressive_scroll = float_amount;
14803 if (aggressive_scroll == 0 && float_amount > 0)
14804 aggressive_scroll = 1;
14805 /* Don't let point enter the scroll margin near
14806 bottom of the window, if the value of
14807 scroll_down_aggressively happens to be too
14808 large. */
14809 if (aggressive_scroll + 2*this_scroll_margin > height)
14810 aggressive_scroll = height - 2*this_scroll_margin;
14811 amount_to_scroll = dy + aggressive_scroll;
14812 }
14813 }
14814
14815 if (amount_to_scroll <= 0)
14816 return SCROLLING_FAILED;
14817
14818 move_it_vertically_backward (&it, amount_to_scroll);
14819 startp = it.current.pos;
14820 }
14821 }
14822
14823 /* Run window scroll functions. */
14824 startp = run_window_scroll_functions (window, startp);
14825
14826 /* Display the window. Give up if new fonts are loaded, or if point
14827 doesn't appear. */
14828 if (!try_window (window, startp, 0))
14829 rc = SCROLLING_NEED_LARGER_MATRICES;
14830 else if (w->cursor.vpos < 0)
14831 {
14832 clear_glyph_matrix (w->desired_matrix);
14833 rc = SCROLLING_FAILED;
14834 }
14835 else
14836 {
14837 /* Maybe forget recorded base line for line number display. */
14838 if (!just_this_one_p
14839 || current_buffer->clip_changed
14840 || BEG_UNCHANGED < CHARPOS (startp))
14841 w->base_line_number = 0;
14842
14843 /* If cursor ends up on a partially visible line,
14844 treat that as being off the bottom of the screen. */
14845 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14846 /* It's possible that the cursor is on the first line of the
14847 buffer, which is partially obscured due to a vscroll
14848 (Bug#7537). In that case, avoid looping forever . */
14849 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14850 {
14851 clear_glyph_matrix (w->desired_matrix);
14852 ++extra_scroll_margin_lines;
14853 goto too_near_end;
14854 }
14855 rc = SCROLLING_SUCCESS;
14856 }
14857
14858 return rc;
14859 }
14860
14861
14862 /* Compute a suitable window start for window W if display of W starts
14863 on a continuation line. Value is non-zero if a new window start
14864 was computed.
14865
14866 The new window start will be computed, based on W's width, starting
14867 from the start of the continued line. It is the start of the
14868 screen line with the minimum distance from the old start W->start. */
14869
14870 static int
14871 compute_window_start_on_continuation_line (struct window *w)
14872 {
14873 struct text_pos pos, start_pos;
14874 int window_start_changed_p = 0;
14875
14876 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14877
14878 /* If window start is on a continuation line... Window start may be
14879 < BEGV in case there's invisible text at the start of the
14880 buffer (M-x rmail, for example). */
14881 if (CHARPOS (start_pos) > BEGV
14882 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14883 {
14884 struct it it;
14885 struct glyph_row *row;
14886
14887 /* Handle the case that the window start is out of range. */
14888 if (CHARPOS (start_pos) < BEGV)
14889 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14890 else if (CHARPOS (start_pos) > ZV)
14891 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14892
14893 /* Find the start of the continued line. This should be fast
14894 because find_newline is fast (newline cache). */
14895 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14896 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14897 row, DEFAULT_FACE_ID);
14898 reseat_at_previous_visible_line_start (&it);
14899
14900 /* If the line start is "too far" away from the window start,
14901 say it takes too much time to compute a new window start. */
14902 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14903 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14904 {
14905 int min_distance, distance;
14906
14907 /* Move forward by display lines to find the new window
14908 start. If window width was enlarged, the new start can
14909 be expected to be > the old start. If window width was
14910 decreased, the new window start will be < the old start.
14911 So, we're looking for the display line start with the
14912 minimum distance from the old window start. */
14913 pos = it.current.pos;
14914 min_distance = INFINITY;
14915 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14916 distance < min_distance)
14917 {
14918 min_distance = distance;
14919 pos = it.current.pos;
14920 if (it.line_wrap == WORD_WRAP)
14921 {
14922 /* Under WORD_WRAP, move_it_by_lines is likely to
14923 overshoot and stop not at the first, but the
14924 second character from the left margin. So in
14925 that case, we need a more tight control on the X
14926 coordinate of the iterator than move_it_by_lines
14927 promises in its contract. The method is to first
14928 go to the last (rightmost) visible character of a
14929 line, then move to the leftmost character on the
14930 next line in a separate call. */
14931 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14932 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14933 move_it_to (&it, ZV, 0,
14934 it.current_y + it.max_ascent + it.max_descent, -1,
14935 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14936 }
14937 else
14938 move_it_by_lines (&it, 1);
14939 }
14940
14941 /* Set the window start there. */
14942 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14943 window_start_changed_p = 1;
14944 }
14945 }
14946
14947 return window_start_changed_p;
14948 }
14949
14950
14951 /* Try cursor movement in case text has not changed in window WINDOW,
14952 with window start STARTP. Value is
14953
14954 CURSOR_MOVEMENT_SUCCESS if successful
14955
14956 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14957
14958 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14959 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14960 we want to scroll as if scroll-step were set to 1. See the code.
14961
14962 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14963 which case we have to abort this redisplay, and adjust matrices
14964 first. */
14965
14966 enum
14967 {
14968 CURSOR_MOVEMENT_SUCCESS,
14969 CURSOR_MOVEMENT_CANNOT_BE_USED,
14970 CURSOR_MOVEMENT_MUST_SCROLL,
14971 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14972 };
14973
14974 static int
14975 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14976 {
14977 struct window *w = XWINDOW (window);
14978 struct frame *f = XFRAME (w->frame);
14979 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14980
14981 #ifdef GLYPH_DEBUG
14982 if (inhibit_try_cursor_movement)
14983 return rc;
14984 #endif
14985
14986 /* Previously, there was a check for Lisp integer in the
14987 if-statement below. Now, this field is converted to
14988 ptrdiff_t, thus zero means invalid position in a buffer. */
14989 eassert (w->last_point > 0);
14990 /* Likewise there was a check whether window_end_vpos is nil or larger
14991 than the window. Now window_end_vpos is int and so never nil, but
14992 let's leave eassert to check whether it fits in the window. */
14993 eassert (w->window_end_vpos < w->current_matrix->nrows);
14994
14995 /* Handle case where text has not changed, only point, and it has
14996 not moved off the frame. */
14997 if (/* Point may be in this window. */
14998 PT >= CHARPOS (startp)
14999 /* Selective display hasn't changed. */
15000 && !current_buffer->clip_changed
15001 /* Function force-mode-line-update is used to force a thorough
15002 redisplay. It sets either windows_or_buffers_changed or
15003 update_mode_lines. So don't take a shortcut here for these
15004 cases. */
15005 && !update_mode_lines
15006 && !windows_or_buffers_changed
15007 && !f->cursor_type_changed
15008 /* Can't use this case if highlighting a region. When a
15009 region exists, cursor movement has to do more than just
15010 set the cursor. */
15011 && markpos_of_region () < 0
15012 && !w->region_showing
15013 && NILP (Vshow_trailing_whitespace)
15014 /* This code is not used for mini-buffer for the sake of the case
15015 of redisplaying to replace an echo area message; since in
15016 that case the mini-buffer contents per se are usually
15017 unchanged. This code is of no real use in the mini-buffer
15018 since the handling of this_line_start_pos, etc., in redisplay
15019 handles the same cases. */
15020 && !EQ (window, minibuf_window)
15021 && (FRAME_WINDOW_P (f)
15022 || !overlay_arrow_in_current_buffer_p ()))
15023 {
15024 int this_scroll_margin, top_scroll_margin;
15025 struct glyph_row *row = NULL;
15026 int frame_line_height = default_line_pixel_height (w);
15027 int window_total_lines
15028 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15029
15030 #ifdef GLYPH_DEBUG
15031 debug_method_add (w, "cursor movement");
15032 #endif
15033
15034 /* Scroll if point within this distance from the top or bottom
15035 of the window. This is a pixel value. */
15036 if (scroll_margin > 0)
15037 {
15038 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15039 this_scroll_margin *= frame_line_height;
15040 }
15041 else
15042 this_scroll_margin = 0;
15043
15044 top_scroll_margin = this_scroll_margin;
15045 if (WINDOW_WANTS_HEADER_LINE_P (w))
15046 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15047
15048 /* Start with the row the cursor was displayed during the last
15049 not paused redisplay. Give up if that row is not valid. */
15050 if (w->last_cursor_vpos < 0
15051 || w->last_cursor_vpos >= w->current_matrix->nrows)
15052 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15053 else
15054 {
15055 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15056 if (row->mode_line_p)
15057 ++row;
15058 if (!row->enabled_p)
15059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15060 }
15061
15062 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15063 {
15064 int scroll_p = 0, must_scroll = 0;
15065 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15066
15067 if (PT > w->last_point)
15068 {
15069 /* Point has moved forward. */
15070 while (MATRIX_ROW_END_CHARPOS (row) < PT
15071 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15072 {
15073 eassert (row->enabled_p);
15074 ++row;
15075 }
15076
15077 /* If the end position of a row equals the start
15078 position of the next row, and PT is at that position,
15079 we would rather display cursor in the next line. */
15080 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15081 && MATRIX_ROW_END_CHARPOS (row) == PT
15082 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15083 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15084 && !cursor_row_p (row))
15085 ++row;
15086
15087 /* If within the scroll margin, scroll. Note that
15088 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15089 the next line would be drawn, and that
15090 this_scroll_margin can be zero. */
15091 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15092 || PT > MATRIX_ROW_END_CHARPOS (row)
15093 /* Line is completely visible last line in window
15094 and PT is to be set in the next line. */
15095 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15096 && PT == MATRIX_ROW_END_CHARPOS (row)
15097 && !row->ends_at_zv_p
15098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15099 scroll_p = 1;
15100 }
15101 else if (PT < w->last_point)
15102 {
15103 /* Cursor has to be moved backward. Note that PT >=
15104 CHARPOS (startp) because of the outer if-statement. */
15105 while (!row->mode_line_p
15106 && (MATRIX_ROW_START_CHARPOS (row) > PT
15107 || (MATRIX_ROW_START_CHARPOS (row) == PT
15108 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15109 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15110 row > w->current_matrix->rows
15111 && (row-1)->ends_in_newline_from_string_p))))
15112 && (row->y > top_scroll_margin
15113 || CHARPOS (startp) == BEGV))
15114 {
15115 eassert (row->enabled_p);
15116 --row;
15117 }
15118
15119 /* Consider the following case: Window starts at BEGV,
15120 there is invisible, intangible text at BEGV, so that
15121 display starts at some point START > BEGV. It can
15122 happen that we are called with PT somewhere between
15123 BEGV and START. Try to handle that case. */
15124 if (row < w->current_matrix->rows
15125 || row->mode_line_p)
15126 {
15127 row = w->current_matrix->rows;
15128 if (row->mode_line_p)
15129 ++row;
15130 }
15131
15132 /* Due to newlines in overlay strings, we may have to
15133 skip forward over overlay strings. */
15134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15135 && MATRIX_ROW_END_CHARPOS (row) == PT
15136 && !cursor_row_p (row))
15137 ++row;
15138
15139 /* If within the scroll margin, scroll. */
15140 if (row->y < top_scroll_margin
15141 && CHARPOS (startp) != BEGV)
15142 scroll_p = 1;
15143 }
15144 else
15145 {
15146 /* Cursor did not move. So don't scroll even if cursor line
15147 is partially visible, as it was so before. */
15148 rc = CURSOR_MOVEMENT_SUCCESS;
15149 }
15150
15151 if (PT < MATRIX_ROW_START_CHARPOS (row)
15152 || PT > MATRIX_ROW_END_CHARPOS (row))
15153 {
15154 /* if PT is not in the glyph row, give up. */
15155 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15156 must_scroll = 1;
15157 }
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15160 {
15161 struct glyph_row *row1;
15162
15163 /* If rows are bidi-reordered and point moved, back up
15164 until we find a row that does not belong to a
15165 continuation line. This is because we must consider
15166 all rows of a continued line as candidates for the
15167 new cursor positioning, since row start and end
15168 positions change non-linearly with vertical position
15169 in such rows. */
15170 /* FIXME: Revisit this when glyph ``spilling'' in
15171 continuation lines' rows is implemented for
15172 bidi-reordered rows. */
15173 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15174 MATRIX_ROW_CONTINUATION_LINE_P (row);
15175 --row)
15176 {
15177 /* If we hit the beginning of the displayed portion
15178 without finding the first row of a continued
15179 line, give up. */
15180 if (row <= row1)
15181 {
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 break;
15184 }
15185 eassert (row->enabled_p);
15186 }
15187 }
15188 if (must_scroll)
15189 ;
15190 else if (rc != CURSOR_MOVEMENT_SUCCESS
15191 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15192 /* Make sure this isn't a header line by any chance, since
15193 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15194 && !row->mode_line_p
15195 && make_cursor_line_fully_visible_p)
15196 {
15197 if (PT == MATRIX_ROW_END_CHARPOS (row)
15198 && !row->ends_at_zv_p
15199 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 else if (row->height > window_box_height (w))
15202 {
15203 /* If we end up in a partially visible line, let's
15204 make it fully visible, except when it's taller
15205 than the window, in which case we can't do much
15206 about it. */
15207 *scroll_step = 1;
15208 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15209 }
15210 else
15211 {
15212 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15213 if (!cursor_row_fully_visible_p (w, 0, 1))
15214 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15215 else
15216 rc = CURSOR_MOVEMENT_SUCCESS;
15217 }
15218 }
15219 else if (scroll_p)
15220 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15221 else if (rc != CURSOR_MOVEMENT_SUCCESS
15222 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15223 {
15224 /* With bidi-reordered rows, there could be more than
15225 one candidate row whose start and end positions
15226 occlude point. We need to let set_cursor_from_row
15227 find the best candidate. */
15228 /* FIXME: Revisit this when glyph ``spilling'' in
15229 continuation lines' rows is implemented for
15230 bidi-reordered rows. */
15231 int rv = 0;
15232
15233 do
15234 {
15235 int at_zv_p = 0, exact_match_p = 0;
15236
15237 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15238 && PT <= MATRIX_ROW_END_CHARPOS (row)
15239 && cursor_row_p (row))
15240 rv |= set_cursor_from_row (w, row, w->current_matrix,
15241 0, 0, 0, 0);
15242 /* As soon as we've found the exact match for point,
15243 or the first suitable row whose ends_at_zv_p flag
15244 is set, we are done. */
15245 at_zv_p =
15246 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15247 if (rv && !at_zv_p
15248 && w->cursor.hpos >= 0
15249 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15250 w->cursor.vpos))
15251 {
15252 struct glyph_row *candidate =
15253 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15254 struct glyph *g =
15255 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15256 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15257
15258 exact_match_p =
15259 (BUFFERP (g->object) && g->charpos == PT)
15260 || (INTEGERP (g->object)
15261 && (g->charpos == PT
15262 || (g->charpos == 0 && endpos - 1 == PT)));
15263 }
15264 if (rv && (at_zv_p || exact_match_p))
15265 {
15266 rc = CURSOR_MOVEMENT_SUCCESS;
15267 break;
15268 }
15269 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15270 break;
15271 ++row;
15272 }
15273 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15274 || row->continued_p)
15275 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15276 || (MATRIX_ROW_START_CHARPOS (row) == PT
15277 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15278 /* If we didn't find any candidate rows, or exited the
15279 loop before all the candidates were examined, signal
15280 to the caller that this method failed. */
15281 if (rc != CURSOR_MOVEMENT_SUCCESS
15282 && !(rv
15283 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15284 && !row->continued_p))
15285 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15286 else if (rv)
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15288 }
15289 else
15290 {
15291 do
15292 {
15293 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15294 {
15295 rc = CURSOR_MOVEMENT_SUCCESS;
15296 break;
15297 }
15298 ++row;
15299 }
15300 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15301 && MATRIX_ROW_START_CHARPOS (row) == PT
15302 && cursor_row_p (row));
15303 }
15304 }
15305 }
15306
15307 return rc;
15308 }
15309
15310 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15311 static
15312 #endif
15313 void
15314 set_vertical_scroll_bar (struct window *w)
15315 {
15316 ptrdiff_t start, end, whole;
15317
15318 /* Calculate the start and end positions for the current window.
15319 At some point, it would be nice to choose between scrollbars
15320 which reflect the whole buffer size, with special markers
15321 indicating narrowing, and scrollbars which reflect only the
15322 visible region.
15323
15324 Note that mini-buffers sometimes aren't displaying any text. */
15325 if (!MINI_WINDOW_P (w)
15326 || (w == XWINDOW (minibuf_window)
15327 && NILP (echo_area_buffer[0])))
15328 {
15329 struct buffer *buf = XBUFFER (w->contents);
15330 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15331 start = marker_position (w->start) - BUF_BEGV (buf);
15332 /* I don't think this is guaranteed to be right. For the
15333 moment, we'll pretend it is. */
15334 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15335
15336 if (end < start)
15337 end = start;
15338 if (whole < (end - start))
15339 whole = end - start;
15340 }
15341 else
15342 start = end = whole = 0;
15343
15344 /* Indicate what this scroll bar ought to be displaying now. */
15345 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15346 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15347 (w, end - start, whole, start);
15348 }
15349
15350
15351 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15352 selected_window is redisplayed.
15353
15354 We can return without actually redisplaying the window if fonts has been
15355 changed on window's frame. In that case, redisplay_internal will retry. */
15356
15357 static void
15358 redisplay_window (Lisp_Object window, int just_this_one_p)
15359 {
15360 struct window *w = XWINDOW (window);
15361 struct frame *f = XFRAME (w->frame);
15362 struct buffer *buffer = XBUFFER (w->contents);
15363 struct buffer *old = current_buffer;
15364 struct text_pos lpoint, opoint, startp;
15365 int update_mode_line;
15366 int tem;
15367 struct it it;
15368 /* Record it now because it's overwritten. */
15369 int current_matrix_up_to_date_p = 0;
15370 int used_current_matrix_p = 0;
15371 /* This is less strict than current_matrix_up_to_date_p.
15372 It indicates that the buffer contents and narrowing are unchanged. */
15373 int buffer_unchanged_p = 0;
15374 int temp_scroll_step = 0;
15375 ptrdiff_t count = SPECPDL_INDEX ();
15376 int rc;
15377 int centering_position = -1;
15378 int last_line_misfit = 0;
15379 ptrdiff_t beg_unchanged, end_unchanged;
15380 int frame_line_height;
15381
15382 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15383 opoint = lpoint;
15384
15385 #ifdef GLYPH_DEBUG
15386 *w->desired_matrix->method = 0;
15387 #endif
15388
15389 /* Make sure that both W's markers are valid. */
15390 eassert (XMARKER (w->start)->buffer == buffer);
15391 eassert (XMARKER (w->pointm)->buffer == buffer);
15392
15393 restart:
15394 reconsider_clip_changes (w);
15395 frame_line_height = default_line_pixel_height (w);
15396
15397 /* Has the mode line to be updated? */
15398 update_mode_line = (w->update_mode_line
15399 || update_mode_lines
15400 || buffer->clip_changed
15401 || buffer->prevent_redisplay_optimizations_p);
15402
15403 if (MINI_WINDOW_P (w))
15404 {
15405 if (w == XWINDOW (echo_area_window)
15406 && !NILP (echo_area_buffer[0]))
15407 {
15408 if (update_mode_line)
15409 /* We may have to update a tty frame's menu bar or a
15410 tool-bar. Example `M-x C-h C-h C-g'. */
15411 goto finish_menu_bars;
15412 else
15413 /* We've already displayed the echo area glyphs in this window. */
15414 goto finish_scroll_bars;
15415 }
15416 else if ((w != XWINDOW (minibuf_window)
15417 || minibuf_level == 0)
15418 /* When buffer is nonempty, redisplay window normally. */
15419 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15420 /* Quail displays non-mini buffers in minibuffer window.
15421 In that case, redisplay the window normally. */
15422 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15423 {
15424 /* W is a mini-buffer window, but it's not active, so clear
15425 it. */
15426 int yb = window_text_bottom_y (w);
15427 struct glyph_row *row;
15428 int y;
15429
15430 for (y = 0, row = w->desired_matrix->rows;
15431 y < yb;
15432 y += row->height, ++row)
15433 blank_row (w, row, y);
15434 goto finish_scroll_bars;
15435 }
15436
15437 clear_glyph_matrix (w->desired_matrix);
15438 }
15439
15440 /* Otherwise set up data on this window; select its buffer and point
15441 value. */
15442 /* Really select the buffer, for the sake of buffer-local
15443 variables. */
15444 set_buffer_internal_1 (XBUFFER (w->contents));
15445
15446 current_matrix_up_to_date_p
15447 = (w->window_end_valid
15448 && !current_buffer->clip_changed
15449 && !current_buffer->prevent_redisplay_optimizations_p
15450 && !window_outdated (w));
15451
15452 /* Run the window-bottom-change-functions
15453 if it is possible that the text on the screen has changed
15454 (either due to modification of the text, or any other reason). */
15455 if (!current_matrix_up_to_date_p
15456 && !NILP (Vwindow_text_change_functions))
15457 {
15458 safe_run_hooks (Qwindow_text_change_functions);
15459 goto restart;
15460 }
15461
15462 beg_unchanged = BEG_UNCHANGED;
15463 end_unchanged = END_UNCHANGED;
15464
15465 SET_TEXT_POS (opoint, PT, PT_BYTE);
15466
15467 specbind (Qinhibit_point_motion_hooks, Qt);
15468
15469 buffer_unchanged_p
15470 = (w->window_end_valid
15471 && !current_buffer->clip_changed
15472 && !window_outdated (w));
15473
15474 /* When windows_or_buffers_changed is non-zero, we can't rely
15475 on the window end being valid, so set it to zero there. */
15476 if (windows_or_buffers_changed)
15477 {
15478 /* If window starts on a continuation line, maybe adjust the
15479 window start in case the window's width changed. */
15480 if (XMARKER (w->start)->buffer == current_buffer)
15481 compute_window_start_on_continuation_line (w);
15482
15483 w->window_end_valid = 0;
15484 /* If so, we also can't rely on current matrix
15485 and should not fool try_cursor_movement below. */
15486 current_matrix_up_to_date_p = 0;
15487 }
15488
15489 /* Some sanity checks. */
15490 CHECK_WINDOW_END (w);
15491 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15492 emacs_abort ();
15493 if (BYTEPOS (opoint) < CHARPOS (opoint))
15494 emacs_abort ();
15495
15496 if (mode_line_update_needed (w))
15497 update_mode_line = 1;
15498
15499 /* Point refers normally to the selected window. For any other
15500 window, set up appropriate value. */
15501 if (!EQ (window, selected_window))
15502 {
15503 ptrdiff_t new_pt = marker_position (w->pointm);
15504 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15505 if (new_pt < BEGV)
15506 {
15507 new_pt = BEGV;
15508 new_pt_byte = BEGV_BYTE;
15509 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15510 }
15511 else if (new_pt > (ZV - 1))
15512 {
15513 new_pt = ZV;
15514 new_pt_byte = ZV_BYTE;
15515 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15516 }
15517
15518 /* We don't use SET_PT so that the point-motion hooks don't run. */
15519 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15520 }
15521
15522 /* If any of the character widths specified in the display table
15523 have changed, invalidate the width run cache. It's true that
15524 this may be a bit late to catch such changes, but the rest of
15525 redisplay goes (non-fatally) haywire when the display table is
15526 changed, so why should we worry about doing any better? */
15527 if (current_buffer->width_run_cache)
15528 {
15529 struct Lisp_Char_Table *disptab = buffer_display_table ();
15530
15531 if (! disptab_matches_widthtab
15532 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15533 {
15534 invalidate_region_cache (current_buffer,
15535 current_buffer->width_run_cache,
15536 BEG, Z);
15537 recompute_width_table (current_buffer, disptab);
15538 }
15539 }
15540
15541 /* If window-start is screwed up, choose a new one. */
15542 if (XMARKER (w->start)->buffer != current_buffer)
15543 goto recenter;
15544
15545 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15546
15547 /* If someone specified a new starting point but did not insist,
15548 check whether it can be used. */
15549 if (w->optional_new_start
15550 && CHARPOS (startp) >= BEGV
15551 && CHARPOS (startp) <= ZV)
15552 {
15553 w->optional_new_start = 0;
15554 start_display (&it, w, startp);
15555 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15556 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15557 if (IT_CHARPOS (it) == PT)
15558 w->force_start = 1;
15559 /* IT may overshoot PT if text at PT is invisible. */
15560 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15561 w->force_start = 1;
15562 }
15563
15564 force_start:
15565
15566 /* Handle case where place to start displaying has been specified,
15567 unless the specified location is outside the accessible range. */
15568 if (w->force_start || window_frozen_p (w))
15569 {
15570 /* We set this later on if we have to adjust point. */
15571 int new_vpos = -1;
15572
15573 w->force_start = 0;
15574 w->vscroll = 0;
15575 w->window_end_valid = 0;
15576
15577 /* Forget any recorded base line for line number display. */
15578 if (!buffer_unchanged_p)
15579 w->base_line_number = 0;
15580
15581 /* Redisplay the mode line. Select the buffer properly for that.
15582 Also, run the hook window-scroll-functions
15583 because we have scrolled. */
15584 /* Note, we do this after clearing force_start because
15585 if there's an error, it is better to forget about force_start
15586 than to get into an infinite loop calling the hook functions
15587 and having them get more errors. */
15588 if (!update_mode_line
15589 || ! NILP (Vwindow_scroll_functions))
15590 {
15591 update_mode_line = 1;
15592 w->update_mode_line = 1;
15593 startp = run_window_scroll_functions (window, startp);
15594 }
15595
15596 if (CHARPOS (startp) < BEGV)
15597 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15598 else if (CHARPOS (startp) > ZV)
15599 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15600
15601 /* Redisplay, then check if cursor has been set during the
15602 redisplay. Give up if new fonts were loaded. */
15603 /* We used to issue a CHECK_MARGINS argument to try_window here,
15604 but this causes scrolling to fail when point begins inside
15605 the scroll margin (bug#148) -- cyd */
15606 if (!try_window (window, startp, 0))
15607 {
15608 w->force_start = 1;
15609 clear_glyph_matrix (w->desired_matrix);
15610 goto need_larger_matrices;
15611 }
15612
15613 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15614 {
15615 /* If point does not appear, try to move point so it does
15616 appear. The desired matrix has been built above, so we
15617 can use it here. */
15618 new_vpos = window_box_height (w) / 2;
15619 }
15620
15621 if (!cursor_row_fully_visible_p (w, 0, 0))
15622 {
15623 /* Point does appear, but on a line partly visible at end of window.
15624 Move it back to a fully-visible line. */
15625 new_vpos = window_box_height (w);
15626 }
15627 else if (w->cursor.vpos >=0)
15628 {
15629 /* Some people insist on not letting point enter the scroll
15630 margin, even though this part handles windows that didn't
15631 scroll at all. */
15632 int window_total_lines
15633 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15634 int margin = min (scroll_margin, window_total_lines / 4);
15635 int pixel_margin = margin * frame_line_height;
15636 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15637
15638 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15639 below, which finds the row to move point to, advances by
15640 the Y coordinate of the _next_ row, see the definition of
15641 MATRIX_ROW_BOTTOM_Y. */
15642 if (w->cursor.vpos < margin + header_line)
15643 {
15644 w->cursor.vpos = -1;
15645 clear_glyph_matrix (w->desired_matrix);
15646 goto try_to_scroll;
15647 }
15648 else
15649 {
15650 int window_height = window_box_height (w);
15651
15652 if (header_line)
15653 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15654 if (w->cursor.y >= window_height - pixel_margin)
15655 {
15656 w->cursor.vpos = -1;
15657 clear_glyph_matrix (w->desired_matrix);
15658 goto try_to_scroll;
15659 }
15660 }
15661 }
15662
15663 /* If we need to move point for either of the above reasons,
15664 now actually do it. */
15665 if (new_vpos >= 0)
15666 {
15667 struct glyph_row *row;
15668
15669 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15670 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15671 ++row;
15672
15673 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15674 MATRIX_ROW_START_BYTEPOS (row));
15675
15676 if (w != XWINDOW (selected_window))
15677 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15678 else if (current_buffer == old)
15679 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15680
15681 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15682
15683 /* If we are highlighting the region, then we just changed
15684 the region, so redisplay to show it. */
15685 if (markpos_of_region () >= 0)
15686 {
15687 clear_glyph_matrix (w->desired_matrix);
15688 if (!try_window (window, startp, 0))
15689 goto need_larger_matrices;
15690 }
15691 }
15692
15693 #ifdef GLYPH_DEBUG
15694 debug_method_add (w, "forced window start");
15695 #endif
15696 goto done;
15697 }
15698
15699 /* Handle case where text has not changed, only point, and it has
15700 not moved off the frame, and we are not retrying after hscroll.
15701 (current_matrix_up_to_date_p is nonzero when retrying.) */
15702 if (current_matrix_up_to_date_p
15703 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15704 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15705 {
15706 switch (rc)
15707 {
15708 case CURSOR_MOVEMENT_SUCCESS:
15709 used_current_matrix_p = 1;
15710 goto done;
15711
15712 case CURSOR_MOVEMENT_MUST_SCROLL:
15713 goto try_to_scroll;
15714
15715 default:
15716 emacs_abort ();
15717 }
15718 }
15719 /* If current starting point was originally the beginning of a line
15720 but no longer is, find a new starting point. */
15721 else if (w->start_at_line_beg
15722 && !(CHARPOS (startp) <= BEGV
15723 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15724 {
15725 #ifdef GLYPH_DEBUG
15726 debug_method_add (w, "recenter 1");
15727 #endif
15728 goto recenter;
15729 }
15730
15731 /* Try scrolling with try_window_id. Value is > 0 if update has
15732 been done, it is -1 if we know that the same window start will
15733 not work. It is 0 if unsuccessful for some other reason. */
15734 else if ((tem = try_window_id (w)) != 0)
15735 {
15736 #ifdef GLYPH_DEBUG
15737 debug_method_add (w, "try_window_id %d", tem);
15738 #endif
15739
15740 if (f->fonts_changed)
15741 goto need_larger_matrices;
15742 if (tem > 0)
15743 goto done;
15744
15745 /* Otherwise try_window_id has returned -1 which means that we
15746 don't want the alternative below this comment to execute. */
15747 }
15748 else if (CHARPOS (startp) >= BEGV
15749 && CHARPOS (startp) <= ZV
15750 && PT >= CHARPOS (startp)
15751 && (CHARPOS (startp) < ZV
15752 /* Avoid starting at end of buffer. */
15753 || CHARPOS (startp) == BEGV
15754 || !window_outdated (w)))
15755 {
15756 int d1, d2, d3, d4, d5, d6;
15757
15758 /* If first window line is a continuation line, and window start
15759 is inside the modified region, but the first change is before
15760 current window start, we must select a new window start.
15761
15762 However, if this is the result of a down-mouse event (e.g. by
15763 extending the mouse-drag-overlay), we don't want to select a
15764 new window start, since that would change the position under
15765 the mouse, resulting in an unwanted mouse-movement rather
15766 than a simple mouse-click. */
15767 if (!w->start_at_line_beg
15768 && NILP (do_mouse_tracking)
15769 && CHARPOS (startp) > BEGV
15770 && CHARPOS (startp) > BEG + beg_unchanged
15771 && CHARPOS (startp) <= Z - end_unchanged
15772 /* Even if w->start_at_line_beg is nil, a new window may
15773 start at a line_beg, since that's how set_buffer_window
15774 sets it. So, we need to check the return value of
15775 compute_window_start_on_continuation_line. (See also
15776 bug#197). */
15777 && XMARKER (w->start)->buffer == current_buffer
15778 && compute_window_start_on_continuation_line (w)
15779 /* It doesn't make sense to force the window start like we
15780 do at label force_start if it is already known that point
15781 will not be visible in the resulting window, because
15782 doing so will move point from its correct position
15783 instead of scrolling the window to bring point into view.
15784 See bug#9324. */
15785 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15786 {
15787 w->force_start = 1;
15788 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15789 goto force_start;
15790 }
15791
15792 #ifdef GLYPH_DEBUG
15793 debug_method_add (w, "same window start");
15794 #endif
15795
15796 /* Try to redisplay starting at same place as before.
15797 If point has not moved off frame, accept the results. */
15798 if (!current_matrix_up_to_date_p
15799 /* Don't use try_window_reusing_current_matrix in this case
15800 because a window scroll function can have changed the
15801 buffer. */
15802 || !NILP (Vwindow_scroll_functions)
15803 || MINI_WINDOW_P (w)
15804 || !(used_current_matrix_p
15805 = try_window_reusing_current_matrix (w)))
15806 {
15807 IF_DEBUG (debug_method_add (w, "1"));
15808 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15809 /* -1 means we need to scroll.
15810 0 means we need new matrices, but fonts_changed
15811 is set in that case, so we will detect it below. */
15812 goto try_to_scroll;
15813 }
15814
15815 if (f->fonts_changed)
15816 goto need_larger_matrices;
15817
15818 if (w->cursor.vpos >= 0)
15819 {
15820 if (!just_this_one_p
15821 || current_buffer->clip_changed
15822 || BEG_UNCHANGED < CHARPOS (startp))
15823 /* Forget any recorded base line for line number display. */
15824 w->base_line_number = 0;
15825
15826 if (!cursor_row_fully_visible_p (w, 1, 0))
15827 {
15828 clear_glyph_matrix (w->desired_matrix);
15829 last_line_misfit = 1;
15830 }
15831 /* Drop through and scroll. */
15832 else
15833 goto done;
15834 }
15835 else
15836 clear_glyph_matrix (w->desired_matrix);
15837 }
15838
15839 try_to_scroll:
15840
15841 /* Redisplay the mode line. Select the buffer properly for that. */
15842 if (!update_mode_line)
15843 {
15844 update_mode_line = 1;
15845 w->update_mode_line = 1;
15846 }
15847
15848 /* Try to scroll by specified few lines. */
15849 if ((scroll_conservatively
15850 || emacs_scroll_step
15851 || temp_scroll_step
15852 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15853 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15854 && CHARPOS (startp) >= BEGV
15855 && CHARPOS (startp) <= ZV)
15856 {
15857 /* The function returns -1 if new fonts were loaded, 1 if
15858 successful, 0 if not successful. */
15859 int ss = try_scrolling (window, just_this_one_p,
15860 scroll_conservatively,
15861 emacs_scroll_step,
15862 temp_scroll_step, last_line_misfit);
15863 switch (ss)
15864 {
15865 case SCROLLING_SUCCESS:
15866 goto done;
15867
15868 case SCROLLING_NEED_LARGER_MATRICES:
15869 goto need_larger_matrices;
15870
15871 case SCROLLING_FAILED:
15872 break;
15873
15874 default:
15875 emacs_abort ();
15876 }
15877 }
15878
15879 /* Finally, just choose a place to start which positions point
15880 according to user preferences. */
15881
15882 recenter:
15883
15884 #ifdef GLYPH_DEBUG
15885 debug_method_add (w, "recenter");
15886 #endif
15887
15888 /* Forget any previously recorded base line for line number display. */
15889 if (!buffer_unchanged_p)
15890 w->base_line_number = 0;
15891
15892 /* Determine the window start relative to point. */
15893 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15894 it.current_y = it.last_visible_y;
15895 if (centering_position < 0)
15896 {
15897 int window_total_lines
15898 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15899 int margin =
15900 scroll_margin > 0
15901 ? min (scroll_margin, window_total_lines / 4)
15902 : 0;
15903 ptrdiff_t margin_pos = CHARPOS (startp);
15904 Lisp_Object aggressive;
15905 int scrolling_up;
15906
15907 /* If there is a scroll margin at the top of the window, find
15908 its character position. */
15909 if (margin
15910 /* Cannot call start_display if startp is not in the
15911 accessible region of the buffer. This can happen when we
15912 have just switched to a different buffer and/or changed
15913 its restriction. In that case, startp is initialized to
15914 the character position 1 (BEGV) because we did not yet
15915 have chance to display the buffer even once. */
15916 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15917 {
15918 struct it it1;
15919 void *it1data = NULL;
15920
15921 SAVE_IT (it1, it, it1data);
15922 start_display (&it1, w, startp);
15923 move_it_vertically (&it1, margin * frame_line_height);
15924 margin_pos = IT_CHARPOS (it1);
15925 RESTORE_IT (&it, &it, it1data);
15926 }
15927 scrolling_up = PT > margin_pos;
15928 aggressive =
15929 scrolling_up
15930 ? BVAR (current_buffer, scroll_up_aggressively)
15931 : BVAR (current_buffer, scroll_down_aggressively);
15932
15933 if (!MINI_WINDOW_P (w)
15934 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15935 {
15936 int pt_offset = 0;
15937
15938 /* Setting scroll-conservatively overrides
15939 scroll-*-aggressively. */
15940 if (!scroll_conservatively && NUMBERP (aggressive))
15941 {
15942 double float_amount = XFLOATINT (aggressive);
15943
15944 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15945 if (pt_offset == 0 && float_amount > 0)
15946 pt_offset = 1;
15947 if (pt_offset && margin > 0)
15948 margin -= 1;
15949 }
15950 /* Compute how much to move the window start backward from
15951 point so that point will be displayed where the user
15952 wants it. */
15953 if (scrolling_up)
15954 {
15955 centering_position = it.last_visible_y;
15956 if (pt_offset)
15957 centering_position -= pt_offset;
15958 centering_position -=
15959 frame_line_height * (1 + margin + (last_line_misfit != 0))
15960 + WINDOW_HEADER_LINE_HEIGHT (w);
15961 /* Don't let point enter the scroll margin near top of
15962 the window. */
15963 if (centering_position < margin * frame_line_height)
15964 centering_position = margin * frame_line_height;
15965 }
15966 else
15967 centering_position = margin * frame_line_height + pt_offset;
15968 }
15969 else
15970 /* Set the window start half the height of the window backward
15971 from point. */
15972 centering_position = window_box_height (w) / 2;
15973 }
15974 move_it_vertically_backward (&it, centering_position);
15975
15976 eassert (IT_CHARPOS (it) >= BEGV);
15977
15978 /* The function move_it_vertically_backward may move over more
15979 than the specified y-distance. If it->w is small, e.g. a
15980 mini-buffer window, we may end up in front of the window's
15981 display area. Start displaying at the start of the line
15982 containing PT in this case. */
15983 if (it.current_y <= 0)
15984 {
15985 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15986 move_it_vertically_backward (&it, 0);
15987 it.current_y = 0;
15988 }
15989
15990 it.current_x = it.hpos = 0;
15991
15992 /* Set the window start position here explicitly, to avoid an
15993 infinite loop in case the functions in window-scroll-functions
15994 get errors. */
15995 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15996
15997 /* Run scroll hooks. */
15998 startp = run_window_scroll_functions (window, it.current.pos);
15999
16000 /* Redisplay the window. */
16001 if (!current_matrix_up_to_date_p
16002 || windows_or_buffers_changed
16003 || f->cursor_type_changed
16004 /* Don't use try_window_reusing_current_matrix in this case
16005 because it can have changed the buffer. */
16006 || !NILP (Vwindow_scroll_functions)
16007 || !just_this_one_p
16008 || MINI_WINDOW_P (w)
16009 || !(used_current_matrix_p
16010 = try_window_reusing_current_matrix (w)))
16011 try_window (window, startp, 0);
16012
16013 /* If new fonts have been loaded (due to fontsets), give up. We
16014 have to start a new redisplay since we need to re-adjust glyph
16015 matrices. */
16016 if (f->fonts_changed)
16017 goto need_larger_matrices;
16018
16019 /* If cursor did not appear assume that the middle of the window is
16020 in the first line of the window. Do it again with the next line.
16021 (Imagine a window of height 100, displaying two lines of height
16022 60. Moving back 50 from it->last_visible_y will end in the first
16023 line.) */
16024 if (w->cursor.vpos < 0)
16025 {
16026 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16027 {
16028 clear_glyph_matrix (w->desired_matrix);
16029 move_it_by_lines (&it, 1);
16030 try_window (window, it.current.pos, 0);
16031 }
16032 else if (PT < IT_CHARPOS (it))
16033 {
16034 clear_glyph_matrix (w->desired_matrix);
16035 move_it_by_lines (&it, -1);
16036 try_window (window, it.current.pos, 0);
16037 }
16038 else
16039 {
16040 /* Not much we can do about it. */
16041 }
16042 }
16043
16044 /* Consider the following case: Window starts at BEGV, there is
16045 invisible, intangible text at BEGV, so that display starts at
16046 some point START > BEGV. It can happen that we are called with
16047 PT somewhere between BEGV and START. Try to handle that case. */
16048 if (w->cursor.vpos < 0)
16049 {
16050 struct glyph_row *row = w->current_matrix->rows;
16051 if (row->mode_line_p)
16052 ++row;
16053 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16054 }
16055
16056 if (!cursor_row_fully_visible_p (w, 0, 0))
16057 {
16058 /* If vscroll is enabled, disable it and try again. */
16059 if (w->vscroll)
16060 {
16061 w->vscroll = 0;
16062 clear_glyph_matrix (w->desired_matrix);
16063 goto recenter;
16064 }
16065
16066 /* Users who set scroll-conservatively to a large number want
16067 point just above/below the scroll margin. If we ended up
16068 with point's row partially visible, move the window start to
16069 make that row fully visible and out of the margin. */
16070 if (scroll_conservatively > SCROLL_LIMIT)
16071 {
16072 int window_total_lines
16073 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16074 int margin =
16075 scroll_margin > 0
16076 ? min (scroll_margin, window_total_lines / 4)
16077 : 0;
16078 int move_down = w->cursor.vpos >= window_total_lines / 2;
16079
16080 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16081 clear_glyph_matrix (w->desired_matrix);
16082 if (1 == try_window (window, it.current.pos,
16083 TRY_WINDOW_CHECK_MARGINS))
16084 goto done;
16085 }
16086
16087 /* If centering point failed to make the whole line visible,
16088 put point at the top instead. That has to make the whole line
16089 visible, if it can be done. */
16090 if (centering_position == 0)
16091 goto done;
16092
16093 clear_glyph_matrix (w->desired_matrix);
16094 centering_position = 0;
16095 goto recenter;
16096 }
16097
16098 done:
16099
16100 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16101 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16102 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16103
16104 /* Display the mode line, if we must. */
16105 if ((update_mode_line
16106 /* If window not full width, must redo its mode line
16107 if (a) the window to its side is being redone and
16108 (b) we do a frame-based redisplay. This is a consequence
16109 of how inverted lines are drawn in frame-based redisplay. */
16110 || (!just_this_one_p
16111 && !FRAME_WINDOW_P (f)
16112 && !WINDOW_FULL_WIDTH_P (w))
16113 /* Line number to display. */
16114 || w->base_line_pos > 0
16115 /* Column number is displayed and different from the one displayed. */
16116 || (w->column_number_displayed != -1
16117 && (w->column_number_displayed != current_column ())))
16118 /* This means that the window has a mode line. */
16119 && (WINDOW_WANTS_MODELINE_P (w)
16120 || WINDOW_WANTS_HEADER_LINE_P (w)))
16121 {
16122 display_mode_lines (w);
16123
16124 /* If mode line height has changed, arrange for a thorough
16125 immediate redisplay using the correct mode line height. */
16126 if (WINDOW_WANTS_MODELINE_P (w)
16127 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16128 {
16129 f->fonts_changed = 1;
16130 w->mode_line_height = -1;
16131 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16132 = DESIRED_MODE_LINE_HEIGHT (w);
16133 }
16134
16135 /* If header line height has changed, arrange for a thorough
16136 immediate redisplay using the correct header line height. */
16137 if (WINDOW_WANTS_HEADER_LINE_P (w)
16138 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16139 {
16140 f->fonts_changed = 1;
16141 w->header_line_height = -1;
16142 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16143 = DESIRED_HEADER_LINE_HEIGHT (w);
16144 }
16145
16146 if (f->fonts_changed)
16147 goto need_larger_matrices;
16148 }
16149
16150 if (!line_number_displayed && w->base_line_pos != -1)
16151 {
16152 w->base_line_pos = 0;
16153 w->base_line_number = 0;
16154 }
16155
16156 finish_menu_bars:
16157
16158 /* When we reach a frame's selected window, redo the frame's menu bar. */
16159 if (update_mode_line
16160 && EQ (FRAME_SELECTED_WINDOW (f), window))
16161 {
16162 int redisplay_menu_p = 0;
16163
16164 if (FRAME_WINDOW_P (f))
16165 {
16166 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16167 || defined (HAVE_NS) || defined (USE_GTK)
16168 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16169 #else
16170 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16171 #endif
16172 }
16173 else
16174 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16175
16176 if (redisplay_menu_p)
16177 display_menu_bar (w);
16178
16179 #ifdef HAVE_WINDOW_SYSTEM
16180 if (FRAME_WINDOW_P (f))
16181 {
16182 #if defined (USE_GTK) || defined (HAVE_NS)
16183 if (FRAME_EXTERNAL_TOOL_BAR (f))
16184 redisplay_tool_bar (f);
16185 #else
16186 if (WINDOWP (f->tool_bar_window)
16187 && (FRAME_TOOL_BAR_LINES (f) > 0
16188 || !NILP (Vauto_resize_tool_bars))
16189 && redisplay_tool_bar (f))
16190 ignore_mouse_drag_p = 1;
16191 #endif
16192 }
16193 #endif
16194 }
16195
16196 #ifdef HAVE_WINDOW_SYSTEM
16197 if (FRAME_WINDOW_P (f)
16198 && update_window_fringes (w, (just_this_one_p
16199 || (!used_current_matrix_p && !overlay_arrow_seen)
16200 || w->pseudo_window_p)))
16201 {
16202 update_begin (f);
16203 block_input ();
16204 if (draw_window_fringes (w, 1))
16205 x_draw_vertical_border (w);
16206 unblock_input ();
16207 update_end (f);
16208 }
16209 #endif /* HAVE_WINDOW_SYSTEM */
16210
16211 /* We go to this label, with fonts_changed set, if it is
16212 necessary to try again using larger glyph matrices.
16213 We have to redeem the scroll bar even in this case,
16214 because the loop in redisplay_internal expects that. */
16215 need_larger_matrices:
16216 ;
16217 finish_scroll_bars:
16218
16219 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16220 {
16221 /* Set the thumb's position and size. */
16222 set_vertical_scroll_bar (w);
16223
16224 /* Note that we actually used the scroll bar attached to this
16225 window, so it shouldn't be deleted at the end of redisplay. */
16226 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16227 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16228 }
16229
16230 /* Restore current_buffer and value of point in it. The window
16231 update may have changed the buffer, so first make sure `opoint'
16232 is still valid (Bug#6177). */
16233 if (CHARPOS (opoint) < BEGV)
16234 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16235 else if (CHARPOS (opoint) > ZV)
16236 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16237 else
16238 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16239
16240 set_buffer_internal_1 (old);
16241 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16242 shorter. This can be caused by log truncation in *Messages*. */
16243 if (CHARPOS (lpoint) <= ZV)
16244 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16245
16246 unbind_to (count, Qnil);
16247 }
16248
16249
16250 /* Build the complete desired matrix of WINDOW with a window start
16251 buffer position POS.
16252
16253 Value is 1 if successful. It is zero if fonts were loaded during
16254 redisplay which makes re-adjusting glyph matrices necessary, and -1
16255 if point would appear in the scroll margins.
16256 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16257 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16258 set in FLAGS.) */
16259
16260 int
16261 try_window (Lisp_Object window, struct text_pos pos, int flags)
16262 {
16263 struct window *w = XWINDOW (window);
16264 struct it it;
16265 struct glyph_row *last_text_row = NULL;
16266 struct frame *f = XFRAME (w->frame);
16267 int frame_line_height = default_line_pixel_height (w);
16268
16269 /* Make POS the new window start. */
16270 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16271
16272 /* Mark cursor position as unknown. No overlay arrow seen. */
16273 w->cursor.vpos = -1;
16274 overlay_arrow_seen = 0;
16275
16276 /* Initialize iterator and info to start at POS. */
16277 start_display (&it, w, pos);
16278
16279 /* Display all lines of W. */
16280 while (it.current_y < it.last_visible_y)
16281 {
16282 if (display_line (&it))
16283 last_text_row = it.glyph_row - 1;
16284 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16285 return 0;
16286 }
16287
16288 /* Don't let the cursor end in the scroll margins. */
16289 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16290 && !MINI_WINDOW_P (w))
16291 {
16292 int this_scroll_margin;
16293 int window_total_lines
16294 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16295
16296 if (scroll_margin > 0)
16297 {
16298 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16299 this_scroll_margin *= frame_line_height;
16300 }
16301 else
16302 this_scroll_margin = 0;
16303
16304 if ((w->cursor.y >= 0 /* not vscrolled */
16305 && w->cursor.y < this_scroll_margin
16306 && CHARPOS (pos) > BEGV
16307 && IT_CHARPOS (it) < ZV)
16308 /* rms: considering make_cursor_line_fully_visible_p here
16309 seems to give wrong results. We don't want to recenter
16310 when the last line is partly visible, we want to allow
16311 that case to be handled in the usual way. */
16312 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16313 {
16314 w->cursor.vpos = -1;
16315 clear_glyph_matrix (w->desired_matrix);
16316 return -1;
16317 }
16318 }
16319
16320 /* If bottom moved off end of frame, change mode line percentage. */
16321 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16322 w->update_mode_line = 1;
16323
16324 /* Set window_end_pos to the offset of the last character displayed
16325 on the window from the end of current_buffer. Set
16326 window_end_vpos to its row number. */
16327 if (last_text_row)
16328 {
16329 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16330 adjust_window_ends (w, last_text_row, 0);
16331 eassert
16332 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16333 w->window_end_vpos)));
16334 }
16335 else
16336 {
16337 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16338 w->window_end_pos = Z - ZV;
16339 w->window_end_vpos = 0;
16340 }
16341
16342 /* But that is not valid info until redisplay finishes. */
16343 w->window_end_valid = 0;
16344 return 1;
16345 }
16346
16347
16348 \f
16349 /************************************************************************
16350 Window redisplay reusing current matrix when buffer has not changed
16351 ************************************************************************/
16352
16353 /* Try redisplay of window W showing an unchanged buffer with a
16354 different window start than the last time it was displayed by
16355 reusing its current matrix. Value is non-zero if successful.
16356 W->start is the new window start. */
16357
16358 static int
16359 try_window_reusing_current_matrix (struct window *w)
16360 {
16361 struct frame *f = XFRAME (w->frame);
16362 struct glyph_row *bottom_row;
16363 struct it it;
16364 struct run run;
16365 struct text_pos start, new_start;
16366 int nrows_scrolled, i;
16367 struct glyph_row *last_text_row;
16368 struct glyph_row *last_reused_text_row;
16369 struct glyph_row *start_row;
16370 int start_vpos, min_y, max_y;
16371
16372 #ifdef GLYPH_DEBUG
16373 if (inhibit_try_window_reusing)
16374 return 0;
16375 #endif
16376
16377 if (/* This function doesn't handle terminal frames. */
16378 !FRAME_WINDOW_P (f)
16379 /* Don't try to reuse the display if windows have been split
16380 or such. */
16381 || windows_or_buffers_changed
16382 || f->cursor_type_changed)
16383 return 0;
16384
16385 /* Can't do this if region may have changed. */
16386 if (markpos_of_region () >= 0
16387 || w->region_showing
16388 || !NILP (Vshow_trailing_whitespace))
16389 return 0;
16390
16391 /* If top-line visibility has changed, give up. */
16392 if (WINDOW_WANTS_HEADER_LINE_P (w)
16393 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16394 return 0;
16395
16396 /* Give up if old or new display is scrolled vertically. We could
16397 make this function handle this, but right now it doesn't. */
16398 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16399 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16400 return 0;
16401
16402 /* The variable new_start now holds the new window start. The old
16403 start `start' can be determined from the current matrix. */
16404 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16405 start = start_row->minpos;
16406 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16407
16408 /* Clear the desired matrix for the display below. */
16409 clear_glyph_matrix (w->desired_matrix);
16410
16411 if (CHARPOS (new_start) <= CHARPOS (start))
16412 {
16413 /* Don't use this method if the display starts with an ellipsis
16414 displayed for invisible text. It's not easy to handle that case
16415 below, and it's certainly not worth the effort since this is
16416 not a frequent case. */
16417 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16418 return 0;
16419
16420 IF_DEBUG (debug_method_add (w, "twu1"));
16421
16422 /* Display up to a row that can be reused. The variable
16423 last_text_row is set to the last row displayed that displays
16424 text. Note that it.vpos == 0 if or if not there is a
16425 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16426 start_display (&it, w, new_start);
16427 w->cursor.vpos = -1;
16428 last_text_row = last_reused_text_row = NULL;
16429
16430 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16431 {
16432 /* If we have reached into the characters in the START row,
16433 that means the line boundaries have changed. So we
16434 can't start copying with the row START. Maybe it will
16435 work to start copying with the following row. */
16436 while (IT_CHARPOS (it) > CHARPOS (start))
16437 {
16438 /* Advance to the next row as the "start". */
16439 start_row++;
16440 start = start_row->minpos;
16441 /* If there are no more rows to try, or just one, give up. */
16442 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16443 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16444 || CHARPOS (start) == ZV)
16445 {
16446 clear_glyph_matrix (w->desired_matrix);
16447 return 0;
16448 }
16449
16450 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16451 }
16452 /* If we have reached alignment, we can copy the rest of the
16453 rows. */
16454 if (IT_CHARPOS (it) == CHARPOS (start)
16455 /* Don't accept "alignment" inside a display vector,
16456 since start_row could have started in the middle of
16457 that same display vector (thus their character
16458 positions match), and we have no way of telling if
16459 that is the case. */
16460 && it.current.dpvec_index < 0)
16461 break;
16462
16463 if (display_line (&it))
16464 last_text_row = it.glyph_row - 1;
16465
16466 }
16467
16468 /* A value of current_y < last_visible_y means that we stopped
16469 at the previous window start, which in turn means that we
16470 have at least one reusable row. */
16471 if (it.current_y < it.last_visible_y)
16472 {
16473 struct glyph_row *row;
16474
16475 /* IT.vpos always starts from 0; it counts text lines. */
16476 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16477
16478 /* Find PT if not already found in the lines displayed. */
16479 if (w->cursor.vpos < 0)
16480 {
16481 int dy = it.current_y - start_row->y;
16482
16483 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16484 row = row_containing_pos (w, PT, row, NULL, dy);
16485 if (row)
16486 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16487 dy, nrows_scrolled);
16488 else
16489 {
16490 clear_glyph_matrix (w->desired_matrix);
16491 return 0;
16492 }
16493 }
16494
16495 /* Scroll the display. Do it before the current matrix is
16496 changed. The problem here is that update has not yet
16497 run, i.e. part of the current matrix is not up to date.
16498 scroll_run_hook will clear the cursor, and use the
16499 current matrix to get the height of the row the cursor is
16500 in. */
16501 run.current_y = start_row->y;
16502 run.desired_y = it.current_y;
16503 run.height = it.last_visible_y - it.current_y;
16504
16505 if (run.height > 0 && run.current_y != run.desired_y)
16506 {
16507 update_begin (f);
16508 FRAME_RIF (f)->update_window_begin_hook (w);
16509 FRAME_RIF (f)->clear_window_mouse_face (w);
16510 FRAME_RIF (f)->scroll_run_hook (w, &run);
16511 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16512 update_end (f);
16513 }
16514
16515 /* Shift current matrix down by nrows_scrolled lines. */
16516 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16517 rotate_matrix (w->current_matrix,
16518 start_vpos,
16519 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16520 nrows_scrolled);
16521
16522 /* Disable lines that must be updated. */
16523 for (i = 0; i < nrows_scrolled; ++i)
16524 (start_row + i)->enabled_p = 0;
16525
16526 /* Re-compute Y positions. */
16527 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16528 max_y = it.last_visible_y;
16529 for (row = start_row + nrows_scrolled;
16530 row < bottom_row;
16531 ++row)
16532 {
16533 row->y = it.current_y;
16534 row->visible_height = row->height;
16535
16536 if (row->y < min_y)
16537 row->visible_height -= min_y - row->y;
16538 if (row->y + row->height > max_y)
16539 row->visible_height -= row->y + row->height - max_y;
16540 if (row->fringe_bitmap_periodic_p)
16541 row->redraw_fringe_bitmaps_p = 1;
16542
16543 it.current_y += row->height;
16544
16545 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16546 last_reused_text_row = row;
16547 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16548 break;
16549 }
16550
16551 /* Disable lines in the current matrix which are now
16552 below the window. */
16553 for (++row; row < bottom_row; ++row)
16554 row->enabled_p = row->mode_line_p = 0;
16555 }
16556
16557 /* Update window_end_pos etc.; last_reused_text_row is the last
16558 reused row from the current matrix containing text, if any.
16559 The value of last_text_row is the last displayed line
16560 containing text. */
16561 if (last_reused_text_row)
16562 adjust_window_ends (w, last_reused_text_row, 1);
16563 else if (last_text_row)
16564 adjust_window_ends (w, last_text_row, 0);
16565 else
16566 {
16567 /* This window must be completely empty. */
16568 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16569 w->window_end_pos = Z - ZV;
16570 w->window_end_vpos = 0;
16571 }
16572 w->window_end_valid = 0;
16573
16574 /* Update hint: don't try scrolling again in update_window. */
16575 w->desired_matrix->no_scrolling_p = 1;
16576
16577 #ifdef GLYPH_DEBUG
16578 debug_method_add (w, "try_window_reusing_current_matrix 1");
16579 #endif
16580 return 1;
16581 }
16582 else if (CHARPOS (new_start) > CHARPOS (start))
16583 {
16584 struct glyph_row *pt_row, *row;
16585 struct glyph_row *first_reusable_row;
16586 struct glyph_row *first_row_to_display;
16587 int dy;
16588 int yb = window_text_bottom_y (w);
16589
16590 /* Find the row starting at new_start, if there is one. Don't
16591 reuse a partially visible line at the end. */
16592 first_reusable_row = start_row;
16593 while (first_reusable_row->enabled_p
16594 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16595 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16596 < CHARPOS (new_start)))
16597 ++first_reusable_row;
16598
16599 /* Give up if there is no row to reuse. */
16600 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16601 || !first_reusable_row->enabled_p
16602 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16603 != CHARPOS (new_start)))
16604 return 0;
16605
16606 /* We can reuse fully visible rows beginning with
16607 first_reusable_row to the end of the window. Set
16608 first_row_to_display to the first row that cannot be reused.
16609 Set pt_row to the row containing point, if there is any. */
16610 pt_row = NULL;
16611 for (first_row_to_display = first_reusable_row;
16612 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16613 ++first_row_to_display)
16614 {
16615 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16616 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16617 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16618 && first_row_to_display->ends_at_zv_p
16619 && pt_row == NULL)))
16620 pt_row = first_row_to_display;
16621 }
16622
16623 /* Start displaying at the start of first_row_to_display. */
16624 eassert (first_row_to_display->y < yb);
16625 init_to_row_start (&it, w, first_row_to_display);
16626
16627 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16628 - start_vpos);
16629 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16630 - nrows_scrolled);
16631 it.current_y = (first_row_to_display->y - first_reusable_row->y
16632 + WINDOW_HEADER_LINE_HEIGHT (w));
16633
16634 /* Display lines beginning with first_row_to_display in the
16635 desired matrix. Set last_text_row to the last row displayed
16636 that displays text. */
16637 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16638 if (pt_row == NULL)
16639 w->cursor.vpos = -1;
16640 last_text_row = NULL;
16641 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16642 if (display_line (&it))
16643 last_text_row = it.glyph_row - 1;
16644
16645 /* If point is in a reused row, adjust y and vpos of the cursor
16646 position. */
16647 if (pt_row)
16648 {
16649 w->cursor.vpos -= nrows_scrolled;
16650 w->cursor.y -= first_reusable_row->y - start_row->y;
16651 }
16652
16653 /* Give up if point isn't in a row displayed or reused. (This
16654 also handles the case where w->cursor.vpos < nrows_scrolled
16655 after the calls to display_line, which can happen with scroll
16656 margins. See bug#1295.) */
16657 if (w->cursor.vpos < 0)
16658 {
16659 clear_glyph_matrix (w->desired_matrix);
16660 return 0;
16661 }
16662
16663 /* Scroll the display. */
16664 run.current_y = first_reusable_row->y;
16665 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16666 run.height = it.last_visible_y - run.current_y;
16667 dy = run.current_y - run.desired_y;
16668
16669 if (run.height)
16670 {
16671 update_begin (f);
16672 FRAME_RIF (f)->update_window_begin_hook (w);
16673 FRAME_RIF (f)->clear_window_mouse_face (w);
16674 FRAME_RIF (f)->scroll_run_hook (w, &run);
16675 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16676 update_end (f);
16677 }
16678
16679 /* Adjust Y positions of reused rows. */
16680 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16681 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16682 max_y = it.last_visible_y;
16683 for (row = first_reusable_row; row < first_row_to_display; ++row)
16684 {
16685 row->y -= dy;
16686 row->visible_height = row->height;
16687 if (row->y < min_y)
16688 row->visible_height -= min_y - row->y;
16689 if (row->y + row->height > max_y)
16690 row->visible_height -= row->y + row->height - max_y;
16691 if (row->fringe_bitmap_periodic_p)
16692 row->redraw_fringe_bitmaps_p = 1;
16693 }
16694
16695 /* Scroll the current matrix. */
16696 eassert (nrows_scrolled > 0);
16697 rotate_matrix (w->current_matrix,
16698 start_vpos,
16699 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16700 -nrows_scrolled);
16701
16702 /* Disable rows not reused. */
16703 for (row -= nrows_scrolled; row < bottom_row; ++row)
16704 row->enabled_p = 0;
16705
16706 /* Point may have moved to a different line, so we cannot assume that
16707 the previous cursor position is valid; locate the correct row. */
16708 if (pt_row)
16709 {
16710 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16711 row < bottom_row
16712 && PT >= MATRIX_ROW_END_CHARPOS (row)
16713 && !row->ends_at_zv_p;
16714 row++)
16715 {
16716 w->cursor.vpos++;
16717 w->cursor.y = row->y;
16718 }
16719 if (row < bottom_row)
16720 {
16721 /* Can't simply scan the row for point with
16722 bidi-reordered glyph rows. Let set_cursor_from_row
16723 figure out where to put the cursor, and if it fails,
16724 give up. */
16725 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16726 {
16727 if (!set_cursor_from_row (w, row, w->current_matrix,
16728 0, 0, 0, 0))
16729 {
16730 clear_glyph_matrix (w->desired_matrix);
16731 return 0;
16732 }
16733 }
16734 else
16735 {
16736 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16737 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16738
16739 for (; glyph < end
16740 && (!BUFFERP (glyph->object)
16741 || glyph->charpos < PT);
16742 glyph++)
16743 {
16744 w->cursor.hpos++;
16745 w->cursor.x += glyph->pixel_width;
16746 }
16747 }
16748 }
16749 }
16750
16751 /* Adjust window end. A null value of last_text_row means that
16752 the window end is in reused rows which in turn means that
16753 only its vpos can have changed. */
16754 if (last_text_row)
16755 adjust_window_ends (w, last_text_row, 0);
16756 else
16757 w->window_end_vpos -= nrows_scrolled;
16758
16759 w->window_end_valid = 0;
16760 w->desired_matrix->no_scrolling_p = 1;
16761
16762 #ifdef GLYPH_DEBUG
16763 debug_method_add (w, "try_window_reusing_current_matrix 2");
16764 #endif
16765 return 1;
16766 }
16767
16768 return 0;
16769 }
16770
16771
16772 \f
16773 /************************************************************************
16774 Window redisplay reusing current matrix when buffer has changed
16775 ************************************************************************/
16776
16777 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16778 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16779 ptrdiff_t *, ptrdiff_t *);
16780 static struct glyph_row *
16781 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16782 struct glyph_row *);
16783
16784
16785 /* Return the last row in MATRIX displaying text. If row START is
16786 non-null, start searching with that row. IT gives the dimensions
16787 of the display. Value is null if matrix is empty; otherwise it is
16788 a pointer to the row found. */
16789
16790 static struct glyph_row *
16791 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16792 struct glyph_row *start)
16793 {
16794 struct glyph_row *row, *row_found;
16795
16796 /* Set row_found to the last row in IT->w's current matrix
16797 displaying text. The loop looks funny but think of partially
16798 visible lines. */
16799 row_found = NULL;
16800 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16801 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16802 {
16803 eassert (row->enabled_p);
16804 row_found = row;
16805 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16806 break;
16807 ++row;
16808 }
16809
16810 return row_found;
16811 }
16812
16813
16814 /* Return the last row in the current matrix of W that is not affected
16815 by changes at the start of current_buffer that occurred since W's
16816 current matrix was built. Value is null if no such row exists.
16817
16818 BEG_UNCHANGED us the number of characters unchanged at the start of
16819 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16820 first changed character in current_buffer. Characters at positions <
16821 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16822 when the current matrix was built. */
16823
16824 static struct glyph_row *
16825 find_last_unchanged_at_beg_row (struct window *w)
16826 {
16827 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16828 struct glyph_row *row;
16829 struct glyph_row *row_found = NULL;
16830 int yb = window_text_bottom_y (w);
16831
16832 /* Find the last row displaying unchanged text. */
16833 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16834 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16835 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16836 ++row)
16837 {
16838 if (/* If row ends before first_changed_pos, it is unchanged,
16839 except in some case. */
16840 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16841 /* When row ends in ZV and we write at ZV it is not
16842 unchanged. */
16843 && !row->ends_at_zv_p
16844 /* When first_changed_pos is the end of a continued line,
16845 row is not unchanged because it may be no longer
16846 continued. */
16847 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16848 && (row->continued_p
16849 || row->exact_window_width_line_p))
16850 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16851 needs to be recomputed, so don't consider this row as
16852 unchanged. This happens when the last line was
16853 bidi-reordered and was killed immediately before this
16854 redisplay cycle. In that case, ROW->end stores the
16855 buffer position of the first visual-order character of
16856 the killed text, which is now beyond ZV. */
16857 && CHARPOS (row->end.pos) <= ZV)
16858 row_found = row;
16859
16860 /* Stop if last visible row. */
16861 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16862 break;
16863 }
16864
16865 return row_found;
16866 }
16867
16868
16869 /* Find the first glyph row in the current matrix of W that is not
16870 affected by changes at the end of current_buffer since the
16871 time W's current matrix was built.
16872
16873 Return in *DELTA the number of chars by which buffer positions in
16874 unchanged text at the end of current_buffer must be adjusted.
16875
16876 Return in *DELTA_BYTES the corresponding number of bytes.
16877
16878 Value is null if no such row exists, i.e. all rows are affected by
16879 changes. */
16880
16881 static struct glyph_row *
16882 find_first_unchanged_at_end_row (struct window *w,
16883 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16884 {
16885 struct glyph_row *row;
16886 struct glyph_row *row_found = NULL;
16887
16888 *delta = *delta_bytes = 0;
16889
16890 /* Display must not have been paused, otherwise the current matrix
16891 is not up to date. */
16892 eassert (w->window_end_valid);
16893
16894 /* A value of window_end_pos >= END_UNCHANGED means that the window
16895 end is in the range of changed text. If so, there is no
16896 unchanged row at the end of W's current matrix. */
16897 if (w->window_end_pos >= END_UNCHANGED)
16898 return NULL;
16899
16900 /* Set row to the last row in W's current matrix displaying text. */
16901 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16902
16903 /* If matrix is entirely empty, no unchanged row exists. */
16904 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16905 {
16906 /* The value of row is the last glyph row in the matrix having a
16907 meaningful buffer position in it. The end position of row
16908 corresponds to window_end_pos. This allows us to translate
16909 buffer positions in the current matrix to current buffer
16910 positions for characters not in changed text. */
16911 ptrdiff_t Z_old =
16912 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16913 ptrdiff_t Z_BYTE_old =
16914 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16915 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16916 struct glyph_row *first_text_row
16917 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16918
16919 *delta = Z - Z_old;
16920 *delta_bytes = Z_BYTE - Z_BYTE_old;
16921
16922 /* Set last_unchanged_pos to the buffer position of the last
16923 character in the buffer that has not been changed. Z is the
16924 index + 1 of the last character in current_buffer, i.e. by
16925 subtracting END_UNCHANGED we get the index of the last
16926 unchanged character, and we have to add BEG to get its buffer
16927 position. */
16928 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16929 last_unchanged_pos_old = last_unchanged_pos - *delta;
16930
16931 /* Search backward from ROW for a row displaying a line that
16932 starts at a minimum position >= last_unchanged_pos_old. */
16933 for (; row > first_text_row; --row)
16934 {
16935 /* This used to abort, but it can happen.
16936 It is ok to just stop the search instead here. KFS. */
16937 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16938 break;
16939
16940 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16941 row_found = row;
16942 }
16943 }
16944
16945 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16946
16947 return row_found;
16948 }
16949
16950
16951 /* Make sure that glyph rows in the current matrix of window W
16952 reference the same glyph memory as corresponding rows in the
16953 frame's frame matrix. This function is called after scrolling W's
16954 current matrix on a terminal frame in try_window_id and
16955 try_window_reusing_current_matrix. */
16956
16957 static void
16958 sync_frame_with_window_matrix_rows (struct window *w)
16959 {
16960 struct frame *f = XFRAME (w->frame);
16961 struct glyph_row *window_row, *window_row_end, *frame_row;
16962
16963 /* Preconditions: W must be a leaf window and full-width. Its frame
16964 must have a frame matrix. */
16965 eassert (BUFFERP (w->contents));
16966 eassert (WINDOW_FULL_WIDTH_P (w));
16967 eassert (!FRAME_WINDOW_P (f));
16968
16969 /* If W is a full-width window, glyph pointers in W's current matrix
16970 have, by definition, to be the same as glyph pointers in the
16971 corresponding frame matrix. Note that frame matrices have no
16972 marginal areas (see build_frame_matrix). */
16973 window_row = w->current_matrix->rows;
16974 window_row_end = window_row + w->current_matrix->nrows;
16975 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16976 while (window_row < window_row_end)
16977 {
16978 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16979 struct glyph *end = window_row->glyphs[LAST_AREA];
16980
16981 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16982 frame_row->glyphs[TEXT_AREA] = start;
16983 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16984 frame_row->glyphs[LAST_AREA] = end;
16985
16986 /* Disable frame rows whose corresponding window rows have
16987 been disabled in try_window_id. */
16988 if (!window_row->enabled_p)
16989 frame_row->enabled_p = 0;
16990
16991 ++window_row, ++frame_row;
16992 }
16993 }
16994
16995
16996 /* Find the glyph row in window W containing CHARPOS. Consider all
16997 rows between START and END (not inclusive). END null means search
16998 all rows to the end of the display area of W. Value is the row
16999 containing CHARPOS or null. */
17000
17001 struct glyph_row *
17002 row_containing_pos (struct window *w, ptrdiff_t charpos,
17003 struct glyph_row *start, struct glyph_row *end, int dy)
17004 {
17005 struct glyph_row *row = start;
17006 struct glyph_row *best_row = NULL;
17007 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17008 int last_y;
17009
17010 /* If we happen to start on a header-line, skip that. */
17011 if (row->mode_line_p)
17012 ++row;
17013
17014 if ((end && row >= end) || !row->enabled_p)
17015 return NULL;
17016
17017 last_y = window_text_bottom_y (w) - dy;
17018
17019 while (1)
17020 {
17021 /* Give up if we have gone too far. */
17022 if (end && row >= end)
17023 return NULL;
17024 /* This formerly returned if they were equal.
17025 I think that both quantities are of a "last plus one" type;
17026 if so, when they are equal, the row is within the screen. -- rms. */
17027 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17028 return NULL;
17029
17030 /* If it is in this row, return this row. */
17031 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17032 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17033 /* The end position of a row equals the start
17034 position of the next row. If CHARPOS is there, we
17035 would rather consider it displayed in the next
17036 line, except when this line ends in ZV. */
17037 && !row_for_charpos_p (row, charpos)))
17038 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17039 {
17040 struct glyph *g;
17041
17042 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17043 || (!best_row && !row->continued_p))
17044 return row;
17045 /* In bidi-reordered rows, there could be several rows whose
17046 edges surround CHARPOS, all of these rows belonging to
17047 the same continued line. We need to find the row which
17048 fits CHARPOS the best. */
17049 for (g = row->glyphs[TEXT_AREA];
17050 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17051 g++)
17052 {
17053 if (!STRINGP (g->object))
17054 {
17055 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17056 {
17057 mindif = eabs (g->charpos - charpos);
17058 best_row = row;
17059 /* Exact match always wins. */
17060 if (mindif == 0)
17061 return best_row;
17062 }
17063 }
17064 }
17065 }
17066 else if (best_row && !row->continued_p)
17067 return best_row;
17068 ++row;
17069 }
17070 }
17071
17072
17073 /* Try to redisplay window W by reusing its existing display. W's
17074 current matrix must be up to date when this function is called,
17075 i.e. window_end_valid must be nonzero.
17076
17077 Value is
17078
17079 1 if display has been updated
17080 0 if otherwise unsuccessful
17081 -1 if redisplay with same window start is known not to succeed
17082
17083 The following steps are performed:
17084
17085 1. Find the last row in the current matrix of W that is not
17086 affected by changes at the start of current_buffer. If no such row
17087 is found, give up.
17088
17089 2. Find the first row in W's current matrix that is not affected by
17090 changes at the end of current_buffer. Maybe there is no such row.
17091
17092 3. Display lines beginning with the row + 1 found in step 1 to the
17093 row found in step 2 or, if step 2 didn't find a row, to the end of
17094 the window.
17095
17096 4. If cursor is not known to appear on the window, give up.
17097
17098 5. If display stopped at the row found in step 2, scroll the
17099 display and current matrix as needed.
17100
17101 6. Maybe display some lines at the end of W, if we must. This can
17102 happen under various circumstances, like a partially visible line
17103 becoming fully visible, or because newly displayed lines are displayed
17104 in smaller font sizes.
17105
17106 7. Update W's window end information. */
17107
17108 static int
17109 try_window_id (struct window *w)
17110 {
17111 struct frame *f = XFRAME (w->frame);
17112 struct glyph_matrix *current_matrix = w->current_matrix;
17113 struct glyph_matrix *desired_matrix = w->desired_matrix;
17114 struct glyph_row *last_unchanged_at_beg_row;
17115 struct glyph_row *first_unchanged_at_end_row;
17116 struct glyph_row *row;
17117 struct glyph_row *bottom_row;
17118 int bottom_vpos;
17119 struct it it;
17120 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17121 int dvpos, dy;
17122 struct text_pos start_pos;
17123 struct run run;
17124 int first_unchanged_at_end_vpos = 0;
17125 struct glyph_row *last_text_row, *last_text_row_at_end;
17126 struct text_pos start;
17127 ptrdiff_t first_changed_charpos, last_changed_charpos;
17128
17129 #ifdef GLYPH_DEBUG
17130 if (inhibit_try_window_id)
17131 return 0;
17132 #endif
17133
17134 /* This is handy for debugging. */
17135 #if 0
17136 #define GIVE_UP(X) \
17137 do { \
17138 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17139 return 0; \
17140 } while (0)
17141 #else
17142 #define GIVE_UP(X) return 0
17143 #endif
17144
17145 SET_TEXT_POS_FROM_MARKER (start, w->start);
17146
17147 /* Don't use this for mini-windows because these can show
17148 messages and mini-buffers, and we don't handle that here. */
17149 if (MINI_WINDOW_P (w))
17150 GIVE_UP (1);
17151
17152 /* This flag is used to prevent redisplay optimizations. */
17153 if (windows_or_buffers_changed || f->cursor_type_changed)
17154 GIVE_UP (2);
17155
17156 /* Verify that narrowing has not changed.
17157 Also verify that we were not told to prevent redisplay optimizations.
17158 It would be nice to further
17159 reduce the number of cases where this prevents try_window_id. */
17160 if (current_buffer->clip_changed
17161 || current_buffer->prevent_redisplay_optimizations_p)
17162 GIVE_UP (3);
17163
17164 /* Window must either use window-based redisplay or be full width. */
17165 if (!FRAME_WINDOW_P (f)
17166 && (!FRAME_LINE_INS_DEL_OK (f)
17167 || !WINDOW_FULL_WIDTH_P (w)))
17168 GIVE_UP (4);
17169
17170 /* Give up if point is known NOT to appear in W. */
17171 if (PT < CHARPOS (start))
17172 GIVE_UP (5);
17173
17174 /* Another way to prevent redisplay optimizations. */
17175 if (w->last_modified == 0)
17176 GIVE_UP (6);
17177
17178 /* Verify that window is not hscrolled. */
17179 if (w->hscroll != 0)
17180 GIVE_UP (7);
17181
17182 /* Verify that display wasn't paused. */
17183 if (!w->window_end_valid)
17184 GIVE_UP (8);
17185
17186 /* Can't use this if highlighting a region because a cursor movement
17187 will do more than just set the cursor. */
17188 if (markpos_of_region () >= 0)
17189 GIVE_UP (9);
17190
17191 /* Likewise if highlighting trailing whitespace. */
17192 if (!NILP (Vshow_trailing_whitespace))
17193 GIVE_UP (11);
17194
17195 /* Likewise if showing a region. */
17196 if (w->region_showing)
17197 GIVE_UP (10);
17198
17199 /* Can't use this if overlay arrow position and/or string have
17200 changed. */
17201 if (overlay_arrows_changed_p ())
17202 GIVE_UP (12);
17203
17204 /* When word-wrap is on, adding a space to the first word of a
17205 wrapped line can change the wrap position, altering the line
17206 above it. It might be worthwhile to handle this more
17207 intelligently, but for now just redisplay from scratch. */
17208 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17209 GIVE_UP (21);
17210
17211 /* Under bidi reordering, adding or deleting a character in the
17212 beginning of a paragraph, before the first strong directional
17213 character, can change the base direction of the paragraph (unless
17214 the buffer specifies a fixed paragraph direction), which will
17215 require to redisplay the whole paragraph. It might be worthwhile
17216 to find the paragraph limits and widen the range of redisplayed
17217 lines to that, but for now just give up this optimization and
17218 redisplay from scratch. */
17219 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17220 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17221 GIVE_UP (22);
17222
17223 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17224 only if buffer has really changed. The reason is that the gap is
17225 initially at Z for freshly visited files. The code below would
17226 set end_unchanged to 0 in that case. */
17227 if (MODIFF > SAVE_MODIFF
17228 /* This seems to happen sometimes after saving a buffer. */
17229 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17230 {
17231 if (GPT - BEG < BEG_UNCHANGED)
17232 BEG_UNCHANGED = GPT - BEG;
17233 if (Z - GPT < END_UNCHANGED)
17234 END_UNCHANGED = Z - GPT;
17235 }
17236
17237 /* The position of the first and last character that has been changed. */
17238 first_changed_charpos = BEG + BEG_UNCHANGED;
17239 last_changed_charpos = Z - END_UNCHANGED;
17240
17241 /* If window starts after a line end, and the last change is in
17242 front of that newline, then changes don't affect the display.
17243 This case happens with stealth-fontification. Note that although
17244 the display is unchanged, glyph positions in the matrix have to
17245 be adjusted, of course. */
17246 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17247 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17248 && ((last_changed_charpos < CHARPOS (start)
17249 && CHARPOS (start) == BEGV)
17250 || (last_changed_charpos < CHARPOS (start) - 1
17251 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17252 {
17253 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17254 struct glyph_row *r0;
17255
17256 /* Compute how many chars/bytes have been added to or removed
17257 from the buffer. */
17258 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17259 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17260 Z_delta = Z - Z_old;
17261 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17262
17263 /* Give up if PT is not in the window. Note that it already has
17264 been checked at the start of try_window_id that PT is not in
17265 front of the window start. */
17266 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17267 GIVE_UP (13);
17268
17269 /* If window start is unchanged, we can reuse the whole matrix
17270 as is, after adjusting glyph positions. No need to compute
17271 the window end again, since its offset from Z hasn't changed. */
17272 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17273 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17274 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17275 /* PT must not be in a partially visible line. */
17276 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17277 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17278 {
17279 /* Adjust positions in the glyph matrix. */
17280 if (Z_delta || Z_delta_bytes)
17281 {
17282 struct glyph_row *r1
17283 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17284 increment_matrix_positions (w->current_matrix,
17285 MATRIX_ROW_VPOS (r0, current_matrix),
17286 MATRIX_ROW_VPOS (r1, current_matrix),
17287 Z_delta, Z_delta_bytes);
17288 }
17289
17290 /* Set the cursor. */
17291 row = row_containing_pos (w, PT, r0, NULL, 0);
17292 if (row)
17293 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17294 return 1;
17295 }
17296 }
17297
17298 /* Handle the case that changes are all below what is displayed in
17299 the window, and that PT is in the window. This shortcut cannot
17300 be taken if ZV is visible in the window, and text has been added
17301 there that is visible in the window. */
17302 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17303 /* ZV is not visible in the window, or there are no
17304 changes at ZV, actually. */
17305 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17306 || first_changed_charpos == last_changed_charpos))
17307 {
17308 struct glyph_row *r0;
17309
17310 /* Give up if PT is not in the window. Note that it already has
17311 been checked at the start of try_window_id that PT is not in
17312 front of the window start. */
17313 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17314 GIVE_UP (14);
17315
17316 /* If window start is unchanged, we can reuse the whole matrix
17317 as is, without changing glyph positions since no text has
17318 been added/removed in front of the window end. */
17319 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17320 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17321 /* PT must not be in a partially visible line. */
17322 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17323 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17324 {
17325 /* We have to compute the window end anew since text
17326 could have been added/removed after it. */
17327 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17328 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17329
17330 /* Set the cursor. */
17331 row = row_containing_pos (w, PT, r0, NULL, 0);
17332 if (row)
17333 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17334 return 2;
17335 }
17336 }
17337
17338 /* Give up if window start is in the changed area.
17339
17340 The condition used to read
17341
17342 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17343
17344 but why that was tested escapes me at the moment. */
17345 if (CHARPOS (start) >= first_changed_charpos
17346 && CHARPOS (start) <= last_changed_charpos)
17347 GIVE_UP (15);
17348
17349 /* Check that window start agrees with the start of the first glyph
17350 row in its current matrix. Check this after we know the window
17351 start is not in changed text, otherwise positions would not be
17352 comparable. */
17353 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17354 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17355 GIVE_UP (16);
17356
17357 /* Give up if the window ends in strings. Overlay strings
17358 at the end are difficult to handle, so don't try. */
17359 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17360 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17361 GIVE_UP (20);
17362
17363 /* Compute the position at which we have to start displaying new
17364 lines. Some of the lines at the top of the window might be
17365 reusable because they are not displaying changed text. Find the
17366 last row in W's current matrix not affected by changes at the
17367 start of current_buffer. Value is null if changes start in the
17368 first line of window. */
17369 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17370 if (last_unchanged_at_beg_row)
17371 {
17372 /* Avoid starting to display in the middle of a character, a TAB
17373 for instance. This is easier than to set up the iterator
17374 exactly, and it's not a frequent case, so the additional
17375 effort wouldn't really pay off. */
17376 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17377 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17378 && last_unchanged_at_beg_row > w->current_matrix->rows)
17379 --last_unchanged_at_beg_row;
17380
17381 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17382 GIVE_UP (17);
17383
17384 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17385 GIVE_UP (18);
17386 start_pos = it.current.pos;
17387
17388 /* Start displaying new lines in the desired matrix at the same
17389 vpos we would use in the current matrix, i.e. below
17390 last_unchanged_at_beg_row. */
17391 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17392 current_matrix);
17393 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17394 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17395
17396 eassert (it.hpos == 0 && it.current_x == 0);
17397 }
17398 else
17399 {
17400 /* There are no reusable lines at the start of the window.
17401 Start displaying in the first text line. */
17402 start_display (&it, w, start);
17403 it.vpos = it.first_vpos;
17404 start_pos = it.current.pos;
17405 }
17406
17407 /* Find the first row that is not affected by changes at the end of
17408 the buffer. Value will be null if there is no unchanged row, in
17409 which case we must redisplay to the end of the window. delta
17410 will be set to the value by which buffer positions beginning with
17411 first_unchanged_at_end_row have to be adjusted due to text
17412 changes. */
17413 first_unchanged_at_end_row
17414 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17415 IF_DEBUG (debug_delta = delta);
17416 IF_DEBUG (debug_delta_bytes = delta_bytes);
17417
17418 /* Set stop_pos to the buffer position up to which we will have to
17419 display new lines. If first_unchanged_at_end_row != NULL, this
17420 is the buffer position of the start of the line displayed in that
17421 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17422 that we don't stop at a buffer position. */
17423 stop_pos = 0;
17424 if (first_unchanged_at_end_row)
17425 {
17426 eassert (last_unchanged_at_beg_row == NULL
17427 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17428
17429 /* If this is a continuation line, move forward to the next one
17430 that isn't. Changes in lines above affect this line.
17431 Caution: this may move first_unchanged_at_end_row to a row
17432 not displaying text. */
17433 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17434 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17435 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17436 < it.last_visible_y))
17437 ++first_unchanged_at_end_row;
17438
17439 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17440 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17441 >= it.last_visible_y))
17442 first_unchanged_at_end_row = NULL;
17443 else
17444 {
17445 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17446 + delta);
17447 first_unchanged_at_end_vpos
17448 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17449 eassert (stop_pos >= Z - END_UNCHANGED);
17450 }
17451 }
17452 else if (last_unchanged_at_beg_row == NULL)
17453 GIVE_UP (19);
17454
17455
17456 #ifdef GLYPH_DEBUG
17457
17458 /* Either there is no unchanged row at the end, or the one we have
17459 now displays text. This is a necessary condition for the window
17460 end pos calculation at the end of this function. */
17461 eassert (first_unchanged_at_end_row == NULL
17462 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17463
17464 debug_last_unchanged_at_beg_vpos
17465 = (last_unchanged_at_beg_row
17466 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17467 : -1);
17468 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17469
17470 #endif /* GLYPH_DEBUG */
17471
17472
17473 /* Display new lines. Set last_text_row to the last new line
17474 displayed which has text on it, i.e. might end up as being the
17475 line where the window_end_vpos is. */
17476 w->cursor.vpos = -1;
17477 last_text_row = NULL;
17478 overlay_arrow_seen = 0;
17479 while (it.current_y < it.last_visible_y
17480 && !f->fonts_changed
17481 && (first_unchanged_at_end_row == NULL
17482 || IT_CHARPOS (it) < stop_pos))
17483 {
17484 if (display_line (&it))
17485 last_text_row = it.glyph_row - 1;
17486 }
17487
17488 if (f->fonts_changed)
17489 return -1;
17490
17491
17492 /* Compute differences in buffer positions, y-positions etc. for
17493 lines reused at the bottom of the window. Compute what we can
17494 scroll. */
17495 if (first_unchanged_at_end_row
17496 /* No lines reused because we displayed everything up to the
17497 bottom of the window. */
17498 && it.current_y < it.last_visible_y)
17499 {
17500 dvpos = (it.vpos
17501 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17502 current_matrix));
17503 dy = it.current_y - first_unchanged_at_end_row->y;
17504 run.current_y = first_unchanged_at_end_row->y;
17505 run.desired_y = run.current_y + dy;
17506 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17507 }
17508 else
17509 {
17510 delta = delta_bytes = dvpos = dy
17511 = run.current_y = run.desired_y = run.height = 0;
17512 first_unchanged_at_end_row = NULL;
17513 }
17514 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17515
17516
17517 /* Find the cursor if not already found. We have to decide whether
17518 PT will appear on this window (it sometimes doesn't, but this is
17519 not a very frequent case.) This decision has to be made before
17520 the current matrix is altered. A value of cursor.vpos < 0 means
17521 that PT is either in one of the lines beginning at
17522 first_unchanged_at_end_row or below the window. Don't care for
17523 lines that might be displayed later at the window end; as
17524 mentioned, this is not a frequent case. */
17525 if (w->cursor.vpos < 0)
17526 {
17527 /* Cursor in unchanged rows at the top? */
17528 if (PT < CHARPOS (start_pos)
17529 && last_unchanged_at_beg_row)
17530 {
17531 row = row_containing_pos (w, PT,
17532 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17533 last_unchanged_at_beg_row + 1, 0);
17534 if (row)
17535 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17536 }
17537
17538 /* Start from first_unchanged_at_end_row looking for PT. */
17539 else if (first_unchanged_at_end_row)
17540 {
17541 row = row_containing_pos (w, PT - delta,
17542 first_unchanged_at_end_row, NULL, 0);
17543 if (row)
17544 set_cursor_from_row (w, row, w->current_matrix, delta,
17545 delta_bytes, dy, dvpos);
17546 }
17547
17548 /* Give up if cursor was not found. */
17549 if (w->cursor.vpos < 0)
17550 {
17551 clear_glyph_matrix (w->desired_matrix);
17552 return -1;
17553 }
17554 }
17555
17556 /* Don't let the cursor end in the scroll margins. */
17557 {
17558 int this_scroll_margin, cursor_height;
17559 int frame_line_height = default_line_pixel_height (w);
17560 int window_total_lines
17561 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17562
17563 this_scroll_margin =
17564 max (0, min (scroll_margin, window_total_lines / 4));
17565 this_scroll_margin *= frame_line_height;
17566 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17567
17568 if ((w->cursor.y < this_scroll_margin
17569 && CHARPOS (start) > BEGV)
17570 /* Old redisplay didn't take scroll margin into account at the bottom,
17571 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17572 || (w->cursor.y + (make_cursor_line_fully_visible_p
17573 ? cursor_height + this_scroll_margin
17574 : 1)) > it.last_visible_y)
17575 {
17576 w->cursor.vpos = -1;
17577 clear_glyph_matrix (w->desired_matrix);
17578 return -1;
17579 }
17580 }
17581
17582 /* Scroll the display. Do it before changing the current matrix so
17583 that xterm.c doesn't get confused about where the cursor glyph is
17584 found. */
17585 if (dy && run.height)
17586 {
17587 update_begin (f);
17588
17589 if (FRAME_WINDOW_P (f))
17590 {
17591 FRAME_RIF (f)->update_window_begin_hook (w);
17592 FRAME_RIF (f)->clear_window_mouse_face (w);
17593 FRAME_RIF (f)->scroll_run_hook (w, &run);
17594 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17595 }
17596 else
17597 {
17598 /* Terminal frame. In this case, dvpos gives the number of
17599 lines to scroll by; dvpos < 0 means scroll up. */
17600 int from_vpos
17601 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17602 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17603 int end = (WINDOW_TOP_EDGE_LINE (w)
17604 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17605 + window_internal_height (w));
17606
17607 #if defined (HAVE_GPM) || defined (MSDOS)
17608 x_clear_window_mouse_face (w);
17609 #endif
17610 /* Perform the operation on the screen. */
17611 if (dvpos > 0)
17612 {
17613 /* Scroll last_unchanged_at_beg_row to the end of the
17614 window down dvpos lines. */
17615 set_terminal_window (f, end);
17616
17617 /* On dumb terminals delete dvpos lines at the end
17618 before inserting dvpos empty lines. */
17619 if (!FRAME_SCROLL_REGION_OK (f))
17620 ins_del_lines (f, end - dvpos, -dvpos);
17621
17622 /* Insert dvpos empty lines in front of
17623 last_unchanged_at_beg_row. */
17624 ins_del_lines (f, from, dvpos);
17625 }
17626 else if (dvpos < 0)
17627 {
17628 /* Scroll up last_unchanged_at_beg_vpos to the end of
17629 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17630 set_terminal_window (f, end);
17631
17632 /* Delete dvpos lines in front of
17633 last_unchanged_at_beg_vpos. ins_del_lines will set
17634 the cursor to the given vpos and emit |dvpos| delete
17635 line sequences. */
17636 ins_del_lines (f, from + dvpos, dvpos);
17637
17638 /* On a dumb terminal insert dvpos empty lines at the
17639 end. */
17640 if (!FRAME_SCROLL_REGION_OK (f))
17641 ins_del_lines (f, end + dvpos, -dvpos);
17642 }
17643
17644 set_terminal_window (f, 0);
17645 }
17646
17647 update_end (f);
17648 }
17649
17650 /* Shift reused rows of the current matrix to the right position.
17651 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17652 text. */
17653 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17654 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17655 if (dvpos < 0)
17656 {
17657 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17658 bottom_vpos, dvpos);
17659 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17660 bottom_vpos);
17661 }
17662 else if (dvpos > 0)
17663 {
17664 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17665 bottom_vpos, dvpos);
17666 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17667 first_unchanged_at_end_vpos + dvpos);
17668 }
17669
17670 /* For frame-based redisplay, make sure that current frame and window
17671 matrix are in sync with respect to glyph memory. */
17672 if (!FRAME_WINDOW_P (f))
17673 sync_frame_with_window_matrix_rows (w);
17674
17675 /* Adjust buffer positions in reused rows. */
17676 if (delta || delta_bytes)
17677 increment_matrix_positions (current_matrix,
17678 first_unchanged_at_end_vpos + dvpos,
17679 bottom_vpos, delta, delta_bytes);
17680
17681 /* Adjust Y positions. */
17682 if (dy)
17683 shift_glyph_matrix (w, current_matrix,
17684 first_unchanged_at_end_vpos + dvpos,
17685 bottom_vpos, dy);
17686
17687 if (first_unchanged_at_end_row)
17688 {
17689 first_unchanged_at_end_row += dvpos;
17690 if (first_unchanged_at_end_row->y >= it.last_visible_y
17691 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17692 first_unchanged_at_end_row = NULL;
17693 }
17694
17695 /* If scrolling up, there may be some lines to display at the end of
17696 the window. */
17697 last_text_row_at_end = NULL;
17698 if (dy < 0)
17699 {
17700 /* Scrolling up can leave for example a partially visible line
17701 at the end of the window to be redisplayed. */
17702 /* Set last_row to the glyph row in the current matrix where the
17703 window end line is found. It has been moved up or down in
17704 the matrix by dvpos. */
17705 int last_vpos = w->window_end_vpos + dvpos;
17706 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17707
17708 /* If last_row is the window end line, it should display text. */
17709 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17710
17711 /* If window end line was partially visible before, begin
17712 displaying at that line. Otherwise begin displaying with the
17713 line following it. */
17714 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17715 {
17716 init_to_row_start (&it, w, last_row);
17717 it.vpos = last_vpos;
17718 it.current_y = last_row->y;
17719 }
17720 else
17721 {
17722 init_to_row_end (&it, w, last_row);
17723 it.vpos = 1 + last_vpos;
17724 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17725 ++last_row;
17726 }
17727
17728 /* We may start in a continuation line. If so, we have to
17729 get the right continuation_lines_width and current_x. */
17730 it.continuation_lines_width = last_row->continuation_lines_width;
17731 it.hpos = it.current_x = 0;
17732
17733 /* Display the rest of the lines at the window end. */
17734 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17735 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17736 {
17737 /* Is it always sure that the display agrees with lines in
17738 the current matrix? I don't think so, so we mark rows
17739 displayed invalid in the current matrix by setting their
17740 enabled_p flag to zero. */
17741 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17742 if (display_line (&it))
17743 last_text_row_at_end = it.glyph_row - 1;
17744 }
17745 }
17746
17747 /* Update window_end_pos and window_end_vpos. */
17748 if (first_unchanged_at_end_row && !last_text_row_at_end)
17749 {
17750 /* Window end line if one of the preserved rows from the current
17751 matrix. Set row to the last row displaying text in current
17752 matrix starting at first_unchanged_at_end_row, after
17753 scrolling. */
17754 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17755 row = find_last_row_displaying_text (w->current_matrix, &it,
17756 first_unchanged_at_end_row);
17757 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17758 adjust_window_ends (w, row, 1);
17759 eassert (w->window_end_bytepos >= 0);
17760 IF_DEBUG (debug_method_add (w, "A"));
17761 }
17762 else if (last_text_row_at_end)
17763 {
17764 adjust_window_ends (w, last_text_row_at_end, 0);
17765 eassert (w->window_end_bytepos >= 0);
17766 IF_DEBUG (debug_method_add (w, "B"));
17767 }
17768 else if (last_text_row)
17769 {
17770 /* We have displayed either to the end of the window or at the
17771 end of the window, i.e. the last row with text is to be found
17772 in the desired matrix. */
17773 adjust_window_ends (w, last_text_row, 0);
17774 eassert (w->window_end_bytepos >= 0);
17775 }
17776 else if (first_unchanged_at_end_row == NULL
17777 && last_text_row == NULL
17778 && last_text_row_at_end == NULL)
17779 {
17780 /* Displayed to end of window, but no line containing text was
17781 displayed. Lines were deleted at the end of the window. */
17782 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17783 int vpos = w->window_end_vpos;
17784 struct glyph_row *current_row = current_matrix->rows + vpos;
17785 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17786
17787 for (row = NULL;
17788 row == NULL && vpos >= first_vpos;
17789 --vpos, --current_row, --desired_row)
17790 {
17791 if (desired_row->enabled_p)
17792 {
17793 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17794 row = desired_row;
17795 }
17796 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17797 row = current_row;
17798 }
17799
17800 eassert (row != NULL);
17801 w->window_end_vpos = vpos + 1;
17802 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17803 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17804 eassert (w->window_end_bytepos >= 0);
17805 IF_DEBUG (debug_method_add (w, "C"));
17806 }
17807 else
17808 emacs_abort ();
17809
17810 IF_DEBUG (debug_end_pos = w->window_end_pos;
17811 debug_end_vpos = w->window_end_vpos);
17812
17813 /* Record that display has not been completed. */
17814 w->window_end_valid = 0;
17815 w->desired_matrix->no_scrolling_p = 1;
17816 return 3;
17817
17818 #undef GIVE_UP
17819 }
17820
17821
17822 \f
17823 /***********************************************************************
17824 More debugging support
17825 ***********************************************************************/
17826
17827 #ifdef GLYPH_DEBUG
17828
17829 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17830 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17831 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17832
17833
17834 /* Dump the contents of glyph matrix MATRIX on stderr.
17835
17836 GLYPHS 0 means don't show glyph contents.
17837 GLYPHS 1 means show glyphs in short form
17838 GLYPHS > 1 means show glyphs in long form. */
17839
17840 void
17841 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17842 {
17843 int i;
17844 for (i = 0; i < matrix->nrows; ++i)
17845 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17846 }
17847
17848
17849 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17850 the glyph row and area where the glyph comes from. */
17851
17852 void
17853 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17854 {
17855 if (glyph->type == CHAR_GLYPH
17856 || glyph->type == GLYPHLESS_GLYPH)
17857 {
17858 fprintf (stderr,
17859 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17860 glyph - row->glyphs[TEXT_AREA],
17861 (glyph->type == CHAR_GLYPH
17862 ? 'C'
17863 : 'G'),
17864 glyph->charpos,
17865 (BUFFERP (glyph->object)
17866 ? 'B'
17867 : (STRINGP (glyph->object)
17868 ? 'S'
17869 : (INTEGERP (glyph->object)
17870 ? '0'
17871 : '-'))),
17872 glyph->pixel_width,
17873 glyph->u.ch,
17874 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17875 ? glyph->u.ch
17876 : '.'),
17877 glyph->face_id,
17878 glyph->left_box_line_p,
17879 glyph->right_box_line_p);
17880 }
17881 else if (glyph->type == STRETCH_GLYPH)
17882 {
17883 fprintf (stderr,
17884 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17885 glyph - row->glyphs[TEXT_AREA],
17886 'S',
17887 glyph->charpos,
17888 (BUFFERP (glyph->object)
17889 ? 'B'
17890 : (STRINGP (glyph->object)
17891 ? 'S'
17892 : (INTEGERP (glyph->object)
17893 ? '0'
17894 : '-'))),
17895 glyph->pixel_width,
17896 0,
17897 ' ',
17898 glyph->face_id,
17899 glyph->left_box_line_p,
17900 glyph->right_box_line_p);
17901 }
17902 else if (glyph->type == IMAGE_GLYPH)
17903 {
17904 fprintf (stderr,
17905 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17906 glyph - row->glyphs[TEXT_AREA],
17907 'I',
17908 glyph->charpos,
17909 (BUFFERP (glyph->object)
17910 ? 'B'
17911 : (STRINGP (glyph->object)
17912 ? 'S'
17913 : (INTEGERP (glyph->object)
17914 ? '0'
17915 : '-'))),
17916 glyph->pixel_width,
17917 glyph->u.img_id,
17918 '.',
17919 glyph->face_id,
17920 glyph->left_box_line_p,
17921 glyph->right_box_line_p);
17922 }
17923 else if (glyph->type == COMPOSITE_GLYPH)
17924 {
17925 fprintf (stderr,
17926 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17927 glyph - row->glyphs[TEXT_AREA],
17928 '+',
17929 glyph->charpos,
17930 (BUFFERP (glyph->object)
17931 ? 'B'
17932 : (STRINGP (glyph->object)
17933 ? 'S'
17934 : (INTEGERP (glyph->object)
17935 ? '0'
17936 : '-'))),
17937 glyph->pixel_width,
17938 glyph->u.cmp.id);
17939 if (glyph->u.cmp.automatic)
17940 fprintf (stderr,
17941 "[%d-%d]",
17942 glyph->slice.cmp.from, glyph->slice.cmp.to);
17943 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17944 glyph->face_id,
17945 glyph->left_box_line_p,
17946 glyph->right_box_line_p);
17947 }
17948 }
17949
17950
17951 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17952 GLYPHS 0 means don't show glyph contents.
17953 GLYPHS 1 means show glyphs in short form
17954 GLYPHS > 1 means show glyphs in long form. */
17955
17956 void
17957 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17958 {
17959 if (glyphs != 1)
17960 {
17961 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17962 fprintf (stderr, "==============================================================================\n");
17963
17964 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17965 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17966 vpos,
17967 MATRIX_ROW_START_CHARPOS (row),
17968 MATRIX_ROW_END_CHARPOS (row),
17969 row->used[TEXT_AREA],
17970 row->contains_overlapping_glyphs_p,
17971 row->enabled_p,
17972 row->truncated_on_left_p,
17973 row->truncated_on_right_p,
17974 row->continued_p,
17975 MATRIX_ROW_CONTINUATION_LINE_P (row),
17976 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17977 row->ends_at_zv_p,
17978 row->fill_line_p,
17979 row->ends_in_middle_of_char_p,
17980 row->starts_in_middle_of_char_p,
17981 row->mouse_face_p,
17982 row->x,
17983 row->y,
17984 row->pixel_width,
17985 row->height,
17986 row->visible_height,
17987 row->ascent,
17988 row->phys_ascent);
17989 /* The next 3 lines should align to "Start" in the header. */
17990 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17991 row->end.overlay_string_index,
17992 row->continuation_lines_width);
17993 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17994 CHARPOS (row->start.string_pos),
17995 CHARPOS (row->end.string_pos));
17996 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17997 row->end.dpvec_index);
17998 }
17999
18000 if (glyphs > 1)
18001 {
18002 int area;
18003
18004 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18005 {
18006 struct glyph *glyph = row->glyphs[area];
18007 struct glyph *glyph_end = glyph + row->used[area];
18008
18009 /* Glyph for a line end in text. */
18010 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18011 ++glyph_end;
18012
18013 if (glyph < glyph_end)
18014 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18015
18016 for (; glyph < glyph_end; ++glyph)
18017 dump_glyph (row, glyph, area);
18018 }
18019 }
18020 else if (glyphs == 1)
18021 {
18022 int area;
18023
18024 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18025 {
18026 char *s = alloca (row->used[area] + 4);
18027 int i;
18028
18029 for (i = 0; i < row->used[area]; ++i)
18030 {
18031 struct glyph *glyph = row->glyphs[area] + i;
18032 if (i == row->used[area] - 1
18033 && area == TEXT_AREA
18034 && INTEGERP (glyph->object)
18035 && glyph->type == CHAR_GLYPH
18036 && glyph->u.ch == ' ')
18037 {
18038 strcpy (&s[i], "[\\n]");
18039 i += 4;
18040 }
18041 else if (glyph->type == CHAR_GLYPH
18042 && glyph->u.ch < 0x80
18043 && glyph->u.ch >= ' ')
18044 s[i] = glyph->u.ch;
18045 else
18046 s[i] = '.';
18047 }
18048
18049 s[i] = '\0';
18050 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18051 }
18052 }
18053 }
18054
18055
18056 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18057 Sdump_glyph_matrix, 0, 1, "p",
18058 doc: /* Dump the current matrix of the selected window to stderr.
18059 Shows contents of glyph row structures. With non-nil
18060 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18061 glyphs in short form, otherwise show glyphs in long form. */)
18062 (Lisp_Object glyphs)
18063 {
18064 struct window *w = XWINDOW (selected_window);
18065 struct buffer *buffer = XBUFFER (w->contents);
18066
18067 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18068 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18069 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18070 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18071 fprintf (stderr, "=============================================\n");
18072 dump_glyph_matrix (w->current_matrix,
18073 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18074 return Qnil;
18075 }
18076
18077
18078 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18079 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18080 (void)
18081 {
18082 struct frame *f = XFRAME (selected_frame);
18083 dump_glyph_matrix (f->current_matrix, 1);
18084 return Qnil;
18085 }
18086
18087
18088 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18089 doc: /* Dump glyph row ROW to stderr.
18090 GLYPH 0 means don't dump glyphs.
18091 GLYPH 1 means dump glyphs in short form.
18092 GLYPH > 1 or omitted means dump glyphs in long form. */)
18093 (Lisp_Object row, Lisp_Object glyphs)
18094 {
18095 struct glyph_matrix *matrix;
18096 EMACS_INT vpos;
18097
18098 CHECK_NUMBER (row);
18099 matrix = XWINDOW (selected_window)->current_matrix;
18100 vpos = XINT (row);
18101 if (vpos >= 0 && vpos < matrix->nrows)
18102 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18103 vpos,
18104 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18105 return Qnil;
18106 }
18107
18108
18109 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18110 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18111 GLYPH 0 means don't dump glyphs.
18112 GLYPH 1 means dump glyphs in short form.
18113 GLYPH > 1 or omitted means dump glyphs in long form. */)
18114 (Lisp_Object row, Lisp_Object glyphs)
18115 {
18116 struct frame *sf = SELECTED_FRAME ();
18117 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18118 EMACS_INT vpos;
18119
18120 CHECK_NUMBER (row);
18121 vpos = XINT (row);
18122 if (vpos >= 0 && vpos < m->nrows)
18123 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18124 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18125 return Qnil;
18126 }
18127
18128
18129 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18130 doc: /* Toggle tracing of redisplay.
18131 With ARG, turn tracing on if and only if ARG is positive. */)
18132 (Lisp_Object arg)
18133 {
18134 if (NILP (arg))
18135 trace_redisplay_p = !trace_redisplay_p;
18136 else
18137 {
18138 arg = Fprefix_numeric_value (arg);
18139 trace_redisplay_p = XINT (arg) > 0;
18140 }
18141
18142 return Qnil;
18143 }
18144
18145
18146 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18147 doc: /* Like `format', but print result to stderr.
18148 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18149 (ptrdiff_t nargs, Lisp_Object *args)
18150 {
18151 Lisp_Object s = Fformat (nargs, args);
18152 fprintf (stderr, "%s", SDATA (s));
18153 return Qnil;
18154 }
18155
18156 #endif /* GLYPH_DEBUG */
18157
18158
18159 \f
18160 /***********************************************************************
18161 Building Desired Matrix Rows
18162 ***********************************************************************/
18163
18164 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18165 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18166
18167 static struct glyph_row *
18168 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18169 {
18170 struct frame *f = XFRAME (WINDOW_FRAME (w));
18171 struct buffer *buffer = XBUFFER (w->contents);
18172 struct buffer *old = current_buffer;
18173 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18174 int arrow_len = SCHARS (overlay_arrow_string);
18175 const unsigned char *arrow_end = arrow_string + arrow_len;
18176 const unsigned char *p;
18177 struct it it;
18178 bool multibyte_p;
18179 int n_glyphs_before;
18180
18181 set_buffer_temp (buffer);
18182 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18183 it.glyph_row->used[TEXT_AREA] = 0;
18184 SET_TEXT_POS (it.position, 0, 0);
18185
18186 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18187 p = arrow_string;
18188 while (p < arrow_end)
18189 {
18190 Lisp_Object face, ilisp;
18191
18192 /* Get the next character. */
18193 if (multibyte_p)
18194 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18195 else
18196 {
18197 it.c = it.char_to_display = *p, it.len = 1;
18198 if (! ASCII_CHAR_P (it.c))
18199 it.char_to_display = BYTE8_TO_CHAR (it.c);
18200 }
18201 p += it.len;
18202
18203 /* Get its face. */
18204 ilisp = make_number (p - arrow_string);
18205 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18206 it.face_id = compute_char_face (f, it.char_to_display, face);
18207
18208 /* Compute its width, get its glyphs. */
18209 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18210 SET_TEXT_POS (it.position, -1, -1);
18211 PRODUCE_GLYPHS (&it);
18212
18213 /* If this character doesn't fit any more in the line, we have
18214 to remove some glyphs. */
18215 if (it.current_x > it.last_visible_x)
18216 {
18217 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18218 break;
18219 }
18220 }
18221
18222 set_buffer_temp (old);
18223 return it.glyph_row;
18224 }
18225
18226
18227 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18228 glyphs to insert is determined by produce_special_glyphs. */
18229
18230 static void
18231 insert_left_trunc_glyphs (struct it *it)
18232 {
18233 struct it truncate_it;
18234 struct glyph *from, *end, *to, *toend;
18235
18236 eassert (!FRAME_WINDOW_P (it->f)
18237 || (!it->glyph_row->reversed_p
18238 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18239 || (it->glyph_row->reversed_p
18240 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18241
18242 /* Get the truncation glyphs. */
18243 truncate_it = *it;
18244 truncate_it.current_x = 0;
18245 truncate_it.face_id = DEFAULT_FACE_ID;
18246 truncate_it.glyph_row = &scratch_glyph_row;
18247 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18248 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18249 truncate_it.object = make_number (0);
18250 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18251
18252 /* Overwrite glyphs from IT with truncation glyphs. */
18253 if (!it->glyph_row->reversed_p)
18254 {
18255 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18256
18257 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18258 end = from + tused;
18259 to = it->glyph_row->glyphs[TEXT_AREA];
18260 toend = to + it->glyph_row->used[TEXT_AREA];
18261 if (FRAME_WINDOW_P (it->f))
18262 {
18263 /* On GUI frames, when variable-size fonts are displayed,
18264 the truncation glyphs may need more pixels than the row's
18265 glyphs they overwrite. We overwrite more glyphs to free
18266 enough screen real estate, and enlarge the stretch glyph
18267 on the right (see display_line), if there is one, to
18268 preserve the screen position of the truncation glyphs on
18269 the right. */
18270 int w = 0;
18271 struct glyph *g = to;
18272 short used;
18273
18274 /* The first glyph could be partially visible, in which case
18275 it->glyph_row->x will be negative. But we want the left
18276 truncation glyphs to be aligned at the left margin of the
18277 window, so we override the x coordinate at which the row
18278 will begin. */
18279 it->glyph_row->x = 0;
18280 while (g < toend && w < it->truncation_pixel_width)
18281 {
18282 w += g->pixel_width;
18283 ++g;
18284 }
18285 if (g - to - tused > 0)
18286 {
18287 memmove (to + tused, g, (toend - g) * sizeof(*g));
18288 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18289 }
18290 used = it->glyph_row->used[TEXT_AREA];
18291 if (it->glyph_row->truncated_on_right_p
18292 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18293 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18294 == STRETCH_GLYPH)
18295 {
18296 int extra = w - it->truncation_pixel_width;
18297
18298 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18299 }
18300 }
18301
18302 while (from < end)
18303 *to++ = *from++;
18304
18305 /* There may be padding glyphs left over. Overwrite them too. */
18306 if (!FRAME_WINDOW_P (it->f))
18307 {
18308 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18309 {
18310 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18311 while (from < end)
18312 *to++ = *from++;
18313 }
18314 }
18315
18316 if (to > toend)
18317 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18318 }
18319 else
18320 {
18321 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18322
18323 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18324 that back to front. */
18325 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18326 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18327 toend = it->glyph_row->glyphs[TEXT_AREA];
18328 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18329 if (FRAME_WINDOW_P (it->f))
18330 {
18331 int w = 0;
18332 struct glyph *g = to;
18333
18334 while (g >= toend && w < it->truncation_pixel_width)
18335 {
18336 w += g->pixel_width;
18337 --g;
18338 }
18339 if (to - g - tused > 0)
18340 to = g + tused;
18341 if (it->glyph_row->truncated_on_right_p
18342 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18343 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18344 {
18345 int extra = w - it->truncation_pixel_width;
18346
18347 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18348 }
18349 }
18350
18351 while (from >= end && to >= toend)
18352 *to-- = *from--;
18353 if (!FRAME_WINDOW_P (it->f))
18354 {
18355 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18356 {
18357 from =
18358 truncate_it.glyph_row->glyphs[TEXT_AREA]
18359 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18360 while (from >= end && to >= toend)
18361 *to-- = *from--;
18362 }
18363 }
18364 if (from >= end)
18365 {
18366 /* Need to free some room before prepending additional
18367 glyphs. */
18368 int move_by = from - end + 1;
18369 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18370 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18371
18372 for ( ; g >= g0; g--)
18373 g[move_by] = *g;
18374 while (from >= end)
18375 *to-- = *from--;
18376 it->glyph_row->used[TEXT_AREA] += move_by;
18377 }
18378 }
18379 }
18380
18381 /* Compute the hash code for ROW. */
18382 unsigned
18383 row_hash (struct glyph_row *row)
18384 {
18385 int area, k;
18386 unsigned hashval = 0;
18387
18388 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18389 for (k = 0; k < row->used[area]; ++k)
18390 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18391 + row->glyphs[area][k].u.val
18392 + row->glyphs[area][k].face_id
18393 + row->glyphs[area][k].padding_p
18394 + (row->glyphs[area][k].type << 2));
18395
18396 return hashval;
18397 }
18398
18399 /* Compute the pixel height and width of IT->glyph_row.
18400
18401 Most of the time, ascent and height of a display line will be equal
18402 to the max_ascent and max_height values of the display iterator
18403 structure. This is not the case if
18404
18405 1. We hit ZV without displaying anything. In this case, max_ascent
18406 and max_height will be zero.
18407
18408 2. We have some glyphs that don't contribute to the line height.
18409 (The glyph row flag contributes_to_line_height_p is for future
18410 pixmap extensions).
18411
18412 The first case is easily covered by using default values because in
18413 these cases, the line height does not really matter, except that it
18414 must not be zero. */
18415
18416 static void
18417 compute_line_metrics (struct it *it)
18418 {
18419 struct glyph_row *row = it->glyph_row;
18420
18421 if (FRAME_WINDOW_P (it->f))
18422 {
18423 int i, min_y, max_y;
18424
18425 /* The line may consist of one space only, that was added to
18426 place the cursor on it. If so, the row's height hasn't been
18427 computed yet. */
18428 if (row->height == 0)
18429 {
18430 if (it->max_ascent + it->max_descent == 0)
18431 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18432 row->ascent = it->max_ascent;
18433 row->height = it->max_ascent + it->max_descent;
18434 row->phys_ascent = it->max_phys_ascent;
18435 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18436 row->extra_line_spacing = it->max_extra_line_spacing;
18437 }
18438
18439 /* Compute the width of this line. */
18440 row->pixel_width = row->x;
18441 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18442 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18443
18444 eassert (row->pixel_width >= 0);
18445 eassert (row->ascent >= 0 && row->height > 0);
18446
18447 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18448 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18449
18450 /* If first line's physical ascent is larger than its logical
18451 ascent, use the physical ascent, and make the row taller.
18452 This makes accented characters fully visible. */
18453 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18454 && row->phys_ascent > row->ascent)
18455 {
18456 row->height += row->phys_ascent - row->ascent;
18457 row->ascent = row->phys_ascent;
18458 }
18459
18460 /* Compute how much of the line is visible. */
18461 row->visible_height = row->height;
18462
18463 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18464 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18465
18466 if (row->y < min_y)
18467 row->visible_height -= min_y - row->y;
18468 if (row->y + row->height > max_y)
18469 row->visible_height -= row->y + row->height - max_y;
18470 }
18471 else
18472 {
18473 row->pixel_width = row->used[TEXT_AREA];
18474 if (row->continued_p)
18475 row->pixel_width -= it->continuation_pixel_width;
18476 else if (row->truncated_on_right_p)
18477 row->pixel_width -= it->truncation_pixel_width;
18478 row->ascent = row->phys_ascent = 0;
18479 row->height = row->phys_height = row->visible_height = 1;
18480 row->extra_line_spacing = 0;
18481 }
18482
18483 /* Compute a hash code for this row. */
18484 row->hash = row_hash (row);
18485
18486 it->max_ascent = it->max_descent = 0;
18487 it->max_phys_ascent = it->max_phys_descent = 0;
18488 }
18489
18490
18491 /* Append one space to the glyph row of iterator IT if doing a
18492 window-based redisplay. The space has the same face as
18493 IT->face_id. Value is non-zero if a space was added.
18494
18495 This function is called to make sure that there is always one glyph
18496 at the end of a glyph row that the cursor can be set on under
18497 window-systems. (If there weren't such a glyph we would not know
18498 how wide and tall a box cursor should be displayed).
18499
18500 At the same time this space let's a nicely handle clearing to the
18501 end of the line if the row ends in italic text. */
18502
18503 static int
18504 append_space_for_newline (struct it *it, int default_face_p)
18505 {
18506 if (FRAME_WINDOW_P (it->f))
18507 {
18508 int n = it->glyph_row->used[TEXT_AREA];
18509
18510 if (it->glyph_row->glyphs[TEXT_AREA] + n
18511 < it->glyph_row->glyphs[1 + TEXT_AREA])
18512 {
18513 /* Save some values that must not be changed.
18514 Must save IT->c and IT->len because otherwise
18515 ITERATOR_AT_END_P wouldn't work anymore after
18516 append_space_for_newline has been called. */
18517 enum display_element_type saved_what = it->what;
18518 int saved_c = it->c, saved_len = it->len;
18519 int saved_char_to_display = it->char_to_display;
18520 int saved_x = it->current_x;
18521 int saved_face_id = it->face_id;
18522 int saved_box_end = it->end_of_box_run_p;
18523 struct text_pos saved_pos;
18524 Lisp_Object saved_object;
18525 struct face *face;
18526
18527 saved_object = it->object;
18528 saved_pos = it->position;
18529
18530 it->what = IT_CHARACTER;
18531 memset (&it->position, 0, sizeof it->position);
18532 it->object = make_number (0);
18533 it->c = it->char_to_display = ' ';
18534 it->len = 1;
18535
18536 /* If the default face was remapped, be sure to use the
18537 remapped face for the appended newline. */
18538 if (default_face_p)
18539 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18540 else if (it->face_before_selective_p)
18541 it->face_id = it->saved_face_id;
18542 face = FACE_FROM_ID (it->f, it->face_id);
18543 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18544 /* In R2L rows, we will prepend a stretch glyph that will
18545 have the end_of_box_run_p flag set for it, so there's no
18546 need for the appended newline glyph to have that flag
18547 set. */
18548 if (it->glyph_row->reversed_p
18549 /* But if the appended newline glyph goes all the way to
18550 the end of the row, there will be no stretch glyph,
18551 so leave the box flag set. */
18552 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18553 it->end_of_box_run_p = 0;
18554
18555 PRODUCE_GLYPHS (it);
18556
18557 it->override_ascent = -1;
18558 it->constrain_row_ascent_descent_p = 0;
18559 it->current_x = saved_x;
18560 it->object = saved_object;
18561 it->position = saved_pos;
18562 it->what = saved_what;
18563 it->face_id = saved_face_id;
18564 it->len = saved_len;
18565 it->c = saved_c;
18566 it->char_to_display = saved_char_to_display;
18567 it->end_of_box_run_p = saved_box_end;
18568 return 1;
18569 }
18570 }
18571
18572 return 0;
18573 }
18574
18575
18576 /* Extend the face of the last glyph in the text area of IT->glyph_row
18577 to the end of the display line. Called from display_line. If the
18578 glyph row is empty, add a space glyph to it so that we know the
18579 face to draw. Set the glyph row flag fill_line_p. If the glyph
18580 row is R2L, prepend a stretch glyph to cover the empty space to the
18581 left of the leftmost glyph. */
18582
18583 static void
18584 extend_face_to_end_of_line (struct it *it)
18585 {
18586 struct face *face, *default_face;
18587 struct frame *f = it->f;
18588
18589 /* If line is already filled, do nothing. Non window-system frames
18590 get a grace of one more ``pixel'' because their characters are
18591 1-``pixel'' wide, so they hit the equality too early. This grace
18592 is needed only for R2L rows that are not continued, to produce
18593 one extra blank where we could display the cursor. */
18594 if (it->current_x >= it->last_visible_x
18595 + (!FRAME_WINDOW_P (f)
18596 && it->glyph_row->reversed_p
18597 && !it->glyph_row->continued_p))
18598 return;
18599
18600 /* The default face, possibly remapped. */
18601 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18602
18603 /* Face extension extends the background and box of IT->face_id
18604 to the end of the line. If the background equals the background
18605 of the frame, we don't have to do anything. */
18606 if (it->face_before_selective_p)
18607 face = FACE_FROM_ID (f, it->saved_face_id);
18608 else
18609 face = FACE_FROM_ID (f, it->face_id);
18610
18611 if (FRAME_WINDOW_P (f)
18612 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18613 && face->box == FACE_NO_BOX
18614 && face->background == FRAME_BACKGROUND_PIXEL (f)
18615 && !face->stipple
18616 && !it->glyph_row->reversed_p)
18617 return;
18618
18619 /* Set the glyph row flag indicating that the face of the last glyph
18620 in the text area has to be drawn to the end of the text area. */
18621 it->glyph_row->fill_line_p = 1;
18622
18623 /* If current character of IT is not ASCII, make sure we have the
18624 ASCII face. This will be automatically undone the next time
18625 get_next_display_element returns a multibyte character. Note
18626 that the character will always be single byte in unibyte
18627 text. */
18628 if (!ASCII_CHAR_P (it->c))
18629 {
18630 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18631 }
18632
18633 if (FRAME_WINDOW_P (f))
18634 {
18635 /* If the row is empty, add a space with the current face of IT,
18636 so that we know which face to draw. */
18637 if (it->glyph_row->used[TEXT_AREA] == 0)
18638 {
18639 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18640 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18641 it->glyph_row->used[TEXT_AREA] = 1;
18642 }
18643 #ifdef HAVE_WINDOW_SYSTEM
18644 if (it->glyph_row->reversed_p)
18645 {
18646 /* Prepend a stretch glyph to the row, such that the
18647 rightmost glyph will be drawn flushed all the way to the
18648 right margin of the window. The stretch glyph that will
18649 occupy the empty space, if any, to the left of the
18650 glyphs. */
18651 struct font *font = face->font ? face->font : FRAME_FONT (f);
18652 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18653 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18654 struct glyph *g;
18655 int row_width, stretch_ascent, stretch_width;
18656 struct text_pos saved_pos;
18657 int saved_face_id, saved_avoid_cursor, saved_box_start;
18658
18659 for (row_width = 0, g = row_start; g < row_end; g++)
18660 row_width += g->pixel_width;
18661 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18662 if (stretch_width > 0)
18663 {
18664 stretch_ascent =
18665 (((it->ascent + it->descent)
18666 * FONT_BASE (font)) / FONT_HEIGHT (font));
18667 saved_pos = it->position;
18668 memset (&it->position, 0, sizeof it->position);
18669 saved_avoid_cursor = it->avoid_cursor_p;
18670 it->avoid_cursor_p = 1;
18671 saved_face_id = it->face_id;
18672 saved_box_start = it->start_of_box_run_p;
18673 /* The last row's stretch glyph should get the default
18674 face, to avoid painting the rest of the window with
18675 the region face, if the region ends at ZV. */
18676 if (it->glyph_row->ends_at_zv_p)
18677 it->face_id = default_face->id;
18678 else
18679 it->face_id = face->id;
18680 it->start_of_box_run_p = 0;
18681 append_stretch_glyph (it, make_number (0), stretch_width,
18682 it->ascent + it->descent, stretch_ascent);
18683 it->position = saved_pos;
18684 it->avoid_cursor_p = saved_avoid_cursor;
18685 it->face_id = saved_face_id;
18686 it->start_of_box_run_p = saved_box_start;
18687 }
18688 }
18689 #endif /* HAVE_WINDOW_SYSTEM */
18690 }
18691 else
18692 {
18693 /* Save some values that must not be changed. */
18694 int saved_x = it->current_x;
18695 struct text_pos saved_pos;
18696 Lisp_Object saved_object;
18697 enum display_element_type saved_what = it->what;
18698 int saved_face_id = it->face_id;
18699
18700 saved_object = it->object;
18701 saved_pos = it->position;
18702
18703 it->what = IT_CHARACTER;
18704 memset (&it->position, 0, sizeof it->position);
18705 it->object = make_number (0);
18706 it->c = it->char_to_display = ' ';
18707 it->len = 1;
18708 /* The last row's blank glyphs should get the default face, to
18709 avoid painting the rest of the window with the region face,
18710 if the region ends at ZV. */
18711 if (it->glyph_row->ends_at_zv_p)
18712 it->face_id = default_face->id;
18713 else
18714 it->face_id = face->id;
18715
18716 PRODUCE_GLYPHS (it);
18717
18718 while (it->current_x <= it->last_visible_x)
18719 PRODUCE_GLYPHS (it);
18720
18721 /* Don't count these blanks really. It would let us insert a left
18722 truncation glyph below and make us set the cursor on them, maybe. */
18723 it->current_x = saved_x;
18724 it->object = saved_object;
18725 it->position = saved_pos;
18726 it->what = saved_what;
18727 it->face_id = saved_face_id;
18728 }
18729 }
18730
18731
18732 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18733 trailing whitespace. */
18734
18735 static int
18736 trailing_whitespace_p (ptrdiff_t charpos)
18737 {
18738 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18739 int c = 0;
18740
18741 while (bytepos < ZV_BYTE
18742 && (c = FETCH_CHAR (bytepos),
18743 c == ' ' || c == '\t'))
18744 ++bytepos;
18745
18746 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18747 {
18748 if (bytepos != PT_BYTE)
18749 return 1;
18750 }
18751 return 0;
18752 }
18753
18754
18755 /* Highlight trailing whitespace, if any, in ROW. */
18756
18757 static void
18758 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18759 {
18760 int used = row->used[TEXT_AREA];
18761
18762 if (used)
18763 {
18764 struct glyph *start = row->glyphs[TEXT_AREA];
18765 struct glyph *glyph = start + used - 1;
18766
18767 if (row->reversed_p)
18768 {
18769 /* Right-to-left rows need to be processed in the opposite
18770 direction, so swap the edge pointers. */
18771 glyph = start;
18772 start = row->glyphs[TEXT_AREA] + used - 1;
18773 }
18774
18775 /* Skip over glyphs inserted to display the cursor at the
18776 end of a line, for extending the face of the last glyph
18777 to the end of the line on terminals, and for truncation
18778 and continuation glyphs. */
18779 if (!row->reversed_p)
18780 {
18781 while (glyph >= start
18782 && glyph->type == CHAR_GLYPH
18783 && INTEGERP (glyph->object))
18784 --glyph;
18785 }
18786 else
18787 {
18788 while (glyph <= start
18789 && glyph->type == CHAR_GLYPH
18790 && INTEGERP (glyph->object))
18791 ++glyph;
18792 }
18793
18794 /* If last glyph is a space or stretch, and it's trailing
18795 whitespace, set the face of all trailing whitespace glyphs in
18796 IT->glyph_row to `trailing-whitespace'. */
18797 if ((row->reversed_p ? glyph <= start : glyph >= start)
18798 && BUFFERP (glyph->object)
18799 && (glyph->type == STRETCH_GLYPH
18800 || (glyph->type == CHAR_GLYPH
18801 && glyph->u.ch == ' '))
18802 && trailing_whitespace_p (glyph->charpos))
18803 {
18804 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18805 if (face_id < 0)
18806 return;
18807
18808 if (!row->reversed_p)
18809 {
18810 while (glyph >= start
18811 && BUFFERP (glyph->object)
18812 && (glyph->type == STRETCH_GLYPH
18813 || (glyph->type == CHAR_GLYPH
18814 && glyph->u.ch == ' ')))
18815 (glyph--)->face_id = face_id;
18816 }
18817 else
18818 {
18819 while (glyph <= start
18820 && BUFFERP (glyph->object)
18821 && (glyph->type == STRETCH_GLYPH
18822 || (glyph->type == CHAR_GLYPH
18823 && glyph->u.ch == ' ')))
18824 (glyph++)->face_id = face_id;
18825 }
18826 }
18827 }
18828 }
18829
18830
18831 /* Value is non-zero if glyph row ROW should be
18832 considered to hold the buffer position CHARPOS. */
18833
18834 static int
18835 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18836 {
18837 int result = 1;
18838
18839 if (charpos == CHARPOS (row->end.pos)
18840 || charpos == MATRIX_ROW_END_CHARPOS (row))
18841 {
18842 /* Suppose the row ends on a string.
18843 Unless the row is continued, that means it ends on a newline
18844 in the string. If it's anything other than a display string
18845 (e.g., a before-string from an overlay), we don't want the
18846 cursor there. (This heuristic seems to give the optimal
18847 behavior for the various types of multi-line strings.)
18848 One exception: if the string has `cursor' property on one of
18849 its characters, we _do_ want the cursor there. */
18850 if (CHARPOS (row->end.string_pos) >= 0)
18851 {
18852 if (row->continued_p)
18853 result = 1;
18854 else
18855 {
18856 /* Check for `display' property. */
18857 struct glyph *beg = row->glyphs[TEXT_AREA];
18858 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18859 struct glyph *glyph;
18860
18861 result = 0;
18862 for (glyph = end; glyph >= beg; --glyph)
18863 if (STRINGP (glyph->object))
18864 {
18865 Lisp_Object prop
18866 = Fget_char_property (make_number (charpos),
18867 Qdisplay, Qnil);
18868 result =
18869 (!NILP (prop)
18870 && display_prop_string_p (prop, glyph->object));
18871 /* If there's a `cursor' property on one of the
18872 string's characters, this row is a cursor row,
18873 even though this is not a display string. */
18874 if (!result)
18875 {
18876 Lisp_Object s = glyph->object;
18877
18878 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18879 {
18880 ptrdiff_t gpos = glyph->charpos;
18881
18882 if (!NILP (Fget_char_property (make_number (gpos),
18883 Qcursor, s)))
18884 {
18885 result = 1;
18886 break;
18887 }
18888 }
18889 }
18890 break;
18891 }
18892 }
18893 }
18894 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18895 {
18896 /* If the row ends in middle of a real character,
18897 and the line is continued, we want the cursor here.
18898 That's because CHARPOS (ROW->end.pos) would equal
18899 PT if PT is before the character. */
18900 if (!row->ends_in_ellipsis_p)
18901 result = row->continued_p;
18902 else
18903 /* If the row ends in an ellipsis, then
18904 CHARPOS (ROW->end.pos) will equal point after the
18905 invisible text. We want that position to be displayed
18906 after the ellipsis. */
18907 result = 0;
18908 }
18909 /* If the row ends at ZV, display the cursor at the end of that
18910 row instead of at the start of the row below. */
18911 else if (row->ends_at_zv_p)
18912 result = 1;
18913 else
18914 result = 0;
18915 }
18916
18917 return result;
18918 }
18919
18920 /* Value is non-zero if glyph row ROW should be
18921 used to hold the cursor. */
18922
18923 static int
18924 cursor_row_p (struct glyph_row *row)
18925 {
18926 return row_for_charpos_p (row, PT);
18927 }
18928
18929 \f
18930
18931 /* Push the property PROP so that it will be rendered at the current
18932 position in IT. Return 1 if PROP was successfully pushed, 0
18933 otherwise. Called from handle_line_prefix to handle the
18934 `line-prefix' and `wrap-prefix' properties. */
18935
18936 static int
18937 push_prefix_prop (struct it *it, Lisp_Object prop)
18938 {
18939 struct text_pos pos =
18940 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18941
18942 eassert (it->method == GET_FROM_BUFFER
18943 || it->method == GET_FROM_DISPLAY_VECTOR
18944 || it->method == GET_FROM_STRING);
18945
18946 /* We need to save the current buffer/string position, so it will be
18947 restored by pop_it, because iterate_out_of_display_property
18948 depends on that being set correctly, but some situations leave
18949 it->position not yet set when this function is called. */
18950 push_it (it, &pos);
18951
18952 if (STRINGP (prop))
18953 {
18954 if (SCHARS (prop) == 0)
18955 {
18956 pop_it (it);
18957 return 0;
18958 }
18959
18960 it->string = prop;
18961 it->string_from_prefix_prop_p = 1;
18962 it->multibyte_p = STRING_MULTIBYTE (it->string);
18963 it->current.overlay_string_index = -1;
18964 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18965 it->end_charpos = it->string_nchars = SCHARS (it->string);
18966 it->method = GET_FROM_STRING;
18967 it->stop_charpos = 0;
18968 it->prev_stop = 0;
18969 it->base_level_stop = 0;
18970
18971 /* Force paragraph direction to be that of the parent
18972 buffer/string. */
18973 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18974 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18975 else
18976 it->paragraph_embedding = L2R;
18977
18978 /* Set up the bidi iterator for this display string. */
18979 if (it->bidi_p)
18980 {
18981 it->bidi_it.string.lstring = it->string;
18982 it->bidi_it.string.s = NULL;
18983 it->bidi_it.string.schars = it->end_charpos;
18984 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18985 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18986 it->bidi_it.string.unibyte = !it->multibyte_p;
18987 it->bidi_it.w = it->w;
18988 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18989 }
18990 }
18991 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18992 {
18993 it->method = GET_FROM_STRETCH;
18994 it->object = prop;
18995 }
18996 #ifdef HAVE_WINDOW_SYSTEM
18997 else if (IMAGEP (prop))
18998 {
18999 it->what = IT_IMAGE;
19000 it->image_id = lookup_image (it->f, prop);
19001 it->method = GET_FROM_IMAGE;
19002 }
19003 #endif /* HAVE_WINDOW_SYSTEM */
19004 else
19005 {
19006 pop_it (it); /* bogus display property, give up */
19007 return 0;
19008 }
19009
19010 return 1;
19011 }
19012
19013 /* Return the character-property PROP at the current position in IT. */
19014
19015 static Lisp_Object
19016 get_it_property (struct it *it, Lisp_Object prop)
19017 {
19018 Lisp_Object position, object = it->object;
19019
19020 if (STRINGP (object))
19021 position = make_number (IT_STRING_CHARPOS (*it));
19022 else if (BUFFERP (object))
19023 {
19024 position = make_number (IT_CHARPOS (*it));
19025 object = it->window;
19026 }
19027 else
19028 return Qnil;
19029
19030 return Fget_char_property (position, prop, object);
19031 }
19032
19033 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19034
19035 static void
19036 handle_line_prefix (struct it *it)
19037 {
19038 Lisp_Object prefix;
19039
19040 if (it->continuation_lines_width > 0)
19041 {
19042 prefix = get_it_property (it, Qwrap_prefix);
19043 if (NILP (prefix))
19044 prefix = Vwrap_prefix;
19045 }
19046 else
19047 {
19048 prefix = get_it_property (it, Qline_prefix);
19049 if (NILP (prefix))
19050 prefix = Vline_prefix;
19051 }
19052 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19053 {
19054 /* If the prefix is wider than the window, and we try to wrap
19055 it, it would acquire its own wrap prefix, and so on till the
19056 iterator stack overflows. So, don't wrap the prefix. */
19057 it->line_wrap = TRUNCATE;
19058 it->avoid_cursor_p = 1;
19059 }
19060 }
19061
19062 \f
19063
19064 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19065 only for R2L lines from display_line and display_string, when they
19066 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19067 the line/string needs to be continued on the next glyph row. */
19068 static void
19069 unproduce_glyphs (struct it *it, int n)
19070 {
19071 struct glyph *glyph, *end;
19072
19073 eassert (it->glyph_row);
19074 eassert (it->glyph_row->reversed_p);
19075 eassert (it->area == TEXT_AREA);
19076 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19077
19078 if (n > it->glyph_row->used[TEXT_AREA])
19079 n = it->glyph_row->used[TEXT_AREA];
19080 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19081 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19082 for ( ; glyph < end; glyph++)
19083 glyph[-n] = *glyph;
19084 }
19085
19086 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19087 and ROW->maxpos. */
19088 static void
19089 find_row_edges (struct it *it, struct glyph_row *row,
19090 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19091 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19092 {
19093 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19094 lines' rows is implemented for bidi-reordered rows. */
19095
19096 /* ROW->minpos is the value of min_pos, the minimal buffer position
19097 we have in ROW, or ROW->start.pos if that is smaller. */
19098 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19099 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19100 else
19101 /* We didn't find buffer positions smaller than ROW->start, or
19102 didn't find _any_ valid buffer positions in any of the glyphs,
19103 so we must trust the iterator's computed positions. */
19104 row->minpos = row->start.pos;
19105 if (max_pos <= 0)
19106 {
19107 max_pos = CHARPOS (it->current.pos);
19108 max_bpos = BYTEPOS (it->current.pos);
19109 }
19110
19111 /* Here are the various use-cases for ending the row, and the
19112 corresponding values for ROW->maxpos:
19113
19114 Line ends in a newline from buffer eol_pos + 1
19115 Line is continued from buffer max_pos + 1
19116 Line is truncated on right it->current.pos
19117 Line ends in a newline from string max_pos + 1(*)
19118 (*) + 1 only when line ends in a forward scan
19119 Line is continued from string max_pos
19120 Line is continued from display vector max_pos
19121 Line is entirely from a string min_pos == max_pos
19122 Line is entirely from a display vector min_pos == max_pos
19123 Line that ends at ZV ZV
19124
19125 If you discover other use-cases, please add them here as
19126 appropriate. */
19127 if (row->ends_at_zv_p)
19128 row->maxpos = it->current.pos;
19129 else if (row->used[TEXT_AREA])
19130 {
19131 int seen_this_string = 0;
19132 struct glyph_row *r1 = row - 1;
19133
19134 /* Did we see the same display string on the previous row? */
19135 if (STRINGP (it->object)
19136 /* this is not the first row */
19137 && row > it->w->desired_matrix->rows
19138 /* previous row is not the header line */
19139 && !r1->mode_line_p
19140 /* previous row also ends in a newline from a string */
19141 && r1->ends_in_newline_from_string_p)
19142 {
19143 struct glyph *start, *end;
19144
19145 /* Search for the last glyph of the previous row that came
19146 from buffer or string. Depending on whether the row is
19147 L2R or R2L, we need to process it front to back or the
19148 other way round. */
19149 if (!r1->reversed_p)
19150 {
19151 start = r1->glyphs[TEXT_AREA];
19152 end = start + r1->used[TEXT_AREA];
19153 /* Glyphs inserted by redisplay have an integer (zero)
19154 as their object. */
19155 while (end > start
19156 && INTEGERP ((end - 1)->object)
19157 && (end - 1)->charpos <= 0)
19158 --end;
19159 if (end > start)
19160 {
19161 if (EQ ((end - 1)->object, it->object))
19162 seen_this_string = 1;
19163 }
19164 else
19165 /* If all the glyphs of the previous row were inserted
19166 by redisplay, it means the previous row was
19167 produced from a single newline, which is only
19168 possible if that newline came from the same string
19169 as the one which produced this ROW. */
19170 seen_this_string = 1;
19171 }
19172 else
19173 {
19174 end = r1->glyphs[TEXT_AREA] - 1;
19175 start = end + r1->used[TEXT_AREA];
19176 while (end < start
19177 && INTEGERP ((end + 1)->object)
19178 && (end + 1)->charpos <= 0)
19179 ++end;
19180 if (end < start)
19181 {
19182 if (EQ ((end + 1)->object, it->object))
19183 seen_this_string = 1;
19184 }
19185 else
19186 seen_this_string = 1;
19187 }
19188 }
19189 /* Take note of each display string that covers a newline only
19190 once, the first time we see it. This is for when a display
19191 string includes more than one newline in it. */
19192 if (row->ends_in_newline_from_string_p && !seen_this_string)
19193 {
19194 /* If we were scanning the buffer forward when we displayed
19195 the string, we want to account for at least one buffer
19196 position that belongs to this row (position covered by
19197 the display string), so that cursor positioning will
19198 consider this row as a candidate when point is at the end
19199 of the visual line represented by this row. This is not
19200 required when scanning back, because max_pos will already
19201 have a much larger value. */
19202 if (CHARPOS (row->end.pos) > max_pos)
19203 INC_BOTH (max_pos, max_bpos);
19204 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19205 }
19206 else if (CHARPOS (it->eol_pos) > 0)
19207 SET_TEXT_POS (row->maxpos,
19208 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19209 else if (row->continued_p)
19210 {
19211 /* If max_pos is different from IT's current position, it
19212 means IT->method does not belong to the display element
19213 at max_pos. However, it also means that the display
19214 element at max_pos was displayed in its entirety on this
19215 line, which is equivalent to saying that the next line
19216 starts at the next buffer position. */
19217 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19218 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19219 else
19220 {
19221 INC_BOTH (max_pos, max_bpos);
19222 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19223 }
19224 }
19225 else if (row->truncated_on_right_p)
19226 /* display_line already called reseat_at_next_visible_line_start,
19227 which puts the iterator at the beginning of the next line, in
19228 the logical order. */
19229 row->maxpos = it->current.pos;
19230 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19231 /* A line that is entirely from a string/image/stretch... */
19232 row->maxpos = row->minpos;
19233 else
19234 emacs_abort ();
19235 }
19236 else
19237 row->maxpos = it->current.pos;
19238 }
19239
19240 /* Construct the glyph row IT->glyph_row in the desired matrix of
19241 IT->w from text at the current position of IT. See dispextern.h
19242 for an overview of struct it. Value is non-zero if
19243 IT->glyph_row displays text, as opposed to a line displaying ZV
19244 only. */
19245
19246 static int
19247 display_line (struct it *it)
19248 {
19249 struct glyph_row *row = it->glyph_row;
19250 Lisp_Object overlay_arrow_string;
19251 struct it wrap_it;
19252 void *wrap_data = NULL;
19253 int may_wrap = 0, wrap_x IF_LINT (= 0);
19254 int wrap_row_used = -1;
19255 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19256 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19257 int wrap_row_extra_line_spacing IF_LINT (= 0);
19258 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19259 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19260 int cvpos;
19261 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19262 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19263
19264 /* We always start displaying at hpos zero even if hscrolled. */
19265 eassert (it->hpos == 0 && it->current_x == 0);
19266
19267 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19268 >= it->w->desired_matrix->nrows)
19269 {
19270 it->w->nrows_scale_factor++;
19271 it->f->fonts_changed = 1;
19272 return 0;
19273 }
19274
19275 /* Is IT->w showing the region? */
19276 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19277
19278 /* Clear the result glyph row and enable it. */
19279 prepare_desired_row (row);
19280
19281 row->y = it->current_y;
19282 row->start = it->start;
19283 row->continuation_lines_width = it->continuation_lines_width;
19284 row->displays_text_p = 1;
19285 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19286 it->starts_in_middle_of_char_p = 0;
19287
19288 /* Arrange the overlays nicely for our purposes. Usually, we call
19289 display_line on only one line at a time, in which case this
19290 can't really hurt too much, or we call it on lines which appear
19291 one after another in the buffer, in which case all calls to
19292 recenter_overlay_lists but the first will be pretty cheap. */
19293 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19294
19295 /* Move over display elements that are not visible because we are
19296 hscrolled. This may stop at an x-position < IT->first_visible_x
19297 if the first glyph is partially visible or if we hit a line end. */
19298 if (it->current_x < it->first_visible_x)
19299 {
19300 enum move_it_result move_result;
19301
19302 this_line_min_pos = row->start.pos;
19303 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19304 MOVE_TO_POS | MOVE_TO_X);
19305 /* If we are under a large hscroll, move_it_in_display_line_to
19306 could hit the end of the line without reaching
19307 it->first_visible_x. Pretend that we did reach it. This is
19308 especially important on a TTY, where we will call
19309 extend_face_to_end_of_line, which needs to know how many
19310 blank glyphs to produce. */
19311 if (it->current_x < it->first_visible_x
19312 && (move_result == MOVE_NEWLINE_OR_CR
19313 || move_result == MOVE_POS_MATCH_OR_ZV))
19314 it->current_x = it->first_visible_x;
19315
19316 /* Record the smallest positions seen while we moved over
19317 display elements that are not visible. This is needed by
19318 redisplay_internal for optimizing the case where the cursor
19319 stays inside the same line. The rest of this function only
19320 considers positions that are actually displayed, so
19321 RECORD_MAX_MIN_POS will not otherwise record positions that
19322 are hscrolled to the left of the left edge of the window. */
19323 min_pos = CHARPOS (this_line_min_pos);
19324 min_bpos = BYTEPOS (this_line_min_pos);
19325 }
19326 else
19327 {
19328 /* We only do this when not calling `move_it_in_display_line_to'
19329 above, because move_it_in_display_line_to calls
19330 handle_line_prefix itself. */
19331 handle_line_prefix (it);
19332 }
19333
19334 /* Get the initial row height. This is either the height of the
19335 text hscrolled, if there is any, or zero. */
19336 row->ascent = it->max_ascent;
19337 row->height = it->max_ascent + it->max_descent;
19338 row->phys_ascent = it->max_phys_ascent;
19339 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19340 row->extra_line_spacing = it->max_extra_line_spacing;
19341
19342 /* Utility macro to record max and min buffer positions seen until now. */
19343 #define RECORD_MAX_MIN_POS(IT) \
19344 do \
19345 { \
19346 int composition_p = !STRINGP ((IT)->string) \
19347 && ((IT)->what == IT_COMPOSITION); \
19348 ptrdiff_t current_pos = \
19349 composition_p ? (IT)->cmp_it.charpos \
19350 : IT_CHARPOS (*(IT)); \
19351 ptrdiff_t current_bpos = \
19352 composition_p ? CHAR_TO_BYTE (current_pos) \
19353 : IT_BYTEPOS (*(IT)); \
19354 if (current_pos < min_pos) \
19355 { \
19356 min_pos = current_pos; \
19357 min_bpos = current_bpos; \
19358 } \
19359 if (IT_CHARPOS (*it) > max_pos) \
19360 { \
19361 max_pos = IT_CHARPOS (*it); \
19362 max_bpos = IT_BYTEPOS (*it); \
19363 } \
19364 } \
19365 while (0)
19366
19367 /* Loop generating characters. The loop is left with IT on the next
19368 character to display. */
19369 while (1)
19370 {
19371 int n_glyphs_before, hpos_before, x_before;
19372 int x, nglyphs;
19373 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19374
19375 /* Retrieve the next thing to display. Value is zero if end of
19376 buffer reached. */
19377 if (!get_next_display_element (it))
19378 {
19379 /* Maybe add a space at the end of this line that is used to
19380 display the cursor there under X. Set the charpos of the
19381 first glyph of blank lines not corresponding to any text
19382 to -1. */
19383 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19384 row->exact_window_width_line_p = 1;
19385 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19386 || row->used[TEXT_AREA] == 0)
19387 {
19388 row->glyphs[TEXT_AREA]->charpos = -1;
19389 row->displays_text_p = 0;
19390
19391 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19392 && (!MINI_WINDOW_P (it->w)
19393 || (minibuf_level && EQ (it->window, minibuf_window))))
19394 row->indicate_empty_line_p = 1;
19395 }
19396
19397 it->continuation_lines_width = 0;
19398 row->ends_at_zv_p = 1;
19399 /* A row that displays right-to-left text must always have
19400 its last face extended all the way to the end of line,
19401 even if this row ends in ZV, because we still write to
19402 the screen left to right. We also need to extend the
19403 last face if the default face is remapped to some
19404 different face, otherwise the functions that clear
19405 portions of the screen will clear with the default face's
19406 background color. */
19407 if (row->reversed_p
19408 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19409 extend_face_to_end_of_line (it);
19410 break;
19411 }
19412
19413 /* Now, get the metrics of what we want to display. This also
19414 generates glyphs in `row' (which is IT->glyph_row). */
19415 n_glyphs_before = row->used[TEXT_AREA];
19416 x = it->current_x;
19417
19418 /* Remember the line height so far in case the next element doesn't
19419 fit on the line. */
19420 if (it->line_wrap != TRUNCATE)
19421 {
19422 ascent = it->max_ascent;
19423 descent = it->max_descent;
19424 phys_ascent = it->max_phys_ascent;
19425 phys_descent = it->max_phys_descent;
19426
19427 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19428 {
19429 if (IT_DISPLAYING_WHITESPACE (it))
19430 may_wrap = 1;
19431 else if (may_wrap)
19432 {
19433 SAVE_IT (wrap_it, *it, wrap_data);
19434 wrap_x = x;
19435 wrap_row_used = row->used[TEXT_AREA];
19436 wrap_row_ascent = row->ascent;
19437 wrap_row_height = row->height;
19438 wrap_row_phys_ascent = row->phys_ascent;
19439 wrap_row_phys_height = row->phys_height;
19440 wrap_row_extra_line_spacing = row->extra_line_spacing;
19441 wrap_row_min_pos = min_pos;
19442 wrap_row_min_bpos = min_bpos;
19443 wrap_row_max_pos = max_pos;
19444 wrap_row_max_bpos = max_bpos;
19445 may_wrap = 0;
19446 }
19447 }
19448 }
19449
19450 PRODUCE_GLYPHS (it);
19451
19452 /* If this display element was in marginal areas, continue with
19453 the next one. */
19454 if (it->area != TEXT_AREA)
19455 {
19456 row->ascent = max (row->ascent, it->max_ascent);
19457 row->height = max (row->height, it->max_ascent + it->max_descent);
19458 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19459 row->phys_height = max (row->phys_height,
19460 it->max_phys_ascent + it->max_phys_descent);
19461 row->extra_line_spacing = max (row->extra_line_spacing,
19462 it->max_extra_line_spacing);
19463 set_iterator_to_next (it, 1);
19464 continue;
19465 }
19466
19467 /* Does the display element fit on the line? If we truncate
19468 lines, we should draw past the right edge of the window. If
19469 we don't truncate, we want to stop so that we can display the
19470 continuation glyph before the right margin. If lines are
19471 continued, there are two possible strategies for characters
19472 resulting in more than 1 glyph (e.g. tabs): Display as many
19473 glyphs as possible in this line and leave the rest for the
19474 continuation line, or display the whole element in the next
19475 line. Original redisplay did the former, so we do it also. */
19476 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19477 hpos_before = it->hpos;
19478 x_before = x;
19479
19480 if (/* Not a newline. */
19481 nglyphs > 0
19482 /* Glyphs produced fit entirely in the line. */
19483 && it->current_x < it->last_visible_x)
19484 {
19485 it->hpos += nglyphs;
19486 row->ascent = max (row->ascent, it->max_ascent);
19487 row->height = max (row->height, it->max_ascent + it->max_descent);
19488 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19489 row->phys_height = max (row->phys_height,
19490 it->max_phys_ascent + it->max_phys_descent);
19491 row->extra_line_spacing = max (row->extra_line_spacing,
19492 it->max_extra_line_spacing);
19493 if (it->current_x - it->pixel_width < it->first_visible_x)
19494 row->x = x - it->first_visible_x;
19495 /* Record the maximum and minimum buffer positions seen so
19496 far in glyphs that will be displayed by this row. */
19497 if (it->bidi_p)
19498 RECORD_MAX_MIN_POS (it);
19499 }
19500 else
19501 {
19502 int i, new_x;
19503 struct glyph *glyph;
19504
19505 for (i = 0; i < nglyphs; ++i, x = new_x)
19506 {
19507 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19508 new_x = x + glyph->pixel_width;
19509
19510 if (/* Lines are continued. */
19511 it->line_wrap != TRUNCATE
19512 && (/* Glyph doesn't fit on the line. */
19513 new_x > it->last_visible_x
19514 /* Or it fits exactly on a window system frame. */
19515 || (new_x == it->last_visible_x
19516 && FRAME_WINDOW_P (it->f)
19517 && (row->reversed_p
19518 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19519 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19520 {
19521 /* End of a continued line. */
19522
19523 if (it->hpos == 0
19524 || (new_x == it->last_visible_x
19525 && FRAME_WINDOW_P (it->f)
19526 && (row->reversed_p
19527 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19528 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19529 {
19530 /* Current glyph is the only one on the line or
19531 fits exactly on the line. We must continue
19532 the line because we can't draw the cursor
19533 after the glyph. */
19534 row->continued_p = 1;
19535 it->current_x = new_x;
19536 it->continuation_lines_width += new_x;
19537 ++it->hpos;
19538 if (i == nglyphs - 1)
19539 {
19540 /* If line-wrap is on, check if a previous
19541 wrap point was found. */
19542 if (wrap_row_used > 0
19543 /* Even if there is a previous wrap
19544 point, continue the line here as
19545 usual, if (i) the previous character
19546 was a space or tab AND (ii) the
19547 current character is not. */
19548 && (!may_wrap
19549 || IT_DISPLAYING_WHITESPACE (it)))
19550 goto back_to_wrap;
19551
19552 /* Record the maximum and minimum buffer
19553 positions seen so far in glyphs that will be
19554 displayed by this row. */
19555 if (it->bidi_p)
19556 RECORD_MAX_MIN_POS (it);
19557 set_iterator_to_next (it, 1);
19558 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19559 {
19560 if (!get_next_display_element (it))
19561 {
19562 row->exact_window_width_line_p = 1;
19563 it->continuation_lines_width = 0;
19564 row->continued_p = 0;
19565 row->ends_at_zv_p = 1;
19566 }
19567 else if (ITERATOR_AT_END_OF_LINE_P (it))
19568 {
19569 row->continued_p = 0;
19570 row->exact_window_width_line_p = 1;
19571 }
19572 }
19573 }
19574 else if (it->bidi_p)
19575 RECORD_MAX_MIN_POS (it);
19576 }
19577 else if (CHAR_GLYPH_PADDING_P (*glyph)
19578 && !FRAME_WINDOW_P (it->f))
19579 {
19580 /* A padding glyph that doesn't fit on this line.
19581 This means the whole character doesn't fit
19582 on the line. */
19583 if (row->reversed_p)
19584 unproduce_glyphs (it, row->used[TEXT_AREA]
19585 - n_glyphs_before);
19586 row->used[TEXT_AREA] = n_glyphs_before;
19587
19588 /* Fill the rest of the row with continuation
19589 glyphs like in 20.x. */
19590 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19591 < row->glyphs[1 + TEXT_AREA])
19592 produce_special_glyphs (it, IT_CONTINUATION);
19593
19594 row->continued_p = 1;
19595 it->current_x = x_before;
19596 it->continuation_lines_width += x_before;
19597
19598 /* Restore the height to what it was before the
19599 element not fitting on the line. */
19600 it->max_ascent = ascent;
19601 it->max_descent = descent;
19602 it->max_phys_ascent = phys_ascent;
19603 it->max_phys_descent = phys_descent;
19604 }
19605 else if (wrap_row_used > 0)
19606 {
19607 back_to_wrap:
19608 if (row->reversed_p)
19609 unproduce_glyphs (it,
19610 row->used[TEXT_AREA] - wrap_row_used);
19611 RESTORE_IT (it, &wrap_it, wrap_data);
19612 it->continuation_lines_width += wrap_x;
19613 row->used[TEXT_AREA] = wrap_row_used;
19614 row->ascent = wrap_row_ascent;
19615 row->height = wrap_row_height;
19616 row->phys_ascent = wrap_row_phys_ascent;
19617 row->phys_height = wrap_row_phys_height;
19618 row->extra_line_spacing = wrap_row_extra_line_spacing;
19619 min_pos = wrap_row_min_pos;
19620 min_bpos = wrap_row_min_bpos;
19621 max_pos = wrap_row_max_pos;
19622 max_bpos = wrap_row_max_bpos;
19623 row->continued_p = 1;
19624 row->ends_at_zv_p = 0;
19625 row->exact_window_width_line_p = 0;
19626 it->continuation_lines_width += x;
19627
19628 /* Make sure that a non-default face is extended
19629 up to the right margin of the window. */
19630 extend_face_to_end_of_line (it);
19631 }
19632 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19633 {
19634 /* A TAB that extends past the right edge of the
19635 window. This produces a single glyph on
19636 window system frames. We leave the glyph in
19637 this row and let it fill the row, but don't
19638 consume the TAB. */
19639 if ((row->reversed_p
19640 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19641 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19642 produce_special_glyphs (it, IT_CONTINUATION);
19643 it->continuation_lines_width += it->last_visible_x;
19644 row->ends_in_middle_of_char_p = 1;
19645 row->continued_p = 1;
19646 glyph->pixel_width = it->last_visible_x - x;
19647 it->starts_in_middle_of_char_p = 1;
19648 }
19649 else
19650 {
19651 /* Something other than a TAB that draws past
19652 the right edge of the window. Restore
19653 positions to values before the element. */
19654 if (row->reversed_p)
19655 unproduce_glyphs (it, row->used[TEXT_AREA]
19656 - (n_glyphs_before + i));
19657 row->used[TEXT_AREA] = n_glyphs_before + i;
19658
19659 /* Display continuation glyphs. */
19660 it->current_x = x_before;
19661 it->continuation_lines_width += x;
19662 if (!FRAME_WINDOW_P (it->f)
19663 || (row->reversed_p
19664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19666 produce_special_glyphs (it, IT_CONTINUATION);
19667 row->continued_p = 1;
19668
19669 extend_face_to_end_of_line (it);
19670
19671 if (nglyphs > 1 && i > 0)
19672 {
19673 row->ends_in_middle_of_char_p = 1;
19674 it->starts_in_middle_of_char_p = 1;
19675 }
19676
19677 /* Restore the height to what it was before the
19678 element not fitting on the line. */
19679 it->max_ascent = ascent;
19680 it->max_descent = descent;
19681 it->max_phys_ascent = phys_ascent;
19682 it->max_phys_descent = phys_descent;
19683 }
19684
19685 break;
19686 }
19687 else if (new_x > it->first_visible_x)
19688 {
19689 /* Increment number of glyphs actually displayed. */
19690 ++it->hpos;
19691
19692 /* Record the maximum and minimum buffer positions
19693 seen so far in glyphs that will be displayed by
19694 this row. */
19695 if (it->bidi_p)
19696 RECORD_MAX_MIN_POS (it);
19697
19698 if (x < it->first_visible_x)
19699 /* Glyph is partially visible, i.e. row starts at
19700 negative X position. */
19701 row->x = x - it->first_visible_x;
19702 }
19703 else
19704 {
19705 /* Glyph is completely off the left margin of the
19706 window. This should not happen because of the
19707 move_it_in_display_line at the start of this
19708 function, unless the text display area of the
19709 window is empty. */
19710 eassert (it->first_visible_x <= it->last_visible_x);
19711 }
19712 }
19713 /* Even if this display element produced no glyphs at all,
19714 we want to record its position. */
19715 if (it->bidi_p && nglyphs == 0)
19716 RECORD_MAX_MIN_POS (it);
19717
19718 row->ascent = max (row->ascent, it->max_ascent);
19719 row->height = max (row->height, it->max_ascent + it->max_descent);
19720 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19721 row->phys_height = max (row->phys_height,
19722 it->max_phys_ascent + it->max_phys_descent);
19723 row->extra_line_spacing = max (row->extra_line_spacing,
19724 it->max_extra_line_spacing);
19725
19726 /* End of this display line if row is continued. */
19727 if (row->continued_p || row->ends_at_zv_p)
19728 break;
19729 }
19730
19731 at_end_of_line:
19732 /* Is this a line end? If yes, we're also done, after making
19733 sure that a non-default face is extended up to the right
19734 margin of the window. */
19735 if (ITERATOR_AT_END_OF_LINE_P (it))
19736 {
19737 int used_before = row->used[TEXT_AREA];
19738
19739 row->ends_in_newline_from_string_p = STRINGP (it->object);
19740
19741 /* Add a space at the end of the line that is used to
19742 display the cursor there. */
19743 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19744 append_space_for_newline (it, 0);
19745
19746 /* Extend the face to the end of the line. */
19747 extend_face_to_end_of_line (it);
19748
19749 /* Make sure we have the position. */
19750 if (used_before == 0)
19751 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19752
19753 /* Record the position of the newline, for use in
19754 find_row_edges. */
19755 it->eol_pos = it->current.pos;
19756
19757 /* Consume the line end. This skips over invisible lines. */
19758 set_iterator_to_next (it, 1);
19759 it->continuation_lines_width = 0;
19760 break;
19761 }
19762
19763 /* Proceed with next display element. Note that this skips
19764 over lines invisible because of selective display. */
19765 set_iterator_to_next (it, 1);
19766
19767 /* If we truncate lines, we are done when the last displayed
19768 glyphs reach past the right margin of the window. */
19769 if (it->line_wrap == TRUNCATE
19770 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19771 ? (it->current_x >= it->last_visible_x)
19772 : (it->current_x > it->last_visible_x)))
19773 {
19774 /* Maybe add truncation glyphs. */
19775 if (!FRAME_WINDOW_P (it->f)
19776 || (row->reversed_p
19777 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19778 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19779 {
19780 int i, n;
19781
19782 if (!row->reversed_p)
19783 {
19784 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19786 break;
19787 }
19788 else
19789 {
19790 for (i = 0; i < row->used[TEXT_AREA]; i++)
19791 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19792 break;
19793 /* Remove any padding glyphs at the front of ROW, to
19794 make room for the truncation glyphs we will be
19795 adding below. The loop below always inserts at
19796 least one truncation glyph, so also remove the
19797 last glyph added to ROW. */
19798 unproduce_glyphs (it, i + 1);
19799 /* Adjust i for the loop below. */
19800 i = row->used[TEXT_AREA] - (i + 1);
19801 }
19802
19803 it->current_x = x_before;
19804 if (!FRAME_WINDOW_P (it->f))
19805 {
19806 for (n = row->used[TEXT_AREA]; i < n; ++i)
19807 {
19808 row->used[TEXT_AREA] = i;
19809 produce_special_glyphs (it, IT_TRUNCATION);
19810 }
19811 }
19812 else
19813 {
19814 row->used[TEXT_AREA] = i;
19815 produce_special_glyphs (it, IT_TRUNCATION);
19816 }
19817 }
19818 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19819 {
19820 /* Don't truncate if we can overflow newline into fringe. */
19821 if (!get_next_display_element (it))
19822 {
19823 it->continuation_lines_width = 0;
19824 row->ends_at_zv_p = 1;
19825 row->exact_window_width_line_p = 1;
19826 break;
19827 }
19828 if (ITERATOR_AT_END_OF_LINE_P (it))
19829 {
19830 row->exact_window_width_line_p = 1;
19831 goto at_end_of_line;
19832 }
19833 it->current_x = x_before;
19834 }
19835
19836 row->truncated_on_right_p = 1;
19837 it->continuation_lines_width = 0;
19838 reseat_at_next_visible_line_start (it, 0);
19839 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19840 it->hpos = hpos_before;
19841 break;
19842 }
19843 }
19844
19845 if (wrap_data)
19846 bidi_unshelve_cache (wrap_data, 1);
19847
19848 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19849 at the left window margin. */
19850 if (it->first_visible_x
19851 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19852 {
19853 if (!FRAME_WINDOW_P (it->f)
19854 || (row->reversed_p
19855 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19856 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19857 insert_left_trunc_glyphs (it);
19858 row->truncated_on_left_p = 1;
19859 }
19860
19861 /* Remember the position at which this line ends.
19862
19863 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19864 cannot be before the call to find_row_edges below, since that is
19865 where these positions are determined. */
19866 row->end = it->current;
19867 if (!it->bidi_p)
19868 {
19869 row->minpos = row->start.pos;
19870 row->maxpos = row->end.pos;
19871 }
19872 else
19873 {
19874 /* ROW->minpos and ROW->maxpos must be the smallest and
19875 `1 + the largest' buffer positions in ROW. But if ROW was
19876 bidi-reordered, these two positions can be anywhere in the
19877 row, so we must determine them now. */
19878 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19879 }
19880
19881 /* If the start of this line is the overlay arrow-position, then
19882 mark this glyph row as the one containing the overlay arrow.
19883 This is clearly a mess with variable size fonts. It would be
19884 better to let it be displayed like cursors under X. */
19885 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19886 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19887 !NILP (overlay_arrow_string)))
19888 {
19889 /* Overlay arrow in window redisplay is a fringe bitmap. */
19890 if (STRINGP (overlay_arrow_string))
19891 {
19892 struct glyph_row *arrow_row
19893 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19894 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19895 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19896 struct glyph *p = row->glyphs[TEXT_AREA];
19897 struct glyph *p2, *end;
19898
19899 /* Copy the arrow glyphs. */
19900 while (glyph < arrow_end)
19901 *p++ = *glyph++;
19902
19903 /* Throw away padding glyphs. */
19904 p2 = p;
19905 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19906 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19907 ++p2;
19908 if (p2 > p)
19909 {
19910 while (p2 < end)
19911 *p++ = *p2++;
19912 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19913 }
19914 }
19915 else
19916 {
19917 eassert (INTEGERP (overlay_arrow_string));
19918 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19919 }
19920 overlay_arrow_seen = 1;
19921 }
19922
19923 /* Highlight trailing whitespace. */
19924 if (!NILP (Vshow_trailing_whitespace))
19925 highlight_trailing_whitespace (it->f, it->glyph_row);
19926
19927 /* Compute pixel dimensions of this line. */
19928 compute_line_metrics (it);
19929
19930 /* Implementation note: No changes in the glyphs of ROW or in their
19931 faces can be done past this point, because compute_line_metrics
19932 computes ROW's hash value and stores it within the glyph_row
19933 structure. */
19934
19935 /* Record whether this row ends inside an ellipsis. */
19936 row->ends_in_ellipsis_p
19937 = (it->method == GET_FROM_DISPLAY_VECTOR
19938 && it->ellipsis_p);
19939
19940 /* Save fringe bitmaps in this row. */
19941 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19942 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19943 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19944 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19945
19946 it->left_user_fringe_bitmap = 0;
19947 it->left_user_fringe_face_id = 0;
19948 it->right_user_fringe_bitmap = 0;
19949 it->right_user_fringe_face_id = 0;
19950
19951 /* Maybe set the cursor. */
19952 cvpos = it->w->cursor.vpos;
19953 if ((cvpos < 0
19954 /* In bidi-reordered rows, keep checking for proper cursor
19955 position even if one has been found already, because buffer
19956 positions in such rows change non-linearly with ROW->VPOS,
19957 when a line is continued. One exception: when we are at ZV,
19958 display cursor on the first suitable glyph row, since all
19959 the empty rows after that also have their position set to ZV. */
19960 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19961 lines' rows is implemented for bidi-reordered rows. */
19962 || (it->bidi_p
19963 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19964 && PT >= MATRIX_ROW_START_CHARPOS (row)
19965 && PT <= MATRIX_ROW_END_CHARPOS (row)
19966 && cursor_row_p (row))
19967 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19968
19969 /* Prepare for the next line. This line starts horizontally at (X
19970 HPOS) = (0 0). Vertical positions are incremented. As a
19971 convenience for the caller, IT->glyph_row is set to the next
19972 row to be used. */
19973 it->current_x = it->hpos = 0;
19974 it->current_y += row->height;
19975 SET_TEXT_POS (it->eol_pos, 0, 0);
19976 ++it->vpos;
19977 ++it->glyph_row;
19978 /* The next row should by default use the same value of the
19979 reversed_p flag as this one. set_iterator_to_next decides when
19980 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19981 the flag accordingly. */
19982 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19983 it->glyph_row->reversed_p = row->reversed_p;
19984 it->start = row->end;
19985 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19986
19987 #undef RECORD_MAX_MIN_POS
19988 }
19989
19990 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19991 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19992 doc: /* Return paragraph direction at point in BUFFER.
19993 Value is either `left-to-right' or `right-to-left'.
19994 If BUFFER is omitted or nil, it defaults to the current buffer.
19995
19996 Paragraph direction determines how the text in the paragraph is displayed.
19997 In left-to-right paragraphs, text begins at the left margin of the window
19998 and the reading direction is generally left to right. In right-to-left
19999 paragraphs, text begins at the right margin and is read from right to left.
20000
20001 See also `bidi-paragraph-direction'. */)
20002 (Lisp_Object buffer)
20003 {
20004 struct buffer *buf = current_buffer;
20005 struct buffer *old = buf;
20006
20007 if (! NILP (buffer))
20008 {
20009 CHECK_BUFFER (buffer);
20010 buf = XBUFFER (buffer);
20011 }
20012
20013 if (NILP (BVAR (buf, bidi_display_reordering))
20014 || NILP (BVAR (buf, enable_multibyte_characters))
20015 /* When we are loading loadup.el, the character property tables
20016 needed for bidi iteration are not yet available. */
20017 || !NILP (Vpurify_flag))
20018 return Qleft_to_right;
20019 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20020 return BVAR (buf, bidi_paragraph_direction);
20021 else
20022 {
20023 /* Determine the direction from buffer text. We could try to
20024 use current_matrix if it is up to date, but this seems fast
20025 enough as it is. */
20026 struct bidi_it itb;
20027 ptrdiff_t pos = BUF_PT (buf);
20028 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20029 int c;
20030 void *itb_data = bidi_shelve_cache ();
20031
20032 set_buffer_temp (buf);
20033 /* bidi_paragraph_init finds the base direction of the paragraph
20034 by searching forward from paragraph start. We need the base
20035 direction of the current or _previous_ paragraph, so we need
20036 to make sure we are within that paragraph. To that end, find
20037 the previous non-empty line. */
20038 if (pos >= ZV && pos > BEGV)
20039 DEC_BOTH (pos, bytepos);
20040 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20041 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20042 {
20043 while ((c = FETCH_BYTE (bytepos)) == '\n'
20044 || c == ' ' || c == '\t' || c == '\f')
20045 {
20046 if (bytepos <= BEGV_BYTE)
20047 break;
20048 bytepos--;
20049 pos--;
20050 }
20051 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20052 bytepos--;
20053 }
20054 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20055 itb.paragraph_dir = NEUTRAL_DIR;
20056 itb.string.s = NULL;
20057 itb.string.lstring = Qnil;
20058 itb.string.bufpos = 0;
20059 itb.string.unibyte = 0;
20060 /* We have no window to use here for ignoring window-specific
20061 overlays. Using NULL for window pointer will cause
20062 compute_display_string_pos to use the current buffer. */
20063 itb.w = NULL;
20064 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20065 bidi_unshelve_cache (itb_data, 0);
20066 set_buffer_temp (old);
20067 switch (itb.paragraph_dir)
20068 {
20069 case L2R:
20070 return Qleft_to_right;
20071 break;
20072 case R2L:
20073 return Qright_to_left;
20074 break;
20075 default:
20076 emacs_abort ();
20077 }
20078 }
20079 }
20080
20081 DEFUN ("move-point-visually", Fmove_point_visually,
20082 Smove_point_visually, 1, 1, 0,
20083 doc: /* Move point in the visual order in the specified DIRECTION.
20084 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20085 left.
20086
20087 Value is the new character position of point. */)
20088 (Lisp_Object direction)
20089 {
20090 struct window *w = XWINDOW (selected_window);
20091 struct buffer *b = XBUFFER (w->contents);
20092 struct glyph_row *row;
20093 int dir;
20094 Lisp_Object paragraph_dir;
20095
20096 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20097 (!(ROW)->continued_p \
20098 && INTEGERP ((GLYPH)->object) \
20099 && (GLYPH)->type == CHAR_GLYPH \
20100 && (GLYPH)->u.ch == ' ' \
20101 && (GLYPH)->charpos >= 0 \
20102 && !(GLYPH)->avoid_cursor_p)
20103
20104 CHECK_NUMBER (direction);
20105 dir = XINT (direction);
20106 if (dir > 0)
20107 dir = 1;
20108 else
20109 dir = -1;
20110
20111 /* If current matrix is up-to-date, we can use the information
20112 recorded in the glyphs, at least as long as the goal is on the
20113 screen. */
20114 if (w->window_end_valid
20115 && !windows_or_buffers_changed
20116 && b
20117 && !b->clip_changed
20118 && !b->prevent_redisplay_optimizations_p
20119 && !window_outdated (w)
20120 && w->cursor.vpos >= 0
20121 && w->cursor.vpos < w->current_matrix->nrows
20122 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20123 {
20124 struct glyph *g = row->glyphs[TEXT_AREA];
20125 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20126 struct glyph *gpt = g + w->cursor.hpos;
20127
20128 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20129 {
20130 if (BUFFERP (g->object) && g->charpos != PT)
20131 {
20132 SET_PT (g->charpos);
20133 w->cursor.vpos = -1;
20134 return make_number (PT);
20135 }
20136 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20137 {
20138 ptrdiff_t new_pos;
20139
20140 if (BUFFERP (gpt->object))
20141 {
20142 new_pos = PT;
20143 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20144 new_pos += (row->reversed_p ? -dir : dir);
20145 else
20146 new_pos -= (row->reversed_p ? -dir : dir);;
20147 }
20148 else if (BUFFERP (g->object))
20149 new_pos = g->charpos;
20150 else
20151 break;
20152 SET_PT (new_pos);
20153 w->cursor.vpos = -1;
20154 return make_number (PT);
20155 }
20156 else if (ROW_GLYPH_NEWLINE_P (row, g))
20157 {
20158 /* Glyphs inserted at the end of a non-empty line for
20159 positioning the cursor have zero charpos, so we must
20160 deduce the value of point by other means. */
20161 if (g->charpos > 0)
20162 SET_PT (g->charpos);
20163 else if (row->ends_at_zv_p && PT != ZV)
20164 SET_PT (ZV);
20165 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20166 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20167 else
20168 break;
20169 w->cursor.vpos = -1;
20170 return make_number (PT);
20171 }
20172 }
20173 if (g == e || INTEGERP (g->object))
20174 {
20175 if (row->truncated_on_left_p || row->truncated_on_right_p)
20176 goto simulate_display;
20177 if (!row->reversed_p)
20178 row += dir;
20179 else
20180 row -= dir;
20181 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20182 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20183 goto simulate_display;
20184
20185 if (dir > 0)
20186 {
20187 if (row->reversed_p && !row->continued_p)
20188 {
20189 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20190 w->cursor.vpos = -1;
20191 return make_number (PT);
20192 }
20193 g = row->glyphs[TEXT_AREA];
20194 e = g + row->used[TEXT_AREA];
20195 for ( ; g < e; g++)
20196 {
20197 if (BUFFERP (g->object)
20198 /* Empty lines have only one glyph, which stands
20199 for the newline, and whose charpos is the
20200 buffer position of the newline. */
20201 || ROW_GLYPH_NEWLINE_P (row, g)
20202 /* When the buffer ends in a newline, the line at
20203 EOB also has one glyph, but its charpos is -1. */
20204 || (row->ends_at_zv_p
20205 && !row->reversed_p
20206 && INTEGERP (g->object)
20207 && g->type == CHAR_GLYPH
20208 && g->u.ch == ' '))
20209 {
20210 if (g->charpos > 0)
20211 SET_PT (g->charpos);
20212 else if (!row->reversed_p
20213 && row->ends_at_zv_p
20214 && PT != ZV)
20215 SET_PT (ZV);
20216 else
20217 continue;
20218 w->cursor.vpos = -1;
20219 return make_number (PT);
20220 }
20221 }
20222 }
20223 else
20224 {
20225 if (!row->reversed_p && !row->continued_p)
20226 {
20227 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20228 w->cursor.vpos = -1;
20229 return make_number (PT);
20230 }
20231 e = row->glyphs[TEXT_AREA];
20232 g = e + row->used[TEXT_AREA] - 1;
20233 for ( ; g >= e; g--)
20234 {
20235 if (BUFFERP (g->object)
20236 || (ROW_GLYPH_NEWLINE_P (row, g)
20237 && g->charpos > 0)
20238 /* Empty R2L lines on GUI frames have the buffer
20239 position of the newline stored in the stretch
20240 glyph. */
20241 || g->type == STRETCH_GLYPH
20242 || (row->ends_at_zv_p
20243 && row->reversed_p
20244 && INTEGERP (g->object)
20245 && g->type == CHAR_GLYPH
20246 && g->u.ch == ' '))
20247 {
20248 if (g->charpos > 0)
20249 SET_PT (g->charpos);
20250 else if (row->reversed_p
20251 && row->ends_at_zv_p
20252 && PT != ZV)
20253 SET_PT (ZV);
20254 else
20255 continue;
20256 w->cursor.vpos = -1;
20257 return make_number (PT);
20258 }
20259 }
20260 }
20261 }
20262 }
20263
20264 simulate_display:
20265
20266 /* If we wind up here, we failed to move by using the glyphs, so we
20267 need to simulate display instead. */
20268
20269 if (b)
20270 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20271 else
20272 paragraph_dir = Qleft_to_right;
20273 if (EQ (paragraph_dir, Qright_to_left))
20274 dir = -dir;
20275 if (PT <= BEGV && dir < 0)
20276 xsignal0 (Qbeginning_of_buffer);
20277 else if (PT >= ZV && dir > 0)
20278 xsignal0 (Qend_of_buffer);
20279 else
20280 {
20281 struct text_pos pt;
20282 struct it it;
20283 int pt_x, target_x, pixel_width, pt_vpos;
20284 bool at_eol_p;
20285 bool overshoot_expected = false;
20286 bool target_is_eol_p = false;
20287
20288 /* Setup the arena. */
20289 SET_TEXT_POS (pt, PT, PT_BYTE);
20290 start_display (&it, w, pt);
20291
20292 if (it.cmp_it.id < 0
20293 && it.method == GET_FROM_STRING
20294 && it.area == TEXT_AREA
20295 && it.string_from_display_prop_p
20296 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20297 overshoot_expected = true;
20298
20299 /* Find the X coordinate of point. We start from the beginning
20300 of this or previous line to make sure we are before point in
20301 the logical order (since the move_it_* functions can only
20302 move forward). */
20303 reseat_at_previous_visible_line_start (&it);
20304 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20305 if (IT_CHARPOS (it) != PT)
20306 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20307 -1, -1, -1, MOVE_TO_POS);
20308 pt_x = it.current_x;
20309 pt_vpos = it.vpos;
20310 if (dir > 0 || overshoot_expected)
20311 {
20312 struct glyph_row *row = it.glyph_row;
20313
20314 /* When point is at beginning of line, we don't have
20315 information about the glyph there loaded into struct
20316 it. Calling get_next_display_element fixes that. */
20317 if (pt_x == 0)
20318 get_next_display_element (&it);
20319 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20320 it.glyph_row = NULL;
20321 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20322 it.glyph_row = row;
20323 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20324 it, lest it will become out of sync with it's buffer
20325 position. */
20326 it.current_x = pt_x;
20327 }
20328 else
20329 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20330 pixel_width = it.pixel_width;
20331 if (overshoot_expected && at_eol_p)
20332 pixel_width = 0;
20333 else if (pixel_width <= 0)
20334 pixel_width = 1;
20335
20336 /* If there's a display string at point, we are actually at the
20337 glyph to the left of point, so we need to correct the X
20338 coordinate. */
20339 if (overshoot_expected)
20340 pt_x += pixel_width;
20341
20342 /* Compute target X coordinate, either to the left or to the
20343 right of point. On TTY frames, all characters have the same
20344 pixel width of 1, so we can use that. On GUI frames we don't
20345 have an easy way of getting at the pixel width of the
20346 character to the left of point, so we use a different method
20347 of getting to that place. */
20348 if (dir > 0)
20349 target_x = pt_x + pixel_width;
20350 else
20351 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20352
20353 /* Target X coordinate could be one line above or below the line
20354 of point, in which case we need to adjust the target X
20355 coordinate. Also, if moving to the left, we need to begin at
20356 the left edge of the point's screen line. */
20357 if (dir < 0)
20358 {
20359 if (pt_x > 0)
20360 {
20361 start_display (&it, w, pt);
20362 reseat_at_previous_visible_line_start (&it);
20363 it.current_x = it.current_y = it.hpos = 0;
20364 if (pt_vpos != 0)
20365 move_it_by_lines (&it, pt_vpos);
20366 }
20367 else
20368 {
20369 move_it_by_lines (&it, -1);
20370 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20371 target_is_eol_p = true;
20372 }
20373 }
20374 else
20375 {
20376 if (at_eol_p
20377 || (target_x >= it.last_visible_x
20378 && it.line_wrap != TRUNCATE))
20379 {
20380 if (pt_x > 0)
20381 move_it_by_lines (&it, 0);
20382 move_it_by_lines (&it, 1);
20383 target_x = 0;
20384 }
20385 }
20386
20387 /* Move to the target X coordinate. */
20388 #ifdef HAVE_WINDOW_SYSTEM
20389 /* On GUI frames, as we don't know the X coordinate of the
20390 character to the left of point, moving point to the left
20391 requires walking, one grapheme cluster at a time, until we
20392 find ourself at a place immediately to the left of the
20393 character at point. */
20394 if (FRAME_WINDOW_P (it.f) && dir < 0)
20395 {
20396 struct text_pos new_pos = it.current.pos;
20397 enum move_it_result rc = MOVE_X_REACHED;
20398
20399 while (it.current_x + it.pixel_width <= target_x
20400 && rc == MOVE_X_REACHED)
20401 {
20402 int new_x = it.current_x + it.pixel_width;
20403
20404 new_pos = it.current.pos;
20405 if (new_x == it.current_x)
20406 new_x++;
20407 rc = move_it_in_display_line_to (&it, ZV, new_x,
20408 MOVE_TO_POS | MOVE_TO_X);
20409 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20410 break;
20411 }
20412 /* If we ended up on a composed character inside
20413 bidi-reordered text (e.g., Hebrew text with diacritics),
20414 the iterator gives us the buffer position of the last (in
20415 logical order) character of the composed grapheme cluster,
20416 which is not what we want. So we cheat: we compute the
20417 character position of the character that follows (in the
20418 logical order) the one where the above loop stopped. That
20419 character will appear on display to the left of point. */
20420 if (it.bidi_p
20421 && it.bidi_it.scan_dir == -1
20422 && new_pos.charpos - IT_CHARPOS (it) > 1)
20423 {
20424 new_pos.charpos = IT_CHARPOS (it) + 1;
20425 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20426 }
20427 it.current.pos = new_pos;
20428 }
20429 else
20430 #endif
20431 if (it.current_x != target_x)
20432 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20433
20434 /* When lines are truncated, the above loop will stop at the
20435 window edge. But we want to get to the end of line, even if
20436 it is beyond the window edge; automatic hscroll will then
20437 scroll the window to show point as appropriate. */
20438 if (target_is_eol_p && it.line_wrap == TRUNCATE
20439 && get_next_display_element (&it))
20440 {
20441 struct text_pos new_pos = it.current.pos;
20442
20443 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20444 {
20445 set_iterator_to_next (&it, 0);
20446 if (it.method == GET_FROM_BUFFER)
20447 new_pos = it.current.pos;
20448 if (!get_next_display_element (&it))
20449 break;
20450 }
20451
20452 it.current.pos = new_pos;
20453 }
20454
20455 /* If we ended up in a display string that covers point, move to
20456 buffer position to the right in the visual order. */
20457 if (dir > 0)
20458 {
20459 while (IT_CHARPOS (it) == PT)
20460 {
20461 set_iterator_to_next (&it, 0);
20462 if (!get_next_display_element (&it))
20463 break;
20464 }
20465 }
20466
20467 /* Move point to that position. */
20468 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20469 }
20470
20471 return make_number (PT);
20472
20473 #undef ROW_GLYPH_NEWLINE_P
20474 }
20475
20476 \f
20477 /***********************************************************************
20478 Menu Bar
20479 ***********************************************************************/
20480
20481 /* Redisplay the menu bar in the frame for window W.
20482
20483 The menu bar of X frames that don't have X toolkit support is
20484 displayed in a special window W->frame->menu_bar_window.
20485
20486 The menu bar of terminal frames is treated specially as far as
20487 glyph matrices are concerned. Menu bar lines are not part of
20488 windows, so the update is done directly on the frame matrix rows
20489 for the menu bar. */
20490
20491 static void
20492 display_menu_bar (struct window *w)
20493 {
20494 struct frame *f = XFRAME (WINDOW_FRAME (w));
20495 struct it it;
20496 Lisp_Object items;
20497 int i;
20498
20499 /* Don't do all this for graphical frames. */
20500 #ifdef HAVE_NTGUI
20501 if (FRAME_W32_P (f))
20502 return;
20503 #endif
20504 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20505 if (FRAME_X_P (f))
20506 return;
20507 #endif
20508
20509 #ifdef HAVE_NS
20510 if (FRAME_NS_P (f))
20511 return;
20512 #endif /* HAVE_NS */
20513
20514 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20515 eassert (!FRAME_WINDOW_P (f));
20516 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20517 it.first_visible_x = 0;
20518 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20519 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20520 if (FRAME_WINDOW_P (f))
20521 {
20522 /* Menu bar lines are displayed in the desired matrix of the
20523 dummy window menu_bar_window. */
20524 struct window *menu_w;
20525 menu_w = XWINDOW (f->menu_bar_window);
20526 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20527 MENU_FACE_ID);
20528 it.first_visible_x = 0;
20529 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20530 }
20531 else
20532 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20533 {
20534 /* This is a TTY frame, i.e. character hpos/vpos are used as
20535 pixel x/y. */
20536 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20537 MENU_FACE_ID);
20538 it.first_visible_x = 0;
20539 it.last_visible_x = FRAME_COLS (f);
20540 }
20541
20542 /* FIXME: This should be controlled by a user option. See the
20543 comments in redisplay_tool_bar and display_mode_line about
20544 this. */
20545 it.paragraph_embedding = L2R;
20546
20547 /* Clear all rows of the menu bar. */
20548 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20549 {
20550 struct glyph_row *row = it.glyph_row + i;
20551 clear_glyph_row (row);
20552 row->enabled_p = 1;
20553 row->full_width_p = 1;
20554 }
20555
20556 /* Display all items of the menu bar. */
20557 items = FRAME_MENU_BAR_ITEMS (it.f);
20558 for (i = 0; i < ASIZE (items); i += 4)
20559 {
20560 Lisp_Object string;
20561
20562 /* Stop at nil string. */
20563 string = AREF (items, i + 1);
20564 if (NILP (string))
20565 break;
20566
20567 /* Remember where item was displayed. */
20568 ASET (items, i + 3, make_number (it.hpos));
20569
20570 /* Display the item, pad with one space. */
20571 if (it.current_x < it.last_visible_x)
20572 display_string (NULL, string, Qnil, 0, 0, &it,
20573 SCHARS (string) + 1, 0, 0, -1);
20574 }
20575
20576 /* Fill out the line with spaces. */
20577 if (it.current_x < it.last_visible_x)
20578 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20579
20580 /* Compute the total height of the lines. */
20581 compute_line_metrics (&it);
20582 }
20583
20584
20585 \f
20586 /***********************************************************************
20587 Mode Line
20588 ***********************************************************************/
20589
20590 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20591 FORCE is non-zero, redisplay mode lines unconditionally.
20592 Otherwise, redisplay only mode lines that are garbaged. Value is
20593 the number of windows whose mode lines were redisplayed. */
20594
20595 static int
20596 redisplay_mode_lines (Lisp_Object window, int force)
20597 {
20598 int nwindows = 0;
20599
20600 while (!NILP (window))
20601 {
20602 struct window *w = XWINDOW (window);
20603
20604 if (WINDOWP (w->contents))
20605 nwindows += redisplay_mode_lines (w->contents, force);
20606 else if (force
20607 || FRAME_GARBAGED_P (XFRAME (w->frame))
20608 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20609 {
20610 struct text_pos lpoint;
20611 struct buffer *old = current_buffer;
20612
20613 /* Set the window's buffer for the mode line display. */
20614 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20615 set_buffer_internal_1 (XBUFFER (w->contents));
20616
20617 /* Point refers normally to the selected window. For any
20618 other window, set up appropriate value. */
20619 if (!EQ (window, selected_window))
20620 {
20621 struct text_pos pt;
20622
20623 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20624 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20625 }
20626
20627 /* Display mode lines. */
20628 clear_glyph_matrix (w->desired_matrix);
20629 if (display_mode_lines (w))
20630 {
20631 ++nwindows;
20632 w->must_be_updated_p = 1;
20633 }
20634
20635 /* Restore old settings. */
20636 set_buffer_internal_1 (old);
20637 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20638 }
20639
20640 window = w->next;
20641 }
20642
20643 return nwindows;
20644 }
20645
20646
20647 /* Display the mode and/or header line of window W. Value is the
20648 sum number of mode lines and header lines displayed. */
20649
20650 static int
20651 display_mode_lines (struct window *w)
20652 {
20653 Lisp_Object old_selected_window = selected_window;
20654 Lisp_Object old_selected_frame = selected_frame;
20655 Lisp_Object new_frame = w->frame;
20656 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20657 int n = 0;
20658
20659 selected_frame = new_frame;
20660 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20661 or window's point, then we'd need select_window_1 here as well. */
20662 XSETWINDOW (selected_window, w);
20663 XFRAME (new_frame)->selected_window = selected_window;
20664
20665 /* These will be set while the mode line specs are processed. */
20666 line_number_displayed = 0;
20667 w->column_number_displayed = -1;
20668
20669 if (WINDOW_WANTS_MODELINE_P (w))
20670 {
20671 struct window *sel_w = XWINDOW (old_selected_window);
20672
20673 /* Select mode line face based on the real selected window. */
20674 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20675 BVAR (current_buffer, mode_line_format));
20676 ++n;
20677 }
20678
20679 if (WINDOW_WANTS_HEADER_LINE_P (w))
20680 {
20681 display_mode_line (w, HEADER_LINE_FACE_ID,
20682 BVAR (current_buffer, header_line_format));
20683 ++n;
20684 }
20685
20686 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20687 selected_frame = old_selected_frame;
20688 selected_window = old_selected_window;
20689 return n;
20690 }
20691
20692
20693 /* Display mode or header line of window W. FACE_ID specifies which
20694 line to display; it is either MODE_LINE_FACE_ID or
20695 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20696 display. Value is the pixel height of the mode/header line
20697 displayed. */
20698
20699 static int
20700 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20701 {
20702 struct it it;
20703 struct face *face;
20704 ptrdiff_t count = SPECPDL_INDEX ();
20705
20706 init_iterator (&it, w, -1, -1, NULL, face_id);
20707 /* Don't extend on a previously drawn mode-line.
20708 This may happen if called from pos_visible_p. */
20709 it.glyph_row->enabled_p = 0;
20710 prepare_desired_row (it.glyph_row);
20711
20712 it.glyph_row->mode_line_p = 1;
20713
20714 /* FIXME: This should be controlled by a user option. But
20715 supporting such an option is not trivial, since the mode line is
20716 made up of many separate strings. */
20717 it.paragraph_embedding = L2R;
20718
20719 record_unwind_protect (unwind_format_mode_line,
20720 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20721
20722 mode_line_target = MODE_LINE_DISPLAY;
20723
20724 /* Temporarily make frame's keyboard the current kboard so that
20725 kboard-local variables in the mode_line_format will get the right
20726 values. */
20727 push_kboard (FRAME_KBOARD (it.f));
20728 record_unwind_save_match_data ();
20729 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20730 pop_kboard ();
20731
20732 unbind_to (count, Qnil);
20733
20734 /* Fill up with spaces. */
20735 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20736
20737 compute_line_metrics (&it);
20738 it.glyph_row->full_width_p = 1;
20739 it.glyph_row->continued_p = 0;
20740 it.glyph_row->truncated_on_left_p = 0;
20741 it.glyph_row->truncated_on_right_p = 0;
20742
20743 /* Make a 3D mode-line have a shadow at its right end. */
20744 face = FACE_FROM_ID (it.f, face_id);
20745 extend_face_to_end_of_line (&it);
20746 if (face->box != FACE_NO_BOX)
20747 {
20748 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20749 + it.glyph_row->used[TEXT_AREA] - 1);
20750 last->right_box_line_p = 1;
20751 }
20752
20753 return it.glyph_row->height;
20754 }
20755
20756 /* Move element ELT in LIST to the front of LIST.
20757 Return the updated list. */
20758
20759 static Lisp_Object
20760 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20761 {
20762 register Lisp_Object tail, prev;
20763 register Lisp_Object tem;
20764
20765 tail = list;
20766 prev = Qnil;
20767 while (CONSP (tail))
20768 {
20769 tem = XCAR (tail);
20770
20771 if (EQ (elt, tem))
20772 {
20773 /* Splice out the link TAIL. */
20774 if (NILP (prev))
20775 list = XCDR (tail);
20776 else
20777 Fsetcdr (prev, XCDR (tail));
20778
20779 /* Now make it the first. */
20780 Fsetcdr (tail, list);
20781 return tail;
20782 }
20783 else
20784 prev = tail;
20785 tail = XCDR (tail);
20786 QUIT;
20787 }
20788
20789 /* Not found--return unchanged LIST. */
20790 return list;
20791 }
20792
20793 /* Contribute ELT to the mode line for window IT->w. How it
20794 translates into text depends on its data type.
20795
20796 IT describes the display environment in which we display, as usual.
20797
20798 DEPTH is the depth in recursion. It is used to prevent
20799 infinite recursion here.
20800
20801 FIELD_WIDTH is the number of characters the display of ELT should
20802 occupy in the mode line, and PRECISION is the maximum number of
20803 characters to display from ELT's representation. See
20804 display_string for details.
20805
20806 Returns the hpos of the end of the text generated by ELT.
20807
20808 PROPS is a property list to add to any string we encounter.
20809
20810 If RISKY is nonzero, remove (disregard) any properties in any string
20811 we encounter, and ignore :eval and :propertize.
20812
20813 The global variable `mode_line_target' determines whether the
20814 output is passed to `store_mode_line_noprop',
20815 `store_mode_line_string', or `display_string'. */
20816
20817 static int
20818 display_mode_element (struct it *it, int depth, int field_width, int precision,
20819 Lisp_Object elt, Lisp_Object props, int risky)
20820 {
20821 int n = 0, field, prec;
20822 int literal = 0;
20823
20824 tail_recurse:
20825 if (depth > 100)
20826 elt = build_string ("*too-deep*");
20827
20828 depth++;
20829
20830 switch (XTYPE (elt))
20831 {
20832 case Lisp_String:
20833 {
20834 /* A string: output it and check for %-constructs within it. */
20835 unsigned char c;
20836 ptrdiff_t offset = 0;
20837
20838 if (SCHARS (elt) > 0
20839 && (!NILP (props) || risky))
20840 {
20841 Lisp_Object oprops, aelt;
20842 oprops = Ftext_properties_at (make_number (0), elt);
20843
20844 /* If the starting string's properties are not what
20845 we want, translate the string. Also, if the string
20846 is risky, do that anyway. */
20847
20848 if (NILP (Fequal (props, oprops)) || risky)
20849 {
20850 /* If the starting string has properties,
20851 merge the specified ones onto the existing ones. */
20852 if (! NILP (oprops) && !risky)
20853 {
20854 Lisp_Object tem;
20855
20856 oprops = Fcopy_sequence (oprops);
20857 tem = props;
20858 while (CONSP (tem))
20859 {
20860 oprops = Fplist_put (oprops, XCAR (tem),
20861 XCAR (XCDR (tem)));
20862 tem = XCDR (XCDR (tem));
20863 }
20864 props = oprops;
20865 }
20866
20867 aelt = Fassoc (elt, mode_line_proptrans_alist);
20868 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20869 {
20870 /* AELT is what we want. Move it to the front
20871 without consing. */
20872 elt = XCAR (aelt);
20873 mode_line_proptrans_alist
20874 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20875 }
20876 else
20877 {
20878 Lisp_Object tem;
20879
20880 /* If AELT has the wrong props, it is useless.
20881 so get rid of it. */
20882 if (! NILP (aelt))
20883 mode_line_proptrans_alist
20884 = Fdelq (aelt, mode_line_proptrans_alist);
20885
20886 elt = Fcopy_sequence (elt);
20887 Fset_text_properties (make_number (0), Flength (elt),
20888 props, elt);
20889 /* Add this item to mode_line_proptrans_alist. */
20890 mode_line_proptrans_alist
20891 = Fcons (Fcons (elt, props),
20892 mode_line_proptrans_alist);
20893 /* Truncate mode_line_proptrans_alist
20894 to at most 50 elements. */
20895 tem = Fnthcdr (make_number (50),
20896 mode_line_proptrans_alist);
20897 if (! NILP (tem))
20898 XSETCDR (tem, Qnil);
20899 }
20900 }
20901 }
20902
20903 offset = 0;
20904
20905 if (literal)
20906 {
20907 prec = precision - n;
20908 switch (mode_line_target)
20909 {
20910 case MODE_LINE_NOPROP:
20911 case MODE_LINE_TITLE:
20912 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20913 break;
20914 case MODE_LINE_STRING:
20915 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20916 break;
20917 case MODE_LINE_DISPLAY:
20918 n += display_string (NULL, elt, Qnil, 0, 0, it,
20919 0, prec, 0, STRING_MULTIBYTE (elt));
20920 break;
20921 }
20922
20923 break;
20924 }
20925
20926 /* Handle the non-literal case. */
20927
20928 while ((precision <= 0 || n < precision)
20929 && SREF (elt, offset) != 0
20930 && (mode_line_target != MODE_LINE_DISPLAY
20931 || it->current_x < it->last_visible_x))
20932 {
20933 ptrdiff_t last_offset = offset;
20934
20935 /* Advance to end of string or next format specifier. */
20936 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20937 ;
20938
20939 if (offset - 1 != last_offset)
20940 {
20941 ptrdiff_t nchars, nbytes;
20942
20943 /* Output to end of string or up to '%'. Field width
20944 is length of string. Don't output more than
20945 PRECISION allows us. */
20946 offset--;
20947
20948 prec = c_string_width (SDATA (elt) + last_offset,
20949 offset - last_offset, precision - n,
20950 &nchars, &nbytes);
20951
20952 switch (mode_line_target)
20953 {
20954 case MODE_LINE_NOPROP:
20955 case MODE_LINE_TITLE:
20956 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20957 break;
20958 case MODE_LINE_STRING:
20959 {
20960 ptrdiff_t bytepos = last_offset;
20961 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20962 ptrdiff_t endpos = (precision <= 0
20963 ? string_byte_to_char (elt, offset)
20964 : charpos + nchars);
20965
20966 n += store_mode_line_string (NULL,
20967 Fsubstring (elt, make_number (charpos),
20968 make_number (endpos)),
20969 0, 0, 0, Qnil);
20970 }
20971 break;
20972 case MODE_LINE_DISPLAY:
20973 {
20974 ptrdiff_t bytepos = last_offset;
20975 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20976
20977 if (precision <= 0)
20978 nchars = string_byte_to_char (elt, offset) - charpos;
20979 n += display_string (NULL, elt, Qnil, 0, charpos,
20980 it, 0, nchars, 0,
20981 STRING_MULTIBYTE (elt));
20982 }
20983 break;
20984 }
20985 }
20986 else /* c == '%' */
20987 {
20988 ptrdiff_t percent_position = offset;
20989
20990 /* Get the specified minimum width. Zero means
20991 don't pad. */
20992 field = 0;
20993 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20994 field = field * 10 + c - '0';
20995
20996 /* Don't pad beyond the total padding allowed. */
20997 if (field_width - n > 0 && field > field_width - n)
20998 field = field_width - n;
20999
21000 /* Note that either PRECISION <= 0 or N < PRECISION. */
21001 prec = precision - n;
21002
21003 if (c == 'M')
21004 n += display_mode_element (it, depth, field, prec,
21005 Vglobal_mode_string, props,
21006 risky);
21007 else if (c != 0)
21008 {
21009 bool multibyte;
21010 ptrdiff_t bytepos, charpos;
21011 const char *spec;
21012 Lisp_Object string;
21013
21014 bytepos = percent_position;
21015 charpos = (STRING_MULTIBYTE (elt)
21016 ? string_byte_to_char (elt, bytepos)
21017 : bytepos);
21018 spec = decode_mode_spec (it->w, c, field, &string);
21019 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21020
21021 switch (mode_line_target)
21022 {
21023 case MODE_LINE_NOPROP:
21024 case MODE_LINE_TITLE:
21025 n += store_mode_line_noprop (spec, field, prec);
21026 break;
21027 case MODE_LINE_STRING:
21028 {
21029 Lisp_Object tem = build_string (spec);
21030 props = Ftext_properties_at (make_number (charpos), elt);
21031 /* Should only keep face property in props */
21032 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21033 }
21034 break;
21035 case MODE_LINE_DISPLAY:
21036 {
21037 int nglyphs_before, nwritten;
21038
21039 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21040 nwritten = display_string (spec, string, elt,
21041 charpos, 0, it,
21042 field, prec, 0,
21043 multibyte);
21044
21045 /* Assign to the glyphs written above the
21046 string where the `%x' came from, position
21047 of the `%'. */
21048 if (nwritten > 0)
21049 {
21050 struct glyph *glyph
21051 = (it->glyph_row->glyphs[TEXT_AREA]
21052 + nglyphs_before);
21053 int i;
21054
21055 for (i = 0; i < nwritten; ++i)
21056 {
21057 glyph[i].object = elt;
21058 glyph[i].charpos = charpos;
21059 }
21060
21061 n += nwritten;
21062 }
21063 }
21064 break;
21065 }
21066 }
21067 else /* c == 0 */
21068 break;
21069 }
21070 }
21071 }
21072 break;
21073
21074 case Lisp_Symbol:
21075 /* A symbol: process the value of the symbol recursively
21076 as if it appeared here directly. Avoid error if symbol void.
21077 Special case: if value of symbol is a string, output the string
21078 literally. */
21079 {
21080 register Lisp_Object tem;
21081
21082 /* If the variable is not marked as risky to set
21083 then its contents are risky to use. */
21084 if (NILP (Fget (elt, Qrisky_local_variable)))
21085 risky = 1;
21086
21087 tem = Fboundp (elt);
21088 if (!NILP (tem))
21089 {
21090 tem = Fsymbol_value (elt);
21091 /* If value is a string, output that string literally:
21092 don't check for % within it. */
21093 if (STRINGP (tem))
21094 literal = 1;
21095
21096 if (!EQ (tem, elt))
21097 {
21098 /* Give up right away for nil or t. */
21099 elt = tem;
21100 goto tail_recurse;
21101 }
21102 }
21103 }
21104 break;
21105
21106 case Lisp_Cons:
21107 {
21108 register Lisp_Object car, tem;
21109
21110 /* A cons cell: five distinct cases.
21111 If first element is :eval or :propertize, do something special.
21112 If first element is a string or a cons, process all the elements
21113 and effectively concatenate them.
21114 If first element is a negative number, truncate displaying cdr to
21115 at most that many characters. If positive, pad (with spaces)
21116 to at least that many characters.
21117 If first element is a symbol, process the cadr or caddr recursively
21118 according to whether the symbol's value is non-nil or nil. */
21119 car = XCAR (elt);
21120 if (EQ (car, QCeval))
21121 {
21122 /* An element of the form (:eval FORM) means evaluate FORM
21123 and use the result as mode line elements. */
21124
21125 if (risky)
21126 break;
21127
21128 if (CONSP (XCDR (elt)))
21129 {
21130 Lisp_Object spec;
21131 spec = safe_eval (XCAR (XCDR (elt)));
21132 n += display_mode_element (it, depth, field_width - n,
21133 precision - n, spec, props,
21134 risky);
21135 }
21136 }
21137 else if (EQ (car, QCpropertize))
21138 {
21139 /* An element of the form (:propertize ELT PROPS...)
21140 means display ELT but applying properties PROPS. */
21141
21142 if (risky)
21143 break;
21144
21145 if (CONSP (XCDR (elt)))
21146 n += display_mode_element (it, depth, field_width - n,
21147 precision - n, XCAR (XCDR (elt)),
21148 XCDR (XCDR (elt)), risky);
21149 }
21150 else if (SYMBOLP (car))
21151 {
21152 tem = Fboundp (car);
21153 elt = XCDR (elt);
21154 if (!CONSP (elt))
21155 goto invalid;
21156 /* elt is now the cdr, and we know it is a cons cell.
21157 Use its car if CAR has a non-nil value. */
21158 if (!NILP (tem))
21159 {
21160 tem = Fsymbol_value (car);
21161 if (!NILP (tem))
21162 {
21163 elt = XCAR (elt);
21164 goto tail_recurse;
21165 }
21166 }
21167 /* Symbol's value is nil (or symbol is unbound)
21168 Get the cddr of the original list
21169 and if possible find the caddr and use that. */
21170 elt = XCDR (elt);
21171 if (NILP (elt))
21172 break;
21173 else if (!CONSP (elt))
21174 goto invalid;
21175 elt = XCAR (elt);
21176 goto tail_recurse;
21177 }
21178 else if (INTEGERP (car))
21179 {
21180 register int lim = XINT (car);
21181 elt = XCDR (elt);
21182 if (lim < 0)
21183 {
21184 /* Negative int means reduce maximum width. */
21185 if (precision <= 0)
21186 precision = -lim;
21187 else
21188 precision = min (precision, -lim);
21189 }
21190 else if (lim > 0)
21191 {
21192 /* Padding specified. Don't let it be more than
21193 current maximum. */
21194 if (precision > 0)
21195 lim = min (precision, lim);
21196
21197 /* If that's more padding than already wanted, queue it.
21198 But don't reduce padding already specified even if
21199 that is beyond the current truncation point. */
21200 field_width = max (lim, field_width);
21201 }
21202 goto tail_recurse;
21203 }
21204 else if (STRINGP (car) || CONSP (car))
21205 {
21206 Lisp_Object halftail = elt;
21207 int len = 0;
21208
21209 while (CONSP (elt)
21210 && (precision <= 0 || n < precision))
21211 {
21212 n += display_mode_element (it, depth,
21213 /* Do padding only after the last
21214 element in the list. */
21215 (! CONSP (XCDR (elt))
21216 ? field_width - n
21217 : 0),
21218 precision - n, XCAR (elt),
21219 props, risky);
21220 elt = XCDR (elt);
21221 len++;
21222 if ((len & 1) == 0)
21223 halftail = XCDR (halftail);
21224 /* Check for cycle. */
21225 if (EQ (halftail, elt))
21226 break;
21227 }
21228 }
21229 }
21230 break;
21231
21232 default:
21233 invalid:
21234 elt = build_string ("*invalid*");
21235 goto tail_recurse;
21236 }
21237
21238 /* Pad to FIELD_WIDTH. */
21239 if (field_width > 0 && n < field_width)
21240 {
21241 switch (mode_line_target)
21242 {
21243 case MODE_LINE_NOPROP:
21244 case MODE_LINE_TITLE:
21245 n += store_mode_line_noprop ("", field_width - n, 0);
21246 break;
21247 case MODE_LINE_STRING:
21248 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21249 break;
21250 case MODE_LINE_DISPLAY:
21251 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21252 0, 0, 0);
21253 break;
21254 }
21255 }
21256
21257 return n;
21258 }
21259
21260 /* Store a mode-line string element in mode_line_string_list.
21261
21262 If STRING is non-null, display that C string. Otherwise, the Lisp
21263 string LISP_STRING is displayed.
21264
21265 FIELD_WIDTH is the minimum number of output glyphs to produce.
21266 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21267 with spaces. FIELD_WIDTH <= 0 means don't pad.
21268
21269 PRECISION is the maximum number of characters to output from
21270 STRING. PRECISION <= 0 means don't truncate the string.
21271
21272 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21273 properties to the string.
21274
21275 PROPS are the properties to add to the string.
21276 The mode_line_string_face face property is always added to the string.
21277 */
21278
21279 static int
21280 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21281 int field_width, int precision, Lisp_Object props)
21282 {
21283 ptrdiff_t len;
21284 int n = 0;
21285
21286 if (string != NULL)
21287 {
21288 len = strlen (string);
21289 if (precision > 0 && len > precision)
21290 len = precision;
21291 lisp_string = make_string (string, len);
21292 if (NILP (props))
21293 props = mode_line_string_face_prop;
21294 else if (!NILP (mode_line_string_face))
21295 {
21296 Lisp_Object face = Fplist_get (props, Qface);
21297 props = Fcopy_sequence (props);
21298 if (NILP (face))
21299 face = mode_line_string_face;
21300 else
21301 face = list2 (face, mode_line_string_face);
21302 props = Fplist_put (props, Qface, face);
21303 }
21304 Fadd_text_properties (make_number (0), make_number (len),
21305 props, lisp_string);
21306 }
21307 else
21308 {
21309 len = XFASTINT (Flength (lisp_string));
21310 if (precision > 0 && len > precision)
21311 {
21312 len = precision;
21313 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21314 precision = -1;
21315 }
21316 if (!NILP (mode_line_string_face))
21317 {
21318 Lisp_Object face;
21319 if (NILP (props))
21320 props = Ftext_properties_at (make_number (0), lisp_string);
21321 face = Fplist_get (props, Qface);
21322 if (NILP (face))
21323 face = mode_line_string_face;
21324 else
21325 face = list2 (face, mode_line_string_face);
21326 props = list2 (Qface, face);
21327 if (copy_string)
21328 lisp_string = Fcopy_sequence (lisp_string);
21329 }
21330 if (!NILP (props))
21331 Fadd_text_properties (make_number (0), make_number (len),
21332 props, lisp_string);
21333 }
21334
21335 if (len > 0)
21336 {
21337 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21338 n += len;
21339 }
21340
21341 if (field_width > len)
21342 {
21343 field_width -= len;
21344 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21345 if (!NILP (props))
21346 Fadd_text_properties (make_number (0), make_number (field_width),
21347 props, lisp_string);
21348 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21349 n += field_width;
21350 }
21351
21352 return n;
21353 }
21354
21355
21356 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21357 1, 4, 0,
21358 doc: /* Format a string out of a mode line format specification.
21359 First arg FORMAT specifies the mode line format (see `mode-line-format'
21360 for details) to use.
21361
21362 By default, the format is evaluated for the currently selected window.
21363
21364 Optional second arg FACE specifies the face property to put on all
21365 characters for which no face is specified. The value nil means the
21366 default face. The value t means whatever face the window's mode line
21367 currently uses (either `mode-line' or `mode-line-inactive',
21368 depending on whether the window is the selected window or not).
21369 An integer value means the value string has no text
21370 properties.
21371
21372 Optional third and fourth args WINDOW and BUFFER specify the window
21373 and buffer to use as the context for the formatting (defaults
21374 are the selected window and the WINDOW's buffer). */)
21375 (Lisp_Object format, Lisp_Object face,
21376 Lisp_Object window, Lisp_Object buffer)
21377 {
21378 struct it it;
21379 int len;
21380 struct window *w;
21381 struct buffer *old_buffer = NULL;
21382 int face_id;
21383 int no_props = INTEGERP (face);
21384 ptrdiff_t count = SPECPDL_INDEX ();
21385 Lisp_Object str;
21386 int string_start = 0;
21387
21388 w = decode_any_window (window);
21389 XSETWINDOW (window, w);
21390
21391 if (NILP (buffer))
21392 buffer = w->contents;
21393 CHECK_BUFFER (buffer);
21394
21395 /* Make formatting the modeline a non-op when noninteractive, otherwise
21396 there will be problems later caused by a partially initialized frame. */
21397 if (NILP (format) || noninteractive)
21398 return empty_unibyte_string;
21399
21400 if (no_props)
21401 face = Qnil;
21402
21403 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21404 : EQ (face, Qt) ? (EQ (window, selected_window)
21405 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21406 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21407 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21408 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21409 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21410 : DEFAULT_FACE_ID;
21411
21412 old_buffer = current_buffer;
21413
21414 /* Save things including mode_line_proptrans_alist,
21415 and set that to nil so that we don't alter the outer value. */
21416 record_unwind_protect (unwind_format_mode_line,
21417 format_mode_line_unwind_data
21418 (XFRAME (WINDOW_FRAME (w)),
21419 old_buffer, selected_window, 1));
21420 mode_line_proptrans_alist = Qnil;
21421
21422 Fselect_window (window, Qt);
21423 set_buffer_internal_1 (XBUFFER (buffer));
21424
21425 init_iterator (&it, w, -1, -1, NULL, face_id);
21426
21427 if (no_props)
21428 {
21429 mode_line_target = MODE_LINE_NOPROP;
21430 mode_line_string_face_prop = Qnil;
21431 mode_line_string_list = Qnil;
21432 string_start = MODE_LINE_NOPROP_LEN (0);
21433 }
21434 else
21435 {
21436 mode_line_target = MODE_LINE_STRING;
21437 mode_line_string_list = Qnil;
21438 mode_line_string_face = face;
21439 mode_line_string_face_prop
21440 = NILP (face) ? Qnil : list2 (Qface, face);
21441 }
21442
21443 push_kboard (FRAME_KBOARD (it.f));
21444 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21445 pop_kboard ();
21446
21447 if (no_props)
21448 {
21449 len = MODE_LINE_NOPROP_LEN (string_start);
21450 str = make_string (mode_line_noprop_buf + string_start, len);
21451 }
21452 else
21453 {
21454 mode_line_string_list = Fnreverse (mode_line_string_list);
21455 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21456 empty_unibyte_string);
21457 }
21458
21459 unbind_to (count, Qnil);
21460 return str;
21461 }
21462
21463 /* Write a null-terminated, right justified decimal representation of
21464 the positive integer D to BUF using a minimal field width WIDTH. */
21465
21466 static void
21467 pint2str (register char *buf, register int width, register ptrdiff_t d)
21468 {
21469 register char *p = buf;
21470
21471 if (d <= 0)
21472 *p++ = '0';
21473 else
21474 {
21475 while (d > 0)
21476 {
21477 *p++ = d % 10 + '0';
21478 d /= 10;
21479 }
21480 }
21481
21482 for (width -= (int) (p - buf); width > 0; --width)
21483 *p++ = ' ';
21484 *p-- = '\0';
21485 while (p > buf)
21486 {
21487 d = *buf;
21488 *buf++ = *p;
21489 *p-- = d;
21490 }
21491 }
21492
21493 /* Write a null-terminated, right justified decimal and "human
21494 readable" representation of the nonnegative integer D to BUF using
21495 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21496
21497 static const char power_letter[] =
21498 {
21499 0, /* no letter */
21500 'k', /* kilo */
21501 'M', /* mega */
21502 'G', /* giga */
21503 'T', /* tera */
21504 'P', /* peta */
21505 'E', /* exa */
21506 'Z', /* zetta */
21507 'Y' /* yotta */
21508 };
21509
21510 static void
21511 pint2hrstr (char *buf, int width, ptrdiff_t d)
21512 {
21513 /* We aim to represent the nonnegative integer D as
21514 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21515 ptrdiff_t quotient = d;
21516 int remainder = 0;
21517 /* -1 means: do not use TENTHS. */
21518 int tenths = -1;
21519 int exponent = 0;
21520
21521 /* Length of QUOTIENT.TENTHS as a string. */
21522 int length;
21523
21524 char * psuffix;
21525 char * p;
21526
21527 if (quotient >= 1000)
21528 {
21529 /* Scale to the appropriate EXPONENT. */
21530 do
21531 {
21532 remainder = quotient % 1000;
21533 quotient /= 1000;
21534 exponent++;
21535 }
21536 while (quotient >= 1000);
21537
21538 /* Round to nearest and decide whether to use TENTHS or not. */
21539 if (quotient <= 9)
21540 {
21541 tenths = remainder / 100;
21542 if (remainder % 100 >= 50)
21543 {
21544 if (tenths < 9)
21545 tenths++;
21546 else
21547 {
21548 quotient++;
21549 if (quotient == 10)
21550 tenths = -1;
21551 else
21552 tenths = 0;
21553 }
21554 }
21555 }
21556 else
21557 if (remainder >= 500)
21558 {
21559 if (quotient < 999)
21560 quotient++;
21561 else
21562 {
21563 quotient = 1;
21564 exponent++;
21565 tenths = 0;
21566 }
21567 }
21568 }
21569
21570 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21571 if (tenths == -1 && quotient <= 99)
21572 if (quotient <= 9)
21573 length = 1;
21574 else
21575 length = 2;
21576 else
21577 length = 3;
21578 p = psuffix = buf + max (width, length);
21579
21580 /* Print EXPONENT. */
21581 *psuffix++ = power_letter[exponent];
21582 *psuffix = '\0';
21583
21584 /* Print TENTHS. */
21585 if (tenths >= 0)
21586 {
21587 *--p = '0' + tenths;
21588 *--p = '.';
21589 }
21590
21591 /* Print QUOTIENT. */
21592 do
21593 {
21594 int digit = quotient % 10;
21595 *--p = '0' + digit;
21596 }
21597 while ((quotient /= 10) != 0);
21598
21599 /* Print leading spaces. */
21600 while (buf < p)
21601 *--p = ' ';
21602 }
21603
21604 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21605 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21606 type of CODING_SYSTEM. Return updated pointer into BUF. */
21607
21608 static unsigned char invalid_eol_type[] = "(*invalid*)";
21609
21610 static char *
21611 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21612 {
21613 Lisp_Object val;
21614 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21615 const unsigned char *eol_str;
21616 int eol_str_len;
21617 /* The EOL conversion we are using. */
21618 Lisp_Object eoltype;
21619
21620 val = CODING_SYSTEM_SPEC (coding_system);
21621 eoltype = Qnil;
21622
21623 if (!VECTORP (val)) /* Not yet decided. */
21624 {
21625 *buf++ = multibyte ? '-' : ' ';
21626 if (eol_flag)
21627 eoltype = eol_mnemonic_undecided;
21628 /* Don't mention EOL conversion if it isn't decided. */
21629 }
21630 else
21631 {
21632 Lisp_Object attrs;
21633 Lisp_Object eolvalue;
21634
21635 attrs = AREF (val, 0);
21636 eolvalue = AREF (val, 2);
21637
21638 *buf++ = multibyte
21639 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21640 : ' ';
21641
21642 if (eol_flag)
21643 {
21644 /* The EOL conversion that is normal on this system. */
21645
21646 if (NILP (eolvalue)) /* Not yet decided. */
21647 eoltype = eol_mnemonic_undecided;
21648 else if (VECTORP (eolvalue)) /* Not yet decided. */
21649 eoltype = eol_mnemonic_undecided;
21650 else /* eolvalue is Qunix, Qdos, or Qmac. */
21651 eoltype = (EQ (eolvalue, Qunix)
21652 ? eol_mnemonic_unix
21653 : (EQ (eolvalue, Qdos) == 1
21654 ? eol_mnemonic_dos : eol_mnemonic_mac));
21655 }
21656 }
21657
21658 if (eol_flag)
21659 {
21660 /* Mention the EOL conversion if it is not the usual one. */
21661 if (STRINGP (eoltype))
21662 {
21663 eol_str = SDATA (eoltype);
21664 eol_str_len = SBYTES (eoltype);
21665 }
21666 else if (CHARACTERP (eoltype))
21667 {
21668 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21669 int c = XFASTINT (eoltype);
21670 eol_str_len = CHAR_STRING (c, tmp);
21671 eol_str = tmp;
21672 }
21673 else
21674 {
21675 eol_str = invalid_eol_type;
21676 eol_str_len = sizeof (invalid_eol_type) - 1;
21677 }
21678 memcpy (buf, eol_str, eol_str_len);
21679 buf += eol_str_len;
21680 }
21681
21682 return buf;
21683 }
21684
21685 /* Return a string for the output of a mode line %-spec for window W,
21686 generated by character C. FIELD_WIDTH > 0 means pad the string
21687 returned with spaces to that value. Return a Lisp string in
21688 *STRING if the resulting string is taken from that Lisp string.
21689
21690 Note we operate on the current buffer for most purposes. */
21691
21692 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21693
21694 static const char *
21695 decode_mode_spec (struct window *w, register int c, int field_width,
21696 Lisp_Object *string)
21697 {
21698 Lisp_Object obj;
21699 struct frame *f = XFRAME (WINDOW_FRAME (w));
21700 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21701 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21702 produce strings from numerical values, so limit preposterously
21703 large values of FIELD_WIDTH to avoid overrunning the buffer's
21704 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21705 bytes plus the terminating null. */
21706 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21707 struct buffer *b = current_buffer;
21708
21709 obj = Qnil;
21710 *string = Qnil;
21711
21712 switch (c)
21713 {
21714 case '*':
21715 if (!NILP (BVAR (b, read_only)))
21716 return "%";
21717 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21718 return "*";
21719 return "-";
21720
21721 case '+':
21722 /* This differs from %* only for a modified read-only buffer. */
21723 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21724 return "*";
21725 if (!NILP (BVAR (b, read_only)))
21726 return "%";
21727 return "-";
21728
21729 case '&':
21730 /* This differs from %* in ignoring read-only-ness. */
21731 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21732 return "*";
21733 return "-";
21734
21735 case '%':
21736 return "%";
21737
21738 case '[':
21739 {
21740 int i;
21741 char *p;
21742
21743 if (command_loop_level > 5)
21744 return "[[[... ";
21745 p = decode_mode_spec_buf;
21746 for (i = 0; i < command_loop_level; i++)
21747 *p++ = '[';
21748 *p = 0;
21749 return decode_mode_spec_buf;
21750 }
21751
21752 case ']':
21753 {
21754 int i;
21755 char *p;
21756
21757 if (command_loop_level > 5)
21758 return " ...]]]";
21759 p = decode_mode_spec_buf;
21760 for (i = 0; i < command_loop_level; i++)
21761 *p++ = ']';
21762 *p = 0;
21763 return decode_mode_spec_buf;
21764 }
21765
21766 case '-':
21767 {
21768 register int i;
21769
21770 /* Let lots_of_dashes be a string of infinite length. */
21771 if (mode_line_target == MODE_LINE_NOPROP
21772 || mode_line_target == MODE_LINE_STRING)
21773 return "--";
21774 if (field_width <= 0
21775 || field_width > sizeof (lots_of_dashes))
21776 {
21777 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21778 decode_mode_spec_buf[i] = '-';
21779 decode_mode_spec_buf[i] = '\0';
21780 return decode_mode_spec_buf;
21781 }
21782 else
21783 return lots_of_dashes;
21784 }
21785
21786 case 'b':
21787 obj = BVAR (b, name);
21788 break;
21789
21790 case 'c':
21791 /* %c and %l are ignored in `frame-title-format'.
21792 (In redisplay_internal, the frame title is drawn _before_ the
21793 windows are updated, so the stuff which depends on actual
21794 window contents (such as %l) may fail to render properly, or
21795 even crash emacs.) */
21796 if (mode_line_target == MODE_LINE_TITLE)
21797 return "";
21798 else
21799 {
21800 ptrdiff_t col = current_column ();
21801 w->column_number_displayed = col;
21802 pint2str (decode_mode_spec_buf, width, col);
21803 return decode_mode_spec_buf;
21804 }
21805
21806 case 'e':
21807 #ifndef SYSTEM_MALLOC
21808 {
21809 if (NILP (Vmemory_full))
21810 return "";
21811 else
21812 return "!MEM FULL! ";
21813 }
21814 #else
21815 return "";
21816 #endif
21817
21818 case 'F':
21819 /* %F displays the frame name. */
21820 if (!NILP (f->title))
21821 return SSDATA (f->title);
21822 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21823 return SSDATA (f->name);
21824 return "Emacs";
21825
21826 case 'f':
21827 obj = BVAR (b, filename);
21828 break;
21829
21830 case 'i':
21831 {
21832 ptrdiff_t size = ZV - BEGV;
21833 pint2str (decode_mode_spec_buf, width, size);
21834 return decode_mode_spec_buf;
21835 }
21836
21837 case 'I':
21838 {
21839 ptrdiff_t size = ZV - BEGV;
21840 pint2hrstr (decode_mode_spec_buf, width, size);
21841 return decode_mode_spec_buf;
21842 }
21843
21844 case 'l':
21845 {
21846 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21847 ptrdiff_t topline, nlines, height;
21848 ptrdiff_t junk;
21849
21850 /* %c and %l are ignored in `frame-title-format'. */
21851 if (mode_line_target == MODE_LINE_TITLE)
21852 return "";
21853
21854 startpos = marker_position (w->start);
21855 startpos_byte = marker_byte_position (w->start);
21856 height = WINDOW_TOTAL_LINES (w);
21857
21858 /* If we decided that this buffer isn't suitable for line numbers,
21859 don't forget that too fast. */
21860 if (w->base_line_pos == -1)
21861 goto no_value;
21862
21863 /* If the buffer is very big, don't waste time. */
21864 if (INTEGERP (Vline_number_display_limit)
21865 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21866 {
21867 w->base_line_pos = 0;
21868 w->base_line_number = 0;
21869 goto no_value;
21870 }
21871
21872 if (w->base_line_number > 0
21873 && w->base_line_pos > 0
21874 && w->base_line_pos <= startpos)
21875 {
21876 line = w->base_line_number;
21877 linepos = w->base_line_pos;
21878 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21879 }
21880 else
21881 {
21882 line = 1;
21883 linepos = BUF_BEGV (b);
21884 linepos_byte = BUF_BEGV_BYTE (b);
21885 }
21886
21887 /* Count lines from base line to window start position. */
21888 nlines = display_count_lines (linepos_byte,
21889 startpos_byte,
21890 startpos, &junk);
21891
21892 topline = nlines + line;
21893
21894 /* Determine a new base line, if the old one is too close
21895 or too far away, or if we did not have one.
21896 "Too close" means it's plausible a scroll-down would
21897 go back past it. */
21898 if (startpos == BUF_BEGV (b))
21899 {
21900 w->base_line_number = topline;
21901 w->base_line_pos = BUF_BEGV (b);
21902 }
21903 else if (nlines < height + 25 || nlines > height * 3 + 50
21904 || linepos == BUF_BEGV (b))
21905 {
21906 ptrdiff_t limit = BUF_BEGV (b);
21907 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21908 ptrdiff_t position;
21909 ptrdiff_t distance =
21910 (height * 2 + 30) * line_number_display_limit_width;
21911
21912 if (startpos - distance > limit)
21913 {
21914 limit = startpos - distance;
21915 limit_byte = CHAR_TO_BYTE (limit);
21916 }
21917
21918 nlines = display_count_lines (startpos_byte,
21919 limit_byte,
21920 - (height * 2 + 30),
21921 &position);
21922 /* If we couldn't find the lines we wanted within
21923 line_number_display_limit_width chars per line,
21924 give up on line numbers for this window. */
21925 if (position == limit_byte && limit == startpos - distance)
21926 {
21927 w->base_line_pos = -1;
21928 w->base_line_number = 0;
21929 goto no_value;
21930 }
21931
21932 w->base_line_number = topline - nlines;
21933 w->base_line_pos = BYTE_TO_CHAR (position);
21934 }
21935
21936 /* Now count lines from the start pos to point. */
21937 nlines = display_count_lines (startpos_byte,
21938 PT_BYTE, PT, &junk);
21939
21940 /* Record that we did display the line number. */
21941 line_number_displayed = 1;
21942
21943 /* Make the string to show. */
21944 pint2str (decode_mode_spec_buf, width, topline + nlines);
21945 return decode_mode_spec_buf;
21946 no_value:
21947 {
21948 char* p = decode_mode_spec_buf;
21949 int pad = width - 2;
21950 while (pad-- > 0)
21951 *p++ = ' ';
21952 *p++ = '?';
21953 *p++ = '?';
21954 *p = '\0';
21955 return decode_mode_spec_buf;
21956 }
21957 }
21958 break;
21959
21960 case 'm':
21961 obj = BVAR (b, mode_name);
21962 break;
21963
21964 case 'n':
21965 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21966 return " Narrow";
21967 break;
21968
21969 case 'p':
21970 {
21971 ptrdiff_t pos = marker_position (w->start);
21972 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21973
21974 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21975 {
21976 if (pos <= BUF_BEGV (b))
21977 return "All";
21978 else
21979 return "Bottom";
21980 }
21981 else if (pos <= BUF_BEGV (b))
21982 return "Top";
21983 else
21984 {
21985 if (total > 1000000)
21986 /* Do it differently for a large value, to avoid overflow. */
21987 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21988 else
21989 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21990 /* We can't normally display a 3-digit number,
21991 so get us a 2-digit number that is close. */
21992 if (total == 100)
21993 total = 99;
21994 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21995 return decode_mode_spec_buf;
21996 }
21997 }
21998
21999 /* Display percentage of size above the bottom of the screen. */
22000 case 'P':
22001 {
22002 ptrdiff_t toppos = marker_position (w->start);
22003 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22004 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22005
22006 if (botpos >= BUF_ZV (b))
22007 {
22008 if (toppos <= BUF_BEGV (b))
22009 return "All";
22010 else
22011 return "Bottom";
22012 }
22013 else
22014 {
22015 if (total > 1000000)
22016 /* Do it differently for a large value, to avoid overflow. */
22017 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22018 else
22019 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22020 /* We can't normally display a 3-digit number,
22021 so get us a 2-digit number that is close. */
22022 if (total == 100)
22023 total = 99;
22024 if (toppos <= BUF_BEGV (b))
22025 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22026 else
22027 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22028 return decode_mode_spec_buf;
22029 }
22030 }
22031
22032 case 's':
22033 /* status of process */
22034 obj = Fget_buffer_process (Fcurrent_buffer ());
22035 if (NILP (obj))
22036 return "no process";
22037 #ifndef MSDOS
22038 obj = Fsymbol_name (Fprocess_status (obj));
22039 #endif
22040 break;
22041
22042 case '@':
22043 {
22044 ptrdiff_t count = inhibit_garbage_collection ();
22045 Lisp_Object val = call1 (intern ("file-remote-p"),
22046 BVAR (current_buffer, directory));
22047 unbind_to (count, Qnil);
22048
22049 if (NILP (val))
22050 return "-";
22051 else
22052 return "@";
22053 }
22054
22055 case 'z':
22056 /* coding-system (not including end-of-line format) */
22057 case 'Z':
22058 /* coding-system (including end-of-line type) */
22059 {
22060 int eol_flag = (c == 'Z');
22061 char *p = decode_mode_spec_buf;
22062
22063 if (! FRAME_WINDOW_P (f))
22064 {
22065 /* No need to mention EOL here--the terminal never needs
22066 to do EOL conversion. */
22067 p = decode_mode_spec_coding (CODING_ID_NAME
22068 (FRAME_KEYBOARD_CODING (f)->id),
22069 p, 0);
22070 p = decode_mode_spec_coding (CODING_ID_NAME
22071 (FRAME_TERMINAL_CODING (f)->id),
22072 p, 0);
22073 }
22074 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22075 p, eol_flag);
22076
22077 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22078 #ifdef subprocesses
22079 obj = Fget_buffer_process (Fcurrent_buffer ());
22080 if (PROCESSP (obj))
22081 {
22082 p = decode_mode_spec_coding
22083 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22084 p = decode_mode_spec_coding
22085 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22086 }
22087 #endif /* subprocesses */
22088 #endif /* 0 */
22089 *p = 0;
22090 return decode_mode_spec_buf;
22091 }
22092 }
22093
22094 if (STRINGP (obj))
22095 {
22096 *string = obj;
22097 return SSDATA (obj);
22098 }
22099 else
22100 return "";
22101 }
22102
22103
22104 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22105 means count lines back from START_BYTE. But don't go beyond
22106 LIMIT_BYTE. Return the number of lines thus found (always
22107 nonnegative).
22108
22109 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22110 either the position COUNT lines after/before START_BYTE, if we
22111 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22112 COUNT lines. */
22113
22114 static ptrdiff_t
22115 display_count_lines (ptrdiff_t start_byte,
22116 ptrdiff_t limit_byte, ptrdiff_t count,
22117 ptrdiff_t *byte_pos_ptr)
22118 {
22119 register unsigned char *cursor;
22120 unsigned char *base;
22121
22122 register ptrdiff_t ceiling;
22123 register unsigned char *ceiling_addr;
22124 ptrdiff_t orig_count = count;
22125
22126 /* If we are not in selective display mode,
22127 check only for newlines. */
22128 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22129 && !INTEGERP (BVAR (current_buffer, selective_display)));
22130
22131 if (count > 0)
22132 {
22133 while (start_byte < limit_byte)
22134 {
22135 ceiling = BUFFER_CEILING_OF (start_byte);
22136 ceiling = min (limit_byte - 1, ceiling);
22137 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22138 base = (cursor = BYTE_POS_ADDR (start_byte));
22139
22140 do
22141 {
22142 if (selective_display)
22143 {
22144 while (*cursor != '\n' && *cursor != 015
22145 && ++cursor != ceiling_addr)
22146 continue;
22147 if (cursor == ceiling_addr)
22148 break;
22149 }
22150 else
22151 {
22152 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22153 if (! cursor)
22154 break;
22155 }
22156
22157 cursor++;
22158
22159 if (--count == 0)
22160 {
22161 start_byte += cursor - base;
22162 *byte_pos_ptr = start_byte;
22163 return orig_count;
22164 }
22165 }
22166 while (cursor < ceiling_addr);
22167
22168 start_byte += ceiling_addr - base;
22169 }
22170 }
22171 else
22172 {
22173 while (start_byte > limit_byte)
22174 {
22175 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22176 ceiling = max (limit_byte, ceiling);
22177 ceiling_addr = BYTE_POS_ADDR (ceiling);
22178 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22179 while (1)
22180 {
22181 if (selective_display)
22182 {
22183 while (--cursor >= ceiling_addr
22184 && *cursor != '\n' && *cursor != 015)
22185 continue;
22186 if (cursor < ceiling_addr)
22187 break;
22188 }
22189 else
22190 {
22191 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22192 if (! cursor)
22193 break;
22194 }
22195
22196 if (++count == 0)
22197 {
22198 start_byte += cursor - base + 1;
22199 *byte_pos_ptr = start_byte;
22200 /* When scanning backwards, we should
22201 not count the newline posterior to which we stop. */
22202 return - orig_count - 1;
22203 }
22204 }
22205 start_byte += ceiling_addr - base;
22206 }
22207 }
22208
22209 *byte_pos_ptr = limit_byte;
22210
22211 if (count < 0)
22212 return - orig_count + count;
22213 return orig_count - count;
22214
22215 }
22216
22217
22218 \f
22219 /***********************************************************************
22220 Displaying strings
22221 ***********************************************************************/
22222
22223 /* Display a NUL-terminated string, starting with index START.
22224
22225 If STRING is non-null, display that C string. Otherwise, the Lisp
22226 string LISP_STRING is displayed. There's a case that STRING is
22227 non-null and LISP_STRING is not nil. It means STRING is a string
22228 data of LISP_STRING. In that case, we display LISP_STRING while
22229 ignoring its text properties.
22230
22231 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22232 FACE_STRING. Display STRING or LISP_STRING with the face at
22233 FACE_STRING_POS in FACE_STRING:
22234
22235 Display the string in the environment given by IT, but use the
22236 standard display table, temporarily.
22237
22238 FIELD_WIDTH is the minimum number of output glyphs to produce.
22239 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22240 with spaces. If STRING has more characters, more than FIELD_WIDTH
22241 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22242
22243 PRECISION is the maximum number of characters to output from
22244 STRING. PRECISION < 0 means don't truncate the string.
22245
22246 This is roughly equivalent to printf format specifiers:
22247
22248 FIELD_WIDTH PRECISION PRINTF
22249 ----------------------------------------
22250 -1 -1 %s
22251 -1 10 %.10s
22252 10 -1 %10s
22253 20 10 %20.10s
22254
22255 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22256 display them, and < 0 means obey the current buffer's value of
22257 enable_multibyte_characters.
22258
22259 Value is the number of columns displayed. */
22260
22261 static int
22262 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22263 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22264 int field_width, int precision, int max_x, int multibyte)
22265 {
22266 int hpos_at_start = it->hpos;
22267 int saved_face_id = it->face_id;
22268 struct glyph_row *row = it->glyph_row;
22269 ptrdiff_t it_charpos;
22270
22271 /* Initialize the iterator IT for iteration over STRING beginning
22272 with index START. */
22273 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22274 precision, field_width, multibyte);
22275 if (string && STRINGP (lisp_string))
22276 /* LISP_STRING is the one returned by decode_mode_spec. We should
22277 ignore its text properties. */
22278 it->stop_charpos = it->end_charpos;
22279
22280 /* If displaying STRING, set up the face of the iterator from
22281 FACE_STRING, if that's given. */
22282 if (STRINGP (face_string))
22283 {
22284 ptrdiff_t endptr;
22285 struct face *face;
22286
22287 it->face_id
22288 = face_at_string_position (it->w, face_string, face_string_pos,
22289 0, it->region_beg_charpos,
22290 it->region_end_charpos,
22291 &endptr, it->base_face_id, 0);
22292 face = FACE_FROM_ID (it->f, it->face_id);
22293 it->face_box_p = face->box != FACE_NO_BOX;
22294 }
22295
22296 /* Set max_x to the maximum allowed X position. Don't let it go
22297 beyond the right edge of the window. */
22298 if (max_x <= 0)
22299 max_x = it->last_visible_x;
22300 else
22301 max_x = min (max_x, it->last_visible_x);
22302
22303 /* Skip over display elements that are not visible. because IT->w is
22304 hscrolled. */
22305 if (it->current_x < it->first_visible_x)
22306 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22307 MOVE_TO_POS | MOVE_TO_X);
22308
22309 row->ascent = it->max_ascent;
22310 row->height = it->max_ascent + it->max_descent;
22311 row->phys_ascent = it->max_phys_ascent;
22312 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22313 row->extra_line_spacing = it->max_extra_line_spacing;
22314
22315 if (STRINGP (it->string))
22316 it_charpos = IT_STRING_CHARPOS (*it);
22317 else
22318 it_charpos = IT_CHARPOS (*it);
22319
22320 /* This condition is for the case that we are called with current_x
22321 past last_visible_x. */
22322 while (it->current_x < max_x)
22323 {
22324 int x_before, x, n_glyphs_before, i, nglyphs;
22325
22326 /* Get the next display element. */
22327 if (!get_next_display_element (it))
22328 break;
22329
22330 /* Produce glyphs. */
22331 x_before = it->current_x;
22332 n_glyphs_before = row->used[TEXT_AREA];
22333 PRODUCE_GLYPHS (it);
22334
22335 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22336 i = 0;
22337 x = x_before;
22338 while (i < nglyphs)
22339 {
22340 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22341
22342 if (it->line_wrap != TRUNCATE
22343 && x + glyph->pixel_width > max_x)
22344 {
22345 /* End of continued line or max_x reached. */
22346 if (CHAR_GLYPH_PADDING_P (*glyph))
22347 {
22348 /* A wide character is unbreakable. */
22349 if (row->reversed_p)
22350 unproduce_glyphs (it, row->used[TEXT_AREA]
22351 - n_glyphs_before);
22352 row->used[TEXT_AREA] = n_glyphs_before;
22353 it->current_x = x_before;
22354 }
22355 else
22356 {
22357 if (row->reversed_p)
22358 unproduce_glyphs (it, row->used[TEXT_AREA]
22359 - (n_glyphs_before + i));
22360 row->used[TEXT_AREA] = n_glyphs_before + i;
22361 it->current_x = x;
22362 }
22363 break;
22364 }
22365 else if (x + glyph->pixel_width >= it->first_visible_x)
22366 {
22367 /* Glyph is at least partially visible. */
22368 ++it->hpos;
22369 if (x < it->first_visible_x)
22370 row->x = x - it->first_visible_x;
22371 }
22372 else
22373 {
22374 /* Glyph is off the left margin of the display area.
22375 Should not happen. */
22376 emacs_abort ();
22377 }
22378
22379 row->ascent = max (row->ascent, it->max_ascent);
22380 row->height = max (row->height, it->max_ascent + it->max_descent);
22381 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22382 row->phys_height = max (row->phys_height,
22383 it->max_phys_ascent + it->max_phys_descent);
22384 row->extra_line_spacing = max (row->extra_line_spacing,
22385 it->max_extra_line_spacing);
22386 x += glyph->pixel_width;
22387 ++i;
22388 }
22389
22390 /* Stop if max_x reached. */
22391 if (i < nglyphs)
22392 break;
22393
22394 /* Stop at line ends. */
22395 if (ITERATOR_AT_END_OF_LINE_P (it))
22396 {
22397 it->continuation_lines_width = 0;
22398 break;
22399 }
22400
22401 set_iterator_to_next (it, 1);
22402 if (STRINGP (it->string))
22403 it_charpos = IT_STRING_CHARPOS (*it);
22404 else
22405 it_charpos = IT_CHARPOS (*it);
22406
22407 /* Stop if truncating at the right edge. */
22408 if (it->line_wrap == TRUNCATE
22409 && it->current_x >= it->last_visible_x)
22410 {
22411 /* Add truncation mark, but don't do it if the line is
22412 truncated at a padding space. */
22413 if (it_charpos < it->string_nchars)
22414 {
22415 if (!FRAME_WINDOW_P (it->f))
22416 {
22417 int ii, n;
22418
22419 if (it->current_x > it->last_visible_x)
22420 {
22421 if (!row->reversed_p)
22422 {
22423 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22424 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22425 break;
22426 }
22427 else
22428 {
22429 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22430 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22431 break;
22432 unproduce_glyphs (it, ii + 1);
22433 ii = row->used[TEXT_AREA] - (ii + 1);
22434 }
22435 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22436 {
22437 row->used[TEXT_AREA] = ii;
22438 produce_special_glyphs (it, IT_TRUNCATION);
22439 }
22440 }
22441 produce_special_glyphs (it, IT_TRUNCATION);
22442 }
22443 row->truncated_on_right_p = 1;
22444 }
22445 break;
22446 }
22447 }
22448
22449 /* Maybe insert a truncation at the left. */
22450 if (it->first_visible_x
22451 && it_charpos > 0)
22452 {
22453 if (!FRAME_WINDOW_P (it->f)
22454 || (row->reversed_p
22455 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22456 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22457 insert_left_trunc_glyphs (it);
22458 row->truncated_on_left_p = 1;
22459 }
22460
22461 it->face_id = saved_face_id;
22462
22463 /* Value is number of columns displayed. */
22464 return it->hpos - hpos_at_start;
22465 }
22466
22467
22468 \f
22469 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22470 appears as an element of LIST or as the car of an element of LIST.
22471 If PROPVAL is a list, compare each element against LIST in that
22472 way, and return 1/2 if any element of PROPVAL is found in LIST.
22473 Otherwise return 0. This function cannot quit.
22474 The return value is 2 if the text is invisible but with an ellipsis
22475 and 1 if it's invisible and without an ellipsis. */
22476
22477 int
22478 invisible_p (register Lisp_Object propval, Lisp_Object list)
22479 {
22480 register Lisp_Object tail, proptail;
22481
22482 for (tail = list; CONSP (tail); tail = XCDR (tail))
22483 {
22484 register Lisp_Object tem;
22485 tem = XCAR (tail);
22486 if (EQ (propval, tem))
22487 return 1;
22488 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22489 return NILP (XCDR (tem)) ? 1 : 2;
22490 }
22491
22492 if (CONSP (propval))
22493 {
22494 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22495 {
22496 Lisp_Object propelt;
22497 propelt = XCAR (proptail);
22498 for (tail = list; CONSP (tail); tail = XCDR (tail))
22499 {
22500 register Lisp_Object tem;
22501 tem = XCAR (tail);
22502 if (EQ (propelt, tem))
22503 return 1;
22504 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22505 return NILP (XCDR (tem)) ? 1 : 2;
22506 }
22507 }
22508 }
22509
22510 return 0;
22511 }
22512
22513 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22514 doc: /* Non-nil if the property makes the text invisible.
22515 POS-OR-PROP can be a marker or number, in which case it is taken to be
22516 a position in the current buffer and the value of the `invisible' property
22517 is checked; or it can be some other value, which is then presumed to be the
22518 value of the `invisible' property of the text of interest.
22519 The non-nil value returned can be t for truly invisible text or something
22520 else if the text is replaced by an ellipsis. */)
22521 (Lisp_Object pos_or_prop)
22522 {
22523 Lisp_Object prop
22524 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22525 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22526 : pos_or_prop);
22527 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22528 return (invis == 0 ? Qnil
22529 : invis == 1 ? Qt
22530 : make_number (invis));
22531 }
22532
22533 /* Calculate a width or height in pixels from a specification using
22534 the following elements:
22535
22536 SPEC ::=
22537 NUM - a (fractional) multiple of the default font width/height
22538 (NUM) - specifies exactly NUM pixels
22539 UNIT - a fixed number of pixels, see below.
22540 ELEMENT - size of a display element in pixels, see below.
22541 (NUM . SPEC) - equals NUM * SPEC
22542 (+ SPEC SPEC ...) - add pixel values
22543 (- SPEC SPEC ...) - subtract pixel values
22544 (- SPEC) - negate pixel value
22545
22546 NUM ::=
22547 INT or FLOAT - a number constant
22548 SYMBOL - use symbol's (buffer local) variable binding.
22549
22550 UNIT ::=
22551 in - pixels per inch *)
22552 mm - pixels per 1/1000 meter *)
22553 cm - pixels per 1/100 meter *)
22554 width - width of current font in pixels.
22555 height - height of current font in pixels.
22556
22557 *) using the ratio(s) defined in display-pixels-per-inch.
22558
22559 ELEMENT ::=
22560
22561 left-fringe - left fringe width in pixels
22562 right-fringe - right fringe width in pixels
22563
22564 left-margin - left margin width in pixels
22565 right-margin - right margin width in pixels
22566
22567 scroll-bar - scroll-bar area width in pixels
22568
22569 Examples:
22570
22571 Pixels corresponding to 5 inches:
22572 (5 . in)
22573
22574 Total width of non-text areas on left side of window (if scroll-bar is on left):
22575 '(space :width (+ left-fringe left-margin scroll-bar))
22576
22577 Align to first text column (in header line):
22578 '(space :align-to 0)
22579
22580 Align to middle of text area minus half the width of variable `my-image'
22581 containing a loaded image:
22582 '(space :align-to (0.5 . (- text my-image)))
22583
22584 Width of left margin minus width of 1 character in the default font:
22585 '(space :width (- left-margin 1))
22586
22587 Width of left margin minus width of 2 characters in the current font:
22588 '(space :width (- left-margin (2 . width)))
22589
22590 Center 1 character over left-margin (in header line):
22591 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22592
22593 Different ways to express width of left fringe plus left margin minus one pixel:
22594 '(space :width (- (+ left-fringe left-margin) (1)))
22595 '(space :width (+ left-fringe left-margin (- (1))))
22596 '(space :width (+ left-fringe left-margin (-1)))
22597
22598 */
22599
22600 static int
22601 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22602 struct font *font, int width_p, int *align_to)
22603 {
22604 double pixels;
22605
22606 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22607 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22608
22609 if (NILP (prop))
22610 return OK_PIXELS (0);
22611
22612 eassert (FRAME_LIVE_P (it->f));
22613
22614 if (SYMBOLP (prop))
22615 {
22616 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22617 {
22618 char *unit = SSDATA (SYMBOL_NAME (prop));
22619
22620 if (unit[0] == 'i' && unit[1] == 'n')
22621 pixels = 1.0;
22622 else if (unit[0] == 'm' && unit[1] == 'm')
22623 pixels = 25.4;
22624 else if (unit[0] == 'c' && unit[1] == 'm')
22625 pixels = 2.54;
22626 else
22627 pixels = 0;
22628 if (pixels > 0)
22629 {
22630 double ppi = (width_p ? FRAME_RES_X (it->f)
22631 : FRAME_RES_Y (it->f));
22632
22633 if (ppi > 0)
22634 return OK_PIXELS (ppi / pixels);
22635 return 0;
22636 }
22637 }
22638
22639 #ifdef HAVE_WINDOW_SYSTEM
22640 if (EQ (prop, Qheight))
22641 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22642 if (EQ (prop, Qwidth))
22643 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22644 #else
22645 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22646 return OK_PIXELS (1);
22647 #endif
22648
22649 if (EQ (prop, Qtext))
22650 return OK_PIXELS (width_p
22651 ? window_box_width (it->w, TEXT_AREA)
22652 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22653
22654 if (align_to && *align_to < 0)
22655 {
22656 *res = 0;
22657 if (EQ (prop, Qleft))
22658 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22659 if (EQ (prop, Qright))
22660 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22661 if (EQ (prop, Qcenter))
22662 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22663 + window_box_width (it->w, TEXT_AREA) / 2);
22664 if (EQ (prop, Qleft_fringe))
22665 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22666 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22667 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22668 if (EQ (prop, Qright_fringe))
22669 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22670 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22671 : window_box_right_offset (it->w, TEXT_AREA));
22672 if (EQ (prop, Qleft_margin))
22673 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22674 if (EQ (prop, Qright_margin))
22675 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22676 if (EQ (prop, Qscroll_bar))
22677 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22678 ? 0
22679 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22680 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22681 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22682 : 0)));
22683 }
22684 else
22685 {
22686 if (EQ (prop, Qleft_fringe))
22687 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22688 if (EQ (prop, Qright_fringe))
22689 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22690 if (EQ (prop, Qleft_margin))
22691 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22692 if (EQ (prop, Qright_margin))
22693 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22694 if (EQ (prop, Qscroll_bar))
22695 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22696 }
22697
22698 prop = buffer_local_value_1 (prop, it->w->contents);
22699 if (EQ (prop, Qunbound))
22700 prop = Qnil;
22701 }
22702
22703 if (INTEGERP (prop) || FLOATP (prop))
22704 {
22705 int base_unit = (width_p
22706 ? FRAME_COLUMN_WIDTH (it->f)
22707 : FRAME_LINE_HEIGHT (it->f));
22708 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22709 }
22710
22711 if (CONSP (prop))
22712 {
22713 Lisp_Object car = XCAR (prop);
22714 Lisp_Object cdr = XCDR (prop);
22715
22716 if (SYMBOLP (car))
22717 {
22718 #ifdef HAVE_WINDOW_SYSTEM
22719 if (FRAME_WINDOW_P (it->f)
22720 && valid_image_p (prop))
22721 {
22722 ptrdiff_t id = lookup_image (it->f, prop);
22723 struct image *img = IMAGE_FROM_ID (it->f, id);
22724
22725 return OK_PIXELS (width_p ? img->width : img->height);
22726 }
22727 #endif
22728 if (EQ (car, Qplus) || EQ (car, Qminus))
22729 {
22730 int first = 1;
22731 double px;
22732
22733 pixels = 0;
22734 while (CONSP (cdr))
22735 {
22736 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22737 font, width_p, align_to))
22738 return 0;
22739 if (first)
22740 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22741 else
22742 pixels += px;
22743 cdr = XCDR (cdr);
22744 }
22745 if (EQ (car, Qminus))
22746 pixels = -pixels;
22747 return OK_PIXELS (pixels);
22748 }
22749
22750 car = buffer_local_value_1 (car, it->w->contents);
22751 if (EQ (car, Qunbound))
22752 car = Qnil;
22753 }
22754
22755 if (INTEGERP (car) || FLOATP (car))
22756 {
22757 double fact;
22758 pixels = XFLOATINT (car);
22759 if (NILP (cdr))
22760 return OK_PIXELS (pixels);
22761 if (calc_pixel_width_or_height (&fact, it, cdr,
22762 font, width_p, align_to))
22763 return OK_PIXELS (pixels * fact);
22764 return 0;
22765 }
22766
22767 return 0;
22768 }
22769
22770 return 0;
22771 }
22772
22773 \f
22774 /***********************************************************************
22775 Glyph Display
22776 ***********************************************************************/
22777
22778 #ifdef HAVE_WINDOW_SYSTEM
22779
22780 #ifdef GLYPH_DEBUG
22781
22782 void
22783 dump_glyph_string (struct glyph_string *s)
22784 {
22785 fprintf (stderr, "glyph string\n");
22786 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22787 s->x, s->y, s->width, s->height);
22788 fprintf (stderr, " ybase = %d\n", s->ybase);
22789 fprintf (stderr, " hl = %d\n", s->hl);
22790 fprintf (stderr, " left overhang = %d, right = %d\n",
22791 s->left_overhang, s->right_overhang);
22792 fprintf (stderr, " nchars = %d\n", s->nchars);
22793 fprintf (stderr, " extends to end of line = %d\n",
22794 s->extends_to_end_of_line_p);
22795 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22796 fprintf (stderr, " bg width = %d\n", s->background_width);
22797 }
22798
22799 #endif /* GLYPH_DEBUG */
22800
22801 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22802 of XChar2b structures for S; it can't be allocated in
22803 init_glyph_string because it must be allocated via `alloca'. W
22804 is the window on which S is drawn. ROW and AREA are the glyph row
22805 and area within the row from which S is constructed. START is the
22806 index of the first glyph structure covered by S. HL is a
22807 face-override for drawing S. */
22808
22809 #ifdef HAVE_NTGUI
22810 #define OPTIONAL_HDC(hdc) HDC hdc,
22811 #define DECLARE_HDC(hdc) HDC hdc;
22812 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22813 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22814 #endif
22815
22816 #ifndef OPTIONAL_HDC
22817 #define OPTIONAL_HDC(hdc)
22818 #define DECLARE_HDC(hdc)
22819 #define ALLOCATE_HDC(hdc, f)
22820 #define RELEASE_HDC(hdc, f)
22821 #endif
22822
22823 static void
22824 init_glyph_string (struct glyph_string *s,
22825 OPTIONAL_HDC (hdc)
22826 XChar2b *char2b, struct window *w, struct glyph_row *row,
22827 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22828 {
22829 memset (s, 0, sizeof *s);
22830 s->w = w;
22831 s->f = XFRAME (w->frame);
22832 #ifdef HAVE_NTGUI
22833 s->hdc = hdc;
22834 #endif
22835 s->display = FRAME_X_DISPLAY (s->f);
22836 s->window = FRAME_X_WINDOW (s->f);
22837 s->char2b = char2b;
22838 s->hl = hl;
22839 s->row = row;
22840 s->area = area;
22841 s->first_glyph = row->glyphs[area] + start;
22842 s->height = row->height;
22843 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22844 s->ybase = s->y + row->ascent;
22845 }
22846
22847
22848 /* Append the list of glyph strings with head H and tail T to the list
22849 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22850
22851 static void
22852 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22853 struct glyph_string *h, struct glyph_string *t)
22854 {
22855 if (h)
22856 {
22857 if (*head)
22858 (*tail)->next = h;
22859 else
22860 *head = h;
22861 h->prev = *tail;
22862 *tail = t;
22863 }
22864 }
22865
22866
22867 /* Prepend the list of glyph strings with head H and tail T to the
22868 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22869 result. */
22870
22871 static void
22872 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22873 struct glyph_string *h, struct glyph_string *t)
22874 {
22875 if (h)
22876 {
22877 if (*head)
22878 (*head)->prev = t;
22879 else
22880 *tail = t;
22881 t->next = *head;
22882 *head = h;
22883 }
22884 }
22885
22886
22887 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22888 Set *HEAD and *TAIL to the resulting list. */
22889
22890 static void
22891 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22892 struct glyph_string *s)
22893 {
22894 s->next = s->prev = NULL;
22895 append_glyph_string_lists (head, tail, s, s);
22896 }
22897
22898
22899 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22900 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22901 make sure that X resources for the face returned are allocated.
22902 Value is a pointer to a realized face that is ready for display if
22903 DISPLAY_P is non-zero. */
22904
22905 static struct face *
22906 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22907 XChar2b *char2b, int display_p)
22908 {
22909 struct face *face = FACE_FROM_ID (f, face_id);
22910 unsigned code = 0;
22911
22912 if (face->font)
22913 {
22914 code = face->font->driver->encode_char (face->font, c);
22915
22916 if (code == FONT_INVALID_CODE)
22917 code = 0;
22918 }
22919 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22920
22921 /* Make sure X resources of the face are allocated. */
22922 #ifdef HAVE_X_WINDOWS
22923 if (display_p)
22924 #endif
22925 {
22926 eassert (face != NULL);
22927 PREPARE_FACE_FOR_DISPLAY (f, face);
22928 }
22929
22930 return face;
22931 }
22932
22933
22934 /* Get face and two-byte form of character glyph GLYPH on frame F.
22935 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22936 a pointer to a realized face that is ready for display. */
22937
22938 static struct face *
22939 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22940 XChar2b *char2b, int *two_byte_p)
22941 {
22942 struct face *face;
22943 unsigned code = 0;
22944
22945 eassert (glyph->type == CHAR_GLYPH);
22946 face = FACE_FROM_ID (f, glyph->face_id);
22947
22948 /* Make sure X resources of the face are allocated. */
22949 eassert (face != NULL);
22950 PREPARE_FACE_FOR_DISPLAY (f, face);
22951
22952 if (two_byte_p)
22953 *two_byte_p = 0;
22954
22955 if (face->font)
22956 {
22957 if (CHAR_BYTE8_P (glyph->u.ch))
22958 code = CHAR_TO_BYTE8 (glyph->u.ch);
22959 else
22960 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22961
22962 if (code == FONT_INVALID_CODE)
22963 code = 0;
22964 }
22965
22966 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22967 return face;
22968 }
22969
22970
22971 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22972 Return 1 if FONT has a glyph for C, otherwise return 0. */
22973
22974 static int
22975 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22976 {
22977 unsigned code;
22978
22979 if (CHAR_BYTE8_P (c))
22980 code = CHAR_TO_BYTE8 (c);
22981 else
22982 code = font->driver->encode_char (font, c);
22983
22984 if (code == FONT_INVALID_CODE)
22985 return 0;
22986 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22987 return 1;
22988 }
22989
22990
22991 /* Fill glyph string S with composition components specified by S->cmp.
22992
22993 BASE_FACE is the base face of the composition.
22994 S->cmp_from is the index of the first component for S.
22995
22996 OVERLAPS non-zero means S should draw the foreground only, and use
22997 its physical height for clipping. See also draw_glyphs.
22998
22999 Value is the index of a component not in S. */
23000
23001 static int
23002 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23003 int overlaps)
23004 {
23005 int i;
23006 /* For all glyphs of this composition, starting at the offset
23007 S->cmp_from, until we reach the end of the definition or encounter a
23008 glyph that requires the different face, add it to S. */
23009 struct face *face;
23010
23011 eassert (s);
23012
23013 s->for_overlaps = overlaps;
23014 s->face = NULL;
23015 s->font = NULL;
23016 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23017 {
23018 int c = COMPOSITION_GLYPH (s->cmp, i);
23019
23020 /* TAB in a composition means display glyphs with padding space
23021 on the left or right. */
23022 if (c != '\t')
23023 {
23024 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23025 -1, Qnil);
23026
23027 face = get_char_face_and_encoding (s->f, c, face_id,
23028 s->char2b + i, 1);
23029 if (face)
23030 {
23031 if (! s->face)
23032 {
23033 s->face = face;
23034 s->font = s->face->font;
23035 }
23036 else if (s->face != face)
23037 break;
23038 }
23039 }
23040 ++s->nchars;
23041 }
23042 s->cmp_to = i;
23043
23044 if (s->face == NULL)
23045 {
23046 s->face = base_face->ascii_face;
23047 s->font = s->face->font;
23048 }
23049
23050 /* All glyph strings for the same composition has the same width,
23051 i.e. the width set for the first component of the composition. */
23052 s->width = s->first_glyph->pixel_width;
23053
23054 /* If the specified font could not be loaded, use the frame's
23055 default font, but record the fact that we couldn't load it in
23056 the glyph string so that we can draw rectangles for the
23057 characters of the glyph string. */
23058 if (s->font == NULL)
23059 {
23060 s->font_not_found_p = 1;
23061 s->font = FRAME_FONT (s->f);
23062 }
23063
23064 /* Adjust base line for subscript/superscript text. */
23065 s->ybase += s->first_glyph->voffset;
23066
23067 /* This glyph string must always be drawn with 16-bit functions. */
23068 s->two_byte_p = 1;
23069
23070 return s->cmp_to;
23071 }
23072
23073 static int
23074 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23075 int start, int end, int overlaps)
23076 {
23077 struct glyph *glyph, *last;
23078 Lisp_Object lgstring;
23079 int i;
23080
23081 s->for_overlaps = overlaps;
23082 glyph = s->row->glyphs[s->area] + start;
23083 last = s->row->glyphs[s->area] + end;
23084 s->cmp_id = glyph->u.cmp.id;
23085 s->cmp_from = glyph->slice.cmp.from;
23086 s->cmp_to = glyph->slice.cmp.to + 1;
23087 s->face = FACE_FROM_ID (s->f, face_id);
23088 lgstring = composition_gstring_from_id (s->cmp_id);
23089 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23090 glyph++;
23091 while (glyph < last
23092 && glyph->u.cmp.automatic
23093 && glyph->u.cmp.id == s->cmp_id
23094 && s->cmp_to == glyph->slice.cmp.from)
23095 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23096
23097 for (i = s->cmp_from; i < s->cmp_to; i++)
23098 {
23099 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23100 unsigned code = LGLYPH_CODE (lglyph);
23101
23102 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23103 }
23104 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23105 return glyph - s->row->glyphs[s->area];
23106 }
23107
23108
23109 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23110 See the comment of fill_glyph_string for arguments.
23111 Value is the index of the first glyph not in S. */
23112
23113
23114 static int
23115 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23116 int start, int end, int overlaps)
23117 {
23118 struct glyph *glyph, *last;
23119 int voffset;
23120
23121 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23122 s->for_overlaps = overlaps;
23123 glyph = s->row->glyphs[s->area] + start;
23124 last = s->row->glyphs[s->area] + end;
23125 voffset = glyph->voffset;
23126 s->face = FACE_FROM_ID (s->f, face_id);
23127 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23128 s->nchars = 1;
23129 s->width = glyph->pixel_width;
23130 glyph++;
23131 while (glyph < last
23132 && glyph->type == GLYPHLESS_GLYPH
23133 && glyph->voffset == voffset
23134 && glyph->face_id == face_id)
23135 {
23136 s->nchars++;
23137 s->width += glyph->pixel_width;
23138 glyph++;
23139 }
23140 s->ybase += voffset;
23141 return glyph - s->row->glyphs[s->area];
23142 }
23143
23144
23145 /* Fill glyph string S from a sequence of character glyphs.
23146
23147 FACE_ID is the face id of the string. START is the index of the
23148 first glyph to consider, END is the index of the last + 1.
23149 OVERLAPS non-zero means S should draw the foreground only, and use
23150 its physical height for clipping. See also draw_glyphs.
23151
23152 Value is the index of the first glyph not in S. */
23153
23154 static int
23155 fill_glyph_string (struct glyph_string *s, int face_id,
23156 int start, int end, int overlaps)
23157 {
23158 struct glyph *glyph, *last;
23159 int voffset;
23160 int glyph_not_available_p;
23161
23162 eassert (s->f == XFRAME (s->w->frame));
23163 eassert (s->nchars == 0);
23164 eassert (start >= 0 && end > start);
23165
23166 s->for_overlaps = overlaps;
23167 glyph = s->row->glyphs[s->area] + start;
23168 last = s->row->glyphs[s->area] + end;
23169 voffset = glyph->voffset;
23170 s->padding_p = glyph->padding_p;
23171 glyph_not_available_p = glyph->glyph_not_available_p;
23172
23173 while (glyph < last
23174 && glyph->type == CHAR_GLYPH
23175 && glyph->voffset == voffset
23176 /* Same face id implies same font, nowadays. */
23177 && glyph->face_id == face_id
23178 && glyph->glyph_not_available_p == glyph_not_available_p)
23179 {
23180 int two_byte_p;
23181
23182 s->face = get_glyph_face_and_encoding (s->f, glyph,
23183 s->char2b + s->nchars,
23184 &two_byte_p);
23185 s->two_byte_p = two_byte_p;
23186 ++s->nchars;
23187 eassert (s->nchars <= end - start);
23188 s->width += glyph->pixel_width;
23189 if (glyph++->padding_p != s->padding_p)
23190 break;
23191 }
23192
23193 s->font = s->face->font;
23194
23195 /* If the specified font could not be loaded, use the frame's font,
23196 but record the fact that we couldn't load it in
23197 S->font_not_found_p so that we can draw rectangles for the
23198 characters of the glyph string. */
23199 if (s->font == NULL || glyph_not_available_p)
23200 {
23201 s->font_not_found_p = 1;
23202 s->font = FRAME_FONT (s->f);
23203 }
23204
23205 /* Adjust base line for subscript/superscript text. */
23206 s->ybase += voffset;
23207
23208 eassert (s->face && s->face->gc);
23209 return glyph - s->row->glyphs[s->area];
23210 }
23211
23212
23213 /* Fill glyph string S from image glyph S->first_glyph. */
23214
23215 static void
23216 fill_image_glyph_string (struct glyph_string *s)
23217 {
23218 eassert (s->first_glyph->type == IMAGE_GLYPH);
23219 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23220 eassert (s->img);
23221 s->slice = s->first_glyph->slice.img;
23222 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23223 s->font = s->face->font;
23224 s->width = s->first_glyph->pixel_width;
23225
23226 /* Adjust base line for subscript/superscript text. */
23227 s->ybase += s->first_glyph->voffset;
23228 }
23229
23230
23231 /* Fill glyph string S from a sequence of stretch glyphs.
23232
23233 START is the index of the first glyph to consider,
23234 END is the index of the last + 1.
23235
23236 Value is the index of the first glyph not in S. */
23237
23238 static int
23239 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23240 {
23241 struct glyph *glyph, *last;
23242 int voffset, face_id;
23243
23244 eassert (s->first_glyph->type == STRETCH_GLYPH);
23245
23246 glyph = s->row->glyphs[s->area] + start;
23247 last = s->row->glyphs[s->area] + end;
23248 face_id = glyph->face_id;
23249 s->face = FACE_FROM_ID (s->f, face_id);
23250 s->font = s->face->font;
23251 s->width = glyph->pixel_width;
23252 s->nchars = 1;
23253 voffset = glyph->voffset;
23254
23255 for (++glyph;
23256 (glyph < last
23257 && glyph->type == STRETCH_GLYPH
23258 && glyph->voffset == voffset
23259 && glyph->face_id == face_id);
23260 ++glyph)
23261 s->width += glyph->pixel_width;
23262
23263 /* Adjust base line for subscript/superscript text. */
23264 s->ybase += voffset;
23265
23266 /* The case that face->gc == 0 is handled when drawing the glyph
23267 string by calling PREPARE_FACE_FOR_DISPLAY. */
23268 eassert (s->face);
23269 return glyph - s->row->glyphs[s->area];
23270 }
23271
23272 static struct font_metrics *
23273 get_per_char_metric (struct font *font, XChar2b *char2b)
23274 {
23275 static struct font_metrics metrics;
23276 unsigned code;
23277
23278 if (! font)
23279 return NULL;
23280 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23281 if (code == FONT_INVALID_CODE)
23282 return NULL;
23283 font->driver->text_extents (font, &code, 1, &metrics);
23284 return &metrics;
23285 }
23286
23287 /* EXPORT for RIF:
23288 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23289 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23290 assumed to be zero. */
23291
23292 void
23293 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23294 {
23295 *left = *right = 0;
23296
23297 if (glyph->type == CHAR_GLYPH)
23298 {
23299 struct face *face;
23300 XChar2b char2b;
23301 struct font_metrics *pcm;
23302
23303 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23304 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23305 {
23306 if (pcm->rbearing > pcm->width)
23307 *right = pcm->rbearing - pcm->width;
23308 if (pcm->lbearing < 0)
23309 *left = -pcm->lbearing;
23310 }
23311 }
23312 else if (glyph->type == COMPOSITE_GLYPH)
23313 {
23314 if (! glyph->u.cmp.automatic)
23315 {
23316 struct composition *cmp = composition_table[glyph->u.cmp.id];
23317
23318 if (cmp->rbearing > cmp->pixel_width)
23319 *right = cmp->rbearing - cmp->pixel_width;
23320 if (cmp->lbearing < 0)
23321 *left = - cmp->lbearing;
23322 }
23323 else
23324 {
23325 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23326 struct font_metrics metrics;
23327
23328 composition_gstring_width (gstring, glyph->slice.cmp.from,
23329 glyph->slice.cmp.to + 1, &metrics);
23330 if (metrics.rbearing > metrics.width)
23331 *right = metrics.rbearing - metrics.width;
23332 if (metrics.lbearing < 0)
23333 *left = - metrics.lbearing;
23334 }
23335 }
23336 }
23337
23338
23339 /* Return the index of the first glyph preceding glyph string S that
23340 is overwritten by S because of S's left overhang. Value is -1
23341 if no glyphs are overwritten. */
23342
23343 static int
23344 left_overwritten (struct glyph_string *s)
23345 {
23346 int k;
23347
23348 if (s->left_overhang)
23349 {
23350 int x = 0, i;
23351 struct glyph *glyphs = s->row->glyphs[s->area];
23352 int first = s->first_glyph - glyphs;
23353
23354 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23355 x -= glyphs[i].pixel_width;
23356
23357 k = i + 1;
23358 }
23359 else
23360 k = -1;
23361
23362 return k;
23363 }
23364
23365
23366 /* Return the index of the first glyph preceding glyph string S that
23367 is overwriting S because of its right overhang. Value is -1 if no
23368 glyph in front of S overwrites S. */
23369
23370 static int
23371 left_overwriting (struct glyph_string *s)
23372 {
23373 int i, k, x;
23374 struct glyph *glyphs = s->row->glyphs[s->area];
23375 int first = s->first_glyph - glyphs;
23376
23377 k = -1;
23378 x = 0;
23379 for (i = first - 1; i >= 0; --i)
23380 {
23381 int left, right;
23382 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23383 if (x + right > 0)
23384 k = i;
23385 x -= glyphs[i].pixel_width;
23386 }
23387
23388 return k;
23389 }
23390
23391
23392 /* Return the index of the last glyph following glyph string S that is
23393 overwritten by S because of S's right overhang. Value is -1 if
23394 no such glyph is found. */
23395
23396 static int
23397 right_overwritten (struct glyph_string *s)
23398 {
23399 int k = -1;
23400
23401 if (s->right_overhang)
23402 {
23403 int x = 0, i;
23404 struct glyph *glyphs = s->row->glyphs[s->area];
23405 int first = (s->first_glyph - glyphs
23406 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23407 int end = s->row->used[s->area];
23408
23409 for (i = first; i < end && s->right_overhang > x; ++i)
23410 x += glyphs[i].pixel_width;
23411
23412 k = i;
23413 }
23414
23415 return k;
23416 }
23417
23418
23419 /* Return the index of the last glyph following glyph string S that
23420 overwrites S because of its left overhang. Value is negative
23421 if no such glyph is found. */
23422
23423 static int
23424 right_overwriting (struct glyph_string *s)
23425 {
23426 int i, k, x;
23427 int end = s->row->used[s->area];
23428 struct glyph *glyphs = s->row->glyphs[s->area];
23429 int first = (s->first_glyph - glyphs
23430 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23431
23432 k = -1;
23433 x = 0;
23434 for (i = first; i < end; ++i)
23435 {
23436 int left, right;
23437 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23438 if (x - left < 0)
23439 k = i;
23440 x += glyphs[i].pixel_width;
23441 }
23442
23443 return k;
23444 }
23445
23446
23447 /* Set background width of glyph string S. START is the index of the
23448 first glyph following S. LAST_X is the right-most x-position + 1
23449 in the drawing area. */
23450
23451 static void
23452 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23453 {
23454 /* If the face of this glyph string has to be drawn to the end of
23455 the drawing area, set S->extends_to_end_of_line_p. */
23456
23457 if (start == s->row->used[s->area]
23458 && s->area == TEXT_AREA
23459 && ((s->row->fill_line_p
23460 && (s->hl == DRAW_NORMAL_TEXT
23461 || s->hl == DRAW_IMAGE_RAISED
23462 || s->hl == DRAW_IMAGE_SUNKEN))
23463 || s->hl == DRAW_MOUSE_FACE))
23464 s->extends_to_end_of_line_p = 1;
23465
23466 /* If S extends its face to the end of the line, set its
23467 background_width to the distance to the right edge of the drawing
23468 area. */
23469 if (s->extends_to_end_of_line_p)
23470 s->background_width = last_x - s->x + 1;
23471 else
23472 s->background_width = s->width;
23473 }
23474
23475
23476 /* Compute overhangs and x-positions for glyph string S and its
23477 predecessors, or successors. X is the starting x-position for S.
23478 BACKWARD_P non-zero means process predecessors. */
23479
23480 static void
23481 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23482 {
23483 if (backward_p)
23484 {
23485 while (s)
23486 {
23487 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23488 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23489 x -= s->width;
23490 s->x = x;
23491 s = s->prev;
23492 }
23493 }
23494 else
23495 {
23496 while (s)
23497 {
23498 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23499 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23500 s->x = x;
23501 x += s->width;
23502 s = s->next;
23503 }
23504 }
23505 }
23506
23507
23508
23509 /* The following macros are only called from draw_glyphs below.
23510 They reference the following parameters of that function directly:
23511 `w', `row', `area', and `overlap_p'
23512 as well as the following local variables:
23513 `s', `f', and `hdc' (in W32) */
23514
23515 #ifdef HAVE_NTGUI
23516 /* On W32, silently add local `hdc' variable to argument list of
23517 init_glyph_string. */
23518 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23519 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23520 #else
23521 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23522 init_glyph_string (s, char2b, w, row, area, start, hl)
23523 #endif
23524
23525 /* Add a glyph string for a stretch glyph to the list of strings
23526 between HEAD and TAIL. START is the index of the stretch glyph in
23527 row area AREA of glyph row ROW. END is the index of the last glyph
23528 in that glyph row area. X is the current output position assigned
23529 to the new glyph string constructed. HL overrides that face of the
23530 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23531 is the right-most x-position of the drawing area. */
23532
23533 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23534 and below -- keep them on one line. */
23535 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23536 do \
23537 { \
23538 s = alloca (sizeof *s); \
23539 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23540 START = fill_stretch_glyph_string (s, START, END); \
23541 append_glyph_string (&HEAD, &TAIL, s); \
23542 s->x = (X); \
23543 } \
23544 while (0)
23545
23546
23547 /* Add a glyph string for an image glyph to the list of strings
23548 between HEAD and TAIL. START is the index of the image glyph in
23549 row area AREA of glyph row ROW. END is the index of the last glyph
23550 in that glyph row area. X is the current output position assigned
23551 to the new glyph string constructed. HL overrides that face of the
23552 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23553 is the right-most x-position of the drawing area. */
23554
23555 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23556 do \
23557 { \
23558 s = alloca (sizeof *s); \
23559 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23560 fill_image_glyph_string (s); \
23561 append_glyph_string (&HEAD, &TAIL, s); \
23562 ++START; \
23563 s->x = (X); \
23564 } \
23565 while (0)
23566
23567
23568 /* Add a glyph string for a sequence of character glyphs to the list
23569 of strings between HEAD and TAIL. START is the index of the first
23570 glyph in row area AREA of glyph row ROW that is part of the new
23571 glyph string. END is the index of the last glyph in that glyph row
23572 area. X is the current output position assigned to the new glyph
23573 string constructed. HL overrides that face of the glyph; e.g. it
23574 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23575 right-most x-position of the drawing area. */
23576
23577 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23578 do \
23579 { \
23580 int face_id; \
23581 XChar2b *char2b; \
23582 \
23583 face_id = (row)->glyphs[area][START].face_id; \
23584 \
23585 s = alloca (sizeof *s); \
23586 char2b = alloca ((END - START) * sizeof *char2b); \
23587 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23588 append_glyph_string (&HEAD, &TAIL, s); \
23589 s->x = (X); \
23590 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23591 } \
23592 while (0)
23593
23594
23595 /* Add a glyph string for a composite sequence to the list of strings
23596 between HEAD and TAIL. START is the index of the first glyph in
23597 row area AREA of glyph row ROW that is part of the new glyph
23598 string. END is the index of the last glyph in that glyph row area.
23599 X is the current output position assigned to the new glyph string
23600 constructed. HL overrides that face of the glyph; e.g. it is
23601 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23602 x-position of the drawing area. */
23603
23604 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23605 do { \
23606 int face_id = (row)->glyphs[area][START].face_id; \
23607 struct face *base_face = FACE_FROM_ID (f, face_id); \
23608 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23609 struct composition *cmp = composition_table[cmp_id]; \
23610 XChar2b *char2b; \
23611 struct glyph_string *first_s = NULL; \
23612 int n; \
23613 \
23614 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23615 \
23616 /* Make glyph_strings for each glyph sequence that is drawable by \
23617 the same face, and append them to HEAD/TAIL. */ \
23618 for (n = 0; n < cmp->glyph_len;) \
23619 { \
23620 s = alloca (sizeof *s); \
23621 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23622 append_glyph_string (&(HEAD), &(TAIL), s); \
23623 s->cmp = cmp; \
23624 s->cmp_from = n; \
23625 s->x = (X); \
23626 if (n == 0) \
23627 first_s = s; \
23628 n = fill_composite_glyph_string (s, base_face, overlaps); \
23629 } \
23630 \
23631 ++START; \
23632 s = first_s; \
23633 } while (0)
23634
23635
23636 /* Add a glyph string for a glyph-string sequence to the list of strings
23637 between HEAD and TAIL. */
23638
23639 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23640 do { \
23641 int face_id; \
23642 XChar2b *char2b; \
23643 Lisp_Object gstring; \
23644 \
23645 face_id = (row)->glyphs[area][START].face_id; \
23646 gstring = (composition_gstring_from_id \
23647 ((row)->glyphs[area][START].u.cmp.id)); \
23648 s = alloca (sizeof *s); \
23649 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23650 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23651 append_glyph_string (&(HEAD), &(TAIL), s); \
23652 s->x = (X); \
23653 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23654 } while (0)
23655
23656
23657 /* Add a glyph string for a sequence of glyphless character's glyphs
23658 to the list of strings between HEAD and TAIL. The meanings of
23659 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23660
23661 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23662 do \
23663 { \
23664 int face_id; \
23665 \
23666 face_id = (row)->glyphs[area][START].face_id; \
23667 \
23668 s = alloca (sizeof *s); \
23669 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23670 append_glyph_string (&HEAD, &TAIL, s); \
23671 s->x = (X); \
23672 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23673 overlaps); \
23674 } \
23675 while (0)
23676
23677
23678 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23679 of AREA of glyph row ROW on window W between indices START and END.
23680 HL overrides the face for drawing glyph strings, e.g. it is
23681 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23682 x-positions of the drawing area.
23683
23684 This is an ugly monster macro construct because we must use alloca
23685 to allocate glyph strings (because draw_glyphs can be called
23686 asynchronously). */
23687
23688 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23689 do \
23690 { \
23691 HEAD = TAIL = NULL; \
23692 while (START < END) \
23693 { \
23694 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23695 switch (first_glyph->type) \
23696 { \
23697 case CHAR_GLYPH: \
23698 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23699 HL, X, LAST_X); \
23700 break; \
23701 \
23702 case COMPOSITE_GLYPH: \
23703 if (first_glyph->u.cmp.automatic) \
23704 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23705 HL, X, LAST_X); \
23706 else \
23707 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23708 HL, X, LAST_X); \
23709 break; \
23710 \
23711 case STRETCH_GLYPH: \
23712 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23713 HL, X, LAST_X); \
23714 break; \
23715 \
23716 case IMAGE_GLYPH: \
23717 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23718 HL, X, LAST_X); \
23719 break; \
23720 \
23721 case GLYPHLESS_GLYPH: \
23722 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23723 HL, X, LAST_X); \
23724 break; \
23725 \
23726 default: \
23727 emacs_abort (); \
23728 } \
23729 \
23730 if (s) \
23731 { \
23732 set_glyph_string_background_width (s, START, LAST_X); \
23733 (X) += s->width; \
23734 } \
23735 } \
23736 } while (0)
23737
23738
23739 /* Draw glyphs between START and END in AREA of ROW on window W,
23740 starting at x-position X. X is relative to AREA in W. HL is a
23741 face-override with the following meaning:
23742
23743 DRAW_NORMAL_TEXT draw normally
23744 DRAW_CURSOR draw in cursor face
23745 DRAW_MOUSE_FACE draw in mouse face.
23746 DRAW_INVERSE_VIDEO draw in mode line face
23747 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23748 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23749
23750 If OVERLAPS is non-zero, draw only the foreground of characters and
23751 clip to the physical height of ROW. Non-zero value also defines
23752 the overlapping part to be drawn:
23753
23754 OVERLAPS_PRED overlap with preceding rows
23755 OVERLAPS_SUCC overlap with succeeding rows
23756 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23757 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23758
23759 Value is the x-position reached, relative to AREA of W. */
23760
23761 static int
23762 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23763 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23764 enum draw_glyphs_face hl, int overlaps)
23765 {
23766 struct glyph_string *head, *tail;
23767 struct glyph_string *s;
23768 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23769 int i, j, x_reached, last_x, area_left = 0;
23770 struct frame *f = XFRAME (WINDOW_FRAME (w));
23771 DECLARE_HDC (hdc);
23772
23773 ALLOCATE_HDC (hdc, f);
23774
23775 /* Let's rather be paranoid than getting a SEGV. */
23776 end = min (end, row->used[area]);
23777 start = clip_to_bounds (0, start, end);
23778
23779 /* Translate X to frame coordinates. Set last_x to the right
23780 end of the drawing area. */
23781 if (row->full_width_p)
23782 {
23783 /* X is relative to the left edge of W, without scroll bars
23784 or fringes. */
23785 area_left = WINDOW_LEFT_EDGE_X (w);
23786 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23787 }
23788 else
23789 {
23790 area_left = window_box_left (w, area);
23791 last_x = area_left + window_box_width (w, area);
23792 }
23793 x += area_left;
23794
23795 /* Build a doubly-linked list of glyph_string structures between
23796 head and tail from what we have to draw. Note that the macro
23797 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23798 the reason we use a separate variable `i'. */
23799 i = start;
23800 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23801 if (tail)
23802 x_reached = tail->x + tail->background_width;
23803 else
23804 x_reached = x;
23805
23806 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23807 the row, redraw some glyphs in front or following the glyph
23808 strings built above. */
23809 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23810 {
23811 struct glyph_string *h, *t;
23812 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23813 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23814 int check_mouse_face = 0;
23815 int dummy_x = 0;
23816
23817 /* If mouse highlighting is on, we may need to draw adjacent
23818 glyphs using mouse-face highlighting. */
23819 if (area == TEXT_AREA && row->mouse_face_p
23820 && hlinfo->mouse_face_beg_row >= 0
23821 && hlinfo->mouse_face_end_row >= 0)
23822 {
23823 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23824
23825 if (row_vpos >= hlinfo->mouse_face_beg_row
23826 && row_vpos <= hlinfo->mouse_face_end_row)
23827 {
23828 check_mouse_face = 1;
23829 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23830 ? hlinfo->mouse_face_beg_col : 0;
23831 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23832 ? hlinfo->mouse_face_end_col
23833 : row->used[TEXT_AREA];
23834 }
23835 }
23836
23837 /* Compute overhangs for all glyph strings. */
23838 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23839 for (s = head; s; s = s->next)
23840 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23841
23842 /* Prepend glyph strings for glyphs in front of the first glyph
23843 string that are overwritten because of the first glyph
23844 string's left overhang. The background of all strings
23845 prepended must be drawn because the first glyph string
23846 draws over it. */
23847 i = left_overwritten (head);
23848 if (i >= 0)
23849 {
23850 enum draw_glyphs_face overlap_hl;
23851
23852 /* If this row contains mouse highlighting, attempt to draw
23853 the overlapped glyphs with the correct highlight. This
23854 code fails if the overlap encompasses more than one glyph
23855 and mouse-highlight spans only some of these glyphs.
23856 However, making it work perfectly involves a lot more
23857 code, and I don't know if the pathological case occurs in
23858 practice, so we'll stick to this for now. --- cyd */
23859 if (check_mouse_face
23860 && mouse_beg_col < start && mouse_end_col > i)
23861 overlap_hl = DRAW_MOUSE_FACE;
23862 else
23863 overlap_hl = DRAW_NORMAL_TEXT;
23864
23865 j = i;
23866 BUILD_GLYPH_STRINGS (j, start, h, t,
23867 overlap_hl, dummy_x, last_x);
23868 start = i;
23869 compute_overhangs_and_x (t, head->x, 1);
23870 prepend_glyph_string_lists (&head, &tail, h, t);
23871 clip_head = head;
23872 }
23873
23874 /* Prepend glyph strings for glyphs in front of the first glyph
23875 string that overwrite that glyph string because of their
23876 right overhang. For these strings, only the foreground must
23877 be drawn, because it draws over the glyph string at `head'.
23878 The background must not be drawn because this would overwrite
23879 right overhangs of preceding glyphs for which no glyph
23880 strings exist. */
23881 i = left_overwriting (head);
23882 if (i >= 0)
23883 {
23884 enum draw_glyphs_face overlap_hl;
23885
23886 if (check_mouse_face
23887 && mouse_beg_col < start && mouse_end_col > i)
23888 overlap_hl = DRAW_MOUSE_FACE;
23889 else
23890 overlap_hl = DRAW_NORMAL_TEXT;
23891
23892 clip_head = head;
23893 BUILD_GLYPH_STRINGS (i, start, h, t,
23894 overlap_hl, dummy_x, last_x);
23895 for (s = h; s; s = s->next)
23896 s->background_filled_p = 1;
23897 compute_overhangs_and_x (t, head->x, 1);
23898 prepend_glyph_string_lists (&head, &tail, h, t);
23899 }
23900
23901 /* Append glyphs strings for glyphs following the last glyph
23902 string tail that are overwritten by tail. The background of
23903 these strings has to be drawn because tail's foreground draws
23904 over it. */
23905 i = right_overwritten (tail);
23906 if (i >= 0)
23907 {
23908 enum draw_glyphs_face overlap_hl;
23909
23910 if (check_mouse_face
23911 && mouse_beg_col < i && mouse_end_col > end)
23912 overlap_hl = DRAW_MOUSE_FACE;
23913 else
23914 overlap_hl = DRAW_NORMAL_TEXT;
23915
23916 BUILD_GLYPH_STRINGS (end, i, h, t,
23917 overlap_hl, x, last_x);
23918 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23919 we don't have `end = i;' here. */
23920 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23921 append_glyph_string_lists (&head, &tail, h, t);
23922 clip_tail = tail;
23923 }
23924
23925 /* Append glyph strings for glyphs following the last glyph
23926 string tail that overwrite tail. The foreground of such
23927 glyphs has to be drawn because it writes into the background
23928 of tail. The background must not be drawn because it could
23929 paint over the foreground of following glyphs. */
23930 i = right_overwriting (tail);
23931 if (i >= 0)
23932 {
23933 enum draw_glyphs_face overlap_hl;
23934 if (check_mouse_face
23935 && mouse_beg_col < i && mouse_end_col > end)
23936 overlap_hl = DRAW_MOUSE_FACE;
23937 else
23938 overlap_hl = DRAW_NORMAL_TEXT;
23939
23940 clip_tail = tail;
23941 i++; /* We must include the Ith glyph. */
23942 BUILD_GLYPH_STRINGS (end, i, h, t,
23943 overlap_hl, x, last_x);
23944 for (s = h; s; s = s->next)
23945 s->background_filled_p = 1;
23946 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23947 append_glyph_string_lists (&head, &tail, h, t);
23948 }
23949 if (clip_head || clip_tail)
23950 for (s = head; s; s = s->next)
23951 {
23952 s->clip_head = clip_head;
23953 s->clip_tail = clip_tail;
23954 }
23955 }
23956
23957 /* Draw all strings. */
23958 for (s = head; s; s = s->next)
23959 FRAME_RIF (f)->draw_glyph_string (s);
23960
23961 #ifndef HAVE_NS
23962 /* When focus a sole frame and move horizontally, this sets on_p to 0
23963 causing a failure to erase prev cursor position. */
23964 if (area == TEXT_AREA
23965 && !row->full_width_p
23966 /* When drawing overlapping rows, only the glyph strings'
23967 foreground is drawn, which doesn't erase a cursor
23968 completely. */
23969 && !overlaps)
23970 {
23971 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23972 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23973 : (tail ? tail->x + tail->background_width : x));
23974 x0 -= area_left;
23975 x1 -= area_left;
23976
23977 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23978 row->y, MATRIX_ROW_BOTTOM_Y (row));
23979 }
23980 #endif
23981
23982 /* Value is the x-position up to which drawn, relative to AREA of W.
23983 This doesn't include parts drawn because of overhangs. */
23984 if (row->full_width_p)
23985 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23986 else
23987 x_reached -= area_left;
23988
23989 RELEASE_HDC (hdc, f);
23990
23991 return x_reached;
23992 }
23993
23994 /* Expand row matrix if too narrow. Don't expand if area
23995 is not present. */
23996
23997 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23998 { \
23999 if (!it->f->fonts_changed \
24000 && (it->glyph_row->glyphs[area] \
24001 < it->glyph_row->glyphs[area + 1])) \
24002 { \
24003 it->w->ncols_scale_factor++; \
24004 it->f->fonts_changed = 1; \
24005 } \
24006 }
24007
24008 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24009 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24010
24011 static void
24012 append_glyph (struct it *it)
24013 {
24014 struct glyph *glyph;
24015 enum glyph_row_area area = it->area;
24016
24017 eassert (it->glyph_row);
24018 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24019
24020 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24021 if (glyph < it->glyph_row->glyphs[area + 1])
24022 {
24023 /* If the glyph row is reversed, we need to prepend the glyph
24024 rather than append it. */
24025 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24026 {
24027 struct glyph *g;
24028
24029 /* Make room for the additional glyph. */
24030 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24031 g[1] = *g;
24032 glyph = it->glyph_row->glyphs[area];
24033 }
24034 glyph->charpos = CHARPOS (it->position);
24035 glyph->object = it->object;
24036 if (it->pixel_width > 0)
24037 {
24038 glyph->pixel_width = it->pixel_width;
24039 glyph->padding_p = 0;
24040 }
24041 else
24042 {
24043 /* Assure at least 1-pixel width. Otherwise, cursor can't
24044 be displayed correctly. */
24045 glyph->pixel_width = 1;
24046 glyph->padding_p = 1;
24047 }
24048 glyph->ascent = it->ascent;
24049 glyph->descent = it->descent;
24050 glyph->voffset = it->voffset;
24051 glyph->type = CHAR_GLYPH;
24052 glyph->avoid_cursor_p = it->avoid_cursor_p;
24053 glyph->multibyte_p = it->multibyte_p;
24054 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24055 {
24056 /* In R2L rows, the left and the right box edges need to be
24057 drawn in reverse direction. */
24058 glyph->right_box_line_p = it->start_of_box_run_p;
24059 glyph->left_box_line_p = it->end_of_box_run_p;
24060 }
24061 else
24062 {
24063 glyph->left_box_line_p = it->start_of_box_run_p;
24064 glyph->right_box_line_p = it->end_of_box_run_p;
24065 }
24066 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24067 || it->phys_descent > it->descent);
24068 glyph->glyph_not_available_p = it->glyph_not_available_p;
24069 glyph->face_id = it->face_id;
24070 glyph->u.ch = it->char_to_display;
24071 glyph->slice.img = null_glyph_slice;
24072 glyph->font_type = FONT_TYPE_UNKNOWN;
24073 if (it->bidi_p)
24074 {
24075 glyph->resolved_level = it->bidi_it.resolved_level;
24076 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24077 emacs_abort ();
24078 glyph->bidi_type = it->bidi_it.type;
24079 }
24080 else
24081 {
24082 glyph->resolved_level = 0;
24083 glyph->bidi_type = UNKNOWN_BT;
24084 }
24085 ++it->glyph_row->used[area];
24086 }
24087 else
24088 IT_EXPAND_MATRIX_WIDTH (it, area);
24089 }
24090
24091 /* Store one glyph for the composition IT->cmp_it.id in
24092 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24093 non-null. */
24094
24095 static void
24096 append_composite_glyph (struct it *it)
24097 {
24098 struct glyph *glyph;
24099 enum glyph_row_area area = it->area;
24100
24101 eassert (it->glyph_row);
24102
24103 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24104 if (glyph < it->glyph_row->glyphs[area + 1])
24105 {
24106 /* If the glyph row is reversed, we need to prepend the glyph
24107 rather than append it. */
24108 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24109 {
24110 struct glyph *g;
24111
24112 /* Make room for the new glyph. */
24113 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24114 g[1] = *g;
24115 glyph = it->glyph_row->glyphs[it->area];
24116 }
24117 glyph->charpos = it->cmp_it.charpos;
24118 glyph->object = it->object;
24119 glyph->pixel_width = it->pixel_width;
24120 glyph->ascent = it->ascent;
24121 glyph->descent = it->descent;
24122 glyph->voffset = it->voffset;
24123 glyph->type = COMPOSITE_GLYPH;
24124 if (it->cmp_it.ch < 0)
24125 {
24126 glyph->u.cmp.automatic = 0;
24127 glyph->u.cmp.id = it->cmp_it.id;
24128 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24129 }
24130 else
24131 {
24132 glyph->u.cmp.automatic = 1;
24133 glyph->u.cmp.id = it->cmp_it.id;
24134 glyph->slice.cmp.from = it->cmp_it.from;
24135 glyph->slice.cmp.to = it->cmp_it.to - 1;
24136 }
24137 glyph->avoid_cursor_p = it->avoid_cursor_p;
24138 glyph->multibyte_p = it->multibyte_p;
24139 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24140 {
24141 /* In R2L rows, the left and the right box edges need to be
24142 drawn in reverse direction. */
24143 glyph->right_box_line_p = it->start_of_box_run_p;
24144 glyph->left_box_line_p = it->end_of_box_run_p;
24145 }
24146 else
24147 {
24148 glyph->left_box_line_p = it->start_of_box_run_p;
24149 glyph->right_box_line_p = it->end_of_box_run_p;
24150 }
24151 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24152 || it->phys_descent > it->descent);
24153 glyph->padding_p = 0;
24154 glyph->glyph_not_available_p = 0;
24155 glyph->face_id = it->face_id;
24156 glyph->font_type = FONT_TYPE_UNKNOWN;
24157 if (it->bidi_p)
24158 {
24159 glyph->resolved_level = it->bidi_it.resolved_level;
24160 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24161 emacs_abort ();
24162 glyph->bidi_type = it->bidi_it.type;
24163 }
24164 ++it->glyph_row->used[area];
24165 }
24166 else
24167 IT_EXPAND_MATRIX_WIDTH (it, area);
24168 }
24169
24170
24171 /* Change IT->ascent and IT->height according to the setting of
24172 IT->voffset. */
24173
24174 static void
24175 take_vertical_position_into_account (struct it *it)
24176 {
24177 if (it->voffset)
24178 {
24179 if (it->voffset < 0)
24180 /* Increase the ascent so that we can display the text higher
24181 in the line. */
24182 it->ascent -= it->voffset;
24183 else
24184 /* Increase the descent so that we can display the text lower
24185 in the line. */
24186 it->descent += it->voffset;
24187 }
24188 }
24189
24190
24191 /* Produce glyphs/get display metrics for the image IT is loaded with.
24192 See the description of struct display_iterator in dispextern.h for
24193 an overview of struct display_iterator. */
24194
24195 static void
24196 produce_image_glyph (struct it *it)
24197 {
24198 struct image *img;
24199 struct face *face;
24200 int glyph_ascent, crop;
24201 struct glyph_slice slice;
24202
24203 eassert (it->what == IT_IMAGE);
24204
24205 face = FACE_FROM_ID (it->f, it->face_id);
24206 eassert (face);
24207 /* Make sure X resources of the face is loaded. */
24208 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24209
24210 if (it->image_id < 0)
24211 {
24212 /* Fringe bitmap. */
24213 it->ascent = it->phys_ascent = 0;
24214 it->descent = it->phys_descent = 0;
24215 it->pixel_width = 0;
24216 it->nglyphs = 0;
24217 return;
24218 }
24219
24220 img = IMAGE_FROM_ID (it->f, it->image_id);
24221 eassert (img);
24222 /* Make sure X resources of the image is loaded. */
24223 prepare_image_for_display (it->f, img);
24224
24225 slice.x = slice.y = 0;
24226 slice.width = img->width;
24227 slice.height = img->height;
24228
24229 if (INTEGERP (it->slice.x))
24230 slice.x = XINT (it->slice.x);
24231 else if (FLOATP (it->slice.x))
24232 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24233
24234 if (INTEGERP (it->slice.y))
24235 slice.y = XINT (it->slice.y);
24236 else if (FLOATP (it->slice.y))
24237 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24238
24239 if (INTEGERP (it->slice.width))
24240 slice.width = XINT (it->slice.width);
24241 else if (FLOATP (it->slice.width))
24242 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24243
24244 if (INTEGERP (it->slice.height))
24245 slice.height = XINT (it->slice.height);
24246 else if (FLOATP (it->slice.height))
24247 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24248
24249 if (slice.x >= img->width)
24250 slice.x = img->width;
24251 if (slice.y >= img->height)
24252 slice.y = img->height;
24253 if (slice.x + slice.width >= img->width)
24254 slice.width = img->width - slice.x;
24255 if (slice.y + slice.height > img->height)
24256 slice.height = img->height - slice.y;
24257
24258 if (slice.width == 0 || slice.height == 0)
24259 return;
24260
24261 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24262
24263 it->descent = slice.height - glyph_ascent;
24264 if (slice.y == 0)
24265 it->descent += img->vmargin;
24266 if (slice.y + slice.height == img->height)
24267 it->descent += img->vmargin;
24268 it->phys_descent = it->descent;
24269
24270 it->pixel_width = slice.width;
24271 if (slice.x == 0)
24272 it->pixel_width += img->hmargin;
24273 if (slice.x + slice.width == img->width)
24274 it->pixel_width += img->hmargin;
24275
24276 /* It's quite possible for images to have an ascent greater than
24277 their height, so don't get confused in that case. */
24278 if (it->descent < 0)
24279 it->descent = 0;
24280
24281 it->nglyphs = 1;
24282
24283 if (face->box != FACE_NO_BOX)
24284 {
24285 if (face->box_line_width > 0)
24286 {
24287 if (slice.y == 0)
24288 it->ascent += face->box_line_width;
24289 if (slice.y + slice.height == img->height)
24290 it->descent += face->box_line_width;
24291 }
24292
24293 if (it->start_of_box_run_p && slice.x == 0)
24294 it->pixel_width += eabs (face->box_line_width);
24295 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24296 it->pixel_width += eabs (face->box_line_width);
24297 }
24298
24299 take_vertical_position_into_account (it);
24300
24301 /* Automatically crop wide image glyphs at right edge so we can
24302 draw the cursor on same display row. */
24303 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24304 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24305 {
24306 it->pixel_width -= crop;
24307 slice.width -= crop;
24308 }
24309
24310 if (it->glyph_row)
24311 {
24312 struct glyph *glyph;
24313 enum glyph_row_area area = it->area;
24314
24315 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24316 if (glyph < it->glyph_row->glyphs[area + 1])
24317 {
24318 glyph->charpos = CHARPOS (it->position);
24319 glyph->object = it->object;
24320 glyph->pixel_width = it->pixel_width;
24321 glyph->ascent = glyph_ascent;
24322 glyph->descent = it->descent;
24323 glyph->voffset = it->voffset;
24324 glyph->type = IMAGE_GLYPH;
24325 glyph->avoid_cursor_p = it->avoid_cursor_p;
24326 glyph->multibyte_p = it->multibyte_p;
24327 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24328 {
24329 /* In R2L rows, the left and the right box edges need to be
24330 drawn in reverse direction. */
24331 glyph->right_box_line_p = it->start_of_box_run_p;
24332 glyph->left_box_line_p = it->end_of_box_run_p;
24333 }
24334 else
24335 {
24336 glyph->left_box_line_p = it->start_of_box_run_p;
24337 glyph->right_box_line_p = it->end_of_box_run_p;
24338 }
24339 glyph->overlaps_vertically_p = 0;
24340 glyph->padding_p = 0;
24341 glyph->glyph_not_available_p = 0;
24342 glyph->face_id = it->face_id;
24343 glyph->u.img_id = img->id;
24344 glyph->slice.img = slice;
24345 glyph->font_type = FONT_TYPE_UNKNOWN;
24346 if (it->bidi_p)
24347 {
24348 glyph->resolved_level = it->bidi_it.resolved_level;
24349 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24350 emacs_abort ();
24351 glyph->bidi_type = it->bidi_it.type;
24352 }
24353 ++it->glyph_row->used[area];
24354 }
24355 else
24356 IT_EXPAND_MATRIX_WIDTH (it, area);
24357 }
24358 }
24359
24360
24361 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24362 of the glyph, WIDTH and HEIGHT are the width and height of the
24363 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24364
24365 static void
24366 append_stretch_glyph (struct it *it, Lisp_Object object,
24367 int width, int height, int ascent)
24368 {
24369 struct glyph *glyph;
24370 enum glyph_row_area area = it->area;
24371
24372 eassert (ascent >= 0 && ascent <= height);
24373
24374 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24375 if (glyph < it->glyph_row->glyphs[area + 1])
24376 {
24377 /* If the glyph row is reversed, we need to prepend the glyph
24378 rather than append it. */
24379 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24380 {
24381 struct glyph *g;
24382
24383 /* Make room for the additional glyph. */
24384 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24385 g[1] = *g;
24386 glyph = it->glyph_row->glyphs[area];
24387 }
24388 glyph->charpos = CHARPOS (it->position);
24389 glyph->object = object;
24390 glyph->pixel_width = width;
24391 glyph->ascent = ascent;
24392 glyph->descent = height - ascent;
24393 glyph->voffset = it->voffset;
24394 glyph->type = STRETCH_GLYPH;
24395 glyph->avoid_cursor_p = it->avoid_cursor_p;
24396 glyph->multibyte_p = it->multibyte_p;
24397 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24398 {
24399 /* In R2L rows, the left and the right box edges need to be
24400 drawn in reverse direction. */
24401 glyph->right_box_line_p = it->start_of_box_run_p;
24402 glyph->left_box_line_p = it->end_of_box_run_p;
24403 }
24404 else
24405 {
24406 glyph->left_box_line_p = it->start_of_box_run_p;
24407 glyph->right_box_line_p = it->end_of_box_run_p;
24408 }
24409 glyph->overlaps_vertically_p = 0;
24410 glyph->padding_p = 0;
24411 glyph->glyph_not_available_p = 0;
24412 glyph->face_id = it->face_id;
24413 glyph->u.stretch.ascent = ascent;
24414 glyph->u.stretch.height = height;
24415 glyph->slice.img = null_glyph_slice;
24416 glyph->font_type = FONT_TYPE_UNKNOWN;
24417 if (it->bidi_p)
24418 {
24419 glyph->resolved_level = it->bidi_it.resolved_level;
24420 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24421 emacs_abort ();
24422 glyph->bidi_type = it->bidi_it.type;
24423 }
24424 else
24425 {
24426 glyph->resolved_level = 0;
24427 glyph->bidi_type = UNKNOWN_BT;
24428 }
24429 ++it->glyph_row->used[area];
24430 }
24431 else
24432 IT_EXPAND_MATRIX_WIDTH (it, area);
24433 }
24434
24435 #endif /* HAVE_WINDOW_SYSTEM */
24436
24437 /* Produce a stretch glyph for iterator IT. IT->object is the value
24438 of the glyph property displayed. The value must be a list
24439 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24440 being recognized:
24441
24442 1. `:width WIDTH' specifies that the space should be WIDTH *
24443 canonical char width wide. WIDTH may be an integer or floating
24444 point number.
24445
24446 2. `:relative-width FACTOR' specifies that the width of the stretch
24447 should be computed from the width of the first character having the
24448 `glyph' property, and should be FACTOR times that width.
24449
24450 3. `:align-to HPOS' specifies that the space should be wide enough
24451 to reach HPOS, a value in canonical character units.
24452
24453 Exactly one of the above pairs must be present.
24454
24455 4. `:height HEIGHT' specifies that the height of the stretch produced
24456 should be HEIGHT, measured in canonical character units.
24457
24458 5. `:relative-height FACTOR' specifies that the height of the
24459 stretch should be FACTOR times the height of the characters having
24460 the glyph property.
24461
24462 Either none or exactly one of 4 or 5 must be present.
24463
24464 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24465 of the stretch should be used for the ascent of the stretch.
24466 ASCENT must be in the range 0 <= ASCENT <= 100. */
24467
24468 void
24469 produce_stretch_glyph (struct it *it)
24470 {
24471 /* (space :width WIDTH :height HEIGHT ...) */
24472 Lisp_Object prop, plist;
24473 int width = 0, height = 0, align_to = -1;
24474 int zero_width_ok_p = 0;
24475 double tem;
24476 struct font *font = NULL;
24477
24478 #ifdef HAVE_WINDOW_SYSTEM
24479 int ascent = 0;
24480 int zero_height_ok_p = 0;
24481
24482 if (FRAME_WINDOW_P (it->f))
24483 {
24484 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24485 font = face->font ? face->font : FRAME_FONT (it->f);
24486 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24487 }
24488 #endif
24489
24490 /* List should start with `space'. */
24491 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24492 plist = XCDR (it->object);
24493
24494 /* Compute the width of the stretch. */
24495 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24496 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24497 {
24498 /* Absolute width `:width WIDTH' specified and valid. */
24499 zero_width_ok_p = 1;
24500 width = (int)tem;
24501 }
24502 #ifdef HAVE_WINDOW_SYSTEM
24503 else if (FRAME_WINDOW_P (it->f)
24504 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24505 {
24506 /* Relative width `:relative-width FACTOR' specified and valid.
24507 Compute the width of the characters having the `glyph'
24508 property. */
24509 struct it it2;
24510 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24511
24512 it2 = *it;
24513 if (it->multibyte_p)
24514 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24515 else
24516 {
24517 it2.c = it2.char_to_display = *p, it2.len = 1;
24518 if (! ASCII_CHAR_P (it2.c))
24519 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24520 }
24521
24522 it2.glyph_row = NULL;
24523 it2.what = IT_CHARACTER;
24524 x_produce_glyphs (&it2);
24525 width = NUMVAL (prop) * it2.pixel_width;
24526 }
24527 #endif /* HAVE_WINDOW_SYSTEM */
24528 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24529 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24530 {
24531 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24532 align_to = (align_to < 0
24533 ? 0
24534 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24535 else if (align_to < 0)
24536 align_to = window_box_left_offset (it->w, TEXT_AREA);
24537 width = max (0, (int)tem + align_to - it->current_x);
24538 zero_width_ok_p = 1;
24539 }
24540 else
24541 /* Nothing specified -> width defaults to canonical char width. */
24542 width = FRAME_COLUMN_WIDTH (it->f);
24543
24544 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24545 width = 1;
24546
24547 #ifdef HAVE_WINDOW_SYSTEM
24548 /* Compute height. */
24549 if (FRAME_WINDOW_P (it->f))
24550 {
24551 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24552 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24553 {
24554 height = (int)tem;
24555 zero_height_ok_p = 1;
24556 }
24557 else if (prop = Fplist_get (plist, QCrelative_height),
24558 NUMVAL (prop) > 0)
24559 height = FONT_HEIGHT (font) * NUMVAL (prop);
24560 else
24561 height = FONT_HEIGHT (font);
24562
24563 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24564 height = 1;
24565
24566 /* Compute percentage of height used for ascent. If
24567 `:ascent ASCENT' is present and valid, use that. Otherwise,
24568 derive the ascent from the font in use. */
24569 if (prop = Fplist_get (plist, QCascent),
24570 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24571 ascent = height * NUMVAL (prop) / 100.0;
24572 else if (!NILP (prop)
24573 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24574 ascent = min (max (0, (int)tem), height);
24575 else
24576 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24577 }
24578 else
24579 #endif /* HAVE_WINDOW_SYSTEM */
24580 height = 1;
24581
24582 if (width > 0 && it->line_wrap != TRUNCATE
24583 && it->current_x + width > it->last_visible_x)
24584 {
24585 width = it->last_visible_x - it->current_x;
24586 #ifdef HAVE_WINDOW_SYSTEM
24587 /* Subtract one more pixel from the stretch width, but only on
24588 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24589 width -= FRAME_WINDOW_P (it->f);
24590 #endif
24591 }
24592
24593 if (width > 0 && height > 0 && it->glyph_row)
24594 {
24595 Lisp_Object o_object = it->object;
24596 Lisp_Object object = it->stack[it->sp - 1].string;
24597 int n = width;
24598
24599 if (!STRINGP (object))
24600 object = it->w->contents;
24601 #ifdef HAVE_WINDOW_SYSTEM
24602 if (FRAME_WINDOW_P (it->f))
24603 append_stretch_glyph (it, object, width, height, ascent);
24604 else
24605 #endif
24606 {
24607 it->object = object;
24608 it->char_to_display = ' ';
24609 it->pixel_width = it->len = 1;
24610 while (n--)
24611 tty_append_glyph (it);
24612 it->object = o_object;
24613 }
24614 }
24615
24616 it->pixel_width = width;
24617 #ifdef HAVE_WINDOW_SYSTEM
24618 if (FRAME_WINDOW_P (it->f))
24619 {
24620 it->ascent = it->phys_ascent = ascent;
24621 it->descent = it->phys_descent = height - it->ascent;
24622 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24623 take_vertical_position_into_account (it);
24624 }
24625 else
24626 #endif
24627 it->nglyphs = width;
24628 }
24629
24630 /* Get information about special display element WHAT in an
24631 environment described by IT. WHAT is one of IT_TRUNCATION or
24632 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24633 non-null glyph_row member. This function ensures that fields like
24634 face_id, c, len of IT are left untouched. */
24635
24636 static void
24637 produce_special_glyphs (struct it *it, enum display_element_type what)
24638 {
24639 struct it temp_it;
24640 Lisp_Object gc;
24641 GLYPH glyph;
24642
24643 temp_it = *it;
24644 temp_it.object = make_number (0);
24645 memset (&temp_it.current, 0, sizeof temp_it.current);
24646
24647 if (what == IT_CONTINUATION)
24648 {
24649 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24650 if (it->bidi_it.paragraph_dir == R2L)
24651 SET_GLYPH_FROM_CHAR (glyph, '/');
24652 else
24653 SET_GLYPH_FROM_CHAR (glyph, '\\');
24654 if (it->dp
24655 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24656 {
24657 /* FIXME: Should we mirror GC for R2L lines? */
24658 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24659 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24660 }
24661 }
24662 else if (what == IT_TRUNCATION)
24663 {
24664 /* Truncation glyph. */
24665 SET_GLYPH_FROM_CHAR (glyph, '$');
24666 if (it->dp
24667 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24668 {
24669 /* FIXME: Should we mirror GC for R2L lines? */
24670 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24671 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24672 }
24673 }
24674 else
24675 emacs_abort ();
24676
24677 #ifdef HAVE_WINDOW_SYSTEM
24678 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24679 is turned off, we precede the truncation/continuation glyphs by a
24680 stretch glyph whose width is computed such that these special
24681 glyphs are aligned at the window margin, even when very different
24682 fonts are used in different glyph rows. */
24683 if (FRAME_WINDOW_P (temp_it.f)
24684 /* init_iterator calls this with it->glyph_row == NULL, and it
24685 wants only the pixel width of the truncation/continuation
24686 glyphs. */
24687 && temp_it.glyph_row
24688 /* insert_left_trunc_glyphs calls us at the beginning of the
24689 row, and it has its own calculation of the stretch glyph
24690 width. */
24691 && temp_it.glyph_row->used[TEXT_AREA] > 0
24692 && (temp_it.glyph_row->reversed_p
24693 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24694 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24695 {
24696 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24697
24698 if (stretch_width > 0)
24699 {
24700 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24701 struct font *font =
24702 face->font ? face->font : FRAME_FONT (temp_it.f);
24703 int stretch_ascent =
24704 (((temp_it.ascent + temp_it.descent)
24705 * FONT_BASE (font)) / FONT_HEIGHT (font));
24706
24707 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24708 temp_it.ascent + temp_it.descent,
24709 stretch_ascent);
24710 }
24711 }
24712 #endif
24713
24714 temp_it.dp = NULL;
24715 temp_it.what = IT_CHARACTER;
24716 temp_it.len = 1;
24717 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24718 temp_it.face_id = GLYPH_FACE (glyph);
24719 temp_it.len = CHAR_BYTES (temp_it.c);
24720
24721 PRODUCE_GLYPHS (&temp_it);
24722 it->pixel_width = temp_it.pixel_width;
24723 it->nglyphs = temp_it.pixel_width;
24724 }
24725
24726 #ifdef HAVE_WINDOW_SYSTEM
24727
24728 /* Calculate line-height and line-spacing properties.
24729 An integer value specifies explicit pixel value.
24730 A float value specifies relative value to current face height.
24731 A cons (float . face-name) specifies relative value to
24732 height of specified face font.
24733
24734 Returns height in pixels, or nil. */
24735
24736
24737 static Lisp_Object
24738 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24739 int boff, int override)
24740 {
24741 Lisp_Object face_name = Qnil;
24742 int ascent, descent, height;
24743
24744 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24745 return val;
24746
24747 if (CONSP (val))
24748 {
24749 face_name = XCAR (val);
24750 val = XCDR (val);
24751 if (!NUMBERP (val))
24752 val = make_number (1);
24753 if (NILP (face_name))
24754 {
24755 height = it->ascent + it->descent;
24756 goto scale;
24757 }
24758 }
24759
24760 if (NILP (face_name))
24761 {
24762 font = FRAME_FONT (it->f);
24763 boff = FRAME_BASELINE_OFFSET (it->f);
24764 }
24765 else if (EQ (face_name, Qt))
24766 {
24767 override = 0;
24768 }
24769 else
24770 {
24771 int face_id;
24772 struct face *face;
24773
24774 face_id = lookup_named_face (it->f, face_name, 0);
24775 if (face_id < 0)
24776 return make_number (-1);
24777
24778 face = FACE_FROM_ID (it->f, face_id);
24779 font = face->font;
24780 if (font == NULL)
24781 return make_number (-1);
24782 boff = font->baseline_offset;
24783 if (font->vertical_centering)
24784 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24785 }
24786
24787 ascent = FONT_BASE (font) + boff;
24788 descent = FONT_DESCENT (font) - boff;
24789
24790 if (override)
24791 {
24792 it->override_ascent = ascent;
24793 it->override_descent = descent;
24794 it->override_boff = boff;
24795 }
24796
24797 height = ascent + descent;
24798
24799 scale:
24800 if (FLOATP (val))
24801 height = (int)(XFLOAT_DATA (val) * height);
24802 else if (INTEGERP (val))
24803 height *= XINT (val);
24804
24805 return make_number (height);
24806 }
24807
24808
24809 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24810 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24811 and only if this is for a character for which no font was found.
24812
24813 If the display method (it->glyphless_method) is
24814 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24815 length of the acronym or the hexadecimal string, UPPER_XOFF and
24816 UPPER_YOFF are pixel offsets for the upper part of the string,
24817 LOWER_XOFF and LOWER_YOFF are for the lower part.
24818
24819 For the other display methods, LEN through LOWER_YOFF are zero. */
24820
24821 static void
24822 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24823 short upper_xoff, short upper_yoff,
24824 short lower_xoff, short lower_yoff)
24825 {
24826 struct glyph *glyph;
24827 enum glyph_row_area area = it->area;
24828
24829 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24830 if (glyph < it->glyph_row->glyphs[area + 1])
24831 {
24832 /* If the glyph row is reversed, we need to prepend the glyph
24833 rather than append it. */
24834 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24835 {
24836 struct glyph *g;
24837
24838 /* Make room for the additional glyph. */
24839 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24840 g[1] = *g;
24841 glyph = it->glyph_row->glyphs[area];
24842 }
24843 glyph->charpos = CHARPOS (it->position);
24844 glyph->object = it->object;
24845 glyph->pixel_width = it->pixel_width;
24846 glyph->ascent = it->ascent;
24847 glyph->descent = it->descent;
24848 glyph->voffset = it->voffset;
24849 glyph->type = GLYPHLESS_GLYPH;
24850 glyph->u.glyphless.method = it->glyphless_method;
24851 glyph->u.glyphless.for_no_font = for_no_font;
24852 glyph->u.glyphless.len = len;
24853 glyph->u.glyphless.ch = it->c;
24854 glyph->slice.glyphless.upper_xoff = upper_xoff;
24855 glyph->slice.glyphless.upper_yoff = upper_yoff;
24856 glyph->slice.glyphless.lower_xoff = lower_xoff;
24857 glyph->slice.glyphless.lower_yoff = lower_yoff;
24858 glyph->avoid_cursor_p = it->avoid_cursor_p;
24859 glyph->multibyte_p = it->multibyte_p;
24860 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24861 {
24862 /* In R2L rows, the left and the right box edges need to be
24863 drawn in reverse direction. */
24864 glyph->right_box_line_p = it->start_of_box_run_p;
24865 glyph->left_box_line_p = it->end_of_box_run_p;
24866 }
24867 else
24868 {
24869 glyph->left_box_line_p = it->start_of_box_run_p;
24870 glyph->right_box_line_p = it->end_of_box_run_p;
24871 }
24872 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24873 || it->phys_descent > it->descent);
24874 glyph->padding_p = 0;
24875 glyph->glyph_not_available_p = 0;
24876 glyph->face_id = face_id;
24877 glyph->font_type = FONT_TYPE_UNKNOWN;
24878 if (it->bidi_p)
24879 {
24880 glyph->resolved_level = it->bidi_it.resolved_level;
24881 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24882 emacs_abort ();
24883 glyph->bidi_type = it->bidi_it.type;
24884 }
24885 ++it->glyph_row->used[area];
24886 }
24887 else
24888 IT_EXPAND_MATRIX_WIDTH (it, area);
24889 }
24890
24891
24892 /* Produce a glyph for a glyphless character for iterator IT.
24893 IT->glyphless_method specifies which method to use for displaying
24894 the character. See the description of enum
24895 glyphless_display_method in dispextern.h for the detail.
24896
24897 FOR_NO_FONT is nonzero if and only if this is for a character for
24898 which no font was found. ACRONYM, if non-nil, is an acronym string
24899 for the character. */
24900
24901 static void
24902 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24903 {
24904 int face_id;
24905 struct face *face;
24906 struct font *font;
24907 int base_width, base_height, width, height;
24908 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24909 int len;
24910
24911 /* Get the metrics of the base font. We always refer to the current
24912 ASCII face. */
24913 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24914 font = face->font ? face->font : FRAME_FONT (it->f);
24915 it->ascent = FONT_BASE (font) + font->baseline_offset;
24916 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24917 base_height = it->ascent + it->descent;
24918 base_width = font->average_width;
24919
24920 face_id = merge_glyphless_glyph_face (it);
24921
24922 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24923 {
24924 it->pixel_width = THIN_SPACE_WIDTH;
24925 len = 0;
24926 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24927 }
24928 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24929 {
24930 width = CHAR_WIDTH (it->c);
24931 if (width == 0)
24932 width = 1;
24933 else if (width > 4)
24934 width = 4;
24935 it->pixel_width = base_width * width;
24936 len = 0;
24937 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24938 }
24939 else
24940 {
24941 char buf[7];
24942 const char *str;
24943 unsigned int code[6];
24944 int upper_len;
24945 int ascent, descent;
24946 struct font_metrics metrics_upper, metrics_lower;
24947
24948 face = FACE_FROM_ID (it->f, face_id);
24949 font = face->font ? face->font : FRAME_FONT (it->f);
24950 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24951
24952 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24953 {
24954 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24955 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24956 if (CONSP (acronym))
24957 acronym = XCAR (acronym);
24958 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24959 }
24960 else
24961 {
24962 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24963 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24964 str = buf;
24965 }
24966 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24967 code[len] = font->driver->encode_char (font, str[len]);
24968 upper_len = (len + 1) / 2;
24969 font->driver->text_extents (font, code, upper_len,
24970 &metrics_upper);
24971 font->driver->text_extents (font, code + upper_len, len - upper_len,
24972 &metrics_lower);
24973
24974
24975
24976 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24977 width = max (metrics_upper.width, metrics_lower.width) + 4;
24978 upper_xoff = upper_yoff = 2; /* the typical case */
24979 if (base_width >= width)
24980 {
24981 /* Align the upper to the left, the lower to the right. */
24982 it->pixel_width = base_width;
24983 lower_xoff = base_width - 2 - metrics_lower.width;
24984 }
24985 else
24986 {
24987 /* Center the shorter one. */
24988 it->pixel_width = width;
24989 if (metrics_upper.width >= metrics_lower.width)
24990 lower_xoff = (width - metrics_lower.width) / 2;
24991 else
24992 {
24993 /* FIXME: This code doesn't look right. It formerly was
24994 missing the "lower_xoff = 0;", which couldn't have
24995 been right since it left lower_xoff uninitialized. */
24996 lower_xoff = 0;
24997 upper_xoff = (width - metrics_upper.width) / 2;
24998 }
24999 }
25000
25001 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25002 top, bottom, and between upper and lower strings. */
25003 height = (metrics_upper.ascent + metrics_upper.descent
25004 + metrics_lower.ascent + metrics_lower.descent) + 5;
25005 /* Center vertically.
25006 H:base_height, D:base_descent
25007 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25008
25009 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25010 descent = D - H/2 + h/2;
25011 lower_yoff = descent - 2 - ld;
25012 upper_yoff = lower_yoff - la - 1 - ud; */
25013 ascent = - (it->descent - (base_height + height + 1) / 2);
25014 descent = it->descent - (base_height - height) / 2;
25015 lower_yoff = descent - 2 - metrics_lower.descent;
25016 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25017 - metrics_upper.descent);
25018 /* Don't make the height shorter than the base height. */
25019 if (height > base_height)
25020 {
25021 it->ascent = ascent;
25022 it->descent = descent;
25023 }
25024 }
25025
25026 it->phys_ascent = it->ascent;
25027 it->phys_descent = it->descent;
25028 if (it->glyph_row)
25029 append_glyphless_glyph (it, face_id, for_no_font, len,
25030 upper_xoff, upper_yoff,
25031 lower_xoff, lower_yoff);
25032 it->nglyphs = 1;
25033 take_vertical_position_into_account (it);
25034 }
25035
25036
25037 /* RIF:
25038 Produce glyphs/get display metrics for the display element IT is
25039 loaded with. See the description of struct it in dispextern.h
25040 for an overview of struct it. */
25041
25042 void
25043 x_produce_glyphs (struct it *it)
25044 {
25045 int extra_line_spacing = it->extra_line_spacing;
25046
25047 it->glyph_not_available_p = 0;
25048
25049 if (it->what == IT_CHARACTER)
25050 {
25051 XChar2b char2b;
25052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25053 struct font *font = face->font;
25054 struct font_metrics *pcm = NULL;
25055 int boff; /* baseline offset */
25056
25057 if (font == NULL)
25058 {
25059 /* When no suitable font is found, display this character by
25060 the method specified in the first extra slot of
25061 Vglyphless_char_display. */
25062 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25063
25064 eassert (it->what == IT_GLYPHLESS);
25065 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25066 goto done;
25067 }
25068
25069 boff = font->baseline_offset;
25070 if (font->vertical_centering)
25071 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25072
25073 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25074 {
25075 int stretched_p;
25076
25077 it->nglyphs = 1;
25078
25079 if (it->override_ascent >= 0)
25080 {
25081 it->ascent = it->override_ascent;
25082 it->descent = it->override_descent;
25083 boff = it->override_boff;
25084 }
25085 else
25086 {
25087 it->ascent = FONT_BASE (font) + boff;
25088 it->descent = FONT_DESCENT (font) - boff;
25089 }
25090
25091 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25092 {
25093 pcm = get_per_char_metric (font, &char2b);
25094 if (pcm->width == 0
25095 && pcm->rbearing == 0 && pcm->lbearing == 0)
25096 pcm = NULL;
25097 }
25098
25099 if (pcm)
25100 {
25101 it->phys_ascent = pcm->ascent + boff;
25102 it->phys_descent = pcm->descent - boff;
25103 it->pixel_width = pcm->width;
25104 }
25105 else
25106 {
25107 it->glyph_not_available_p = 1;
25108 it->phys_ascent = it->ascent;
25109 it->phys_descent = it->descent;
25110 it->pixel_width = font->space_width;
25111 }
25112
25113 if (it->constrain_row_ascent_descent_p)
25114 {
25115 if (it->descent > it->max_descent)
25116 {
25117 it->ascent += it->descent - it->max_descent;
25118 it->descent = it->max_descent;
25119 }
25120 if (it->ascent > it->max_ascent)
25121 {
25122 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25123 it->ascent = it->max_ascent;
25124 }
25125 it->phys_ascent = min (it->phys_ascent, it->ascent);
25126 it->phys_descent = min (it->phys_descent, it->descent);
25127 extra_line_spacing = 0;
25128 }
25129
25130 /* If this is a space inside a region of text with
25131 `space-width' property, change its width. */
25132 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25133 if (stretched_p)
25134 it->pixel_width *= XFLOATINT (it->space_width);
25135
25136 /* If face has a box, add the box thickness to the character
25137 height. If character has a box line to the left and/or
25138 right, add the box line width to the character's width. */
25139 if (face->box != FACE_NO_BOX)
25140 {
25141 int thick = face->box_line_width;
25142
25143 if (thick > 0)
25144 {
25145 it->ascent += thick;
25146 it->descent += thick;
25147 }
25148 else
25149 thick = -thick;
25150
25151 if (it->start_of_box_run_p)
25152 it->pixel_width += thick;
25153 if (it->end_of_box_run_p)
25154 it->pixel_width += thick;
25155 }
25156
25157 /* If face has an overline, add the height of the overline
25158 (1 pixel) and a 1 pixel margin to the character height. */
25159 if (face->overline_p)
25160 it->ascent += overline_margin;
25161
25162 if (it->constrain_row_ascent_descent_p)
25163 {
25164 if (it->ascent > it->max_ascent)
25165 it->ascent = it->max_ascent;
25166 if (it->descent > it->max_descent)
25167 it->descent = it->max_descent;
25168 }
25169
25170 take_vertical_position_into_account (it);
25171
25172 /* If we have to actually produce glyphs, do it. */
25173 if (it->glyph_row)
25174 {
25175 if (stretched_p)
25176 {
25177 /* Translate a space with a `space-width' property
25178 into a stretch glyph. */
25179 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25180 / FONT_HEIGHT (font));
25181 append_stretch_glyph (it, it->object, it->pixel_width,
25182 it->ascent + it->descent, ascent);
25183 }
25184 else
25185 append_glyph (it);
25186
25187 /* If characters with lbearing or rbearing are displayed
25188 in this line, record that fact in a flag of the
25189 glyph row. This is used to optimize X output code. */
25190 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25191 it->glyph_row->contains_overlapping_glyphs_p = 1;
25192 }
25193 if (! stretched_p && it->pixel_width == 0)
25194 /* We assure that all visible glyphs have at least 1-pixel
25195 width. */
25196 it->pixel_width = 1;
25197 }
25198 else if (it->char_to_display == '\n')
25199 {
25200 /* A newline has no width, but we need the height of the
25201 line. But if previous part of the line sets a height,
25202 don't increase that height */
25203
25204 Lisp_Object height;
25205 Lisp_Object total_height = Qnil;
25206
25207 it->override_ascent = -1;
25208 it->pixel_width = 0;
25209 it->nglyphs = 0;
25210
25211 height = get_it_property (it, Qline_height);
25212 /* Split (line-height total-height) list */
25213 if (CONSP (height)
25214 && CONSP (XCDR (height))
25215 && NILP (XCDR (XCDR (height))))
25216 {
25217 total_height = XCAR (XCDR (height));
25218 height = XCAR (height);
25219 }
25220 height = calc_line_height_property (it, height, font, boff, 1);
25221
25222 if (it->override_ascent >= 0)
25223 {
25224 it->ascent = it->override_ascent;
25225 it->descent = it->override_descent;
25226 boff = it->override_boff;
25227 }
25228 else
25229 {
25230 it->ascent = FONT_BASE (font) + boff;
25231 it->descent = FONT_DESCENT (font) - boff;
25232 }
25233
25234 if (EQ (height, Qt))
25235 {
25236 if (it->descent > it->max_descent)
25237 {
25238 it->ascent += it->descent - it->max_descent;
25239 it->descent = it->max_descent;
25240 }
25241 if (it->ascent > it->max_ascent)
25242 {
25243 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25244 it->ascent = it->max_ascent;
25245 }
25246 it->phys_ascent = min (it->phys_ascent, it->ascent);
25247 it->phys_descent = min (it->phys_descent, it->descent);
25248 it->constrain_row_ascent_descent_p = 1;
25249 extra_line_spacing = 0;
25250 }
25251 else
25252 {
25253 Lisp_Object spacing;
25254
25255 it->phys_ascent = it->ascent;
25256 it->phys_descent = it->descent;
25257
25258 if ((it->max_ascent > 0 || it->max_descent > 0)
25259 && face->box != FACE_NO_BOX
25260 && face->box_line_width > 0)
25261 {
25262 it->ascent += face->box_line_width;
25263 it->descent += face->box_line_width;
25264 }
25265 if (!NILP (height)
25266 && XINT (height) > it->ascent + it->descent)
25267 it->ascent = XINT (height) - it->descent;
25268
25269 if (!NILP (total_height))
25270 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25271 else
25272 {
25273 spacing = get_it_property (it, Qline_spacing);
25274 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25275 }
25276 if (INTEGERP (spacing))
25277 {
25278 extra_line_spacing = XINT (spacing);
25279 if (!NILP (total_height))
25280 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25281 }
25282 }
25283 }
25284 else /* i.e. (it->char_to_display == '\t') */
25285 {
25286 if (font->space_width > 0)
25287 {
25288 int tab_width = it->tab_width * font->space_width;
25289 int x = it->current_x + it->continuation_lines_width;
25290 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25291
25292 /* If the distance from the current position to the next tab
25293 stop is less than a space character width, use the
25294 tab stop after that. */
25295 if (next_tab_x - x < font->space_width)
25296 next_tab_x += tab_width;
25297
25298 it->pixel_width = next_tab_x - x;
25299 it->nglyphs = 1;
25300 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25301 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25302
25303 if (it->glyph_row)
25304 {
25305 append_stretch_glyph (it, it->object, it->pixel_width,
25306 it->ascent + it->descent, it->ascent);
25307 }
25308 }
25309 else
25310 {
25311 it->pixel_width = 0;
25312 it->nglyphs = 1;
25313 }
25314 }
25315 }
25316 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25317 {
25318 /* A static composition.
25319
25320 Note: A composition is represented as one glyph in the
25321 glyph matrix. There are no padding glyphs.
25322
25323 Important note: pixel_width, ascent, and descent are the
25324 values of what is drawn by draw_glyphs (i.e. the values of
25325 the overall glyphs composed). */
25326 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25327 int boff; /* baseline offset */
25328 struct composition *cmp = composition_table[it->cmp_it.id];
25329 int glyph_len = cmp->glyph_len;
25330 struct font *font = face->font;
25331
25332 it->nglyphs = 1;
25333
25334 /* If we have not yet calculated pixel size data of glyphs of
25335 the composition for the current face font, calculate them
25336 now. Theoretically, we have to check all fonts for the
25337 glyphs, but that requires much time and memory space. So,
25338 here we check only the font of the first glyph. This may
25339 lead to incorrect display, but it's very rare, and C-l
25340 (recenter-top-bottom) can correct the display anyway. */
25341 if (! cmp->font || cmp->font != font)
25342 {
25343 /* Ascent and descent of the font of the first character
25344 of this composition (adjusted by baseline offset).
25345 Ascent and descent of overall glyphs should not be less
25346 than these, respectively. */
25347 int font_ascent, font_descent, font_height;
25348 /* Bounding box of the overall glyphs. */
25349 int leftmost, rightmost, lowest, highest;
25350 int lbearing, rbearing;
25351 int i, width, ascent, descent;
25352 int left_padded = 0, right_padded = 0;
25353 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25354 XChar2b char2b;
25355 struct font_metrics *pcm;
25356 int font_not_found_p;
25357 ptrdiff_t pos;
25358
25359 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25360 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25361 break;
25362 if (glyph_len < cmp->glyph_len)
25363 right_padded = 1;
25364 for (i = 0; i < glyph_len; i++)
25365 {
25366 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25367 break;
25368 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25369 }
25370 if (i > 0)
25371 left_padded = 1;
25372
25373 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25374 : IT_CHARPOS (*it));
25375 /* If no suitable font is found, use the default font. */
25376 font_not_found_p = font == NULL;
25377 if (font_not_found_p)
25378 {
25379 face = face->ascii_face;
25380 font = face->font;
25381 }
25382 boff = font->baseline_offset;
25383 if (font->vertical_centering)
25384 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25385 font_ascent = FONT_BASE (font) + boff;
25386 font_descent = FONT_DESCENT (font) - boff;
25387 font_height = FONT_HEIGHT (font);
25388
25389 cmp->font = font;
25390
25391 pcm = NULL;
25392 if (! font_not_found_p)
25393 {
25394 get_char_face_and_encoding (it->f, c, it->face_id,
25395 &char2b, 0);
25396 pcm = get_per_char_metric (font, &char2b);
25397 }
25398
25399 /* Initialize the bounding box. */
25400 if (pcm)
25401 {
25402 width = cmp->glyph_len > 0 ? pcm->width : 0;
25403 ascent = pcm->ascent;
25404 descent = pcm->descent;
25405 lbearing = pcm->lbearing;
25406 rbearing = pcm->rbearing;
25407 }
25408 else
25409 {
25410 width = cmp->glyph_len > 0 ? font->space_width : 0;
25411 ascent = FONT_BASE (font);
25412 descent = FONT_DESCENT (font);
25413 lbearing = 0;
25414 rbearing = width;
25415 }
25416
25417 rightmost = width;
25418 leftmost = 0;
25419 lowest = - descent + boff;
25420 highest = ascent + boff;
25421
25422 if (! font_not_found_p
25423 && font->default_ascent
25424 && CHAR_TABLE_P (Vuse_default_ascent)
25425 && !NILP (Faref (Vuse_default_ascent,
25426 make_number (it->char_to_display))))
25427 highest = font->default_ascent + boff;
25428
25429 /* Draw the first glyph at the normal position. It may be
25430 shifted to right later if some other glyphs are drawn
25431 at the left. */
25432 cmp->offsets[i * 2] = 0;
25433 cmp->offsets[i * 2 + 1] = boff;
25434 cmp->lbearing = lbearing;
25435 cmp->rbearing = rbearing;
25436
25437 /* Set cmp->offsets for the remaining glyphs. */
25438 for (i++; i < glyph_len; i++)
25439 {
25440 int left, right, btm, top;
25441 int ch = COMPOSITION_GLYPH (cmp, i);
25442 int face_id;
25443 struct face *this_face;
25444
25445 if (ch == '\t')
25446 ch = ' ';
25447 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25448 this_face = FACE_FROM_ID (it->f, face_id);
25449 font = this_face->font;
25450
25451 if (font == NULL)
25452 pcm = NULL;
25453 else
25454 {
25455 get_char_face_and_encoding (it->f, ch, face_id,
25456 &char2b, 0);
25457 pcm = get_per_char_metric (font, &char2b);
25458 }
25459 if (! pcm)
25460 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25461 else
25462 {
25463 width = pcm->width;
25464 ascent = pcm->ascent;
25465 descent = pcm->descent;
25466 lbearing = pcm->lbearing;
25467 rbearing = pcm->rbearing;
25468 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25469 {
25470 /* Relative composition with or without
25471 alternate chars. */
25472 left = (leftmost + rightmost - width) / 2;
25473 btm = - descent + boff;
25474 if (font->relative_compose
25475 && (! CHAR_TABLE_P (Vignore_relative_composition)
25476 || NILP (Faref (Vignore_relative_composition,
25477 make_number (ch)))))
25478 {
25479
25480 if (- descent >= font->relative_compose)
25481 /* One extra pixel between two glyphs. */
25482 btm = highest + 1;
25483 else if (ascent <= 0)
25484 /* One extra pixel between two glyphs. */
25485 btm = lowest - 1 - ascent - descent;
25486 }
25487 }
25488 else
25489 {
25490 /* A composition rule is specified by an integer
25491 value that encodes global and new reference
25492 points (GREF and NREF). GREF and NREF are
25493 specified by numbers as below:
25494
25495 0---1---2 -- ascent
25496 | |
25497 | |
25498 | |
25499 9--10--11 -- center
25500 | |
25501 ---3---4---5--- baseline
25502 | |
25503 6---7---8 -- descent
25504 */
25505 int rule = COMPOSITION_RULE (cmp, i);
25506 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25507
25508 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25509 grefx = gref % 3, nrefx = nref % 3;
25510 grefy = gref / 3, nrefy = nref / 3;
25511 if (xoff)
25512 xoff = font_height * (xoff - 128) / 256;
25513 if (yoff)
25514 yoff = font_height * (yoff - 128) / 256;
25515
25516 left = (leftmost
25517 + grefx * (rightmost - leftmost) / 2
25518 - nrefx * width / 2
25519 + xoff);
25520
25521 btm = ((grefy == 0 ? highest
25522 : grefy == 1 ? 0
25523 : grefy == 2 ? lowest
25524 : (highest + lowest) / 2)
25525 - (nrefy == 0 ? ascent + descent
25526 : nrefy == 1 ? descent - boff
25527 : nrefy == 2 ? 0
25528 : (ascent + descent) / 2)
25529 + yoff);
25530 }
25531
25532 cmp->offsets[i * 2] = left;
25533 cmp->offsets[i * 2 + 1] = btm + descent;
25534
25535 /* Update the bounding box of the overall glyphs. */
25536 if (width > 0)
25537 {
25538 right = left + width;
25539 if (left < leftmost)
25540 leftmost = left;
25541 if (right > rightmost)
25542 rightmost = right;
25543 }
25544 top = btm + descent + ascent;
25545 if (top > highest)
25546 highest = top;
25547 if (btm < lowest)
25548 lowest = btm;
25549
25550 if (cmp->lbearing > left + lbearing)
25551 cmp->lbearing = left + lbearing;
25552 if (cmp->rbearing < left + rbearing)
25553 cmp->rbearing = left + rbearing;
25554 }
25555 }
25556
25557 /* If there are glyphs whose x-offsets are negative,
25558 shift all glyphs to the right and make all x-offsets
25559 non-negative. */
25560 if (leftmost < 0)
25561 {
25562 for (i = 0; i < cmp->glyph_len; i++)
25563 cmp->offsets[i * 2] -= leftmost;
25564 rightmost -= leftmost;
25565 cmp->lbearing -= leftmost;
25566 cmp->rbearing -= leftmost;
25567 }
25568
25569 if (left_padded && cmp->lbearing < 0)
25570 {
25571 for (i = 0; i < cmp->glyph_len; i++)
25572 cmp->offsets[i * 2] -= cmp->lbearing;
25573 rightmost -= cmp->lbearing;
25574 cmp->rbearing -= cmp->lbearing;
25575 cmp->lbearing = 0;
25576 }
25577 if (right_padded && rightmost < cmp->rbearing)
25578 {
25579 rightmost = cmp->rbearing;
25580 }
25581
25582 cmp->pixel_width = rightmost;
25583 cmp->ascent = highest;
25584 cmp->descent = - lowest;
25585 if (cmp->ascent < font_ascent)
25586 cmp->ascent = font_ascent;
25587 if (cmp->descent < font_descent)
25588 cmp->descent = font_descent;
25589 }
25590
25591 if (it->glyph_row
25592 && (cmp->lbearing < 0
25593 || cmp->rbearing > cmp->pixel_width))
25594 it->glyph_row->contains_overlapping_glyphs_p = 1;
25595
25596 it->pixel_width = cmp->pixel_width;
25597 it->ascent = it->phys_ascent = cmp->ascent;
25598 it->descent = it->phys_descent = cmp->descent;
25599 if (face->box != FACE_NO_BOX)
25600 {
25601 int thick = face->box_line_width;
25602
25603 if (thick > 0)
25604 {
25605 it->ascent += thick;
25606 it->descent += thick;
25607 }
25608 else
25609 thick = - thick;
25610
25611 if (it->start_of_box_run_p)
25612 it->pixel_width += thick;
25613 if (it->end_of_box_run_p)
25614 it->pixel_width += thick;
25615 }
25616
25617 /* If face has an overline, add the height of the overline
25618 (1 pixel) and a 1 pixel margin to the character height. */
25619 if (face->overline_p)
25620 it->ascent += overline_margin;
25621
25622 take_vertical_position_into_account (it);
25623 if (it->ascent < 0)
25624 it->ascent = 0;
25625 if (it->descent < 0)
25626 it->descent = 0;
25627
25628 if (it->glyph_row && cmp->glyph_len > 0)
25629 append_composite_glyph (it);
25630 }
25631 else if (it->what == IT_COMPOSITION)
25632 {
25633 /* A dynamic (automatic) composition. */
25634 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25635 Lisp_Object gstring;
25636 struct font_metrics metrics;
25637
25638 it->nglyphs = 1;
25639
25640 gstring = composition_gstring_from_id (it->cmp_it.id);
25641 it->pixel_width
25642 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25643 &metrics);
25644 if (it->glyph_row
25645 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25646 it->glyph_row->contains_overlapping_glyphs_p = 1;
25647 it->ascent = it->phys_ascent = metrics.ascent;
25648 it->descent = it->phys_descent = metrics.descent;
25649 if (face->box != FACE_NO_BOX)
25650 {
25651 int thick = face->box_line_width;
25652
25653 if (thick > 0)
25654 {
25655 it->ascent += thick;
25656 it->descent += thick;
25657 }
25658 else
25659 thick = - thick;
25660
25661 if (it->start_of_box_run_p)
25662 it->pixel_width += thick;
25663 if (it->end_of_box_run_p)
25664 it->pixel_width += thick;
25665 }
25666 /* If face has an overline, add the height of the overline
25667 (1 pixel) and a 1 pixel margin to the character height. */
25668 if (face->overline_p)
25669 it->ascent += overline_margin;
25670 take_vertical_position_into_account (it);
25671 if (it->ascent < 0)
25672 it->ascent = 0;
25673 if (it->descent < 0)
25674 it->descent = 0;
25675
25676 if (it->glyph_row)
25677 append_composite_glyph (it);
25678 }
25679 else if (it->what == IT_GLYPHLESS)
25680 produce_glyphless_glyph (it, 0, Qnil);
25681 else if (it->what == IT_IMAGE)
25682 produce_image_glyph (it);
25683 else if (it->what == IT_STRETCH)
25684 produce_stretch_glyph (it);
25685
25686 done:
25687 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25688 because this isn't true for images with `:ascent 100'. */
25689 eassert (it->ascent >= 0 && it->descent >= 0);
25690 if (it->area == TEXT_AREA)
25691 it->current_x += it->pixel_width;
25692
25693 if (extra_line_spacing > 0)
25694 {
25695 it->descent += extra_line_spacing;
25696 if (extra_line_spacing > it->max_extra_line_spacing)
25697 it->max_extra_line_spacing = extra_line_spacing;
25698 }
25699
25700 it->max_ascent = max (it->max_ascent, it->ascent);
25701 it->max_descent = max (it->max_descent, it->descent);
25702 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25703 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25704 }
25705
25706 /* EXPORT for RIF:
25707 Output LEN glyphs starting at START at the nominal cursor position.
25708 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25709 being updated, and UPDATED_AREA is the area of that row being updated. */
25710
25711 void
25712 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25713 struct glyph *start, enum glyph_row_area updated_area, int len)
25714 {
25715 int x, hpos, chpos = w->phys_cursor.hpos;
25716
25717 eassert (updated_row);
25718 /* When the window is hscrolled, cursor hpos can legitimately be out
25719 of bounds, but we draw the cursor at the corresponding window
25720 margin in that case. */
25721 if (!updated_row->reversed_p && chpos < 0)
25722 chpos = 0;
25723 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25724 chpos = updated_row->used[TEXT_AREA] - 1;
25725
25726 block_input ();
25727
25728 /* Write glyphs. */
25729
25730 hpos = start - updated_row->glyphs[updated_area];
25731 x = draw_glyphs (w, w->output_cursor.x,
25732 updated_row, updated_area,
25733 hpos, hpos + len,
25734 DRAW_NORMAL_TEXT, 0);
25735
25736 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25737 if (updated_area == TEXT_AREA
25738 && w->phys_cursor_on_p
25739 && w->phys_cursor.vpos == w->output_cursor.vpos
25740 && chpos >= hpos
25741 && chpos < hpos + len)
25742 w->phys_cursor_on_p = 0;
25743
25744 unblock_input ();
25745
25746 /* Advance the output cursor. */
25747 w->output_cursor.hpos += len;
25748 w->output_cursor.x = x;
25749 }
25750
25751
25752 /* EXPORT for RIF:
25753 Insert LEN glyphs from START at the nominal cursor position. */
25754
25755 void
25756 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25757 struct glyph *start, enum glyph_row_area updated_area, int len)
25758 {
25759 struct frame *f;
25760 int line_height, shift_by_width, shifted_region_width;
25761 struct glyph_row *row;
25762 struct glyph *glyph;
25763 int frame_x, frame_y;
25764 ptrdiff_t hpos;
25765
25766 eassert (updated_row);
25767 block_input ();
25768 f = XFRAME (WINDOW_FRAME (w));
25769
25770 /* Get the height of the line we are in. */
25771 row = updated_row;
25772 line_height = row->height;
25773
25774 /* Get the width of the glyphs to insert. */
25775 shift_by_width = 0;
25776 for (glyph = start; glyph < start + len; ++glyph)
25777 shift_by_width += glyph->pixel_width;
25778
25779 /* Get the width of the region to shift right. */
25780 shifted_region_width = (window_box_width (w, updated_area)
25781 - w->output_cursor.x
25782 - shift_by_width);
25783
25784 /* Shift right. */
25785 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25786 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25787
25788 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25789 line_height, shift_by_width);
25790
25791 /* Write the glyphs. */
25792 hpos = start - row->glyphs[updated_area];
25793 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25794 hpos, hpos + len,
25795 DRAW_NORMAL_TEXT, 0);
25796
25797 /* Advance the output cursor. */
25798 w->output_cursor.hpos += len;
25799 w->output_cursor.x += shift_by_width;
25800 unblock_input ();
25801 }
25802
25803
25804 /* EXPORT for RIF:
25805 Erase the current text line from the nominal cursor position
25806 (inclusive) to pixel column TO_X (exclusive). The idea is that
25807 everything from TO_X onward is already erased.
25808
25809 TO_X is a pixel position relative to UPDATED_AREA of currently
25810 updated window W. TO_X == -1 means clear to the end of this area. */
25811
25812 void
25813 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25814 enum glyph_row_area updated_area, int to_x)
25815 {
25816 struct frame *f;
25817 int max_x, min_y, max_y;
25818 int from_x, from_y, to_y;
25819
25820 eassert (updated_row);
25821 f = XFRAME (w->frame);
25822
25823 if (updated_row->full_width_p)
25824 max_x = WINDOW_TOTAL_WIDTH (w);
25825 else
25826 max_x = window_box_width (w, updated_area);
25827 max_y = window_text_bottom_y (w);
25828
25829 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25830 of window. For TO_X > 0, truncate to end of drawing area. */
25831 if (to_x == 0)
25832 return;
25833 else if (to_x < 0)
25834 to_x = max_x;
25835 else
25836 to_x = min (to_x, max_x);
25837
25838 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25839
25840 /* Notice if the cursor will be cleared by this operation. */
25841 if (!updated_row->full_width_p)
25842 notice_overwritten_cursor (w, updated_area,
25843 w->output_cursor.x, -1,
25844 updated_row->y,
25845 MATRIX_ROW_BOTTOM_Y (updated_row));
25846
25847 from_x = w->output_cursor.x;
25848
25849 /* Translate to frame coordinates. */
25850 if (updated_row->full_width_p)
25851 {
25852 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25853 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25854 }
25855 else
25856 {
25857 int area_left = window_box_left (w, updated_area);
25858 from_x += area_left;
25859 to_x += area_left;
25860 }
25861
25862 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25863 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25864 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25865
25866 /* Prevent inadvertently clearing to end of the X window. */
25867 if (to_x > from_x && to_y > from_y)
25868 {
25869 block_input ();
25870 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25871 to_x - from_x, to_y - from_y);
25872 unblock_input ();
25873 }
25874 }
25875
25876 #endif /* HAVE_WINDOW_SYSTEM */
25877
25878
25879 \f
25880 /***********************************************************************
25881 Cursor types
25882 ***********************************************************************/
25883
25884 /* Value is the internal representation of the specified cursor type
25885 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25886 of the bar cursor. */
25887
25888 static enum text_cursor_kinds
25889 get_specified_cursor_type (Lisp_Object arg, int *width)
25890 {
25891 enum text_cursor_kinds type;
25892
25893 if (NILP (arg))
25894 return NO_CURSOR;
25895
25896 if (EQ (arg, Qbox))
25897 return FILLED_BOX_CURSOR;
25898
25899 if (EQ (arg, Qhollow))
25900 return HOLLOW_BOX_CURSOR;
25901
25902 if (EQ (arg, Qbar))
25903 {
25904 *width = 2;
25905 return BAR_CURSOR;
25906 }
25907
25908 if (CONSP (arg)
25909 && EQ (XCAR (arg), Qbar)
25910 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25911 {
25912 *width = XINT (XCDR (arg));
25913 return BAR_CURSOR;
25914 }
25915
25916 if (EQ (arg, Qhbar))
25917 {
25918 *width = 2;
25919 return HBAR_CURSOR;
25920 }
25921
25922 if (CONSP (arg)
25923 && EQ (XCAR (arg), Qhbar)
25924 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25925 {
25926 *width = XINT (XCDR (arg));
25927 return HBAR_CURSOR;
25928 }
25929
25930 /* Treat anything unknown as "hollow box cursor".
25931 It was bad to signal an error; people have trouble fixing
25932 .Xdefaults with Emacs, when it has something bad in it. */
25933 type = HOLLOW_BOX_CURSOR;
25934
25935 return type;
25936 }
25937
25938 /* Set the default cursor types for specified frame. */
25939 void
25940 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25941 {
25942 int width = 1;
25943 Lisp_Object tem;
25944
25945 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25946 FRAME_CURSOR_WIDTH (f) = width;
25947
25948 /* By default, set up the blink-off state depending on the on-state. */
25949
25950 tem = Fassoc (arg, Vblink_cursor_alist);
25951 if (!NILP (tem))
25952 {
25953 FRAME_BLINK_OFF_CURSOR (f)
25954 = get_specified_cursor_type (XCDR (tem), &width);
25955 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25956 }
25957 else
25958 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25959
25960 /* Make sure the cursor gets redrawn. */
25961 f->cursor_type_changed = 1;
25962 }
25963
25964
25965 #ifdef HAVE_WINDOW_SYSTEM
25966
25967 /* Return the cursor we want to be displayed in window W. Return
25968 width of bar/hbar cursor through WIDTH arg. Return with
25969 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25970 (i.e. if the `system caret' should track this cursor).
25971
25972 In a mini-buffer window, we want the cursor only to appear if we
25973 are reading input from this window. For the selected window, we
25974 want the cursor type given by the frame parameter or buffer local
25975 setting of cursor-type. If explicitly marked off, draw no cursor.
25976 In all other cases, we want a hollow box cursor. */
25977
25978 static enum text_cursor_kinds
25979 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25980 int *active_cursor)
25981 {
25982 struct frame *f = XFRAME (w->frame);
25983 struct buffer *b = XBUFFER (w->contents);
25984 int cursor_type = DEFAULT_CURSOR;
25985 Lisp_Object alt_cursor;
25986 int non_selected = 0;
25987
25988 *active_cursor = 1;
25989
25990 /* Echo area */
25991 if (cursor_in_echo_area
25992 && FRAME_HAS_MINIBUF_P (f)
25993 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25994 {
25995 if (w == XWINDOW (echo_area_window))
25996 {
25997 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25998 {
25999 *width = FRAME_CURSOR_WIDTH (f);
26000 return FRAME_DESIRED_CURSOR (f);
26001 }
26002 else
26003 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26004 }
26005
26006 *active_cursor = 0;
26007 non_selected = 1;
26008 }
26009
26010 /* Detect a nonselected window or nonselected frame. */
26011 else if (w != XWINDOW (f->selected_window)
26012 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26013 {
26014 *active_cursor = 0;
26015
26016 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26017 return NO_CURSOR;
26018
26019 non_selected = 1;
26020 }
26021
26022 /* Never display a cursor in a window in which cursor-type is nil. */
26023 if (NILP (BVAR (b, cursor_type)))
26024 return NO_CURSOR;
26025
26026 /* Get the normal cursor type for this window. */
26027 if (EQ (BVAR (b, cursor_type), Qt))
26028 {
26029 cursor_type = FRAME_DESIRED_CURSOR (f);
26030 *width = FRAME_CURSOR_WIDTH (f);
26031 }
26032 else
26033 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26034
26035 /* Use cursor-in-non-selected-windows instead
26036 for non-selected window or frame. */
26037 if (non_selected)
26038 {
26039 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26040 if (!EQ (Qt, alt_cursor))
26041 return get_specified_cursor_type (alt_cursor, width);
26042 /* t means modify the normal cursor type. */
26043 if (cursor_type == FILLED_BOX_CURSOR)
26044 cursor_type = HOLLOW_BOX_CURSOR;
26045 else if (cursor_type == BAR_CURSOR && *width > 1)
26046 --*width;
26047 return cursor_type;
26048 }
26049
26050 /* Use normal cursor if not blinked off. */
26051 if (!w->cursor_off_p)
26052 {
26053 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26054 {
26055 if (cursor_type == FILLED_BOX_CURSOR)
26056 {
26057 /* Using a block cursor on large images can be very annoying.
26058 So use a hollow cursor for "large" images.
26059 If image is not transparent (no mask), also use hollow cursor. */
26060 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26061 if (img != NULL && IMAGEP (img->spec))
26062 {
26063 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26064 where N = size of default frame font size.
26065 This should cover most of the "tiny" icons people may use. */
26066 if (!img->mask
26067 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26068 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26069 cursor_type = HOLLOW_BOX_CURSOR;
26070 }
26071 }
26072 else if (cursor_type != NO_CURSOR)
26073 {
26074 /* Display current only supports BOX and HOLLOW cursors for images.
26075 So for now, unconditionally use a HOLLOW cursor when cursor is
26076 not a solid box cursor. */
26077 cursor_type = HOLLOW_BOX_CURSOR;
26078 }
26079 }
26080 return cursor_type;
26081 }
26082
26083 /* Cursor is blinked off, so determine how to "toggle" it. */
26084
26085 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26086 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26087 return get_specified_cursor_type (XCDR (alt_cursor), width);
26088
26089 /* Then see if frame has specified a specific blink off cursor type. */
26090 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26091 {
26092 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26093 return FRAME_BLINK_OFF_CURSOR (f);
26094 }
26095
26096 #if 0
26097 /* Some people liked having a permanently visible blinking cursor,
26098 while others had very strong opinions against it. So it was
26099 decided to remove it. KFS 2003-09-03 */
26100
26101 /* Finally perform built-in cursor blinking:
26102 filled box <-> hollow box
26103 wide [h]bar <-> narrow [h]bar
26104 narrow [h]bar <-> no cursor
26105 other type <-> no cursor */
26106
26107 if (cursor_type == FILLED_BOX_CURSOR)
26108 return HOLLOW_BOX_CURSOR;
26109
26110 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26111 {
26112 *width = 1;
26113 return cursor_type;
26114 }
26115 #endif
26116
26117 return NO_CURSOR;
26118 }
26119
26120
26121 /* Notice when the text cursor of window W has been completely
26122 overwritten by a drawing operation that outputs glyphs in AREA
26123 starting at X0 and ending at X1 in the line starting at Y0 and
26124 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26125 the rest of the line after X0 has been written. Y coordinates
26126 are window-relative. */
26127
26128 static void
26129 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26130 int x0, int x1, int y0, int y1)
26131 {
26132 int cx0, cx1, cy0, cy1;
26133 struct glyph_row *row;
26134
26135 if (!w->phys_cursor_on_p)
26136 return;
26137 if (area != TEXT_AREA)
26138 return;
26139
26140 if (w->phys_cursor.vpos < 0
26141 || w->phys_cursor.vpos >= w->current_matrix->nrows
26142 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26143 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26144 return;
26145
26146 if (row->cursor_in_fringe_p)
26147 {
26148 row->cursor_in_fringe_p = 0;
26149 draw_fringe_bitmap (w, row, row->reversed_p);
26150 w->phys_cursor_on_p = 0;
26151 return;
26152 }
26153
26154 cx0 = w->phys_cursor.x;
26155 cx1 = cx0 + w->phys_cursor_width;
26156 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26157 return;
26158
26159 /* The cursor image will be completely removed from the
26160 screen if the output area intersects the cursor area in
26161 y-direction. When we draw in [y0 y1[, and some part of
26162 the cursor is at y < y0, that part must have been drawn
26163 before. When scrolling, the cursor is erased before
26164 actually scrolling, so we don't come here. When not
26165 scrolling, the rows above the old cursor row must have
26166 changed, and in this case these rows must have written
26167 over the cursor image.
26168
26169 Likewise if part of the cursor is below y1, with the
26170 exception of the cursor being in the first blank row at
26171 the buffer and window end because update_text_area
26172 doesn't draw that row. (Except when it does, but
26173 that's handled in update_text_area.) */
26174
26175 cy0 = w->phys_cursor.y;
26176 cy1 = cy0 + w->phys_cursor_height;
26177 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26178 return;
26179
26180 w->phys_cursor_on_p = 0;
26181 }
26182
26183 #endif /* HAVE_WINDOW_SYSTEM */
26184
26185 \f
26186 /************************************************************************
26187 Mouse Face
26188 ************************************************************************/
26189
26190 #ifdef HAVE_WINDOW_SYSTEM
26191
26192 /* EXPORT for RIF:
26193 Fix the display of area AREA of overlapping row ROW in window W
26194 with respect to the overlapping part OVERLAPS. */
26195
26196 void
26197 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26198 enum glyph_row_area area, int overlaps)
26199 {
26200 int i, x;
26201
26202 block_input ();
26203
26204 x = 0;
26205 for (i = 0; i < row->used[area];)
26206 {
26207 if (row->glyphs[area][i].overlaps_vertically_p)
26208 {
26209 int start = i, start_x = x;
26210
26211 do
26212 {
26213 x += row->glyphs[area][i].pixel_width;
26214 ++i;
26215 }
26216 while (i < row->used[area]
26217 && row->glyphs[area][i].overlaps_vertically_p);
26218
26219 draw_glyphs (w, start_x, row, area,
26220 start, i,
26221 DRAW_NORMAL_TEXT, overlaps);
26222 }
26223 else
26224 {
26225 x += row->glyphs[area][i].pixel_width;
26226 ++i;
26227 }
26228 }
26229
26230 unblock_input ();
26231 }
26232
26233
26234 /* EXPORT:
26235 Draw the cursor glyph of window W in glyph row ROW. See the
26236 comment of draw_glyphs for the meaning of HL. */
26237
26238 void
26239 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26240 enum draw_glyphs_face hl)
26241 {
26242 /* If cursor hpos is out of bounds, don't draw garbage. This can
26243 happen in mini-buffer windows when switching between echo area
26244 glyphs and mini-buffer. */
26245 if ((row->reversed_p
26246 ? (w->phys_cursor.hpos >= 0)
26247 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26248 {
26249 int on_p = w->phys_cursor_on_p;
26250 int x1;
26251 int hpos = w->phys_cursor.hpos;
26252
26253 /* When the window is hscrolled, cursor hpos can legitimately be
26254 out of bounds, but we draw the cursor at the corresponding
26255 window margin in that case. */
26256 if (!row->reversed_p && hpos < 0)
26257 hpos = 0;
26258 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26259 hpos = row->used[TEXT_AREA] - 1;
26260
26261 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26262 hl, 0);
26263 w->phys_cursor_on_p = on_p;
26264
26265 if (hl == DRAW_CURSOR)
26266 w->phys_cursor_width = x1 - w->phys_cursor.x;
26267 /* When we erase the cursor, and ROW is overlapped by other
26268 rows, make sure that these overlapping parts of other rows
26269 are redrawn. */
26270 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26271 {
26272 w->phys_cursor_width = x1 - w->phys_cursor.x;
26273
26274 if (row > w->current_matrix->rows
26275 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26276 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26277 OVERLAPS_ERASED_CURSOR);
26278
26279 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26280 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26281 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26282 OVERLAPS_ERASED_CURSOR);
26283 }
26284 }
26285 }
26286
26287
26288 /* EXPORT:
26289 Erase the image of a cursor of window W from the screen. */
26290
26291 void
26292 erase_phys_cursor (struct window *w)
26293 {
26294 struct frame *f = XFRAME (w->frame);
26295 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26296 int hpos = w->phys_cursor.hpos;
26297 int vpos = w->phys_cursor.vpos;
26298 int mouse_face_here_p = 0;
26299 struct glyph_matrix *active_glyphs = w->current_matrix;
26300 struct glyph_row *cursor_row;
26301 struct glyph *cursor_glyph;
26302 enum draw_glyphs_face hl;
26303
26304 /* No cursor displayed or row invalidated => nothing to do on the
26305 screen. */
26306 if (w->phys_cursor_type == NO_CURSOR)
26307 goto mark_cursor_off;
26308
26309 /* VPOS >= active_glyphs->nrows means that window has been resized.
26310 Don't bother to erase the cursor. */
26311 if (vpos >= active_glyphs->nrows)
26312 goto mark_cursor_off;
26313
26314 /* If row containing cursor is marked invalid, there is nothing we
26315 can do. */
26316 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26317 if (!cursor_row->enabled_p)
26318 goto mark_cursor_off;
26319
26320 /* If line spacing is > 0, old cursor may only be partially visible in
26321 window after split-window. So adjust visible height. */
26322 cursor_row->visible_height = min (cursor_row->visible_height,
26323 window_text_bottom_y (w) - cursor_row->y);
26324
26325 /* If row is completely invisible, don't attempt to delete a cursor which
26326 isn't there. This can happen if cursor is at top of a window, and
26327 we switch to a buffer with a header line in that window. */
26328 if (cursor_row->visible_height <= 0)
26329 goto mark_cursor_off;
26330
26331 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26332 if (cursor_row->cursor_in_fringe_p)
26333 {
26334 cursor_row->cursor_in_fringe_p = 0;
26335 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26336 goto mark_cursor_off;
26337 }
26338
26339 /* This can happen when the new row is shorter than the old one.
26340 In this case, either draw_glyphs or clear_end_of_line
26341 should have cleared the cursor. Note that we wouldn't be
26342 able to erase the cursor in this case because we don't have a
26343 cursor glyph at hand. */
26344 if ((cursor_row->reversed_p
26345 ? (w->phys_cursor.hpos < 0)
26346 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26347 goto mark_cursor_off;
26348
26349 /* When the window is hscrolled, cursor hpos can legitimately be out
26350 of bounds, but we draw the cursor at the corresponding window
26351 margin in that case. */
26352 if (!cursor_row->reversed_p && hpos < 0)
26353 hpos = 0;
26354 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26355 hpos = cursor_row->used[TEXT_AREA] - 1;
26356
26357 /* If the cursor is in the mouse face area, redisplay that when
26358 we clear the cursor. */
26359 if (! NILP (hlinfo->mouse_face_window)
26360 && coords_in_mouse_face_p (w, hpos, vpos)
26361 /* Don't redraw the cursor's spot in mouse face if it is at the
26362 end of a line (on a newline). The cursor appears there, but
26363 mouse highlighting does not. */
26364 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26365 mouse_face_here_p = 1;
26366
26367 /* Maybe clear the display under the cursor. */
26368 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26369 {
26370 int x, y, left_x;
26371 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26372 int width;
26373
26374 cursor_glyph = get_phys_cursor_glyph (w);
26375 if (cursor_glyph == NULL)
26376 goto mark_cursor_off;
26377
26378 width = cursor_glyph->pixel_width;
26379 left_x = window_box_left_offset (w, TEXT_AREA);
26380 x = w->phys_cursor.x;
26381 if (x < left_x)
26382 width -= left_x - x;
26383 width = min (width, window_box_width (w, TEXT_AREA) - x);
26384 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26385 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26386
26387 if (width > 0)
26388 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26389 }
26390
26391 /* Erase the cursor by redrawing the character underneath it. */
26392 if (mouse_face_here_p)
26393 hl = DRAW_MOUSE_FACE;
26394 else
26395 hl = DRAW_NORMAL_TEXT;
26396 draw_phys_cursor_glyph (w, cursor_row, hl);
26397
26398 mark_cursor_off:
26399 w->phys_cursor_on_p = 0;
26400 w->phys_cursor_type = NO_CURSOR;
26401 }
26402
26403
26404 /* EXPORT:
26405 Display or clear cursor of window W. If ON is zero, clear the
26406 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26407 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26408
26409 void
26410 display_and_set_cursor (struct window *w, bool on,
26411 int hpos, int vpos, int x, int y)
26412 {
26413 struct frame *f = XFRAME (w->frame);
26414 int new_cursor_type;
26415 int new_cursor_width;
26416 int active_cursor;
26417 struct glyph_row *glyph_row;
26418 struct glyph *glyph;
26419
26420 /* This is pointless on invisible frames, and dangerous on garbaged
26421 windows and frames; in the latter case, the frame or window may
26422 be in the midst of changing its size, and x and y may be off the
26423 window. */
26424 if (! FRAME_VISIBLE_P (f)
26425 || FRAME_GARBAGED_P (f)
26426 || vpos >= w->current_matrix->nrows
26427 || hpos >= w->current_matrix->matrix_w)
26428 return;
26429
26430 /* If cursor is off and we want it off, return quickly. */
26431 if (!on && !w->phys_cursor_on_p)
26432 return;
26433
26434 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26435 /* If cursor row is not enabled, we don't really know where to
26436 display the cursor. */
26437 if (!glyph_row->enabled_p)
26438 {
26439 w->phys_cursor_on_p = 0;
26440 return;
26441 }
26442
26443 glyph = NULL;
26444 if (!glyph_row->exact_window_width_line_p
26445 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26446 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26447
26448 eassert (input_blocked_p ());
26449
26450 /* Set new_cursor_type to the cursor we want to be displayed. */
26451 new_cursor_type = get_window_cursor_type (w, glyph,
26452 &new_cursor_width, &active_cursor);
26453
26454 /* If cursor is currently being shown and we don't want it to be or
26455 it is in the wrong place, or the cursor type is not what we want,
26456 erase it. */
26457 if (w->phys_cursor_on_p
26458 && (!on
26459 || w->phys_cursor.x != x
26460 || w->phys_cursor.y != y
26461 || new_cursor_type != w->phys_cursor_type
26462 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26463 && new_cursor_width != w->phys_cursor_width)))
26464 erase_phys_cursor (w);
26465
26466 /* Don't check phys_cursor_on_p here because that flag is only set
26467 to zero in some cases where we know that the cursor has been
26468 completely erased, to avoid the extra work of erasing the cursor
26469 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26470 still not be visible, or it has only been partly erased. */
26471 if (on)
26472 {
26473 w->phys_cursor_ascent = glyph_row->ascent;
26474 w->phys_cursor_height = glyph_row->height;
26475
26476 /* Set phys_cursor_.* before x_draw_.* is called because some
26477 of them may need the information. */
26478 w->phys_cursor.x = x;
26479 w->phys_cursor.y = glyph_row->y;
26480 w->phys_cursor.hpos = hpos;
26481 w->phys_cursor.vpos = vpos;
26482 }
26483
26484 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26485 new_cursor_type, new_cursor_width,
26486 on, active_cursor);
26487 }
26488
26489
26490 /* Switch the display of W's cursor on or off, according to the value
26491 of ON. */
26492
26493 static void
26494 update_window_cursor (struct window *w, bool on)
26495 {
26496 /* Don't update cursor in windows whose frame is in the process
26497 of being deleted. */
26498 if (w->current_matrix)
26499 {
26500 int hpos = w->phys_cursor.hpos;
26501 int vpos = w->phys_cursor.vpos;
26502 struct glyph_row *row;
26503
26504 if (vpos >= w->current_matrix->nrows
26505 || hpos >= w->current_matrix->matrix_w)
26506 return;
26507
26508 row = MATRIX_ROW (w->current_matrix, vpos);
26509
26510 /* When the window is hscrolled, cursor hpos can legitimately be
26511 out of bounds, but we draw the cursor at the corresponding
26512 window margin in that case. */
26513 if (!row->reversed_p && hpos < 0)
26514 hpos = 0;
26515 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26516 hpos = row->used[TEXT_AREA] - 1;
26517
26518 block_input ();
26519 display_and_set_cursor (w, on, hpos, vpos,
26520 w->phys_cursor.x, w->phys_cursor.y);
26521 unblock_input ();
26522 }
26523 }
26524
26525
26526 /* Call update_window_cursor with parameter ON_P on all leaf windows
26527 in the window tree rooted at W. */
26528
26529 static void
26530 update_cursor_in_window_tree (struct window *w, bool on_p)
26531 {
26532 while (w)
26533 {
26534 if (WINDOWP (w->contents))
26535 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26536 else
26537 update_window_cursor (w, on_p);
26538
26539 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26540 }
26541 }
26542
26543
26544 /* EXPORT:
26545 Display the cursor on window W, or clear it, according to ON_P.
26546 Don't change the cursor's position. */
26547
26548 void
26549 x_update_cursor (struct frame *f, bool on_p)
26550 {
26551 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26552 }
26553
26554
26555 /* EXPORT:
26556 Clear the cursor of window W to background color, and mark the
26557 cursor as not shown. This is used when the text where the cursor
26558 is about to be rewritten. */
26559
26560 void
26561 x_clear_cursor (struct window *w)
26562 {
26563 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26564 update_window_cursor (w, 0);
26565 }
26566
26567 #endif /* HAVE_WINDOW_SYSTEM */
26568
26569 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26570 and MSDOS. */
26571 static void
26572 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26573 int start_hpos, int end_hpos,
26574 enum draw_glyphs_face draw)
26575 {
26576 #ifdef HAVE_WINDOW_SYSTEM
26577 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26578 {
26579 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26580 return;
26581 }
26582 #endif
26583 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26584 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26585 #endif
26586 }
26587
26588 /* Display the active region described by mouse_face_* according to DRAW. */
26589
26590 static void
26591 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26592 {
26593 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26594 struct frame *f = XFRAME (WINDOW_FRAME (w));
26595
26596 if (/* If window is in the process of being destroyed, don't bother
26597 to do anything. */
26598 w->current_matrix != NULL
26599 /* Don't update mouse highlight if hidden */
26600 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26601 /* Recognize when we are called to operate on rows that don't exist
26602 anymore. This can happen when a window is split. */
26603 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26604 {
26605 int phys_cursor_on_p = w->phys_cursor_on_p;
26606 struct glyph_row *row, *first, *last;
26607
26608 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26609 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26610
26611 for (row = first; row <= last && row->enabled_p; ++row)
26612 {
26613 int start_hpos, end_hpos, start_x;
26614
26615 /* For all but the first row, the highlight starts at column 0. */
26616 if (row == first)
26617 {
26618 /* R2L rows have BEG and END in reversed order, but the
26619 screen drawing geometry is always left to right. So
26620 we need to mirror the beginning and end of the
26621 highlighted area in R2L rows. */
26622 if (!row->reversed_p)
26623 {
26624 start_hpos = hlinfo->mouse_face_beg_col;
26625 start_x = hlinfo->mouse_face_beg_x;
26626 }
26627 else if (row == last)
26628 {
26629 start_hpos = hlinfo->mouse_face_end_col;
26630 start_x = hlinfo->mouse_face_end_x;
26631 }
26632 else
26633 {
26634 start_hpos = 0;
26635 start_x = 0;
26636 }
26637 }
26638 else if (row->reversed_p && row == last)
26639 {
26640 start_hpos = hlinfo->mouse_face_end_col;
26641 start_x = hlinfo->mouse_face_end_x;
26642 }
26643 else
26644 {
26645 start_hpos = 0;
26646 start_x = 0;
26647 }
26648
26649 if (row == last)
26650 {
26651 if (!row->reversed_p)
26652 end_hpos = hlinfo->mouse_face_end_col;
26653 else if (row == first)
26654 end_hpos = hlinfo->mouse_face_beg_col;
26655 else
26656 {
26657 end_hpos = row->used[TEXT_AREA];
26658 if (draw == DRAW_NORMAL_TEXT)
26659 row->fill_line_p = 1; /* Clear to end of line */
26660 }
26661 }
26662 else if (row->reversed_p && row == first)
26663 end_hpos = hlinfo->mouse_face_beg_col;
26664 else
26665 {
26666 end_hpos = row->used[TEXT_AREA];
26667 if (draw == DRAW_NORMAL_TEXT)
26668 row->fill_line_p = 1; /* Clear to end of line */
26669 }
26670
26671 if (end_hpos > start_hpos)
26672 {
26673 draw_row_with_mouse_face (w, start_x, row,
26674 start_hpos, end_hpos, draw);
26675
26676 row->mouse_face_p
26677 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26678 }
26679 }
26680
26681 #ifdef HAVE_WINDOW_SYSTEM
26682 /* When we've written over the cursor, arrange for it to
26683 be displayed again. */
26684 if (FRAME_WINDOW_P (f)
26685 && phys_cursor_on_p && !w->phys_cursor_on_p)
26686 {
26687 int hpos = w->phys_cursor.hpos;
26688
26689 /* When the window is hscrolled, cursor hpos can legitimately be
26690 out of bounds, but we draw the cursor at the corresponding
26691 window margin in that case. */
26692 if (!row->reversed_p && hpos < 0)
26693 hpos = 0;
26694 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26695 hpos = row->used[TEXT_AREA] - 1;
26696
26697 block_input ();
26698 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26699 w->phys_cursor.x, w->phys_cursor.y);
26700 unblock_input ();
26701 }
26702 #endif /* HAVE_WINDOW_SYSTEM */
26703 }
26704
26705 #ifdef HAVE_WINDOW_SYSTEM
26706 /* Change the mouse cursor. */
26707 if (FRAME_WINDOW_P (f))
26708 {
26709 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26710 if (draw == DRAW_NORMAL_TEXT
26711 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26712 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26713 else
26714 #endif
26715 if (draw == DRAW_MOUSE_FACE)
26716 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26717 else
26718 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26719 }
26720 #endif /* HAVE_WINDOW_SYSTEM */
26721 }
26722
26723 /* EXPORT:
26724 Clear out the mouse-highlighted active region.
26725 Redraw it un-highlighted first. Value is non-zero if mouse
26726 face was actually drawn unhighlighted. */
26727
26728 int
26729 clear_mouse_face (Mouse_HLInfo *hlinfo)
26730 {
26731 int cleared = 0;
26732
26733 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26734 {
26735 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26736 cleared = 1;
26737 }
26738
26739 reset_mouse_highlight (hlinfo);
26740 return cleared;
26741 }
26742
26743 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26744 within the mouse face on that window. */
26745 static int
26746 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26747 {
26748 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26749
26750 /* Quickly resolve the easy cases. */
26751 if (!(WINDOWP (hlinfo->mouse_face_window)
26752 && XWINDOW (hlinfo->mouse_face_window) == w))
26753 return 0;
26754 if (vpos < hlinfo->mouse_face_beg_row
26755 || vpos > hlinfo->mouse_face_end_row)
26756 return 0;
26757 if (vpos > hlinfo->mouse_face_beg_row
26758 && vpos < hlinfo->mouse_face_end_row)
26759 return 1;
26760
26761 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26762 {
26763 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26764 {
26765 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26766 return 1;
26767 }
26768 else if ((vpos == hlinfo->mouse_face_beg_row
26769 && hpos >= hlinfo->mouse_face_beg_col)
26770 || (vpos == hlinfo->mouse_face_end_row
26771 && hpos < hlinfo->mouse_face_end_col))
26772 return 1;
26773 }
26774 else
26775 {
26776 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26777 {
26778 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26779 return 1;
26780 }
26781 else if ((vpos == hlinfo->mouse_face_beg_row
26782 && hpos <= hlinfo->mouse_face_beg_col)
26783 || (vpos == hlinfo->mouse_face_end_row
26784 && hpos > hlinfo->mouse_face_end_col))
26785 return 1;
26786 }
26787 return 0;
26788 }
26789
26790
26791 /* EXPORT:
26792 Non-zero if physical cursor of window W is within mouse face. */
26793
26794 int
26795 cursor_in_mouse_face_p (struct window *w)
26796 {
26797 int hpos = w->phys_cursor.hpos;
26798 int vpos = w->phys_cursor.vpos;
26799 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26800
26801 /* When the window is hscrolled, cursor hpos can legitimately be out
26802 of bounds, but we draw the cursor at the corresponding window
26803 margin in that case. */
26804 if (!row->reversed_p && hpos < 0)
26805 hpos = 0;
26806 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26807 hpos = row->used[TEXT_AREA] - 1;
26808
26809 return coords_in_mouse_face_p (w, hpos, vpos);
26810 }
26811
26812
26813 \f
26814 /* Find the glyph rows START_ROW and END_ROW of window W that display
26815 characters between buffer positions START_CHARPOS and END_CHARPOS
26816 (excluding END_CHARPOS). DISP_STRING is a display string that
26817 covers these buffer positions. This is similar to
26818 row_containing_pos, but is more accurate when bidi reordering makes
26819 buffer positions change non-linearly with glyph rows. */
26820 static void
26821 rows_from_pos_range (struct window *w,
26822 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26823 Lisp_Object disp_string,
26824 struct glyph_row **start, struct glyph_row **end)
26825 {
26826 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26827 int last_y = window_text_bottom_y (w);
26828 struct glyph_row *row;
26829
26830 *start = NULL;
26831 *end = NULL;
26832
26833 while (!first->enabled_p
26834 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26835 first++;
26836
26837 /* Find the START row. */
26838 for (row = first;
26839 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26840 row++)
26841 {
26842 /* A row can potentially be the START row if the range of the
26843 characters it displays intersects the range
26844 [START_CHARPOS..END_CHARPOS). */
26845 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26846 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26847 /* See the commentary in row_containing_pos, for the
26848 explanation of the complicated way to check whether
26849 some position is beyond the end of the characters
26850 displayed by a row. */
26851 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26852 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26853 && !row->ends_at_zv_p
26854 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26855 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26856 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26857 && !row->ends_at_zv_p
26858 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26859 {
26860 /* Found a candidate row. Now make sure at least one of the
26861 glyphs it displays has a charpos from the range
26862 [START_CHARPOS..END_CHARPOS).
26863
26864 This is not obvious because bidi reordering could make
26865 buffer positions of a row be 1,2,3,102,101,100, and if we
26866 want to highlight characters in [50..60), we don't want
26867 this row, even though [50..60) does intersect [1..103),
26868 the range of character positions given by the row's start
26869 and end positions. */
26870 struct glyph *g = row->glyphs[TEXT_AREA];
26871 struct glyph *e = g + row->used[TEXT_AREA];
26872
26873 while (g < e)
26874 {
26875 if (((BUFFERP (g->object) || INTEGERP (g->object))
26876 && start_charpos <= g->charpos && g->charpos < end_charpos)
26877 /* A glyph that comes from DISP_STRING is by
26878 definition to be highlighted. */
26879 || EQ (g->object, disp_string))
26880 *start = row;
26881 g++;
26882 }
26883 if (*start)
26884 break;
26885 }
26886 }
26887
26888 /* Find the END row. */
26889 if (!*start
26890 /* If the last row is partially visible, start looking for END
26891 from that row, instead of starting from FIRST. */
26892 && !(row->enabled_p
26893 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26894 row = first;
26895 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26896 {
26897 struct glyph_row *next = row + 1;
26898 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26899
26900 if (!next->enabled_p
26901 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26902 /* The first row >= START whose range of displayed characters
26903 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26904 is the row END + 1. */
26905 || (start_charpos < next_start
26906 && end_charpos < next_start)
26907 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26908 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26909 && !next->ends_at_zv_p
26910 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26911 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26912 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26913 && !next->ends_at_zv_p
26914 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26915 {
26916 *end = row;
26917 break;
26918 }
26919 else
26920 {
26921 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26922 but none of the characters it displays are in the range, it is
26923 also END + 1. */
26924 struct glyph *g = next->glyphs[TEXT_AREA];
26925 struct glyph *s = g;
26926 struct glyph *e = g + next->used[TEXT_AREA];
26927
26928 while (g < e)
26929 {
26930 if (((BUFFERP (g->object) || INTEGERP (g->object))
26931 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26932 /* If the buffer position of the first glyph in
26933 the row is equal to END_CHARPOS, it means
26934 the last character to be highlighted is the
26935 newline of ROW, and we must consider NEXT as
26936 END, not END+1. */
26937 || (((!next->reversed_p && g == s)
26938 || (next->reversed_p && g == e - 1))
26939 && (g->charpos == end_charpos
26940 /* Special case for when NEXT is an
26941 empty line at ZV. */
26942 || (g->charpos == -1
26943 && !row->ends_at_zv_p
26944 && next_start == end_charpos)))))
26945 /* A glyph that comes from DISP_STRING is by
26946 definition to be highlighted. */
26947 || EQ (g->object, disp_string))
26948 break;
26949 g++;
26950 }
26951 if (g == e)
26952 {
26953 *end = row;
26954 break;
26955 }
26956 /* The first row that ends at ZV must be the last to be
26957 highlighted. */
26958 else if (next->ends_at_zv_p)
26959 {
26960 *end = next;
26961 break;
26962 }
26963 }
26964 }
26965 }
26966
26967 /* This function sets the mouse_face_* elements of HLINFO, assuming
26968 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26969 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26970 for the overlay or run of text properties specifying the mouse
26971 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26972 before-string and after-string that must also be highlighted.
26973 DISP_STRING, if non-nil, is a display string that may cover some
26974 or all of the highlighted text. */
26975
26976 static void
26977 mouse_face_from_buffer_pos (Lisp_Object window,
26978 Mouse_HLInfo *hlinfo,
26979 ptrdiff_t mouse_charpos,
26980 ptrdiff_t start_charpos,
26981 ptrdiff_t end_charpos,
26982 Lisp_Object before_string,
26983 Lisp_Object after_string,
26984 Lisp_Object disp_string)
26985 {
26986 struct window *w = XWINDOW (window);
26987 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26988 struct glyph_row *r1, *r2;
26989 struct glyph *glyph, *end;
26990 ptrdiff_t ignore, pos;
26991 int x;
26992
26993 eassert (NILP (disp_string) || STRINGP (disp_string));
26994 eassert (NILP (before_string) || STRINGP (before_string));
26995 eassert (NILP (after_string) || STRINGP (after_string));
26996
26997 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26998 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26999 if (r1 == NULL)
27000 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27001 /* If the before-string or display-string contains newlines,
27002 rows_from_pos_range skips to its last row. Move back. */
27003 if (!NILP (before_string) || !NILP (disp_string))
27004 {
27005 struct glyph_row *prev;
27006 while ((prev = r1 - 1, prev >= first)
27007 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27008 && prev->used[TEXT_AREA] > 0)
27009 {
27010 struct glyph *beg = prev->glyphs[TEXT_AREA];
27011 glyph = beg + prev->used[TEXT_AREA];
27012 while (--glyph >= beg && INTEGERP (glyph->object));
27013 if (glyph < beg
27014 || !(EQ (glyph->object, before_string)
27015 || EQ (glyph->object, disp_string)))
27016 break;
27017 r1 = prev;
27018 }
27019 }
27020 if (r2 == NULL)
27021 {
27022 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27023 hlinfo->mouse_face_past_end = 1;
27024 }
27025 else if (!NILP (after_string))
27026 {
27027 /* If the after-string has newlines, advance to its last row. */
27028 struct glyph_row *next;
27029 struct glyph_row *last
27030 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27031
27032 for (next = r2 + 1;
27033 next <= last
27034 && next->used[TEXT_AREA] > 0
27035 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27036 ++next)
27037 r2 = next;
27038 }
27039 /* The rest of the display engine assumes that mouse_face_beg_row is
27040 either above mouse_face_end_row or identical to it. But with
27041 bidi-reordered continued lines, the row for START_CHARPOS could
27042 be below the row for END_CHARPOS. If so, swap the rows and store
27043 them in correct order. */
27044 if (r1->y > r2->y)
27045 {
27046 struct glyph_row *tem = r2;
27047
27048 r2 = r1;
27049 r1 = tem;
27050 }
27051
27052 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27053 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27054
27055 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27056 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27057 could be anywhere in the row and in any order. The strategy
27058 below is to find the leftmost and the rightmost glyph that
27059 belongs to either of these 3 strings, or whose position is
27060 between START_CHARPOS and END_CHARPOS, and highlight all the
27061 glyphs between those two. This may cover more than just the text
27062 between START_CHARPOS and END_CHARPOS if the range of characters
27063 strides the bidi level boundary, e.g. if the beginning is in R2L
27064 text while the end is in L2R text or vice versa. */
27065 if (!r1->reversed_p)
27066 {
27067 /* This row is in a left to right paragraph. Scan it left to
27068 right. */
27069 glyph = r1->glyphs[TEXT_AREA];
27070 end = glyph + r1->used[TEXT_AREA];
27071 x = r1->x;
27072
27073 /* Skip truncation glyphs at the start of the glyph row. */
27074 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27075 for (; glyph < end
27076 && INTEGERP (glyph->object)
27077 && glyph->charpos < 0;
27078 ++glyph)
27079 x += glyph->pixel_width;
27080
27081 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27082 or DISP_STRING, and the first glyph from buffer whose
27083 position is between START_CHARPOS and END_CHARPOS. */
27084 for (; glyph < end
27085 && !INTEGERP (glyph->object)
27086 && !EQ (glyph->object, disp_string)
27087 && !(BUFFERP (glyph->object)
27088 && (glyph->charpos >= start_charpos
27089 && glyph->charpos < end_charpos));
27090 ++glyph)
27091 {
27092 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27093 are present at buffer positions between START_CHARPOS and
27094 END_CHARPOS, or if they come from an overlay. */
27095 if (EQ (glyph->object, before_string))
27096 {
27097 pos = string_buffer_position (before_string,
27098 start_charpos);
27099 /* If pos == 0, it means before_string came from an
27100 overlay, not from a buffer position. */
27101 if (!pos || (pos >= start_charpos && pos < end_charpos))
27102 break;
27103 }
27104 else if (EQ (glyph->object, after_string))
27105 {
27106 pos = string_buffer_position (after_string, end_charpos);
27107 if (!pos || (pos >= start_charpos && pos < end_charpos))
27108 break;
27109 }
27110 x += glyph->pixel_width;
27111 }
27112 hlinfo->mouse_face_beg_x = x;
27113 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27114 }
27115 else
27116 {
27117 /* This row is in a right to left paragraph. Scan it right to
27118 left. */
27119 struct glyph *g;
27120
27121 end = r1->glyphs[TEXT_AREA] - 1;
27122 glyph = end + r1->used[TEXT_AREA];
27123
27124 /* Skip truncation glyphs at the start of the glyph row. */
27125 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27126 for (; glyph > end
27127 && INTEGERP (glyph->object)
27128 && glyph->charpos < 0;
27129 --glyph)
27130 ;
27131
27132 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27133 or DISP_STRING, and the first glyph from buffer whose
27134 position is between START_CHARPOS and END_CHARPOS. */
27135 for (; glyph > end
27136 && !INTEGERP (glyph->object)
27137 && !EQ (glyph->object, disp_string)
27138 && !(BUFFERP (glyph->object)
27139 && (glyph->charpos >= start_charpos
27140 && glyph->charpos < end_charpos));
27141 --glyph)
27142 {
27143 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27144 are present at buffer positions between START_CHARPOS and
27145 END_CHARPOS, or if they come from an overlay. */
27146 if (EQ (glyph->object, before_string))
27147 {
27148 pos = string_buffer_position (before_string, start_charpos);
27149 /* If pos == 0, it means before_string came from an
27150 overlay, not from a buffer position. */
27151 if (!pos || (pos >= start_charpos && pos < end_charpos))
27152 break;
27153 }
27154 else if (EQ (glyph->object, after_string))
27155 {
27156 pos = string_buffer_position (after_string, end_charpos);
27157 if (!pos || (pos >= start_charpos && pos < end_charpos))
27158 break;
27159 }
27160 }
27161
27162 glyph++; /* first glyph to the right of the highlighted area */
27163 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27164 x += g->pixel_width;
27165 hlinfo->mouse_face_beg_x = x;
27166 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27167 }
27168
27169 /* If the highlight ends in a different row, compute GLYPH and END
27170 for the end row. Otherwise, reuse the values computed above for
27171 the row where the highlight begins. */
27172 if (r2 != r1)
27173 {
27174 if (!r2->reversed_p)
27175 {
27176 glyph = r2->glyphs[TEXT_AREA];
27177 end = glyph + r2->used[TEXT_AREA];
27178 x = r2->x;
27179 }
27180 else
27181 {
27182 end = r2->glyphs[TEXT_AREA] - 1;
27183 glyph = end + r2->used[TEXT_AREA];
27184 }
27185 }
27186
27187 if (!r2->reversed_p)
27188 {
27189 /* Skip truncation and continuation glyphs near the end of the
27190 row, and also blanks and stretch glyphs inserted by
27191 extend_face_to_end_of_line. */
27192 while (end > glyph
27193 && INTEGERP ((end - 1)->object))
27194 --end;
27195 /* Scan the rest of the glyph row from the end, looking for the
27196 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27197 DISP_STRING, or whose position is between START_CHARPOS
27198 and END_CHARPOS */
27199 for (--end;
27200 end > glyph
27201 && !INTEGERP (end->object)
27202 && !EQ (end->object, disp_string)
27203 && !(BUFFERP (end->object)
27204 && (end->charpos >= start_charpos
27205 && end->charpos < end_charpos));
27206 --end)
27207 {
27208 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27209 are present at buffer positions between START_CHARPOS and
27210 END_CHARPOS, or if they come from an overlay. */
27211 if (EQ (end->object, before_string))
27212 {
27213 pos = string_buffer_position (before_string, start_charpos);
27214 if (!pos || (pos >= start_charpos && pos < end_charpos))
27215 break;
27216 }
27217 else if (EQ (end->object, after_string))
27218 {
27219 pos = string_buffer_position (after_string, end_charpos);
27220 if (!pos || (pos >= start_charpos && pos < end_charpos))
27221 break;
27222 }
27223 }
27224 /* Find the X coordinate of the last glyph to be highlighted. */
27225 for (; glyph <= end; ++glyph)
27226 x += glyph->pixel_width;
27227
27228 hlinfo->mouse_face_end_x = x;
27229 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27230 }
27231 else
27232 {
27233 /* Skip truncation and continuation glyphs near the end of the
27234 row, and also blanks and stretch glyphs inserted by
27235 extend_face_to_end_of_line. */
27236 x = r2->x;
27237 end++;
27238 while (end < glyph
27239 && INTEGERP (end->object))
27240 {
27241 x += end->pixel_width;
27242 ++end;
27243 }
27244 /* Scan the rest of the glyph row from the end, looking for the
27245 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27246 DISP_STRING, or whose position is between START_CHARPOS
27247 and END_CHARPOS */
27248 for ( ;
27249 end < glyph
27250 && !INTEGERP (end->object)
27251 && !EQ (end->object, disp_string)
27252 && !(BUFFERP (end->object)
27253 && (end->charpos >= start_charpos
27254 && end->charpos < end_charpos));
27255 ++end)
27256 {
27257 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27258 are present at buffer positions between START_CHARPOS and
27259 END_CHARPOS, or if they come from an overlay. */
27260 if (EQ (end->object, before_string))
27261 {
27262 pos = string_buffer_position (before_string, start_charpos);
27263 if (!pos || (pos >= start_charpos && pos < end_charpos))
27264 break;
27265 }
27266 else if (EQ (end->object, after_string))
27267 {
27268 pos = string_buffer_position (after_string, end_charpos);
27269 if (!pos || (pos >= start_charpos && pos < end_charpos))
27270 break;
27271 }
27272 x += end->pixel_width;
27273 }
27274 /* If we exited the above loop because we arrived at the last
27275 glyph of the row, and its buffer position is still not in
27276 range, it means the last character in range is the preceding
27277 newline. Bump the end column and x values to get past the
27278 last glyph. */
27279 if (end == glyph
27280 && BUFFERP (end->object)
27281 && (end->charpos < start_charpos
27282 || end->charpos >= end_charpos))
27283 {
27284 x += end->pixel_width;
27285 ++end;
27286 }
27287 hlinfo->mouse_face_end_x = x;
27288 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27289 }
27290
27291 hlinfo->mouse_face_window = window;
27292 hlinfo->mouse_face_face_id
27293 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27294 mouse_charpos + 1,
27295 !hlinfo->mouse_face_hidden, -1);
27296 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27297 }
27298
27299 /* The following function is not used anymore (replaced with
27300 mouse_face_from_string_pos), but I leave it here for the time
27301 being, in case someone would. */
27302
27303 #if 0 /* not used */
27304
27305 /* Find the position of the glyph for position POS in OBJECT in
27306 window W's current matrix, and return in *X, *Y the pixel
27307 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27308
27309 RIGHT_P non-zero means return the position of the right edge of the
27310 glyph, RIGHT_P zero means return the left edge position.
27311
27312 If no glyph for POS exists in the matrix, return the position of
27313 the glyph with the next smaller position that is in the matrix, if
27314 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27315 exists in the matrix, return the position of the glyph with the
27316 next larger position in OBJECT.
27317
27318 Value is non-zero if a glyph was found. */
27319
27320 static int
27321 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27322 int *hpos, int *vpos, int *x, int *y, int right_p)
27323 {
27324 int yb = window_text_bottom_y (w);
27325 struct glyph_row *r;
27326 struct glyph *best_glyph = NULL;
27327 struct glyph_row *best_row = NULL;
27328 int best_x = 0;
27329
27330 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27331 r->enabled_p && r->y < yb;
27332 ++r)
27333 {
27334 struct glyph *g = r->glyphs[TEXT_AREA];
27335 struct glyph *e = g + r->used[TEXT_AREA];
27336 int gx;
27337
27338 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27339 if (EQ (g->object, object))
27340 {
27341 if (g->charpos == pos)
27342 {
27343 best_glyph = g;
27344 best_x = gx;
27345 best_row = r;
27346 goto found;
27347 }
27348 else if (best_glyph == NULL
27349 || ((eabs (g->charpos - pos)
27350 < eabs (best_glyph->charpos - pos))
27351 && (right_p
27352 ? g->charpos < pos
27353 : g->charpos > pos)))
27354 {
27355 best_glyph = g;
27356 best_x = gx;
27357 best_row = r;
27358 }
27359 }
27360 }
27361
27362 found:
27363
27364 if (best_glyph)
27365 {
27366 *x = best_x;
27367 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27368
27369 if (right_p)
27370 {
27371 *x += best_glyph->pixel_width;
27372 ++*hpos;
27373 }
27374
27375 *y = best_row->y;
27376 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27377 }
27378
27379 return best_glyph != NULL;
27380 }
27381 #endif /* not used */
27382
27383 /* Find the positions of the first and the last glyphs in window W's
27384 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27385 (assumed to be a string), and return in HLINFO's mouse_face_*
27386 members the pixel and column/row coordinates of those glyphs. */
27387
27388 static void
27389 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27390 Lisp_Object object,
27391 ptrdiff_t startpos, ptrdiff_t endpos)
27392 {
27393 int yb = window_text_bottom_y (w);
27394 struct glyph_row *r;
27395 struct glyph *g, *e;
27396 int gx;
27397 int found = 0;
27398
27399 /* Find the glyph row with at least one position in the range
27400 [STARTPOS..ENDPOS), and the first glyph in that row whose
27401 position belongs to that range. */
27402 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27403 r->enabled_p && r->y < yb;
27404 ++r)
27405 {
27406 if (!r->reversed_p)
27407 {
27408 g = r->glyphs[TEXT_AREA];
27409 e = g + r->used[TEXT_AREA];
27410 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27411 if (EQ (g->object, object)
27412 && startpos <= g->charpos && g->charpos < endpos)
27413 {
27414 hlinfo->mouse_face_beg_row
27415 = MATRIX_ROW_VPOS (r, w->current_matrix);
27416 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27417 hlinfo->mouse_face_beg_x = gx;
27418 found = 1;
27419 break;
27420 }
27421 }
27422 else
27423 {
27424 struct glyph *g1;
27425
27426 e = r->glyphs[TEXT_AREA];
27427 g = e + r->used[TEXT_AREA];
27428 for ( ; g > e; --g)
27429 if (EQ ((g-1)->object, object)
27430 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27431 {
27432 hlinfo->mouse_face_beg_row
27433 = MATRIX_ROW_VPOS (r, w->current_matrix);
27434 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27435 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27436 gx += g1->pixel_width;
27437 hlinfo->mouse_face_beg_x = gx;
27438 found = 1;
27439 break;
27440 }
27441 }
27442 if (found)
27443 break;
27444 }
27445
27446 if (!found)
27447 return;
27448
27449 /* Starting with the next row, look for the first row which does NOT
27450 include any glyphs whose positions are in the range. */
27451 for (++r; r->enabled_p && r->y < yb; ++r)
27452 {
27453 g = r->glyphs[TEXT_AREA];
27454 e = g + r->used[TEXT_AREA];
27455 found = 0;
27456 for ( ; g < e; ++g)
27457 if (EQ (g->object, object)
27458 && startpos <= g->charpos && g->charpos < endpos)
27459 {
27460 found = 1;
27461 break;
27462 }
27463 if (!found)
27464 break;
27465 }
27466
27467 /* The highlighted region ends on the previous row. */
27468 r--;
27469
27470 /* Set the end row. */
27471 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27472
27473 /* Compute and set the end column and the end column's horizontal
27474 pixel coordinate. */
27475 if (!r->reversed_p)
27476 {
27477 g = r->glyphs[TEXT_AREA];
27478 e = g + r->used[TEXT_AREA];
27479 for ( ; e > g; --e)
27480 if (EQ ((e-1)->object, object)
27481 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27482 break;
27483 hlinfo->mouse_face_end_col = e - g;
27484
27485 for (gx = r->x; g < e; ++g)
27486 gx += g->pixel_width;
27487 hlinfo->mouse_face_end_x = gx;
27488 }
27489 else
27490 {
27491 e = r->glyphs[TEXT_AREA];
27492 g = e + r->used[TEXT_AREA];
27493 for (gx = r->x ; e < g; ++e)
27494 {
27495 if (EQ (e->object, object)
27496 && startpos <= e->charpos && e->charpos < endpos)
27497 break;
27498 gx += e->pixel_width;
27499 }
27500 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27501 hlinfo->mouse_face_end_x = gx;
27502 }
27503 }
27504
27505 #ifdef HAVE_WINDOW_SYSTEM
27506
27507 /* See if position X, Y is within a hot-spot of an image. */
27508
27509 static int
27510 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27511 {
27512 if (!CONSP (hot_spot))
27513 return 0;
27514
27515 if (EQ (XCAR (hot_spot), Qrect))
27516 {
27517 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27518 Lisp_Object rect = XCDR (hot_spot);
27519 Lisp_Object tem;
27520 if (!CONSP (rect))
27521 return 0;
27522 if (!CONSP (XCAR (rect)))
27523 return 0;
27524 if (!CONSP (XCDR (rect)))
27525 return 0;
27526 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27527 return 0;
27528 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27529 return 0;
27530 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27531 return 0;
27532 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27533 return 0;
27534 return 1;
27535 }
27536 else if (EQ (XCAR (hot_spot), Qcircle))
27537 {
27538 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27539 Lisp_Object circ = XCDR (hot_spot);
27540 Lisp_Object lr, lx0, ly0;
27541 if (CONSP (circ)
27542 && CONSP (XCAR (circ))
27543 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27544 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27545 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27546 {
27547 double r = XFLOATINT (lr);
27548 double dx = XINT (lx0) - x;
27549 double dy = XINT (ly0) - y;
27550 return (dx * dx + dy * dy <= r * r);
27551 }
27552 }
27553 else if (EQ (XCAR (hot_spot), Qpoly))
27554 {
27555 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27556 if (VECTORP (XCDR (hot_spot)))
27557 {
27558 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27559 Lisp_Object *poly = v->contents;
27560 ptrdiff_t n = v->header.size;
27561 ptrdiff_t i;
27562 int inside = 0;
27563 Lisp_Object lx, ly;
27564 int x0, y0;
27565
27566 /* Need an even number of coordinates, and at least 3 edges. */
27567 if (n < 6 || n & 1)
27568 return 0;
27569
27570 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27571 If count is odd, we are inside polygon. Pixels on edges
27572 may or may not be included depending on actual geometry of the
27573 polygon. */
27574 if ((lx = poly[n-2], !INTEGERP (lx))
27575 || (ly = poly[n-1], !INTEGERP (lx)))
27576 return 0;
27577 x0 = XINT (lx), y0 = XINT (ly);
27578 for (i = 0; i < n; i += 2)
27579 {
27580 int x1 = x0, y1 = y0;
27581 if ((lx = poly[i], !INTEGERP (lx))
27582 || (ly = poly[i+1], !INTEGERP (ly)))
27583 return 0;
27584 x0 = XINT (lx), y0 = XINT (ly);
27585
27586 /* Does this segment cross the X line? */
27587 if (x0 >= x)
27588 {
27589 if (x1 >= x)
27590 continue;
27591 }
27592 else if (x1 < x)
27593 continue;
27594 if (y > y0 && y > y1)
27595 continue;
27596 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27597 inside = !inside;
27598 }
27599 return inside;
27600 }
27601 }
27602 return 0;
27603 }
27604
27605 Lisp_Object
27606 find_hot_spot (Lisp_Object map, int x, int y)
27607 {
27608 while (CONSP (map))
27609 {
27610 if (CONSP (XCAR (map))
27611 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27612 return XCAR (map);
27613 map = XCDR (map);
27614 }
27615
27616 return Qnil;
27617 }
27618
27619 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27620 3, 3, 0,
27621 doc: /* Lookup in image map MAP coordinates X and Y.
27622 An image map is an alist where each element has the format (AREA ID PLIST).
27623 An AREA is specified as either a rectangle, a circle, or a polygon:
27624 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27625 pixel coordinates of the upper left and bottom right corners.
27626 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27627 and the radius of the circle; r may be a float or integer.
27628 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27629 vector describes one corner in the polygon.
27630 Returns the alist element for the first matching AREA in MAP. */)
27631 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27632 {
27633 if (NILP (map))
27634 return Qnil;
27635
27636 CHECK_NUMBER (x);
27637 CHECK_NUMBER (y);
27638
27639 return find_hot_spot (map,
27640 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27641 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27642 }
27643
27644
27645 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27646 static void
27647 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27648 {
27649 /* Do not change cursor shape while dragging mouse. */
27650 if (!NILP (do_mouse_tracking))
27651 return;
27652
27653 if (!NILP (pointer))
27654 {
27655 if (EQ (pointer, Qarrow))
27656 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27657 else if (EQ (pointer, Qhand))
27658 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27659 else if (EQ (pointer, Qtext))
27660 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27661 else if (EQ (pointer, intern ("hdrag")))
27662 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27663 #ifdef HAVE_X_WINDOWS
27664 else if (EQ (pointer, intern ("vdrag")))
27665 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27666 #endif
27667 else if (EQ (pointer, intern ("hourglass")))
27668 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27669 else if (EQ (pointer, Qmodeline))
27670 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27671 else
27672 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27673 }
27674
27675 if (cursor != No_Cursor)
27676 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27677 }
27678
27679 #endif /* HAVE_WINDOW_SYSTEM */
27680
27681 /* Take proper action when mouse has moved to the mode or header line
27682 or marginal area AREA of window W, x-position X and y-position Y.
27683 X is relative to the start of the text display area of W, so the
27684 width of bitmap areas and scroll bars must be subtracted to get a
27685 position relative to the start of the mode line. */
27686
27687 static void
27688 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27689 enum window_part area)
27690 {
27691 struct window *w = XWINDOW (window);
27692 struct frame *f = XFRAME (w->frame);
27693 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27694 #ifdef HAVE_WINDOW_SYSTEM
27695 Display_Info *dpyinfo;
27696 #endif
27697 Cursor cursor = No_Cursor;
27698 Lisp_Object pointer = Qnil;
27699 int dx, dy, width, height;
27700 ptrdiff_t charpos;
27701 Lisp_Object string, object = Qnil;
27702 Lisp_Object pos IF_LINT (= Qnil), help;
27703
27704 Lisp_Object mouse_face;
27705 int original_x_pixel = x;
27706 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27707 struct glyph_row *row IF_LINT (= 0);
27708
27709 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27710 {
27711 int x0;
27712 struct glyph *end;
27713
27714 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27715 returns them in row/column units! */
27716 string = mode_line_string (w, area, &x, &y, &charpos,
27717 &object, &dx, &dy, &width, &height);
27718
27719 row = (area == ON_MODE_LINE
27720 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27721 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27722
27723 /* Find the glyph under the mouse pointer. */
27724 if (row->mode_line_p && row->enabled_p)
27725 {
27726 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27727 end = glyph + row->used[TEXT_AREA];
27728
27729 for (x0 = original_x_pixel;
27730 glyph < end && x0 >= glyph->pixel_width;
27731 ++glyph)
27732 x0 -= glyph->pixel_width;
27733
27734 if (glyph >= end)
27735 glyph = NULL;
27736 }
27737 }
27738 else
27739 {
27740 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27741 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27742 returns them in row/column units! */
27743 string = marginal_area_string (w, area, &x, &y, &charpos,
27744 &object, &dx, &dy, &width, &height);
27745 }
27746
27747 help = Qnil;
27748
27749 #ifdef HAVE_WINDOW_SYSTEM
27750 if (IMAGEP (object))
27751 {
27752 Lisp_Object image_map, hotspot;
27753 if ((image_map = Fplist_get (XCDR (object), QCmap),
27754 !NILP (image_map))
27755 && (hotspot = find_hot_spot (image_map, dx, dy),
27756 CONSP (hotspot))
27757 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27758 {
27759 Lisp_Object plist;
27760
27761 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27762 If so, we could look for mouse-enter, mouse-leave
27763 properties in PLIST (and do something...). */
27764 hotspot = XCDR (hotspot);
27765 if (CONSP (hotspot)
27766 && (plist = XCAR (hotspot), CONSP (plist)))
27767 {
27768 pointer = Fplist_get (plist, Qpointer);
27769 if (NILP (pointer))
27770 pointer = Qhand;
27771 help = Fplist_get (plist, Qhelp_echo);
27772 if (!NILP (help))
27773 {
27774 help_echo_string = help;
27775 XSETWINDOW (help_echo_window, w);
27776 help_echo_object = w->contents;
27777 help_echo_pos = charpos;
27778 }
27779 }
27780 }
27781 if (NILP (pointer))
27782 pointer = Fplist_get (XCDR (object), QCpointer);
27783 }
27784 #endif /* HAVE_WINDOW_SYSTEM */
27785
27786 if (STRINGP (string))
27787 pos = make_number (charpos);
27788
27789 /* Set the help text and mouse pointer. If the mouse is on a part
27790 of the mode line without any text (e.g. past the right edge of
27791 the mode line text), use the default help text and pointer. */
27792 if (STRINGP (string) || area == ON_MODE_LINE)
27793 {
27794 /* Arrange to display the help by setting the global variables
27795 help_echo_string, help_echo_object, and help_echo_pos. */
27796 if (NILP (help))
27797 {
27798 if (STRINGP (string))
27799 help = Fget_text_property (pos, Qhelp_echo, string);
27800
27801 if (!NILP (help))
27802 {
27803 help_echo_string = help;
27804 XSETWINDOW (help_echo_window, w);
27805 help_echo_object = string;
27806 help_echo_pos = charpos;
27807 }
27808 else if (area == ON_MODE_LINE)
27809 {
27810 Lisp_Object default_help
27811 = buffer_local_value_1 (Qmode_line_default_help_echo,
27812 w->contents);
27813
27814 if (STRINGP (default_help))
27815 {
27816 help_echo_string = default_help;
27817 XSETWINDOW (help_echo_window, w);
27818 help_echo_object = Qnil;
27819 help_echo_pos = -1;
27820 }
27821 }
27822 }
27823
27824 #ifdef HAVE_WINDOW_SYSTEM
27825 /* Change the mouse pointer according to what is under it. */
27826 if (FRAME_WINDOW_P (f))
27827 {
27828 dpyinfo = FRAME_DISPLAY_INFO (f);
27829 if (STRINGP (string))
27830 {
27831 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27832
27833 if (NILP (pointer))
27834 pointer = Fget_text_property (pos, Qpointer, string);
27835
27836 /* Change the mouse pointer according to what is under X/Y. */
27837 if (NILP (pointer)
27838 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27839 {
27840 Lisp_Object map;
27841 map = Fget_text_property (pos, Qlocal_map, string);
27842 if (!KEYMAPP (map))
27843 map = Fget_text_property (pos, Qkeymap, string);
27844 if (!KEYMAPP (map))
27845 cursor = dpyinfo->vertical_scroll_bar_cursor;
27846 }
27847 }
27848 else
27849 /* Default mode-line pointer. */
27850 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27851 }
27852 #endif
27853 }
27854
27855 /* Change the mouse face according to what is under X/Y. */
27856 if (STRINGP (string))
27857 {
27858 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27859 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27860 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27861 && glyph)
27862 {
27863 Lisp_Object b, e;
27864
27865 struct glyph * tmp_glyph;
27866
27867 int gpos;
27868 int gseq_length;
27869 int total_pixel_width;
27870 ptrdiff_t begpos, endpos, ignore;
27871
27872 int vpos, hpos;
27873
27874 b = Fprevious_single_property_change (make_number (charpos + 1),
27875 Qmouse_face, string, Qnil);
27876 if (NILP (b))
27877 begpos = 0;
27878 else
27879 begpos = XINT (b);
27880
27881 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27882 if (NILP (e))
27883 endpos = SCHARS (string);
27884 else
27885 endpos = XINT (e);
27886
27887 /* Calculate the glyph position GPOS of GLYPH in the
27888 displayed string, relative to the beginning of the
27889 highlighted part of the string.
27890
27891 Note: GPOS is different from CHARPOS. CHARPOS is the
27892 position of GLYPH in the internal string object. A mode
27893 line string format has structures which are converted to
27894 a flattened string by the Emacs Lisp interpreter. The
27895 internal string is an element of those structures. The
27896 displayed string is the flattened string. */
27897 tmp_glyph = row_start_glyph;
27898 while (tmp_glyph < glyph
27899 && (!(EQ (tmp_glyph->object, glyph->object)
27900 && begpos <= tmp_glyph->charpos
27901 && tmp_glyph->charpos < endpos)))
27902 tmp_glyph++;
27903 gpos = glyph - tmp_glyph;
27904
27905 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27906 the highlighted part of the displayed string to which
27907 GLYPH belongs. Note: GSEQ_LENGTH is different from
27908 SCHARS (STRING), because the latter returns the length of
27909 the internal string. */
27910 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27911 tmp_glyph > glyph
27912 && (!(EQ (tmp_glyph->object, glyph->object)
27913 && begpos <= tmp_glyph->charpos
27914 && tmp_glyph->charpos < endpos));
27915 tmp_glyph--)
27916 ;
27917 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27918
27919 /* Calculate the total pixel width of all the glyphs between
27920 the beginning of the highlighted area and GLYPH. */
27921 total_pixel_width = 0;
27922 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27923 total_pixel_width += tmp_glyph->pixel_width;
27924
27925 /* Pre calculation of re-rendering position. Note: X is in
27926 column units here, after the call to mode_line_string or
27927 marginal_area_string. */
27928 hpos = x - gpos;
27929 vpos = (area == ON_MODE_LINE
27930 ? (w->current_matrix)->nrows - 1
27931 : 0);
27932
27933 /* If GLYPH's position is included in the region that is
27934 already drawn in mouse face, we have nothing to do. */
27935 if ( EQ (window, hlinfo->mouse_face_window)
27936 && (!row->reversed_p
27937 ? (hlinfo->mouse_face_beg_col <= hpos
27938 && hpos < hlinfo->mouse_face_end_col)
27939 /* In R2L rows we swap BEG and END, see below. */
27940 : (hlinfo->mouse_face_end_col <= hpos
27941 && hpos < hlinfo->mouse_face_beg_col))
27942 && hlinfo->mouse_face_beg_row == vpos )
27943 return;
27944
27945 if (clear_mouse_face (hlinfo))
27946 cursor = No_Cursor;
27947
27948 if (!row->reversed_p)
27949 {
27950 hlinfo->mouse_face_beg_col = hpos;
27951 hlinfo->mouse_face_beg_x = original_x_pixel
27952 - (total_pixel_width + dx);
27953 hlinfo->mouse_face_end_col = hpos + gseq_length;
27954 hlinfo->mouse_face_end_x = 0;
27955 }
27956 else
27957 {
27958 /* In R2L rows, show_mouse_face expects BEG and END
27959 coordinates to be swapped. */
27960 hlinfo->mouse_face_end_col = hpos;
27961 hlinfo->mouse_face_end_x = original_x_pixel
27962 - (total_pixel_width + dx);
27963 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27964 hlinfo->mouse_face_beg_x = 0;
27965 }
27966
27967 hlinfo->mouse_face_beg_row = vpos;
27968 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27969 hlinfo->mouse_face_past_end = 0;
27970 hlinfo->mouse_face_window = window;
27971
27972 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27973 charpos,
27974 0, 0, 0,
27975 &ignore,
27976 glyph->face_id,
27977 1);
27978 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27979
27980 if (NILP (pointer))
27981 pointer = Qhand;
27982 }
27983 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27984 clear_mouse_face (hlinfo);
27985 }
27986 #ifdef HAVE_WINDOW_SYSTEM
27987 if (FRAME_WINDOW_P (f))
27988 define_frame_cursor1 (f, cursor, pointer);
27989 #endif
27990 }
27991
27992
27993 /* EXPORT:
27994 Take proper action when the mouse has moved to position X, Y on
27995 frame F with regards to highlighting portions of display that have
27996 mouse-face properties. Also de-highlight portions of display where
27997 the mouse was before, set the mouse pointer shape as appropriate
27998 for the mouse coordinates, and activate help echo (tooltips).
27999 X and Y can be negative or out of range. */
28000
28001 void
28002 note_mouse_highlight (struct frame *f, int x, int y)
28003 {
28004 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28005 enum window_part part = ON_NOTHING;
28006 Lisp_Object window;
28007 struct window *w;
28008 Cursor cursor = No_Cursor;
28009 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28010 struct buffer *b;
28011
28012 /* When a menu is active, don't highlight because this looks odd. */
28013 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28014 if (popup_activated ())
28015 return;
28016 #endif
28017
28018 if (!f->glyphs_initialized_p
28019 || f->pointer_invisible)
28020 return;
28021
28022 hlinfo->mouse_face_mouse_x = x;
28023 hlinfo->mouse_face_mouse_y = y;
28024 hlinfo->mouse_face_mouse_frame = f;
28025
28026 if (hlinfo->mouse_face_defer)
28027 return;
28028
28029 /* Which window is that in? */
28030 window = window_from_coordinates (f, x, y, &part, 1);
28031
28032 /* If displaying active text in another window, clear that. */
28033 if (! EQ (window, hlinfo->mouse_face_window)
28034 /* Also clear if we move out of text area in same window. */
28035 || (!NILP (hlinfo->mouse_face_window)
28036 && !NILP (window)
28037 && part != ON_TEXT
28038 && part != ON_MODE_LINE
28039 && part != ON_HEADER_LINE))
28040 clear_mouse_face (hlinfo);
28041
28042 /* Not on a window -> return. */
28043 if (!WINDOWP (window))
28044 return;
28045
28046 /* Reset help_echo_string. It will get recomputed below. */
28047 help_echo_string = Qnil;
28048
28049 /* Convert to window-relative pixel coordinates. */
28050 w = XWINDOW (window);
28051 frame_to_window_pixel_xy (w, &x, &y);
28052
28053 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28054 /* Handle tool-bar window differently since it doesn't display a
28055 buffer. */
28056 if (EQ (window, f->tool_bar_window))
28057 {
28058 note_tool_bar_highlight (f, x, y);
28059 return;
28060 }
28061 #endif
28062
28063 /* Mouse is on the mode, header line or margin? */
28064 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28065 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28066 {
28067 note_mode_line_or_margin_highlight (window, x, y, part);
28068 return;
28069 }
28070
28071 #ifdef HAVE_WINDOW_SYSTEM
28072 if (part == ON_VERTICAL_BORDER)
28073 {
28074 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28075 help_echo_string = build_string ("drag-mouse-1: resize");
28076 }
28077 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28078 || part == ON_SCROLL_BAR)
28079 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28080 else
28081 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28082 #endif
28083
28084 /* Are we in a window whose display is up to date?
28085 And verify the buffer's text has not changed. */
28086 b = XBUFFER (w->contents);
28087 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28088 {
28089 int hpos, vpos, dx, dy, area = LAST_AREA;
28090 ptrdiff_t pos;
28091 struct glyph *glyph;
28092 Lisp_Object object;
28093 Lisp_Object mouse_face = Qnil, position;
28094 Lisp_Object *overlay_vec = NULL;
28095 ptrdiff_t i, noverlays;
28096 struct buffer *obuf;
28097 ptrdiff_t obegv, ozv;
28098 int same_region;
28099
28100 /* Find the glyph under X/Y. */
28101 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28102
28103 #ifdef HAVE_WINDOW_SYSTEM
28104 /* Look for :pointer property on image. */
28105 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28106 {
28107 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28108 if (img != NULL && IMAGEP (img->spec))
28109 {
28110 Lisp_Object image_map, hotspot;
28111 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28112 !NILP (image_map))
28113 && (hotspot = find_hot_spot (image_map,
28114 glyph->slice.img.x + dx,
28115 glyph->slice.img.y + dy),
28116 CONSP (hotspot))
28117 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28118 {
28119 Lisp_Object plist;
28120
28121 /* Could check XCAR (hotspot) to see if we enter/leave
28122 this hot-spot.
28123 If so, we could look for mouse-enter, mouse-leave
28124 properties in PLIST (and do something...). */
28125 hotspot = XCDR (hotspot);
28126 if (CONSP (hotspot)
28127 && (plist = XCAR (hotspot), CONSP (plist)))
28128 {
28129 pointer = Fplist_get (plist, Qpointer);
28130 if (NILP (pointer))
28131 pointer = Qhand;
28132 help_echo_string = Fplist_get (plist, Qhelp_echo);
28133 if (!NILP (help_echo_string))
28134 {
28135 help_echo_window = window;
28136 help_echo_object = glyph->object;
28137 help_echo_pos = glyph->charpos;
28138 }
28139 }
28140 }
28141 if (NILP (pointer))
28142 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28143 }
28144 }
28145 #endif /* HAVE_WINDOW_SYSTEM */
28146
28147 /* Clear mouse face if X/Y not over text. */
28148 if (glyph == NULL
28149 || area != TEXT_AREA
28150 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28151 /* Glyph's OBJECT is an integer for glyphs inserted by the
28152 display engine for its internal purposes, like truncation
28153 and continuation glyphs and blanks beyond the end of
28154 line's text on text terminals. If we are over such a
28155 glyph, we are not over any text. */
28156 || INTEGERP (glyph->object)
28157 /* R2L rows have a stretch glyph at their front, which
28158 stands for no text, whereas L2R rows have no glyphs at
28159 all beyond the end of text. Treat such stretch glyphs
28160 like we do with NULL glyphs in L2R rows. */
28161 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28162 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28163 && glyph->type == STRETCH_GLYPH
28164 && glyph->avoid_cursor_p))
28165 {
28166 if (clear_mouse_face (hlinfo))
28167 cursor = No_Cursor;
28168 #ifdef HAVE_WINDOW_SYSTEM
28169 if (FRAME_WINDOW_P (f) && NILP (pointer))
28170 {
28171 if (area != TEXT_AREA)
28172 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28173 else
28174 pointer = Vvoid_text_area_pointer;
28175 }
28176 #endif
28177 goto set_cursor;
28178 }
28179
28180 pos = glyph->charpos;
28181 object = glyph->object;
28182 if (!STRINGP (object) && !BUFFERP (object))
28183 goto set_cursor;
28184
28185 /* If we get an out-of-range value, return now; avoid an error. */
28186 if (BUFFERP (object) && pos > BUF_Z (b))
28187 goto set_cursor;
28188
28189 /* Make the window's buffer temporarily current for
28190 overlays_at and compute_char_face. */
28191 obuf = current_buffer;
28192 current_buffer = b;
28193 obegv = BEGV;
28194 ozv = ZV;
28195 BEGV = BEG;
28196 ZV = Z;
28197
28198 /* Is this char mouse-active or does it have help-echo? */
28199 position = make_number (pos);
28200
28201 if (BUFFERP (object))
28202 {
28203 /* Put all the overlays we want in a vector in overlay_vec. */
28204 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28205 /* Sort overlays into increasing priority order. */
28206 noverlays = sort_overlays (overlay_vec, noverlays, w);
28207 }
28208 else
28209 noverlays = 0;
28210
28211 if (NILP (Vmouse_highlight))
28212 {
28213 clear_mouse_face (hlinfo);
28214 goto check_help_echo;
28215 }
28216
28217 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28218
28219 if (same_region)
28220 cursor = No_Cursor;
28221
28222 /* Check mouse-face highlighting. */
28223 if (! same_region
28224 /* If there exists an overlay with mouse-face overlapping
28225 the one we are currently highlighting, we have to
28226 check if we enter the overlapping overlay, and then
28227 highlight only that. */
28228 || (OVERLAYP (hlinfo->mouse_face_overlay)
28229 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28230 {
28231 /* Find the highest priority overlay with a mouse-face. */
28232 Lisp_Object overlay = Qnil;
28233 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28234 {
28235 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28236 if (!NILP (mouse_face))
28237 overlay = overlay_vec[i];
28238 }
28239
28240 /* If we're highlighting the same overlay as before, there's
28241 no need to do that again. */
28242 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28243 goto check_help_echo;
28244 hlinfo->mouse_face_overlay = overlay;
28245
28246 /* Clear the display of the old active region, if any. */
28247 if (clear_mouse_face (hlinfo))
28248 cursor = No_Cursor;
28249
28250 /* If no overlay applies, get a text property. */
28251 if (NILP (overlay))
28252 mouse_face = Fget_text_property (position, Qmouse_face, object);
28253
28254 /* Next, compute the bounds of the mouse highlighting and
28255 display it. */
28256 if (!NILP (mouse_face) && STRINGP (object))
28257 {
28258 /* The mouse-highlighting comes from a display string
28259 with a mouse-face. */
28260 Lisp_Object s, e;
28261 ptrdiff_t ignore;
28262
28263 s = Fprevious_single_property_change
28264 (make_number (pos + 1), Qmouse_face, object, Qnil);
28265 e = Fnext_single_property_change
28266 (position, Qmouse_face, object, Qnil);
28267 if (NILP (s))
28268 s = make_number (0);
28269 if (NILP (e))
28270 e = make_number (SCHARS (object) - 1);
28271 mouse_face_from_string_pos (w, hlinfo, object,
28272 XINT (s), XINT (e));
28273 hlinfo->mouse_face_past_end = 0;
28274 hlinfo->mouse_face_window = window;
28275 hlinfo->mouse_face_face_id
28276 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28277 glyph->face_id, 1);
28278 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28279 cursor = No_Cursor;
28280 }
28281 else
28282 {
28283 /* The mouse-highlighting, if any, comes from an overlay
28284 or text property in the buffer. */
28285 Lisp_Object buffer IF_LINT (= Qnil);
28286 Lisp_Object disp_string IF_LINT (= Qnil);
28287
28288 if (STRINGP (object))
28289 {
28290 /* If we are on a display string with no mouse-face,
28291 check if the text under it has one. */
28292 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28293 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28294 pos = string_buffer_position (object, start);
28295 if (pos > 0)
28296 {
28297 mouse_face = get_char_property_and_overlay
28298 (make_number (pos), Qmouse_face, w->contents, &overlay);
28299 buffer = w->contents;
28300 disp_string = object;
28301 }
28302 }
28303 else
28304 {
28305 buffer = object;
28306 disp_string = Qnil;
28307 }
28308
28309 if (!NILP (mouse_face))
28310 {
28311 Lisp_Object before, after;
28312 Lisp_Object before_string, after_string;
28313 /* To correctly find the limits of mouse highlight
28314 in a bidi-reordered buffer, we must not use the
28315 optimization of limiting the search in
28316 previous-single-property-change and
28317 next-single-property-change, because
28318 rows_from_pos_range needs the real start and end
28319 positions to DTRT in this case. That's because
28320 the first row visible in a window does not
28321 necessarily display the character whose position
28322 is the smallest. */
28323 Lisp_Object lim1 =
28324 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28325 ? Fmarker_position (w->start)
28326 : Qnil;
28327 Lisp_Object lim2 =
28328 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28329 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28330 : Qnil;
28331
28332 if (NILP (overlay))
28333 {
28334 /* Handle the text property case. */
28335 before = Fprevious_single_property_change
28336 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28337 after = Fnext_single_property_change
28338 (make_number (pos), Qmouse_face, buffer, lim2);
28339 before_string = after_string = Qnil;
28340 }
28341 else
28342 {
28343 /* Handle the overlay case. */
28344 before = Foverlay_start (overlay);
28345 after = Foverlay_end (overlay);
28346 before_string = Foverlay_get (overlay, Qbefore_string);
28347 after_string = Foverlay_get (overlay, Qafter_string);
28348
28349 if (!STRINGP (before_string)) before_string = Qnil;
28350 if (!STRINGP (after_string)) after_string = Qnil;
28351 }
28352
28353 mouse_face_from_buffer_pos (window, hlinfo, pos,
28354 NILP (before)
28355 ? 1
28356 : XFASTINT (before),
28357 NILP (after)
28358 ? BUF_Z (XBUFFER (buffer))
28359 : XFASTINT (after),
28360 before_string, after_string,
28361 disp_string);
28362 cursor = No_Cursor;
28363 }
28364 }
28365 }
28366
28367 check_help_echo:
28368
28369 /* Look for a `help-echo' property. */
28370 if (NILP (help_echo_string)) {
28371 Lisp_Object help, overlay;
28372
28373 /* Check overlays first. */
28374 help = overlay = Qnil;
28375 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28376 {
28377 overlay = overlay_vec[i];
28378 help = Foverlay_get (overlay, Qhelp_echo);
28379 }
28380
28381 if (!NILP (help))
28382 {
28383 help_echo_string = help;
28384 help_echo_window = window;
28385 help_echo_object = overlay;
28386 help_echo_pos = pos;
28387 }
28388 else
28389 {
28390 Lisp_Object obj = glyph->object;
28391 ptrdiff_t charpos = glyph->charpos;
28392
28393 /* Try text properties. */
28394 if (STRINGP (obj)
28395 && charpos >= 0
28396 && charpos < SCHARS (obj))
28397 {
28398 help = Fget_text_property (make_number (charpos),
28399 Qhelp_echo, obj);
28400 if (NILP (help))
28401 {
28402 /* If the string itself doesn't specify a help-echo,
28403 see if the buffer text ``under'' it does. */
28404 struct glyph_row *r
28405 = MATRIX_ROW (w->current_matrix, vpos);
28406 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28407 ptrdiff_t p = string_buffer_position (obj, start);
28408 if (p > 0)
28409 {
28410 help = Fget_char_property (make_number (p),
28411 Qhelp_echo, w->contents);
28412 if (!NILP (help))
28413 {
28414 charpos = p;
28415 obj = w->contents;
28416 }
28417 }
28418 }
28419 }
28420 else if (BUFFERP (obj)
28421 && charpos >= BEGV
28422 && charpos < ZV)
28423 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28424 obj);
28425
28426 if (!NILP (help))
28427 {
28428 help_echo_string = help;
28429 help_echo_window = window;
28430 help_echo_object = obj;
28431 help_echo_pos = charpos;
28432 }
28433 }
28434 }
28435
28436 #ifdef HAVE_WINDOW_SYSTEM
28437 /* Look for a `pointer' property. */
28438 if (FRAME_WINDOW_P (f) && NILP (pointer))
28439 {
28440 /* Check overlays first. */
28441 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28442 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28443
28444 if (NILP (pointer))
28445 {
28446 Lisp_Object obj = glyph->object;
28447 ptrdiff_t charpos = glyph->charpos;
28448
28449 /* Try text properties. */
28450 if (STRINGP (obj)
28451 && charpos >= 0
28452 && charpos < SCHARS (obj))
28453 {
28454 pointer = Fget_text_property (make_number (charpos),
28455 Qpointer, obj);
28456 if (NILP (pointer))
28457 {
28458 /* If the string itself doesn't specify a pointer,
28459 see if the buffer text ``under'' it does. */
28460 struct glyph_row *r
28461 = MATRIX_ROW (w->current_matrix, vpos);
28462 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28463 ptrdiff_t p = string_buffer_position (obj, start);
28464 if (p > 0)
28465 pointer = Fget_char_property (make_number (p),
28466 Qpointer, w->contents);
28467 }
28468 }
28469 else if (BUFFERP (obj)
28470 && charpos >= BEGV
28471 && charpos < ZV)
28472 pointer = Fget_text_property (make_number (charpos),
28473 Qpointer, obj);
28474 }
28475 }
28476 #endif /* HAVE_WINDOW_SYSTEM */
28477
28478 BEGV = obegv;
28479 ZV = ozv;
28480 current_buffer = obuf;
28481 }
28482
28483 set_cursor:
28484
28485 #ifdef HAVE_WINDOW_SYSTEM
28486 if (FRAME_WINDOW_P (f))
28487 define_frame_cursor1 (f, cursor, pointer);
28488 #else
28489 /* This is here to prevent a compiler error, about "label at end of
28490 compound statement". */
28491 return;
28492 #endif
28493 }
28494
28495
28496 /* EXPORT for RIF:
28497 Clear any mouse-face on window W. This function is part of the
28498 redisplay interface, and is called from try_window_id and similar
28499 functions to ensure the mouse-highlight is off. */
28500
28501 void
28502 x_clear_window_mouse_face (struct window *w)
28503 {
28504 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28505 Lisp_Object window;
28506
28507 block_input ();
28508 XSETWINDOW (window, w);
28509 if (EQ (window, hlinfo->mouse_face_window))
28510 clear_mouse_face (hlinfo);
28511 unblock_input ();
28512 }
28513
28514
28515 /* EXPORT:
28516 Just discard the mouse face information for frame F, if any.
28517 This is used when the size of F is changed. */
28518
28519 void
28520 cancel_mouse_face (struct frame *f)
28521 {
28522 Lisp_Object window;
28523 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28524
28525 window = hlinfo->mouse_face_window;
28526 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28527 reset_mouse_highlight (hlinfo);
28528 }
28529
28530
28531 \f
28532 /***********************************************************************
28533 Exposure Events
28534 ***********************************************************************/
28535
28536 #ifdef HAVE_WINDOW_SYSTEM
28537
28538 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28539 which intersects rectangle R. R is in window-relative coordinates. */
28540
28541 static void
28542 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28543 enum glyph_row_area area)
28544 {
28545 struct glyph *first = row->glyphs[area];
28546 struct glyph *end = row->glyphs[area] + row->used[area];
28547 struct glyph *last;
28548 int first_x, start_x, x;
28549
28550 if (area == TEXT_AREA && row->fill_line_p)
28551 /* If row extends face to end of line write the whole line. */
28552 draw_glyphs (w, 0, row, area,
28553 0, row->used[area],
28554 DRAW_NORMAL_TEXT, 0);
28555 else
28556 {
28557 /* Set START_X to the window-relative start position for drawing glyphs of
28558 AREA. The first glyph of the text area can be partially visible.
28559 The first glyphs of other areas cannot. */
28560 start_x = window_box_left_offset (w, area);
28561 x = start_x;
28562 if (area == TEXT_AREA)
28563 x += row->x;
28564
28565 /* Find the first glyph that must be redrawn. */
28566 while (first < end
28567 && x + first->pixel_width < r->x)
28568 {
28569 x += first->pixel_width;
28570 ++first;
28571 }
28572
28573 /* Find the last one. */
28574 last = first;
28575 first_x = x;
28576 while (last < end
28577 && x < r->x + r->width)
28578 {
28579 x += last->pixel_width;
28580 ++last;
28581 }
28582
28583 /* Repaint. */
28584 if (last > first)
28585 draw_glyphs (w, first_x - start_x, row, area,
28586 first - row->glyphs[area], last - row->glyphs[area],
28587 DRAW_NORMAL_TEXT, 0);
28588 }
28589 }
28590
28591
28592 /* Redraw the parts of the glyph row ROW on window W intersecting
28593 rectangle R. R is in window-relative coordinates. Value is
28594 non-zero if mouse-face was overwritten. */
28595
28596 static int
28597 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28598 {
28599 eassert (row->enabled_p);
28600
28601 if (row->mode_line_p || w->pseudo_window_p)
28602 draw_glyphs (w, 0, row, TEXT_AREA,
28603 0, row->used[TEXT_AREA],
28604 DRAW_NORMAL_TEXT, 0);
28605 else
28606 {
28607 if (row->used[LEFT_MARGIN_AREA])
28608 expose_area (w, row, r, LEFT_MARGIN_AREA);
28609 if (row->used[TEXT_AREA])
28610 expose_area (w, row, r, TEXT_AREA);
28611 if (row->used[RIGHT_MARGIN_AREA])
28612 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28613 draw_row_fringe_bitmaps (w, row);
28614 }
28615
28616 return row->mouse_face_p;
28617 }
28618
28619
28620 /* Redraw those parts of glyphs rows during expose event handling that
28621 overlap other rows. Redrawing of an exposed line writes over parts
28622 of lines overlapping that exposed line; this function fixes that.
28623
28624 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28625 row in W's current matrix that is exposed and overlaps other rows.
28626 LAST_OVERLAPPING_ROW is the last such row. */
28627
28628 static void
28629 expose_overlaps (struct window *w,
28630 struct glyph_row *first_overlapping_row,
28631 struct glyph_row *last_overlapping_row,
28632 XRectangle *r)
28633 {
28634 struct glyph_row *row;
28635
28636 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28637 if (row->overlapping_p)
28638 {
28639 eassert (row->enabled_p && !row->mode_line_p);
28640
28641 row->clip = r;
28642 if (row->used[LEFT_MARGIN_AREA])
28643 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28644
28645 if (row->used[TEXT_AREA])
28646 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28647
28648 if (row->used[RIGHT_MARGIN_AREA])
28649 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28650 row->clip = NULL;
28651 }
28652 }
28653
28654
28655 /* Return non-zero if W's cursor intersects rectangle R. */
28656
28657 static int
28658 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28659 {
28660 XRectangle cr, result;
28661 struct glyph *cursor_glyph;
28662 struct glyph_row *row;
28663
28664 if (w->phys_cursor.vpos >= 0
28665 && w->phys_cursor.vpos < w->current_matrix->nrows
28666 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28667 row->enabled_p)
28668 && row->cursor_in_fringe_p)
28669 {
28670 /* Cursor is in the fringe. */
28671 cr.x = window_box_right_offset (w,
28672 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28673 ? RIGHT_MARGIN_AREA
28674 : TEXT_AREA));
28675 cr.y = row->y;
28676 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28677 cr.height = row->height;
28678 return x_intersect_rectangles (&cr, r, &result);
28679 }
28680
28681 cursor_glyph = get_phys_cursor_glyph (w);
28682 if (cursor_glyph)
28683 {
28684 /* r is relative to W's box, but w->phys_cursor.x is relative
28685 to left edge of W's TEXT area. Adjust it. */
28686 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28687 cr.y = w->phys_cursor.y;
28688 cr.width = cursor_glyph->pixel_width;
28689 cr.height = w->phys_cursor_height;
28690 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28691 I assume the effect is the same -- and this is portable. */
28692 return x_intersect_rectangles (&cr, r, &result);
28693 }
28694 /* If we don't understand the format, pretend we're not in the hot-spot. */
28695 return 0;
28696 }
28697
28698
28699 /* EXPORT:
28700 Draw a vertical window border to the right of window W if W doesn't
28701 have vertical scroll bars. */
28702
28703 void
28704 x_draw_vertical_border (struct window *w)
28705 {
28706 struct frame *f = XFRAME (WINDOW_FRAME (w));
28707
28708 /* We could do better, if we knew what type of scroll-bar the adjacent
28709 windows (on either side) have... But we don't :-(
28710 However, I think this works ok. ++KFS 2003-04-25 */
28711
28712 /* Redraw borders between horizontally adjacent windows. Don't
28713 do it for frames with vertical scroll bars because either the
28714 right scroll bar of a window, or the left scroll bar of its
28715 neighbor will suffice as a border. */
28716 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28717 return;
28718
28719 /* Note: It is necessary to redraw both the left and the right
28720 borders, for when only this single window W is being
28721 redisplayed. */
28722 if (!WINDOW_RIGHTMOST_P (w)
28723 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28724 {
28725 int x0, x1, y0, y1;
28726
28727 window_box_edges (w, &x0, &y0, &x1, &y1);
28728 y1 -= 1;
28729
28730 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28731 x1 -= 1;
28732
28733 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28734 }
28735 if (!WINDOW_LEFTMOST_P (w)
28736 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28737 {
28738 int x0, x1, y0, y1;
28739
28740 window_box_edges (w, &x0, &y0, &x1, &y1);
28741 y1 -= 1;
28742
28743 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28744 x0 -= 1;
28745
28746 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28747 }
28748 }
28749
28750
28751 /* Redraw the part of window W intersection rectangle FR. Pixel
28752 coordinates in FR are frame-relative. Call this function with
28753 input blocked. Value is non-zero if the exposure overwrites
28754 mouse-face. */
28755
28756 static int
28757 expose_window (struct window *w, XRectangle *fr)
28758 {
28759 struct frame *f = XFRAME (w->frame);
28760 XRectangle wr, r;
28761 int mouse_face_overwritten_p = 0;
28762
28763 /* If window is not yet fully initialized, do nothing. This can
28764 happen when toolkit scroll bars are used and a window is split.
28765 Reconfiguring the scroll bar will generate an expose for a newly
28766 created window. */
28767 if (w->current_matrix == NULL)
28768 return 0;
28769
28770 /* When we're currently updating the window, display and current
28771 matrix usually don't agree. Arrange for a thorough display
28772 later. */
28773 if (w->must_be_updated_p)
28774 {
28775 SET_FRAME_GARBAGED (f);
28776 return 0;
28777 }
28778
28779 /* Frame-relative pixel rectangle of W. */
28780 wr.x = WINDOW_LEFT_EDGE_X (w);
28781 wr.y = WINDOW_TOP_EDGE_Y (w);
28782 wr.width = WINDOW_TOTAL_WIDTH (w);
28783 wr.height = WINDOW_TOTAL_HEIGHT (w);
28784
28785 if (x_intersect_rectangles (fr, &wr, &r))
28786 {
28787 int yb = window_text_bottom_y (w);
28788 struct glyph_row *row;
28789 int cursor_cleared_p, phys_cursor_on_p;
28790 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28791
28792 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28793 r.x, r.y, r.width, r.height));
28794
28795 /* Convert to window coordinates. */
28796 r.x -= WINDOW_LEFT_EDGE_X (w);
28797 r.y -= WINDOW_TOP_EDGE_Y (w);
28798
28799 /* Turn off the cursor. */
28800 if (!w->pseudo_window_p
28801 && phys_cursor_in_rect_p (w, &r))
28802 {
28803 x_clear_cursor (w);
28804 cursor_cleared_p = 1;
28805 }
28806 else
28807 cursor_cleared_p = 0;
28808
28809 /* If the row containing the cursor extends face to end of line,
28810 then expose_area might overwrite the cursor outside the
28811 rectangle and thus notice_overwritten_cursor might clear
28812 w->phys_cursor_on_p. We remember the original value and
28813 check later if it is changed. */
28814 phys_cursor_on_p = w->phys_cursor_on_p;
28815
28816 /* Update lines intersecting rectangle R. */
28817 first_overlapping_row = last_overlapping_row = NULL;
28818 for (row = w->current_matrix->rows;
28819 row->enabled_p;
28820 ++row)
28821 {
28822 int y0 = row->y;
28823 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28824
28825 if ((y0 >= r.y && y0 < r.y + r.height)
28826 || (y1 > r.y && y1 < r.y + r.height)
28827 || (r.y >= y0 && r.y < y1)
28828 || (r.y + r.height > y0 && r.y + r.height < y1))
28829 {
28830 /* A header line may be overlapping, but there is no need
28831 to fix overlapping areas for them. KFS 2005-02-12 */
28832 if (row->overlapping_p && !row->mode_line_p)
28833 {
28834 if (first_overlapping_row == NULL)
28835 first_overlapping_row = row;
28836 last_overlapping_row = row;
28837 }
28838
28839 row->clip = fr;
28840 if (expose_line (w, row, &r))
28841 mouse_face_overwritten_p = 1;
28842 row->clip = NULL;
28843 }
28844 else if (row->overlapping_p)
28845 {
28846 /* We must redraw a row overlapping the exposed area. */
28847 if (y0 < r.y
28848 ? y0 + row->phys_height > r.y
28849 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28850 {
28851 if (first_overlapping_row == NULL)
28852 first_overlapping_row = row;
28853 last_overlapping_row = row;
28854 }
28855 }
28856
28857 if (y1 >= yb)
28858 break;
28859 }
28860
28861 /* Display the mode line if there is one. */
28862 if (WINDOW_WANTS_MODELINE_P (w)
28863 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28864 row->enabled_p)
28865 && row->y < r.y + r.height)
28866 {
28867 if (expose_line (w, row, &r))
28868 mouse_face_overwritten_p = 1;
28869 }
28870
28871 if (!w->pseudo_window_p)
28872 {
28873 /* Fix the display of overlapping rows. */
28874 if (first_overlapping_row)
28875 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28876 fr);
28877
28878 /* Draw border between windows. */
28879 x_draw_vertical_border (w);
28880
28881 /* Turn the cursor on again. */
28882 if (cursor_cleared_p
28883 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28884 update_window_cursor (w, 1);
28885 }
28886 }
28887
28888 return mouse_face_overwritten_p;
28889 }
28890
28891
28892
28893 /* Redraw (parts) of all windows in the window tree rooted at W that
28894 intersect R. R contains frame pixel coordinates. Value is
28895 non-zero if the exposure overwrites mouse-face. */
28896
28897 static int
28898 expose_window_tree (struct window *w, XRectangle *r)
28899 {
28900 struct frame *f = XFRAME (w->frame);
28901 int mouse_face_overwritten_p = 0;
28902
28903 while (w && !FRAME_GARBAGED_P (f))
28904 {
28905 if (WINDOWP (w->contents))
28906 mouse_face_overwritten_p
28907 |= expose_window_tree (XWINDOW (w->contents), r);
28908 else
28909 mouse_face_overwritten_p |= expose_window (w, r);
28910
28911 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28912 }
28913
28914 return mouse_face_overwritten_p;
28915 }
28916
28917
28918 /* EXPORT:
28919 Redisplay an exposed area of frame F. X and Y are the upper-left
28920 corner of the exposed rectangle. W and H are width and height of
28921 the exposed area. All are pixel values. W or H zero means redraw
28922 the entire frame. */
28923
28924 void
28925 expose_frame (struct frame *f, int x, int y, int w, int h)
28926 {
28927 XRectangle r;
28928 int mouse_face_overwritten_p = 0;
28929
28930 TRACE ((stderr, "expose_frame "));
28931
28932 /* No need to redraw if frame will be redrawn soon. */
28933 if (FRAME_GARBAGED_P (f))
28934 {
28935 TRACE ((stderr, " garbaged\n"));
28936 return;
28937 }
28938
28939 /* If basic faces haven't been realized yet, there is no point in
28940 trying to redraw anything. This can happen when we get an expose
28941 event while Emacs is starting, e.g. by moving another window. */
28942 if (FRAME_FACE_CACHE (f) == NULL
28943 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28944 {
28945 TRACE ((stderr, " no faces\n"));
28946 return;
28947 }
28948
28949 if (w == 0 || h == 0)
28950 {
28951 r.x = r.y = 0;
28952 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28953 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28954 }
28955 else
28956 {
28957 r.x = x;
28958 r.y = y;
28959 r.width = w;
28960 r.height = h;
28961 }
28962
28963 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28964 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28965
28966 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28967 if (WINDOWP (f->tool_bar_window))
28968 mouse_face_overwritten_p
28969 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28970 #endif
28971
28972 #ifdef HAVE_X_WINDOWS
28973 #ifndef MSDOS
28974 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28975 if (WINDOWP (f->menu_bar_window))
28976 mouse_face_overwritten_p
28977 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28978 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28979 #endif
28980 #endif
28981
28982 /* Some window managers support a focus-follows-mouse style with
28983 delayed raising of frames. Imagine a partially obscured frame,
28984 and moving the mouse into partially obscured mouse-face on that
28985 frame. The visible part of the mouse-face will be highlighted,
28986 then the WM raises the obscured frame. With at least one WM, KDE
28987 2.1, Emacs is not getting any event for the raising of the frame
28988 (even tried with SubstructureRedirectMask), only Expose events.
28989 These expose events will draw text normally, i.e. not
28990 highlighted. Which means we must redo the highlight here.
28991 Subsume it under ``we love X''. --gerd 2001-08-15 */
28992 /* Included in Windows version because Windows most likely does not
28993 do the right thing if any third party tool offers
28994 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28995 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28996 {
28997 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28998 if (f == hlinfo->mouse_face_mouse_frame)
28999 {
29000 int mouse_x = hlinfo->mouse_face_mouse_x;
29001 int mouse_y = hlinfo->mouse_face_mouse_y;
29002 clear_mouse_face (hlinfo);
29003 note_mouse_highlight (f, mouse_x, mouse_y);
29004 }
29005 }
29006 }
29007
29008
29009 /* EXPORT:
29010 Determine the intersection of two rectangles R1 and R2. Return
29011 the intersection in *RESULT. Value is non-zero if RESULT is not
29012 empty. */
29013
29014 int
29015 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29016 {
29017 XRectangle *left, *right;
29018 XRectangle *upper, *lower;
29019 int intersection_p = 0;
29020
29021 /* Rearrange so that R1 is the left-most rectangle. */
29022 if (r1->x < r2->x)
29023 left = r1, right = r2;
29024 else
29025 left = r2, right = r1;
29026
29027 /* X0 of the intersection is right.x0, if this is inside R1,
29028 otherwise there is no intersection. */
29029 if (right->x <= left->x + left->width)
29030 {
29031 result->x = right->x;
29032
29033 /* The right end of the intersection is the minimum of
29034 the right ends of left and right. */
29035 result->width = (min (left->x + left->width, right->x + right->width)
29036 - result->x);
29037
29038 /* Same game for Y. */
29039 if (r1->y < r2->y)
29040 upper = r1, lower = r2;
29041 else
29042 upper = r2, lower = r1;
29043
29044 /* The upper end of the intersection is lower.y0, if this is inside
29045 of upper. Otherwise, there is no intersection. */
29046 if (lower->y <= upper->y + upper->height)
29047 {
29048 result->y = lower->y;
29049
29050 /* The lower end of the intersection is the minimum of the lower
29051 ends of upper and lower. */
29052 result->height = (min (lower->y + lower->height,
29053 upper->y + upper->height)
29054 - result->y);
29055 intersection_p = 1;
29056 }
29057 }
29058
29059 return intersection_p;
29060 }
29061
29062 #endif /* HAVE_WINDOW_SYSTEM */
29063
29064 \f
29065 /***********************************************************************
29066 Initialization
29067 ***********************************************************************/
29068
29069 void
29070 syms_of_xdisp (void)
29071 {
29072 Vwith_echo_area_save_vector = Qnil;
29073 staticpro (&Vwith_echo_area_save_vector);
29074
29075 Vmessage_stack = Qnil;
29076 staticpro (&Vmessage_stack);
29077
29078 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29079 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29080
29081 message_dolog_marker1 = Fmake_marker ();
29082 staticpro (&message_dolog_marker1);
29083 message_dolog_marker2 = Fmake_marker ();
29084 staticpro (&message_dolog_marker2);
29085 message_dolog_marker3 = Fmake_marker ();
29086 staticpro (&message_dolog_marker3);
29087
29088 #ifdef GLYPH_DEBUG
29089 defsubr (&Sdump_frame_glyph_matrix);
29090 defsubr (&Sdump_glyph_matrix);
29091 defsubr (&Sdump_glyph_row);
29092 defsubr (&Sdump_tool_bar_row);
29093 defsubr (&Strace_redisplay);
29094 defsubr (&Strace_to_stderr);
29095 #endif
29096 #ifdef HAVE_WINDOW_SYSTEM
29097 defsubr (&Stool_bar_lines_needed);
29098 defsubr (&Slookup_image_map);
29099 #endif
29100 defsubr (&Sline_pixel_height);
29101 defsubr (&Sformat_mode_line);
29102 defsubr (&Sinvisible_p);
29103 defsubr (&Scurrent_bidi_paragraph_direction);
29104 defsubr (&Smove_point_visually);
29105
29106 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29107 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29108 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29109 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29110 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29111 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29112 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29113 DEFSYM (Qeval, "eval");
29114 DEFSYM (QCdata, ":data");
29115 DEFSYM (Qdisplay, "display");
29116 DEFSYM (Qspace_width, "space-width");
29117 DEFSYM (Qraise, "raise");
29118 DEFSYM (Qslice, "slice");
29119 DEFSYM (Qspace, "space");
29120 DEFSYM (Qmargin, "margin");
29121 DEFSYM (Qpointer, "pointer");
29122 DEFSYM (Qleft_margin, "left-margin");
29123 DEFSYM (Qright_margin, "right-margin");
29124 DEFSYM (Qcenter, "center");
29125 DEFSYM (Qline_height, "line-height");
29126 DEFSYM (QCalign_to, ":align-to");
29127 DEFSYM (QCrelative_width, ":relative-width");
29128 DEFSYM (QCrelative_height, ":relative-height");
29129 DEFSYM (QCeval, ":eval");
29130 DEFSYM (QCpropertize, ":propertize");
29131 DEFSYM (QCfile, ":file");
29132 DEFSYM (Qfontified, "fontified");
29133 DEFSYM (Qfontification_functions, "fontification-functions");
29134 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29135 DEFSYM (Qescape_glyph, "escape-glyph");
29136 DEFSYM (Qnobreak_space, "nobreak-space");
29137 DEFSYM (Qimage, "image");
29138 DEFSYM (Qtext, "text");
29139 DEFSYM (Qboth, "both");
29140 DEFSYM (Qboth_horiz, "both-horiz");
29141 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29142 DEFSYM (QCmap, ":map");
29143 DEFSYM (QCpointer, ":pointer");
29144 DEFSYM (Qrect, "rect");
29145 DEFSYM (Qcircle, "circle");
29146 DEFSYM (Qpoly, "poly");
29147 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29148 DEFSYM (Qgrow_only, "grow-only");
29149 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29150 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29151 DEFSYM (Qposition, "position");
29152 DEFSYM (Qbuffer_position, "buffer-position");
29153 DEFSYM (Qobject, "object");
29154 DEFSYM (Qbar, "bar");
29155 DEFSYM (Qhbar, "hbar");
29156 DEFSYM (Qbox, "box");
29157 DEFSYM (Qhollow, "hollow");
29158 DEFSYM (Qhand, "hand");
29159 DEFSYM (Qarrow, "arrow");
29160 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29161
29162 list_of_error = list1 (list2 (intern_c_string ("error"),
29163 intern_c_string ("void-variable")));
29164 staticpro (&list_of_error);
29165
29166 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29167 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29168 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29169 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29170
29171 echo_buffer[0] = echo_buffer[1] = Qnil;
29172 staticpro (&echo_buffer[0]);
29173 staticpro (&echo_buffer[1]);
29174
29175 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29176 staticpro (&echo_area_buffer[0]);
29177 staticpro (&echo_area_buffer[1]);
29178
29179 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29180 staticpro (&Vmessages_buffer_name);
29181
29182 mode_line_proptrans_alist = Qnil;
29183 staticpro (&mode_line_proptrans_alist);
29184 mode_line_string_list = Qnil;
29185 staticpro (&mode_line_string_list);
29186 mode_line_string_face = Qnil;
29187 staticpro (&mode_line_string_face);
29188 mode_line_string_face_prop = Qnil;
29189 staticpro (&mode_line_string_face_prop);
29190 Vmode_line_unwind_vector = Qnil;
29191 staticpro (&Vmode_line_unwind_vector);
29192
29193 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29194
29195 help_echo_string = Qnil;
29196 staticpro (&help_echo_string);
29197 help_echo_object = Qnil;
29198 staticpro (&help_echo_object);
29199 help_echo_window = Qnil;
29200 staticpro (&help_echo_window);
29201 previous_help_echo_string = Qnil;
29202 staticpro (&previous_help_echo_string);
29203 help_echo_pos = -1;
29204
29205 DEFSYM (Qright_to_left, "right-to-left");
29206 DEFSYM (Qleft_to_right, "left-to-right");
29207
29208 #ifdef HAVE_WINDOW_SYSTEM
29209 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29210 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29211 For example, if a block cursor is over a tab, it will be drawn as
29212 wide as that tab on the display. */);
29213 x_stretch_cursor_p = 0;
29214 #endif
29215
29216 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29217 doc: /* Non-nil means highlight trailing whitespace.
29218 The face used for trailing whitespace is `trailing-whitespace'. */);
29219 Vshow_trailing_whitespace = Qnil;
29220
29221 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29222 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29223 If the value is t, Emacs highlights non-ASCII chars which have the
29224 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29225 or `escape-glyph' face respectively.
29226
29227 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29228 U+2011 (non-breaking hyphen) are affected.
29229
29230 Any other non-nil value means to display these characters as a escape
29231 glyph followed by an ordinary space or hyphen.
29232
29233 A value of nil means no special handling of these characters. */);
29234 Vnobreak_char_display = Qt;
29235
29236 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29237 doc: /* The pointer shape to show in void text areas.
29238 A value of nil means to show the text pointer. Other options are `arrow',
29239 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29240 Vvoid_text_area_pointer = Qarrow;
29241
29242 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29243 doc: /* Non-nil means don't actually do any redisplay.
29244 This is used for internal purposes. */);
29245 Vinhibit_redisplay = Qnil;
29246
29247 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29248 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29249 Vglobal_mode_string = Qnil;
29250
29251 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29252 doc: /* Marker for where to display an arrow on top of the buffer text.
29253 This must be the beginning of a line in order to work.
29254 See also `overlay-arrow-string'. */);
29255 Voverlay_arrow_position = Qnil;
29256
29257 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29258 doc: /* String to display as an arrow in non-window frames.
29259 See also `overlay-arrow-position'. */);
29260 Voverlay_arrow_string = build_pure_c_string ("=>");
29261
29262 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29263 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29264 The symbols on this list are examined during redisplay to determine
29265 where to display overlay arrows. */);
29266 Voverlay_arrow_variable_list
29267 = list1 (intern_c_string ("overlay-arrow-position"));
29268
29269 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29270 doc: /* The number of lines to try scrolling a window by when point moves out.
29271 If that fails to bring point back on frame, point is centered instead.
29272 If this is zero, point is always centered after it moves off frame.
29273 If you want scrolling to always be a line at a time, you should set
29274 `scroll-conservatively' to a large value rather than set this to 1. */);
29275
29276 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29277 doc: /* Scroll up to this many lines, to bring point back on screen.
29278 If point moves off-screen, redisplay will scroll by up to
29279 `scroll-conservatively' lines in order to bring point just barely
29280 onto the screen again. If that cannot be done, then redisplay
29281 recenters point as usual.
29282
29283 If the value is greater than 100, redisplay will never recenter point,
29284 but will always scroll just enough text to bring point into view, even
29285 if you move far away.
29286
29287 A value of zero means always recenter point if it moves off screen. */);
29288 scroll_conservatively = 0;
29289
29290 DEFVAR_INT ("scroll-margin", scroll_margin,
29291 doc: /* Number of lines of margin at the top and bottom of a window.
29292 Recenter the window whenever point gets within this many lines
29293 of the top or bottom of the window. */);
29294 scroll_margin = 0;
29295
29296 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29297 doc: /* Pixels per inch value for non-window system displays.
29298 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29299 Vdisplay_pixels_per_inch = make_float (72.0);
29300
29301 #ifdef GLYPH_DEBUG
29302 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29303 #endif
29304
29305 DEFVAR_LISP ("truncate-partial-width-windows",
29306 Vtruncate_partial_width_windows,
29307 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29308 For an integer value, truncate lines in each window narrower than the
29309 full frame width, provided the window width is less than that integer;
29310 otherwise, respect the value of `truncate-lines'.
29311
29312 For any other non-nil value, truncate lines in all windows that do
29313 not span the full frame width.
29314
29315 A value of nil means to respect the value of `truncate-lines'.
29316
29317 If `word-wrap' is enabled, you might want to reduce this. */);
29318 Vtruncate_partial_width_windows = make_number (50);
29319
29320 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29321 doc: /* Maximum buffer size for which line number should be displayed.
29322 If the buffer is bigger than this, the line number does not appear
29323 in the mode line. A value of nil means no limit. */);
29324 Vline_number_display_limit = Qnil;
29325
29326 DEFVAR_INT ("line-number-display-limit-width",
29327 line_number_display_limit_width,
29328 doc: /* Maximum line width (in characters) for line number display.
29329 If the average length of the lines near point is bigger than this, then the
29330 line number may be omitted from the mode line. */);
29331 line_number_display_limit_width = 200;
29332
29333 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29334 doc: /* Non-nil means highlight region even in nonselected windows. */);
29335 highlight_nonselected_windows = 0;
29336
29337 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29338 doc: /* Non-nil if more than one frame is visible on this display.
29339 Minibuffer-only frames don't count, but iconified frames do.
29340 This variable is not guaranteed to be accurate except while processing
29341 `frame-title-format' and `icon-title-format'. */);
29342
29343 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29344 doc: /* Template for displaying the title bar of visible frames.
29345 \(Assuming the window manager supports this feature.)
29346
29347 This variable has the same structure as `mode-line-format', except that
29348 the %c and %l constructs are ignored. It is used only on frames for
29349 which no explicit name has been set \(see `modify-frame-parameters'). */);
29350
29351 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29352 doc: /* Template for displaying the title bar of an iconified frame.
29353 \(Assuming the window manager supports this feature.)
29354 This variable has the same structure as `mode-line-format' (which see),
29355 and is used only on frames for which no explicit name has been set
29356 \(see `modify-frame-parameters'). */);
29357 Vicon_title_format
29358 = Vframe_title_format
29359 = listn (CONSTYPE_PURE, 3,
29360 intern_c_string ("multiple-frames"),
29361 build_pure_c_string ("%b"),
29362 listn (CONSTYPE_PURE, 4,
29363 empty_unibyte_string,
29364 intern_c_string ("invocation-name"),
29365 build_pure_c_string ("@"),
29366 intern_c_string ("system-name")));
29367
29368 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29369 doc: /* Maximum number of lines to keep in the message log buffer.
29370 If nil, disable message logging. If t, log messages but don't truncate
29371 the buffer when it becomes large. */);
29372 Vmessage_log_max = make_number (1000);
29373
29374 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29375 doc: /* Functions called before redisplay, if window sizes have changed.
29376 The value should be a list of functions that take one argument.
29377 Just before redisplay, for each frame, if any of its windows have changed
29378 size since the last redisplay, or have been split or deleted,
29379 all the functions in the list are called, with the frame as argument. */);
29380 Vwindow_size_change_functions = Qnil;
29381
29382 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29383 doc: /* List of functions to call before redisplaying a window with scrolling.
29384 Each function is called with two arguments, the window and its new
29385 display-start position. Note that these functions are also called by
29386 `set-window-buffer'. Also note that the value of `window-end' is not
29387 valid when these functions are called.
29388
29389 Warning: Do not use this feature to alter the way the window
29390 is scrolled. It is not designed for that, and such use probably won't
29391 work. */);
29392 Vwindow_scroll_functions = Qnil;
29393
29394 DEFVAR_LISP ("window-text-change-functions",
29395 Vwindow_text_change_functions,
29396 doc: /* Functions to call in redisplay when text in the window might change. */);
29397 Vwindow_text_change_functions = Qnil;
29398
29399 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29400 doc: /* Functions called when redisplay of a window reaches the end trigger.
29401 Each function is called with two arguments, the window and the end trigger value.
29402 See `set-window-redisplay-end-trigger'. */);
29403 Vredisplay_end_trigger_functions = Qnil;
29404
29405 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29406 doc: /* Non-nil means autoselect window with mouse pointer.
29407 If nil, do not autoselect windows.
29408 A positive number means delay autoselection by that many seconds: a
29409 window is autoselected only after the mouse has remained in that
29410 window for the duration of the delay.
29411 A negative number has a similar effect, but causes windows to be
29412 autoselected only after the mouse has stopped moving. \(Because of
29413 the way Emacs compares mouse events, you will occasionally wait twice
29414 that time before the window gets selected.\)
29415 Any other value means to autoselect window instantaneously when the
29416 mouse pointer enters it.
29417
29418 Autoselection selects the minibuffer only if it is active, and never
29419 unselects the minibuffer if it is active.
29420
29421 When customizing this variable make sure that the actual value of
29422 `focus-follows-mouse' matches the behavior of your window manager. */);
29423 Vmouse_autoselect_window = Qnil;
29424
29425 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29426 doc: /* Non-nil means automatically resize tool-bars.
29427 This dynamically changes the tool-bar's height to the minimum height
29428 that is needed to make all tool-bar items visible.
29429 If value is `grow-only', the tool-bar's height is only increased
29430 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29431 Vauto_resize_tool_bars = Qt;
29432
29433 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29434 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29435 auto_raise_tool_bar_buttons_p = 1;
29436
29437 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29438 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29439 make_cursor_line_fully_visible_p = 1;
29440
29441 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29442 doc: /* Border below tool-bar in pixels.
29443 If an integer, use it as the height of the border.
29444 If it is one of `internal-border-width' or `border-width', use the
29445 value of the corresponding frame parameter.
29446 Otherwise, no border is added below the tool-bar. */);
29447 Vtool_bar_border = Qinternal_border_width;
29448
29449 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29450 doc: /* Margin around tool-bar buttons in pixels.
29451 If an integer, use that for both horizontal and vertical margins.
29452 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29453 HORZ specifying the horizontal margin, and VERT specifying the
29454 vertical margin. */);
29455 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29456
29457 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29458 doc: /* Relief thickness of tool-bar buttons. */);
29459 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29460
29461 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29462 doc: /* Tool bar style to use.
29463 It can be one of
29464 image - show images only
29465 text - show text only
29466 both - show both, text below image
29467 both-horiz - show text to the right of the image
29468 text-image-horiz - show text to the left of the image
29469 any other - use system default or image if no system default.
29470
29471 This variable only affects the GTK+ toolkit version of Emacs. */);
29472 Vtool_bar_style = Qnil;
29473
29474 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29475 doc: /* Maximum number of characters a label can have to be shown.
29476 The tool bar style must also show labels for this to have any effect, see
29477 `tool-bar-style'. */);
29478 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29479
29480 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29481 doc: /* List of functions to call to fontify regions of text.
29482 Each function is called with one argument POS. Functions must
29483 fontify a region starting at POS in the current buffer, and give
29484 fontified regions the property `fontified'. */);
29485 Vfontification_functions = Qnil;
29486 Fmake_variable_buffer_local (Qfontification_functions);
29487
29488 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29489 unibyte_display_via_language_environment,
29490 doc: /* Non-nil means display unibyte text according to language environment.
29491 Specifically, this means that raw bytes in the range 160-255 decimal
29492 are displayed by converting them to the equivalent multibyte characters
29493 according to the current language environment. As a result, they are
29494 displayed according to the current fontset.
29495
29496 Note that this variable affects only how these bytes are displayed,
29497 but does not change the fact they are interpreted as raw bytes. */);
29498 unibyte_display_via_language_environment = 0;
29499
29500 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29501 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29502 If a float, it specifies a fraction of the mini-window frame's height.
29503 If an integer, it specifies a number of lines. */);
29504 Vmax_mini_window_height = make_float (0.25);
29505
29506 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29507 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29508 A value of nil means don't automatically resize mini-windows.
29509 A value of t means resize them to fit the text displayed in them.
29510 A value of `grow-only', the default, means let mini-windows grow only;
29511 they return to their normal size when the minibuffer is closed, or the
29512 echo area becomes empty. */);
29513 Vresize_mini_windows = Qgrow_only;
29514
29515 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29516 doc: /* Alist specifying how to blink the cursor off.
29517 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29518 `cursor-type' frame-parameter or variable equals ON-STATE,
29519 comparing using `equal', Emacs uses OFF-STATE to specify
29520 how to blink it off. ON-STATE and OFF-STATE are values for
29521 the `cursor-type' frame parameter.
29522
29523 If a frame's ON-STATE has no entry in this list,
29524 the frame's other specifications determine how to blink the cursor off. */);
29525 Vblink_cursor_alist = Qnil;
29526
29527 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29528 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29529 If non-nil, windows are automatically scrolled horizontally to make
29530 point visible. */);
29531 automatic_hscrolling_p = 1;
29532 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29533
29534 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29535 doc: /* How many columns away from the window edge point is allowed to get
29536 before automatic hscrolling will horizontally scroll the window. */);
29537 hscroll_margin = 5;
29538
29539 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29540 doc: /* How many columns to scroll the window when point gets too close to the edge.
29541 When point is less than `hscroll-margin' columns from the window
29542 edge, automatic hscrolling will scroll the window by the amount of columns
29543 determined by this variable. If its value is a positive integer, scroll that
29544 many columns. If it's a positive floating-point number, it specifies the
29545 fraction of the window's width to scroll. If it's nil or zero, point will be
29546 centered horizontally after the scroll. Any other value, including negative
29547 numbers, are treated as if the value were zero.
29548
29549 Automatic hscrolling always moves point outside the scroll margin, so if
29550 point was more than scroll step columns inside the margin, the window will
29551 scroll more than the value given by the scroll step.
29552
29553 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29554 and `scroll-right' overrides this variable's effect. */);
29555 Vhscroll_step = make_number (0);
29556
29557 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29558 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29559 Bind this around calls to `message' to let it take effect. */);
29560 message_truncate_lines = 0;
29561
29562 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29563 doc: /* Normal hook run to update the menu bar definitions.
29564 Redisplay runs this hook before it redisplays the menu bar.
29565 This is used to update submenus such as Buffers,
29566 whose contents depend on various data. */);
29567 Vmenu_bar_update_hook = Qnil;
29568
29569 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29570 doc: /* Frame for which we are updating a menu.
29571 The enable predicate for a menu binding should check this variable. */);
29572 Vmenu_updating_frame = Qnil;
29573
29574 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29575 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29576 inhibit_menubar_update = 0;
29577
29578 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29579 doc: /* Prefix prepended to all continuation lines at display time.
29580 The value may be a string, an image, or a stretch-glyph; it is
29581 interpreted in the same way as the value of a `display' text property.
29582
29583 This variable is overridden by any `wrap-prefix' text or overlay
29584 property.
29585
29586 To add a prefix to non-continuation lines, use `line-prefix'. */);
29587 Vwrap_prefix = Qnil;
29588 DEFSYM (Qwrap_prefix, "wrap-prefix");
29589 Fmake_variable_buffer_local (Qwrap_prefix);
29590
29591 DEFVAR_LISP ("line-prefix", Vline_prefix,
29592 doc: /* Prefix prepended to all non-continuation lines at display time.
29593 The value may be a string, an image, or a stretch-glyph; it is
29594 interpreted in the same way as the value of a `display' text property.
29595
29596 This variable is overridden by any `line-prefix' text or overlay
29597 property.
29598
29599 To add a prefix to continuation lines, use `wrap-prefix'. */);
29600 Vline_prefix = Qnil;
29601 DEFSYM (Qline_prefix, "line-prefix");
29602 Fmake_variable_buffer_local (Qline_prefix);
29603
29604 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29605 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29606 inhibit_eval_during_redisplay = 0;
29607
29608 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29609 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29610 inhibit_free_realized_faces = 0;
29611
29612 #ifdef GLYPH_DEBUG
29613 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29614 doc: /* Inhibit try_window_id display optimization. */);
29615 inhibit_try_window_id = 0;
29616
29617 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29618 doc: /* Inhibit try_window_reusing display optimization. */);
29619 inhibit_try_window_reusing = 0;
29620
29621 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29622 doc: /* Inhibit try_cursor_movement display optimization. */);
29623 inhibit_try_cursor_movement = 0;
29624 #endif /* GLYPH_DEBUG */
29625
29626 DEFVAR_INT ("overline-margin", overline_margin,
29627 doc: /* Space between overline and text, in pixels.
29628 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29629 margin to the character height. */);
29630 overline_margin = 2;
29631
29632 DEFVAR_INT ("underline-minimum-offset",
29633 underline_minimum_offset,
29634 doc: /* Minimum distance between baseline and underline.
29635 This can improve legibility of underlined text at small font sizes,
29636 particularly when using variable `x-use-underline-position-properties'
29637 with fonts that specify an UNDERLINE_POSITION relatively close to the
29638 baseline. The default value is 1. */);
29639 underline_minimum_offset = 1;
29640
29641 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29642 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29643 This feature only works when on a window system that can change
29644 cursor shapes. */);
29645 display_hourglass_p = 1;
29646
29647 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29648 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29649 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29650
29651 #ifdef HAVE_WINDOW_SYSTEM
29652 hourglass_atimer = NULL;
29653 hourglass_shown_p = 0;
29654 #endif /* HAVE_WINDOW_SYSTEM */
29655
29656 DEFSYM (Qglyphless_char, "glyphless-char");
29657 DEFSYM (Qhex_code, "hex-code");
29658 DEFSYM (Qempty_box, "empty-box");
29659 DEFSYM (Qthin_space, "thin-space");
29660 DEFSYM (Qzero_width, "zero-width");
29661
29662 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29663 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29664
29665 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29666 doc: /* Char-table defining glyphless characters.
29667 Each element, if non-nil, should be one of the following:
29668 an ASCII acronym string: display this string in a box
29669 `hex-code': display the hexadecimal code of a character in a box
29670 `empty-box': display as an empty box
29671 `thin-space': display as 1-pixel width space
29672 `zero-width': don't display
29673 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29674 display method for graphical terminals and text terminals respectively.
29675 GRAPHICAL and TEXT should each have one of the values listed above.
29676
29677 The char-table has one extra slot to control the display of a character for
29678 which no font is found. This slot only takes effect on graphical terminals.
29679 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29680 `thin-space'. The default is `empty-box'. */);
29681 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29682 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29683 Qempty_box);
29684
29685 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29686 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29687 Vdebug_on_message = Qnil;
29688 }
29689
29690
29691 /* Initialize this module when Emacs starts. */
29692
29693 void
29694 init_xdisp (void)
29695 {
29696 CHARPOS (this_line_start_pos) = 0;
29697
29698 if (!noninteractive)
29699 {
29700 struct window *m = XWINDOW (minibuf_window);
29701 Lisp_Object frame = m->frame;
29702 struct frame *f = XFRAME (frame);
29703 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29704 struct window *r = XWINDOW (root);
29705 int i;
29706
29707 echo_area_window = minibuf_window;
29708
29709 r->top_line = FRAME_TOP_MARGIN (f);
29710 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29711 r->total_cols = FRAME_COLS (f);
29712
29713 m->top_line = FRAME_LINES (f) - 1;
29714 m->total_lines = 1;
29715 m->total_cols = FRAME_COLS (f);
29716
29717 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29718 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29719 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29720
29721 /* The default ellipsis glyphs `...'. */
29722 for (i = 0; i < 3; ++i)
29723 default_invis_vector[i] = make_number ('.');
29724 }
29725
29726 {
29727 /* Allocate the buffer for frame titles.
29728 Also used for `format-mode-line'. */
29729 int size = 100;
29730 mode_line_noprop_buf = xmalloc (size);
29731 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29732 mode_line_noprop_ptr = mode_line_noprop_buf;
29733 mode_line_target = MODE_LINE_DISPLAY;
29734 }
29735
29736 help_echo_showing_p = 0;
29737 }
29738
29739 #ifdef HAVE_WINDOW_SYSTEM
29740
29741 /* Platform-independent portion of hourglass implementation. */
29742
29743 /* Cancel a currently active hourglass timer, and start a new one. */
29744 void
29745 start_hourglass (void)
29746 {
29747 struct timespec delay;
29748
29749 cancel_hourglass ();
29750
29751 if (INTEGERP (Vhourglass_delay)
29752 && XINT (Vhourglass_delay) > 0)
29753 delay = make_timespec (min (XINT (Vhourglass_delay),
29754 TYPE_MAXIMUM (time_t)),
29755 0);
29756 else if (FLOATP (Vhourglass_delay)
29757 && XFLOAT_DATA (Vhourglass_delay) > 0)
29758 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29759 else
29760 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29761
29762 #ifdef HAVE_NTGUI
29763 {
29764 extern void w32_note_current_window (void);
29765 w32_note_current_window ();
29766 }
29767 #endif /* HAVE_NTGUI */
29768
29769 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29770 show_hourglass, NULL);
29771 }
29772
29773
29774 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29775 shown. */
29776 void
29777 cancel_hourglass (void)
29778 {
29779 if (hourglass_atimer)
29780 {
29781 cancel_atimer (hourglass_atimer);
29782 hourglass_atimer = NULL;
29783 }
29784
29785 if (hourglass_shown_p)
29786 hide_hourglass ();
29787 }
29788
29789 #endif /* HAVE_WINDOW_SYSTEM */